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 */
409 static int flag_typed_selectors;
411 /* Store all constructed constant strings in a hash table so that
412 they get uniqued properly. */
414 struct GTY(()) string_descriptor {
415 /* The literal argument . */
418 /* The resulting constant string. */
422 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
424 FILE *gen_declaration_file;
426 /* Tells "encode_pointer/encode_aggregate" whether we are generating
427 type descriptors for instance variables (as opposed to methods).
428 Type descriptors for instance variables contain more information
429 than methods (for static typing and embedded structures). */
431 static int generating_instance_variables = 0;
433 /* For building an objc struct. These may not be used when this file
434 is compiled as part of obj-c++. */
436 static bool objc_building_struct;
437 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
439 /* Start building a struct for objc. */
442 objc_start_struct (tree name)
444 gcc_assert (!objc_building_struct);
445 objc_building_struct = true;
446 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
449 /* Finish building a struct for objc. */
452 objc_finish_struct (tree type, tree fieldlist)
454 gcc_assert (objc_building_struct);
455 objc_building_struct = false;
456 return finish_struct (input_location, type, fieldlist, NULL_TREE,
461 build_sized_array_type (tree base_type, int size)
463 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
464 return build_array_type (base_type, index_type);
468 add_field_decl (tree type, const char *name, tree **chain)
470 tree field = create_field_decl (type, name);
474 *chain = &DECL_CHAIN (field);
479 /* Create a temporary variable of type 'type'. If 'name' is set, uses
480 the specified name, else use no name. Returns the declaration of
481 the type. The 'name' is mostly useful for debugging.
484 objc_create_temporary_var (tree type, const char *name)
490 decl = build_decl (input_location,
491 VAR_DECL, get_identifier (name), type);
495 decl = build_decl (input_location,
496 VAR_DECL, NULL_TREE, type);
498 TREE_USED (decl) = 1;
499 DECL_ARTIFICIAL (decl) = 1;
500 DECL_IGNORED_P (decl) = 1;
501 DECL_CONTEXT (decl) = current_function_decl;
506 /* Some platforms pass small structures through registers versus
507 through an invisible pointer. Determine at what size structure is
508 the transition point between the two possibilities. */
511 generate_struct_by_value_array (void)
516 int aggregate_in_mem[32];
519 /* Presumably no platform passes 32 byte structures in a register. */
520 for (i = 1; i < 32; i++)
525 /* Create an unnamed struct that has `i' character components */
526 type = objc_start_struct (NULL_TREE);
528 strcpy (buffer, "c1");
529 decls = add_field_decl (char_type_node, buffer, &chain);
531 for (j = 1; j < i; j++)
533 sprintf (buffer, "c%d", j + 1);
534 add_field_decl (char_type_node, buffer, &chain);
536 objc_finish_struct (type, decls);
538 aggregate_in_mem[i] = aggregate_value_p (type, 0);
539 if (!aggregate_in_mem[i])
543 /* We found some structures that are returned in registers instead of memory
544 so output the necessary data. */
547 for (i = 31; i >= 0; i--)
548 if (!aggregate_in_mem[i])
550 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
552 /* The first member of the structure is always 0 because we don't handle
553 structures with 0 members */
554 printf ("static int struct_forward_array[] = {\n 0");
556 for (j = 1; j <= i; j++)
557 printf (", %d", aggregate_in_mem[j]);
568 if (cxx_init () == false)
570 if (c_objc_common_init () == false)
574 /* If gen_declaration desired, open the output file. */
575 if (flag_gen_declaration)
577 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
578 gen_declaration_file = fopen (dumpname, "w");
579 if (gen_declaration_file == 0)
580 fatal_error ("can%'t open %s: %m", dumpname);
584 if (flag_next_runtime)
586 TAG_GETCLASS = "objc_getClass";
587 TAG_GETMETACLASS = "objc_getMetaClass";
588 TAG_MSGSEND = "objc_msgSend";
589 TAG_MSGSENDSUPER = "objc_msgSendSuper";
590 TAG_MSGSEND_STRET = "objc_msgSend_stret";
591 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
592 default_constant_string_class_name = "NSConstantString";
596 TAG_GETCLASS = "objc_get_class";
597 TAG_GETMETACLASS = "objc_get_meta_class";
598 TAG_MSGSEND = "objc_msg_lookup";
599 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
600 /* GNU runtime does not provide special functions to support
601 structure-returning methods. */
602 default_constant_string_class_name = "NXConstantString";
603 flag_typed_selectors = 1;
604 /* GNU runtime does not need the compiler to change code
605 in order to do GC. */
608 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
615 if (print_struct_values && !flag_compare_debug)
616 generate_struct_by_value_array ();
621 /* This is called automatically (at the very end of compilation) by
622 c_write_global_declarations and cp_write_global_declarations. */
624 objc_write_global_declarations (void)
626 mark_referenced_methods ();
628 /* Finalize Objective-C runtime data. */
631 if (gen_declaration_file)
632 fclose (gen_declaration_file);
635 /* Return the first occurrence of a method declaration corresponding
636 to sel_name in rproto_list. Search rproto_list recursively.
637 If is_class is 0, search for instance methods, otherwise for class
640 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
645 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
647 p = TREE_VALUE (rproto);
650 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
652 /* First, search the @required protocol methods. */
654 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
656 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
661 /* If still not found, search the @optional protocol methods. */
663 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
665 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
670 /* If still not found, search the attached protocols. */
671 if (PROTOCOL_LIST (p))
672 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
679 ; /* An identifier...if we could not find a protocol. */
687 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
691 /* Make sure the protocol is supported by the object on the rhs. */
692 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
695 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
697 p = TREE_VALUE (rproto);
699 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
704 else if (PROTOCOL_LIST (p))
705 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
714 ; /* An identifier...if we could not find a protocol. */
721 objc_start_class_interface (tree klass, tree super_class,
722 tree protos, tree attributes)
724 if (flag_objc1_only && attributes)
725 error_at (input_location, "class attributes are not available in Objective-C 1.0");
727 objc_interface_context
729 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
730 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
734 objc_start_category_interface (tree klass, tree categ,
735 tree protos, tree attributes)
740 error_at (input_location, "category attributes are not available in Objective-C 1.0");
742 warning_at (input_location, OPT_Wattributes,
743 "category attributes are not available in this version"
744 " of the compiler, (ignored)");
746 objc_interface_context
747 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
749 = continue_class (objc_interface_context);
753 objc_start_protocol (tree name, tree protos, tree attributes)
755 if (flag_objc1_only && attributes)
756 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
758 objc_interface_context
759 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
760 objc_method_optional_flag = false;
764 objc_continue_interface (void)
767 = continue_class (objc_interface_context);
771 objc_finish_interface (void)
773 finish_class (objc_interface_context);
774 objc_interface_context = NULL_TREE;
775 objc_method_optional_flag = false;
779 objc_start_class_implementation (tree klass, tree super_class)
781 objc_implementation_context
783 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
785 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
789 objc_start_category_implementation (tree klass, tree categ)
791 objc_implementation_context
792 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
795 = continue_class (objc_implementation_context);
799 objc_continue_implementation (void)
802 = continue_class (objc_implementation_context);
806 objc_finish_implementation (void)
809 if (flag_objc_call_cxx_cdtors)
810 objc_generate_cxx_cdtors ();
813 if (objc_implementation_context)
815 finish_class (objc_implementation_context);
816 objc_ivar_chain = NULL_TREE;
817 objc_implementation_context = NULL_TREE;
820 warning (0, "%<@end%> must appear in an @implementation context");
824 objc_set_visibility (objc_ivar_visibility_kind visibility)
826 if (visibility == OBJC_IVAR_VIS_PACKAGE)
829 error ("%<@package%> is not available in Objective-C 1.0");
831 warning (0, "%<@package%> presently has the same effect as %<@public%>");
833 objc_ivar_visibility = visibility;
837 objc_set_method_opt (bool optional)
840 error_at (input_location, "@optional/@required are not available in Objective-C 1.0");
842 objc_method_optional_flag = optional;
843 if (!objc_interface_context
844 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
846 error ("@optional/@required is allowed in @protocol context only");
847 objc_method_optional_flag = false;
851 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
854 lookup_property_in_list (tree chain, tree property)
857 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
858 if (PROPERTY_NAME (x) == property)
863 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
864 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
867 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
869 tree p = TREE_VALUE (rproto);
870 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
872 if ((x = lookup_property_in_list (p, property)))
874 if (PROTOCOL_LIST (p))
875 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
879 ; /* An identifier...if we could not find a protocol. */
885 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
886 chain of interface hierarchy. */
888 lookup_property (tree interface_type, tree property)
890 tree inter = interface_type;
894 if ((x = lookup_property_in_list (inter, property)))
896 /* Failing that, look for the property in each category of the class. */
898 while ((category = CLASS_CATEGORY_LIST (category)))
900 if ((x = lookup_property_in_list (category, property)))
903 /* When checking a category, also check the protocols
904 attached with the category itself. */
905 if (CLASS_PROTOCOL_LIST (category)
906 && (x = lookup_property_in_protocol_list
907 (CLASS_PROTOCOL_LIST (category), property)))
911 /* Failing to find in categories, look for property in protocol list. */
912 if (CLASS_PROTOCOL_LIST (inter)
913 && (x = lookup_property_in_protocol_list
914 (CLASS_PROTOCOL_LIST (inter), property)))
917 /* Failing that, climb up the inheritance hierarchy. */
918 inter = lookup_interface (CLASS_SUPER_NAME (inter));
923 /* This routine is called by the parser when a
924 @property... declaration is found. 'decl' is the declaration of
925 the property (type/identifier), and the other arguments represent
926 property attributes that may have been specified in the Objective-C
927 declaration. 'parsed_property_readonly' is 'true' if the attribute
928 'readonly' was specified, and 'false' if not; similarly for the
929 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
930 if the attribute 'getter' was not specified, and is the identifier
931 corresponding to the specified getter if it was; similarly for
932 'parsed_property_setter_ident'. */
934 objc_add_property_declaration (location_t location, tree decl,
935 bool parsed_property_readonly, bool parsed_property_readwrite,
936 bool parsed_property_assign, bool parsed_property_retain,
937 bool parsed_property_copy, bool parsed_property_nonatomic,
938 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
942 /* 'property_readonly' and 'property_assign_semantics' are the final
943 attributes of the property after all parsed attributes have been
944 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
945 parsed_property_readonly = false and parsed_property_readwrite =
946 false, then property_readonly will be false because the default
948 bool property_readonly = false;
949 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
952 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
954 if (parsed_property_readonly && parsed_property_readwrite)
956 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
957 /* In case of conflicting attributes (here and below), after
958 producing an error, we pick one of the attributes and keep
960 property_readonly = false;
964 if (parsed_property_readonly)
965 property_readonly = true;
967 if (parsed_property_readwrite)
968 property_readonly = false;
971 if (parsed_property_readonly && parsed_property_setter_ident)
973 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
974 property_readonly = false;
977 if (parsed_property_assign && parsed_property_retain)
979 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
980 property_assign_semantics = OBJC_PROPERTY_RETAIN;
982 else if (parsed_property_assign && parsed_property_copy)
984 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
985 property_assign_semantics = OBJC_PROPERTY_COPY;
987 else if (parsed_property_retain && parsed_property_copy)
989 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
990 property_assign_semantics = OBJC_PROPERTY_COPY;
994 if (parsed_property_assign)
995 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
997 if (parsed_property_retain)
998 property_assign_semantics = OBJC_PROPERTY_RETAIN;
1000 if (parsed_property_copy)
1001 property_assign_semantics = OBJC_PROPERTY_COPY;
1004 if (!objc_interface_context)
1006 error_at (location, "property declaration not in @interface or @protocol context");
1010 /* At this point we know that we are either in an interface, a
1011 category, or a protocol. */
1013 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
1014 something else, as that would confuse the checks below. */
1015 if (TREE_CODE (decl) != FIELD_DECL)
1017 error_at (location, "invalid property declaration");
1021 /* Do some spot-checks for the most obvious invalid types. */
1023 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1025 error_at (location, "property can not be an array");
1029 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
1030 parsing, while the C/ObjC parser accepts it and gives us a
1031 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
1032 to check for a bitfield when doing ObjC. */
1034 if (DECL_INITIAL (decl))
1036 /* A @property is not an actual variable, but it is a way to
1037 describe a pair of accessor methods, so its type (which is
1038 the type of the return value of the getter and the first
1039 argument of the setter) can't be a bitfield (as return values
1040 and arguments of functions can not be bitfields). The
1041 underlying instance variable could be a bitfield, but that is
1042 a different matter. */
1043 error_at (location, "property can not be a bit-field");
1048 /* TODO: Check that the property type is an Objective-C object or a
1051 /* Implement -Wproperty-assign-default (which is enabled by default). */
1052 if (warn_property_assign_default
1053 /* If garbage collection is not being used, then 'assign' is
1054 valid for objects (and typically used for delegates) but it
1055 is wrong in most cases (since most objects need to be
1056 retained or copied in setters). Warn users when 'assign' is
1058 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
1059 /* Read-only properties are never assigned, so the assignment
1060 semantics do not matter in that case. */
1061 && !property_readonly
1064 /* Please note that it would make sense to default to 'assign'
1065 for non-{Objective-C objects}, and to 'retain' for
1066 Objective-C objects. But that would break compatibility with
1068 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
1070 /* Use 'false' so we do not warn for Class objects. */
1071 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
1073 warning_at (location,
1075 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
1078 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
1083 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
1084 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1085 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
1087 if (property_assign_semantics == OBJC_PROPERTY_COPY
1088 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1089 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
1091 /* Now determine the final property getter and setter names. They
1092 will be stored in the PROPERTY_DECL, from which they'll always be
1093 extracted and used. */
1095 /* Adjust, or fill in, setter and getter names. We overwrite the
1096 parsed_property_setter_ident and parsed_property_getter_ident
1097 with the final setter and getter identifiers that will be
1099 if (parsed_property_setter_ident)
1101 /* The setter should be terminated by ':', but the parser only
1102 gives us an identifier without ':'. So, we need to add ':'
1104 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1105 size_t length = strlen (parsed_setter);
1106 char *final_setter = (char *)alloca (length + 2);
1108 sprintf (final_setter, "%s:", parsed_setter);
1109 parsed_property_setter_ident = get_identifier (final_setter);
1113 if (!property_readonly)
1114 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1115 (DECL_NAME (decl)));
1118 if (!parsed_property_getter_ident)
1119 parsed_property_getter_ident = DECL_NAME (decl);
1121 /* Check for duplicate property declarations. We first check the
1122 immediate context for a property with the same name. Any such
1123 declarations are an error. */
1124 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1126 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1128 location_t original_location = DECL_SOURCE_LOCATION (x);
1130 error_at (location, "redeclaration of property %qD", decl);
1132 if (original_location != UNKNOWN_LOCATION)
1133 inform (original_location, "originally specified here");
1138 /* We now need to check for existing property declarations (in the
1139 superclass, other categories or protocols) and check that the new
1140 declaration is not in conflict with existing ones. */
1142 /* Search for a previous, existing declaration of a property with
1143 the same name in superclasses, protocols etc. If one is found,
1144 it will be in the 'x' variable. */
1147 /* Note that, for simplicity, the following may search again the
1148 local context. That's Ok as nothing will be found (else we'd
1149 have thrown an error above); it's only a little inefficient, but
1150 the code is simpler. */
1151 switch (TREE_CODE (objc_interface_context))
1153 case CLASS_INTERFACE_TYPE:
1154 /* Look up the property in the current @interface (which will
1155 find nothing), then its protocols and categories and
1157 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1159 case CATEGORY_INTERFACE_TYPE:
1160 /* Look up the property in the main @interface, then protocols
1161 and categories (one of them is ours, and will find nothing)
1162 and superclasses. */
1163 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1166 case PROTOCOL_INTERFACE_TYPE:
1167 /* Looks up the property in any protocols attached to the
1168 current protocol. */
1169 if (PROTOCOL_LIST (objc_interface_context))
1171 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1181 /* An existing property was found; check that it has the same
1182 types, or it is compatible. */
1183 location_t original_location = DECL_SOURCE_LOCATION (x);
1185 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1187 warning_at (location, 0,
1188 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1190 if (original_location != UNKNOWN_LOCATION)
1191 inform (original_location, "originally specified here");
1195 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1197 warning_at (location, 0,
1198 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1200 if (original_location != UNKNOWN_LOCATION)
1201 inform (original_location, "originally specified here");
1205 /* We can only compare the setter names if both the old and new property have a setter. */
1206 if (!property_readonly && !PROPERTY_READONLY(x))
1208 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1210 warning_at (location, 0,
1211 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1213 if (original_location != UNKNOWN_LOCATION)
1214 inform (original_location, "originally specified here");
1219 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1221 warning_at (location, 0,
1222 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1224 if (original_location != UNKNOWN_LOCATION)
1225 inform (original_location, "originally specified here");
1229 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1230 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1232 warning_at (location, 0,
1233 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1235 if (original_location != UNKNOWN_LOCATION)
1236 inform (original_location, "originally specified here");
1240 /* We now check that the new and old property declarations have
1241 the same types (or compatible one). In the Objective-C
1242 tradition of loose type checking, we do type-checking but
1243 only generate warnings (not errors) if they do not match.
1244 For non-readonly properties, the types must match exactly;
1245 for readonly properties, it is allowed to use a "more
1246 specialized" type in the new property declaration. Eg, the
1247 superclass has a getter returning (NSArray *) and the
1248 subclass a getter returning (NSMutableArray *). The object's
1249 getter returns an (NSMutableArray *); but if you cast the
1250 object to the superclass, which is allowed, you'd still
1251 expect the getter to return an (NSArray *), which works since
1252 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1253 objects belonging to the type of the new @property should be
1254 a subset of the set of objects belonging to the type of the
1255 old @property. This is what "specialization" means. And the
1256 reason it only applies to readonly properties is that for a
1257 readwrite property the setter would have the opposite
1258 requirement - ie that the superclass type is more specialized
1259 then the subclass one; hence the only way to satisfy both
1260 constraints is that the types match. */
1262 /* If the types are not the same in the C sense, we warn ... */
1263 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1264 /* ... unless the property is readonly, in which case we
1265 allow a new, more specialized, declaration. */
1266 && (!property_readonly
1267 || !objc_compare_types (TREE_TYPE (x),
1268 TREE_TYPE (decl), -5, NULL_TREE)))
1270 warning_at (location, 0,
1271 "type of property %qD conflicts with previous declaration", decl);
1272 if (original_location != UNKNOWN_LOCATION)
1273 inform (original_location, "originally specified here");
1278 /* Create a PROPERTY_DECL node. */
1279 property_decl = make_node (PROPERTY_DECL);
1281 /* Copy the basic information from the original decl. */
1282 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1283 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1284 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1286 /* Add property-specific information. */
1287 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1288 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1289 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1290 PROPERTY_READONLY (property_decl) = property_readonly;
1291 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1292 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1293 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1294 PROPERTY_DYNAMIC (property_decl) = 0;
1296 /* Note that PROPERTY_GETTER_NAME is always set for all
1297 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1298 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1299 with a getter or setter, we should get the PROPERTY_DECL and use
1300 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1303 /* Add the PROPERTY_DECL to the list of properties for the class. */
1304 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1305 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1308 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1309 list of methods in the interface (and, failing that, the local list
1310 in the implementation, and failing that, the protocol list)
1311 provided for a 'setter' or 'getter' for 'component' with default
1312 names (ie, if 'component' is "name", then search for "name" and
1313 "setName:"). If any is found, then create an artificial property
1314 that uses them. Return NULL_TREE if 'getter' or 'setter' could not
1317 maybe_make_artificial_property_decl (tree interface, tree implementation,
1318 tree protocol_list, tree component, bool is_class)
1320 tree getter_name = component;
1321 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1322 tree getter = NULL_TREE;
1323 tree setter = NULL_TREE;
1325 /* First, check the @interface and all superclasses. */
1330 /* Using instance methods of the root class as accessors is most
1331 likely unwanted and can be extremely confusing (and, most
1332 importantly, other Objective-C 2.0 compilers do not do it).
1335 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1337 getter = lookup_method_static (interface, getter_name, flags);
1338 setter = lookup_method_static (interface, setter_name, flags);
1341 /* Second, check the local @implementation context. */
1342 if (!getter && !setter)
1348 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1349 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1353 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1354 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1359 /* Try the protocol_list if we didn't find anything in the
1360 @interface and in the @implementation. */
1361 if (!getter && !setter)
1363 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1364 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1367 /* There needs to be at least a getter or setter for this to be a
1368 valid 'object.component' syntax. */
1369 if (getter || setter)
1371 /* Yes ... determine the type of the expression. */
1376 type = TREE_VALUE (TREE_TYPE (getter));
1378 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1380 /* Create an artificial property declaration with the
1381 information we collected on the type and getter/setter
1383 property_decl = make_node (PROPERTY_DECL);
1385 TREE_TYPE (property_decl) = type;
1386 DECL_SOURCE_LOCATION (property_decl) = input_location;
1387 TREE_DEPRECATED (property_decl) = 0;
1388 DECL_ARTIFICIAL (property_decl) = 1;
1390 /* Add property-specific information. Note that one of
1391 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1392 non-existing method; this will generate an error when the
1393 expression is later compiled. At this stage we don't know if
1394 the getter or setter will be used, so we can't generate an
1396 PROPERTY_NAME (property_decl) = component;
1397 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1398 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1399 PROPERTY_READONLY (property_decl) = 0;
1400 PROPERTY_NONATOMIC (property_decl) = 0;
1401 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1402 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1403 PROPERTY_DYNAMIC (property_decl) = 0;
1406 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1408 /* The following is currently unused, but it's nice to have
1409 there. We may use it if we need in the future. */
1411 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1413 return property_decl;
1419 /* This hook routine is invoked by the parser when an expression such
1420 as 'xxx.yyy' is parsed. We get a chance to process these
1421 expressions in a way that is specified to Objective-C (to implement
1422 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1423 If the expression is not an Objective-C specified expression, we
1424 should return NULL_TREE; else we return the expression.
1426 At the moment this only implements dot-syntax and properties (not
1427 non-fragile ivars yet), ie 'object.property' or 'object.component'
1428 where 'component' is not a declared property, but a valid getter or
1429 setter for it could be found. */
1431 objc_maybe_build_component_ref (tree object, tree property_ident)
1436 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1438 if (flag_objc1_only)
1441 /* Try to determine if 'object' is an Objective-C object or not. If
1443 if (object == NULL_TREE || object == error_mark_node
1444 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1447 if (property_ident == NULL_TREE || property_ident == error_mark_node
1448 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1451 /* The following analysis of 'object' is similar to the one used for
1452 the 'receiver' of a method invocation. We need to determine what
1453 'object' is and find the appropriate property (either declared,
1454 or artificial) for it (in the same way as we need to find the
1455 appropriate method prototype for a method invocation). There are
1456 some simplifications here though: "object.property" is invalid if
1457 "object" has a type of "id" or "Class"; it must at least have a
1458 protocol attached to it, and "object" is never a class name as
1459 that is done by objc_build_class_component_ref. Finally, we
1460 don't know if this really is a dot-syntax expression, so we want
1461 to make a quick exit if it is not; for this reason, we try to
1462 postpone checks after determining that 'object' looks like an
1463 Objective-C object. */
1465 if (objc_is_id (rtype))
1467 /* This is the case that the 'object' is of type 'id' or
1470 /* Check if at least it is of type 'id <Protocol>' or 'Class
1471 <Protocol>'; if so, look the property up in the
1473 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1475 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1479 /* No point looking up declared @properties if we are
1480 dealing with a class. Classes have no declared
1482 if (!IS_CLASS (rtype))
1483 x = lookup_property_in_protocol_list (rprotos, property_ident);
1487 /* Ok, no property. Maybe it was an
1488 object.component dot-syntax without a declared
1489 property (this is valid for classes too). Look
1490 for getter/setter methods and internally declare
1491 an artifical property based on them if found. */
1492 x = maybe_make_artificial_property_decl (NULL_TREE,
1500 else if (objc_method_context)
1502 /* Else, if we are inside a method it could be the case of
1503 'super' or 'self'. */
1504 tree interface_type = NULL_TREE;
1506 while (TREE_CODE (t) == COMPOUND_EXPR
1507 || TREE_CODE (t) == MODIFY_EXPR
1508 || CONVERT_EXPR_P (t)
1509 || TREE_CODE (t) == COMPONENT_REF)
1510 t = TREE_OPERAND (t, 0);
1512 if (t == UOBJC_SUPER_decl)
1514 /* TODO: Check if this is correct also for 'super' in categories. */
1515 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1517 else if (t == self_decl)
1518 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1522 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1523 x = lookup_property (interface_type, property_ident);
1527 /* Try the dot-syntax without a declared property.
1528 If this is an access to 'self', it is possible
1529 that they may refer to a setter/getter that is
1530 not declared in the interface, but exists locally
1531 in the implementation. In that case, get the
1532 implementation context and use it. */
1533 tree implementation = NULL_TREE;
1536 implementation = objc_implementation_context;
1538 x = maybe_make_artificial_property_decl
1539 (interface_type, implementation, NULL_TREE,
1541 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL));
1548 /* This is the case where we have more information on 'rtype'. */
1549 tree basetype = TYPE_MAIN_VARIANT (rtype);
1551 /* Skip the pointer - if none, it's not an Objective-C object or
1553 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1554 basetype = TREE_TYPE (basetype);
1558 /* Traverse typedefs. */
1559 while (basetype != NULL_TREE
1560 && TREE_CODE (basetype) == RECORD_TYPE
1561 && OBJC_TYPE_NAME (basetype)
1562 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1563 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1564 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1566 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1568 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1569 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1572 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1573 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1574 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1576 /* Not sure 'rtype' could ever be a class here! Just
1577 for safety we keep the checks. */
1578 if (!IS_CLASS (rtype))
1580 x = lookup_property (interface_type, property_ident);
1583 x = lookup_property_in_protocol_list (protocol_list,
1589 /* Try the dot-syntax without a declared property.
1590 If we are inside a method implementation, it is
1591 possible that they may refer to a setter/getter
1592 that is not declared in the interface, but exists
1593 locally in the implementation. In that case, get
1594 the implementation context and use it. */
1595 tree implementation = NULL_TREE;
1597 if (objc_implementation_context
1598 && CLASS_NAME (objc_implementation_context)
1599 == OBJC_TYPE_NAME (interface_type))
1600 implementation = objc_implementation_context;
1602 x = maybe_make_artificial_property_decl (interface_type,
1617 /* We have an additional nasty problem here; if this
1618 PROPERTY_REF needs to become a 'getter', then the conversion
1619 from PROPERTY_REF into a getter call happens in gimplify,
1620 after the selector table has already been generated and when
1621 it is too late to add another selector to it. To work around
1622 the problem, we always create the getter call at this stage,
1623 which puts the selector in the table. Note that if the
1624 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1625 we have added a selector too many to the selector table.
1626 This is a little inefficient.
1628 Also note that method calls to 'self' and 'super' require the
1629 context (self_decl, UOBJS_SUPER_decl,
1630 objc_implementation_context etc) to be built correctly; this
1631 is yet another reason why building the call at the gimplify
1632 stage (when this context has been lost) is not very
1633 practical. If we build it at this stage, we know it will
1634 always be built correctly.
1636 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1637 property decl created to deal with a dotsyntax not really
1638 referring to an existing property) then do not try to build a
1639 call to the getter as there is no getter. */
1640 if (PROPERTY_HAS_NO_GETTER (x))
1641 getter_call = NULL_TREE;
1643 getter_call = objc_finish_message_expr (object,
1644 PROPERTY_GETTER_NAME (x),
1647 if (TREE_DEPRECATED (x))
1648 warn_deprecated_use (x, NULL_TREE);
1650 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1651 SET_EXPR_LOCATION (expression, input_location);
1652 TREE_SIDE_EFFECTS (expression) = 1;
1660 /* This hook routine is invoked by the parser when an expression such
1661 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1662 Objective-C 2.0 dot-syntax applied to classes, so we need to
1663 convert it into a setter/getter call on the class. */
1665 objc_build_class_component_ref (tree class_name, tree property_ident)
1670 if (flag_objc1_only)
1671 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1673 if (class_name == NULL_TREE || class_name == error_mark_node
1674 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1675 return error_mark_node;
1677 if (property_ident == NULL_TREE || property_ident == error_mark_node
1678 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1681 object = objc_get_class_reference (class_name);
1684 /* We know that 'class_name' is an Objective-C class name as the
1685 parser won't call this function if it is not. This is only a
1686 double-check for safety. */
1687 error_at (input_location, "could not find class %qE", class_name);
1688 return error_mark_node;
1691 rtype = lookup_interface (class_name);
1694 /* Again, this should never happen, but we do check. */
1695 error_at (input_location, "could not find interface for class %qE", class_name);
1696 return error_mark_node;
1700 if (TREE_DEPRECATED (rtype))
1701 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1704 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1713 if (PROPERTY_HAS_NO_GETTER (x))
1714 getter_call = NULL_TREE;
1716 getter_call = objc_finish_message_expr (object,
1717 PROPERTY_GETTER_NAME (x),
1719 if (TREE_DEPRECATED (x))
1720 warn_deprecated_use (x, NULL_TREE);
1722 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1723 SET_EXPR_LOCATION (expression, input_location);
1724 TREE_SIDE_EFFECTS (expression) = 1;
1730 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1731 property_ident, class_name);
1732 return error_mark_node;
1740 /* This is used because we don't want to expose PROPERTY_REF to the
1741 C/C++ frontends. Maybe we should! */
1743 objc_is_property_ref (tree node)
1745 if (node && TREE_CODE (node) == PROPERTY_REF)
1751 /* This function builds a setter call for a PROPERTY_REF (real, for a
1752 declared property, or artificial, for a dot-syntax accessor which
1753 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1754 (the caller must check this beforehand). 'rhs' is the value to
1755 assign to the property. A plain setter call is returned, or
1756 error_mark_node if the property is readonly. */
1759 objc_build_setter_call (tree lhs, tree rhs)
1761 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1762 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1764 if (PROPERTY_READONLY (property_decl))
1766 error ("readonly property can not be set");
1767 return error_mark_node;
1771 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1774 /* TODO: Check that the setter return type is 'void'. */
1776 /* TODO: Decay arguments in C. */
1777 setter = objc_finish_message_expr (object_expr,
1778 PROPERTY_SETTER_NAME (property_decl),
1783 /* Unreachable, but the compiler may not realize. */
1784 return error_mark_node;
1787 /* This hook routine is called when a MODIFY_EXPR is being built. We
1788 check what is being modified; if it is a PROPERTY_REF, we need to
1789 generate a 'setter' function call for the property. If this is not
1790 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1791 on creating their MODIFY_EXPR.
1793 This is used for example if you write
1797 where 'count' is a property. The left-hand side creates a
1798 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1799 to assign something to it. We intercept that here, and generate a
1800 call to the 'setter' method instead. */
1802 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1804 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1806 /* Building a simple call to the setter method would work for cases such as
1810 but wouldn't work for cases such as
1812 count = object2.count = 1;
1814 to get these to work with very little effort, we build a
1815 compound statement which does the setter call (to set the
1816 property to 'rhs'), but which can also be evaluated returning
1817 the 'rhs'. So, we want to create the following:
1819 (temp = rhs; [object setProperty: temp]; temp)
1821 tree temp_variable_decl, bind;
1822 /* s1, s2 and s3 are the tree statements that we need in the
1823 compound expression. */
1824 tree s1, s2, s3, compound_expr;
1826 /* TODO: If 'rhs' is a constant, we could maybe do without the
1827 'temp' variable ? */
1829 /* Declare __objc_property_temp in a local bind. */
1830 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1831 DECL_SOURCE_LOCATION (temp_variable_decl) = input_location;
1832 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1833 SET_EXPR_LOCATION (bind, input_location);
1834 TREE_SIDE_EFFECTS (bind) = 1;
1837 /* Now build the compound statement. */
1839 /* s1: __objc_property_temp = rhs */
1840 s1 = build_modify_expr (input_location, temp_variable_decl, NULL_TREE,
1842 input_location, rhs, NULL_TREE);
1843 SET_EXPR_LOCATION (s1, input_location);
1845 /* s2: [object setProperty: __objc_property_temp] */
1846 s2 = objc_build_setter_call (lhs, temp_variable_decl);
1848 /* This happens if building the setter failed because the property
1850 if (s2 == error_mark_node)
1851 return error_mark_node;
1853 SET_EXPR_LOCATION (s2, input_location);
1855 /* s3: __objc_property_temp */
1856 s3 = convert (TREE_TYPE (lhs), temp_variable_decl);
1858 /* Now build the compound statement (s1, s2, s3) */
1859 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1861 /* Without this, with -Wall you get a 'valued computed is not
1862 used' every time there is a "object.property = x" where the
1863 value of the resulting MODIFY_EXPR is not used. That is
1864 correct (maybe a more sophisticated implementation could
1865 avoid generating the compound expression if not needed), but
1866 we need to turn it off. */
1867 TREE_NO_WARNING (compound_expr) = 1;
1868 return compound_expr;
1874 /* This hook is called by the frontend when one of the four unary
1875 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1876 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1877 argument which is a PROPERTY_REF. For example, this happens if you have
1881 where 'count' is a property. We need to use the 'getter' and
1882 'setter' for the property in an appropriate way to build the
1883 appropriate expression. 'code' is the code for the expression (one
1884 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1885 'increment' is how much we need to add or subtract. */
1887 objc_build_incr_expr_for_property_ref (location_t location,
1888 enum tree_code code,
1889 tree argument, tree increment)
1891 /* Here are the expressions that we want to build:
1893 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1894 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1896 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1897 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1899 tree temp_variable_decl, bind;
1900 /* s1, s2 and s3 are the tree statements that we need in the
1901 compound expression. */
1902 tree s1, s2, s3, compound_expr;
1905 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1906 return error_mark_node;
1908 /* Declare __objc_property_temp in a local bind. */
1909 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1910 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1911 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1912 SET_EXPR_LOCATION (bind, location);
1913 TREE_SIDE_EFFECTS (bind) = 1;
1916 /* Now build the compound statement. */
1918 /* Note that the 'getter' is generated at gimplify time; at this
1919 time, we can simply put the property_ref (ie, argument) wherever
1920 we want the getter ultimately to be. */
1922 /* s1: __objc_property_temp = [object property] <+/- increment> */
1925 case PREINCREMENT_EXPR:
1926 /* __objc_property_temp = [object property] + increment */
1927 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1929 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1930 argument, increment), NULL_TREE);
1932 case PREDECREMENT_EXPR:
1933 /* __objc_property_temp = [object property] - increment */
1934 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1936 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1937 argument, increment), NULL_TREE);
1939 case POSTINCREMENT_EXPR:
1940 case POSTDECREMENT_EXPR:
1941 /* __objc_property_temp = [object property] */
1942 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1944 location, argument, NULL_TREE);
1950 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1953 case PREINCREMENT_EXPR:
1954 case PREDECREMENT_EXPR:
1955 /* [object setProperty: __objc_property_temp] */
1956 s2 = objc_build_setter_call (argument, temp_variable_decl);
1958 case POSTINCREMENT_EXPR:
1959 /* [object setProperty: __objc_property_temp + increment] */
1960 s2 = objc_build_setter_call (argument,
1961 build2 (PLUS_EXPR, TREE_TYPE (argument),
1962 temp_variable_decl, increment));
1964 case POSTDECREMENT_EXPR:
1965 /* [object setProperty: __objc_property_temp - increment] */
1966 s2 = objc_build_setter_call (argument,
1967 build2 (MINUS_EXPR, TREE_TYPE (argument),
1968 temp_variable_decl, increment));
1974 /* This happens if building the setter failed because the property
1976 if (s2 == error_mark_node)
1977 return error_mark_node;
1979 SET_EXPR_LOCATION (s2, location);
1981 /* s3: __objc_property_temp */
1982 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
1984 /* Now build the compound statement (s1, s2, s3) */
1985 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
1987 /* Prevent C++ from warning with -Wall that "right operand of comma
1988 operator has no effect". */
1989 TREE_NO_WARNING (compound_expr) = 1;
1990 return compound_expr;
1994 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
1995 tree optparms, bool ellipsis)
1997 if (is_class_method)
1998 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
1999 optparms, ellipsis);
2001 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2002 optparms, ellipsis);
2006 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2008 if (!objc_interface_context)
2010 /* PS: At the moment, due to how the parser works, it should be
2011 impossible to get here. But it's good to have the check in
2012 case the parser changes.
2014 fatal_error ("method declaration not in @interface context");
2017 if (flag_objc1_only && attributes)
2018 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2020 objc_decl_method_attributes (&decl, attributes, 0);
2021 objc_add_method (objc_interface_context,
2024 objc_method_optional_flag);
2027 /* Return 'true' if the method definition could be started, and
2028 'false' if not (because we are outside an @implementation context).
2031 objc_start_method_definition (bool is_class_method, tree decl, tree attributes)
2033 if (!objc_implementation_context)
2035 error ("method definition not in @implementation context");
2039 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2043 /* Indicate no valid break/continue context by setting these variables
2044 to some non-null, non-label value. We'll notice and emit the proper
2045 error message in c_finish_bc_stmt. */
2046 c_break_label = c_cont_label = size_zero_node;
2050 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2052 objc_decl_method_attributes (&decl, attributes, 0);
2054 objc_add_method (objc_implementation_context,
2057 /* is optional */ false);
2058 start_method_def (decl);
2063 objc_add_instance_variable (tree decl)
2065 (void) add_instance_variable (objc_ivar_context,
2066 objc_ivar_visibility,
2070 /* Return true if TYPE is 'id'. */
2073 objc_is_object_id (tree type)
2075 return OBJC_TYPE_NAME (type) == objc_object_id;
2079 objc_is_class_id (tree type)
2081 return OBJC_TYPE_NAME (type) == objc_class_id;
2084 /* Construct a C struct with same name as KLASS, a base struct with tag
2085 SUPER_NAME (if any), and FIELDS indicated. */
2088 objc_build_struct (tree klass, tree fields, tree super_name)
2090 tree name = CLASS_NAME (klass);
2091 tree s = objc_start_struct (name);
2092 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2094 VEC(tree,heap) *objc_info = NULL;
2099 /* Prepend a packed variant of the base class into the layout. This
2100 is necessary to preserve ObjC ABI compatibility. */
2101 tree base = build_decl (input_location,
2102 FIELD_DECL, NULL_TREE, super);
2103 tree field = TYPE_FIELDS (super);
2105 while (field && DECL_CHAIN (field)
2106 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2107 field = DECL_CHAIN (field);
2109 /* For ObjC ABI purposes, the "packed" size of a base class is
2110 the sum of the offset and the size (in bits) of the last field
2113 = (field && TREE_CODE (field) == FIELD_DECL
2114 ? size_binop (PLUS_EXPR,
2115 size_binop (PLUS_EXPR,
2118 convert (bitsizetype,
2119 DECL_FIELD_OFFSET (field)),
2120 bitsize_int (BITS_PER_UNIT)),
2121 DECL_FIELD_BIT_OFFSET (field)),
2123 : bitsize_zero_node);
2124 DECL_SIZE_UNIT (base)
2125 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2126 size_int (BITS_PER_UNIT));
2127 DECL_ARTIFICIAL (base) = 1;
2128 DECL_ALIGN (base) = 1;
2129 DECL_FIELD_CONTEXT (base) = s;
2131 DECL_FIELD_IS_BASE (base) = 1;
2134 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2135 #endif /* are following the ObjC ABI here. */
2136 DECL_CHAIN (base) = fields;
2140 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2141 information in all variants of this RECORD_TYPE to be destroyed
2142 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2143 for something else and then will change all variants to use the
2144 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2145 it for ObjC protocols and that such propagation will make all
2146 variants use the same objc_info), but it is therein that we store
2147 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2148 Hence, we must save the ObjC-specific information before calling
2149 finish_struct(), and then reinstate it afterwards. */
2151 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2153 INIT_TYPE_OBJC_INFO (t);
2154 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
2157 s = objc_finish_struct (s, fields);
2159 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2161 /* We now want to restore the different TYPE_OBJC_INFO, but we
2162 have the additional problem that the C frontend doesn't just
2163 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2164 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2165 we need a different TYPE_OBJC_INFO for each (and
2166 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2167 make a copy of each TYPE_LANG_SPECIFIC before we modify
2169 if (TYPE_LANG_SPECIFIC (t))
2171 /* Create a copy of TYPE_LANG_SPECIFIC. */
2172 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2173 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2174 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2175 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2179 /* Just create a new one. */
2180 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2182 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2183 protocol information that may have been associated with the
2185 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
2186 /* Replace the IDENTIFIER_NODE with an actual @interface now
2188 TYPE_OBJC_INTERFACE (t) = klass;
2190 VEC_free (tree, heap, objc_info);
2192 /* Use TYPE_BINFO structures to point at the super class, if any. */
2193 objc_xref_basetypes (s, super);
2195 /* Mark this struct as a class template. */
2196 CLASS_STATIC_TEMPLATE (klass) = s;
2201 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
2202 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
2205 objc_build_volatilized_type (tree type)
2209 /* Check if we have not constructed the desired variant already. */
2210 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2212 /* The type qualifiers must (obviously) match up. */
2213 if (!TYPE_VOLATILE (t)
2214 || (TYPE_READONLY (t) != TYPE_READONLY (type))
2215 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
2218 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
2219 info, if any) must match up. */
2220 if (POINTER_TYPE_P (t)
2221 && (TREE_TYPE (t) != TREE_TYPE (type)))
2224 /* Only match up the types which were previously volatilized in similar fashion and not
2225 because they were declared as such. */
2226 if (!lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (t)))
2229 /* Everything matches up! */
2233 /* Ok, we could not re-use any of the pre-existing variants. Create
2235 t = build_variant_type_copy (type);
2236 TYPE_VOLATILE (t) = 1;
2238 TYPE_ATTRIBUTES (t) = merge_attributes (TYPE_ATTRIBUTES (type),
2239 tree_cons (get_identifier ("objc_volatilized"),
2242 if (TREE_CODE (t) == ARRAY_TYPE)
2243 TREE_TYPE (t) = objc_build_volatilized_type (TREE_TYPE (t));
2245 /* Set up the canonical type information. */
2246 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2247 SET_TYPE_STRUCTURAL_EQUALITY (t);
2248 else if (TYPE_CANONICAL (type) != type)
2249 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
2251 TYPE_CANONICAL (t) = t;
2256 /* Mark DECL as being 'volatile' for purposes of Darwin
2257 _setjmp()/_longjmp() exception handling. Called from
2258 objc_mark_locals_volatile(). */
2260 objc_volatilize_decl (tree decl)
2262 /* Do not mess with variables that are 'static' or (already)
2264 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2265 && (TREE_CODE (decl) == VAR_DECL
2266 || TREE_CODE (decl) == PARM_DECL))
2268 tree t = TREE_TYPE (decl);
2270 t = objc_build_volatilized_type (t);
2272 TREE_TYPE (decl) = t;
2273 TREE_THIS_VOLATILE (decl) = 1;
2274 TREE_SIDE_EFFECTS (decl) = 1;
2275 DECL_REGISTER (decl) = 0;
2277 C_DECL_REGISTER (decl) = 0;
2282 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2283 (including its categories and superclasses) or by object type TYP.
2284 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2287 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2289 bool class_type = (cls != NULL_TREE);
2295 /* Check protocols adopted by the class and its categories. */
2296 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2298 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2302 /* Repeat for superclasses. */
2303 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2306 /* Check for any protocols attached directly to the object type. */
2307 if (TYPE_HAS_OBJC_INFO (typ))
2309 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2316 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2317 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2318 "implementing" a given protocol, since they do not have an
2321 warning (0, "class %qs does not implement the %qE protocol",
2322 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2324 warning (0, "type %qs does not conform to the %qE protocol",
2325 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2331 /* Check if class RCLS and instance struct type RTYP conform to at least the
2332 same protocols that LCLS and LTYP conform to. */
2335 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2338 bool have_lproto = false;
2342 /* NB: We do _not_ look at categories defined for LCLS; these may or
2343 may not get loaded in, and therefore it is unreasonable to require
2344 that RCLS/RTYP must implement any of their protocols. */
2345 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2349 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2353 /* Repeat for superclasses. */
2354 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2357 /* Check for any protocols attached directly to the object type. */
2358 if (TYPE_HAS_OBJC_INFO (ltyp))
2360 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2364 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2369 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2370 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2371 away with simply checking for 'id' or 'Class' (!RCLS), since this
2372 routine will not get called in other cases. */
2373 return have_lproto || (rcls != NULL_TREE);
2376 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2377 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2378 compatible by objc_compare_types() below. */
2381 objc_common_type (tree type1, tree type2)
2383 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2385 while (POINTER_TYPE_P (inner1))
2387 inner1 = TREE_TYPE (inner1);
2388 inner2 = TREE_TYPE (inner2);
2391 /* If one type is derived from another, return the base type. */
2392 if (DERIVED_FROM_P (inner1, inner2))
2394 else if (DERIVED_FROM_P (inner2, inner1))
2397 /* If both types are 'Class', return 'Class'. */
2398 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2399 return objc_class_type;
2401 /* Otherwise, return 'id'. */
2402 return objc_object_type;
2405 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2406 an instance of RTYP to an instance of LTYP or to compare the two
2407 (if ARGNO is equal to -3), per ObjC type system rules. Before
2408 returning 'true', this routine may issue warnings related to, e.g.,
2409 protocol conformance. When returning 'false', the routine must
2410 produce absolutely no warnings; the C or C++ front-end will do so
2411 instead, if needed. If either LTYP or RTYP is not an Objective-C
2412 type, the routine must return 'false'.
2414 The ARGNO parameter is encoded as follows:
2415 >= 1 Parameter number (CALLEE contains function being called);
2419 -3 Comparison (LTYP and RTYP may match in either direction);
2420 -4 Silent comparison (for C++ overload resolution);
2421 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2422 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2423 so that each object of type RTYP is also of type LTYP). This is used
2424 when comparing property types. */
2427 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2429 tree lcls, rcls, lproto, rproto;
2430 bool pointers_compatible;
2432 /* We must be dealing with pointer types */
2433 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2438 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2439 rtyp = TREE_TYPE (rtyp);
2441 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2443 /* We must also handle function pointers, since ObjC is a bit more
2444 lenient than C or C++ on this. */
2445 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2447 /* Return types must be covariant. */
2448 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2449 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2453 /* Argument types must be contravariant. */
2454 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
2455 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
2457 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
2458 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
2463 return (ltyp == rtyp);
2466 /* Past this point, we are only interested in ObjC class instances,
2467 or 'id' or 'Class'. */
2468 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2471 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2472 && !TYPE_HAS_OBJC_INFO (ltyp))
2475 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2476 && !TYPE_HAS_OBJC_INFO (rtyp))
2479 /* Past this point, we are committed to returning 'true' to the caller
2480 (unless performing a silent comparison; see below). However, we can
2481 still warn about type and/or protocol mismatches. */
2483 if (TYPE_HAS_OBJC_INFO (ltyp))
2485 lcls = TYPE_OBJC_INTERFACE (ltyp);
2486 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2489 lcls = lproto = NULL_TREE;
2491 if (TYPE_HAS_OBJC_INFO (rtyp))
2493 rcls = TYPE_OBJC_INTERFACE (rtyp);
2494 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2497 rcls = rproto = NULL_TREE;
2499 /* If we could not find an @interface declaration, we must have
2500 only seen a @class declaration; for purposes of type comparison,
2501 treat it as a stand-alone (root) class. */
2503 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2506 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2509 /* If either type is an unqualified 'id', we're done. This is because
2510 an 'id' can be assigned to or from any type with no warnings. */
2513 if ((!lproto && objc_is_object_id (ltyp))
2514 || (!rproto && objc_is_object_id (rtyp)))
2519 /* For property checks, though, an 'id' is considered the most
2520 general type of object, hence if you try to specialize an
2521 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2523 if (!lproto && objc_is_object_id (ltyp))
2527 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2529 /* If the underlying types are the same, and at most one of them has
2530 a protocol list, we do not need to issue any diagnostics. */
2531 if (pointers_compatible && (!lproto || !rproto))
2534 /* If exactly one of the types is 'Class', issue a diagnostic; any
2535 exceptions of this rule have already been handled. */
2536 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2537 pointers_compatible = false;
2538 /* Otherwise, check for inheritance relations. */
2541 if (!pointers_compatible)
2543 /* Again, if any of the two is an 'id', we're satisfied,
2544 unless we're comparing properties, in which case only an
2545 'id' on the left-hand side (old property) is good
2549 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2551 pointers_compatible = objc_is_object_id (ltyp);
2554 if (!pointers_compatible)
2555 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2557 if (!pointers_compatible && (argno == -3 || argno == -4))
2558 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2561 /* If the pointers match modulo protocols, check for protocol conformance
2563 if (pointers_compatible)
2565 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2568 if (!pointers_compatible && argno == -3)
2569 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2573 if (!pointers_compatible)
2575 /* The two pointers are not exactly compatible. Issue a warning, unless
2576 we are performing a silent comparison, in which case return 'false'
2578 /* NB: For the time being, we shall make our warnings look like their
2579 C counterparts. In the future, we may wish to make them more
2588 warning (0, "comparison of distinct Objective-C types lacks a cast");
2592 warning (0, "initialization from distinct Objective-C type");
2596 warning (0, "assignment from distinct Objective-C type");
2600 warning (0, "distinct Objective-C type in return");
2604 warning (0, "passing argument %d of %qE from distinct "
2605 "Objective-C type", argno, callee);
2613 /* This routine is similar to objc_compare_types except that function-pointers are
2614 excluded. This is because, caller assumes that common types are of (id, Object*)
2615 variety and calls objc_common_type to obtain a common type. There is no commonolty
2616 between two function-pointers in this regard. */
2619 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2621 if (objc_compare_types (ltyp, rtyp, argno, callee))
2623 /* exclude function-pointer types. */
2626 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2627 rtyp = TREE_TYPE (rtyp);
2629 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2630 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2635 /* Check if LTYP and RTYP have the same type qualifiers. If either type
2636 lives in the volatilized hash table, ignore the 'volatile' bit when
2637 making the comparison. */
2640 objc_type_quals_match (tree ltyp, tree rtyp)
2642 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
2644 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ltyp)))
2645 lquals &= ~TYPE_QUAL_VOLATILE;
2647 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (rtyp)))
2648 rquals &= ~TYPE_QUAL_VOLATILE;
2650 return (lquals == rquals);
2654 /* Determine if CHILD is derived from PARENT. The routine assumes that
2655 both parameters are RECORD_TYPEs, and is non-reflexive. */
2658 objc_derived_from_p (tree parent, tree child)
2660 parent = TYPE_MAIN_VARIANT (parent);
2662 for (child = TYPE_MAIN_VARIANT (child);
2663 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2665 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2666 (TYPE_BINFO (child),
2669 if (child == parent)
2678 objc_build_component_ref (tree datum, tree component)
2680 /* If COMPONENT is NULL, the caller is referring to the anonymous
2681 base class field. */
2684 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2686 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2689 /* The 'build_component_ref' routine has been removed from the C++
2690 front-end, but 'finish_class_member_access_expr' seems to be
2691 a worthy substitute. */
2693 return finish_class_member_access_expr (datum, component, false,
2694 tf_warning_or_error);
2696 return build_component_ref (input_location, datum, component);
2700 /* Recursively copy inheritance information rooted at BINFO. To do this,
2701 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2704 objc_copy_binfo (tree binfo)
2706 tree btype = BINFO_TYPE (binfo);
2707 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2711 BINFO_TYPE (binfo2) = btype;
2712 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2713 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2715 /* Recursively copy base binfos of BINFO. */
2716 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2718 tree base_binfo2 = objc_copy_binfo (base_binfo);
2720 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2721 BINFO_BASE_APPEND (binfo2, base_binfo2);
2727 /* Record superclass information provided in BASETYPE for ObjC class REF.
2728 This is loosely based on cp/decl.c:xref_basetypes(). */
2731 objc_xref_basetypes (tree ref, tree basetype)
2733 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2735 TYPE_BINFO (ref) = binfo;
2736 BINFO_OFFSET (binfo) = size_zero_node;
2737 BINFO_TYPE (binfo) = ref;
2741 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2743 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2744 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
2745 BINFO_BASE_APPEND (binfo, base_binfo);
2746 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2750 /* Called from finish_decl. */
2753 objc_check_decl (tree decl)
2755 tree type = TREE_TYPE (decl);
2757 if (TREE_CODE (type) != RECORD_TYPE)
2759 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2760 error ("statically allocated instance of Objective-C class %qE",
2765 objc_check_global_decl (tree decl)
2767 tree id = DECL_NAME (decl);
2768 if (objc_is_class_name (id) && global_bindings_p())
2769 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2772 /* Return a non-volatalized version of TYPE. */
2775 objc_non_volatilized_type (tree type)
2777 if (lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (type)))
2778 type = build_qualified_type (type, (TYPE_QUALS (type) & ~TYPE_QUAL_VOLATILE));
2782 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2783 INTERFACE may either name an Objective-C class, or refer to the
2784 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2785 type, just return it unchanged. This function is often called when
2786 PROTOCOLS is NULL_TREE, in which case we simply look up the
2787 appropriate INTERFACE. */
2790 objc_get_protocol_qualified_type (tree interface, tree protocols)
2792 /* If INTERFACE is not provided, default to 'id'. */
2793 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2794 bool is_ptr = (type != NULL_TREE);
2798 type = objc_is_class_name (interface);
2802 /* If looking at a typedef, retrieve the precise type it
2804 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2805 interface = identifier_global_value (interface);
2807 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2808 && DECL_ORIGINAL_TYPE (interface))
2809 ? DECL_ORIGINAL_TYPE (interface)
2810 : xref_tag (RECORD_TYPE, type));
2814 /* This case happens when we are given an 'interface' which
2815 is not a valid class name. For example if a typedef was
2816 used, and 'interface' really is the identifier of the
2817 typedef, but when you resolve it you don't get an
2818 Objective-C class, but something else, such as 'int'.
2819 This is an error; protocols make no sense unless you use
2820 them with Objective-C objects. */
2821 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2823 /* Try to recover. Ignore the invalid class name, and treat
2824 the object as an 'id' to silence further warnings about
2826 type = objc_object_type;
2833 type = build_variant_type_copy (type);
2835 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2839 tree orig_pointee_type = TREE_TYPE (type);
2840 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2842 /* Set up the canonical type information. */
2843 TYPE_CANONICAL (type)
2844 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2846 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2847 type = TREE_TYPE (type);
2850 /* Look up protocols and install in lang specific list. */
2851 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2852 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
2854 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2855 return the pointer to the new pointee variant. */
2857 type = TYPE_POINTER_TO (type);
2859 TYPE_OBJC_INTERFACE (type)
2860 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2866 /* Check for circular dependencies in protocols. The arguments are
2867 PROTO, the protocol to check, and LIST, a list of protocol it
2871 check_protocol_recursively (tree proto, tree list)
2875 for (p = list; p; p = TREE_CHAIN (p))
2877 tree pp = TREE_VALUE (p);
2879 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2880 pp = lookup_protocol (pp, /* warn if deprecated */ false);
2883 fatal_error ("protocol %qE has circular dependency",
2884 PROTOCOL_NAME (pp));
2886 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2890 /* Look up PROTOCOLS, and return a list of those that are found. If
2891 none are found, return NULL. Note that this function will emit a
2892 warning if a protocol is found and is deprecated. */
2895 lookup_and_install_protocols (tree protocols)
2898 tree return_value = NULL_TREE;
2900 if (protocols == error_mark_node)
2903 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2905 tree ident = TREE_VALUE (proto);
2906 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true);
2909 return_value = chainon (return_value,
2910 build_tree_list (NULL_TREE, p));
2911 else if (ident != error_mark_node)
2912 error ("cannot find protocol declaration for %qE",
2916 return return_value;
2919 /* Create a declaration for field NAME of a given TYPE. */
2922 create_field_decl (tree type, const char *name)
2924 return build_decl (input_location,
2925 FIELD_DECL, get_identifier (name), type);
2928 /* Create a global, static declaration for variable NAME of a given TYPE. The
2929 finish_var_decl() routine will need to be called on it afterwards. */
2932 start_var_decl (tree type, const char *name)
2934 tree var = build_decl (input_location,
2935 VAR_DECL, get_identifier (name), type);
2937 TREE_STATIC (var) = 1;
2938 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
2939 DECL_IGNORED_P (var) = 1;
2940 DECL_ARTIFICIAL (var) = 1;
2941 DECL_CONTEXT (var) = NULL_TREE;
2943 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
2949 /* Finish off the variable declaration created by start_var_decl(). */
2952 finish_var_decl (tree var, tree initializer)
2954 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
2957 /* Find the decl for the constant string class reference. This is only
2958 used for the NeXT runtime. */
2961 setup_string_decl (void)
2966 /* %s in format will provide room for terminating null */
2967 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
2968 + strlen (constant_string_class_name);
2969 name = XNEWVEC (char, length);
2970 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
2971 constant_string_class_name);
2972 constant_string_global_id = get_identifier (name);
2973 string_class_decl = lookup_name (constant_string_global_id);
2975 return string_class_decl;
2978 /* Purpose: "play" parser, creating/installing representations
2979 of the declarations that are required by Objective-C.
2983 type_spec--------->sc_spec
2984 (tree_list) (tree_list)
2987 identifier_node identifier_node */
2990 synth_module_prologue (void)
2993 enum debug_info_type save_write_symbols = write_symbols;
2994 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2996 /* Suppress outputting debug symbols, because
2997 dbxout_init hasn't been called yet. */
2998 write_symbols = NO_DEBUG;
2999 debug_hooks = &do_nothing_debug_hooks;
3002 push_lang_context (lang_name_c); /* extern "C" */
3005 /* The following are also defined in <objc/objc.h> and friends. */
3007 objc_object_id = get_identifier (TAG_OBJECT);
3008 objc_class_id = get_identifier (TAG_CLASS);
3010 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
3011 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
3013 objc_object_type = build_pointer_type (objc_object_reference);
3014 objc_class_type = build_pointer_type (objc_class_reference);
3016 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
3017 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
3019 /* Declare the 'id' and 'Class' typedefs. */
3021 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3025 TREE_NO_WARNING (type) = 1;
3026 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3030 TREE_NO_WARNING (type) = 1;
3032 /* Forward-declare '@interface Protocol'. */
3034 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
3035 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
3036 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
3039 /* Declare type of selector-objects that represent an operation name. */
3041 if (flag_next_runtime)
3042 /* `struct objc_selector *' */
3044 = build_pointer_type (xref_tag (RECORD_TYPE,
3045 get_identifier (TAG_SELECTOR)));
3047 /* `const struct objc_selector *' */
3049 = build_pointer_type
3050 (build_qualified_type (xref_tag (RECORD_TYPE,
3051 get_identifier (TAG_SELECTOR)),
3054 /* Declare receiver type used for dispatching messages to 'super'. */
3056 /* `struct objc_super *' */
3057 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
3058 get_identifier (TAG_SUPER)));
3060 /* Declare pointers to method and ivar lists. */
3061 objc_method_list_ptr = build_pointer_type
3062 (xref_tag (RECORD_TYPE,
3063 get_identifier (UTAG_METHOD_LIST)));
3064 objc_method_proto_list_ptr
3065 = build_pointer_type (xref_tag (RECORD_TYPE,
3066 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3067 objc_ivar_list_ptr = build_pointer_type
3068 (xref_tag (RECORD_TYPE,
3069 get_identifier (UTAG_IVAR_LIST)));
3071 /* TREE_NOTHROW is cleared for the message-sending functions,
3072 because the function that gets called can throw in Obj-C++, or
3073 could itself call something that can throw even in Obj-C. */
3075 if (flag_next_runtime)
3077 /* NB: In order to call one of the ..._stret (struct-returning)
3078 functions, the function *MUST* first be cast to a signature that
3079 corresponds to the actual ObjC method being invoked. This is
3080 what is done by the build_objc_method_call() routine below. */
3082 /* id objc_msgSend (id, SEL, ...); */
3083 /* id objc_msgSendNonNil (id, SEL, ...); */
3084 /* id objc_msgSend_stret (id, SEL, ...); */
3085 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
3087 = build_varargs_function_type_list (objc_object_type,
3091 umsg_decl = add_builtin_function (TAG_MSGSEND,
3092 type, 0, NOT_BUILT_IN,
3094 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
3095 type, 0, NOT_BUILT_IN,
3097 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
3098 type, 0, NOT_BUILT_IN,
3100 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
3101 type, 0, NOT_BUILT_IN,
3104 /* These can throw, because the function that gets called can throw
3105 in Obj-C++, or could itself call something that can throw even
3107 TREE_NOTHROW (umsg_decl) = 0;
3108 TREE_NOTHROW (umsg_nonnil_decl) = 0;
3109 TREE_NOTHROW (umsg_stret_decl) = 0;
3110 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
3112 /* id objc_msgSend_Fast (id, SEL, ...)
3113 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
3114 #ifdef OFFS_MSGSEND_FAST
3115 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
3116 type, 0, NOT_BUILT_IN,
3118 TREE_NOTHROW (umsg_fast_decl) = 0;
3119 DECL_ATTRIBUTES (umsg_fast_decl)
3120 = tree_cons (get_identifier ("hard_coded_address"),
3121 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
3124 /* No direct dispatch available. */
3125 umsg_fast_decl = umsg_decl;
3128 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
3129 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
3131 = build_varargs_function_type_list (objc_object_type,
3135 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
3136 type, 0, NOT_BUILT_IN,
3138 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
3139 type, 0, NOT_BUILT_IN, 0,
3141 TREE_NOTHROW (umsg_super_decl) = 0;
3142 TREE_NOTHROW (umsg_super_stret_decl) = 0;
3146 /* GNU runtime messenger entry points. */
3148 /* typedef id (*IMP)(id, SEL, ...); */
3150 build_varargs_function_type_list (objc_object_type,
3154 tree IMP_type = build_pointer_type (ftype);
3156 /* IMP objc_msg_lookup (id, SEL); */
3157 type = build_function_type_list (IMP_type,
3161 umsg_decl = add_builtin_function (TAG_MSGSEND,
3162 type, 0, NOT_BUILT_IN,
3164 TREE_NOTHROW (umsg_decl) = 0;
3166 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
3168 = build_function_type_list (IMP_type,
3172 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
3173 type, 0, NOT_BUILT_IN,
3175 TREE_NOTHROW (umsg_super_decl) = 0;
3177 /* The following GNU runtime entry point is called to initialize
3180 __objc_exec_class (void *); */
3182 = build_function_type_list (void_type_node,
3185 execclass_decl = add_builtin_function (TAG_EXECCLASS,
3186 type, 0, NOT_BUILT_IN,
3190 /* id objc_getClass (const char *); */
3192 type = build_function_type_list (objc_object_type,
3193 const_string_type_node,
3197 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
3200 /* id objc_getMetaClass (const char *); */
3202 objc_get_meta_class_decl
3203 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3205 build_class_template ();
3206 build_super_template ();
3207 build_protocol_template ();
3208 build_category_template ();
3209 build_objc_exception_stuff ();
3211 /* Declare objc_getProperty, object_setProperty and other property
3212 accessor helpers. */
3213 build_objc_property_accessor_helpers ();
3215 if (flag_next_runtime)
3216 build_next_objc_exception_stuff ();
3218 /* static SEL _OBJC_SELECTOR_TABLE[]; */
3220 if (! flag_next_runtime)
3221 build_selector_table_decl ();
3223 /* Forward declare constant_string_id and constant_string_type. */
3224 if (!constant_string_class_name)
3225 constant_string_class_name = default_constant_string_class_name;
3227 constant_string_id = get_identifier (constant_string_class_name);
3228 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
3230 /* Pre-build the following entities - for speed/convenience. */
3231 self_id = get_identifier ("self");
3232 ucmd_id = get_identifier ("_cmd");
3234 /* Declare struct _objc_fast_enumeration_state { ... }; */
3235 build_fast_enumeration_state_template ();
3237 /* void objc_enumeration_mutation (id) */
3238 type = build_function_type (void_type_node,
3239 tree_cons (NULL_TREE, objc_object_type, NULL_TREE));
3240 objc_enumeration_mutation_decl
3241 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3243 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3246 pop_lang_context ();
3249 write_symbols = save_write_symbols;
3250 debug_hooks = save_hooks;
3253 /* Ensure that the ivar list for NSConstantString/NXConstantString
3254 (or whatever was specified via `-fconstant-string-class')
3255 contains fields at least as large as the following three, so that
3256 the runtime can stomp on them with confidence:
3258 struct STRING_OBJECT_CLASS_NAME
3262 unsigned int length;
3266 check_string_class_template (void)
3268 tree field_decl = objc_get_class_ivars (constant_string_id);
3270 #define AT_LEAST_AS_LARGE_AS(F, T) \
3271 (F && TREE_CODE (F) == FIELD_DECL \
3272 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3273 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3275 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3278 field_decl = DECL_CHAIN (field_decl);
3279 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3282 field_decl = DECL_CHAIN (field_decl);
3283 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3285 #undef AT_LEAST_AS_LARGE_AS
3288 /* Avoid calling `check_string_class_template ()' more than once. */
3289 static GTY(()) int string_layout_checked;
3291 /* Construct an internal string layout to be used as a template for
3292 creating NSConstantString/NXConstantString instances. */
3295 objc_build_internal_const_str_type (void)
3297 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3298 tree fields = build_decl (input_location,
3299 FIELD_DECL, NULL_TREE, ptr_type_node);
3300 tree field = build_decl (input_location,
3301 FIELD_DECL, NULL_TREE, ptr_type_node);
3303 DECL_CHAIN (field) = fields; fields = field;
3304 field = build_decl (input_location,
3305 FIELD_DECL, NULL_TREE, unsigned_type_node);
3306 DECL_CHAIN (field) = fields; fields = field;
3307 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3309 finish_builtin_struct (type, "__builtin_ObjCString",
3315 /* Custom build_string which sets TREE_TYPE! */
3318 my_build_string (int len, const char *str)
3320 return fix_string_type (build_string (len, str));
3323 /* Build a string with contents STR and length LEN and convert it to a
3327 my_build_string_pointer (int len, const char *str)
3329 tree string = my_build_string (len, str);
3330 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3331 return build1 (ADDR_EXPR, ptrtype, string);
3335 string_hash (const void *ptr)
3337 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
3338 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3339 int i, len = TREE_STRING_LENGTH (str);
3342 for (i = 0; i < len; i++)
3343 h = ((h * 613) + p[i]);
3349 string_eq (const void *ptr1, const void *ptr2)
3351 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
3352 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
3353 int len1 = TREE_STRING_LENGTH (str1);
3355 return (len1 == TREE_STRING_LENGTH (str2)
3356 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3360 /* Given a chain of STRING_CST's, build a static instance of
3361 NXConstantString which points at the concatenation of those
3362 strings. We place the string object in the __string_objects
3363 section of the __OBJC segment. The Objective-C runtime will
3364 initialize the isa pointers of the string objects to point at the
3365 NXConstantString class object. */
3368 objc_build_string_object (tree string)
3370 tree constant_string_class;
3373 struct string_descriptor *desc, key;
3376 /* Prep the string argument. */
3377 string = fix_string_type (string);
3378 TREE_SET_CODE (string, STRING_CST);
3379 length = TREE_STRING_LENGTH (string) - 1;
3381 /* The target may have different ideas on how to construct an ObjC string
3382 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3383 constant CFString reference instead.
3384 At present, this is only supported for the NeXT runtime. */
3385 if (flag_next_runtime && targetcm.objc_construct_string_object)
3387 tree constructor = (*targetcm.objc_construct_string_object) (string);
3389 return build1 (NOP_EXPR, objc_object_type, constructor);
3392 /* Check whether the string class being used actually exists and has the
3393 correct ivar layout. */
3394 if (!string_layout_checked)
3396 string_layout_checked = -1;
3397 constant_string_class = lookup_interface (constant_string_id);
3398 internal_const_str_type = objc_build_internal_const_str_type ();
3400 if (!constant_string_class
3401 || !(constant_string_type
3402 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3403 error ("cannot find interface declaration for %qE",
3404 constant_string_id);
3405 /* The NSConstantString/NXConstantString ivar layout is now known. */
3406 else if (!check_string_class_template ())
3407 error ("interface %qE does not have valid constant string layout",
3408 constant_string_id);
3409 /* For the NeXT runtime, we can generate a literal reference
3410 to the string class, don't need to run a constructor. */
3411 else if (flag_next_runtime && !setup_string_decl ())
3412 error ("cannot find reference tag for class %qE",
3413 constant_string_id);
3416 string_layout_checked = 1; /* Success! */
3417 add_class_reference (constant_string_id);
3421 if (string_layout_checked == -1)
3422 return error_mark_node;
3424 /* Perhaps we already constructed a constant string just like this one? */
3425 key.literal = string;
3426 loc = htab_find_slot (string_htab, &key, INSERT);
3427 desc = (struct string_descriptor *) *loc;
3431 tree var, constructor;
3432 VEC(constructor_elt,gc) *v = NULL;
3433 *loc = desc = ggc_alloc_string_descriptor ();
3434 desc->literal = string;
3436 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
3437 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
3438 fields = TYPE_FIELDS (internal_const_str_type);
3439 CONSTRUCTOR_APPEND_ELT (v, fields,
3441 ? build_unary_op (input_location,
3442 ADDR_EXPR, string_class_decl, 0)
3443 : build_int_cst (NULL_TREE, 0));
3444 fields = DECL_CHAIN (fields);
3445 CONSTRUCTOR_APPEND_ELT (v, fields,
3446 build_unary_op (input_location,
3447 ADDR_EXPR, string, 1));
3448 fields = DECL_CHAIN (fields);
3449 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
3450 constructor = objc_build_constructor (internal_const_str_type, v);
3452 if (!flag_next_runtime)
3454 = objc_add_static_instance (constructor, constant_string_type);
3457 var = build_decl (input_location,
3458 CONST_DECL, NULL, TREE_TYPE (constructor));
3459 DECL_INITIAL (var) = constructor;
3460 TREE_STATIC (var) = 1;
3461 pushdecl_top_level (var);
3464 desc->constructor = constructor;
3467 addr = convert (build_pointer_type (constant_string_type),
3468 build_unary_op (input_location,
3469 ADDR_EXPR, desc->constructor, 1));
3474 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
3476 static GTY(()) int num_static_inst;
3479 objc_add_static_instance (tree constructor, tree class_decl)
3484 /* Find the list of static instances for the CLASS_DECL. Create one if
3486 for (chain = &objc_static_instances;
3487 *chain && TREE_VALUE (*chain) != class_decl;
3488 chain = &TREE_CHAIN (*chain));
3491 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
3492 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
3495 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
3496 decl = build_decl (input_location,
3497 VAR_DECL, get_identifier (buf), class_decl);
3498 TREE_STATIC (decl) = 1;
3499 DECL_ARTIFICIAL (decl) = 1;
3500 TREE_USED (decl) = 1;
3501 DECL_INITIAL (decl) = constructor;
3503 /* We may be writing something else just now.
3504 Postpone till end of input. */
3505 DECL_DEFER_OUTPUT (decl) = 1;
3506 pushdecl_top_level (decl);
3507 rest_of_decl_compilation (decl, 1, 0);
3509 /* Add the DECL to the head of this CLASS' list. */
3510 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
3515 /* Build a static constant CONSTRUCTOR
3516 with type TYPE and elements ELTS. */
3519 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
3521 tree constructor = build_constructor (type, elts);
3523 TREE_CONSTANT (constructor) = 1;
3524 TREE_STATIC (constructor) = 1;
3525 TREE_READONLY (constructor) = 1;
3528 /* Adjust for impedance mismatch. We should figure out how to build
3529 CONSTRUCTORs that consistently please both the C and C++ gods. */
3530 if (!VEC_index (constructor_elt, elts, 0)->index)
3531 TREE_TYPE (constructor) = init_list_type_node;
3537 /* Take care of defining and initializing _OBJC_SYMBOLS. */
3539 /* Predefine the following data type:
3547 void *defs[cls_def_cnt + cat_def_cnt];
3551 build_objc_symtab_template (void)
3553 tree fields, *chain = NULL;
3555 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
3557 /* long sel_ref_cnt; */
3558 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
3561 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
3563 /* short cls_def_cnt; */
3564 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
3566 /* short cat_def_cnt; */
3567 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
3569 if (imp_count || cat_count || !flag_next_runtime)
3571 /* void *defs[imp_count + cat_count (+ 1)]; */
3572 /* NB: The index is one less than the size of the array. */
3573 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
3574 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
3575 add_field_decl (array_type, "defs", &chain);
3578 objc_finish_struct (objc_symtab_template, fields);
3581 /* Create the initial value for the `defs' field of _objc_symtab.
3582 This is a CONSTRUCTOR. */
3585 init_def_list (tree type)
3588 struct imp_entry *impent;
3589 VEC(constructor_elt,gc) *v = NULL;
3592 for (impent = imp_list; impent; impent = impent->next)
3594 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
3596 expr = build_unary_op (input_location,
3597 ADDR_EXPR, impent->class_decl, 0);
3598 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3603 for (impent = imp_list; impent; impent = impent->next)
3605 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3607 expr = build_unary_op (input_location,
3608 ADDR_EXPR, impent->class_decl, 0);
3609 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3613 if (!flag_next_runtime)
3615 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
3616 if (static_instances_decl)
3617 expr = build_unary_op (input_location,
3618 ADDR_EXPR, static_instances_decl, 0);
3620 expr = integer_zero_node;
3622 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3625 return objc_build_constructor (type, v);
3628 /* Construct the initial value for all of _objc_symtab. */
3631 init_objc_symtab (tree type)
3633 VEC(constructor_elt,gc) *v = NULL;
3635 /* sel_ref_cnt = { ..., 5, ... } */
3637 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3638 build_int_cst (long_integer_type_node, 0));
3640 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
3642 if (flag_next_runtime || ! sel_ref_chain)
3643 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
3644 build_pointer_type (objc_selector_type),
3645 integer_zero_node));
3648 tree expr = build_unary_op (input_location, ADDR_EXPR,
3649 UOBJC_SELECTOR_TABLE_decl, 1);
3651 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3652 convert (build_pointer_type (objc_selector_type),
3656 /* cls_def_cnt = { ..., 5, ... } */
3658 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3659 build_int_cst (short_integer_type_node, imp_count));
3661 /* cat_def_cnt = { ..., 5, ... } */
3663 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3664 build_int_cst (short_integer_type_node, cat_count));
3666 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
3668 if (imp_count || cat_count || !flag_next_runtime)
3671 tree field = TYPE_FIELDS (type);
3672 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
3674 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
3677 return objc_build_constructor (type, v);
3680 /* Generate forward declarations for metadata such as
3681 'OBJC_CLASS_...'. */
3684 build_metadata_decl (const char *name, tree type)
3688 /* struct TYPE NAME_<name>; */
3689 decl = start_var_decl (type, synth_id_with_class_suffix
3691 objc_implementation_context));
3696 /* Push forward-declarations of all the categories so that
3697 init_def_list can use them in a CONSTRUCTOR. */
3700 forward_declare_categories (void)
3702 struct imp_entry *impent;
3703 tree sav = objc_implementation_context;
3705 for (impent = imp_list; impent; impent = impent->next)
3707 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3709 /* Set an invisible arg to synth_id_with_class_suffix. */
3710 objc_implementation_context = impent->imp_context;
3711 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
3712 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
3713 objc_category_template);
3716 objc_implementation_context = sav;
3719 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
3720 and initialized appropriately. */
3723 generate_objc_symtab_decl (void)
3726 build_objc_symtab_template ();
3727 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
3728 finish_var_decl (UOBJC_SYMBOLS_decl,
3729 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
3733 init_module_descriptor (tree type)
3736 VEC(constructor_elt,gc) *v = NULL;
3738 /* version = { 1, ... } */
3740 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
3741 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3743 /* size = { ..., sizeof (struct _objc_module), ... } */
3745 expr = convert (long_integer_type_node,
3746 size_in_bytes (objc_module_template));
3747 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3749 /* Don't provide any file name for security reasons. */
3750 /* name = { ..., "", ... } */
3752 expr = add_objc_string (get_identifier (""), class_names);
3753 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3755 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
3757 if (UOBJC_SYMBOLS_decl)
3758 expr = build_unary_op (input_location,
3759 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
3761 expr = null_pointer_node;
3762 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3764 return objc_build_constructor (type, v);
3767 /* Write out the data structures to describe Objective C classes defined.
3769 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
3772 build_module_descriptor (void)
3774 tree decls, *chain = NULL;
3777 push_lang_context (lang_name_c); /* extern "C" */
3780 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
3783 decls = add_field_decl (long_integer_type_node, "version", &chain);
3786 add_field_decl (long_integer_type_node, "size", &chain);
3789 add_field_decl (string_type_node, "name", &chain);
3791 /* struct _objc_symtab *symtab; */
3792 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
3793 get_identifier (UTAG_SYMTAB))),
3796 objc_finish_struct (objc_module_template, decls);
3798 /* Create an instance of "_objc_module". */
3799 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
3800 /* This is the root of the metadata for defined classes and categories, it
3801 is referenced by the runtime and, therefore, needed. */
3802 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
3803 finish_var_decl (UOBJC_MODULES_decl,
3804 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
3807 pop_lang_context ();
3811 /* The GNU runtime requires us to provide a static initializer function
3814 static void __objc_gnu_init (void) {
3815 __objc_exec_class (&L_OBJC_MODULES);
3819 build_module_initializer_routine (void)
3824 push_lang_context (lang_name_c); /* extern "C" */
3827 objc_push_parm (build_decl (input_location,
3828 PARM_DECL, NULL_TREE, void_type_node));
3830 objc_start_function (get_identifier (TAG_GNUINIT),
3831 build_function_type_list (void_type_node, NULL_TREE),
3832 NULL_TREE, NULL_TREE);
3834 objc_start_function (get_identifier (TAG_GNUINIT),
3835 build_function_type_list (void_type_node, NULL_TREE),
3836 NULL_TREE, objc_get_parm_info (0));
3838 body = c_begin_compound_stmt (true);
3839 add_stmt (build_function_call
3844 build_unary_op (input_location, ADDR_EXPR,
3845 UOBJC_MODULES_decl, 0))));
3846 add_stmt (c_end_compound_stmt (input_location, body, true));
3848 TREE_PUBLIC (current_function_decl) = 0;
3851 /* For Objective-C++, we will need to call __objc_gnu_init
3852 from objc_generate_static_init_call() below. */
3853 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
3856 GNU_INIT_decl = current_function_decl;
3860 pop_lang_context ();
3865 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
3866 to be called by the module initializer routine. */
3869 objc_static_init_needed_p (void)
3871 return (GNU_INIT_decl != NULL_TREE);
3874 /* Generate a call to the __objc_gnu_init initializer function. */
3877 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
3879 add_stmt (build_stmt (input_location, EXPR_STMT,
3880 build_function_call (input_location,
3881 GNU_INIT_decl, NULL_TREE)));
3885 #endif /* OBJCPLUS */
3887 /* Return the DECL of the string IDENT in the SECTION. */
3890 get_objc_string_decl (tree ident, enum string_section section)
3897 chain = class_names_chain;
3899 case meth_var_names:
3900 chain = meth_var_names_chain;
3902 case meth_var_types:
3903 chain = meth_var_types_chain;
3909 for (; chain != 0; chain = TREE_CHAIN (chain))
3910 if (TREE_VALUE (chain) == ident)
3911 return (TREE_PURPOSE (chain));
3917 /* Output references to all statically allocated objects. Return the DECL
3918 for the array built. */
3921 generate_static_references (void)
3923 tree expr = NULL_TREE;
3924 tree class_name, klass, decl;
3925 tree cl_chain, in_chain, type
3926 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
3927 int num_inst, num_class;
3929 VEC(constructor_elt,gc) *decls = NULL;
3931 if (flag_next_runtime)
3934 for (cl_chain = objc_static_instances, num_class = 0;
3935 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
3937 VEC(constructor_elt,gc) *v = NULL;
3939 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
3940 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
3942 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
3943 decl = start_var_decl (type, buf);
3945 /* Output {class_name, ...}. */
3946 klass = TREE_VALUE (cl_chain);
3947 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
3948 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3949 build_unary_op (input_location,
3950 ADDR_EXPR, class_name, 1));
3952 /* Output {..., instance, ...}. */
3953 for (in_chain = TREE_PURPOSE (cl_chain);
3954 in_chain; in_chain = TREE_CHAIN (in_chain))
3956 expr = build_unary_op (input_location,
3957 ADDR_EXPR, TREE_VALUE (in_chain), 1);
3958 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3961 /* Output {..., NULL}. */
3962 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
3964 expr = objc_build_constructor (TREE_TYPE (decl), v);
3965 finish_var_decl (decl, expr);
3966 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
3967 build_unary_op (input_location,
3968 ADDR_EXPR, decl, 1));
3971 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
3972 expr = objc_build_constructor (type, decls);
3973 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
3974 finish_var_decl (static_instances_decl, expr);
3977 static GTY(()) int selector_reference_idx;
3980 build_selector_reference_decl (void)
3985 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
3986 decl = start_var_decl (objc_selector_type, buf);
3992 build_selector_table_decl (void)
3996 if (flag_typed_selectors)
3998 build_selector_template ();
3999 temp = build_array_type (objc_selector_template, NULL_TREE);
4002 temp = build_array_type (objc_selector_type, NULL_TREE);
4004 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
4007 /* Just a handy wrapper for add_objc_string. */
4010 build_selector (tree ident)
4012 return convert (objc_selector_type,
4013 add_objc_string (ident, meth_var_names));
4016 /* Used only by build_*_selector_translation_table (). */
4018 diagnose_missing_method (tree meth, location_t here)
4022 for (method_chain = meth_var_names_chain;
4024 method_chain = TREE_CHAIN (method_chain))
4026 if (TREE_VALUE (method_chain) == meth)
4034 warning_at (here, 0, "creating selector for nonexistent method %qE",
4039 build_next_selector_translation_table (void)
4042 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
4045 tree decl = TREE_PURPOSE (chain);
4046 if (warn_selector && objc_implementation_context)
4050 loc = DECL_SOURCE_LOCATION (decl);
4052 loc = input_location;
4053 diagnose_missing_method (TREE_VALUE (chain), loc);
4056 expr = build_selector (TREE_VALUE (chain));
4060 /* Entries of this form are used for references to methods.
4061 The runtime re-writes these on start-up, but the compiler can't see
4062 that and optimizes it away unless we force it. */
4063 DECL_PRESERVE_P (decl) = 1;
4064 finish_var_decl (decl, expr);
4070 build_gnu_selector_translation_table (void)
4074 tree decl = NULL_TREE;*/
4075 VEC(constructor_elt,gc) *inits = NULL;
4077 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
4081 if (warn_selector && objc_implementation_context)
4082 diagnose_missing_method (TREE_VALUE (chain), input_location);
4084 expr = build_selector (TREE_VALUE (chain));
4085 /* add one for the '\0' character
4086 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
4089 if (flag_typed_selectors)
4091 VEC(constructor_elt,gc) *v = NULL;
4092 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
4093 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
4094 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
4095 expr = objc_build_constructor (objc_selector_template, v);
4098 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
4100 } /* each element in the chain */
4103 /* Cause the selector table (previously forward-declared)
4104 to be actually output. */
4107 if (flag_typed_selectors)
4109 VEC(constructor_elt,gc) *v = NULL;
4110 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
4111 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
4112 expr = objc_build_constructor (objc_selector_template, v);
4115 expr = integer_zero_node;
4117 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
4118 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
4120 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
4125 get_proto_encoding (tree proto)
4130 if (! METHOD_ENCODING (proto))
4132 encoding = encode_method_prototype (proto);
4133 METHOD_ENCODING (proto) = encoding;
4136 encoding = METHOD_ENCODING (proto);
4138 return add_objc_string (encoding, meth_var_types);
4141 return build_int_cst (NULL_TREE, 0);
4144 /* sel_ref_chain is a list whose "value" fields will be instances of
4145 identifier_node that represent the selector. LOC is the location of
4149 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
4151 tree *chain = &sel_ref_chain;
4157 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
4158 goto return_at_index;
4161 chain = &TREE_CHAIN (*chain);
4164 *chain = tree_cons (prototype, ident, NULL_TREE);
4167 expr = build_unary_op (loc, ADDR_EXPR,
4168 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
4169 build_int_cst (NULL_TREE, index)),
4171 return convert (objc_selector_type, expr);
4175 build_selector_reference (location_t loc, tree ident)
4177 tree *chain = &sel_ref_chain;
4183 if (TREE_VALUE (*chain) == ident)
4184 return (flag_next_runtime
4185 ? TREE_PURPOSE (*chain)
4186 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
4187 build_int_cst (NULL_TREE, index)));
4190 chain = &TREE_CHAIN (*chain);
4193 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
4195 *chain = tree_cons (expr, ident, NULL_TREE);
4197 return (flag_next_runtime
4199 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
4200 build_int_cst (NULL_TREE, index)));
4203 static GTY(()) int class_reference_idx;
4206 build_class_reference_decl (void)
4211 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
4212 decl = start_var_decl (objc_class_type, buf);
4217 /* Create a class reference, but don't create a variable to reference
4221 add_class_reference (tree ident)
4225 if ((chain = cls_ref_chain))
4230 if (ident == TREE_VALUE (chain))
4234 chain = TREE_CHAIN (chain);
4238 /* Append to the end of the list */
4239 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
4242 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
4245 /* Get a class reference, creating it if necessary. Also create the
4246 reference variable. */
4248 objc_get_class_reference (tree ident)
4250 tree orig_ident = (DECL_P (ident)
4253 ? OBJC_TYPE_NAME (ident)
4255 bool local_scope = false;
4258 if (processing_template_decl)
4259 /* Must wait until template instantiation time. */
4260 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
4263 if (TREE_CODE (ident) == TYPE_DECL)
4264 ident = (DECL_ORIGINAL_TYPE (ident)
4265 ? DECL_ORIGINAL_TYPE (ident)
4266 : TREE_TYPE (ident));
4270 && CP_TYPE_CONTEXT (ident) != global_namespace)
4274 if (local_scope || !(ident = objc_is_class_name (ident)))
4276 error ("%qE is not an Objective-C class name or alias",
4278 return error_mark_node;
4281 if (flag_next_runtime && !flag_zero_link)
4286 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
4287 if (TREE_VALUE (*chain) == ident)
4289 if (! TREE_PURPOSE (*chain))
4290 TREE_PURPOSE (*chain) = build_class_reference_decl ();
4292 return TREE_PURPOSE (*chain);
4295 decl = build_class_reference_decl ();
4296 *chain = tree_cons (decl, ident, NULL_TREE);
4303 add_class_reference (ident);
4305 params = build_tree_list (NULL_TREE,
4306 my_build_string_pointer
4307 (IDENTIFIER_LENGTH (ident) + 1,
4308 IDENTIFIER_POINTER (ident)));
4310 assemble_external (objc_get_class_decl);
4311 return build_function_call (input_location, objc_get_class_decl, params);
4315 /* For each string section we have a chain which maps identifier nodes
4316 to decls for the strings. */
4318 static GTY(()) int class_names_idx;
4319 static GTY(()) int meth_var_names_idx;
4320 static GTY(()) int meth_var_types_idx;
4323 add_objc_string (tree ident, enum string_section section)
4325 tree *chain, decl, type, string_expr;
4332 chain = &class_names_chain;
4333 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
4335 case meth_var_names:
4336 chain = &meth_var_names_chain;
4337 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
4339 case meth_var_types:
4340 chain = &meth_var_types_chain;
4341 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
4349 if (TREE_VALUE (*chain) == ident)
4350 return convert (string_type_node,
4351 build_unary_op (input_location,
4352 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
4354 chain = &TREE_CHAIN (*chain);
4357 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
4358 decl = start_var_decl (type, buf);
4359 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
4360 IDENTIFIER_POINTER (ident));
4361 TREE_CONSTANT (decl) = 1;
4362 finish_var_decl (decl, string_expr);
4364 *chain = tree_cons (decl, ident, NULL_TREE);
4366 return convert (string_type_node, build_unary_op (input_location,
4367 ADDR_EXPR, decl, 1));
4371 objc_declare_alias (tree alias_ident, tree class_ident)
4373 tree underlying_class;
4376 if (current_namespace != global_namespace) {
4377 error ("Objective-C declarations may only appear in global scope");
4379 #endif /* OBJCPLUS */
4381 if (!(underlying_class = objc_is_class_name (class_ident)))
4382 warning (0, "cannot find class %qE", class_ident);
4383 else if (objc_is_class_name (alias_ident))
4384 warning (0, "class %qE already exists", alias_ident);
4387 /* Implement @compatibility_alias as a typedef. */
4389 push_lang_context (lang_name_c); /* extern "C" */
4391 lang_hooks.decls.pushdecl (build_decl
4395 xref_tag (RECORD_TYPE, underlying_class)));
4397 pop_lang_context ();
4399 hash_class_name_enter (als_name_hash_list, alias_ident,
4405 objc_declare_class (tree ident_list)
4409 if (current_namespace != global_namespace) {
4410 error ("Objective-C declarations may only appear in global scope");
4412 #endif /* OBJCPLUS */
4414 for (list = ident_list; list; list = TREE_CHAIN (list))
4416 tree ident = TREE_VALUE (list);
4418 if (! objc_is_class_name (ident))
4420 tree record = lookup_name (ident), type = record;
4424 if (TREE_CODE (record) == TYPE_DECL)
4425 type = DECL_ORIGINAL_TYPE (record) ?
4426 DECL_ORIGINAL_TYPE (record) :
4429 if (!TYPE_HAS_OBJC_INFO (type)
4430 || !TYPE_OBJC_INTERFACE (type))
4432 error ("%qE redeclared as different kind of symbol",
4434 error ("previous declaration of %q+D",
4439 record = xref_tag (RECORD_TYPE, ident);
4440 INIT_TYPE_OBJC_INFO (record);
4441 /* In the case of a @class declaration, we store the ident
4442 in the TYPE_OBJC_INTERFACE. If later an @interface is
4443 found, we'll replace the ident with the interface. */
4444 TYPE_OBJC_INTERFACE (record) = ident;
4445 hash_class_name_enter (cls_name_hash_list, ident, NULL_TREE);
4451 objc_is_class_name (tree ident)
4455 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
4456 && identifier_global_value (ident))
4457 ident = identifier_global_value (ident);
4458 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
4459 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
4461 if (ident && TREE_CODE (ident) == RECORD_TYPE)
4462 ident = OBJC_TYPE_NAME (ident);
4464 if (ident && TREE_CODE (ident) == TYPE_DECL)
4466 tree type = TREE_TYPE (ident);
4467 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
4469 ident = DECL_NAME (ident);
4472 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
4475 if (lookup_interface (ident))
4478 target = hash_class_name_lookup (cls_name_hash_list, ident);
4482 target = hash_class_name_lookup (als_name_hash_list, ident);
4485 gcc_assert (target->list && target->list->value);
4486 return target->list->value;
4492 /* Check whether TYPE is either 'id' or 'Class'. */
4495 objc_is_id (tree type)
4497 if (type && TREE_CODE (type) == IDENTIFIER_NODE
4498 && identifier_global_value (type))
4499 type = identifier_global_value (type);
4501 if (type && TREE_CODE (type) == TYPE_DECL)
4502 type = TREE_TYPE (type);
4504 /* NB: This function may be called before the ObjC front-end has
4505 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
4506 return (objc_object_type && type
4507 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
4512 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
4513 class instance. This is needed by other parts of the compiler to
4514 handle ObjC types gracefully. */
4517 objc_is_object_ptr (tree type)
4521 type = TYPE_MAIN_VARIANT (type);
4522 if (!POINTER_TYPE_P (type))
4525 ret = objc_is_id (type);
4527 ret = objc_is_class_name (TREE_TYPE (type));
4533 objc_is_gcable_type (tree type, int or_strong_p)
4539 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
4541 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
4543 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
4545 type = TREE_TYPE (type);
4546 if (TREE_CODE (type) != RECORD_TYPE)
4548 name = TYPE_NAME (type);
4549 return (objc_is_class_name (name) != NULL_TREE);
4553 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
4555 if (expr == oldexpr)
4558 switch (TREE_CODE (expr))
4561 return objc_build_component_ref
4562 (objc_substitute_decl (TREE_OPERAND (expr, 0),
4565 DECL_NAME (TREE_OPERAND (expr, 1)));
4567 return build_array_ref (input_location,
4568 objc_substitute_decl (TREE_OPERAND (expr, 0),
4571 TREE_OPERAND (expr, 1));
4573 return build_indirect_ref (input_location,
4574 objc_substitute_decl (TREE_OPERAND (expr, 0),
4576 newexpr), RO_ARROW);
4583 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
4586 /* The LHS parameter contains the expression 'outervar->memberspec';
4587 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
4588 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
4591 = objc_substitute_decl
4592 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
4594 = (flag_objc_direct_dispatch
4595 ? objc_assign_ivar_fast_decl
4596 : objc_assign_ivar_decl);
4598 offs = convert (integer_type_node, build_unary_op (input_location,
4599 ADDR_EXPR, offs, 0));
4601 func_params = tree_cons (NULL_TREE,
4602 convert (objc_object_type, rhs),
4603 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
4604 tree_cons (NULL_TREE, offs,
4607 assemble_external (func);
4608 return build_function_call (input_location, func, func_params);
4612 objc_build_global_assignment (tree lhs, tree rhs)
4614 tree func_params = tree_cons (NULL_TREE,
4615 convert (objc_object_type, rhs),
4616 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
4617 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
4620 assemble_external (objc_assign_global_decl);
4621 return build_function_call (input_location,
4622 objc_assign_global_decl, func_params);
4626 objc_build_strong_cast_assignment (tree lhs, tree rhs)
4628 tree func_params = tree_cons (NULL_TREE,
4629 convert (objc_object_type, rhs),
4630 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
4631 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
4634 assemble_external (objc_assign_strong_cast_decl);
4635 return build_function_call (input_location,
4636 objc_assign_strong_cast_decl, func_params);
4640 objc_is_gcable_p (tree expr)
4642 return (TREE_CODE (expr) == COMPONENT_REF
4643 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
4644 : TREE_CODE (expr) == ARRAY_REF
4645 ? (objc_is_gcable_p (TREE_TYPE (expr))
4646 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
4647 : TREE_CODE (expr) == ARRAY_TYPE
4648 ? objc_is_gcable_p (TREE_TYPE (expr))
4650 ? objc_is_gcable_type (expr, 1)
4651 : (objc_is_gcable_p (TREE_TYPE (expr))
4653 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
4657 objc_is_ivar_reference_p (tree expr)
4659 return (TREE_CODE (expr) == ARRAY_REF
4660 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
4661 : TREE_CODE (expr) == COMPONENT_REF
4662 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
4667 objc_is_global_reference_p (tree expr)
4669 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
4670 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
4672 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
4677 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
4679 tree result = NULL_TREE, outer;
4680 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
4682 /* This function is currently only used with the next runtime with
4683 garbage collection enabled (-fobjc-gc). */
4684 gcc_assert (flag_next_runtime);
4686 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
4687 will have been transformed to the form '*(type *)&expr'. */
4688 if (TREE_CODE (lhs) == INDIRECT_REF)
4690 outer = TREE_OPERAND (lhs, 0);
4692 while (!strong_cast_p
4693 && (CONVERT_EXPR_P (outer)
4694 || TREE_CODE (outer) == NON_LVALUE_EXPR))
4696 tree lhstype = TREE_TYPE (outer);
4698 /* Descend down the cast chain, and record the first objc_gc
4700 if (POINTER_TYPE_P (lhstype))
4703 = lookup_attribute ("objc_gc",
4704 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
4710 outer = TREE_OPERAND (outer, 0);
4714 /* If we have a __strong cast, it trumps all else. */
4717 if (modifycode != NOP_EXPR)
4718 goto invalid_pointer_arithmetic;
4720 if (warn_assign_intercept)
4721 warning (0, "strong-cast assignment has been intercepted");
4723 result = objc_build_strong_cast_assignment (lhs, rhs);
4728 /* the lhs must be of a suitable type, regardless of its underlying
4730 if (!objc_is_gcable_p (lhs))
4736 && (TREE_CODE (outer) == COMPONENT_REF
4737 || TREE_CODE (outer) == ARRAY_REF))
4738 outer = TREE_OPERAND (outer, 0);
4740 if (TREE_CODE (outer) == INDIRECT_REF)
4742 outer = TREE_OPERAND (outer, 0);
4746 outer_gc_p = objc_is_gcable_p (outer);
4748 /* Handle ivar assignments. */
4749 if (objc_is_ivar_reference_p (lhs))
4751 /* if the struct to the left of the ivar is not an Objective-C object (__strong
4752 doesn't cut it here), the best we can do here is suggest a cast. */
4753 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
4755 /* We may still be able to use the global write barrier... */
4756 if (!indirect_p && objc_is_global_reference_p (outer))
4757 goto global_reference;
4760 if (modifycode == NOP_EXPR)
4762 if (warn_assign_intercept)
4763 warning (0, "strong-cast may possibly be needed");
4769 if (modifycode != NOP_EXPR)
4770 goto invalid_pointer_arithmetic;
4772 if (warn_assign_intercept)
4773 warning (0, "instance variable assignment has been intercepted");
4775 result = objc_build_ivar_assignment (outer, lhs, rhs);
4780 /* Likewise, intercept assignment to global/static variables if their type is
4782 if (objc_is_global_reference_p (outer))
4788 if (modifycode != NOP_EXPR)
4790 invalid_pointer_arithmetic:
4792 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
4797 if (warn_assign_intercept)
4798 warning (0, "global/static variable assignment has been intercepted");
4800 result = objc_build_global_assignment (lhs, rhs);
4803 /* In all other cases, fall back to the normal mechanism. */
4808 struct GTY(()) interface_tuple {
4813 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
4816 hash_interface (const void *p)
4818 const struct interface_tuple *d = (const struct interface_tuple *) p;
4819 return IDENTIFIER_HASH_VALUE (d->id);
4823 eq_interface (const void *p1, const void *p2)
4825 const struct interface_tuple *d = (const struct interface_tuple *) p1;
4830 lookup_interface (tree ident)
4833 if (ident && TREE_CODE (ident) == TYPE_DECL)
4834 ident = DECL_NAME (ident);
4837 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
4841 struct interface_tuple **slot;
4846 slot = (struct interface_tuple **)
4847 htab_find_slot_with_hash (interface_htab, ident,
4848 IDENTIFIER_HASH_VALUE (ident),
4851 i = (*slot)->class_name;
4857 /* Implement @defs (<classname>) within struct bodies. */
4860 objc_get_class_ivars (tree class_name)
4862 tree interface = lookup_interface (class_name);
4865 return get_class_ivars (interface, true);
4867 error ("cannot find interface declaration for %qE",
4870 return error_mark_node;
4873 /* Called when checking the variables in a struct. If we are not
4874 doing the ivars list inside an @interface context, then returns
4875 fieldlist unchanged. Else, returns the list of class ivars.
4878 objc_get_interface_ivars (tree fieldlist)
4880 if (!objc_collecting_ivars || !objc_interface_context
4881 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE
4882 || CLASS_SUPER_NAME (objc_interface_context) == NULL_TREE)
4885 return get_class_ivars (objc_interface_context, true);
4888 /* Used by: build_private_template, continue_class,
4889 and for @defs constructs. */
4892 get_class_ivars (tree interface, bool inherited)
4894 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4896 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4897 by the current class (i.e., they do not include super-class ivars).
4898 However, the CLASS_IVARS list will be side-effected by a call to
4899 finish_struct(), which will fill in field offsets. */
4900 if (!CLASS_IVARS (interface))
4901 CLASS_IVARS (interface) = ivar_chain;
4906 while (CLASS_SUPER_NAME (interface))
4908 /* Prepend super-class ivars. */
4909 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4910 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4918 /* Exception handling constructs. We begin by having the parser do most
4919 of the work and passing us blocks. What we do next depends on whether
4920 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
4921 We abstract all of this in a handful of appropriately named routines. */
4923 /* Stack of open try blocks. */
4925 struct objc_try_context
4927 struct objc_try_context *outer;
4929 /* Statements (or statement lists) as processed by the parser. */
4933 /* Some file position locations. */
4934 location_t try_locus;
4935 location_t end_try_locus;
4936 location_t end_catch_locus;
4937 location_t finally_locus;
4938 location_t end_finally_locus;
4940 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
4941 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
4944 /* The CATCH_EXPR of an open @catch clause. */
4947 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
4953 static struct objc_try_context *cur_try_context;
4955 static GTY(()) tree objc_eh_personality_decl;
4957 /* This hook, called via lang_eh_runtime_type, generates a runtime object
4958 that represents TYPE. For Objective-C, this is just the class name. */
4959 /* ??? Isn't there a class object or some such? Is it easy to get? */
4963 objc_eh_runtime_type (tree type)
4965 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
4969 objc_eh_personality (void)
4971 if (!flag_objc_sjlj_exceptions && !objc_eh_personality_decl)
4972 objc_eh_personality_decl = build_personality_function ("gnu_objc");
4973 return objc_eh_personality_decl;
4977 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
4978 of Darwin, we'll arrange for it to be initialized (and associated
4979 with a binding) later. */
4982 objc_build_exc_ptr (void)
4984 if (flag_objc_sjlj_exceptions)
4986 tree var = cur_try_context->caught_decl;
4989 var = objc_create_temporary_var (objc_object_type, NULL);
4990 cur_try_context->caught_decl = var;
4997 t = built_in_decls[BUILT_IN_EH_POINTER];
4998 t = build_call_expr (t, 1, integer_zero_node);
4999 return fold_convert (objc_object_type, t);
5003 /* Build "objc_exception_try_exit(&_stack)". */
5006 next_sjlj_build_try_exit (void)
5009 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
5010 t = tree_cons (NULL, t, NULL);
5011 t = build_function_call (input_location,
5012 objc_exception_try_exit_decl, t);
5017 objc_exception_try_enter (&_stack);
5018 if (_setjmp(&_stack.buf))
5022 Return the COND_EXPR. Note that the THEN and ELSE fields are left
5023 empty, ready for the caller to fill them in. */
5026 next_sjlj_build_enter_and_setjmp (void)
5028 tree t, enter, sj, cond;
5030 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
5031 t = tree_cons (NULL, t, NULL);
5032 enter = build_function_call (input_location,
5033 objc_exception_try_enter_decl, t);
5035 t = objc_build_component_ref (cur_try_context->stack_decl,
5036 get_identifier ("buf"));
5037 t = build_fold_addr_expr_loc (input_location, t);
5039 /* Convert _setjmp argument to type that is expected. */
5040 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
5041 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
5043 t = convert (ptr_type_node, t);
5045 t = convert (ptr_type_node, t);
5047 t = tree_cons (NULL, t, NULL);
5048 sj = build_function_call (input_location,
5049 objc_setjmp_decl, t);
5051 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
5052 cond = c_common_truthvalue_conversion (input_location, cond);
5054 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
5059 DECL = objc_exception_extract(&_stack); */
5062 next_sjlj_build_exc_extract (tree decl)
5066 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
5067 t = tree_cons (NULL, t, NULL);
5068 t = build_function_call (input_location,
5069 objc_exception_extract_decl, t);
5070 t = convert (TREE_TYPE (decl), t);
5071 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
5077 if (objc_exception_match(obj_get_class(TYPE), _caught)
5084 objc_exception_try_exit(&_stack);
5086 from the sequence of CATCH_EXPRs in the current try context. */
5089 next_sjlj_build_catch_list (void)
5091 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
5093 tree *last = &catch_seq;
5094 bool saw_id = false;
5096 for (; !tsi_end_p (i); tsi_next (&i))
5098 tree stmt = tsi_stmt (i);
5099 tree type = CATCH_TYPES (stmt);
5100 tree body = CATCH_BODY (stmt);
5112 if (type == error_mark_node)
5113 cond = error_mark_node;
5116 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
5117 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
5118 args = tree_cons (NULL, t, args);
5119 t = build_function_call (input_location,
5120 objc_exception_match_decl, args);
5121 cond = c_common_truthvalue_conversion (input_location, t);
5123 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
5124 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
5127 last = &COND_EXPR_ELSE (t);
5133 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
5134 cur_try_context->caught_decl);
5135 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
5136 append_to_statement_list (t, last);
5138 t = next_sjlj_build_try_exit ();
5139 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
5140 append_to_statement_list (t, last);
5146 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
5147 exception handling. We aim to build:
5150 struct _objc_exception_data _stack;
5154 objc_exception_try_enter (&_stack);
5155 if (_setjmp(&_stack.buf))
5157 id _caught = objc_exception_extract(&_stack);
5158 objc_exception_try_enter (&_stack);
5159 if (_setjmp(&_stack.buf))
5160 _rethrow = objc_exception_extract(&_stack);
5170 objc_exception_try_exit(&_stack);
5173 objc_exception_throw(_rethrow);
5177 If CATCH-LIST is empty, we can omit all of the block containing
5178 "_caught" except for the setting of _rethrow. Note the use of
5179 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
5180 but handles goto and other exits from the block. */
5183 next_sjlj_build_try_catch_finally (void)
5185 tree rethrow_decl, stack_decl, t;
5186 tree catch_seq, try_fin, bind;
5188 /* Create the declarations involved. */
5189 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
5190 stack_decl = objc_create_temporary_var (t, NULL);
5191 cur_try_context->stack_decl = stack_decl;
5193 rethrow_decl = objc_create_temporary_var (objc_object_type, NULL);
5194 cur_try_context->rethrow_decl = rethrow_decl;
5195 TREE_CHAIN (rethrow_decl) = stack_decl;
5197 /* Build the outermost variable binding level. */
5198 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
5199 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
5200 TREE_SIDE_EFFECTS (bind) = 1;
5202 /* Initialize rethrow_decl. */
5203 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
5204 convert (objc_object_type, null_pointer_node));
5205 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
5206 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
5208 /* Build the outermost TRY_FINALLY_EXPR. */
5209 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
5210 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
5211 TREE_SIDE_EFFECTS (try_fin) = 1;
5212 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
5214 /* Create the complete catch sequence. */
5215 if (cur_try_context->catch_list)
5217 tree caught_decl = objc_build_exc_ptr ();
5218 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
5219 TREE_SIDE_EFFECTS (catch_seq) = 1;
5221 t = next_sjlj_build_exc_extract (caught_decl);
5222 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
5224 t = next_sjlj_build_enter_and_setjmp ();
5225 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
5226 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
5227 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
5230 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
5231 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
5233 /* Build the main register-and-try if statement. */
5234 t = next_sjlj_build_enter_and_setjmp ();
5235 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
5236 COND_EXPR_THEN (t) = catch_seq;
5237 COND_EXPR_ELSE (t) = cur_try_context->try_body;
5238 TREE_OPERAND (try_fin, 0) = t;
5240 /* Build the complete FINALLY statement list. */
5241 t = next_sjlj_build_try_exit ();
5242 t = build_stmt (input_location, COND_EXPR,
5243 c_common_truthvalue_conversion
5244 (input_location, rethrow_decl),
5246 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
5247 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
5249 append_to_statement_list (cur_try_context->finally_body,
5250 &TREE_OPERAND (try_fin, 1));
5252 t = tree_cons (NULL, rethrow_decl, NULL);
5253 t = build_function_call (input_location,
5254 objc_exception_throw_decl, t);
5255 t = build_stmt (input_location, COND_EXPR,
5256 c_common_truthvalue_conversion (input_location,
5259 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
5260 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
5265 /* Called just after parsing the @try and its associated BODY. We now
5266 must prepare for the tricky bits -- handling the catches and finally. */
5269 objc_begin_try_stmt (location_t try_locus, tree body)
5271 struct objc_try_context *c = XCNEW (struct objc_try_context);
5272 c->outer = cur_try_context;
5274 c->try_locus = try_locus;
5275 c->end_try_locus = input_location;
5276 cur_try_context = c;
5278 /* -fobjc-exceptions is required to enable Objective-C exceptions.
5279 For example, on Darwin, ObjC exceptions require a sufficiently
5280 recent version of the runtime, so the user must ask for them
5281 explicitly. On other platforms, at the moment -fobjc-exceptions
5282 triggers -fexceptions which again is required for exceptions to
5285 if (!flag_objc_exceptions)
5287 error_at (try_locus, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
5290 if (flag_objc_sjlj_exceptions)
5291 objc_mark_locals_volatile (NULL);
5294 /* Called just after parsing "@catch (parm)". Open a binding level,
5295 enter DECL into the binding level, and initialize it. Leave the
5296 binding level open while the body of the compound statement is parsed. */
5299 objc_begin_catch_clause (tree decl)
5301 tree compound, type, t;
5303 /* Begin a new scope that the entire catch clause will live in. */
5304 compound = c_begin_compound_stmt (true);
5306 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
5307 decl = build_decl (input_location,
5308 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
5309 lang_hooks.decls.pushdecl (decl);
5311 /* Since a decl is required here by syntax, don't warn if its unused. */
5312 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
5313 be what the previous objc implementation did. */
5314 TREE_USED (decl) = 1;
5315 DECL_READ_P (decl) = 1;
5317 /* Verify that the type of the catch is valid. It must be a pointer
5318 to an Objective-C class, or "id" (which is catch-all). */
5319 type = TREE_TYPE (decl);
5321 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
5323 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
5325 error ("@catch parameter is not a known Objective-C class type");
5326 type = error_mark_node;
5328 else if (cur_try_context->catch_list)
5330 /* Examine previous @catch clauses and see if we've already
5331 caught the type in question. */
5332 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
5333 for (; !tsi_end_p (i); tsi_next (&i))
5335 tree stmt = tsi_stmt (i);
5336 t = CATCH_TYPES (stmt);
5337 if (t == error_mark_node)
5339 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
5341 warning (0, "exception of type %<%T%> will be caught",
5343 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
5344 TREE_TYPE (t ? t : objc_object_type));
5350 /* Record the data for the catch in the try context so that we can
5351 finalize it later. */
5352 t = build_stmt (input_location, CATCH_EXPR, type, compound);
5353 cur_try_context->current_catch = t;
5355 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
5356 t = objc_build_exc_ptr ();
5357 t = convert (TREE_TYPE (decl), t);
5358 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
5362 /* Called just after parsing the closing brace of a @catch clause. Close
5363 the open binding level, and record a CATCH_EXPR for it. */
5366 objc_finish_catch_clause (void)
5368 tree c = cur_try_context->current_catch;
5369 cur_try_context->current_catch = NULL;
5370 cur_try_context->end_catch_locus = input_location;
5372 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
5373 append_to_statement_list (c, &cur_try_context->catch_list);
5376 /* Called after parsing a @finally clause and its associated BODY.
5377 Record the body for later placement. */
5380 objc_build_finally_clause (location_t finally_locus, tree body)
5382 cur_try_context->finally_body = body;
5383 cur_try_context->finally_locus = finally_locus;
5384 cur_try_context->end_finally_locus = input_location;
5387 /* Called to finalize a @try construct. */
5390 objc_finish_try_stmt (void)
5392 struct objc_try_context *c = cur_try_context;
5395 if (c->catch_list == NULL && c->finally_body == NULL)
5396 error ("%<@try%> without %<@catch%> or %<@finally%>");
5398 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
5399 if (flag_objc_sjlj_exceptions)
5401 bool save = in_late_binary_op;
5402 in_late_binary_op = true;
5403 if (!cur_try_context->finally_body)
5405 cur_try_context->finally_locus = input_location;
5406 cur_try_context->end_finally_locus = input_location;
5408 stmt = next_sjlj_build_try_catch_finally ();
5409 in_late_binary_op = save;
5413 /* Otherwise, nest the CATCH inside a FINALLY. */
5417 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
5418 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
5420 if (c->finally_body)
5422 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
5423 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
5428 cur_try_context = c->outer;
5434 objc_build_throw_stmt (location_t loc, tree throw_expr)
5438 if (!flag_objc_exceptions)
5440 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
5443 if (throw_expr == NULL)
5445 /* If we're not inside a @catch block, there is no "current
5446 exception" to be rethrown. */
5447 if (cur_try_context == NULL
5448 || cur_try_context->current_catch == NULL)
5450 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
5454 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
5455 value that we get from the runtime. */
5456 throw_expr = objc_build_exc_ptr ();
5459 /* A throw is just a call to the runtime throw function with the
5460 object as a parameter. */
5461 args = tree_cons (NULL, throw_expr, NULL);
5462 return add_stmt (build_function_call (loc,
5463 objc_exception_throw_decl, args));
5467 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
5471 /* First lock the mutex. */
5472 mutex = save_expr (mutex);
5473 args = tree_cons (NULL, mutex, NULL);
5474 call = build_function_call (input_location,
5475 objc_sync_enter_decl, args);
5476 SET_EXPR_LOCATION (call, start_locus);
5479 /* Build the mutex unlock. */
5480 args = tree_cons (NULL, mutex, NULL);
5481 call = build_function_call (input_location,
5482 objc_sync_exit_decl, args);
5483 SET_EXPR_LOCATION (call, input_location);
5485 /* Put the that and the body in a TRY_FINALLY. */
5486 objc_begin_try_stmt (start_locus, body);
5487 objc_build_finally_clause (input_location, call);
5488 return objc_finish_try_stmt ();
5492 /* Predefine the following data type:
5494 struct _objc_exception_data
5496 int buf[OBJC_JBLEN];
5500 /* The following yuckiness should prevent users from having to #include
5501 <setjmp.h> in their code... */
5503 /* Define to a harmless positive value so the below code doesn't die. */
5505 #define OBJC_JBLEN 18
5509 build_next_objc_exception_stuff (void)
5511 tree decls, temp_type, *chain = NULL;
5513 objc_exception_data_template
5514 = objc_start_struct (get_identifier (UTAG_EXCDATA));
5516 /* int buf[OBJC_JBLEN]; */
5518 temp_type = build_sized_array_type (integer_type_node, OBJC_JBLEN);
5519 decls = add_field_decl (temp_type, "buf", &chain);
5521 /* void *pointers[4]; */
5523 temp_type = build_sized_array_type (ptr_type_node, 4);
5524 add_field_decl (temp_type, "pointers", &chain);
5526 objc_finish_struct (objc_exception_data_template, decls);
5528 /* int _setjmp(...); */
5529 /* If the user includes <setjmp.h>, this shall be superseded by
5530 'int _setjmp(jmp_buf);' */
5531 temp_type = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5533 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
5535 /* id objc_exception_extract(struct _objc_exception_data *); */
5537 = build_function_type_list (objc_object_type,
5538 build_pointer_type (objc_exception_data_template),
5540 objc_exception_extract_decl
5541 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
5543 /* void objc_exception_try_enter(struct _objc_exception_data *); */
5544 /* void objc_exception_try_exit(struct _objc_exception_data *); */
5546 = build_function_type_list (void_type_node,
5547 build_pointer_type (objc_exception_data_template),
5549 objc_exception_try_enter_decl
5550 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
5552 objc_exception_try_exit_decl
5553 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
5556 /* int objc_exception_match(id, id); */
5558 = build_function_type_list (integer_type_node,
5559 objc_object_type, objc_object_type, NULL_TREE);
5560 objc_exception_match_decl
5561 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
5564 /* id objc_assign_ivar (id, id, unsigned int); */
5565 /* id objc_assign_ivar_Fast (id, id, unsigned int)
5566 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
5568 = build_function_type_list (objc_object_type,
5573 objc_assign_ivar_decl
5574 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
5576 #ifdef OFFS_ASSIGNIVAR_FAST
5577 objc_assign_ivar_fast_decl
5578 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
5579 NOT_BUILT_IN, NULL, NULL_TREE);
5580 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
5581 = tree_cons (get_identifier ("hard_coded_address"),
5582 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
5585 /* Default to slower ivar method. */
5586 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
5589 /* id objc_assign_global (id, id *); */
5590 /* id objc_assign_strongCast (id, id *); */
5591 temp_type = build_function_type_list (objc_object_type,
5593 build_pointer_type (objc_object_type),
5595 objc_assign_global_decl
5596 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
5598 objc_assign_strong_cast_decl
5599 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
5604 build_objc_exception_stuff (void)
5606 tree noreturn_list, nothrow_list, temp_type;
5608 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
5609 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
5611 /* void objc_exception_throw(id) __attribute__((noreturn)); */
5612 /* void objc_sync_enter(id); */
5613 /* void objc_sync_exit(id); */
5614 temp_type = build_function_type_list (void_type_node,
5617 objc_exception_throw_decl
5618 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
5620 objc_sync_enter_decl
5621 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
5622 NULL, nothrow_list);
5624 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
5625 NULL, nothrow_list);
5628 /* Construct a C struct corresponding to ObjC class CLASS, with the same
5631 struct <classname> {
5632 struct _objc_class *isa;
5637 build_private_template (tree klass)
5639 if (!CLASS_STATIC_TEMPLATE (klass))
5641 tree record = objc_build_struct (klass,
5642 get_class_ivars (klass, false),
5643 CLASS_SUPER_NAME (klass));
5645 /* Set the TREE_USED bit for this struct, so that stab generator
5646 can emit stabs for this struct type. */
5647 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
5648 TREE_USED (TYPE_STUB_DECL (record)) = 1;
5650 /* Copy the attributes from the class to the type. */
5651 if (TREE_DEPRECATED (klass))
5652 TREE_DEPRECATED (record) = 1;
5656 /* Begin code generation for protocols... */
5658 /* struct _objc_protocol {
5659 struct _objc_class *isa;
5660 char *protocol_name;
5661 struct _objc_protocol **protocol_list;
5662 struct _objc__method_prototype_list *instance_methods;
5663 struct _objc__method_prototype_list *class_methods;
5667 build_protocol_template (void)
5669 tree ptype, decls, *chain = NULL;
5671 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
5673 /* struct _objc_class *isa; */
5674 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5675 get_identifier (UTAG_CLASS)));
5676 decls = add_field_decl (ptype, "isa", &chain);
5678 /* char *protocol_name; */
5679 add_field_decl (string_type_node, "protocol_name", &chain);
5681 /* struct _objc_protocol **protocol_list; */
5682 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
5683 add_field_decl (ptype, "protocol_list", &chain);
5685 /* struct _objc__method_prototype_list *instance_methods; */
5686 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
5688 /* struct _objc__method_prototype_list *class_methods; */
5689 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
5691 objc_finish_struct (objc_protocol_template, decls);
5695 build_descriptor_table_initializer (tree type, tree entries)
5697 VEC(constructor_elt,gc) *inits = NULL;
5701 VEC(constructor_elt,gc) *elts = NULL;
5703 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5704 build_selector (METHOD_SEL_NAME (entries)));
5705 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
5706 add_objc_string (METHOD_ENCODING (entries),
5709 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5710 objc_build_constructor (type, elts));
5712 entries = DECL_CHAIN (entries);
5716 return objc_build_constructor (build_array_type (type, 0), inits);
5719 /* struct objc_method_prototype_list {
5721 struct objc_method_prototype {
5728 build_method_prototype_list_template (tree list_type, int size)
5730 tree objc_ivar_list_record;
5731 tree array_type, decls, *chain = NULL;
5733 /* Generate an unnamed struct definition. */
5735 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5737 /* int method_count; */
5738 decls = add_field_decl (integer_type_node, "method_count", &chain);
5740 /* struct objc_method method_list[]; */
5741 array_type = build_sized_array_type (list_type, size);
5742 add_field_decl (array_type, "method_list", &chain);
5744 objc_finish_struct (objc_ivar_list_record, decls);
5746 return objc_ivar_list_record;
5750 build_method_prototype_template (void)
5753 tree decls, *chain = NULL;
5755 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
5758 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
5760 /* char *method_types; */
5761 add_field_decl (string_type_node, "method_types", &chain);
5763 objc_finish_struct (proto_record, decls);
5765 return proto_record;
5769 objc_method_parm_type (tree type)
5771 type = TREE_VALUE (TREE_TYPE (type));
5772 if (TREE_CODE (type) == TYPE_DECL)
5773 type = TREE_TYPE (type);
5778 objc_encoded_type_size (tree type)
5780 int sz = int_size_in_bytes (type);
5782 /* Make all integer and enum types at least as large
5784 if (sz > 0 && INTEGRAL_TYPE_P (type))
5785 sz = MAX (sz, int_size_in_bytes (integer_type_node));
5786 /* Treat arrays as pointers, since that's how they're
5788 else if (TREE_CODE (type) == ARRAY_TYPE)
5789 sz = int_size_in_bytes (ptr_type_node);
5793 /* Encode a method prototype.
5795 The format is described in gcc/doc/objc.texi, section 'Method
5799 encode_method_prototype (tree method_decl)
5806 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
5807 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
5809 /* Encode return type. */
5810 encode_type (objc_method_parm_type (method_decl),
5811 obstack_object_size (&util_obstack),
5812 OBJC_ENCODE_INLINE_DEFS);
5815 /* The first two arguments (self and _cmd) are pointers; account for
5817 i = int_size_in_bytes (ptr_type_node);
5818 parm_offset = 2 * i;
5819 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5820 parms = DECL_CHAIN (parms))
5822 tree type = objc_method_parm_type (parms);
5823 int sz = objc_encoded_type_size (type);
5825 /* If a type size is not known, bail out. */
5828 error ("type %q+D does not have a known size",
5830 /* Pretend that the encoding succeeded; the compilation will
5831 fail nevertheless. */
5832 goto finish_encoding;
5837 sprintf (buf, "%d@0:%d", parm_offset, i);
5838 obstack_grow (&util_obstack, buf, strlen (buf));
5840 /* Argument types. */
5841 parm_offset = 2 * i;
5842 for (parms = METHOD_SEL_ARGS (method_decl); parms;
5843 parms = DECL_CHAIN (parms))
5845 tree type = objc_method_parm_type (parms);
5847 /* Process argument qualifiers for user supplied arguments. */
5848 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
5851 encode_type (type, obstack_object_size (&util_obstack),
5852 OBJC_ENCODE_INLINE_DEFS);
5854 /* Compute offset. */
5855 sprintf (buf, "%d", parm_offset);
5856 parm_offset += objc_encoded_type_size (type);
5858 obstack_grow (&util_obstack, buf, strlen (buf));
5862 obstack_1grow (&util_obstack, '\0');
5863 result = get_identifier (XOBFINISH (&util_obstack, char *));
5864 obstack_free (&util_obstack, util_firstobj);
5869 generate_descriptor_table (tree type, const char *name, int size, tree list,
5873 VEC(constructor_elt,gc) *v = NULL;
5875 decl = start_var_decl (type, synth_id_with_class_suffix (name, proto));
5877 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
5878 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5880 finish_var_decl (decl, objc_build_constructor (type, v));
5886 generate_method_descriptors (tree protocol)
5888 tree initlist, chain, method_list_template;
5891 if (!objc_method_prototype_template)
5892 objc_method_prototype_template = build_method_prototype_template ();
5894 chain = PROTOCOL_CLS_METHODS (protocol);
5897 size = list_length (chain);
5899 method_list_template
5900 = build_method_prototype_list_template (objc_method_prototype_template,
5904 = build_descriptor_table_initializer (objc_method_prototype_template,
5907 UOBJC_CLASS_METHODS_decl
5908 = generate_descriptor_table (method_list_template,
5909 "_OBJC_PROTOCOL_CLASS_METHODS",
5910 size, initlist, protocol);
5913 UOBJC_CLASS_METHODS_decl = 0;
5915 chain = PROTOCOL_NST_METHODS (protocol);
5918 size = list_length (chain);
5920 method_list_template
5921 = build_method_prototype_list_template (objc_method_prototype_template,
5924 = build_descriptor_table_initializer (objc_method_prototype_template,
5927 UOBJC_INSTANCE_METHODS_decl
5928 = generate_descriptor_table (method_list_template,
5929 "_OBJC_PROTOCOL_INSTANCE_METHODS",
5930 size, initlist, protocol);
5933 UOBJC_INSTANCE_METHODS_decl = 0;
5937 generate_protocol_references (tree plist)
5941 /* Forward declare protocols referenced. */
5942 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5944 tree proto = TREE_VALUE (lproto);
5946 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
5947 && PROTOCOL_NAME (proto))
5949 if (! PROTOCOL_FORWARD_DECL (proto))
5950 build_protocol_reference (proto);
5952 if (PROTOCOL_LIST (proto))
5953 generate_protocol_references (PROTOCOL_LIST (proto));
5958 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
5962 objc_generate_cxx_ctor_or_dtor (bool dtor)
5964 tree fn, body, compound_stmt, ivar;
5966 /* - (id) .cxx_construct { ... return self; } */
5967 /* - (void) .cxx_construct { ... } */
5969 objc_start_method_definition
5970 (false /* is_class_method */,
5971 objc_build_method_signature (false /* is_class_method */,
5972 build_tree_list (NULL_TREE,
5975 : objc_object_type),
5976 get_identifier (dtor
5978 : TAG_CXX_CONSTRUCT),
5979 make_node (TREE_LIST),
5981 body = begin_function_body ();
5982 compound_stmt = begin_compound_stmt (0);
5984 ivar = CLASS_IVARS (implementation_template);
5985 /* Destroy ivars in reverse order. */
5987 ivar = nreverse (copy_list (ivar));
5989 for (; ivar; ivar = TREE_CHAIN (ivar))
5991 if (TREE_CODE (ivar) == FIELD_DECL)
5993 tree type = TREE_TYPE (ivar);
5995 /* Call the ivar's default constructor or destructor. Do not
5996 call the destructor unless a corresponding constructor call
5997 has also been made (or is not needed). */
5998 if (MAYBE_CLASS_TYPE_P (type)
6000 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6001 && (!TYPE_NEEDS_CONSTRUCTING (type)
6002 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6003 : (TYPE_NEEDS_CONSTRUCTING (type)
6004 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
6006 (build_special_member_call
6007 (build_ivar_reference (DECL_NAME (ivar)),
6008 dtor ? complete_dtor_identifier : complete_ctor_identifier,
6009 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
6013 /* The constructor returns 'self'. */
6015 finish_return_stmt (self_decl);
6017 finish_compound_stmt (compound_stmt);
6018 finish_function_body (body);
6019 fn = current_function_decl;
6021 objc_finish_method_definition (fn);
6024 /* The following routine will examine the current @interface for any
6025 non-POD C++ ivars requiring non-trivial construction and/or
6026 destruction, and then synthesize special '- .cxx_construct' and/or
6027 '- .cxx_destruct' methods which will run the appropriate
6028 construction or destruction code. Note that ivars inherited from
6029 super-classes are _not_ considered. */
6031 objc_generate_cxx_cdtors (void)
6033 bool need_ctor = false, need_dtor = false;
6036 /* Error case, due to possibly an extra @end. */
6037 if (!objc_implementation_context)
6040 /* We do not want to do this for categories, since they do not have
6043 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
6046 /* First, determine if we even need a constructor and/or destructor. */
6048 for (ivar = CLASS_IVARS (implementation_template); ivar;
6049 ivar = TREE_CHAIN (ivar))
6051 if (TREE_CODE (ivar) == FIELD_DECL)
6053 tree type = TREE_TYPE (ivar);
6055 if (MAYBE_CLASS_TYPE_P (type))
6057 if (TYPE_NEEDS_CONSTRUCTING (type)
6058 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
6059 /* NB: If a default constructor is not available, we will not
6060 be able to initialize this ivar; the add_instance_variable()
6061 routine will already have warned about this. */
6064 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
6065 && (!TYPE_NEEDS_CONSTRUCTING (type)
6066 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6067 /* NB: If a default constructor is not available, we will not
6068 call the destructor either, for symmetry. */
6074 /* Generate '- .cxx_construct' if needed. */
6077 objc_generate_cxx_ctor_or_dtor (false);
6079 /* Generate '- .cxx_destruct' if needed. */
6082 objc_generate_cxx_ctor_or_dtor (true);
6084 /* The 'imp_list' variable points at an imp_entry record for the current
6085 @implementation. Record the existence of '- .cxx_construct' and/or
6086 '- .cxx_destruct' methods therein; it will be included in the
6087 metadata for the class. */
6088 if (flag_next_runtime)
6089 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
6093 /* For each protocol which was referenced either from a @protocol()
6094 expression, or because a class/category implements it (then a
6095 pointer to the protocol is stored in the struct describing the
6096 class/category), we create a statically allocated instance of the
6097 Protocol class. The code is written in such a way as to generate
6098 as few Protocol objects as possible; we generate a unique Protocol
6099 instance for each protocol, and we don't generate a Protocol
6100 instance if the protocol is never referenced (either from a
6101 @protocol() or from a class/category implementation). These
6102 statically allocated objects can be referred to via the static
6103 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
6105 The statically allocated Protocol objects that we generate here
6106 need to be fixed up at runtime in order to be used: the 'isa'
6107 pointer of the objects need to be set up to point to the 'Protocol'
6108 class, as known at runtime.
6110 The NeXT runtime fixes up all protocols at program startup time,
6111 before main() is entered. It uses a low-level trick to look up all
6112 those symbols, then loops on them and fixes them up.
6114 The GNU runtime as well fixes up all protocols before user code
6115 from the module is executed; it requires pointers to those symbols
6116 to be put in the objc_symtab (which is then passed as argument to
6117 the function __objc_exec_class() which the compiler sets up to be
6118 executed automatically when the module is loaded); setup of those
6119 Protocol objects happen in two ways in the GNU runtime: all
6120 Protocol objects referred to by a class or category implementation
6121 are fixed up when the class/category is loaded; all Protocol
6122 objects referred to by a @protocol() expression are added by the
6123 compiler to the list of statically allocated instances to fixup
6124 (the same list holding the statically allocated constant string
6125 objects). Because, as explained above, the compiler generates as
6126 few Protocol objects as possible, some Protocol object might end up
6127 being referenced multiple times when compiled with the GNU runtime,
6128 and end up being fixed up multiple times at runtime initialization.
6129 But that doesn't hurt, it's just a little inefficient. */
6132 generate_protocols (void)
6136 tree initlist, protocol_name_expr, refs_decl, refs_expr;
6138 /* If a protocol was directly referenced, pull in indirect references. */
6139 for (p = protocol_chain; p; p = TREE_CHAIN (p))
6140 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
6141 generate_protocol_references (PROTOCOL_LIST (p));
6143 for (p = protocol_chain; p; p = TREE_CHAIN (p))
6145 tree nst_methods = PROTOCOL_NST_METHODS (p);
6146 tree cls_methods = PROTOCOL_CLS_METHODS (p);
6148 /* If protocol wasn't referenced, don't generate any code. */
6149 decl = PROTOCOL_FORWARD_DECL (p);
6154 /* Make sure we link in the Protocol class. */
6155 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
6159 if (! METHOD_ENCODING (nst_methods))
6161 encoding = encode_method_prototype (nst_methods);
6162 METHOD_ENCODING (nst_methods) = encoding;
6164 nst_methods = DECL_CHAIN (nst_methods);
6169 if (! METHOD_ENCODING (cls_methods))
6171 encoding = encode_method_prototype (cls_methods);
6172 METHOD_ENCODING (cls_methods) = encoding;
6175 cls_methods = DECL_CHAIN (cls_methods);
6177 generate_method_descriptors (p);
6179 if (PROTOCOL_LIST (p))
6180 refs_decl = generate_protocol_list (p);
6184 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
6185 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
6188 refs_expr = convert (build_pointer_type (build_pointer_type
6189 (objc_protocol_template)),
6190 build_unary_op (input_location,
6191 ADDR_EXPR, refs_decl, 0));
6193 refs_expr = build_int_cst (NULL_TREE, 0);
6195 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
6196 by generate_method_descriptors, which is called above. */
6197 initlist = build_protocol_initializer (TREE_TYPE (decl),
6198 protocol_name_expr, refs_expr,
6199 UOBJC_INSTANCE_METHODS_decl,
6200 UOBJC_CLASS_METHODS_decl);
6201 finish_var_decl (decl, initlist);
6206 build_protocol_initializer (tree type, tree protocol_name,
6207 tree protocol_list, tree instance_methods,
6211 tree cast_type = build_pointer_type
6212 (xref_tag (RECORD_TYPE,
6213 get_identifier (UTAG_CLASS)));
6214 VEC(constructor_elt,gc) *inits = NULL;
6216 /* Filling the "isa" in with one allows the runtime system to
6217 detect that the version change...should remove before final release. */
6219 expr = build_int_cst (cast_type, PROTOCOL_VERSION);
6220 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
6221 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
6222 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
6224 if (!instance_methods)
6225 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
6228 expr = convert (objc_method_proto_list_ptr,
6229 build_unary_op (input_location,
6230 ADDR_EXPR, instance_methods, 0));
6231 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
6235 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
6238 expr = convert (objc_method_proto_list_ptr,
6239 build_unary_op (input_location,
6240 ADDR_EXPR, class_methods, 0));
6241 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
6244 return objc_build_constructor (type, inits);
6247 /* struct _objc_category {
6248 char *category_name;
6250 struct _objc_method_list *instance_methods;
6251 struct _objc_method_list *class_methods;
6252 struct _objc_protocol_list *protocols;
6256 build_category_template (void)
6258 tree ptype, decls, *chain = NULL;
6260 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
6262 /* char *category_name; */
6263 decls = add_field_decl (string_type_node, "category_name", &chain);
6265 /* char *class_name; */
6266 add_field_decl (string_type_node, "class_name", &chain);
6268 /* struct _objc_method_list *instance_methods; */
6269 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
6271 /* struct _objc_method_list *class_methods; */
6272 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
6274 /* struct _objc_protocol **protocol_list; */
6275 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
6276 add_field_decl (ptype, "protocol_list", &chain);
6278 objc_finish_struct (objc_category_template, decls);
6281 /* struct _objc_selector {
6287 build_selector_template (void)
6289 tree decls, *chain = NULL;
6291 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
6294 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
6296 /* char *sel_type; */
6297 add_field_decl (string_type_node, "sel_type", &chain);
6299 objc_finish_struct (objc_selector_template, decls);
6302 /* struct _objc_class {
6303 struct _objc_class *isa;
6304 struct _objc_class *super_class;
6309 struct _objc_ivar_list *ivars;
6310 struct _objc_method_list *methods;
6311 #ifdef __NEXT_RUNTIME__
6312 struct objc_cache *cache;
6314 struct sarray *dtable;
6315 struct _objc_class *subclass_list;
6316 struct _objc_class *sibling_class;
6318 struct _objc_protocol_list *protocols;
6319 #ifdef __NEXT_RUNTIME__
6322 void *gc_object_type;
6325 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
6326 the NeXT/Apple runtime; still, the compiler must generate them to
6327 maintain backward binary compatibility (and to allow for future
6331 build_class_template (void)
6333 tree ptype, decls, *chain = NULL;
6335 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
6337 /* struct _objc_class *isa; */
6338 decls = add_field_decl (build_pointer_type (objc_class_template),
6341 /* struct _objc_class *super_class; */
6342 add_field_decl (build_pointer_type (objc_class_template),
6343 "super_class", &chain);
6346 add_field_decl (string_type_node, "name", &chain);
6349 add_field_decl (long_integer_type_node, "version", &chain);
6352 add_field_decl (long_integer_type_node, "info", &chain);
6354 /* long instance_size; */
6355 add_field_decl (long_integer_type_node, "instance_size", &chain);
6357 /* struct _objc_ivar_list *ivars; */
6358 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
6360 /* struct _objc_method_list *methods; */
6361 add_field_decl (objc_method_list_ptr, "methods", &chain);
6363 if (flag_next_runtime)
6365 /* struct objc_cache *cache; */
6366 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
6367 get_identifier ("objc_cache")));
6368 add_field_decl (ptype, "cache", &chain);
6372 /* struct sarray *dtable; */
6373 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
6374 get_identifier ("sarray")));
6375 add_field_decl (ptype, "dtable", &chain);
6377 /* struct objc_class *subclass_list; */
6378 ptype = build_pointer_type (objc_class_template);
6379 add_field_decl (ptype, "subclass_list", &chain);
6381 /* struct objc_class *sibling_class; */
6382 ptype = build_pointer_type (objc_class_template);
6383 add_field_decl (ptype, "sibling_class", &chain);
6386 /* struct _objc_protocol **protocol_list; */
6387 ptype = build_pointer_type (build_pointer_type
6388 (xref_tag (RECORD_TYPE,
6389 get_identifier (UTAG_PROTOCOL))));
6390 add_field_decl (ptype, "protocol_list", &chain);
6392 if (flag_next_runtime)
6395 add_field_decl (build_pointer_type (void_type_node), "sel_id", &chain);
6398 /* void *gc_object_type; */
6399 add_field_decl (build_pointer_type (void_type_node),
6400 "gc_object_type", &chain);
6402 objc_finish_struct (objc_class_template, decls);
6405 /* Generate appropriate forward declarations for an implementation. */
6408 synth_forward_declarations (void)
6412 /* static struct objc_class _OBJC_CLASS_<my_name>; */
6413 UOBJC_CLASS_decl = build_metadata_decl ("_OBJC_CLASS",
6414 objc_class_template);
6416 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
6417 UOBJC_METACLASS_decl = build_metadata_decl ("_OBJC_METACLASS",
6418 objc_class_template);
6420 /* Pre-build the following entities - for speed/convenience. */
6422 an_id = get_identifier ("super_class");
6423 ucls_super_ref = objc_build_component_ref (UOBJC_CLASS_decl, an_id);
6424 uucls_super_ref = objc_build_component_ref (UOBJC_METACLASS_decl, an_id);
6428 error_with_ivar (const char *message, tree decl)
6430 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
6431 message, identifier_to_locale (gen_declaration (decl)));
6436 check_ivars (tree inter, tree imp)
6438 tree intdecls = CLASS_RAW_IVARS (inter);
6439 tree impdecls = CLASS_RAW_IVARS (imp);
6446 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
6447 intdecls = TREE_CHAIN (intdecls);
6449 if (intdecls == 0 && impdecls == 0)
6451 if (intdecls == 0 || impdecls == 0)
6453 error ("inconsistent instance variable specification");
6457 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
6459 if (!comptypes (t1, t2)
6460 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
6461 DECL_INITIAL (impdecls)))
6463 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
6465 error_with_ivar ("conflicting instance variable type",
6467 error_with_ivar ("previous declaration of",
6470 else /* both the type and the name don't match */
6472 error ("inconsistent instance variable specification");
6477 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
6479 error_with_ivar ("conflicting instance variable name",
6481 error_with_ivar ("previous declaration of",
6485 intdecls = DECL_CHAIN (intdecls);
6486 impdecls = DECL_CHAIN (impdecls);
6490 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
6491 This needs to be done just once per compilation. */
6493 /* struct _objc_super {
6494 struct _objc_object *self;
6495 struct _objc_class *super_class;
6499 build_super_template (void)
6501 tree decls, *chain = NULL;
6503 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
6505 /* struct _objc_object *self; */
6506 decls = add_field_decl (objc_object_type, "self", &chain);
6508 /* struct _objc_class *super_class; */
6509 add_field_decl (build_pointer_type (objc_class_template),
6510 "super_class", &chain);
6512 objc_finish_struct (objc_super_template, decls);
6515 /* struct _objc_ivar {
6522 build_ivar_template (void)
6524 tree objc_ivar_id, objc_ivar_record;
6525 tree decls, *chain = NULL;
6527 objc_ivar_id = get_identifier (UTAG_IVAR);
6528 objc_ivar_record = objc_start_struct (objc_ivar_id);
6530 /* char *ivar_name; */
6531 decls = add_field_decl (string_type_node, "ivar_name", &chain);
6533 /* char *ivar_type; */
6534 add_field_decl (string_type_node, "ivar_type", &chain);
6536 /* int ivar_offset; */
6537 add_field_decl (integer_type_node, "ivar_offset", &chain);
6539 objc_finish_struct (objc_ivar_record, decls);
6541 return objc_ivar_record;
6546 struct objc_ivar ivar_list[ivar_count];
6550 build_ivar_list_template (tree list_type, int size)
6552 tree objc_ivar_list_record;
6553 tree array_type, decls, *chain = NULL;
6555 objc_ivar_list_record = objc_start_struct (NULL_TREE);
6557 /* int ivar_count; */
6558 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
6560 /* struct objc_ivar ivar_list[]; */
6561 array_type = build_sized_array_type (list_type, size);
6562 add_field_decl (array_type, "ivar_list", &chain);
6564 objc_finish_struct (objc_ivar_list_record, decls);
6566 return objc_ivar_list_record;
6570 struct _objc__method_prototype_list *method_next;
6572 struct objc_method method_list[method_count];
6576 build_method_list_template (tree list_type, int size)
6578 tree objc_ivar_list_record;
6579 tree array_type, decls, *chain = NULL;
6581 objc_ivar_list_record = objc_start_struct (NULL_TREE);
6583 /* struct _objc__method_prototype_list *method_next; */
6584 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
6586 /* int method_count; */
6587 add_field_decl (integer_type_node, "method_count", &chain);
6589 /* struct objc_method method_list[]; */
6590 array_type = build_sized_array_type (list_type, size);
6591 add_field_decl (array_type, "method_list", &chain);
6593 objc_finish_struct (objc_ivar_list_record, decls);
6595 return objc_ivar_list_record;
6599 build_ivar_list_initializer (tree type, tree field_decl)
6601 VEC(constructor_elt,gc) *inits = NULL;
6605 VEC(constructor_elt,gc) *ivar = NULL;
6609 if (DECL_NAME (field_decl))
6610 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
6611 add_objc_string (DECL_NAME (field_decl),
6614 /* Unnamed bit-field ivar (yuck). */
6615 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
6618 encode_field_decl (field_decl,
6619 obstack_object_size (&util_obstack),
6620 OBJC_ENCODE_DONT_INLINE_DEFS);
6622 /* Null terminate string. */
6623 obstack_1grow (&util_obstack, 0);
6624 id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
6626 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
6627 obstack_free (&util_obstack, util_firstobj);
6630 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
6631 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6632 objc_build_constructor (type, ivar));
6634 field_decl = DECL_CHAIN (field_decl);
6635 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
6639 return objc_build_constructor (build_array_type (type, 0), inits);
6643 generate_ivars_list (tree type, const char *name, int size, tree list)
6646 VEC(constructor_elt,gc) *inits = NULL;
6648 decl = start_var_decl (type, synth_id_with_class_suffix
6649 (name, objc_implementation_context));
6651 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
6652 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, list);
6654 finish_var_decl (decl,
6655 objc_build_constructor (TREE_TYPE (decl), inits));
6660 /* Count only the fields occurring in T. */
6663 ivar_list_length (tree t)
6667 for (; t; t = DECL_CHAIN (t))
6668 if (TREE_CODE (t) == FIELD_DECL)
6675 generate_ivar_lists (void)
6677 tree initlist, ivar_list_template, chain;
6680 generating_instance_variables = 1;
6682 if (!objc_ivar_template)
6683 objc_ivar_template = build_ivar_template ();
6685 /* Only generate class variables for the root of the inheritance
6686 hierarchy since these will be the same for every class. */
6688 if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
6689 && (chain = TYPE_FIELDS (objc_class_template)))
6691 size = ivar_list_length (chain);
6693 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6694 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6696 UOBJC_CLASS_VARIABLES_decl
6697 = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
6701 UOBJC_CLASS_VARIABLES_decl = 0;
6703 chain = CLASS_IVARS (implementation_template);
6706 size = ivar_list_length (chain);
6707 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
6708 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
6710 UOBJC_INSTANCE_VARIABLES_decl
6711 = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
6715 UOBJC_INSTANCE_VARIABLES_decl = 0;
6717 generating_instance_variables = 0;
6721 build_dispatch_table_initializer (tree type, tree entries)
6723 VEC(constructor_elt,gc) *inits = NULL;
6727 VEC(constructor_elt,gc) *elems = NULL;
6730 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6731 build_selector (METHOD_SEL_NAME (entries)));
6733 /* Generate the method encoding if we don't have one already. */
6734 if (! METHOD_ENCODING (entries))
6735 METHOD_ENCODING (entries) =
6736 encode_method_prototype (entries);
6738 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
6739 add_objc_string (METHOD_ENCODING (entries),
6742 expr = convert (ptr_type_node,
6743 build_unary_op (input_location, ADDR_EXPR,
6744 METHOD_DEFINITION (entries), 1));
6745 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
6747 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
6748 objc_build_constructor (type, elems));
6750 entries = DECL_CHAIN (entries);
6754 return objc_build_constructor (build_array_type (type, 0), inits);
6757 /* To accomplish method prototyping without generating all kinds of
6758 inane warnings, the definition of the dispatch table entries were
6761 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
6763 struct objc_method { SEL _cmd; ...; void *_imp; }; */
6766 build_method_template (void)
6769 tree decls, *chain = NULL;
6771 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
6774 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
6776 /* char *method_types; */
6777 add_field_decl (string_type_node, "method_types", &chain);
6780 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
6782 objc_finish_struct (_SLT_record, decls);
6789 generate_dispatch_table (tree type, const char *name, int size, tree list)
6792 VEC(constructor_elt,gc) *v = NULL;
6794 decl = start_var_decl (type, synth_id_with_class_suffix
6795 (name, objc_implementation_context));
6797 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
6798 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, size));
6799 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
6801 finish_var_decl (decl,
6802 objc_build_constructor (TREE_TYPE (decl), v));
6808 mark_referenced_methods (void)
6810 struct imp_entry *impent;
6813 for (impent = imp_list; impent; impent = impent->next)
6815 chain = CLASS_CLS_METHODS (impent->imp_context);
6818 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6819 chain = DECL_CHAIN (chain);
6822 chain = CLASS_NST_METHODS (impent->imp_context);
6825 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
6826 chain = DECL_CHAIN (chain);
6832 generate_dispatch_tables (void)
6834 tree initlist, chain, method_list_template;
6837 if (!objc_method_template)
6838 objc_method_template = build_method_template ();
6840 chain = CLASS_CLS_METHODS (objc_implementation_context);
6843 size = list_length (chain);
6845 method_list_template
6846 = build_method_list_template (objc_method_template, size);
6848 = build_dispatch_table_initializer (objc_method_template, chain);
6850 UOBJC_CLASS_METHODS_decl
6851 = generate_dispatch_table (method_list_template,
6852 ((TREE_CODE (objc_implementation_context)
6853 == CLASS_IMPLEMENTATION_TYPE)
6854 ? "_OBJC_CLASS_METHODS"
6855 : "_OBJC_CATEGORY_CLASS_METHODS"),
6859 UOBJC_CLASS_METHODS_decl = 0;
6861 chain = CLASS_NST_METHODS (objc_implementation_context);
6864 size = list_length (chain);
6866 method_list_template
6867 = build_method_list_template (objc_method_template, size);
6869 = build_dispatch_table_initializer (objc_method_template, chain);
6871 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
6872 UOBJC_INSTANCE_METHODS_decl
6873 = generate_dispatch_table (method_list_template,
6874 "_OBJC_INSTANCE_METHODS",
6877 /* We have a category. */
6878 UOBJC_INSTANCE_METHODS_decl
6879 = generate_dispatch_table (method_list_template,
6880 "_OBJC_CATEGORY_INSTANCE_METHODS",
6884 UOBJC_INSTANCE_METHODS_decl = 0;
6888 generate_protocol_list (tree i_or_p)
6890 tree array_type, ptype, refs_decl, lproto, e, plist;
6892 const char *ref_name;
6893 VEC(constructor_elt,gc) *v = NULL;
6895 switch (TREE_CODE (i_or_p))
6897 case CLASS_INTERFACE_TYPE:
6898 case CATEGORY_INTERFACE_TYPE:
6899 plist = CLASS_PROTOCOL_LIST (i_or_p);
6901 case PROTOCOL_INTERFACE_TYPE:
6902 plist = PROTOCOL_LIST (i_or_p);
6909 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6910 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
6911 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
6914 /* Build initializer. */
6915 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6916 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
6917 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6919 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
6921 tree pval = TREE_VALUE (lproto);
6923 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
6924 && PROTOCOL_FORWARD_DECL (pval))
6926 e = build_unary_op (input_location, ADDR_EXPR,
6927 PROTOCOL_FORWARD_DECL (pval), 0);
6928 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
6932 /* static struct objc_protocol *refs[n]; */
6934 switch (TREE_CODE (i_or_p))
6936 case PROTOCOL_INTERFACE_TYPE:
6937 ref_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p);
6939 case CLASS_INTERFACE_TYPE:
6940 ref_name = synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p);
6942 case CATEGORY_INTERFACE_TYPE:
6943 ref_name = synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p);
6949 ptype = build_pointer_type (objc_protocol_template);
6950 array_type = build_sized_array_type (ptype, size + 3);
6951 refs_decl = start_var_decl (array_type, ref_name);
6953 finish_var_decl (refs_decl,
6954 objc_build_constructor (TREE_TYPE (refs_decl), v));
6960 build_category_initializer (tree type, tree cat_name, tree class_name,
6961 tree instance_methods, tree class_methods,
6965 VEC(constructor_elt,gc) *v = NULL;
6967 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
6968 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
6970 if (!instance_methods)
6971 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6974 expr = convert (objc_method_list_ptr,
6975 build_unary_op (input_location, ADDR_EXPR,
6976 instance_methods, 0));
6977 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6980 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6983 expr = convert (objc_method_list_ptr,
6984 build_unary_op (input_location, ADDR_EXPR,
6986 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
6989 /* protocol_list = */
6991 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
6994 expr = convert (build_pointer_type
6996 (objc_protocol_template)),
6997 build_unary_op (input_location, ADDR_EXPR,
6999 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
7002 return objc_build_constructor (type, v);
7005 /* struct _objc_class {
7006 struct objc_class *isa;
7007 struct objc_class *super_class;
7012 struct objc_ivar_list *ivars;
7013 struct objc_method_list *methods;
7014 if (flag_next_runtime)
7015 struct objc_cache *cache;
7017 struct sarray *dtable;
7018 struct objc_class *subclass_list;
7019 struct objc_class *sibling_class;
7021 struct objc_protocol_list *protocols;
7022 if (flag_next_runtime)
7024 void *gc_object_type;
7028 build_shared_structure_initializer (tree type, tree isa, tree super,
7029 tree name, tree size, int status,
7030 tree dispatch_table, tree ivar_list,
7034 VEC(constructor_elt,gc) *v = NULL;
7037 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
7040 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
7043 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
7046 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
7047 build_int_cst (long_integer_type_node, 0));
7050 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
7051 build_int_cst (long_integer_type_node, status));
7053 /* instance_size = */
7054 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
7055 convert (long_integer_type_node, size));
7057 /* objc_ivar_list = */
7059 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7062 expr = convert (objc_ivar_list_ptr,
7063 build_unary_op (input_location, ADDR_EXPR,
7065 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
7068 /* objc_method_list = */
7069 if (!dispatch_table)
7070 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7073 expr = convert (objc_method_list_ptr,
7074 build_unary_op (input_location, ADDR_EXPR,
7075 dispatch_table, 0));
7076 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
7079 if (flag_next_runtime)
7080 /* method_cache = */
7081 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7085 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7087 /* subclass_list = */
7088 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7090 /* sibling_class = */
7091 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7094 /* protocol_list = */
7095 if (! protocol_list)
7096 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7099 expr = convert (build_pointer_type
7101 (objc_protocol_template)),
7102 build_unary_op (input_location, ADDR_EXPR,
7104 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
7107 if (flag_next_runtime)
7109 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7111 /* gc_object_type = NULL */
7112 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
7114 return objc_build_constructor (type, v);
7117 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
7120 lookup_category (tree klass, tree cat_name)
7122 tree category = CLASS_CATEGORY_LIST (klass);
7124 while (category && CLASS_SUPER_NAME (category) != cat_name)
7125 category = CLASS_CATEGORY_LIST (category);
7129 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
7132 generate_category (struct imp_entry *impent)
7134 tree initlist, cat_name_expr, class_name_expr;
7135 tree protocol_decl, category;
7136 tree cat = impent->imp_context;
7138 implementation_template = impent->imp_template;
7139 UOBJC_CLASS_decl = impent->class_decl;
7140 UOBJC_METACLASS_decl = impent->meta_decl;
7142 add_class_reference (CLASS_NAME (cat));
7143 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
7145 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
7147 category = lookup_category (implementation_template,
7148 CLASS_SUPER_NAME (cat));
7150 if (category && CLASS_PROTOCOL_LIST (category))
7152 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
7153 protocol_decl = generate_protocol_list (category);
7158 initlist = build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl),
7159 cat_name_expr, class_name_expr,
7160 UOBJC_INSTANCE_METHODS_decl,
7161 UOBJC_CLASS_METHODS_decl,
7163 /* Finish and initialize the forward decl. */
7164 finish_var_decl (UOBJC_CLASS_decl, initlist);
7167 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
7168 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
7171 generate_shared_structures (struct imp_entry *impent)
7173 tree name_expr, super_expr, root_expr;
7174 tree my_root_id, my_super_id;
7175 tree cast_type, initlist, protocol_decl;
7178 objc_implementation_context = impent->imp_context;
7179 implementation_template = impent->imp_template;
7180 UOBJC_CLASS_decl = impent->class_decl;
7181 UOBJC_METACLASS_decl = impent->meta_decl;
7182 cls_flags = impent->has_cxx_cdtors ? CLS_HAS_CXX_STRUCTORS : 0 ;
7184 my_super_id = CLASS_SUPER_NAME (implementation_template);
7187 add_class_reference (my_super_id);
7189 /* Compute "my_root_id" - this is required for code generation.
7190 the "isa" for all meta class structures points to the root of
7191 the inheritance hierarchy (e.g. "__Object")... */
7192 my_root_id = my_super_id;
7195 tree my_root_int = lookup_interface (my_root_id);
7197 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
7198 my_root_id = CLASS_SUPER_NAME (my_root_int);
7205 /* No super class. */
7206 my_root_id = CLASS_NAME (implementation_template);
7208 cast_type = build_pointer_type (objc_class_template);
7209 name_expr = add_objc_string (CLASS_NAME (implementation_template),
7212 /* Install class `isa' and `super' pointers at runtime. */
7214 super_expr = add_objc_string (my_super_id, class_names);
7216 super_expr = integer_zero_node;
7218 super_expr = build_c_cast (input_location,
7219 cast_type, super_expr); /* cast! */
7221 root_expr = add_objc_string (my_root_id, class_names);
7222 root_expr = build_c_cast (input_location, cast_type, root_expr); /* cast! */
7224 if (CLASS_PROTOCOL_LIST (implementation_template))
7226 generate_protocol_references
7227 (CLASS_PROTOCOL_LIST (implementation_template));
7228 protocol_decl = generate_protocol_list (implementation_template);
7233 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
7236 = build_shared_structure_initializer
7237 (TREE_TYPE (UOBJC_METACLASS_decl),
7238 root_expr, super_expr, name_expr,
7239 convert (integer_type_node, TYPE_SIZE_UNIT (objc_class_template)),
7241 UOBJC_CLASS_METHODS_decl,
7242 UOBJC_CLASS_VARIABLES_decl,
7245 finish_var_decl (UOBJC_METACLASS_decl, initlist);
7247 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
7250 = build_shared_structure_initializer
7251 (TREE_TYPE (UOBJC_CLASS_decl),
7252 build_unary_op (input_location, ADDR_EXPR, UOBJC_METACLASS_decl, 0),
7253 super_expr, name_expr,
7254 convert (integer_type_node,
7255 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
7256 (implementation_template))),
7257 1 /*CLS_FACTORY*/ | cls_flags,
7258 UOBJC_INSTANCE_METHODS_decl,
7259 UOBJC_INSTANCE_VARIABLES_decl,
7262 finish_var_decl (UOBJC_CLASS_decl, initlist);
7267 synth_id_with_class_suffix (const char *preamble, tree ctxt)
7269 static char string[BUFSIZE];
7271 switch (TREE_CODE (ctxt))
7273 case CLASS_IMPLEMENTATION_TYPE:
7274 case CLASS_INTERFACE_TYPE:
7275 sprintf (string, "%s_%s", preamble,
7276 IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
7278 case CATEGORY_IMPLEMENTATION_TYPE:
7279 case CATEGORY_INTERFACE_TYPE:
7281 /* We have a category. */
7282 const char *const class_name
7283 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
7284 const char *const class_super_name
7285 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context));
7286 sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
7289 case PROTOCOL_INTERFACE_TYPE:
7291 const char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
7292 sprintf (string, "%s_%s", preamble, protocol_name);
7302 /* If type is empty or only type qualifiers are present, add default
7303 type of id (otherwise grokdeclarator will default to int). */
7305 adjust_type_for_id_default (tree type)
7308 type = make_node (TREE_LIST);
7310 if (!TREE_VALUE (type))
7311 TREE_VALUE (type) = objc_object_type;
7312 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
7313 && TYPED_OBJECT (TREE_VALUE (type)))
7314 error ("can not use an object as parameter to a method");
7319 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
7320 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
7321 OBJC_METHOD_PARM_DECL ?)
7323 A KEYWORD_DECL is a tree representing the declaration of a
7324 parameter of an Objective-C method. It is produced when parsing a
7325 fragment of Objective-C method declaration of the form
7328 selector ':' '(' typename ')' identifier
7330 For example, take the Objective-C method
7332 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
7334 the two fragments "pathForResource:(NSString *)resource" and
7335 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
7336 KEYWORD_DECL stores the 'key_name' (eg, identifier for
7337 "pathForResource"), the 'arg_type' (eg, tree representing a
7338 NSString *), the 'arg_name' (eg identifier for "resource") and
7339 potentially some attributes (for example, a tree representing
7340 __attribute__ ((unused)) if such an attribute was attached to a
7341 certain parameter). You can access this information using the
7342 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
7343 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
7345 'key_name' is an identifier node (and is optional as you can omit
7346 it in Objective-C methods).
7347 'arg_type' is a tree list (and is optional too if no parameter type
7349 'arg_name' is an identifier node and is required.
7350 'attributes' is an optional tree containing parameter attributes. */
7352 objc_build_keyword_decl (tree key_name, tree arg_type,
7353 tree arg_name, tree attributes)
7357 if (flag_objc1_only && attributes)
7358 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
7360 /* If no type is specified, default to "id". */
7361 arg_type = adjust_type_for_id_default (arg_type);
7363 keyword_decl = make_node (KEYWORD_DECL);
7365 TREE_TYPE (keyword_decl) = arg_type;
7366 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
7367 KEYWORD_KEY_NAME (keyword_decl) = key_name;
7368 DECL_ATTRIBUTES (keyword_decl) = attributes;
7370 return keyword_decl;
7373 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
7375 build_keyword_selector (tree selector)
7378 tree key_chain, key_name;
7381 /* Scan the selector to see how much space we'll need. */
7382 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
7384 switch (TREE_CODE (selector))
7387 key_name = KEYWORD_KEY_NAME (key_chain);
7390 key_name = TREE_PURPOSE (key_chain);
7397 len += IDENTIFIER_LENGTH (key_name) + 1;
7399 /* Just a ':' arg. */
7403 buf = (char *) alloca (len + 1);
7404 /* Start the buffer out as an empty string. */
7407 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
7409 switch (TREE_CODE (selector))
7412 key_name = KEYWORD_KEY_NAME (key_chain);
7415 key_name = TREE_PURPOSE (key_chain);
7416 /* The keyword decl chain will later be used as a function
7417 argument chain. Unhook the selector itself so as to not
7418 confuse other parts of the compiler. */
7419 TREE_PURPOSE (key_chain) = NULL_TREE;
7426 strcat (buf, IDENTIFIER_POINTER (key_name));
7430 return get_identifier (buf);
7433 /* Used for declarations and definitions. */
7436 build_method_decl (enum tree_code code, tree ret_type, tree selector,
7437 tree add_args, bool ellipsis)
7441 /* If no type is specified, default to "id". */
7442 ret_type = adjust_type_for_id_default (ret_type);
7444 /* Note how a method_decl has a TREE_TYPE which is not the function
7445 type of the function implementing the method, but only the return
7446 type of the method. We may want to change this, and store the
7447 entire function type in there (eg, it may be used to simplify
7448 dealing with attributes below). */
7449 method_decl = make_node (code);
7450 TREE_TYPE (method_decl) = ret_type;
7452 /* If we have a keyword selector, create an identifier_node that
7453 represents the full selector name (`:' included)... */
7454 if (TREE_CODE (selector) == KEYWORD_DECL)
7456 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
7457 METHOD_SEL_ARGS (method_decl) = selector;
7458 METHOD_ADD_ARGS (method_decl) = add_args;
7459 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
7463 METHOD_SEL_NAME (method_decl) = selector;
7464 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
7465 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
7471 #define METHOD_DEF 0
7472 #define METHOD_REF 1
7474 /* This routine processes objective-c method attributes. */
7477 objc_decl_method_attributes (tree *node, tree attributes, int flags)
7479 /* TODO: Replace the hackery below. An idea would be to store the
7480 full function type in the method declaration (for example in
7481 TREE_TYPE) and then expose ObjC method declarations to c-family
7482 and they could deal with them by simply treating them as
7485 /* Because of the dangers in the hackery below, we filter out any
7486 attribute that we do not know about. For the ones we know about,
7487 we know that they work with the hackery. For the other ones,
7488 there is no guarantee, so we have to filter them out. */
7489 tree filtered_attributes = NULL_TREE;
7494 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
7496 tree name = TREE_PURPOSE (attribute);
7498 if (is_attribute_p ("deprecated", name)
7499 || is_attribute_p ("sentinel", name)
7500 || is_attribute_p ("noreturn", name))
7502 /* An attribute that we support; add it to the filtered
7504 filtered_attributes = chainon (filtered_attributes,
7505 copy_node (attribute));
7507 else if (is_attribute_p ("format", name))
7509 /* "format" is special because before adding it to the
7510 filtered attributes we need to adjust the specified
7511 format by adding the hidden function parameters for
7512 an Objective-C method (self, _cmd). */
7513 tree new_attribute = copy_node (attribute);
7515 /* Check the arguments specified with the attribute, and
7516 modify them adding 2 for the two hidden arguments.
7517 Note how this differs from C++; according to the
7518 specs, C++ does not do it so you have to add the +1
7519 yourself. For Objective-C, instead, the compiler
7520 adds the +2 for you. */
7522 /* The attribute arguments have not been checked yet, so
7523 we need to be careful as they could be missing or
7524 invalid. If anything looks wrong, we skip the
7525 process and the compiler will complain about it later
7526 when it validates the attribute. */
7527 /* Check that we have at least three arguments. */
7528 if (TREE_VALUE (new_attribute)
7529 && TREE_CHAIN (TREE_VALUE (new_attribute))
7530 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
7532 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
7533 tree third_argument = TREE_CHAIN (second_argument);
7536 /* This is the second argument, the "string-index",
7537 which specifies the index of the format string
7539 number = TREE_VALUE (second_argument);
7541 && TREE_CODE (number) == INTEGER_CST
7542 && TREE_INT_CST_HIGH (number) == 0)
7544 TREE_VALUE (second_argument)
7545 = build_int_cst (integer_type_node,
7546 TREE_INT_CST_LOW (number) + 2);
7549 /* This is the third argument, the "first-to-check",
7550 which specifies the index of the first argument to
7551 check. This could be 0, meaning it is not available,
7552 in which case we don't need to add 2. Add 2 if not
7554 number = TREE_VALUE (third_argument);
7556 && TREE_CODE (number) == INTEGER_CST
7557 && TREE_INT_CST_HIGH (number) == 0
7558 && TREE_INT_CST_LOW (number) != 0)
7560 TREE_VALUE (third_argument)
7561 = build_int_cst (integer_type_node,
7562 TREE_INT_CST_LOW (number) + 2);
7565 filtered_attributes = chainon (filtered_attributes,
7569 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
7573 if (filtered_attributes)
7575 /* This hackery changes the TREE_TYPE of the ObjC method
7576 declaration to be a function type, so that decl_attributes
7577 will treat the ObjC method as if it was a function. Some
7578 attributes (sentinel, format) will be applied to the function
7579 type, changing it in place; so after calling decl_attributes,
7580 we extract the function type attributes and store them in
7581 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
7582 deprecated) are applied directly to the method declaration
7583 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
7584 is nothing to do. */
7585 tree saved_type = TREE_TYPE (*node);
7586 TREE_TYPE (*node) = build_function_type
7587 (TREE_VALUE (saved_type), get_arg_type_list (*node, METHOD_REF, 0));
7588 decl_attributes (node, filtered_attributes, flags);
7589 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
7590 TREE_TYPE (*node) = saved_type;
7595 objc_method_decl (enum tree_code opcode)
7597 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
7600 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
7601 an argument list for method METH. CONTEXT is either METHOD_DEF or
7602 METHOD_REF, saying whether we are trying to define a method or call
7603 one. SUPERFLAG says this is for a send to super; this makes a
7604 difference for the NeXT calling sequence in which the lookup and
7605 the method call are done together. If METH is null, user-defined
7606 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
7609 get_arg_type_list (tree meth, int context, int superflag)
7613 /* Receiver type. */
7614 if (flag_next_runtime && superflag)
7615 arglist = build_tree_list (NULL_TREE, objc_super_type);
7616 else if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
7617 arglist = build_tree_list (NULL_TREE, objc_instance_type);
7619 arglist = build_tree_list (NULL_TREE, objc_object_type);
7621 /* Selector type - will eventually change to `int'. */
7622 chainon (arglist, build_tree_list (NULL_TREE, objc_selector_type));
7624 /* No actual method prototype given -- assume that remaining arguments
7629 /* Build a list of argument types. */
7630 for (akey = METHOD_SEL_ARGS (meth); akey; akey = DECL_CHAIN (akey))
7632 tree arg_type = TREE_VALUE (TREE_TYPE (akey));
7634 /* Decay argument types for the underlying C function as appropriate. */
7635 arg_type = objc_decay_parm_type (arg_type);
7637 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7640 if (METHOD_ADD_ARGS (meth))
7642 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (meth));
7643 akey; akey = TREE_CHAIN (akey))
7645 tree arg_type = TREE_TYPE (TREE_VALUE (akey));
7647 arg_type = objc_decay_parm_type (arg_type);
7649 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
7652 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth))
7653 goto lack_of_ellipsis;
7658 chainon (arglist, OBJC_VOID_AT_END);
7665 check_duplicates (hash hsh, int methods, int is_class)
7667 tree meth = NULL_TREE;
7675 /* We have two or more methods with the same name but
7679 /* But just how different are those types? If
7680 -Wno-strict-selector-match is specified, we shall not
7681 complain if the differences are solely among types with
7682 identical size and alignment. */
7683 if (!warn_strict_selector_match)
7685 for (loop = hsh->list; loop; loop = loop->next)
7686 if (!comp_proto_with_proto (meth, loop->value, 0))
7695 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7697 warning_at (input_location, 0,
7698 "multiple methods named %<%c%E%> found",
7699 (is_class ? '+' : '-'),
7700 METHOD_SEL_NAME (meth));
7701 inform (DECL_SOURCE_LOCATION (meth), "using %<%c%s%>",
7703 identifier_to_locale (gen_method_decl (meth)));
7707 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
7709 warning_at (input_location, 0,
7710 "multiple selectors named %<%c%E%> found",
7711 (is_class ? '+' : '-'),
7712 METHOD_SEL_NAME (meth));
7713 inform (DECL_SOURCE_LOCATION (meth), "found %<%c%s%>",
7715 identifier_to_locale (gen_method_decl (meth)));
7718 for (loop = hsh->list; loop; loop = loop->next)
7720 bool type = TREE_CODE (loop->value) == INSTANCE_METHOD_DECL;
7722 inform (DECL_SOURCE_LOCATION (loop->value), "also found %<%c%s%>",
7724 identifier_to_locale (gen_method_decl (loop->value)));
7731 /* If RECEIVER is a class reference, return the identifier node for
7732 the referenced class. RECEIVER is created by objc_get_class_reference,
7733 so we check the exact form created depending on which runtimes are
7737 receiver_is_class_object (tree receiver, int self, int super)
7739 tree chain, exp, arg;
7741 /* The receiver is 'self' or 'super' in the context of a class method. */
7742 if (objc_method_context
7743 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
7746 ? CLASS_SUPER_NAME (implementation_template)
7747 : CLASS_NAME (implementation_template));
7749 if (flag_next_runtime)
7751 /* The receiver is a variable created by
7752 build_class_reference_decl. */
7753 if (TREE_CODE (receiver) == VAR_DECL && IS_CLASS (TREE_TYPE (receiver)))
7754 /* Look up the identifier. */
7755 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
7756 if (TREE_PURPOSE (chain) == receiver)
7757 return TREE_VALUE (chain);
7760 /* The receiver is a function call that returns an id. Check if
7761 it is a call to objc_getClass, if so, pick up the class name. */
7762 if (TREE_CODE (receiver) == CALL_EXPR
7763 && (exp = CALL_EXPR_FN (receiver))
7764 && TREE_CODE (exp) == ADDR_EXPR
7765 && (exp = TREE_OPERAND (exp, 0))
7766 && TREE_CODE (exp) == FUNCTION_DECL
7767 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
7768 prototypes for objc_get_class(). Thankfully, they seem to share the
7769 same function type. */
7770 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
7771 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), TAG_GETCLASS)
7772 /* We have a call to objc_get_class/objc_getClass! */
7773 && (arg = CALL_EXPR_ARG (receiver, 0)))
7776 if (TREE_CODE (arg) == ADDR_EXPR
7777 && (arg = TREE_OPERAND (arg, 0))
7778 && TREE_CODE (arg) == STRING_CST)
7779 /* Finally, we have the class name. */
7780 return get_identifier (TREE_STRING_POINTER (arg));
7785 /* If we are currently building a message expr, this holds
7786 the identifier of the selector of the message. This is
7787 used when printing warnings about argument mismatches. */
7789 static tree current_objc_message_selector = 0;
7792 objc_message_selector (void)
7794 return current_objc_message_selector;
7797 /* Construct an expression for sending a message.
7798 MESS has the object to send to in TREE_PURPOSE
7799 and the argument list (including selector) in TREE_VALUE.
7801 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
7802 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
7805 objc_build_message_expr (tree mess)
7807 tree receiver = TREE_PURPOSE (mess);
7810 tree args = TREE_PURPOSE (TREE_VALUE (mess));
7812 tree args = TREE_VALUE (mess);
7814 tree method_params = NULL_TREE;
7816 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
7817 return error_mark_node;
7819 /* Obtain the full selector name. */
7820 switch (TREE_CODE (args))
7822 case IDENTIFIER_NODE:
7823 /* A unary selector. */
7827 sel_name = build_keyword_selector (args);
7833 /* Build the parameter list to give to the method. */
7834 if (TREE_CODE (args) == TREE_LIST)
7836 method_params = chainon (args, TREE_VALUE (TREE_VALUE (mess)));
7839 tree chain = args, prev = NULL_TREE;
7841 /* We have a keyword selector--check for comma expressions. */
7844 tree element = TREE_VALUE (chain);
7846 /* We have a comma expression, must collapse... */
7847 if (TREE_CODE (element) == TREE_LIST)
7850 TREE_CHAIN (prev) = element;
7855 chain = TREE_CHAIN (chain);
7857 method_params = args;
7862 if (processing_template_decl)
7863 /* Must wait until template instantiation time. */
7864 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
7868 return objc_finish_message_expr (receiver, sel_name, method_params);
7871 /* Look up method SEL_NAME that would be suitable for receiver
7872 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
7873 nonzero), and report on any duplicates. */
7876 lookup_method_in_hash_lists (tree sel_name, int is_class)
7878 hash method_prototype = NULL;
7881 method_prototype = hash_lookup (nst_method_hash_list,
7884 if (!method_prototype)
7886 method_prototype = hash_lookup (cls_method_hash_list,
7891 return check_duplicates (method_prototype, 1, is_class);
7894 /* The 'objc_finish_message_expr' routine is called from within
7895 'objc_build_message_expr' for non-template functions. In the case of
7896 C++ template functions, it is called from 'build_expr_from_tree'
7897 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
7900 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params)
7902 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
7903 tree selector, retval, class_tree;
7904 int self, super, have_cast;
7906 /* We have used the receiver, so mark it as read. */
7907 mark_exp_read (receiver);
7909 /* Extract the receiver of the message, as well as its type
7910 (where the latter may take the form of a cast or be inferred
7911 from the implementation context). */
7913 while (TREE_CODE (rtype) == COMPOUND_EXPR
7914 || TREE_CODE (rtype) == MODIFY_EXPR
7915 || CONVERT_EXPR_P (rtype)
7916 || TREE_CODE (rtype) == COMPONENT_REF)
7917 rtype = TREE_OPERAND (rtype, 0);
7919 self = (rtype == self_decl);
7920 super = (rtype == UOBJC_SUPER_decl);
7921 rtype = TREE_TYPE (receiver);
7923 have_cast = (TREE_CODE (receiver) == NOP_EXPR
7924 || (TREE_CODE (receiver) == COMPOUND_EXPR
7925 && !IS_SUPER (rtype)));
7927 /* If we are calling [super dealloc], reset our warning flag. */
7928 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
7929 should_call_super_dealloc = 0;
7931 /* If the receiver is a class object, retrieve the corresponding
7932 @interface, if one exists. */
7933 class_tree = receiver_is_class_object (receiver, self, super);
7935 /* Now determine the receiver type (if an explicit cast has not been
7940 rtype = lookup_interface (class_tree);
7941 /* Handle `self' and `super'. */
7944 if (!CLASS_SUPER_NAME (implementation_template))
7946 error ("no super class declared in @interface for %qE",
7947 CLASS_NAME (implementation_template));
7948 return error_mark_node;
7950 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
7953 rtype = lookup_interface (CLASS_NAME (implementation_template));
7956 /* If receiver is of type `id' or `Class' (or if the @interface for a
7957 class is not visible), we shall be satisfied with the existence of
7958 any instance or class method. */
7959 if (objc_is_id (rtype))
7961 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
7962 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
7963 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
7969 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
7970 in protocols themselves for the method prototype. */
7972 = lookup_method_in_protocol_list (rprotos, sel_name,
7973 class_tree != NULL_TREE);
7975 /* If messaging 'Class <Proto>' but did not find a class method
7976 prototype, search for an instance method instead, and warn
7977 about having done so. */
7978 if (!method_prototype && !rtype && class_tree != NULL_TREE)
7981 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
7983 if (method_prototype)
7984 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
7985 sel_name, sel_name);
7991 tree orig_rtype = rtype;
7993 if (TREE_CODE (rtype) == POINTER_TYPE)
7994 rtype = TREE_TYPE (rtype);
7995 /* Traverse typedef aliases */
7996 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
7997 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
7998 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
7999 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
8000 if (TYPED_OBJECT (rtype))
8002 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
8003 rtype = TYPE_OBJC_INTERFACE (rtype);
8005 /* If we could not find an @interface declaration, we must have
8006 only seen a @class declaration; so, we cannot say anything
8007 more intelligent about which methods the receiver will
8009 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
8012 /* We could not find an @interface declaration, yet Message maybe in a
8013 @class's protocol. */
8014 if (!method_prototype && rprotos)
8016 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
8018 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
8019 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
8021 /* We have a valid ObjC class name. Look up the method name
8022 in the published @interface for the class (and its
8025 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
8027 /* If the method was not found in the @interface, it may still
8028 exist locally as part of the @implementation. */
8029 if (!method_prototype && objc_implementation_context
8030 && CLASS_NAME (objc_implementation_context)
8031 == OBJC_TYPE_NAME (rtype))
8035 ? CLASS_CLS_METHODS (objc_implementation_context)
8036 : CLASS_NST_METHODS (objc_implementation_context)),
8039 /* If we haven't found a candidate method by now, try looking for
8040 it in the protocol list. */
8041 if (!method_prototype && rprotos)
8043 = lookup_method_in_protocol_list (rprotos, sel_name,
8044 class_tree != NULL_TREE);
8048 warning (0, "invalid receiver type %qs",
8049 identifier_to_locale (gen_type_name (orig_rtype)));
8050 /* After issuing the "invalid receiver" warning, perform method
8051 lookup as if we were messaging 'id'. */
8052 rtype = rprotos = NULL_TREE;
8057 /* For 'id' or 'Class' receivers, search in the global hash table
8058 as a last resort. For all receivers, warn if protocol searches
8060 if (!method_prototype)
8063 warning (0, "%<%c%E%> not found in protocol(s)",
8064 (class_tree ? '+' : '-'),
8069 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
8072 if (!method_prototype)
8074 static bool warn_missing_methods = false;
8077 warning (0, "%qE may not respond to %<%c%E%>",
8078 OBJC_TYPE_NAME (rtype),
8079 (class_tree ? '+' : '-'),
8081 /* If we are messaging an 'id' or 'Class' object and made it here,
8082 then we have failed to find _any_ instance or class method,
8085 warning (0, "no %<%c%E%> method found",
8086 (class_tree ? '+' : '-'),
8089 if (!warn_missing_methods)
8091 warning_at (input_location,
8092 0, "(Messages without a matching method signature");
8093 warning_at (input_location,
8094 0, "will be assumed to return %<id%> and accept");
8095 warning_at (input_location,
8096 0, "%<...%> as arguments.)");
8097 warn_missing_methods = true;
8102 /* Warn if the method is deprecated, but not if the receiver is
8103 a generic 'id'. 'id' is used to cast an object to a generic
8104 object of an unspecified class; in that case, we'll use
8105 whatever method prototype we can find to get the method
8106 argument and return types, but it is not appropriate to
8107 produce deprecation warnings since we don't know the class
8108 that the object will be of at runtime. The @interface(s) for
8109 that class may not even be available to the compiler right
8110 now, and it is perfectly possible that the method is marked
8111 as non-deprecated in such @interface(s).
8113 In practice this makes sense since casting an object to 'id'
8114 is often used precisely to turn off warnings associated with
8115 the object being of a particular class. */
8116 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
8117 warn_deprecated_use (method_prototype, NULL_TREE);
8121 /* Save the selector name for printing error messages. */
8122 current_objc_message_selector = sel_name;
8124 /* Build the parameters list for looking up the method.
8125 These are the object itself and the selector. */
8127 if (flag_typed_selectors)
8128 selector = build_typed_selector_reference (input_location,
8129 sel_name, method_prototype);
8131 selector = build_selector_reference (input_location, sel_name);
8133 retval = build_objc_method_call (input_location, super, method_prototype,
8135 selector, method_params);
8137 current_objc_message_selector = 0;
8142 /* Build a tree expression to send OBJECT the operation SELECTOR,
8143 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
8144 assuming the method has prototype METHOD_PROTOTYPE.
8145 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
8146 LOC is the location of the expression to build.
8147 Use METHOD_PARAMS as list of args to pass to the method.
8148 If SUPER_FLAG is nonzero, we look up the superclass's method. */
8151 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
8152 tree lookup_object, tree selector,
8155 tree sender = (super_flag ? umsg_super_decl :
8156 (!flag_next_runtime || flag_nil_receivers
8157 ? (flag_objc_direct_dispatch
8160 : umsg_nonnil_decl));
8161 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
8162 VEC(tree, gc) *parms = NULL;
8163 unsigned nparm = (method_params ? list_length (method_params) : 0);
8165 /* If a prototype for the method to be called exists, then cast
8166 the sender's return type and arguments to match that of the method.
8167 Otherwise, leave sender as is. */
8170 ? TREE_VALUE (TREE_TYPE (method_prototype))
8171 : objc_object_type);
8173 tree method_param_types =
8174 get_arg_type_list (method_prototype, METHOD_REF, super_flag);
8175 tree ftype = build_function_type (ret_type, method_param_types);
8179 if (method_prototype && METHOD_TYPE_ATTRIBUTES (method_prototype))
8180 ftype = build_type_attribute_variant (ftype,
8181 METHOD_TYPE_ATTRIBUTES
8182 (method_prototype));
8184 sender_cast = build_pointer_type (ftype);
8186 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
8188 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
8189 lookup_object = save_expr (lookup_object);
8191 /* Param list + 2 slots for object and selector. */
8192 parms = VEC_alloc (tree, gc, nparm + 2);
8194 if (flag_next_runtime)
8196 /* If we are returning a struct in memory, and the address
8197 of that memory location is passed as a hidden first
8198 argument, then change which messenger entry point this
8199 expr will call. NB: Note that sender_cast remains
8200 unchanged (it already has a struct return type). */
8201 if (!targetm.calls.struct_value_rtx (0, 0)
8202 && (TREE_CODE (ret_type) == RECORD_TYPE
8203 || TREE_CODE (ret_type) == UNION_TYPE)
8204 && targetm.calls.return_in_memory (ret_type, 0))
8205 sender = (super_flag ? umsg_super_stret_decl :
8206 flag_nil_receivers ? umsg_stret_decl : umsg_nonnil_stret_decl);
8208 method = build_fold_addr_expr_loc (input_location, sender);
8209 /* Pass the object to the method. */
8210 VEC_quick_push (tree, parms, lookup_object);
8214 /* This is the portable (GNU) way. */
8215 /* First, call the lookup function to get a pointer to the method,
8216 then cast the pointer, then call it with the method arguments. */
8217 VEC(tree, gc) *tv = VEC_alloc (tree, gc, 2);
8218 VEC_quick_push (tree, tv, lookup_object);
8219 VEC_quick_push (tree, tv, selector);
8220 method = build_function_call_vec (loc, sender, tv, NULL);
8221 VEC_free (tree, gc, tv);
8223 /* Pass the appropriate object to the method. */
8224 VEC_quick_push (tree, parms, (super_flag ? self_decl : lookup_object));
8227 /* Pass the selector to the method. */
8228 VEC_quick_push (tree, parms, selector);
8229 /* Now append the remainder of the parms. */
8231 for (; method_params; method_params = TREE_CHAIN (method_params))
8232 VEC_quick_push (tree, parms, TREE_VALUE (method_params));
8234 /* Build an obj_type_ref, with the correct cast for the method call. */
8235 t = build3 (OBJ_TYPE_REF, sender_cast, method,
8236 lookup_object, size_zero_node);
8237 t = build_function_call_vec (loc, t, parms, NULL);\
8238 VEC_free (tree, gc, parms);
8243 build_protocol_reference (tree p)
8246 const char *proto_name;
8248 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
8250 proto_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
8251 decl = start_var_decl (objc_protocol_template, proto_name);
8253 PROTOCOL_FORWARD_DECL (p) = decl;
8256 /* This function is called by the parser when (and only when) a
8257 @protocol() expression is found, in order to compile it. */
8259 objc_build_protocol_expr (tree protoname)
8262 tree p = lookup_protocol (protoname, /* warn if deprecated */ true);
8266 error ("cannot find protocol declaration for %qE",
8268 return error_mark_node;
8271 if (!PROTOCOL_FORWARD_DECL (p))
8272 build_protocol_reference (p);
8274 expr = build_unary_op (input_location,
8275 ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
8277 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
8278 if we have it, rather than converting it here. */
8279 expr = convert (objc_protocol_type, expr);
8281 /* The @protocol() expression is being compiled into a pointer to a
8282 statically allocated instance of the Protocol class. To become
8283 usable at runtime, the 'isa' pointer of the instance need to be
8284 fixed up at runtime by the runtime library, to point to the
8285 actual 'Protocol' class. */
8287 /* For the GNU runtime, put the static Protocol instance in the list
8288 of statically allocated instances, so that we make sure that its
8289 'isa' pointer is fixed up at runtime by the GNU runtime library
8290 to point to the Protocol class (at runtime, when loading the
8291 module, the GNU runtime library loops on the statically allocated
8292 instances (as found in the defs field in objc_symtab) and fixups
8293 all the 'isa' pointers of those objects). */
8294 if (! flag_next_runtime)
8296 /* This type is a struct containing the fields of a Protocol
8297 object. (Cfr. objc_protocol_type instead is the type of a pointer
8298 to such a struct). */
8299 tree protocol_struct_type = xref_tag
8300 (RECORD_TYPE, get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
8303 /* Look for the list of Protocol statically allocated instances
8304 to fixup at runtime. Create a new list to hold Protocol
8305 statically allocated instances, if the list is not found. At
8306 present there is only another list, holding NSConstantString
8307 static instances to be fixed up at runtime. */
8308 for (chain = &objc_static_instances;
8309 *chain && TREE_VALUE (*chain) != protocol_struct_type;
8310 chain = &TREE_CHAIN (*chain));
8313 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
8314 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
8318 /* Add this statically allocated instance to the Protocol list. */
8319 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
8320 PROTOCOL_FORWARD_DECL (p),
8321 TREE_PURPOSE (*chain));
8328 /* This function is called by the parser when a @selector() expression
8329 is found, in order to compile it. It is only called by the parser
8330 and only to compile a @selector(). LOC is the location of the
8333 objc_build_selector_expr (location_t loc, tree selnamelist)
8337 /* Obtain the full selector name. */
8338 switch (TREE_CODE (selnamelist))
8340 case IDENTIFIER_NODE:
8341 /* A unary selector. */
8342 selname = selnamelist;
8345 selname = build_keyword_selector (selnamelist);
8351 /* If we are required to check @selector() expressions as they
8352 are found, check that the selector has been declared. */
8353 if (warn_undeclared_selector)
8355 /* Look the selector up in the list of all known class and
8356 instance methods (up to this line) to check that the selector
8360 /* First try with instance methods. */
8361 hsh = hash_lookup (nst_method_hash_list, selname);
8363 /* If not found, try with class methods. */
8366 hsh = hash_lookup (cls_method_hash_list, selname);
8369 /* If still not found, print out a warning. */
8372 warning (0, "undeclared selector %qE", selname);
8377 if (flag_typed_selectors)
8378 return build_typed_selector_reference (loc, selname, 0);
8380 return build_selector_reference (loc, selname);
8383 /* This is used to implement @encode(). See gcc/doc/objc.texi,
8384 section '@encode'. */
8386 objc_build_encode_expr (tree type)
8391 encode_type (type, obstack_object_size (&util_obstack),
8392 OBJC_ENCODE_INLINE_DEFS);
8393 obstack_1grow (&util_obstack, 0); /* null terminate string */
8394 string = XOBFINISH (&util_obstack, const char *);
8396 /* Synthesize a string that represents the encoded struct/union. */
8397 result = my_build_string (strlen (string) + 1, string);
8398 obstack_free (&util_obstack, util_firstobj);
8403 build_ivar_reference (tree id)
8405 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
8407 /* Historically, a class method that produced objects (factory
8408 method) would assign `self' to the instance that it
8409 allocated. This would effectively turn the class method into
8410 an instance method. Following this assignment, the instance
8411 variables could be accessed. That practice, while safe,
8412 violates the simple rule that a class method should not refer
8413 to an instance variable. It's better to catch the cases
8414 where this is done unknowingly than to support the above
8416 warning (0, "instance variable %qE accessed in class method",
8418 self_decl = convert (objc_instance_type, self_decl); /* cast */
8421 return objc_build_component_ref (build_indirect_ref (input_location,
8422 self_decl, RO_ARROW),
8426 /* Compute a hash value for a given method SEL_NAME. */
8429 hash_func (tree sel_name)
8431 const unsigned char *s
8432 = (const unsigned char *)IDENTIFIER_POINTER (sel_name);
8436 h = h * 67 + *s++ - 113;
8443 nst_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8444 cls_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8446 cls_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8447 als_name_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
8449 /* Initialize the hash table used to hold the constant string objects. */
8450 string_htab = htab_create_ggc (31, string_hash,
8454 /* This routine adds sel_name to the hash list. sel_name is a class or alias
8455 name for the class. If alias name, then value is its underlying class.
8456 If class, the value is NULL_TREE. */
8459 hash_class_name_enter (hash *hashlist, tree sel_name, tree value)
8462 int slot = hash_func (sel_name) % SIZEHASHTABLE;
8464 obj = ggc_alloc_hashed_entry ();
8465 if (value != NULL_TREE)
8467 /* Save the underlying class for the 'alias' in the hash table */
8468 attr obj_attr = ggc_alloc_hashed_attribute ();
8469 obj_attr->value = value;
8470 obj->list = obj_attr;
8474 obj->next = hashlist[slot];
8475 obj->key = sel_name;
8477 hashlist[slot] = obj; /* append to front */
8482 Searches in the hash table looking for a match for class or alias name.
8486 hash_class_name_lookup (hash *hashlist, tree sel_name)
8490 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
8494 if (sel_name == target->key)
8497 target = target->next;
8502 /* WARNING!!!! hash_enter is called with a method, and will peek
8503 inside to find its selector! But hash_lookup is given a selector
8504 directly, and looks for the selector that's inside the found
8505 entry's key (method) for comparison. */
8508 hash_enter (hash *hashlist, tree method)
8511 int slot = hash_func (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
8513 obj = ggc_alloc_hashed_entry ();
8515 obj->next = hashlist[slot];
8518 hashlist[slot] = obj; /* append to front */
8522 hash_lookup (hash *hashlist, tree sel_name)
8526 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
8530 if (sel_name == METHOD_SEL_NAME (target->key))
8533 target = target->next;
8539 hash_add_attr (hash entry, tree value)
8543 obj = ggc_alloc_hashed_attribute ();
8544 obj->next = entry->list;
8547 entry->list = obj; /* append to front */
8551 lookup_method (tree mchain, tree method)
8555 if (TREE_CODE (method) == IDENTIFIER_NODE)
8558 key = METHOD_SEL_NAME (method);
8562 if (METHOD_SEL_NAME (mchain) == key)
8565 mchain = DECL_CHAIN (mchain);
8570 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
8571 method in INTERFACE, along with any categories and protocols
8572 attached thereto. If method is not found, and the
8573 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
8574 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
8575 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
8576 be found in INTERFACE or any of its superclasses, look for an
8577 _instance_ method of the same name in the root class as a last
8578 resort. This behaviour can be turned off by using
8579 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
8581 If a suitable method cannot be found, return NULL_TREE. */
8584 lookup_method_static (tree interface, tree ident, int flags)
8586 tree meth = NULL_TREE, root_inter = NULL_TREE;
8587 tree inter = interface;
8588 int is_class = (flags & OBJC_LOOKUP_CLASS);
8589 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
8590 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
8594 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
8595 tree category = inter;
8597 /* First, look up the method in the class itself. */
8598 if ((meth = lookup_method (chain, ident)))
8601 /* Failing that, look for the method in each category of the class. */
8602 while ((category = CLASS_CATEGORY_LIST (category)))
8604 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
8606 /* Check directly in each category. */
8607 if ((meth = lookup_method (chain, ident)))
8610 /* Failing that, check in each category's protocols. */
8611 if (CLASS_PROTOCOL_LIST (category))
8613 if ((meth = (lookup_method_in_protocol_list
8614 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
8619 /* If not found in categories, check in protocols of the main class. */
8620 if (CLASS_PROTOCOL_LIST (inter))
8622 if ((meth = (lookup_method_in_protocol_list
8623 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
8627 /* If we were instructed not to look in superclasses, don't. */
8628 if (no_superclasses)
8631 /* Failing that, climb up the inheritance hierarchy. */
8633 inter = lookup_interface (CLASS_SUPER_NAME (inter));
8637 if (is_class && !no_instance_methods_of_root_class)
8639 /* If no class (factory) method was found, check if an _instance_
8640 method of the same name exists in the root class. This is what
8641 the Objective-C runtime will do. */
8642 return lookup_method_static (root_inter, ident, 0);
8646 /* If an instance method was not found, return 0. */
8651 /* Add the method to the hash list if it doesn't contain an identical
8655 add_method_to_hash_list (hash *hash_list, tree method)
8659 if (!(hsh = hash_lookup (hash_list, METHOD_SEL_NAME (method))))
8661 /* Install on a global chain. */
8662 hash_enter (hash_list, method);
8666 /* Check types against those; if different, add to a list. */
8668 int already_there = comp_proto_with_proto (method, hsh->key, 1);
8669 for (loop = hsh->list; !already_there && loop; loop = loop->next)
8670 already_there |= comp_proto_with_proto (method, loop->value, 1);
8672 hash_add_attr (hsh, method);
8677 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
8681 /* @optional methods are added to protocol's OPTIONAL list. Note
8682 that this disables checking that the methods are implemented by
8683 classes implementing the protocol, since these checks only use
8684 the CLASS_CLS_METHODS and CLASS_NST_METHODS. */
8687 gcc_assert (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE);
8688 if (!(mth = lookup_method (is_class
8689 ? PROTOCOL_OPTIONAL_CLS_METHODS (klass)
8690 : PROTOCOL_OPTIONAL_NST_METHODS (klass),
8695 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
8696 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
8700 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
8701 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
8705 else if (!(mth = lookup_method (is_class
8706 ? CLASS_CLS_METHODS (klass)
8707 : CLASS_NST_METHODS (klass), method)))
8709 /* put method on list in reverse order */
8712 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
8713 CLASS_CLS_METHODS (klass) = method;
8717 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
8718 CLASS_NST_METHODS (klass) = method;
8723 /* When processing an @interface for a class or category, give hard
8724 errors on methods with identical selectors but differing argument
8725 and/or return types. We do not do this for @implementations, because
8726 C/C++ will do it for us (i.e., there will be duplicate function
8727 definition errors). */
8728 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
8729 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE)
8730 && !comp_proto_with_proto (method, mth, 1))
8731 error ("duplicate declaration of method %<%c%E%>",
8732 is_class ? '+' : '-',
8733 METHOD_SEL_NAME (mth));
8737 add_method_to_hash_list (cls_method_hash_list, method);
8740 add_method_to_hash_list (nst_method_hash_list, method);
8742 /* Instance methods in root classes (and categories thereof)
8743 may act as class methods as a last resort. We also add
8744 instance methods listed in @protocol declarations to
8745 the class hash table, on the assumption that @protocols
8746 may be adopted by root classes or categories. */
8747 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
8748 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
8749 klass = lookup_interface (CLASS_NAME (klass));
8751 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
8752 || !CLASS_SUPER_NAME (klass))
8753 add_method_to_hash_list (cls_method_hash_list, method);
8760 add_class (tree class_name, tree name)
8762 struct interface_tuple **slot;
8764 /* Put interfaces on list in reverse order. */
8765 TREE_CHAIN (class_name) = interface_chain;
8766 interface_chain = class_name;
8768 if (interface_htab == NULL)
8769 interface_htab = htab_create_ggc (31, hash_interface, eq_interface, NULL);
8770 slot = (struct interface_tuple **)
8771 htab_find_slot_with_hash (interface_htab, name,
8772 IDENTIFIER_HASH_VALUE (name),
8776 *slot = ggc_alloc_cleared_interface_tuple ();
8779 (*slot)->class_name = class_name;
8781 return interface_chain;
8785 add_category (tree klass, tree category)
8787 /* Put categories on list in reverse order. */
8788 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
8792 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
8794 CLASS_SUPER_NAME (category));
8798 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
8799 CLASS_CATEGORY_LIST (klass) = category;
8803 /* Called after parsing each instance variable declaration. Necessary to
8804 preserve typedefs and implement public/private...
8806 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
8809 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
8812 tree field_type = TREE_TYPE (field_decl);
8813 const char *ivar_name = DECL_NAME (field_decl)
8814 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)))
8818 if (TREE_CODE (field_type) == REFERENCE_TYPE)
8820 error ("illegal reference type specified for instance variable %qs",
8822 /* Return class as is without adding this ivar. */
8827 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
8828 || TYPE_SIZE (field_type) == error_mark_node)
8829 /* 'type[0]' is allowed, but 'type[]' is not! */
8831 error ("instance variable %qs has unknown size", ivar_name);
8832 /* Return class as is without adding this ivar. */
8837 /* Check if the ivar being added has a non-POD C++ type. If so, we will
8838 need to either (1) warn the user about it or (2) generate suitable
8839 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
8840 methods (if '-fobjc-call-cxx-cdtors' was specified). */
8841 if (MAYBE_CLASS_TYPE_P (field_type)
8842 && (TYPE_NEEDS_CONSTRUCTING (field_type)
8843 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
8844 || TYPE_POLYMORPHIC_P (field_type)))
8846 tree type_name = OBJC_TYPE_NAME (field_type);
8848 if (flag_objc_call_cxx_cdtors)
8850 /* Since the ObjC runtime will be calling the constructors and
8851 destructors for us, the only thing we can't handle is the lack
8852 of a default constructor. */
8853 if (TYPE_NEEDS_CONSTRUCTING (field_type)
8854 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
8856 warning (0, "type %qE has no default constructor to call",
8859 /* If we cannot call a constructor, we should also avoid
8860 calling the destructor, for symmetry. */
8861 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8862 warning (0, "destructor for %qE shall not be run either",
8868 static bool warn_cxx_ivars = false;
8870 if (TYPE_POLYMORPHIC_P (field_type))
8872 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
8874 error ("type %qE has virtual member functions", type_name);
8875 error ("illegal aggregate type %qE specified "
8876 "for instance variable %qs",
8877 type_name, ivar_name);
8878 /* Return class as is without adding this ivar. */
8882 /* User-defined constructors and destructors are not known to Obj-C
8883 and hence will not be called. This may or may not be a problem. */
8884 if (TYPE_NEEDS_CONSTRUCTING (field_type))
8885 warning (0, "type %qE has a user-defined constructor", type_name);
8886 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
8887 warning (0, "type %qE has a user-defined destructor", type_name);
8889 if (!warn_cxx_ivars)
8891 warning (0, "C++ constructors and destructors will not "
8892 "be invoked for Objective-C fields");
8893 warn_cxx_ivars = true;
8899 /* Overload the public attribute, it is not used for FIELD_DECLs. */
8902 case OBJC_IVAR_VIS_PROTECTED:
8903 TREE_PUBLIC (field_decl) = 0;
8904 TREE_PRIVATE (field_decl) = 0;
8905 TREE_PROTECTED (field_decl) = 1;
8908 case OBJC_IVAR_VIS_PACKAGE:
8909 /* TODO: Implement the package variant. */
8910 case OBJC_IVAR_VIS_PUBLIC:
8911 TREE_PUBLIC (field_decl) = 1;
8912 TREE_PRIVATE (field_decl) = 0;
8913 TREE_PROTECTED (field_decl) = 0;
8916 case OBJC_IVAR_VIS_PRIVATE:
8917 TREE_PUBLIC (field_decl) = 0;
8918 TREE_PRIVATE (field_decl) = 1;
8919 TREE_PROTECTED (field_decl) = 0;
8924 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
8931 is_ivar (tree decl_chain, tree ident)
8933 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
8934 if (DECL_NAME (decl_chain) == ident)
8939 /* True if the ivar is private and we are not in its implementation. */
8942 is_private (tree decl)
8944 return (TREE_PRIVATE (decl)
8945 && ! is_ivar (CLASS_IVARS (implementation_template),
8949 /* We have an instance variable reference;, check to see if it is public. */
8952 objc_is_public (tree expr, tree identifier)
8954 tree basetype, decl;
8957 if (processing_template_decl)
8961 if (TREE_TYPE (expr) == error_mark_node)
8964 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8966 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
8968 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
8970 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
8974 error ("cannot find interface declaration for %qE",
8975 OBJC_TYPE_NAME (basetype));
8979 if ((decl = is_ivar (get_class_ivars (klass, true), identifier)))
8981 if (TREE_PUBLIC (decl))
8984 /* Important difference between the Stepstone translator:
8985 all instance variables should be public within the context
8986 of the implementation. */
8987 if (objc_implementation_context
8988 && ((TREE_CODE (objc_implementation_context)
8989 == CLASS_IMPLEMENTATION_TYPE)
8990 || (TREE_CODE (objc_implementation_context)
8991 == CATEGORY_IMPLEMENTATION_TYPE)))
8993 tree curtype = TYPE_MAIN_VARIANT
8994 (CLASS_STATIC_TEMPLATE
8995 (implementation_template));
8997 if (basetype == curtype
8998 || DERIVED_FROM_P (basetype, curtype))
9000 int priv = is_private (decl);
9003 error ("instance variable %qE is declared private",
9010 /* The 2.95.2 compiler sometimes allowed C functions to access
9011 non-@public ivars. We will let this slide for now... */
9012 if (!objc_method_context)
9014 warning (0, "instance variable %qE is %s; "
9015 "this will be a hard error in the future",
9017 TREE_PRIVATE (decl) ? "@private" : "@protected");
9021 error ("instance variable %qE is declared %s",
9023 TREE_PRIVATE (decl) ? "private" : "protected");
9032 /* Make sure all methods in CHAIN (a list of method declarations from
9033 an @interface or a @protocol) are in IMPLEMENTATION (the
9034 implementation context). This is used to check for example that
9035 all methods declared in an @interface were implemented in an
9038 Some special methods (property setters/getters) are special and if
9039 they are not found in IMPLEMENTATION, we look them up in its
9043 check_methods (tree chain, tree implementation, int mtype)
9048 if (mtype == (int)'+')
9049 list = CLASS_CLS_METHODS (implementation);
9051 list = CLASS_NST_METHODS (implementation);
9055 /* If the method is associated with a dynamic property, then it
9056 is Ok not to have the method implementation, as it will be
9057 generated dynamically at runtime. To decide if the method is
9058 associated with a @dynamic property, we search the list of
9059 @synthesize and @dynamic for this implementation, and look
9060 for any @dynamic property with the same setter or getter name
9063 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
9064 if (PROPERTY_DYNAMIC (x)
9065 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
9066 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
9071 chain = TREE_CHAIN (chain); /* next method... */
9075 if (!lookup_method (list, chain))
9077 /* If the method is a property setter/getter, we'll still
9078 allow it to be missing if it is implemented by
9079 'interface' or any of its superclasses. */
9080 tree property = METHOD_PROPERTY_CONTEXT (chain);
9083 /* Note that since this is a property getter/setter, it
9084 is obviously an instance method. */
9085 tree interface = NULL_TREE;
9087 /* For a category, first check the main class
9089 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
9091 interface = lookup_interface (CLASS_NAME (implementation));
9093 /* If the method is found in the main class, it's Ok. */
9094 if (lookup_method (CLASS_NST_METHODS (interface), chain))
9096 chain = DECL_CHAIN (chain);
9100 /* Else, get the superclass. */
9101 if (CLASS_SUPER_NAME (interface))
9102 interface = lookup_interface (CLASS_SUPER_NAME (interface));
9104 interface = NULL_TREE;
9107 /* Get the superclass for classes. */
9108 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
9110 if (CLASS_SUPER_NAME (implementation))
9111 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
9113 interface = NULL_TREE;
9116 /* Now, interface is the superclass, if any; go check it. */
9119 if (lookup_method_static (interface, chain, 0))
9121 chain = DECL_CHAIN (chain);
9125 /* Else, fall through - warn. */
9129 switch (TREE_CODE (implementation))
9131 case CLASS_IMPLEMENTATION_TYPE:
9132 warning (0, "incomplete implementation of class %qE",
9133 CLASS_NAME (implementation));
9135 case CATEGORY_IMPLEMENTATION_TYPE:
9136 warning (0, "incomplete implementation of category %qE",
9137 CLASS_SUPER_NAME (implementation));
9145 warning (0, "method definition for %<%c%E%> not found",
9146 mtype, METHOD_SEL_NAME (chain));
9149 chain = DECL_CHAIN (chain);
9155 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
9158 conforms_to_protocol (tree klass, tree protocol)
9160 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
9162 tree p = CLASS_PROTOCOL_LIST (klass);
9163 while (p && TREE_VALUE (p) != protocol)
9168 tree super = (CLASS_SUPER_NAME (klass)
9169 ? lookup_interface (CLASS_SUPER_NAME (klass))
9171 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
9180 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
9181 CONTEXT. This is one of two mechanisms to check protocol integrity. */
9184 check_methods_accessible (tree chain, tree context, int mtype)
9188 tree base_context = context;
9192 /* If the method is associated with a dynamic property, then it
9193 is Ok not to have the method implementation, as it will be
9194 generated dynamically at runtime. Search for any @dynamic
9195 property with the same setter or getter name as this
9196 method. TODO: Use a hashtable lookup. */
9198 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
9199 if (PROPERTY_DYNAMIC (x)
9200 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
9201 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
9206 chain = TREE_CHAIN (chain); /* next method... */
9210 context = base_context;
9214 list = CLASS_CLS_METHODS (context);
9216 list = CLASS_NST_METHODS (context);
9218 if (lookup_method (list, chain))
9221 switch (TREE_CODE (context))
9223 case CLASS_IMPLEMENTATION_TYPE:
9224 case CLASS_INTERFACE_TYPE:
9225 context = (CLASS_SUPER_NAME (context)
9226 ? lookup_interface (CLASS_SUPER_NAME (context))
9229 case CATEGORY_IMPLEMENTATION_TYPE:
9230 case CATEGORY_INTERFACE_TYPE:
9231 context = (CLASS_NAME (context)
9232 ? lookup_interface (CLASS_NAME (context))
9240 if (context == NULL_TREE)
9244 switch (TREE_CODE (objc_implementation_context))
9246 case CLASS_IMPLEMENTATION_TYPE:
9247 warning (0, "incomplete implementation of class %qE",
9248 CLASS_NAME (objc_implementation_context));
9250 case CATEGORY_IMPLEMENTATION_TYPE:
9251 warning (0, "incomplete implementation of category %qE",
9252 CLASS_SUPER_NAME (objc_implementation_context));
9259 warning (0, "method definition for %<%c%E%> not found",
9260 mtype, METHOD_SEL_NAME (chain));
9263 chain = TREE_CHAIN (chain); /* next method... */
9268 /* Check whether the current interface (accessible via
9269 'objc_implementation_context') actually implements protocol P, along
9270 with any protocols that P inherits. */
9273 check_protocol (tree p, const char *type, tree name)
9275 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
9279 /* Ensure that all protocols have bodies! */
9282 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
9283 objc_implementation_context,
9285 f2 = check_methods (PROTOCOL_NST_METHODS (p),
9286 objc_implementation_context,
9291 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
9292 objc_implementation_context,
9294 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
9295 objc_implementation_context,
9300 warning (0, "%s %qE does not fully implement the %qE protocol",
9301 type, name, PROTOCOL_NAME (p));
9304 /* Check protocols recursively. */
9305 if (PROTOCOL_LIST (p))
9307 tree subs = PROTOCOL_LIST (p);
9309 lookup_interface (CLASS_SUPER_NAME (implementation_template));
9313 tree sub = TREE_VALUE (subs);
9315 /* If the superclass does not conform to the protocols
9316 inherited by P, then we must! */
9317 if (!super_class || !conforms_to_protocol (super_class, sub))
9318 check_protocol (sub, type, name);
9319 subs = TREE_CHAIN (subs);
9324 /* Check whether the current interface (accessible via
9325 'objc_implementation_context') actually implements the protocols listed
9329 check_protocols (tree proto_list, const char *type, tree name)
9331 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
9333 tree p = TREE_VALUE (proto_list);
9335 check_protocol (p, type, name);
9339 /* Make sure that the class CLASS_NAME is defined
9340 CODE says which kind of thing CLASS_NAME ought to be.
9341 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
9342 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
9345 start_class (enum tree_code code, tree class_name, tree super_name,
9346 tree protocol_list, tree attributes)
9351 if (current_namespace != global_namespace) {
9352 error ("Objective-C declarations may only appear in global scope");
9354 #endif /* OBJCPLUS */
9356 if (objc_implementation_context)
9358 warning (0, "%<@end%> missing in implementation context");
9359 finish_class (objc_implementation_context);
9360 objc_ivar_chain = NULL_TREE;
9361 objc_implementation_context = NULL_TREE;
9364 klass = make_node (code);
9365 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
9367 /* Check for existence of the super class, if one was specified. Note
9368 that we must have seen an @interface, not just a @class. If we
9369 are looking at a @compatibility_alias, traverse it first. */
9370 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
9373 tree super = objc_is_class_name (super_name);
9374 tree super_interface = NULL_TREE;
9377 super_interface = lookup_interface (super);
9379 if (!super_interface)
9381 error ("cannot find interface declaration for %qE, superclass of %qE",
9382 super ? super : super_name,
9384 super_name = NULL_TREE;
9388 if (TREE_DEPRECATED (super_interface))
9389 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
9395 CLASS_NAME (klass) = class_name;
9396 CLASS_SUPER_NAME (klass) = super_name;
9397 CLASS_CLS_METHODS (klass) = NULL_TREE;
9399 if (! objc_is_class_name (class_name)
9400 && (decl = lookup_name (class_name)))
9402 error ("%qE redeclared as different kind of symbol",
9404 error ("previous declaration of %q+D",
9410 case CLASS_IMPLEMENTATION_TYPE:
9414 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
9415 if (TREE_VALUE (chain) == class_name)
9417 error ("reimplementation of class %qE",
9419 return error_mark_node;
9421 implemented_classes = tree_cons (NULL_TREE, class_name,
9422 implemented_classes);
9425 /* Reset for multiple classes per file. */
9428 objc_implementation_context = klass;
9430 /* Lookup the interface for this implementation. */
9432 if (!(implementation_template = lookup_interface (class_name)))
9434 warning (0, "cannot find interface declaration for %qE",
9436 add_class (implementation_template = objc_implementation_context,
9440 /* If a super class has been specified in the implementation,
9441 insure it conforms to the one specified in the interface. */
9444 && (super_name != CLASS_SUPER_NAME (implementation_template)))
9446 tree previous_name = CLASS_SUPER_NAME (implementation_template);
9447 error ("conflicting super class name %qE",
9450 error ("previous declaration of %qE", previous_name);
9452 error ("previous declaration");
9455 else if (! super_name)
9457 CLASS_SUPER_NAME (objc_implementation_context)
9458 = CLASS_SUPER_NAME (implementation_template);
9462 case CLASS_INTERFACE_TYPE:
9463 if (lookup_interface (class_name))
9465 error ("duplicate interface declaration for class %qE", class_name);
9467 warning (0, "duplicate interface declaration for class %qE", class_name);
9470 add_class (klass, class_name);
9473 CLASS_PROTOCOL_LIST (klass)
9474 = lookup_and_install_protocols (protocol_list);
9476 /* Determine if 'deprecated', the only attribute we recognize
9477 for classes, was used. Ignore all other attributes for now,
9478 but store them in the klass. */
9482 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
9484 tree name = TREE_PURPOSE (attribute);
9486 if (is_attribute_p ("deprecated", name))
9487 TREE_DEPRECATED (klass) = 1;
9489 TYPE_ATTRIBUTES (klass) = attributes;
9493 case CATEGORY_INTERFACE_TYPE:
9495 tree class_category_is_assoc_with;
9497 /* For a category, class_name is really the name of the class that
9498 the following set of methods will be associated with. We must
9499 find the interface so that can derive the objects template. */
9500 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
9502 error ("cannot find interface declaration for %qE",
9504 exit (FATAL_EXIT_CODE);
9508 if (TREE_DEPRECATED (class_category_is_assoc_with))
9509 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
9511 add_category (class_category_is_assoc_with, klass);
9515 CLASS_PROTOCOL_LIST (klass)
9516 = lookup_and_install_protocols (protocol_list);
9520 case CATEGORY_IMPLEMENTATION_TYPE:
9521 /* Reset for multiple classes per file. */
9524 objc_implementation_context = klass;
9526 /* For a category, class_name is really the name of the class that
9527 the following set of methods will be associated with. We must
9528 find the interface so that can derive the objects template. */
9530 if (!(implementation_template = lookup_interface (class_name)))
9532 error ("cannot find interface declaration for %qE",
9534 exit (FATAL_EXIT_CODE);
9544 continue_class (tree klass)
9546 switch (TREE_CODE (klass))
9548 case CLASS_IMPLEMENTATION_TYPE:
9549 case CATEGORY_IMPLEMENTATION_TYPE:
9551 struct imp_entry *imp_entry;
9553 /* Check consistency of the instance variables. */
9555 if (CLASS_RAW_IVARS (klass))
9556 check_ivars (implementation_template, klass);
9558 /* code generation */
9560 push_lang_context (lang_name_c);
9562 build_private_template (implementation_template);
9563 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
9564 objc_instance_type = build_pointer_type (uprivate_record);
9566 imp_entry = ggc_alloc_imp_entry ();
9568 imp_entry->next = imp_list;
9569 imp_entry->imp_context = klass;
9570 imp_entry->imp_template = implementation_template;
9572 synth_forward_declarations ();
9573 imp_entry->class_decl = UOBJC_CLASS_decl;
9574 imp_entry->meta_decl = UOBJC_METACLASS_decl;
9575 imp_entry->has_cxx_cdtors = 0;
9577 /* Append to front and increment count. */
9578 imp_list = imp_entry;
9579 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
9584 pop_lang_context ();
9585 #endif /* OBJCPLUS */
9587 return get_class_ivars (implementation_template, true);
9590 case CLASS_INTERFACE_TYPE:
9593 push_lang_context (lang_name_c);
9594 #endif /* OBJCPLUS */
9595 objc_collecting_ivars = 1;
9596 build_private_template (klass);
9597 objc_collecting_ivars = 0;
9599 pop_lang_context ();
9600 #endif /* OBJCPLUS */
9605 return error_mark_node;
9609 /* This routine builds name of the setter synthesized function. */
9611 objc_build_property_setter_name (tree ident)
9613 /* TODO: Use alloca to allocate buffer of appropriate size. */
9614 static char string[BUFSIZE];
9615 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
9616 string[3] = TOUPPER (string[3]);
9620 /* This routine prepares the declarations of the property accessor
9621 helper functions (objc_getProperty(), etc) that are used when
9622 @synthesize is used. */
9624 build_objc_property_accessor_helpers (void)
9628 /* Declare the following function:
9630 objc_getProperty (id self, SEL _cmd,
9631 ptrdiff_t offset, BOOL is_atomic); */
9632 type = build_function_type_list (objc_object_type,
9638 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
9639 type, 0, NOT_BUILT_IN,
9641 TREE_NOTHROW (objc_getProperty_decl) = 0;
9643 /* Declare the following function:
9645 objc_setProperty (id self, SEL _cmd,
9646 ptrdiff_t offset, id new_value,
9647 BOOL is_atomic, BOOL should_copy); */
9648 type = build_function_type_list (void_type_node,
9656 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
9657 type, 0, NOT_BUILT_IN,
9659 TREE_NOTHROW (objc_setProperty_decl) = 0;
9661 /* This is the type of all of the following functions
9662 (objc_copyStruct(), objc_getPropertyStruct() and
9663 objc_setPropertyStruct()). */
9664 type = build_function_type_list (void_type_node,
9666 const_ptr_type_node,
9672 if (flag_next_runtime)
9674 /* Declare the following function:
9676 objc_copyStruct (void *destination, const void *source,
9677 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9678 objc_copyStruct_decl = add_builtin_function ("objc_copyStruct",
9679 type, 0, NOT_BUILT_IN,
9681 TREE_NOTHROW (objc_copyStruct_decl) = 0;
9682 objc_getPropertyStruct_decl = NULL_TREE;
9683 objc_setPropertyStruct_decl = NULL_TREE;
9687 objc_copyStruct_decl = NULL_TREE;
9689 /* Declare the following function:
9691 objc_getPropertyStruct (void *destination, const void *source,
9692 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9693 objc_getPropertyStruct_decl = add_builtin_function ("objc_getPropertyStruct",
9694 type, 0, NOT_BUILT_IN,
9696 TREE_NOTHROW (objc_getPropertyStruct_decl) = 0;
9697 /* Declare the following function:
9699 objc_setPropertyStruct (void *destination, const void *source,
9700 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
9701 objc_setPropertyStruct_decl = add_builtin_function ("objc_setPropertyStruct",
9702 type, 0, NOT_BUILT_IN,
9704 TREE_NOTHROW (objc_setPropertyStruct_decl) = 0;
9708 /* This looks up an ivar in a class (including superclasses). */
9710 lookup_ivar (tree interface, tree instance_variable_name)
9716 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
9717 if (DECL_NAME (decl_chain) == instance_variable_name)
9720 /* Not found. Search superclass if any. */
9721 if (CLASS_SUPER_NAME (interface))
9722 interface = lookup_interface (CLASS_SUPER_NAME (interface));
9728 /* This routine synthesizes a 'getter' method. This is only called
9729 for @synthesize properties. */
9731 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
9733 location_t location = DECL_SOURCE_LOCATION (property);
9738 /* If user has implemented a getter with same name then do nothing. */
9739 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9740 PROPERTY_GETTER_NAME (property)))
9743 /* Find declaration of the property getter in the interface (or
9744 superclass, or protocol). There must be one. */
9745 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
9747 /* If one not declared in the interface, this condition has already
9748 been reported as user error (because property was not declared in
9753 /* Adapt the 'decl'. Use the source location of the @synthesize
9754 statement for error messages. */
9755 decl = copy_node (decl);
9756 DECL_SOURCE_LOCATION (decl) = location;
9758 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9759 body = c_begin_compound_stmt (true);
9761 /* Now we need to decide how we build the getter. There are three
9764 for 'copy' or 'retain' properties we need to use the
9765 objc_getProperty() accessor helper which knows about retain and
9766 copy. It supports both 'nonatomic' and 'atomic' access.
9768 for 'nonatomic, assign' properties we can access the instance
9769 variable directly. 'nonatomic' means we don't have to use locks,
9770 and 'assign' means we don't have to worry about retain or copy.
9771 If you combine the two, it means we can just access the instance
9774 for 'atomic, assign' properties we use objc_copyStruct() (for the
9775 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
9776 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9778 case OBJC_PROPERTY_RETAIN:
9779 case OBJC_PROPERTY_COPY:
9781 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
9782 tree cmd, ivar, offset, is_atomic;
9783 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9785 /* Find the ivar to compute the offset. */
9786 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9787 if (!ivar || is_private (ivar))
9789 /* This should never happen. */
9791 "can not find instance variable associated with property");
9792 ret_val = error_mark_node;
9795 offset = byte_position (ivar);
9797 if (PROPERTY_NONATOMIC (property))
9798 is_atomic = boolean_false_node;
9800 is_atomic = boolean_true_node;
9802 ret_val = build_function_call
9804 /* Function prototype. */
9805 objc_getProperty_decl,
9807 tree_cons /* self */
9808 (NULL_TREE, self_decl,
9809 tree_cons /* _cmd */
9811 tree_cons /* offset */
9813 tree_cons /* is_atomic */
9814 (NULL_TREE, is_atomic, NULL_TREE)))));
9817 case OBJC_PROPERTY_ASSIGN:
9818 if (PROPERTY_NONATOMIC (property))
9820 /* We build "return self->PROPERTY_IVAR_NAME;" */
9821 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
9827 <property type> __objc_property_temp;
9828 objc_getPropertyStruct (&__objc_property_temp,
9829 &(self->PROPERTY_IVAR_NAME),
9830 sizeof (type of self->PROPERTY_IVAR_NAME),
9833 return __objc_property_temp;
9835 For the NeXT runtime, we need to use objc_copyStruct
9836 instead of objc_getPropertyStruct. */
9837 tree objc_property_temp_decl, function_decl, function_call;
9838 tree size_of, is_atomic;
9840 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
9841 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
9842 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
9844 /* sizeof (ivar type). Since the ivar and the property have
9845 the same type, there is no need to lookup the ivar. */
9846 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
9847 true /* is_sizeof */,
9848 false /* complain */);
9850 if (PROPERTY_NONATOMIC (property))
9851 is_atomic = boolean_false_node;
9853 is_atomic = boolean_true_node;
9855 if (flag_next_runtime)
9856 function_decl = objc_copyStruct_decl;
9858 function_decl = objc_getPropertyStruct_decl;
9860 function_call = build_function_call
9862 /* Function prototype. */
9865 tree_cons /* &__objc_property_temp_decl */
9866 /* Warning: note that using build_fold_addr_expr_loc()
9867 here causes invalid code to be generated. */
9868 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
9869 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
9870 (NULL_TREE, build_fold_addr_expr_loc (location,
9872 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
9873 tree_cons /* sizeof (PROPERTY_IVAR) */
9874 (NULL_TREE, size_of,
9875 tree_cons /* is_atomic */
9876 (NULL_TREE, is_atomic,
9877 /* TODO: This is currently ignored by the GNU
9878 runtime, but what about the next one ? */
9879 tree_cons /* has_strong */
9880 (NULL_TREE, boolean_true_node, NULL_TREE))))));
9882 add_stmt (function_call);
9884 ret_val = objc_property_temp_decl;
9891 gcc_assert (ret_val);
9894 finish_return_stmt (ret_val);
9896 c_finish_return (location, ret_val, NULL_TREE);
9899 add_stmt (c_end_compound_stmt (location, body, true));
9900 fn = current_function_decl;
9904 objc_finish_method_definition (fn);
9907 /* This routine synthesizes a 'setter' method. */
9910 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
9912 location_t location = DECL_SOURCE_LOCATION (property);
9915 tree new_value, statement;
9917 /* If user has implemented a setter with same name then do nothing. */
9918 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
9919 PROPERTY_SETTER_NAME (property)))
9922 /* Find declaration of the property setter in the interface (or
9923 superclass, or protocol). There must be one. */
9924 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
9926 /* If one not declared in the interface, this condition has already
9927 been reported as user error (because property was not declared in
9932 /* Adapt the 'decl'. Use the source location of the @synthesize
9933 statement for error messages. */
9934 decl = copy_node (decl);
9935 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
9937 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE);
9939 body = c_begin_compound_stmt (true);
9941 /* The 'new_value' is the only argument to the method, which is the
9942 3rd argument of the function, after self and _cmd. We use twice
9943 TREE_CHAIN to move forward two arguments. */
9944 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
9946 /* This would presumably happen if the user has specified a
9947 prototype for the setter that does not have an argument! */
9948 if (new_value == NULL_TREE)
9950 /* TODO: This should be caught much earlier than this. */
9951 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
9952 /* Try to recover somehow. */
9953 new_value = error_mark_node;
9956 /* Now we need to decide how we build the setter. There are three
9959 for 'copy' or 'retain' properties we need to use the
9960 objc_setProperty() accessor helper which knows about retain and
9961 copy. It supports both 'nonatomic' and 'atomic' access.
9963 for 'nonatomic, assign' properties we can access the instance
9964 variable directly. 'nonatomic' means we don't have to use locks,
9965 and 'assign' means we don't have to worry about retain or copy.
9966 If you combine the two, it means we can just access the instance
9969 for 'atomic, assign' properties we use objc_copyStruct() (for the
9970 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
9971 switch (PROPERTY_ASSIGN_SEMANTICS (property))
9973 case OBJC_PROPERTY_RETAIN:
9974 case OBJC_PROPERTY_COPY:
9976 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
9977 tree cmd, ivar, offset, is_atomic, should_copy;
9978 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
9980 /* Find the ivar to compute the offset. */
9981 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
9982 if (!ivar || is_private (ivar))
9985 "can not find instance variable associated with property");
9986 statement = error_mark_node;
9989 offset = byte_position (ivar);
9991 if (PROPERTY_NONATOMIC (property))
9992 is_atomic = boolean_false_node;
9994 is_atomic = boolean_true_node;
9996 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
9997 should_copy = boolean_true_node;
9999 should_copy = boolean_false_node;
10001 statement = build_function_call
10003 /* Function prototype. */
10004 objc_setProperty_decl,
10006 tree_cons /* self */
10007 (NULL_TREE, self_decl,
10008 tree_cons /* _cmd */
10010 tree_cons /* offset */
10011 (NULL_TREE, offset,
10012 tree_cons /* new_value */
10013 (NULL_TREE, new_value,
10014 tree_cons /* is_atomic */
10015 (NULL_TREE, is_atomic,
10016 tree_cons /* should_copy */
10017 (NULL_TREE, should_copy, NULL_TREE)))))));
10020 case OBJC_PROPERTY_ASSIGN:
10021 if (PROPERTY_NONATOMIC (property))
10023 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
10024 statement = build_modify_expr
10026 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
10027 NULL_TREE, NOP_EXPR,
10028 location, new_value, NULL_TREE);
10034 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
10036 sizeof (type of self->PROPERTY_IVAR_NAME),
10040 For the NeXT runtime, we need to use objc_copyStruct
10041 instead of objc_getPropertyStruct. */
10042 tree function_decl, size_of, is_atomic;
10044 /* sizeof (ivar type). Since the ivar and the property have
10045 the same type, there is no need to lookup the ivar. */
10046 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
10047 true /* is_sizeof */,
10048 false /* complain */);
10050 if (PROPERTY_NONATOMIC (property))
10051 is_atomic = boolean_false_node;
10053 is_atomic = boolean_true_node;
10055 if (flag_next_runtime)
10056 function_decl = objc_copyStruct_decl;
10058 function_decl = objc_setPropertyStruct_decl;
10060 statement = build_function_call
10062 /* Function prototype. */
10065 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
10066 (NULL_TREE, build_fold_addr_expr_loc (location,
10068 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
10069 tree_cons /* &new_value */
10070 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
10071 tree_cons /* sizeof (PROPERTY_IVAR) */
10072 (NULL_TREE, size_of,
10073 tree_cons /* is_atomic */
10074 (NULL_TREE, is_atomic,
10075 /* TODO: This is currently ignored by the GNU
10076 runtime, but what about the next one ? */
10077 tree_cons /* has_strong */
10078 (NULL_TREE, boolean_true_node, NULL_TREE))))));
10082 gcc_unreachable ();
10084 gcc_assert (statement);
10086 add_stmt (statement);
10087 add_stmt (c_end_compound_stmt (location, body, true));
10088 fn = current_function_decl;
10090 finish_function ();
10092 objc_finish_method_definition (fn);
10095 /* This function is a sub-routine of objc_add_synthesize_declaration.
10096 It is called for each property to synthesize once we have
10097 determined that the context is Ok. */
10099 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
10100 tree property_name, tree ivar_name)
10102 /* Find the @property declaration. */
10106 /* Check that synthesize or dynamic has not already been used for
10107 the same property. */
10108 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
10109 if (PROPERTY_NAME (property) == property_name)
10111 location_t original_location = DECL_SOURCE_LOCATION (property);
10113 if (PROPERTY_DYNAMIC (property))
10114 error_at (location, "property %qs already specified in %<@dynamic%>",
10115 IDENTIFIER_POINTER (property_name));
10117 error_at (location, "property %qs already specified in %<@synthesize%>",
10118 IDENTIFIER_POINTER (property_name));
10120 if (original_location != UNKNOWN_LOCATION)
10121 inform (original_location, "originally specified here");
10125 /* Check that the property is declared in the interface. It could
10126 also be declared in a superclass or protocol. */
10127 property = lookup_property (interface, property_name);
10131 error_at (location, "no declaration of property %qs found in the interface",
10132 IDENTIFIER_POINTER (property_name));
10137 /* We have to copy the property, because we want to chain it to
10138 the implementation context, and we want to store the source
10139 location of the @synthesize, not of the original
10141 property = copy_node (property);
10142 DECL_SOURCE_LOCATION (property) = location;
10145 /* Determine PROPERTY_IVAR_NAME. */
10146 if (ivar_name == NULL_TREE)
10147 ivar_name = property_name;
10149 /* Check that the instance variable exists. You can only use an
10150 instance variable from the same class, not one from the
10151 superclass (this makes sense as it allows us to check that an
10152 instance variable is only used in one synthesized property). */
10154 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
10158 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
10159 IDENTIFIER_POINTER (property_name));
10163 if (DECL_BIT_FIELD_TYPE (ivar))
10164 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
10166 type_of_ivar = TREE_TYPE (ivar);
10168 /* If the instance variable has a different C type, we throw an error ... */
10169 if (!comptypes (TREE_TYPE (property), type_of_ivar)
10170 /* ... unless the property is readonly, in which case we allow
10171 the instance variable to be more specialized (this means we
10172 can generate the getter all right and it works). */
10173 && (!PROPERTY_READONLY (property)
10174 || !objc_compare_types (TREE_TYPE (property),
10175 type_of_ivar, -5, NULL_TREE)))
10177 location_t original_location = DECL_SOURCE_LOCATION (ivar);
10179 error_at (location, "property %qs is using instance variable %qs of incompatible type",
10180 IDENTIFIER_POINTER (property_name),
10181 IDENTIFIER_POINTER (ivar_name));
10183 if (original_location != UNKNOWN_LOCATION)
10184 inform (original_location, "originally specified here");
10187 /* If the instance variable is a bitfield, the property must be
10188 'assign', 'nonatomic' because the runtime getter/setter helper
10189 do not work with bitfield instance variables. */
10190 if (DECL_BIT_FIELD_TYPE (ivar))
10192 /* If there is an error, we return and not generate any
10193 getter/setter because trying to set up the runtime
10194 getter/setter helper calls with bitfields is at high risk
10197 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
10199 location_t original_location = DECL_SOURCE_LOCATION (ivar);
10201 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
10202 IDENTIFIER_POINTER (property_name),
10203 IDENTIFIER_POINTER (ivar_name));
10205 if (original_location != UNKNOWN_LOCATION)
10206 inform (original_location, "originally specified here");
10210 if (!PROPERTY_NONATOMIC (property))
10212 location_t original_location = DECL_SOURCE_LOCATION (ivar);
10214 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
10215 IDENTIFIER_POINTER (property_name),
10216 IDENTIFIER_POINTER (ivar_name));
10218 if (original_location != UNKNOWN_LOCATION)
10219 inform (original_location, "originally specified here");
10225 /* Check that no other property is using the same instance
10227 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
10228 if (PROPERTY_IVAR_NAME (x) == ivar_name)
10230 location_t original_location = DECL_SOURCE_LOCATION (x);
10232 error_at (location, "property %qs is using the same instance variable as property %qs",
10233 IDENTIFIER_POINTER (property_name),
10234 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
10236 if (original_location != UNKNOWN_LOCATION)
10237 inform (original_location, "originally specified here");
10239 /* We keep going on. This won't cause the compiler to fail;
10240 the failure would most likely be at runtime. */
10243 /* Note that a @synthesize (and only a @synthesize) always sets
10244 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
10245 @synthesize by that. */
10246 PROPERTY_IVAR_NAME (property) = ivar_name;
10248 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
10249 original declaration; they are always set (with the exception of
10250 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
10252 /* Add the property to the list of properties for current implementation. */
10253 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
10254 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
10256 /* Note how we don't actually synthesize the getter/setter here; it
10257 would be very natural, but we may miss the fact that the user has
10258 implemented his own getter/setter later on in the @implementation
10259 (in which case we shouldn't generate getter/setter). We wait
10260 until we have parsed it all before generating the code. */
10263 /* This function is called by the parser after a @synthesize
10264 expression is parsed. 'location' is the location of the
10265 @synthesize expression, and 'property_and_ivar_list' is a chained
10266 list of the property and ivar names. */
10268 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
10270 tree interface, chain;
10272 if (flag_objc1_only)
10273 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
10275 if (property_and_ivar_list == error_mark_node)
10278 if (!objc_implementation_context)
10280 /* We can get here only in Objective-C; the Objective-C++ parser
10281 detects the problem while parsing, outputs the error
10282 "misplaced '@synthesize' Objective-C++ construct" and skips
10283 the declaration. */
10284 error_at (location, "%<@synthesize%> not in @implementation context");
10288 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
10290 error_at (location, "%<@synthesize%> can not be used in categories");
10294 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
10297 /* I can't see how this could happen, but it is good as a safety check. */
10298 error_at (location,
10299 "%<@synthesize%> requires the @interface of the class to be available");
10303 /* Now, iterate over the properties and do each of them. */
10304 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
10306 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
10307 TREE_PURPOSE (chain));
10311 /* This function is a sub-routine of objc_add_dynamic_declaration. It
10312 is called for each property to mark as dynamic once we have
10313 determined that the context is Ok. */
10315 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
10316 tree property_name)
10318 /* Find the @property declaration. */
10321 /* Check that synthesize or dynamic has not already been used for
10322 the same property. */
10323 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
10324 if (PROPERTY_NAME (property) == property_name)
10326 location_t original_location = DECL_SOURCE_LOCATION (property);
10328 if (PROPERTY_DYNAMIC (property))
10329 error_at (location, "property %qs already specified in %<@dynamic%>",
10330 IDENTIFIER_POINTER (property_name));
10332 error_at (location, "property %qs already specified in %<@synthesize%>",
10333 IDENTIFIER_POINTER (property_name));
10335 if (original_location != UNKNOWN_LOCATION)
10336 inform (original_location, "originally specified here");
10340 /* Check that the property is declared in the interface. It could
10341 also be declared in a superclass or protocol. */
10342 property = lookup_property (interface, property_name);
10346 error_at (location, "no declaration of property %qs found in the interface",
10347 IDENTIFIER_POINTER (property_name));
10352 /* We have to copy the property, because we want to chain it to
10353 the implementation context, and we want to store the source
10354 location of the @synthesize, not of the original
10356 property = copy_node (property);
10357 DECL_SOURCE_LOCATION (property) = location;
10360 /* Note that a @dynamic (and only a @dynamic) always sets
10361 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
10362 (actually, as explained above, PROPERTY_DECL generated by
10363 @property and associated with a @dynamic property are also marked
10364 as PROPERTY_DYNAMIC). */
10365 PROPERTY_DYNAMIC (property) = 1;
10367 /* Add the property to the list of properties for current implementation. */
10368 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
10369 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
10372 /* This function is called by the parser after a @dynamic expression
10373 is parsed. 'location' is the location of the @dynamic expression,
10374 and 'property_list' is a chained list of all the property
10377 objc_add_dynamic_declaration (location_t location, tree property_list)
10379 tree interface, chain;
10381 if (flag_objc1_only)
10382 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
10384 if (property_list == error_mark_node)
10387 if (!objc_implementation_context)
10389 /* We can get here only in Objective-C; the Objective-C++ parser
10390 detects the problem while parsing, outputs the error
10391 "misplaced '@dynamic' Objective-C++ construct" and skips the
10393 error_at (location, "%<@dynamic%> not in @implementation context");
10397 /* @dynamic is allowed in categories. */
10398 switch (TREE_CODE (objc_implementation_context))
10400 case CLASS_IMPLEMENTATION_TYPE:
10401 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
10403 case CATEGORY_IMPLEMENTATION_TYPE:
10404 interface = lookup_category (implementation_template,
10405 CLASS_SUPER_NAME (objc_implementation_context));
10408 gcc_unreachable ();
10413 /* I can't see how this could happen, but it is good as a safety check. */
10414 error_at (location,
10415 "%<@dynamic%> requires the @interface of the class to be available");
10419 /* Now, iterate over the properties and do each of them. */
10420 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
10422 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
10426 /* Main routine to generate code/data for all the property information for
10427 current implementation (class or category). CLASS is the interface where
10428 ivars are declared. CLASS_METHODS is where methods are found which
10429 could be a class or a category depending on whether we are implementing
10430 property of a class or a category. */
10433 objc_gen_property_data (tree klass, tree class_methods)
10437 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
10439 /* @dynamic property - nothing to check or synthesize. */
10440 if (PROPERTY_DYNAMIC (x))
10443 /* @synthesize property - need to synthesize the accessors. */
10444 if (PROPERTY_IVAR_NAME (x))
10446 objc_synthesize_getter (klass, class_methods, x);
10448 if (PROPERTY_READONLY (x) == 0)
10449 objc_synthesize_setter (klass, class_methods, x);
10454 gcc_unreachable ();
10458 /* This is called once we see the "@end" in an interface/implementation. */
10461 finish_class (tree klass)
10463 switch (TREE_CODE (klass))
10465 case CLASS_IMPLEMENTATION_TYPE:
10467 /* All code generation is done in finish_objc. */
10469 /* Generate what needed for property; setters, getters, etc. */
10470 objc_gen_property_data (implementation_template, implementation_template);
10472 if (implementation_template != objc_implementation_context)
10474 /* Ensure that all method listed in the interface contain bodies. */
10475 check_methods (CLASS_CLS_METHODS (implementation_template),
10476 objc_implementation_context, '+');
10477 check_methods (CLASS_NST_METHODS (implementation_template),
10478 objc_implementation_context, '-');
10480 if (CLASS_PROTOCOL_LIST (implementation_template))
10481 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
10483 CLASS_NAME (objc_implementation_context));
10487 case CATEGORY_IMPLEMENTATION_TYPE:
10489 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
10493 /* Generate what needed for property; setters, getters, etc. */
10494 objc_gen_property_data (implementation_template, category);
10496 /* Ensure all method listed in the interface contain bodies. */
10497 check_methods (CLASS_CLS_METHODS (category),
10498 objc_implementation_context, '+');
10499 check_methods (CLASS_NST_METHODS (category),
10500 objc_implementation_context, '-');
10502 if (CLASS_PROTOCOL_LIST (category))
10503 check_protocols (CLASS_PROTOCOL_LIST (category),
10505 CLASS_SUPER_NAME (objc_implementation_context));
10509 case CLASS_INTERFACE_TYPE:
10510 case CATEGORY_INTERFACE_TYPE:
10511 case PROTOCOL_INTERFACE_TYPE:
10513 /* Process properties of the class. */
10515 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
10517 /* Now we check that the appropriate getter is declared,
10518 and if not, we declare one ourselves. */
10519 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
10520 PROPERTY_GETTER_NAME (x));
10524 /* TODO: Check that the declaration is consistent with the property. */
10529 /* Generate an instance method declaration for the
10530 getter; for example "- (id) name;". In general it
10531 will be of the form
10532 -(type)property_getter_name; */
10533 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
10534 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
10535 rettype, PROPERTY_GETTER_NAME (x),
10537 objc_add_method (objc_interface_context, getter_decl, false, false);
10538 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
10541 if (PROPERTY_READONLY (x) == 0)
10543 /* Now we check that the appropriate setter is declared,
10544 and if not, we declare on ourselves. */
10545 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
10546 PROPERTY_SETTER_NAME (x));
10550 /* TODO: Check that the declaration is consistent with the property. */
10555 /* The setter name is something like 'setName:'.
10556 We need the substring 'setName' to build the
10557 method declaration due to how the declaration
10558 works. TODO: build_method_decl() will then
10559 generate back 'setName:' from 'setName'; it
10560 would be more efficient to hook into there. */
10561 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
10562 size_t length = strlen (full_setter_name);
10563 char *setter_name = (char *) alloca (length);
10564 tree ret_type, selector, arg_type, arg_name;
10566 strcpy (setter_name, full_setter_name);
10567 setter_name[length - 1] = '\0';
10568 ret_type = build_tree_list (NULL_TREE, void_type_node);
10569 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
10570 arg_name = get_identifier ("_value");
10571 selector = objc_build_keyword_decl (get_identifier (setter_name),
10572 arg_type, arg_name, NULL);
10573 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
10574 ret_type, selector,
10575 build_tree_list (NULL_TREE, NULL_TREE),
10577 objc_add_method (objc_interface_context, setter_decl, false, false);
10578 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
10585 gcc_unreachable ();
10591 add_protocol (tree protocol)
10593 /* Put protocol on list in reverse order. */
10594 TREE_CHAIN (protocol) = protocol_chain;
10595 protocol_chain = protocol;
10596 return protocol_chain;
10599 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
10600 emitted if the protocol is deprecated. */
10603 lookup_protocol (tree ident, bool warn_if_deprecated)
10607 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
10608 if (ident == PROTOCOL_NAME (chain))
10610 if (warn_if_deprecated && TREE_DEPRECATED (chain))
10612 /* It would be nice to use warn_deprecated_use() here, but
10613 we are using TREE_CHAIN (which is supposed to be the
10614 TYPE_STUB_DECL for a TYPE) for something different. */
10615 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
10616 PROTOCOL_NAME (chain));
10625 /* This function forward declares the protocols named by NAMES. If
10626 they are already declared or defined, the function has no effect. */
10629 objc_declare_protocols (tree names, tree attributes)
10632 bool deprecated = false;
10635 if (current_namespace != global_namespace) {
10636 error ("Objective-C declarations may only appear in global scope");
10638 #endif /* OBJCPLUS */
10640 /* Determine if 'deprecated', the only attribute we recognize for
10641 protocols, was used. Ignore all other attributes. */
10645 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
10647 tree name = TREE_PURPOSE (attribute);
10649 if (is_attribute_p ("deprecated", name))
10654 for (list = names; list; list = TREE_CHAIN (list))
10656 tree name = TREE_VALUE (list);
10658 if (lookup_protocol (name, /* warn if deprecated */ false) == NULL_TREE)
10660 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
10662 TYPE_LANG_SLOT_1 (protocol)
10663 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
10664 PROTOCOL_NAME (protocol) = name;
10665 PROTOCOL_LIST (protocol) = NULL_TREE;
10666 add_protocol (protocol);
10667 PROTOCOL_DEFINED (protocol) = 0;
10668 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
10672 TYPE_ATTRIBUTES (protocol) = attributes;
10674 TREE_DEPRECATED (protocol) = 1;
10681 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
10684 bool deprecated = false;
10687 if (current_namespace != global_namespace) {
10688 error ("Objective-C declarations may only appear in global scope");
10690 #endif /* OBJCPLUS */
10692 /* Determine if 'deprecated', the only attribute we recognize for
10693 protocols, was used. Ignore all other attributes. */
10697 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
10699 tree name = TREE_PURPOSE (attribute);
10701 if (is_attribute_p ("deprecated", name))
10706 protocol = lookup_protocol (name, /* warn_if_deprecated */ false);
10710 protocol = make_node (code);
10711 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
10713 PROTOCOL_NAME (protocol) = name;
10714 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
10715 add_protocol (protocol);
10716 PROTOCOL_DEFINED (protocol) = 1;
10717 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
10719 check_protocol_recursively (protocol, list);
10721 else if (! PROTOCOL_DEFINED (protocol))
10723 PROTOCOL_DEFINED (protocol) = 1;
10724 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
10726 check_protocol_recursively (protocol, list);
10730 warning (0, "duplicate declaration for protocol %qE",
10736 TYPE_ATTRIBUTES (protocol) = attributes;
10738 TREE_DEPRECATED (protocol) = 1;
10745 /* "Encode" a data type into a string, which grows in util_obstack.
10747 The format is described in gcc/doc/objc.texi, section 'Type
10750 Most of the encode_xxx functions have a 'type' argument, which is
10751 the type to encode, and an integer 'curtype' argument, which is the
10752 index in the encoding string of the beginning of the encoding of
10753 the current type, and allows you to find what characters have
10754 already been written for the current type (they are the ones in the
10755 current encoding string starting from 'curtype').
10757 For example, if we are encoding a method which returns 'int' and
10758 takes a 'char **' argument, then when we get to the point of
10759 encoding the 'char **' argument, the encoded string already
10760 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
10761 'curtype' will be set to 7 when starting to encode 'char **'.
10762 During the whole of the encoding of 'char **', 'curtype' will be
10763 fixed at 7, so the routine encoding the second pointer can find out
10764 that it's actually encoding a pointer to a pointer by looking
10765 backwards at what has already been encoded for the current type,
10766 and seeing there is a "^" (meaning a pointer) in there.
10770 /* Encode type qualifiers encodes one of the "PQ" Objective-C
10771 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
10772 'const', instead, is encoded directly as part of the type.
10776 encode_type_qualifiers (tree declspecs)
10780 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10782 /* FIXME: Shouldn't we use token->keyword here ? */
10783 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
10784 obstack_1grow (&util_obstack, 'n');
10785 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
10786 obstack_1grow (&util_obstack, 'N');
10787 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
10788 obstack_1grow (&util_obstack, 'o');
10789 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
10790 obstack_1grow (&util_obstack, 'O');
10791 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
10792 obstack_1grow (&util_obstack, 'R');
10793 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
10794 obstack_1grow (&util_obstack, 'V');
10796 gcc_unreachable ();
10800 /* Determine if a pointee is marked read-only. Only used by the NeXT
10801 runtime to be compatible with gcc-3.3. */
10804 pointee_is_readonly (tree pointee)
10806 while (POINTER_TYPE_P (pointee))
10807 pointee = TREE_TYPE (pointee);
10809 return TYPE_READONLY (pointee);
10812 /* Encode a pointer type. */
10815 encode_pointer (tree type, int curtype, int format)
10817 tree pointer_to = TREE_TYPE (type);
10819 if (flag_next_runtime)
10821 /* This code is used to be compatible with gcc-3.3. */
10822 /* For historical/compatibility reasons, the read-only qualifier
10823 of the pointee gets emitted _before_ the '^'. The read-only
10824 qualifier of the pointer itself gets ignored, _unless_ we are
10825 looking at a typedef! Also, do not emit the 'r' for anything
10826 but the outermost type! */
10827 if (!generating_instance_variables
10828 && (obstack_object_size (&util_obstack) - curtype <= 1)
10829 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10830 ? TYPE_READONLY (type)
10831 : pointee_is_readonly (pointer_to)))
10832 obstack_1grow (&util_obstack, 'r');
10835 if (TREE_CODE (pointer_to) == RECORD_TYPE)
10837 if (OBJC_TYPE_NAME (pointer_to)
10838 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
10840 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
10842 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
10844 obstack_1grow (&util_obstack, '@');
10847 else if (TYPE_HAS_OBJC_INFO (pointer_to)
10848 && TYPE_OBJC_INTERFACE (pointer_to))
10850 if (generating_instance_variables)
10852 obstack_1grow (&util_obstack, '@');
10853 obstack_1grow (&util_obstack, '"');
10854 obstack_grow (&util_obstack, name, strlen (name));
10855 obstack_1grow (&util_obstack, '"');
10860 obstack_1grow (&util_obstack, '@');
10864 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
10866 obstack_1grow (&util_obstack, '#');
10869 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
10871 obstack_1grow (&util_obstack, ':');
10876 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
10877 && TYPE_MODE (pointer_to) == QImode)
10879 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
10880 ? OBJC_TYPE_NAME (pointer_to)
10881 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
10883 /* (BOOL *) are an exception and are encoded as ^c, while all
10884 other pointers to char are encoded as *. */
10885 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
10887 if (!flag_next_runtime)
10889 /* The NeXT runtime adds the 'r' before getting here. */
10891 /* It appears that "r*" means "const char *" rather than
10892 "char *const". "char *const" is encoded as "*",
10893 which is identical to "char *", so the "const" is
10894 unfortunately lost. */
10895 if (TYPE_READONLY (pointer_to))
10896 obstack_1grow (&util_obstack, 'r');
10899 obstack_1grow (&util_obstack, '*');
10904 /* We have a normal pointer type that does not get special treatment. */
10905 obstack_1grow (&util_obstack, '^');
10906 encode_type (pointer_to, curtype, format);
10910 encode_array (tree type, int curtype, int format)
10912 tree an_int_cst = TYPE_SIZE (type);
10913 tree array_of = TREE_TYPE (type);
10916 if (an_int_cst == NULL)
10918 /* We are trying to encode an incomplete array. An incomplete
10919 array is forbidden as part of an instance variable. */
10920 if (generating_instance_variables)
10922 /* TODO: Detect this error earlier. */
10923 error ("instance variable has unknown size");
10927 /* So the only case in which an incomplete array could occur is
10928 if we are encoding the arguments or return value of a method.
10929 In that case, an incomplete array argument or return value
10930 (eg, -(void)display: (char[])string) is treated like a
10931 pointer because that is how the compiler does the function
10932 call. A special, more complicated case, is when the
10933 incomplete array is the last member of a struct (eg, if we
10934 are encoding "struct { unsigned long int a;double b[];}"),
10935 which is again part of a method argument/return value. In
10936 that case, we really need to communicate to the runtime that
10937 there is an incomplete array (not a pointer!) there. So, we
10938 detect that special case and encode it as a zero-length
10941 Try to detect that we are part of a struct. We do this by
10942 searching for '=' in the type encoding for the current type.
10943 NB: This hack assumes that you can't use '=' as part of a C
10947 char *enc = obstack_base (&util_obstack) + curtype;
10948 if (memchr (enc, '=',
10949 obstack_object_size (&util_obstack) - curtype) == NULL)
10951 /* We are not inside a struct. Encode the array as a
10953 encode_pointer (type, curtype, format);
10958 /* Else, we are in a struct, and we encode it as a zero-length
10960 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10962 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
10963 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
10965 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
10966 TREE_INT_CST_LOW (an_int_cst)
10967 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
10969 obstack_grow (&util_obstack, buffer, strlen (buffer));
10970 encode_type (array_of, curtype, format);
10971 obstack_1grow (&util_obstack, ']');
10975 /* Encode a vector. The vector type is a GCC extension to C. */
10977 encode_vector (tree type, int curtype, int format)
10979 tree vector_of = TREE_TYPE (type);
10982 /* Vectors are like simple fixed-size arrays. */
10984 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
10985 alignment of the vector, and <code> is the base type. Eg, int
10986 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
10987 assuming that the alignment is 32 bytes. We include size and
10988 alignment in bytes so that the runtime does not have to have any
10989 knowledge of the actual types.
10991 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
10992 /* We want to compute the equivalent of sizeof (<vector>).
10993 Code inspired by c_sizeof_or_alignof_type. */
10994 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
10995 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
10996 /* We want to compute the equivalent of __alignof__
10997 (<vector>). Code inspired by
10998 c_sizeof_or_alignof_type. */
10999 TYPE_ALIGN_UNIT (type));
11000 obstack_grow (&util_obstack, buffer, strlen (buffer));
11001 encode_type (vector_of, curtype, format);
11002 obstack_1grow (&util_obstack, ']');
11007 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
11009 tree field = TYPE_FIELDS (type);
11011 for (; field; field = DECL_CHAIN (field))
11014 /* C++ static members, and things that are not field at all,
11015 should not appear in the encoding. */
11016 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
11020 /* Recursively encode fields of embedded base classes. */
11021 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
11022 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
11024 encode_aggregate_fields (TREE_TYPE (field),
11025 pointed_to, curtype, format);
11029 if (generating_instance_variables && !pointed_to)
11031 tree fname = DECL_NAME (field);
11033 obstack_1grow (&util_obstack, '"');
11035 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
11036 obstack_grow (&util_obstack,
11037 IDENTIFIER_POINTER (fname),
11038 strlen (IDENTIFIER_POINTER (fname)));
11040 obstack_1grow (&util_obstack, '"');
11043 encode_field_decl (field, curtype, format);
11048 encode_aggregate_within (tree type, int curtype, int format, int left,
11052 /* NB: aggregates that are pointed to have slightly different encoding
11053 rules in that you never encode the names of instance variables. */
11054 int ob_size = obstack_object_size (&util_obstack);
11055 bool inline_contents = false;
11056 bool pointed_to = false;
11058 if (flag_next_runtime)
11060 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
11063 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
11064 && (!pointed_to || ob_size - curtype == 1
11065 || (ob_size - curtype == 2
11066 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
11067 inline_contents = true;
11071 /* c0 and c1 are the last two characters in the encoding of the
11072 current type; if the last two characters were '^' or '^r',
11073 then we are encoding an aggregate that is "pointed to". The
11074 comment above applies: in that case we should avoid encoding
11075 the names of instance variables.
11077 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
11078 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
11080 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
11083 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
11086 inline_contents = true;
11089 /* Note that the check (ob_size - curtype < 2) prevents
11090 infinite recursion when encoding a structure which is
11091 a linked list (eg, struct node { struct node *next;
11092 }). Each time we follow a pointer, we add one
11093 character to ob_size, and curtype is fixed, so after
11094 at most two pointers we stop inlining contents and
11097 The other case where we don't inline is "^r", which
11098 is a pointer to a constant struct.
11100 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
11101 inline_contents = true;
11106 /* Traverse struct aliases; it is important to get the
11107 original struct and its tag name (if any). */
11108 type = TYPE_MAIN_VARIANT (type);
11109 name = OBJC_TYPE_NAME (type);
11110 /* Open parenth/bracket. */
11111 obstack_1grow (&util_obstack, left);
11113 /* Encode the struct/union tag name, or '?' if a tag was
11114 not provided. Typedef aliases do not qualify. */
11116 /* For compatibility with the NeXT runtime, ObjC++ encodes template
11117 args as a composite struct tag name. */
11118 if (name && TREE_CODE (name) == IDENTIFIER_NODE
11119 /* Did this struct have a tag? */
11120 && !TYPE_WAS_ANONYMOUS (type))
11121 obstack_grow (&util_obstack,
11122 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
11123 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
11125 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
11126 obstack_grow (&util_obstack,
11127 IDENTIFIER_POINTER (name),
11128 strlen (IDENTIFIER_POINTER (name)));
11131 obstack_1grow (&util_obstack, '?');
11133 /* Encode the types (and possibly names) of the inner fields,
11135 if (inline_contents)
11137 obstack_1grow (&util_obstack, '=');
11138 encode_aggregate_fields (type, pointed_to, curtype, format);
11140 /* Close parenth/bracket. */
11141 obstack_1grow (&util_obstack, right);
11144 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
11148 encode_next_bitfield (int width)
11151 sprintf (buffer, "b%d", width);
11152 obstack_grow (&util_obstack, buffer, strlen (buffer));
11156 /* Encodes 'type', ignoring type qualifiers (which you should encode
11157 beforehand if needed) with the exception of 'const', which is
11158 encoded by encode_type. See above for the explanation of
11159 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
11160 OBJC_ENCODE_DONT_INLINE_DEFS.
11163 encode_type (tree type, int curtype, int format)
11165 enum tree_code code = TREE_CODE (type);
11167 /* Ignore type qualifiers other than 'const' when encoding a
11170 if (type == error_mark_node)
11173 if (!flag_next_runtime)
11175 if (TYPE_READONLY (type))
11176 obstack_1grow (&util_obstack, 'r');
11181 case ENUMERAL_TYPE:
11182 if (flag_next_runtime)
11184 /* Kludge for backwards-compatibility with gcc-3.3: enums
11185 are always encoded as 'i' no matter what type they
11186 actually are (!). */
11187 obstack_1grow (&util_obstack, 'i');
11190 /* Else, they are encoded exactly like the integer type that is
11191 used by the compiler to store them. */
11195 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
11197 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
11198 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
11201 tree int_type = type;
11202 if (flag_next_runtime)
11204 /* Another legacy kludge for compatiblity with
11205 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
11206 but not always. For typedefs, we need to use 'i'
11207 or 'I' instead if encoding a struct field, or a
11209 int_type = ((!generating_instance_variables
11210 && (obstack_object_size (&util_obstack)
11211 == (unsigned) curtype))
11212 ? TYPE_MAIN_VARIANT (type)
11215 if (int_type == long_unsigned_type_node
11216 || int_type == long_integer_type_node)
11217 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
11219 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
11222 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
11223 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
11224 default: gcc_unreachable ();
11226 obstack_1grow (&util_obstack, c);
11232 /* Floating point types. */
11233 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
11235 case 32: c = 'f'; break;
11236 case 64: c = 'd'; break;
11238 case 128: c = 'D'; break;
11239 default: gcc_unreachable ();
11241 obstack_1grow (&util_obstack, c);
11245 obstack_1grow (&util_obstack, 'v');
11249 obstack_1grow (&util_obstack, 'B');
11253 encode_array (type, curtype, format);
11258 case REFERENCE_TYPE:
11260 encode_pointer (type, curtype, format);
11264 encode_aggregate_within (type, curtype, format, '{', '}');
11268 encode_aggregate_within (type, curtype, format, '(', ')');
11271 case FUNCTION_TYPE: /* '?' means an unknown type. */
11272 obstack_1grow (&util_obstack, '?');
11276 /* A complex is encoded as 'j' followed by the inner type (eg,
11277 "_Complex int" is encoded as 'ji'). */
11278 obstack_1grow (&util_obstack, 'j');
11279 encode_type (TREE_TYPE (type), curtype, format);
11283 encode_vector (type, curtype, format);
11287 warning (0, "unknown type %s found during Objective-C encoding",
11288 gen_type_name (type));
11289 obstack_1grow (&util_obstack, '?');
11293 if (flag_next_runtime)
11295 /* Super-kludge. Some ObjC qualifier and type combinations need
11296 to be rearranged for compatibility with gcc-3.3. */
11297 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
11299 char *enc = obstack_base (&util_obstack) + curtype;
11301 /* Rewrite "in const" from "nr" to "rn". */
11302 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
11303 strncpy (enc - 1, "rn", 2);
11309 encode_gnu_bitfield (int position, tree type, int size)
11311 enum tree_code code = TREE_CODE (type);
11313 char charType = '?';
11315 /* This code is only executed for the GNU runtime, so we can ignore
11316 the NeXT runtime kludge of always encoding enums as 'i' no matter
11317 what integers they actually are. */
11318 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
11320 if (integer_zerop (TYPE_MIN_VALUE (type)))
11321 /* Unsigned integer types. */
11323 switch (TYPE_MODE (type))
11326 charType = 'C'; break;
11328 charType = 'S'; break;
11331 if (type == long_unsigned_type_node)
11338 charType = 'Q'; break;
11340 gcc_unreachable ();
11344 /* Signed integer types. */
11346 switch (TYPE_MODE (type))
11349 charType = 'c'; break;
11351 charType = 's'; break;
11354 if (type == long_integer_type_node)
11361 charType = 'q'; break;
11363 gcc_unreachable ();
11369 /* Do not do any encoding, produce an error and keep going. */
11370 error ("trying to encode non-integer type as a bitfield");
11374 sprintf (buffer, "b%d%c%d", position, charType, size);
11375 obstack_grow (&util_obstack, buffer, strlen (buffer));
11379 encode_field_decl (tree field_decl, int curtype, int format)
11382 /* C++ static members, and things that are not fields at all,
11383 should not appear in the encoding. */
11384 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
11388 /* Generate the bitfield typing information, if needed. Note the difference
11389 between GNU and NeXT runtimes. */
11390 if (DECL_BIT_FIELD_TYPE (field_decl))
11392 int size = tree_low_cst (DECL_SIZE (field_decl), 1);
11394 if (flag_next_runtime)
11395 encode_next_bitfield (size);
11397 encode_gnu_bitfield (int_bit_position (field_decl),
11398 DECL_BIT_FIELD_TYPE (field_decl), size);
11401 encode_type (TREE_TYPE (field_decl), curtype, format);
11404 /* Decay array and function parameters into pointers. */
11407 objc_decay_parm_type (tree type)
11409 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
11410 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
11417 static GTY(()) tree objc_parmlist = NULL_TREE;
11419 /* Append PARM to a list of formal parameters of a method, making a necessary
11420 array-to-pointer adjustment along the way. */
11423 objc_push_parm (tree parm)
11427 if (TREE_TYPE (parm) == error_mark_node)
11429 objc_parmlist = chainon (objc_parmlist, parm);
11433 /* Decay arrays and functions into pointers. */
11434 type = objc_decay_parm_type (TREE_TYPE (parm));
11436 /* If the parameter type has been decayed, a new PARM_DECL needs to be
11438 if (type != TREE_TYPE (parm))
11439 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
11441 DECL_ARG_TYPE (parm)
11442 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
11444 /* Record constancy and volatility. */
11445 c_apply_type_quals_to_decl
11446 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
11447 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
11448 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
11450 objc_parmlist = chainon (objc_parmlist, parm);
11453 /* Retrieve the formal parameter list constructed via preceding calls to
11454 objc_push_parm(). */
11458 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED)
11460 static struct c_arg_info *
11461 objc_get_parm_info (int have_ellipsis)
11465 tree parm_info = objc_parmlist;
11466 objc_parmlist = NULL_TREE;
11470 tree parm_info = objc_parmlist;
11471 struct c_arg_info *arg_info;
11472 /* The C front-end requires an elaborate song and dance at
11475 declare_parm_level ();
11478 tree next = DECL_CHAIN (parm_info);
11480 DECL_CHAIN (parm_info) = NULL_TREE;
11481 parm_info = pushdecl (parm_info);
11482 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
11485 arg_info = get_parm_info (have_ellipsis);
11487 objc_parmlist = NULL_TREE;
11492 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
11493 method definitions. In the case of instance methods, we can be more
11494 specific as to the type of 'self'. */
11497 synth_self_and_ucmd_args (void)
11501 if (objc_method_context
11502 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
11503 self_type = objc_instance_type;
11505 /* Really a `struct objc_class *'. However, we allow people to
11506 assign to self, which changes its type midstream. */
11507 self_type = objc_object_type;
11510 objc_push_parm (build_decl (input_location,
11511 PARM_DECL, self_id, self_type));
11514 objc_push_parm (build_decl (input_location,
11515 PARM_DECL, ucmd_id, objc_selector_type));
11518 /* Transform an Objective-C method definition into a static C function
11519 definition, synthesizing the first two arguments, "self" and "_cmd",
11523 start_method_def (tree method)
11529 struct c_arg_info *parm_info;
11531 int have_ellipsis = 0;
11533 /* If we are defining a "dealloc" method in a non-root class, we
11534 will need to check if a [super dealloc] is missing, and warn if
11536 if(CLASS_SUPER_NAME (objc_implementation_context)
11537 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
11538 should_call_super_dealloc = 1;
11540 should_call_super_dealloc = 0;
11542 /* Required to implement _msgSuper. */
11543 objc_method_context = method;
11544 UOBJC_SUPER_decl = NULL_TREE;
11546 /* Generate prototype declarations for arguments..."new-style". */
11547 synth_self_and_ucmd_args ();
11549 /* Generate argument declarations if a keyword_decl. */
11550 parmlist = METHOD_SEL_ARGS (method);
11553 /* parmlist is a KEYWORD_DECL. */
11554 tree type = TREE_VALUE (TREE_TYPE (parmlist));
11557 parm = build_decl (input_location,
11558 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
11559 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
11560 objc_push_parm (parm);
11561 parmlist = DECL_CHAIN (parmlist);
11564 if (METHOD_ADD_ARGS (method))
11568 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
11569 akey; akey = TREE_CHAIN (akey))
11571 objc_push_parm (TREE_VALUE (akey));
11574 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
11578 parm_info = objc_get_parm_info (have_ellipsis);
11580 really_start_method (objc_method_context, parm_info);
11583 /* Return 1 if TYPE1 is equivalent to TYPE2
11584 for purposes of method overloading. */
11587 objc_types_are_equivalent (tree type1, tree type2)
11589 if (type1 == type2)
11592 /* Strip away indirections. */
11593 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
11594 && (TREE_CODE (type1) == TREE_CODE (type2)))
11595 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
11596 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
11599 type1 = (TYPE_HAS_OBJC_INFO (type1)
11600 ? TYPE_OBJC_PROTOCOL_LIST (type1)
11602 type2 = (TYPE_HAS_OBJC_INFO (type2)
11603 ? TYPE_OBJC_PROTOCOL_LIST (type2)
11606 if (list_length (type1) == list_length (type2))
11608 for (; type2; type2 = TREE_CHAIN (type2))
11609 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (type2)))
11616 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
11619 objc_types_share_size_and_alignment (tree type1, tree type2)
11621 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
11622 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
11625 /* Return 1 if PROTO1 is equivalent to PROTO2
11626 for purposes of method overloading. Ordinarily, the type signatures
11627 should match up exactly, unless STRICT is zero, in which case we
11628 shall allow differences in which the size and alignment of a type
11632 comp_proto_with_proto (tree proto1, tree proto2, int strict)
11634 /* The following test is needed in case there are hashing
11636 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
11639 return match_proto_with_proto (proto1, proto2, strict);
11643 match_proto_with_proto (tree proto1, tree proto2, int strict)
11647 /* Compare return types. */
11648 type1 = TREE_VALUE (TREE_TYPE (proto1));
11649 type2 = TREE_VALUE (TREE_TYPE (proto2));
11651 if (!objc_types_are_equivalent (type1, type2)
11652 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
11655 /* Compare argument types. */
11656 for (type1 = get_arg_type_list (proto1, METHOD_REF, 0),
11657 type2 = get_arg_type_list (proto2, METHOD_REF, 0);
11659 type1 = TREE_CHAIN (type1), type2 = TREE_CHAIN (type2))
11661 if (!objc_types_are_equivalent (TREE_VALUE (type1), TREE_VALUE (type2))
11663 || !objc_types_share_size_and_alignment (TREE_VALUE (type1),
11664 TREE_VALUE (type2))))
11668 return (!type1 && !type2);
11671 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
11672 this occurs. ObjC method dispatches are _not_ like C++ virtual
11673 member function dispatches, and we account for the difference here. */
11676 objc_fold_obj_type_ref (tree ref, tree known_type)
11678 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED,
11679 tree known_type ATTRIBUTE_UNUSED)
11683 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
11685 /* If the receiver does not have virtual member functions, there
11686 is nothing we can (or need to) do here. */
11690 /* Let C++ handle C++ virtual functions. */
11691 return cp_fold_obj_type_ref (ref, known_type);
11693 /* For plain ObjC, we currently do not need to do anything. */
11699 objc_start_function (tree name, tree type, tree attrs,
11703 struct c_arg_info *params
11707 tree fndecl = build_decl (input_location,
11708 FUNCTION_DECL, name, type);
11711 DECL_ARGUMENTS (fndecl) = params;
11712 DECL_INITIAL (fndecl) = error_mark_node;
11713 DECL_EXTERNAL (fndecl) = 0;
11714 TREE_STATIC (fndecl) = 1;
11715 retrofit_lang_decl (fndecl);
11716 cplus_decl_attributes (&fndecl, attrs, 0);
11717 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
11719 current_function_returns_value = 0; /* Assume, until we see it does. */
11720 current_function_returns_null = 0;
11721 decl_attributes (&fndecl, attrs, 0);
11722 announce_function (fndecl);
11723 DECL_INITIAL (fndecl) = error_mark_node;
11724 DECL_EXTERNAL (fndecl) = 0;
11725 TREE_STATIC (fndecl) = 1;
11726 current_function_decl = pushdecl (fndecl);
11728 declare_parm_level ();
11729 DECL_RESULT (current_function_decl)
11730 = build_decl (input_location,
11731 RESULT_DECL, NULL_TREE,
11732 TREE_TYPE (TREE_TYPE (current_function_decl)));
11733 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
11734 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
11735 start_fname_decls ();
11736 store_parm_decls_from (params);
11739 TREE_USED (current_function_decl) = 1;
11742 /* - Generate an identifier for the function. the format is "_n_cls",
11743 where 1 <= n <= nMethods, and cls is the name the implementation we
11745 - Install the return type from the method declaration.
11746 - If we have a prototype, check for type consistency. */
11749 really_start_method (tree method,
11753 struct c_arg_info *parmlist
11757 tree ret_type, meth_type;
11759 const char *sel_name, *class_name, *cat_name;
11762 /* Synth the storage class & assemble the return type. */
11763 ret_type = TREE_VALUE (TREE_TYPE (method));
11765 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
11766 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
11767 cat_name = ((TREE_CODE (objc_implementation_context)
11768 == CLASS_IMPLEMENTATION_TYPE)
11770 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
11773 /* Make sure this is big enough for any plausible method label. */
11774 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
11775 + (cat_name ? strlen (cat_name) : 0));
11777 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
11778 class_name, cat_name, sel_name, method_slot);
11780 method_id = get_identifier (buf);
11783 /* Objective-C methods cannot be overloaded, so we don't need
11784 the type encoding appended. It looks bad anyway... */
11785 push_lang_context (lang_name_c);
11789 = build_function_type (ret_type,
11790 get_arg_type_list (method, METHOD_DEF, 0));
11791 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
11793 /* Set self_decl from the first argument. */
11794 self_decl = DECL_ARGUMENTS (current_function_decl);
11796 /* Suppress unused warnings. */
11797 TREE_USED (self_decl) = 1;
11798 DECL_READ_P (self_decl) = 1;
11799 TREE_USED (DECL_CHAIN (self_decl)) = 1;
11800 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
11802 pop_lang_context ();
11805 METHOD_DEFINITION (method) = current_function_decl;
11807 /* Check consistency...start_function, pushdecl, duplicate_decls. */
11809 if (implementation_template != objc_implementation_context)
11812 = lookup_method_static (implementation_template,
11813 METHOD_SEL_NAME (method),
11814 ((TREE_CODE (method) == CLASS_METHOD_DECL)
11815 | OBJC_LOOKUP_NO_SUPER));
11819 if (!comp_proto_with_proto (method, proto, 1))
11821 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
11823 warning_at (DECL_SOURCE_LOCATION (method), 0,
11824 "conflicting types for %<%c%s%>",
11825 (type ? '-' : '+'),
11826 identifier_to_locale (gen_method_decl (method)));
11827 inform (DECL_SOURCE_LOCATION (proto),
11828 "previous declaration of %<%c%s%>",
11829 (type ? '-' : '+'),
11830 identifier_to_locale (gen_method_decl (proto)));
11834 /* If the method in the @interface was deprecated, mark
11835 the implemented method as deprecated too. It should
11836 never be used for messaging (when the deprecation
11837 warnings are produced), but just in case. */
11838 if (TREE_DEPRECATED (proto))
11839 TREE_DEPRECATED (method) = 1;
11841 /* If the method in the @interface was marked as
11842 'noreturn', mark the function implementing the method
11843 as 'noreturn' too. */
11844 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
11849 /* We have a method @implementation even though we did not
11850 see a corresponding @interface declaration (which is allowed
11851 by Objective-C rules). Go ahead and place the method in
11852 the @interface anyway, so that message dispatch lookups
11854 tree interface = implementation_template;
11856 if (TREE_CODE (objc_implementation_context)
11857 == CATEGORY_IMPLEMENTATION_TYPE)
11858 interface = lookup_category
11860 CLASS_SUPER_NAME (objc_implementation_context));
11863 objc_add_method (interface, copy_node (method),
11864 TREE_CODE (method) == CLASS_METHOD_DECL,
11865 /* is_optional= */ false);
11870 static void *UOBJC_SUPER_scope = 0;
11872 /* _n_Method (id self, SEL sel, ...)
11874 struct objc_super _S;
11875 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
11879 get_super_receiver (void)
11881 if (objc_method_context)
11883 tree super_expr, super_expr_list;
11885 if (!UOBJC_SUPER_decl)
11887 UOBJC_SUPER_decl = build_decl (input_location,
11888 VAR_DECL, get_identifier (TAG_SUPER),
11889 objc_super_template);
11890 /* This prevents `unused variable' warnings when compiling with -Wall. */
11891 TREE_USED (UOBJC_SUPER_decl) = 1;
11892 DECL_READ_P (UOBJC_SUPER_decl) = 1;
11893 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
11894 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
11896 UOBJC_SUPER_scope = objc_get_current_scope ();
11899 /* Set receiver to self. */
11900 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
11901 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
11902 NOP_EXPR, input_location, self_decl,
11904 super_expr_list = super_expr;
11906 /* Set class to begin searching. */
11907 super_expr = objc_build_component_ref (UOBJC_SUPER_decl,
11908 get_identifier ("super_class"));
11910 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
11912 /* [_cls, __cls]Super are "pre-built" in
11913 synth_forward_declarations. */
11915 super_expr = build_modify_expr (input_location, super_expr,
11916 NULL_TREE, NOP_EXPR,
11918 ((TREE_CODE (objc_method_context)
11919 == INSTANCE_METHOD_DECL)
11921 : uucls_super_ref),
11926 /* We have a category. */
11928 tree super_name = CLASS_SUPER_NAME (implementation_template);
11931 /* Barf if super used in a category of Object. */
11934 error ("no super class declared in interface for %qE",
11935 CLASS_NAME (implementation_template));
11936 return error_mark_node;
11939 if (flag_next_runtime && !flag_zero_link)
11941 super_class = objc_get_class_reference (super_name);
11942 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
11943 /* If we are in a class method, we must retrieve the
11944 _metaclass_ for the current class, pointed at by
11945 the class's "isa" pointer. The following assumes that
11946 "isa" is the first ivar in a class (which it must be). */
11948 = build_indirect_ref
11950 build_c_cast (input_location,
11951 build_pointer_type (objc_class_type),
11952 super_class), RO_UNARY_STAR);
11956 add_class_reference (super_name);
11957 super_class = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
11958 ? objc_get_class_decl : objc_get_meta_class_decl);
11959 assemble_external (super_class);
11961 = build_function_call
11966 my_build_string_pointer
11967 (IDENTIFIER_LENGTH (super_name) + 1,
11968 IDENTIFIER_POINTER (super_name))));
11972 = build_modify_expr (input_location, super_expr, NULL_TREE,
11975 build_c_cast (input_location,
11976 TREE_TYPE (super_expr),
11981 super_expr_list = build_compound_expr (input_location,
11982 super_expr_list, super_expr);
11984 super_expr = build_unary_op (input_location,
11985 ADDR_EXPR, UOBJC_SUPER_decl, 0);
11986 super_expr_list = build_compound_expr (input_location,
11987 super_expr_list, super_expr);
11989 return super_expr_list;
11993 error ("[super ...] must appear in a method context");
11994 return error_mark_node;
11998 /* When exiting a scope, sever links to a 'super' declaration (if any)
11999 therein contained. */
12002 objc_clear_super_receiver (void)
12004 if (objc_method_context
12005 && UOBJC_SUPER_scope == objc_get_current_scope ()) {
12006 UOBJC_SUPER_decl = 0;
12007 UOBJC_SUPER_scope = 0;
12012 objc_finish_method_definition (tree fndecl)
12014 /* We cannot validly inline ObjC methods, at least not without a language
12015 extension to declare that a method need not be dynamically
12016 dispatched, so suppress all thoughts of doing so. */
12017 DECL_UNINLINABLE (fndecl) = 1;
12020 /* The C++ front-end will have called finish_function() for us. */
12021 finish_function ();
12024 METHOD_ENCODING (objc_method_context)
12025 = encode_method_prototype (objc_method_context);
12027 /* Required to implement _msgSuper. This must be done AFTER finish_function,
12028 since the optimizer may find "may be used before set" errors. */
12029 objc_method_context = NULL_TREE;
12031 if (should_call_super_dealloc)
12032 warning (0, "method possibly missing a [super dealloc] call");
12035 /* Given a tree DECL node, produce a printable description of it in the given
12036 buffer, overwriting the buffer. */
12039 gen_declaration (tree decl)
12045 gen_type_name_0 (TREE_TYPE (decl));
12047 if (DECL_NAME (decl))
12049 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
12050 strcat (errbuf, " ");
12052 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
12055 if (DECL_INITIAL (decl)
12056 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
12057 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
12058 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
12064 /* Given a tree TYPE node, produce a printable description of it in the given
12065 buffer, overwriting the buffer. */
12068 gen_type_name_0 (tree type)
12070 tree orig = type, proto;
12072 if (TYPE_P (type) && TYPE_NAME (type))
12073 type = TYPE_NAME (type);
12074 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
12076 tree inner = TREE_TYPE (type);
12078 while (TREE_CODE (inner) == ARRAY_TYPE)
12079 inner = TREE_TYPE (inner);
12081 gen_type_name_0 (inner);
12083 if (!POINTER_TYPE_P (inner))
12084 strcat (errbuf, " ");
12086 if (POINTER_TYPE_P (type))
12087 strcat (errbuf, "*");
12089 while (type != inner)
12091 strcat (errbuf, "[");
12093 if (TYPE_DOMAIN (type))
12097 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
12099 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
12100 strcat (errbuf, sz);
12103 strcat (errbuf, "]");
12104 type = TREE_TYPE (type);
12107 goto exit_function;
12110 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
12111 type = DECL_NAME (type);
12113 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
12114 ? IDENTIFIER_POINTER (type)
12117 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
12118 if (objc_is_id (orig))
12119 orig = TREE_TYPE (orig);
12121 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
12125 strcat (errbuf, " <");
12129 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
12130 proto = TREE_CHAIN (proto);
12131 strcat (errbuf, proto ? ", " : ">");
12140 gen_type_name (tree type)
12144 return gen_type_name_0 (type);
12147 /* Given a method tree, put a printable description into the given
12148 buffer (overwriting) and return a pointer to the buffer. */
12151 gen_method_decl (tree method)
12155 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
12156 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
12157 strcat (errbuf, ")");
12158 chain = METHOD_SEL_ARGS (method);
12162 /* We have a chain of keyword_decls. */
12165 if (KEYWORD_KEY_NAME (chain))
12166 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
12168 strcat (errbuf, ":(");
12169 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
12170 strcat (errbuf, ")");
12172 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
12173 if ((chain = DECL_CHAIN (chain)))
12174 strcat (errbuf, " ");
12178 if (METHOD_ADD_ARGS (method))
12180 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
12182 /* Know we have a chain of parm_decls. */
12185 strcat (errbuf, ", ");
12186 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
12187 chain = TREE_CHAIN (chain);
12190 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
12191 strcat (errbuf, ", ...");
12196 /* We have a unary selector. */
12197 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
12205 /* Dump an @interface declaration of the supplied class CHAIN to the
12206 supplied file FP. Used to implement the -gen-decls option (which
12207 prints out an @interface declaration of all classes compiled in
12208 this run); potentially useful for debugging the compiler too. */
12210 dump_interface (FILE *fp, tree chain)
12212 /* FIXME: A heap overflow here whenever a method (or ivar)
12213 declaration is so long that it doesn't fit in the buffer. The
12214 code and all the related functions should be rewritten to avoid
12215 using fixed size buffers. */
12216 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
12217 tree ivar_decls = CLASS_RAW_IVARS (chain);
12218 tree nst_methods = CLASS_NST_METHODS (chain);
12219 tree cls_methods = CLASS_CLS_METHODS (chain);
12221 fprintf (fp, "\n@interface %s", my_name);
12223 /* CLASS_SUPER_NAME is used to store the superclass name for
12224 classes, and the category name for categories. */
12225 if (CLASS_SUPER_NAME (chain))
12227 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
12229 switch (TREE_CODE (chain))
12231 case CATEGORY_IMPLEMENTATION_TYPE:
12232 case CATEGORY_INTERFACE_TYPE:
12233 fprintf (fp, " (%s)\n", name);
12236 fprintf (fp, " : %s\n", name);
12241 fprintf (fp, "\n");
12243 /* FIXME - the following doesn't seem to work at the moment. */
12246 fprintf (fp, "{\n");
12249 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
12250 ivar_decls = TREE_CHAIN (ivar_decls);
12252 while (ivar_decls);
12253 fprintf (fp, "}\n");
12256 while (nst_methods)
12258 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
12259 nst_methods = TREE_CHAIN (nst_methods);
12262 while (cls_methods)
12264 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
12265 cls_methods = TREE_CHAIN (cls_methods);
12268 fprintf (fp, "@end\n");
12272 /* Produce the pretty printing for an Objective-C method. This is
12273 currently unused, but could be handy while reorganizing the pretty
12274 printing to be more robust. */
12275 static const char *
12276 objc_pretty_print_method (bool is_class_method,
12277 const char *class_name,
12278 const char *category_name,
12279 const char *selector)
12283 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
12284 + strlen (selector) + 7);
12286 if (is_class_method)
12287 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
12289 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
12295 char *result = XNEWVEC (char, strlen (class_name)
12296 + strlen (selector) + 5);
12298 if (is_class_method)
12299 sprintf (result, "+[%s %s]", class_name, selector);
12301 sprintf (result, "-[%s %s]", class_name, selector);
12308 /* Demangle function for Objective-C. Attempt to demangle the
12309 function name associated with a method (eg, going from
12310 "_i_NSObject__class" to "-[NSObject class]"); usually for the
12311 purpose of pretty printing or error messages. Return the demangled
12312 name, or NULL if the string is not an Objective-C mangled method
12315 Because of how the mangling is done, any method that has a '_' in
12316 its original name is at risk of being demangled incorrectly. In
12317 some cases there are multiple valid ways to demangle a method name
12318 and there is no way we can decide.
12320 TODO: objc_demangle() can't always get it right; the right way to
12321 get this correct for all method names would be to store the
12322 Objective-C method name somewhere in the function decl. Then,
12323 there is no demangling to do; we'd just pull the method name out of
12324 the decl. As an additional bonus, when printing error messages we
12325 could check for such a method name, and if we find it, we know the
12326 function is actually an Objective-C method and we could print error
12327 messages saying "In method '+[NSObject class]" instead of "In
12328 function '+[NSObject class]" as we do now. */
12329 static const char *
12330 objc_demangle (const char *mangled)
12332 char *demangled, *cp;
12334 if (mangled[0] == '_' &&
12335 (mangled[1] == 'i' || mangled[1] == 'c') &&
12338 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
12339 if (mangled[1] == 'i')
12340 *cp++ = '-'; /* for instance method */
12342 *cp++ = '+'; /* for class method */
12343 *cp++ = '['; /* opening left brace */
12344 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
12345 while (*cp && *cp == '_')
12346 cp++; /* skip any initial underbars in class name */
12347 cp = strchr(cp, '_'); /* find first non-initial underbar */
12350 free(demangled); /* not mangled name */
12353 if (cp[1] == '_') /* easy case: no category name */
12355 *cp++ = ' '; /* replace two '_' with one ' ' */
12356 strcpy(cp, mangled + (cp - demangled) + 2);
12360 *cp++ = '('; /* less easy case: category name */
12361 cp = strchr(cp, '_');
12364 free(demangled); /* not mangled name */
12368 *cp++ = ' '; /* overwriting 1st char of method name... */
12369 strcpy(cp, mangled + (cp - demangled)); /* get it back */
12371 /* Now we have the method name. We need to generally replace
12372 '_' with ':' but trying to preserve '_' if it could only have
12373 been in the mangled string because it was already in the
12374 original name. In cases where it's ambiguous, we assume that
12375 any '_' originated from a ':'. */
12377 /* Initial '_'s in method name can't have been generating by
12378 converting ':'s. Skip them. */
12379 while (*cp && *cp == '_')
12382 /* If the method name does not end with '_', then it has no
12383 arguments and there was no replacement of ':'s with '_'s
12384 during mangling. Check for that case, and skip any
12385 replacement if so. This at least guarantees that methods
12386 with no arguments are always demangled correctly (unless the
12387 original name ends with '_'). */
12388 if (*(mangled + strlen (mangled) - 1) != '_')
12390 /* Skip to the end. */
12396 /* Replace remaining '_' with ':'. This may get it wrong if
12397 there were '_'s in the original name. In most cases it
12398 is impossible to disambiguate. */
12403 *cp++ = ']'; /* closing right brace */
12404 *cp++ = 0; /* string terminator */
12408 return NULL; /* not an objc mangled name */
12411 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
12412 specific decl, return the printable name for it. If not, return
12415 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
12417 switch (TREE_CODE (decl))
12419 case FUNCTION_DECL:
12420 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
12423 /* The following happens when we are printing a deprecation
12424 warning for a method. The warn_deprecation() will end up
12425 trying to print the decl for INSTANCE_METHOD_DECL or
12426 CLASS_METHOD_DECL. It would be nice to be able to print
12427 "-[NSObject autorelease] is deprecated", but to do that, we'd
12428 need to store the class and method name in the method decl,
12429 which we currently don't do. For now, just return the name
12430 of the method. We don't return NULL, because that may
12431 trigger further attempts to pretty-print the decl in C/C++,
12432 but they wouldn't know how to pretty-print it. */
12433 case INSTANCE_METHOD_DECL:
12434 case CLASS_METHOD_DECL:
12435 return IDENTIFIER_POINTER (DECL_NAME (decl));
12437 /* This happens when printing a deprecation warning for a
12438 property. We may want to consider some sort of pretty
12439 printing (eg, include the class name where it was declared
12441 case PROPERTY_DECL:
12442 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
12450 /* Return a printable name for 'decl'. This first tries
12451 objc_maybe_printable_name(), and if that fails, it returns the name
12452 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
12453 Objective-C; in Objective-C++, setting the hook is not enough
12454 because lots of C++ Front-End code calls cxx_printable_name,
12455 dump_decl and other C++ functions directly. So instead we have
12456 modified dump_decl to call objc_maybe_printable_name directly. */
12458 objc_printable_name (tree decl, int v)
12460 const char *demangled_name = objc_maybe_printable_name (decl, v);
12462 if (demangled_name != NULL)
12463 return demangled_name;
12465 return IDENTIFIER_POINTER (DECL_NAME (decl));
12471 gcc_obstack_init (&util_obstack);
12472 util_firstobj = (char *) obstack_finish (&util_obstack);
12474 errbuf = XNEWVEC (char, 1024 * 10);
12476 synth_module_prologue ();
12482 struct imp_entry *impent;
12484 /* The internally generated initializers appear to have missing braces.
12485 Don't warn about this. */
12486 int save_warn_missing_braces = warn_missing_braces;
12487 warn_missing_braces = 0;
12489 /* A missing @end may not be detected by the parser. */
12490 if (objc_implementation_context)
12492 warning (0, "%<@end%> missing in implementation context");
12493 finish_class (objc_implementation_context);
12494 objc_ivar_chain = NULL_TREE;
12495 objc_implementation_context = NULL_TREE;
12498 /* Process the static instances here because initialization of objc_symtab
12499 depends on them. */
12500 if (objc_static_instances)
12501 generate_static_references ();
12503 /* forward declare categories */
12505 forward_declare_categories ();
12507 for (impent = imp_list; impent; impent = impent->next)
12509 objc_implementation_context = impent->imp_context;
12510 implementation_template = impent->imp_template;
12512 /* FIXME: This needs reworking to be more obvious. */
12514 UOBJC_CLASS_decl = impent->class_decl;
12515 UOBJC_METACLASS_decl = impent->meta_decl;
12517 /* Dump the @interface of each class as we compile it, if the
12518 -gen-decls option is in use. TODO: Dump the classes in the
12519 order they were found, rather than in reverse order as we
12521 if (flag_gen_declaration)
12523 dump_interface (gen_declaration_file, objc_implementation_context);
12526 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
12528 /* all of the following reference the string pool... */
12529 generate_ivar_lists ();
12530 generate_dispatch_tables ();
12531 generate_shared_structures (impent);
12535 generate_dispatch_tables ();
12536 generate_category (impent);
12539 impent->class_decl = UOBJC_CLASS_decl;
12540 impent->meta_decl = UOBJC_METACLASS_decl;
12543 /* If we are using an array of selectors, we must always
12544 finish up the array decl even if no selectors were used. */
12545 if (flag_next_runtime)
12546 build_next_selector_translation_table ();
12548 build_gnu_selector_translation_table ();
12550 if (protocol_chain)
12551 generate_protocols ();
12553 if (flag_next_runtime)
12554 generate_objc_image_info ();
12556 if (imp_list || class_names_chain
12557 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
12558 generate_objc_symtab_decl ();
12560 /* Arrange for ObjC data structures to be initialized at run time. */
12561 if (objc_implementation_context || class_names_chain || objc_static_instances
12562 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
12564 build_module_descriptor ();
12566 if (!flag_next_runtime)
12567 build_module_initializer_routine ();
12570 /* Dump the class references. This forces the appropriate classes
12571 to be linked into the executable image, preserving unix archive
12572 semantics. This can be removed when we move to a more dynamically
12573 linked environment. */
12575 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
12577 handle_class_ref (chain);
12578 if (TREE_PURPOSE (chain))
12579 generate_classref_translation_entry (chain);
12582 for (impent = imp_list; impent; impent = impent->next)
12583 handle_impent (impent);
12590 /* Run through the selector hash tables and print a warning for any
12591 selector which has multiple methods. */
12593 for (slot = 0; slot < SIZEHASHTABLE; slot++)
12595 for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
12596 check_duplicates (hsh, 0, 1);
12597 for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
12598 check_duplicates (hsh, 0, 1);
12602 warn_missing_braces = save_warn_missing_braces;
12605 /* Subroutines of finish_objc. */
12608 generate_classref_translation_entry (tree chain)
12610 tree expr, decl, type;
12612 decl = TREE_PURPOSE (chain);
12613 type = TREE_TYPE (decl);
12615 expr = add_objc_string (TREE_VALUE (chain), class_names);
12616 expr = convert (type, expr); /* cast! */
12618 /* This is a class reference. It is re-written by the runtime,
12619 but will be optimized away unless we force it. */
12620 DECL_PRESERVE_P (decl) = 1;
12621 finish_var_decl (decl, expr);
12626 handle_class_ref (tree chain)
12628 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
12629 char *string = (char *) alloca (strlen (name) + 30);
12633 sprintf (string, "%sobjc_class_name_%s",
12634 (flag_next_runtime ? "." : "__"), name);
12636 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
12637 if (flag_next_runtime)
12639 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file, string);
12644 /* Make a decl for this name, so we can use its address in a tree. */
12645 decl = build_decl (input_location,
12646 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
12647 DECL_EXTERNAL (decl) = 1;
12648 TREE_PUBLIC (decl) = 1;
12650 finish_var_decl (decl, 0);
12652 /* Make a decl for the address. */
12653 sprintf (string, "%sobjc_class_ref_%s",
12654 (flag_next_runtime ? "." : "__"), name);
12655 exp = build1 (ADDR_EXPR, string_type_node, decl);
12656 decl = build_decl (input_location,
12657 VAR_DECL, get_identifier (string), string_type_node);
12658 TREE_STATIC (decl) = 1;
12659 TREE_USED (decl) = 1;
12660 DECL_READ_P (decl) = 1;
12661 DECL_ARTIFICIAL (decl) = 1;
12662 DECL_INITIAL (decl) = error_mark_node;
12664 /* We must force the reference. */
12665 DECL_PRESERVE_P (decl) = 1;
12668 finish_var_decl (decl, exp);
12672 handle_impent (struct imp_entry *impent)
12676 objc_implementation_context = impent->imp_context;
12677 implementation_template = impent->imp_template;
12679 switch (TREE_CODE (impent->imp_context))
12681 case CLASS_IMPLEMENTATION_TYPE:
12683 const char *const class_name =
12684 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
12686 string = (char *) alloca (strlen (class_name) + 30);
12688 sprintf (string, "%sobjc_class_name_%s",
12689 (flag_next_runtime ? "." : "__"), class_name);
12692 case CATEGORY_IMPLEMENTATION_TYPE:
12694 const char *const class_name =
12695 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
12696 const char *const class_super_name =
12697 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
12699 string = (char *) alloca (strlen (class_name)
12700 + strlen (class_super_name) + 30);
12702 /* Do the same for categories. Even though no references to
12703 these symbols are generated automatically by the compiler,
12704 it gives you a handle to pull them into an archive by
12706 sprintf (string, "*%sobjc_category_name_%s_%s",
12707 (flag_next_runtime ? "." : "__"), class_name, class_super_name);
12714 #ifdef ASM_DECLARE_CLASS_REFERENCE
12715 if (flag_next_runtime)
12717 ASM_DECLARE_CLASS_REFERENCE (asm_out_file, string);
12725 init = integer_zero_node;
12726 decl = build_decl (input_location,
12727 VAR_DECL, get_identifier (string), TREE_TYPE (init));
12728 TREE_PUBLIC (decl) = 1;
12729 TREE_READONLY (decl) = 1;
12730 TREE_USED (decl) = 1;
12731 TREE_CONSTANT (decl) = 1;
12732 DECL_CONTEXT (decl) = NULL_TREE;
12733 DECL_ARTIFICIAL (decl) = 1;
12734 TREE_STATIC (decl) = 1;
12735 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
12736 /* We must force the reference. */
12737 DECL_PRESERVE_P (decl) = 1;
12739 finish_var_decl(decl, init) ;
12743 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
12744 later requires that ObjC translation units participating in F&C be
12745 specially marked. The following routine accomplishes this. */
12747 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
12750 generate_objc_image_info (void)
12754 = ((flag_replace_objc_classes && imp_count ? 1 : 0)
12755 | (flag_objc_gc ? 2 : 0));
12756 VEC(constructor_elt,gc) *v = NULL;
12760 return; /* No need for an image_info entry. */
12762 array_type = build_sized_array_type (integer_type_node, 2);
12764 decl = start_var_decl (array_type, "_OBJC_IMAGE_INFO");
12766 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
12767 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, flags));
12768 /* If we need this (determined above) it is because the runtime wants to
12769 refer to it in a manner hidden from the compiler. So we must force the
12771 DECL_PRESERVE_P (decl) = 1;
12772 finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v));
12775 /* Routine is called to issue diagnostic when reference to a private
12776 ivar is made and no other variable with same name is found in
12779 objc_diagnose_private_ivar (tree id)
12782 if (!objc_method_context)
12784 ivar = is_ivar (objc_ivar_chain, id);
12785 if (ivar && is_private (ivar))
12787 error ("instance variable %qs is declared private",
12788 IDENTIFIER_POINTER (id));
12794 /* Look up ID as an instance variable. OTHER contains the result of
12795 the C or C++ lookup, which we may want to use instead. */
12796 /* To use properties inside an instance method, use self.property. */
12798 objc_lookup_ivar (tree other, tree id)
12802 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
12803 if (!objc_method_context)
12806 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
12807 /* We have a message to super. */
12808 return get_super_receiver ();
12810 /* In a class method, look up an instance variable only as a last
12812 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
12813 && other && other != error_mark_node)
12816 /* Look up the ivar, but do not use it if it is not accessible. */
12817 ivar = is_ivar (objc_ivar_chain, id);
12819 if (!ivar || is_private (ivar))
12822 /* In an instance method, a local variable (or parameter) may hide the
12823 instance variable. */
12824 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
12825 && other && other != error_mark_node
12827 && CP_DECL_CONTEXT (other) != global_namespace)
12829 && !DECL_FILE_SCOPE_P (other))
12832 warning (0, "local declaration of %qE hides instance variable", id);
12837 /* At this point, we are either in an instance method with no obscuring
12838 local definitions, or in a class method with no alternate definitions
12840 return build_ivar_reference (id);
12843 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
12844 needs to be done if we are calling a function through a cast. */
12847 objc_rewrite_function_call (tree function, tree first_param)
12849 if (TREE_CODE (function) == NOP_EXPR
12850 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
12851 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
12854 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
12855 TREE_OPERAND (function, 0),
12856 first_param, size_zero_node);
12862 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
12863 corresponding 'getter' function call. Note that we assume the
12864 PROPERTY_REF to be valid since we generated it while parsing. */
12866 objc_gimplify_property_ref (tree *expr_p)
12868 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
12871 if (getter == NULL_TREE)
12873 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
12874 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
12875 should be impossible for real properties, which always
12877 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
12878 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
12879 /* Try to recover from the error to prevent an ICE. We take
12880 zero and cast it to the type of the property. */
12881 *expr_p = convert (TREE_TYPE (property_decl),
12882 integer_zero_node);
12888 /* In C++, a getter which returns an aggregate value results in a
12889 target_expr which initializes a temporary to the call
12891 if (TREE_CODE (getter) == TARGET_EXPR)
12893 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
12894 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
12895 call_exp = TREE_OPERAND (getter, 1);
12898 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
12900 *expr_p = call_exp;
12903 /* This is called when "gimplifying" the trees. We need to gimplify
12904 the Objective-C/Objective-C++ specific trees, then hand over the
12905 process to C/C++. */
12907 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
12909 enum tree_code code = TREE_CODE (*expr_p);
12912 /* Look for the special case of OBJC_TYPE_REF with the address
12913 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
12914 or one of its cousins). */
12916 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
12917 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
12920 enum gimplify_status r0, r1;
12922 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
12923 value of the OBJ_TYPE_REF, so force them to be emitted
12924 during subexpression evaluation rather than after the
12925 OBJ_TYPE_REF. This permits objc_msgSend calls in
12926 Objective C to use direct rather than indirect calls when
12927 the object expression has a postincrement. */
12928 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
12929 is_gimple_val, fb_rvalue);
12930 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
12931 is_gimple_val, fb_rvalue);
12933 return MIN (r0, r1);
12937 objc_gimplify_property_ref (expr_p);
12938 /* Do not return yet; let C/C++ gimplify the resulting expression. */
12945 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
12947 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
12951 /* This routine returns true if TYPE is a valid objc object type,
12952 suitable for messaging; false otherwise. If 'accept_class' is
12953 'true', then a Class object is considered valid for messaging and
12954 'true' is returned if 'type' refers to a Class. If 'accept_class'
12955 is 'false', then a Class object is not considered valid for
12956 messaging and 'false' is returned in that case. */
12959 objc_type_valid_for_messaging (tree type, bool accept_classes)
12961 if (!POINTER_TYPE_P (type))
12964 /* Remove the pointer indirection; don't remove more than one
12965 otherwise we'd consider "NSObject **" a valid type for messaging,
12967 type = TREE_TYPE (type);
12969 if (TREE_CODE (type) != RECORD_TYPE)
12972 if (objc_is_object_id (type))
12975 if (objc_is_class_id (type))
12976 return accept_classes;
12978 if (TYPE_HAS_OBJC_INFO (type))
12984 /* Begin code generation for fast enumeration (foreach) ... */
12988 struct __objcFastEnumerationState
12990 unsigned long state;
12992 unsigned long *mutationsPtr;
12993 unsigned long extra[5];
12996 Confusingly enough, NSFastEnumeration is then defined by libraries
12997 to be the same structure.
13001 build_fast_enumeration_state_template (void)
13003 tree decls, *chain = NULL;
13006 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
13007 (TAG_FAST_ENUMERATION_STATE));
13009 /* unsigned long state; */
13010 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
13012 /* id *itemsPtr; */
13013 add_field_decl (build_pointer_type (objc_object_type),
13014 "itemsPtr", &chain);
13016 /* unsigned long *mutationsPtr; */
13017 add_field_decl (build_pointer_type (long_unsigned_type_node),
13018 "mutationsPtr", &chain);
13020 /* unsigned long extra[5]; */
13021 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
13025 objc_finish_struct (objc_fast_enumeration_state_template, decls);
13029 'objc_finish_foreach_loop()' generates the code for an Objective-C
13030 foreach loop. The 'location' argument is the location of the 'for'
13031 that starts the loop. The 'object_expression' is the expression of
13032 the 'object' that iterates; the 'collection_expression' is the
13033 expression of the collection that we iterate over (we need to make
13034 sure we evaluate this only once); the 'for_body' is the set of
13035 statements to be executed in each iteration; 'break_label' and
13036 'continue_label' are the break and continue labels which we need to
13037 emit since the <statements> may be jumping to 'break_label' (if they
13038 contain 'break') or to 'continue_label' (if they contain
13043 for (<object expression> in <collection expression>)
13046 which is compiled into the following blurb:
13049 id __objc_foreach_collection;
13050 __objc_fast_enumeration_state __objc_foreach_enum_state;
13051 unsigned long __objc_foreach_batchsize;
13052 id __objc_foreach_items[16];
13053 __objc_foreach_collection = <collection expression>;
13054 __objc_foreach_enum_state = { 0 };
13055 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
13057 if (__objc_foreach_batchsize == 0)
13058 <object expression> = nil;
13061 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
13064 unsigned long __objc_foreach_index;
13065 __objc_foreach_index = 0;
13068 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
13069 <object expression> = enumState.itemsPtr[__objc_foreach_index];
13070 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
13073 __objc_foreach_index++;
13074 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
13075 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
13077 if (__objc_foreach_batchsize != 0) goto next_batch;
13078 <object expression> = nil;
13083 'statements' may contain a 'continue' or 'break' instruction, which
13084 the user expects to 'continue' or 'break' the entire foreach loop.
13085 We are provided the labels that 'break' and 'continue' jump to, so
13086 we place them where we want them to jump to when they pick them.
13088 Optimization TODO: we could cache the IMP of
13089 countByEnumeratingWithState:objects:count:.
13092 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
13093 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
13095 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
13096 #include "tree-pretty-print.h"
13100 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
13101 tree break_label, tree continue_label)
13103 /* A tree representing the __objcFastEnumerationState struct type,
13104 or NSFastEnumerationState struct, whatever we are using. */
13105 tree objc_fast_enumeration_state_type;
13107 /* The trees representing the declarations of each of the local variables. */
13108 tree objc_foreach_collection_decl;
13109 tree objc_foreach_enum_state_decl;
13110 tree objc_foreach_items_decl;
13111 tree objc_foreach_batchsize_decl;
13112 tree objc_foreach_mutations_pointer_decl;
13113 tree objc_foreach_index_decl;
13115 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
13116 tree selector_name;
13118 /* A tree representing the local bind. */
13121 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
13124 /* A tree representing the 'else' part of 'first_if' */
13127 /* A tree representing the 'next_batch' label. */
13128 tree next_batch_label_decl;
13130 /* A tree representing the binding after the 'next_batch' label. */
13131 tree next_batch_bind;
13133 /* A tree representing the 'next_object' label. */
13134 tree next_object_label_decl;
13136 /* Temporary variables. */
13140 if (flag_objc1_only)
13141 error_at (location, "fast enumeration is not available in Objective-C 1.0");
13143 if (object_expression == error_mark_node)
13146 if (collection_expression == error_mark_node)
13149 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
13151 error ("iterating variable in fast enumeration is not an object");
13155 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
13157 error ("collection in fast enumeration is not an object");
13161 /* TODO: Check that object_expression is either a variable
13162 declaration, or an lvalue. */
13164 /* This kludge is an idea from apple. We use the
13165 __objcFastEnumerationState struct implicitly defined by the
13166 compiler, unless a NSFastEnumerationState struct has been defined
13167 (by a Foundation library such as GNUstep Base) in which case, we
13170 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
13172 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
13174 if (objc_NSFastEnumeration_type)
13176 /* TODO: We really need to check that
13177 objc_NSFastEnumeration_type is the same as ours! */
13178 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
13180 /* If it's a typedef, use the original type. */
13181 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
13182 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
13184 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
13190 /* Done by c-parser.c. */
13193 /* Done by c-parser.c. */
13195 /* id __objc_foreach_collection */
13196 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
13198 /* __objcFastEnumerationState __objc_foreach_enum_state; */
13199 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
13200 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
13202 /* id __objc_foreach_items[16]; */
13203 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
13204 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
13206 /* unsigned long __objc_foreach_batchsize; */
13207 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
13208 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
13210 /* Generate the local variable binding. */
13211 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
13212 SET_EXPR_LOCATION (bind, location);
13213 TREE_SIDE_EFFECTS (bind) = 1;
13215 /* __objc_foreach_collection = <collection expression>; */
13216 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
13217 SET_EXPR_LOCATION (t, location);
13218 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13220 /* __objc_foreach_enum_state.state = 0; */
13221 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
13222 get_identifier ("state")),
13223 build_int_cst (long_unsigned_type_node, 0));
13224 SET_EXPR_LOCATION (t, location);
13225 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13227 /* __objc_foreach_enum_state.itemsPtr = NULL; */
13228 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
13229 get_identifier ("itemsPtr")),
13230 null_pointer_node);
13231 SET_EXPR_LOCATION (t, location);
13232 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13234 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
13235 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
13236 get_identifier ("mutationsPtr")),
13237 null_pointer_node);
13238 SET_EXPR_LOCATION (t, location);
13239 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13241 /* __objc_foreach_enum_state.extra[0] = 0; */
13242 /* __objc_foreach_enum_state.extra[1] = 0; */
13243 /* __objc_foreach_enum_state.extra[2] = 0; */
13244 /* __objc_foreach_enum_state.extra[3] = 0; */
13245 /* __objc_foreach_enum_state.extra[4] = 0; */
13246 for (i = 0; i < 5 ; i++)
13248 t = build2 (MODIFY_EXPR, void_type_node,
13249 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
13250 get_identifier ("extra")),
13251 build_int_cst (NULL_TREE, i)),
13252 build_int_cst (long_unsigned_type_node, 0));
13253 SET_EXPR_LOCATION (t, location);
13254 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13257 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
13258 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
13260 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13262 tree_cons /* &__objc_foreach_enum_state */
13263 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13264 tree_cons /* __objc_foreach_items */
13265 (NULL_TREE, objc_foreach_items_decl,
13267 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13269 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
13271 struct c_expr array;
13272 array.value = objc_foreach_items_decl;
13273 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13275 tree_cons /* &__objc_foreach_enum_state */
13276 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13277 tree_cons /* __objc_foreach_items */
13278 (NULL_TREE, default_function_array_conversion (location, array).value,
13280 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13283 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
13284 convert (long_unsigned_type_node, t));
13285 SET_EXPR_LOCATION (t, location);
13286 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
13288 /* if (__objc_foreach_batchsize == 0) */
13289 first_if = build3 (COND_EXPR, void_type_node,
13292 (c_common_truthvalue_conversion
13294 build_binary_op (location,
13296 objc_foreach_batchsize_decl,
13297 build_int_cst (long_unsigned_type_node, 0), 1)),
13299 /* Then block (we fill it in later). */
13301 /* Else block (we fill it in later). */
13303 SET_EXPR_LOCATION (first_if, location);
13304 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
13306 /* then <object expression> = nil; */
13307 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
13308 SET_EXPR_LOCATION (t, location);
13309 COND_EXPR_THEN (first_if) = t;
13311 /* Now we build the 'else' part of the if; once we finish building
13312 it, we attach it to first_if as the 'else' part. */
13317 /* unsigned long __objc_foreach_mutations_pointer; */
13318 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
13320 /* Generate the local variable binding. */
13321 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
13322 SET_EXPR_LOCATION (first_else, location);
13323 TREE_SIDE_EFFECTS (first_else) = 1;
13325 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
13326 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
13327 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
13328 get_identifier ("mutationsPtr")),
13330 SET_EXPR_LOCATION (t, location);
13331 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13334 next_batch_label_decl = create_artificial_label (location);
13335 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
13336 SET_EXPR_LOCATION (t, location);
13337 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13341 /* unsigned long __objc_foreach_index; */
13342 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
13344 /* Generate the local variable binding. */
13345 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
13346 SET_EXPR_LOCATION (next_batch_bind, location);
13347 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
13348 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
13350 /* __objc_foreach_index = 0; */
13351 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
13352 build_int_cst (long_unsigned_type_node, 0));
13353 SET_EXPR_LOCATION (t, location);
13354 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13357 next_object_label_decl = create_artificial_label (location);
13358 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
13359 SET_EXPR_LOCATION (t, location);
13360 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13362 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
13363 t = build3 (COND_EXPR, void_type_node,
13366 (c_common_truthvalue_conversion
13371 objc_foreach_mutations_pointer_decl,
13372 build_indirect_ref (location,
13373 objc_build_component_ref (objc_foreach_enum_state_decl,
13374 get_identifier ("mutationsPtr")),
13375 RO_UNARY_STAR), 1)),
13378 build_function_call (input_location,
13379 objc_enumeration_mutation_decl,
13380 tree_cons (NULL, collection_expression, NULL)),
13383 SET_EXPR_LOCATION (t, location);
13384 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13386 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
13387 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
13388 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
13389 get_identifier ("itemsPtr")),
13390 objc_foreach_index_decl));
13391 SET_EXPR_LOCATION (t, location);
13392 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13394 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
13395 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
13397 /* continue_label: */
13398 if (continue_label)
13400 t = build1 (LABEL_EXPR, void_type_node, continue_label);
13401 SET_EXPR_LOCATION (t, location);
13402 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13405 /* __objc_foreach_index++; */
13406 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
13407 build_binary_op (location,
13409 objc_foreach_index_decl,
13410 build_int_cst (long_unsigned_type_node, 1), 1));
13411 SET_EXPR_LOCATION (t, location);
13412 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13414 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
13415 t = build3 (COND_EXPR, void_type_node,
13418 (c_common_truthvalue_conversion
13420 build_binary_op (location,
13422 objc_foreach_index_decl,
13423 objc_foreach_batchsize_decl, 1)),
13426 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
13429 SET_EXPR_LOCATION (t, location);
13430 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13432 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
13434 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13436 tree_cons /* &__objc_foreach_enum_state */
13437 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13438 tree_cons /* __objc_foreach_items */
13439 (NULL_TREE, objc_foreach_items_decl,
13441 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13443 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
13445 struct c_expr array;
13446 array.value = objc_foreach_items_decl;
13447 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
13449 tree_cons /* &__objc_foreach_enum_state */
13450 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
13451 tree_cons /* __objc_foreach_items */
13452 (NULL_TREE, default_function_array_conversion (location, array).value,
13454 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))));
13457 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
13458 convert (long_unsigned_type_node, t));
13459 SET_EXPR_LOCATION (t, location);
13460 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
13464 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
13465 t = build3 (COND_EXPR, void_type_node,
13468 (c_common_truthvalue_conversion
13470 build_binary_op (location,
13472 objc_foreach_batchsize_decl,
13473 build_int_cst (long_unsigned_type_node, 0), 1)),
13476 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
13479 SET_EXPR_LOCATION (t, location);
13480 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13482 /* <object expression> = nil; */
13483 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
13484 SET_EXPR_LOCATION (t, location);
13485 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13490 t = build1 (LABEL_EXPR, void_type_node, break_label);
13491 SET_EXPR_LOCATION (t, location);
13492 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
13496 COND_EXPR_ELSE (first_if) = first_else;
13498 /* Do the whole thing. */
13501 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
13502 /* This will print to stderr the whole blurb generated by the
13503 compiler while compiling (assuming the compiler doesn't crash
13504 before getting here).
13506 debug_generic_stmt (bind);
13510 /* Done by c-parser.c */
13513 /* Return true if we have an NxString object pointer. */
13516 objc_string_ref_type_p (tree strp)
13519 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
13522 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
13523 tmv = OBJC_TYPE_NAME (tmv);
13525 && TREE_CODE (tmv) == IDENTIFIER_NODE
13526 && IDENTIFIER_POINTER (tmv)
13527 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
13530 /* At present the behavior of this is undefined and it does nothing. */
13532 objc_check_format_arg (tree ARG_UNUSED (format_arg),
13533 tree ARG_UNUSED (args_list))
13537 #include "gt-objc-objc-act.h"