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-objc.h"
38 #include "c-family/c-pragma.h"
39 #include "c-family/c-format.h"
41 #include "langhooks.h"
50 #include "diagnostic-core.h"
53 #include "tree-iterator.h"
55 #include "langhooks-def.h"
57 /* For default_tree_printer (). */
58 #include "tree-pretty-print.h"
60 /* For enum gimplify_status */
63 #define OBJC_VOID_AT_END void_list_node
65 static unsigned int should_call_super_dealloc = 0;
67 /* When building Objective-C++, we are not linking against the C front-end
68 and so need to replicate the C tree-construction functions in some way. */
70 #define OBJCP_REMAP_FUNCTIONS
71 #include "objcp-decl.h"
74 /* This is the default way of generating a method name. */
75 /* This has the problem that "test_method:argument:" and
76 "test:method_argument:" will generate the same name
77 ("_i_Test__test_method_argument_" for an instance method of the
78 class "Test"), so you can't have them both in the same class!
79 Moreover, the demangling (going from
80 "_i_Test__test_method_argument" back to the original name) is
81 undefined because there are two correct ways of demangling the
83 #ifndef OBJC_GEN_METHOD_LABEL
84 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
87 sprintf ((BUF), "_%s_%s_%s_%s", \
88 ((IS_INST) ? "i" : "c"), \
90 ((CAT_NAME)? (CAT_NAME) : ""), \
92 for (temp = (BUF); *temp; temp++) \
93 if (*temp == ':') *temp = '_'; \
97 /* These need specifying. */
98 #ifndef OBJC_FORWARDING_STACK_OFFSET
99 #define OBJC_FORWARDING_STACK_OFFSET 0
102 #ifndef OBJC_FORWARDING_MIN_OFFSET
103 #define OBJC_FORWARDING_MIN_OFFSET 0
106 /* Set up for use of obstacks. */
110 /* This obstack is used to accumulate the encoding of a data type. */
111 static struct obstack util_obstack;
113 /* This points to the beginning of obstack contents, so we can free
114 the whole contents. */
117 /* The version identifies which language generation and runtime
118 the module (file) was compiled for, and is recorded in the
119 module descriptor. */
121 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
122 #define PROTOCOL_VERSION 2
124 /* (Decide if these can ever be validly changed.) */
125 #define OBJC_ENCODE_INLINE_DEFS 0
126 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
128 /*** Private Interface (procedures) ***/
130 /* Used by compile_file. */
132 static void init_objc (void);
133 static void finish_objc (void);
135 /* Code generation. */
137 static tree objc_build_constructor (tree, VEC(constructor_elt,gc) *);
138 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
139 static tree get_proto_encoding (tree);
140 static tree lookup_interface (tree);
141 static tree objc_add_static_instance (tree, tree);
143 static tree start_class (enum tree_code, tree, tree, tree, tree);
144 static tree continue_class (tree);
145 static void finish_class (tree);
146 static void start_method_def (tree);
148 static void objc_start_function (tree, tree, tree, tree);
150 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
152 static tree start_protocol (enum tree_code, tree, tree, tree);
153 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
154 static tree objc_add_method (tree, tree, int, bool);
155 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
156 static tree build_ivar_reference (tree);
157 static tree is_ivar (tree, tree);
159 static void build_objc_exception_stuff (void);
160 static void build_next_objc_exception_stuff (void);
162 /* We only need the following for ObjC; ObjC++ will use C++'s definition
163 of DERIVED_FROM_P. */
165 static bool objc_derived_from_p (tree, tree);
166 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
170 static void objc_gen_property_data (tree, tree);
171 static void objc_synthesize_getter (tree, tree, tree);
172 static void objc_synthesize_setter (tree, tree, tree);
173 static char *objc_build_property_setter_name (tree);
174 static int match_proto_with_proto (tree, tree, int);
175 static tree lookup_property (tree, tree);
176 static tree lookup_property_in_list (tree, tree);
177 static tree lookup_property_in_protocol_list (tree, tree);
178 static void build_objc_property_accessor_helpers (void);
180 static void objc_xref_basetypes (tree, tree);
182 static void build_class_template (void);
183 static void build_selector_template (void);
184 static void build_category_template (void);
185 static void build_super_template (void);
186 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
187 static tree get_class_ivars (tree, bool);
188 static tree generate_protocol_list (tree);
189 static void build_protocol_reference (tree);
191 static void build_fast_enumeration_state_template (void);
194 static void objc_generate_cxx_cdtors (void);
198 static void objc_decl_method_attributes (tree*, tree, int);
199 static tree build_keyword_selector (tree);
200 static const char *synth_id_with_class_suffix (const char *, tree);
202 /* Hash tables to manage the global pool of method prototypes. */
204 hash *nst_method_hash_list = 0;
205 hash *cls_method_hash_list = 0;
207 /* Hash tables to manage the global pool of class names. */
209 hash *cls_name_hash_list = 0;
210 hash *als_name_hash_list = 0;
212 static void hash_class_name_enter (hash *, tree, tree);
213 static hash hash_class_name_lookup (hash *, tree);
215 static hash hash_lookup (hash *, tree);
216 static tree lookup_method (tree, tree);
217 static tree lookup_method_static (tree, tree, int);
219 static tree add_class (tree, tree);
220 static void add_category (tree, tree);
221 static inline tree lookup_category (tree, tree);
225 class_names, /* class, category, protocol, module names */
226 meth_var_names, /* method and variable names */
227 meth_var_types /* method and variable type descriptors */
230 static tree add_objc_string (tree, enum string_section);
231 static void build_selector_table_decl (void);
235 static tree lookup_protocol (tree, bool);
236 static tree lookup_and_install_protocols (tree);
240 static void encode_type_qualifiers (tree);
241 static void encode_type (tree, int, int);
242 static void encode_field_decl (tree, int, int);
245 static void really_start_method (tree, tree);
247 static void really_start_method (tree, struct c_arg_info *);
249 static int comp_proto_with_proto (tree, tree, int);
250 static tree get_arg_type_list (tree, int, int);
251 static tree objc_decay_parm_type (tree);
252 static void objc_push_parm (tree);
254 static tree objc_get_parm_info (int);
256 static struct c_arg_info *objc_get_parm_info (int);
259 /* Utilities for debugging and error diagnostics. */
261 static char *gen_type_name (tree);
262 static char *gen_type_name_0 (tree);
263 static char *gen_method_decl (tree);
264 static char *gen_declaration (tree);
266 /* Everything else. */
268 static tree create_field_decl (tree, const char *);
269 static void add_class_reference (tree);
270 static void build_protocol_template (void);
271 static tree encode_method_prototype (tree);
272 static void generate_classref_translation_entry (tree);
273 static void handle_class_ref (tree);
274 static void generate_struct_by_value_array (void)
276 static void mark_referenced_methods (void);
277 static void generate_objc_image_info (void);
278 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
280 /*** Private Interface (data) ***/
282 /* Reserved tag definitions. */
284 #define OBJECT_TYPEDEF_NAME "id"
285 #define CLASS_TYPEDEF_NAME "Class"
287 #define TAG_OBJECT "objc_object"
288 #define TAG_CLASS "objc_class"
289 #define TAG_SUPER "objc_super"
290 #define TAG_SELECTOR "objc_selector"
292 #define UTAG_CLASS "_objc_class"
293 #define UTAG_IVAR "_objc_ivar"
294 #define UTAG_IVAR_LIST "_objc_ivar_list"
295 #define UTAG_METHOD "_objc_method"
296 #define UTAG_METHOD_LIST "_objc_method_list"
297 #define UTAG_CATEGORY "_objc_category"
298 #define UTAG_MODULE "_objc_module"
299 #define UTAG_SYMTAB "_objc_symtab"
300 #define UTAG_SUPER "_objc_super"
301 #define UTAG_SELECTOR "_objc_selector"
303 #define UTAG_PROTOCOL "_objc_protocol"
304 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
305 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
307 /* Note that the string object global name is only needed for the
309 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
311 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
313 #define TAG_ENUMERATION_MUTATION "objc_enumerationMutation"
314 #define TAG_FAST_ENUMERATION_STATE "__objcFastEnumerationState"
316 static const char *TAG_GETCLASS;
317 static const char *TAG_GETMETACLASS;
318 static const char *TAG_MSGSEND;
319 static const char *TAG_MSGSENDSUPER;
320 /* The NeXT Objective-C messenger may have two extra entry points, for use
321 when returning a structure. */
322 static const char *TAG_MSGSEND_STRET;
323 static const char *TAG_MSGSENDSUPER_STRET;
324 static const char *default_constant_string_class_name;
326 /* Runtime metadata flags. */
327 #define CLS_FACTORY 0x0001L
328 #define CLS_META 0x0002L
329 #define CLS_HAS_CXX_STRUCTORS 0x2000L
331 #define OBJC_MODIFIER_STATIC 0x00000001
332 #define OBJC_MODIFIER_FINAL 0x00000002
333 #define OBJC_MODIFIER_PUBLIC 0x00000004
334 #define OBJC_MODIFIER_PRIVATE 0x00000008
335 #define OBJC_MODIFIER_PROTECTED 0x00000010
336 #define OBJC_MODIFIER_NATIVE 0x00000020
337 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
338 #define OBJC_MODIFIER_ABSTRACT 0x00000080
339 #define OBJC_MODIFIER_VOLATILE 0x00000100
340 #define OBJC_MODIFIER_TRANSIENT 0x00000200
341 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
343 /* NeXT-specific tags. */
345 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
346 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
347 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
348 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
349 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
350 #define TAG_EXCEPTIONMATCH "objc_exception_match"
351 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
352 #define TAG_SYNCENTER "objc_sync_enter"
353 #define TAG_SYNCEXIT "objc_sync_exit"
354 #define TAG_SETJMP "_setjmp"
355 #define UTAG_EXCDATA "_objc_exception_data"
357 #define TAG_ASSIGNIVAR "objc_assign_ivar"
358 #define TAG_ASSIGNGLOBAL "objc_assign_global"
359 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
361 /* Branch entry points. All that matters here are the addresses;
362 functions with these names do not really exist in libobjc. */
364 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
365 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
367 #define TAG_CXX_CONSTRUCT ".cxx_construct"
368 #define TAG_CXX_DESTRUCT ".cxx_destruct"
370 /* GNU-specific tags. */
372 #define TAG_EXECCLASS "__objc_exec_class"
373 #define TAG_GNUINIT "__objc_gnu_init"
375 /* Flags for lookup_method_static(). */
377 /* Look for class methods. */
378 #define OBJC_LOOKUP_CLASS 1
379 /* Do not examine superclasses. */
380 #define OBJC_LOOKUP_NO_SUPER 2
381 /* Disable returning an instance method of a root class when a class
382 method can't be found. */
383 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
385 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
386 tree objc_global_trees[OCTI_MAX];
388 static void handle_impent (struct imp_entry *);
390 struct imp_entry *imp_list = 0;
391 int imp_count = 0; /* `@implementation' */
392 int cat_count = 0; /* `@category' */
394 objc_ivar_visibility_kind objc_ivar_visibility;
396 /* Use to generate method labels. */
397 static int method_slot = 0;
399 /* Flag to say whether methods in a protocol are optional or
401 static bool objc_method_optional_flag = false;
403 static int objc_collecting_ivars = 0;
407 static char *errbuf; /* Buffer for error diagnostics */
409 /* An array of all the local variables in the current function that
410 need to be marked as volatile. */
411 VEC(tree,gc) *local_variables_to_volatilize = NULL;
414 static int flag_typed_selectors;
416 /* Store all constructed constant strings in a hash table so that
417 they get uniqued properly. */
419 struct GTY(()) string_descriptor {
420 /* The literal argument . */
423 /* The resulting constant string. */
427 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
429 FILE *gen_declaration_file;
431 /* Tells "encode_pointer/encode_aggregate" whether we are generating
432 type descriptors for instance variables (as opposed to methods).
433 Type descriptors for instance variables contain more information
434 than methods (for static typing and embedded structures). */
436 static int generating_instance_variables = 0;
438 /* For building an objc struct. These may not be used when this file
439 is compiled as part of obj-c++. */
441 static bool objc_building_struct;
442 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
444 /* Start building a struct for objc. */
447 objc_start_struct (tree name)
449 gcc_assert (!objc_building_struct);
450 objc_building_struct = true;
451 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
454 /* Finish building a struct for objc. */
457 objc_finish_struct (tree type, tree fieldlist)
459 gcc_assert (objc_building_struct);
460 objc_building_struct = false;
461 return finish_struct (input_location, type, fieldlist, NULL_TREE,
466 build_sized_array_type (tree base_type, int size)
468 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
469 return build_array_type (base_type, index_type);
473 add_field_decl (tree type, const char *name, tree **chain)
475 tree field = create_field_decl (type, name);
479 *chain = &DECL_CHAIN (field);
484 /* Create a temporary variable of type 'type'. If 'name' is set, uses
485 the specified name, else use no name. Returns the declaration of
486 the type. The 'name' is mostly useful for debugging.
489 objc_create_temporary_var (tree type, const char *name)
495 decl = build_decl (input_location,
496 VAR_DECL, get_identifier (name), type);
500 decl = build_decl (input_location,
501 VAR_DECL, NULL_TREE, type);
503 TREE_USED (decl) = 1;
504 DECL_ARTIFICIAL (decl) = 1;
505 DECL_IGNORED_P (decl) = 1;
506 DECL_CONTEXT (decl) = current_function_decl;
511 /* Some platforms pass small structures through registers versus
512 through an invisible pointer. Determine at what size structure is
513 the transition point between the two possibilities. */
516 generate_struct_by_value_array (void)
521 int aggregate_in_mem[32];
524 /* Presumably no platform passes 32 byte structures in a register. */
525 for (i = 1; i < 32; i++)
530 /* Create an unnamed struct that has `i' character components */
531 type = objc_start_struct (NULL_TREE);
533 strcpy (buffer, "c1");
534 decls = add_field_decl (char_type_node, buffer, &chain);
536 for (j = 1; j < i; j++)
538 sprintf (buffer, "c%d", j + 1);
539 add_field_decl (char_type_node, buffer, &chain);
541 objc_finish_struct (type, decls);
543 aggregate_in_mem[i] = aggregate_value_p (type, 0);
544 if (!aggregate_in_mem[i])
548 /* We found some structures that are returned in registers instead of memory
549 so output the necessary data. */
552 for (i = 31; i >= 0; i--)
553 if (!aggregate_in_mem[i])
555 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
557 /* The first member of the structure is always 0 because we don't handle
558 structures with 0 members */
559 printf ("static int struct_forward_array[] = {\n 0");
561 for (j = 1; j <= i; j++)
562 printf (", %d", aggregate_in_mem[j]);
573 if (cxx_init () == false)
575 if (c_objc_common_init () == false)
579 /* If gen_declaration desired, open the output file. */
580 if (flag_gen_declaration)
582 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
583 gen_declaration_file = fopen (dumpname, "w");
584 if (gen_declaration_file == 0)
585 fatal_error ("can%'t open %s: %m", dumpname);
589 if (flag_next_runtime)
591 TAG_GETCLASS = "objc_getClass";
592 TAG_GETMETACLASS = "objc_getMetaClass";
593 TAG_MSGSEND = "objc_msgSend";
594 TAG_MSGSENDSUPER = "objc_msgSendSuper";
595 TAG_MSGSEND_STRET = "objc_msgSend_stret";
596 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
597 default_constant_string_class_name = "NSConstantString";
601 TAG_GETCLASS = "objc_get_class";
602 TAG_GETMETACLASS = "objc_get_meta_class";
603 TAG_MSGSEND = "objc_msg_lookup";
604 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
605 /* GNU runtime does not provide special functions to support
606 structure-returning methods. */
607 default_constant_string_class_name = "NXConstantString";
608 flag_typed_selectors = 1;
609 /* GNU runtime does not need the compiler to change code
610 in order to do GC. */
613 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
620 if (print_struct_values && !flag_compare_debug)
621 generate_struct_by_value_array ();
626 /* This is called automatically (at the very end of compilation) by
627 c_write_global_declarations and cp_write_global_declarations. */
629 objc_write_global_declarations (void)
631 mark_referenced_methods ();
633 /* Finalize Objective-C runtime data. */
636 if (gen_declaration_file)
637 fclose (gen_declaration_file);
640 /* Return the first occurrence of a method declaration corresponding
641 to sel_name in rproto_list. Search rproto_list recursively.
642 If is_class is 0, search for instance methods, otherwise for class
645 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
650 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
652 p = TREE_VALUE (rproto);
655 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
657 /* First, search the @required protocol methods. */
659 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
661 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
666 /* If still not found, search the @optional protocol methods. */
668 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
670 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
675 /* If still not found, search the attached protocols. */
676 if (PROTOCOL_LIST (p))
677 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
684 ; /* An identifier...if we could not find a protocol. */
692 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
696 /* Make sure the protocol is supported by the object on the rhs. */
697 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
700 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
702 p = TREE_VALUE (rproto);
704 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
709 else if (PROTOCOL_LIST (p))
710 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
719 ; /* An identifier...if we could not find a protocol. */
726 objc_start_class_interface (tree klass, tree super_class,
727 tree protos, tree attributes)
729 if (flag_objc1_only && attributes)
730 error_at (input_location, "class attributes are not available in Objective-C 1.0");
732 objc_interface_context
734 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
735 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
739 objc_start_category_interface (tree klass, tree categ,
740 tree protos, tree attributes)
745 error_at (input_location, "category attributes are not available in Objective-C 1.0");
747 warning_at (input_location, OPT_Wattributes,
748 "category attributes are not available in this version"
749 " of the compiler, (ignored)");
751 objc_interface_context
752 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
754 = continue_class (objc_interface_context);
758 objc_start_protocol (tree name, tree protos, tree attributes)
760 if (flag_objc1_only && attributes)
761 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
763 objc_interface_context
764 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
765 objc_method_optional_flag = false;
769 objc_continue_interface (void)
772 = continue_class (objc_interface_context);
776 objc_finish_interface (void)
778 finish_class (objc_interface_context);
779 objc_interface_context = NULL_TREE;
780 objc_method_optional_flag = false;
784 objc_start_class_implementation (tree klass, tree super_class)
786 objc_implementation_context
788 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
790 objc_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
794 objc_start_category_implementation (tree klass, tree categ)
796 objc_implementation_context
797 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
800 = continue_class (objc_implementation_context);
804 objc_continue_implementation (void)
807 = continue_class (objc_implementation_context);
811 objc_finish_implementation (void)
814 if (flag_objc_call_cxx_cdtors)
815 objc_generate_cxx_cdtors ();
818 if (objc_implementation_context)
820 finish_class (objc_implementation_context);
821 objc_ivar_chain = NULL_TREE;
822 objc_implementation_context = NULL_TREE;
825 warning (0, "%<@end%> must appear in an @implementation context");
829 objc_set_visibility (objc_ivar_visibility_kind visibility)
831 if (visibility == OBJC_IVAR_VIS_PACKAGE)
834 error ("%<@package%> is not available in Objective-C 1.0");
836 warning (0, "%<@package%> presently has the same effect as %<@public%>");
838 objc_ivar_visibility = visibility;
842 objc_set_method_opt (bool optional)
845 error_at (input_location, "@optional/@required are not available in Objective-C 1.0");
847 objc_method_optional_flag = optional;
848 if (!objc_interface_context
849 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
851 error ("@optional/@required is allowed in @protocol context only");
852 objc_method_optional_flag = false;
856 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
859 lookup_property_in_list (tree chain, tree property)
862 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
863 if (PROPERTY_NAME (x) == property)
868 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
869 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
872 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
874 tree p = TREE_VALUE (rproto);
875 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
877 if ((x = lookup_property_in_list (p, property)))
879 if (PROTOCOL_LIST (p))
880 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
884 ; /* An identifier...if we could not find a protocol. */
890 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
891 chain of interface hierarchy. */
893 lookup_property (tree interface_type, tree property)
895 tree inter = interface_type;
899 if ((x = lookup_property_in_list (inter, property)))
901 /* Failing that, look for the property in each category of the class. */
903 while ((category = CLASS_CATEGORY_LIST (category)))
905 if ((x = lookup_property_in_list (category, property)))
908 /* When checking a category, also check the protocols
909 attached with the category itself. */
910 if (CLASS_PROTOCOL_LIST (category)
911 && (x = lookup_property_in_protocol_list
912 (CLASS_PROTOCOL_LIST (category), property)))
916 /* Failing to find in categories, look for property in protocol list. */
917 if (CLASS_PROTOCOL_LIST (inter)
918 && (x = lookup_property_in_protocol_list
919 (CLASS_PROTOCOL_LIST (inter), property)))
922 /* Failing that, climb up the inheritance hierarchy. */
923 inter = lookup_interface (CLASS_SUPER_NAME (inter));
928 /* This routine is called by the parser when a
929 @property... declaration is found. 'decl' is the declaration of
930 the property (type/identifier), and the other arguments represent
931 property attributes that may have been specified in the Objective-C
932 declaration. 'parsed_property_readonly' is 'true' if the attribute
933 'readonly' was specified, and 'false' if not; similarly for the
934 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
935 if the attribute 'getter' was not specified, and is the identifier
936 corresponding to the specified getter if it was; similarly for
937 'parsed_property_setter_ident'. */
939 objc_add_property_declaration (location_t location, tree decl,
940 bool parsed_property_readonly, bool parsed_property_readwrite,
941 bool parsed_property_assign, bool parsed_property_retain,
942 bool parsed_property_copy, bool parsed_property_nonatomic,
943 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
947 /* 'property_readonly' and 'property_assign_semantics' are the final
948 attributes of the property after all parsed attributes have been
949 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
950 parsed_property_readonly = false and parsed_property_readwrite =
951 false, then property_readonly will be false because the default
953 bool property_readonly = false;
954 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
957 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
959 if (parsed_property_readonly && parsed_property_readwrite)
961 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
962 /* In case of conflicting attributes (here and below), after
963 producing an error, we pick one of the attributes and keep
965 property_readonly = false;
969 if (parsed_property_readonly)
970 property_readonly = true;
972 if (parsed_property_readwrite)
973 property_readonly = false;
976 if (parsed_property_readonly && parsed_property_setter_ident)
978 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
979 property_readonly = false;
982 if (parsed_property_assign && parsed_property_retain)
984 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
985 property_assign_semantics = OBJC_PROPERTY_RETAIN;
987 else if (parsed_property_assign && parsed_property_copy)
989 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
990 property_assign_semantics = OBJC_PROPERTY_COPY;
992 else if (parsed_property_retain && parsed_property_copy)
994 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
995 property_assign_semantics = OBJC_PROPERTY_COPY;
999 if (parsed_property_assign)
1000 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
1002 if (parsed_property_retain)
1003 property_assign_semantics = OBJC_PROPERTY_RETAIN;
1005 if (parsed_property_copy)
1006 property_assign_semantics = OBJC_PROPERTY_COPY;
1009 if (!objc_interface_context)
1011 error_at (location, "property declaration not in @interface or @protocol context");
1015 /* At this point we know that we are either in an interface, a
1016 category, or a protocol. */
1018 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
1019 something else, as that would confuse the checks below. */
1020 if (TREE_CODE (decl) != FIELD_DECL)
1022 error_at (location, "invalid property declaration");
1026 /* Do some spot-checks for the most obvious invalid types. */
1028 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1030 error_at (location, "property can not be an array");
1034 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
1035 parsing, while the C/ObjC parser accepts it and gives us a
1036 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
1037 to check for a bitfield when doing ObjC. */
1039 if (DECL_INITIAL (decl))
1041 /* A @property is not an actual variable, but it is a way to
1042 describe a pair of accessor methods, so its type (which is
1043 the type of the return value of the getter and the first
1044 argument of the setter) can't be a bitfield (as return values
1045 and arguments of functions can not be bitfields). The
1046 underlying instance variable could be a bitfield, but that is
1047 a different matter. */
1048 error_at (location, "property can not be a bit-field");
1053 /* TODO: Check that the property type is an Objective-C object or a
1056 /* Implement -Wproperty-assign-default (which is enabled by default). */
1057 if (warn_property_assign_default
1058 /* If garbage collection is not being used, then 'assign' is
1059 valid for objects (and typically used for delegates) but it
1060 is wrong in most cases (since most objects need to be
1061 retained or copied in setters). Warn users when 'assign' is
1063 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
1064 /* Read-only properties are never assigned, so the assignment
1065 semantics do not matter in that case. */
1066 && !property_readonly
1069 /* Please note that it would make sense to default to 'assign'
1070 for non-{Objective-C objects}, and to 'retain' for
1071 Objective-C objects. But that would break compatibility with
1073 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
1075 /* Use 'false' so we do not warn for Class objects. */
1076 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
1078 warning_at (location,
1080 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
1083 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
1088 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
1089 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1090 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
1092 if (property_assign_semantics == OBJC_PROPERTY_COPY
1093 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1094 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
1096 /* Now determine the final property getter and setter names. They
1097 will be stored in the PROPERTY_DECL, from which they'll always be
1098 extracted and used. */
1100 /* Adjust, or fill in, setter and getter names. We overwrite the
1101 parsed_property_setter_ident and parsed_property_getter_ident
1102 with the final setter and getter identifiers that will be
1104 if (parsed_property_setter_ident)
1106 /* The setter should be terminated by ':', but the parser only
1107 gives us an identifier without ':'. So, we need to add ':'
1109 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1110 size_t length = strlen (parsed_setter);
1111 char *final_setter = (char *)alloca (length + 2);
1113 sprintf (final_setter, "%s:", parsed_setter);
1114 parsed_property_setter_ident = get_identifier (final_setter);
1118 if (!property_readonly)
1119 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1120 (DECL_NAME (decl)));
1123 if (!parsed_property_getter_ident)
1124 parsed_property_getter_ident = DECL_NAME (decl);
1126 /* Check for duplicate property declarations. We first check the
1127 immediate context for a property with the same name. Any such
1128 declarations are an error. */
1129 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1131 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1133 location_t original_location = DECL_SOURCE_LOCATION (x);
1135 error_at (location, "redeclaration of property %qD", decl);
1137 if (original_location != UNKNOWN_LOCATION)
1138 inform (original_location, "originally specified here");
1143 /* We now need to check for existing property declarations (in the
1144 superclass, other categories or protocols) and check that the new
1145 declaration is not in conflict with existing ones. */
1147 /* Search for a previous, existing declaration of a property with
1148 the same name in superclasses, protocols etc. If one is found,
1149 it will be in the 'x' variable. */
1152 /* Note that, for simplicity, the following may search again the
1153 local context. That's Ok as nothing will be found (else we'd
1154 have thrown an error above); it's only a little inefficient, but
1155 the code is simpler. */
1156 switch (TREE_CODE (objc_interface_context))
1158 case CLASS_INTERFACE_TYPE:
1159 /* Look up the property in the current @interface (which will
1160 find nothing), then its protocols and categories and
1162 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1164 case CATEGORY_INTERFACE_TYPE:
1165 /* Look up the property in the main @interface, then protocols
1166 and categories (one of them is ours, and will find nothing)
1167 and superclasses. */
1168 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1171 case PROTOCOL_INTERFACE_TYPE:
1172 /* Looks up the property in any protocols attached to the
1173 current protocol. */
1174 if (PROTOCOL_LIST (objc_interface_context))
1176 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1186 /* An existing property was found; check that it has the same
1187 types, or it is compatible. */
1188 location_t original_location = DECL_SOURCE_LOCATION (x);
1190 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1192 warning_at (location, 0,
1193 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1195 if (original_location != UNKNOWN_LOCATION)
1196 inform (original_location, "originally specified here");
1200 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1202 warning_at (location, 0,
1203 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1205 if (original_location != UNKNOWN_LOCATION)
1206 inform (original_location, "originally specified here");
1210 /* We can only compare the setter names if both the old and new property have a setter. */
1211 if (!property_readonly && !PROPERTY_READONLY(x))
1213 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1215 warning_at (location, 0,
1216 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1218 if (original_location != UNKNOWN_LOCATION)
1219 inform (original_location, "originally specified here");
1224 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1226 warning_at (location, 0,
1227 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1229 if (original_location != UNKNOWN_LOCATION)
1230 inform (original_location, "originally specified here");
1234 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1235 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1237 warning_at (location, 0,
1238 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1240 if (original_location != UNKNOWN_LOCATION)
1241 inform (original_location, "originally specified here");
1245 /* We now check that the new and old property declarations have
1246 the same types (or compatible one). In the Objective-C
1247 tradition of loose type checking, we do type-checking but
1248 only generate warnings (not errors) if they do not match.
1249 For non-readonly properties, the types must match exactly;
1250 for readonly properties, it is allowed to use a "more
1251 specialized" type in the new property declaration. Eg, the
1252 superclass has a getter returning (NSArray *) and the
1253 subclass a getter returning (NSMutableArray *). The object's
1254 getter returns an (NSMutableArray *); but if you cast the
1255 object to the superclass, which is allowed, you'd still
1256 expect the getter to return an (NSArray *), which works since
1257 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1258 objects belonging to the type of the new @property should be
1259 a subset of the set of objects belonging to the type of the
1260 old @property. This is what "specialization" means. And the
1261 reason it only applies to readonly properties is that for a
1262 readwrite property the setter would have the opposite
1263 requirement - ie that the superclass type is more specialized
1264 then the subclass one; hence the only way to satisfy both
1265 constraints is that the types match. */
1267 /* If the types are not the same in the C sense, we warn ... */
1268 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1269 /* ... unless the property is readonly, in which case we
1270 allow a new, more specialized, declaration. */
1271 && (!property_readonly
1272 || !objc_compare_types (TREE_TYPE (x),
1273 TREE_TYPE (decl), -5, NULL_TREE)))
1275 warning_at (location, 0,
1276 "type of property %qD conflicts with previous declaration", decl);
1277 if (original_location != UNKNOWN_LOCATION)
1278 inform (original_location, "originally specified here");
1283 /* Create a PROPERTY_DECL node. */
1284 property_decl = make_node (PROPERTY_DECL);
1286 /* Copy the basic information from the original decl. */
1287 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1288 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1289 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1291 /* Add property-specific information. */
1292 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1293 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1294 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1295 PROPERTY_READONLY (property_decl) = property_readonly;
1296 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1297 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1298 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1299 PROPERTY_DYNAMIC (property_decl) = 0;
1301 /* Remember the fact that the property was found in the @optional
1302 section in a @protocol, or not. */
1303 if (objc_method_optional_flag)
1304 PROPERTY_OPTIONAL (property_decl) = 1;
1306 PROPERTY_OPTIONAL (property_decl) = 0;
1308 /* Note that PROPERTY_GETTER_NAME is always set for all
1309 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1310 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1311 with a getter or setter, we should get the PROPERTY_DECL and use
1312 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1315 /* Add the PROPERTY_DECL to the list of properties for the class. */
1316 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1317 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1320 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1321 list of methods in the interface (and, failing that, the local list
1322 in the implementation, and failing that, the protocol list)
1323 provided for a 'setter' or 'getter' for 'component' with default
1324 names (ie, if 'component' is "name", then search for "name" and
1325 "setName:"). It is also possible to specify a different
1326 'getter_name' (this is used for @optional readonly properties). If
1327 any is found, then create an artificial property that uses them.
1328 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1330 maybe_make_artificial_property_decl (tree interface, tree implementation,
1331 tree protocol_list, tree component, bool is_class,
1334 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1335 tree getter = NULL_TREE;
1336 tree setter = NULL_TREE;
1338 if (getter_name == NULL_TREE)
1339 getter_name = component;
1341 /* First, check the @interface and all superclasses. */
1346 /* Using instance methods of the root class as accessors is most
1347 likely unwanted and can be extremely confusing (and, most
1348 importantly, other Objective-C 2.0 compilers do not do it).
1351 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1353 getter = lookup_method_static (interface, getter_name, flags);
1354 setter = lookup_method_static (interface, setter_name, flags);
1357 /* Second, check the local @implementation context. */
1358 if (!getter && !setter)
1364 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1365 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1369 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1370 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1375 /* Try the protocol_list if we didn't find anything in the
1376 @interface and in the @implementation. */
1377 if (!getter && !setter)
1379 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1380 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1383 /* There needs to be at least a getter or setter for this to be a
1384 valid 'object.component' syntax. */
1385 if (getter || setter)
1387 /* Yes ... determine the type of the expression. */
1392 type = TREE_VALUE (TREE_TYPE (getter));
1394 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1396 /* Create an artificial property declaration with the
1397 information we collected on the type and getter/setter
1399 property_decl = make_node (PROPERTY_DECL);
1401 TREE_TYPE (property_decl) = type;
1402 DECL_SOURCE_LOCATION (property_decl) = input_location;
1403 TREE_DEPRECATED (property_decl) = 0;
1404 DECL_ARTIFICIAL (property_decl) = 1;
1406 /* Add property-specific information. Note that one of
1407 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1408 non-existing method; this will generate an error when the
1409 expression is later compiled. At this stage we don't know if
1410 the getter or setter will be used, so we can't generate an
1412 PROPERTY_NAME (property_decl) = component;
1413 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1414 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1415 PROPERTY_READONLY (property_decl) = 0;
1416 PROPERTY_NONATOMIC (property_decl) = 0;
1417 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1418 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1419 PROPERTY_DYNAMIC (property_decl) = 0;
1420 PROPERTY_OPTIONAL (property_decl) = 0;
1423 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1425 /* The following is currently unused, but it's nice to have
1426 there. We may use it if we need in the future. */
1428 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1430 return property_decl;
1436 /* This hook routine is invoked by the parser when an expression such
1437 as 'xxx.yyy' is parsed. We get a chance to process these
1438 expressions in a way that is specified to Objective-C (to implement
1439 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1440 If the expression is not an Objective-C specified expression, we
1441 should return NULL_TREE; else we return the expression.
1443 At the moment this only implements dot-syntax and properties (not
1444 non-fragile ivars yet), ie 'object.property' or 'object.component'
1445 where 'component' is not a declared property, but a valid getter or
1446 setter for it could be found. */
1448 objc_maybe_build_component_ref (tree object, tree property_ident)
1453 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1455 if (flag_objc1_only)
1458 /* Try to determine if 'object' is an Objective-C object or not. If
1460 if (object == NULL_TREE || object == error_mark_node
1461 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1464 if (property_ident == NULL_TREE || property_ident == error_mark_node
1465 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1468 /* The following analysis of 'object' is similar to the one used for
1469 the 'receiver' of a method invocation. We need to determine what
1470 'object' is and find the appropriate property (either declared,
1471 or artificial) for it (in the same way as we need to find the
1472 appropriate method prototype for a method invocation). There are
1473 some simplifications here though: "object.property" is invalid if
1474 "object" has a type of "id" or "Class"; it must at least have a
1475 protocol attached to it, and "object" is never a class name as
1476 that is done by objc_build_class_component_ref. Finally, we
1477 don't know if this really is a dot-syntax expression, so we want
1478 to make a quick exit if it is not; for this reason, we try to
1479 postpone checks after determining that 'object' looks like an
1480 Objective-C object. */
1482 if (objc_is_id (rtype))
1484 /* This is the case that the 'object' is of type 'id' or
1487 /* Check if at least it is of type 'id <Protocol>' or 'Class
1488 <Protocol>'; if so, look the property up in the
1490 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1492 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1496 /* No point looking up declared @properties if we are
1497 dealing with a class. Classes have no declared
1499 if (!IS_CLASS (rtype))
1500 x = lookup_property_in_protocol_list (rprotos, property_ident);
1504 /* Ok, no property. Maybe it was an
1505 object.component dot-syntax without a declared
1506 property (this is valid for classes too). Look
1507 for getter/setter methods and internally declare
1508 an artifical property based on them if found. */
1509 x = maybe_make_artificial_property_decl (NULL_TREE,
1516 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1518 /* This is a special, complicated case. If the
1519 property is optional, and is read-only, then the
1520 property is always used for reading, but an
1521 eventual existing non-property setter can be used
1522 for writing. We create an artificial property
1523 decl copying the getter from the optional
1524 property, and looking up the setter in the
1526 x = maybe_make_artificial_property_decl (NULL_TREE,
1531 PROPERTY_GETTER_NAME (x));
1535 else if (objc_method_context)
1537 /* Else, if we are inside a method it could be the case of
1538 'super' or 'self'. */
1539 tree interface_type = NULL_TREE;
1541 while (TREE_CODE (t) == COMPOUND_EXPR
1542 || TREE_CODE (t) == MODIFY_EXPR
1543 || CONVERT_EXPR_P (t)
1544 || TREE_CODE (t) == COMPONENT_REF)
1545 t = TREE_OPERAND (t, 0);
1547 if (t == UOBJC_SUPER_decl)
1548 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1549 else if (t == self_decl)
1550 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1554 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1555 x = lookup_property (interface_type, property_ident);
1559 /* Try the dot-syntax without a declared property.
1560 If this is an access to 'self', it is possible
1561 that they may refer to a setter/getter that is
1562 not declared in the interface, but exists locally
1563 in the implementation. In that case, get the
1564 implementation context and use it. */
1565 tree implementation = NULL_TREE;
1568 implementation = objc_implementation_context;
1570 x = maybe_make_artificial_property_decl
1571 (interface_type, implementation, NULL_TREE,
1573 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1576 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1578 tree implementation = NULL_TREE;
1581 implementation = objc_implementation_context;
1583 x = maybe_make_artificial_property_decl (interface_type,
1588 PROPERTY_GETTER_NAME (x));
1595 /* This is the case where we have more information on 'rtype'. */
1596 tree basetype = TYPE_MAIN_VARIANT (rtype);
1598 /* Skip the pointer - if none, it's not an Objective-C object or
1600 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1601 basetype = TREE_TYPE (basetype);
1605 /* Traverse typedefs. */
1606 while (basetype != NULL_TREE
1607 && TREE_CODE (basetype) == RECORD_TYPE
1608 && OBJC_TYPE_NAME (basetype)
1609 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1610 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1611 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1613 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1615 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1616 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1619 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1620 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1621 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1623 /* Not sure 'rtype' could ever be a class here! Just
1624 for safety we keep the checks. */
1625 if (!IS_CLASS (rtype))
1627 x = lookup_property (interface_type, property_ident);
1630 x = lookup_property_in_protocol_list (protocol_list,
1636 /* Try the dot-syntax without a declared property.
1637 If we are inside a method implementation, it is
1638 possible that they may refer to a setter/getter
1639 that is not declared in the interface, but exists
1640 locally in the implementation. In that case, get
1641 the implementation context and use it. */
1642 tree implementation = NULL_TREE;
1644 if (objc_implementation_context
1645 && CLASS_NAME (objc_implementation_context)
1646 == OBJC_TYPE_NAME (interface_type))
1647 implementation = objc_implementation_context;
1649 x = maybe_make_artificial_property_decl (interface_type,
1656 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1658 tree implementation = NULL_TREE;
1660 if (objc_implementation_context
1661 && CLASS_NAME (objc_implementation_context)
1662 == OBJC_TYPE_NAME (interface_type))
1663 implementation = objc_implementation_context;
1665 x = maybe_make_artificial_property_decl (interface_type,
1670 PROPERTY_GETTER_NAME (x));
1681 /* We have an additional nasty problem here; if this
1682 PROPERTY_REF needs to become a 'getter', then the conversion
1683 from PROPERTY_REF into a getter call happens in gimplify,
1684 after the selector table has already been generated and when
1685 it is too late to add another selector to it. To work around
1686 the problem, we always create the getter call at this stage,
1687 which puts the selector in the table. Note that if the
1688 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1689 we have added a selector too many to the selector table.
1690 This is a little inefficient.
1692 Also note that method calls to 'self' and 'super' require the
1693 context (self_decl, UOBJS_SUPER_decl,
1694 objc_implementation_context etc) to be built correctly; this
1695 is yet another reason why building the call at the gimplify
1696 stage (when this context has been lost) is not very
1697 practical. If we build it at this stage, we know it will
1698 always be built correctly.
1700 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1701 property decl created to deal with a dotsyntax not really
1702 referring to an existing property) then do not try to build a
1703 call to the getter as there is no getter. */
1704 if (PROPERTY_HAS_NO_GETTER (x))
1705 getter_call = NULL_TREE;
1707 getter_call = objc_finish_message_expr (object,
1708 PROPERTY_GETTER_NAME (x),
1711 if (TREE_DEPRECATED (x))
1712 warn_deprecated_use (x, NULL_TREE);
1714 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1715 SET_EXPR_LOCATION (expression, input_location);
1716 TREE_SIDE_EFFECTS (expression) = 1;
1724 /* This hook routine is invoked by the parser when an expression such
1725 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1726 Objective-C 2.0 dot-syntax applied to classes, so we need to
1727 convert it into a setter/getter call on the class. */
1729 objc_build_class_component_ref (tree class_name, tree property_ident)
1734 if (flag_objc1_only)
1735 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1737 if (class_name == NULL_TREE || class_name == error_mark_node
1738 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1739 return error_mark_node;
1741 if (property_ident == NULL_TREE || property_ident == error_mark_node
1742 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1745 object = objc_get_class_reference (class_name);
1748 /* We know that 'class_name' is an Objective-C class name as the
1749 parser won't call this function if it is not. This is only a
1750 double-check for safety. */
1751 error_at (input_location, "could not find class %qE", class_name);
1752 return error_mark_node;
1755 rtype = lookup_interface (class_name);
1758 /* Again, this should never happen, but we do check. */
1759 error_at (input_location, "could not find interface for class %qE", class_name);
1760 return error_mark_node;
1764 if (TREE_DEPRECATED (rtype))
1765 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1768 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1777 if (PROPERTY_HAS_NO_GETTER (x))
1778 getter_call = NULL_TREE;
1780 getter_call = objc_finish_message_expr (object,
1781 PROPERTY_GETTER_NAME (x),
1783 if (TREE_DEPRECATED (x))
1784 warn_deprecated_use (x, NULL_TREE);
1786 expression = build3 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call);
1787 SET_EXPR_LOCATION (expression, input_location);
1788 TREE_SIDE_EFFECTS (expression) = 1;
1794 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1795 property_ident, class_name);
1796 return error_mark_node;
1804 /* This is used because we don't want to expose PROPERTY_REF to the
1805 C/C++ frontends. Maybe we should! */
1807 objc_is_property_ref (tree node)
1809 if (node && TREE_CODE (node) == PROPERTY_REF)
1815 /* This function builds a setter call for a PROPERTY_REF (real, for a
1816 declared property, or artificial, for a dot-syntax accessor which
1817 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1818 (the caller must check this beforehand). 'rhs' is the value to
1819 assign to the property. A plain setter call is returned, or
1820 error_mark_node if the property is readonly. */
1823 objc_build_setter_call (tree lhs, tree rhs)
1825 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1826 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1828 if (PROPERTY_READONLY (property_decl))
1830 error ("readonly property can not be set");
1831 return error_mark_node;
1835 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1838 /* TODO: Check that the setter return type is 'void'. */
1840 /* TODO: Decay arguments in C. */
1841 setter = objc_finish_message_expr (object_expr,
1842 PROPERTY_SETTER_NAME (property_decl),
1847 /* Unreachable, but the compiler may not realize. */
1848 return error_mark_node;
1851 /* This hook routine is called when a MODIFY_EXPR is being built. We
1852 check what is being modified; if it is a PROPERTY_REF, we need to
1853 generate a 'setter' function call for the property. If this is not
1854 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1855 on creating their MODIFY_EXPR.
1857 This is used for example if you write
1861 where 'count' is a property. The left-hand side creates a
1862 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1863 to assign something to it. We intercept that here, and generate a
1864 call to the 'setter' method instead. */
1866 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1868 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1870 /* Building a simple call to the setter method would work for cases such as
1874 but wouldn't work for cases such as
1876 count = object2.count = 1;
1878 to get these to work with very little effort, we build a
1879 compound statement which does the setter call (to set the
1880 property to 'rhs'), but which can also be evaluated returning
1881 the 'rhs'. So, we want to create the following:
1883 (temp = rhs; [object setProperty: temp]; temp)
1885 tree temp_variable_decl, bind;
1886 /* s1, s2 and s3 are the tree statements that we need in the
1887 compound expression. */
1888 tree s1, s2, s3, compound_expr;
1890 /* TODO: If 'rhs' is a constant, we could maybe do without the
1891 'temp' variable ? */
1893 /* Declare __objc_property_temp in a local bind. */
1894 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1895 DECL_SOURCE_LOCATION (temp_variable_decl) = input_location;
1896 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1897 SET_EXPR_LOCATION (bind, input_location);
1898 TREE_SIDE_EFFECTS (bind) = 1;
1901 /* Now build the compound statement. */
1903 /* s1: __objc_property_temp = rhs */
1904 s1 = build_modify_expr (input_location, temp_variable_decl, NULL_TREE,
1906 input_location, rhs, NULL_TREE);
1907 SET_EXPR_LOCATION (s1, input_location);
1909 /* s2: [object setProperty: __objc_property_temp] */
1910 s2 = objc_build_setter_call (lhs, temp_variable_decl);
1912 /* This happens if building the setter failed because the property
1914 if (s2 == error_mark_node)
1915 return error_mark_node;
1917 SET_EXPR_LOCATION (s2, input_location);
1919 /* s3: __objc_property_temp */
1920 s3 = convert (TREE_TYPE (lhs), temp_variable_decl);
1922 /* Now build the compound statement (s1, s2, s3) */
1923 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1925 /* Without this, with -Wall you get a 'valued computed is not
1926 used' every time there is a "object.property = x" where the
1927 value of the resulting MODIFY_EXPR is not used. That is
1928 correct (maybe a more sophisticated implementation could
1929 avoid generating the compound expression if not needed), but
1930 we need to turn it off. */
1931 TREE_NO_WARNING (compound_expr) = 1;
1932 return compound_expr;
1938 /* This hook is called by the frontend when one of the four unary
1939 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1940 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1941 argument which is a PROPERTY_REF. For example, this happens if you have
1945 where 'count' is a property. We need to use the 'getter' and
1946 'setter' for the property in an appropriate way to build the
1947 appropriate expression. 'code' is the code for the expression (one
1948 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1949 'increment' is how much we need to add or subtract. */
1951 objc_build_incr_expr_for_property_ref (location_t location,
1952 enum tree_code code,
1953 tree argument, tree increment)
1955 /* Here are the expressions that we want to build:
1957 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1958 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1960 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1961 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1963 tree temp_variable_decl, bind;
1964 /* s1, s2 and s3 are the tree statements that we need in the
1965 compound expression. */
1966 tree s1, s2, s3, compound_expr;
1969 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1970 return error_mark_node;
1972 /* Declare __objc_property_temp in a local bind. */
1973 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1974 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1975 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1976 SET_EXPR_LOCATION (bind, location);
1977 TREE_SIDE_EFFECTS (bind) = 1;
1980 /* Now build the compound statement. */
1982 /* Note that the 'getter' is generated at gimplify time; at this
1983 time, we can simply put the property_ref (ie, argument) wherever
1984 we want the getter ultimately to be. */
1986 /* s1: __objc_property_temp = [object property] <+/- increment> */
1989 case PREINCREMENT_EXPR:
1990 /* __objc_property_temp = [object property] + increment */
1991 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1993 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1994 argument, increment), NULL_TREE);
1996 case PREDECREMENT_EXPR:
1997 /* __objc_property_temp = [object property] - increment */
1998 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
2000 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
2001 argument, increment), NULL_TREE);
2003 case POSTINCREMENT_EXPR:
2004 case POSTDECREMENT_EXPR:
2005 /* __objc_property_temp = [object property] */
2006 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
2008 location, argument, NULL_TREE);
2014 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
2017 case PREINCREMENT_EXPR:
2018 case PREDECREMENT_EXPR:
2019 /* [object setProperty: __objc_property_temp] */
2020 s2 = objc_build_setter_call (argument, temp_variable_decl);
2022 case POSTINCREMENT_EXPR:
2023 /* [object setProperty: __objc_property_temp + increment] */
2024 s2 = objc_build_setter_call (argument,
2025 build2 (PLUS_EXPR, TREE_TYPE (argument),
2026 temp_variable_decl, increment));
2028 case POSTDECREMENT_EXPR:
2029 /* [object setProperty: __objc_property_temp - increment] */
2030 s2 = objc_build_setter_call (argument,
2031 build2 (MINUS_EXPR, TREE_TYPE (argument),
2032 temp_variable_decl, increment));
2038 /* This happens if building the setter failed because the property
2040 if (s2 == error_mark_node)
2041 return error_mark_node;
2043 SET_EXPR_LOCATION (s2, location);
2045 /* s3: __objc_property_temp */
2046 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
2048 /* Now build the compound statement (s1, s2, s3) */
2049 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
2051 /* Prevent C++ from warning with -Wall that "right operand of comma
2052 operator has no effect". */
2053 TREE_NO_WARNING (compound_expr) = 1;
2054 return compound_expr;
2058 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
2059 tree optparms, bool ellipsis)
2061 if (is_class_method)
2062 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2063 optparms, ellipsis);
2065 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2066 optparms, ellipsis);
2070 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2072 if (!objc_interface_context)
2074 /* PS: At the moment, due to how the parser works, it should be
2075 impossible to get here. But it's good to have the check in
2076 case the parser changes.
2078 fatal_error ("method declaration not in @interface context");
2081 if (flag_objc1_only && attributes)
2082 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2084 objc_decl_method_attributes (&decl, attributes, 0);
2085 objc_add_method (objc_interface_context,
2088 objc_method_optional_flag);
2091 /* Return 'true' if the method definition could be started, and
2092 'false' if not (because we are outside an @implementation context).
2095 objc_start_method_definition (bool is_class_method, tree decl, tree attributes)
2097 if (!objc_implementation_context)
2099 error ("method definition not in @implementation context");
2103 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2107 /* Indicate no valid break/continue context by setting these variables
2108 to some non-null, non-label value. We'll notice and emit the proper
2109 error message in c_finish_bc_stmt. */
2110 c_break_label = c_cont_label = size_zero_node;
2114 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2116 objc_decl_method_attributes (&decl, attributes, 0);
2118 objc_add_method (objc_implementation_context,
2121 /* is optional */ false);
2122 start_method_def (decl);
2127 objc_add_instance_variable (tree decl)
2129 (void) add_instance_variable (objc_ivar_context,
2130 objc_ivar_visibility,
2134 /* Return true if TYPE is 'id'. */
2137 objc_is_object_id (tree type)
2139 return OBJC_TYPE_NAME (type) == objc_object_id;
2143 objc_is_class_id (tree type)
2145 return OBJC_TYPE_NAME (type) == objc_class_id;
2148 /* Construct a C struct with same name as KLASS, a base struct with tag
2149 SUPER_NAME (if any), and FIELDS indicated. */
2152 objc_build_struct (tree klass, tree fields, tree super_name)
2154 tree name = CLASS_NAME (klass);
2155 tree s = objc_start_struct (name);
2156 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2158 VEC(tree,heap) *objc_info = NULL;
2163 /* Prepend a packed variant of the base class into the layout. This
2164 is necessary to preserve ObjC ABI compatibility. */
2165 tree base = build_decl (input_location,
2166 FIELD_DECL, NULL_TREE, super);
2167 tree field = TYPE_FIELDS (super);
2169 while (field && DECL_CHAIN (field)
2170 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2171 field = DECL_CHAIN (field);
2173 /* For ObjC ABI purposes, the "packed" size of a base class is
2174 the sum of the offset and the size (in bits) of the last field
2177 = (field && TREE_CODE (field) == FIELD_DECL
2178 ? size_binop (PLUS_EXPR,
2179 size_binop (PLUS_EXPR,
2182 convert (bitsizetype,
2183 DECL_FIELD_OFFSET (field)),
2184 bitsize_int (BITS_PER_UNIT)),
2185 DECL_FIELD_BIT_OFFSET (field)),
2187 : bitsize_zero_node);
2188 DECL_SIZE_UNIT (base)
2189 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2190 size_int (BITS_PER_UNIT));
2191 DECL_ARTIFICIAL (base) = 1;
2192 DECL_ALIGN (base) = 1;
2193 DECL_FIELD_CONTEXT (base) = s;
2195 DECL_FIELD_IS_BASE (base) = 1;
2198 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2199 #endif /* are following the ObjC ABI here. */
2200 DECL_CHAIN (base) = fields;
2204 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2205 information in all variants of this RECORD_TYPE to be destroyed
2206 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2207 for something else and then will change all variants to use the
2208 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2209 it for ObjC protocols and that such propagation will make all
2210 variants use the same objc_info), but it is therein that we store
2211 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2212 Hence, we must save the ObjC-specific information before calling
2213 finish_struct(), and then reinstate it afterwards. */
2215 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2217 INIT_TYPE_OBJC_INFO (t);
2218 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
2221 s = objc_finish_struct (s, fields);
2223 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2225 /* We now want to restore the different TYPE_OBJC_INFO, but we
2226 have the additional problem that the C frontend doesn't just
2227 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2228 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2229 we need a different TYPE_OBJC_INFO for each (and
2230 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2231 make a copy of each TYPE_LANG_SPECIFIC before we modify
2233 if (TYPE_LANG_SPECIFIC (t))
2235 /* Create a copy of TYPE_LANG_SPECIFIC. */
2236 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2237 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2238 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2239 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2243 /* Just create a new one. */
2244 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2246 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2247 protocol information that may have been associated with the
2249 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
2250 /* Replace the IDENTIFIER_NODE with an actual @interface now
2252 TYPE_OBJC_INTERFACE (t) = klass;
2254 VEC_free (tree, heap, objc_info);
2256 /* Use TYPE_BINFO structures to point at the super class, if any. */
2257 objc_xref_basetypes (s, super);
2259 /* Mark this struct as a class template. */
2260 CLASS_STATIC_TEMPLATE (klass) = s;
2265 /* Mark DECL as being 'volatile' for purposes of Darwin
2266 _setjmp()/_longjmp() exception handling. Called from
2267 objc_mark_locals_volatile(). */
2269 objc_volatilize_decl (tree decl)
2271 /* Do not mess with variables that are 'static' or (already)
2273 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2274 && (TREE_CODE (decl) == VAR_DECL
2275 || TREE_CODE (decl) == PARM_DECL))
2277 if (local_variables_to_volatilize == NULL)
2278 local_variables_to_volatilize = VEC_alloc (tree, gc, 8);
2280 VEC_safe_push (tree, gc, local_variables_to_volatilize, decl);
2284 /* Called when parsing of a function completes; if any local variables
2285 in the function were marked as variables to volatilize, change them
2286 to volatile. We do this at the end of the function when the
2287 warnings about discarding 'volatile' have already been produced.
2288 We are making the variables as volatile just to force the compiler
2289 to preserve them between setjmp/longjmp, but we don't want warnings
2290 for them as they aren't really volatile. */
2292 objc_finish_function (void)
2294 /* If there are any local variables to volatilize, volatilize them. */
2295 if (local_variables_to_volatilize)
2299 FOR_EACH_VEC_ELT (tree, local_variables_to_volatilize, i, decl)
2301 tree t = TREE_TYPE (decl);
2303 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2304 TREE_TYPE (decl) = t;
2305 TREE_THIS_VOLATILE (decl) = 1;
2306 TREE_SIDE_EFFECTS (decl) = 1;
2307 DECL_REGISTER (decl) = 0;
2309 C_DECL_REGISTER (decl) = 0;
2313 /* Now we delete the vector. This sets it to NULL as well. */
2314 VEC_free (tree, gc, local_variables_to_volatilize);
2318 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2319 (including its categories and superclasses) or by object type TYP.
2320 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2323 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2325 bool class_type = (cls != NULL_TREE);
2331 /* Check protocols adopted by the class and its categories. */
2332 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2334 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2338 /* Repeat for superclasses. */
2339 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2342 /* Check for any protocols attached directly to the object type. */
2343 if (TYPE_HAS_OBJC_INFO (typ))
2345 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2352 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2353 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2354 "implementing" a given protocol, since they do not have an
2357 warning (0, "class %qs does not implement the %qE protocol",
2358 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2360 warning (0, "type %qs does not conform to the %qE protocol",
2361 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2367 /* Check if class RCLS and instance struct type RTYP conform to at least the
2368 same protocols that LCLS and LTYP conform to. */
2371 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2374 bool have_lproto = false;
2378 /* NB: We do _not_ look at categories defined for LCLS; these may or
2379 may not get loaded in, and therefore it is unreasonable to require
2380 that RCLS/RTYP must implement any of their protocols. */
2381 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2385 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2389 /* Repeat for superclasses. */
2390 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2393 /* Check for any protocols attached directly to the object type. */
2394 if (TYPE_HAS_OBJC_INFO (ltyp))
2396 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2400 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2405 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2406 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2407 away with simply checking for 'id' or 'Class' (!RCLS), since this
2408 routine will not get called in other cases. */
2409 return have_lproto || (rcls != NULL_TREE);
2412 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2413 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2414 compatible by objc_compare_types() below. */
2417 objc_common_type (tree type1, tree type2)
2419 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2421 while (POINTER_TYPE_P (inner1))
2423 inner1 = TREE_TYPE (inner1);
2424 inner2 = TREE_TYPE (inner2);
2427 /* If one type is derived from another, return the base type. */
2428 if (DERIVED_FROM_P (inner1, inner2))
2430 else if (DERIVED_FROM_P (inner2, inner1))
2433 /* If both types are 'Class', return 'Class'. */
2434 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2435 return objc_class_type;
2437 /* Otherwise, return 'id'. */
2438 return objc_object_type;
2441 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2442 an instance of RTYP to an instance of LTYP or to compare the two
2443 (if ARGNO is equal to -3), per ObjC type system rules. Before
2444 returning 'true', this routine may issue warnings related to, e.g.,
2445 protocol conformance. When returning 'false', the routine must
2446 produce absolutely no warnings; the C or C++ front-end will do so
2447 instead, if needed. If either LTYP or RTYP is not an Objective-C
2448 type, the routine must return 'false'.
2450 The ARGNO parameter is encoded as follows:
2451 >= 1 Parameter number (CALLEE contains function being called);
2455 -3 Comparison (LTYP and RTYP may match in either direction);
2456 -4 Silent comparison (for C++ overload resolution);
2457 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2458 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2459 so that each object of type RTYP is also of type LTYP). This is used
2460 when comparing property types. */
2463 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2465 tree lcls, rcls, lproto, rproto;
2466 bool pointers_compatible;
2468 /* We must be dealing with pointer types */
2469 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2474 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2475 rtyp = TREE_TYPE (rtyp);
2477 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2479 /* We must also handle function pointers, since ObjC is a bit more
2480 lenient than C or C++ on this. */
2481 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2483 /* Return types must be covariant. */
2484 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2485 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2489 /* Argument types must be contravariant. */
2490 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
2491 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
2493 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
2494 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
2499 return (ltyp == rtyp);
2502 /* Past this point, we are only interested in ObjC class instances,
2503 or 'id' or 'Class'. */
2504 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2507 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2508 && !TYPE_HAS_OBJC_INFO (ltyp))
2511 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2512 && !TYPE_HAS_OBJC_INFO (rtyp))
2515 /* Past this point, we are committed to returning 'true' to the caller
2516 (unless performing a silent comparison; see below). However, we can
2517 still warn about type and/or protocol mismatches. */
2519 if (TYPE_HAS_OBJC_INFO (ltyp))
2521 lcls = TYPE_OBJC_INTERFACE (ltyp);
2522 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2525 lcls = lproto = NULL_TREE;
2527 if (TYPE_HAS_OBJC_INFO (rtyp))
2529 rcls = TYPE_OBJC_INTERFACE (rtyp);
2530 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2533 rcls = rproto = NULL_TREE;
2535 /* If we could not find an @interface declaration, we must have
2536 only seen a @class declaration; for purposes of type comparison,
2537 treat it as a stand-alone (root) class. */
2539 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2542 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2545 /* If either type is an unqualified 'id', we're done. This is because
2546 an 'id' can be assigned to or from any type with no warnings. */
2549 if ((!lproto && objc_is_object_id (ltyp))
2550 || (!rproto && objc_is_object_id (rtyp)))
2555 /* For property checks, though, an 'id' is considered the most
2556 general type of object, hence if you try to specialize an
2557 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2559 if (!lproto && objc_is_object_id (ltyp))
2563 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2565 /* If the underlying types are the same, and at most one of them has
2566 a protocol list, we do not need to issue any diagnostics. */
2567 if (pointers_compatible && (!lproto || !rproto))
2570 /* If exactly one of the types is 'Class', issue a diagnostic; any
2571 exceptions of this rule have already been handled. */
2572 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2573 pointers_compatible = false;
2574 /* Otherwise, check for inheritance relations. */
2577 if (!pointers_compatible)
2579 /* Again, if any of the two is an 'id', we're satisfied,
2580 unless we're comparing properties, in which case only an
2581 'id' on the left-hand side (old property) is good
2585 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2587 pointers_compatible = objc_is_object_id (ltyp);
2590 if (!pointers_compatible)
2591 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2593 if (!pointers_compatible && (argno == -3 || argno == -4))
2594 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2597 /* If the pointers match modulo protocols, check for protocol conformance
2599 if (pointers_compatible)
2601 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2604 if (!pointers_compatible && argno == -3)
2605 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2609 if (!pointers_compatible)
2611 /* The two pointers are not exactly compatible. Issue a warning, unless
2612 we are performing a silent comparison, in which case return 'false'
2614 /* NB: For the time being, we shall make our warnings look like their
2615 C counterparts. In the future, we may wish to make them more
2624 warning (0, "comparison of distinct Objective-C types lacks a cast");
2628 warning (0, "initialization from distinct Objective-C type");
2632 warning (0, "assignment from distinct Objective-C type");
2636 warning (0, "distinct Objective-C type in return");
2640 warning (0, "passing argument %d of %qE from distinct "
2641 "Objective-C type", argno, callee);
2649 /* This routine is similar to objc_compare_types except that function-pointers are
2650 excluded. This is because, caller assumes that common types are of (id, Object*)
2651 variety and calls objc_common_type to obtain a common type. There is no commonolty
2652 between two function-pointers in this regard. */
2655 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2657 if (objc_compare_types (ltyp, rtyp, argno, callee))
2659 /* exclude function-pointer types. */
2662 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2663 rtyp = TREE_TYPE (rtyp);
2665 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2666 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2672 /* Determine if CHILD is derived from PARENT. The routine assumes that
2673 both parameters are RECORD_TYPEs, and is non-reflexive. */
2676 objc_derived_from_p (tree parent, tree child)
2678 parent = TYPE_MAIN_VARIANT (parent);
2680 for (child = TYPE_MAIN_VARIANT (child);
2681 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2683 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2684 (TYPE_BINFO (child),
2687 if (child == parent)
2696 objc_build_component_ref (tree datum, tree component)
2698 /* If COMPONENT is NULL, the caller is referring to the anonymous
2699 base class field. */
2702 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2704 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2707 /* The 'build_component_ref' routine has been removed from the C++
2708 front-end, but 'finish_class_member_access_expr' seems to be
2709 a worthy substitute. */
2711 return finish_class_member_access_expr (datum, component, false,
2712 tf_warning_or_error);
2714 return build_component_ref (input_location, datum, component);
2718 /* Recursively copy inheritance information rooted at BINFO. To do this,
2719 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2722 objc_copy_binfo (tree binfo)
2724 tree btype = BINFO_TYPE (binfo);
2725 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2729 BINFO_TYPE (binfo2) = btype;
2730 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2731 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2733 /* Recursively copy base binfos of BINFO. */
2734 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2736 tree base_binfo2 = objc_copy_binfo (base_binfo);
2738 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2739 BINFO_BASE_APPEND (binfo2, base_binfo2);
2745 /* Record superclass information provided in BASETYPE for ObjC class REF.
2746 This is loosely based on cp/decl.c:xref_basetypes(). */
2749 objc_xref_basetypes (tree ref, tree basetype)
2751 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2753 TYPE_BINFO (ref) = binfo;
2754 BINFO_OFFSET (binfo) = size_zero_node;
2755 BINFO_TYPE (binfo) = ref;
2759 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2761 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2762 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
2763 BINFO_BASE_APPEND (binfo, base_binfo);
2764 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2768 /* Called from finish_decl. */
2771 objc_check_decl (tree decl)
2773 tree type = TREE_TYPE (decl);
2775 if (TREE_CODE (type) != RECORD_TYPE)
2777 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2778 error ("statically allocated instance of Objective-C class %qE",
2783 objc_check_global_decl (tree decl)
2785 tree id = DECL_NAME (decl);
2786 if (objc_is_class_name (id) && global_bindings_p())
2787 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2790 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2791 INTERFACE may either name an Objective-C class, or refer to the
2792 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2793 type, just return it unchanged. This function is often called when
2794 PROTOCOLS is NULL_TREE, in which case we simply look up the
2795 appropriate INTERFACE. */
2798 objc_get_protocol_qualified_type (tree interface, tree protocols)
2800 /* If INTERFACE is not provided, default to 'id'. */
2801 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2802 bool is_ptr = (type != NULL_TREE);
2806 type = objc_is_class_name (interface);
2810 /* If looking at a typedef, retrieve the precise type it
2812 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2813 interface = identifier_global_value (interface);
2815 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2816 && DECL_ORIGINAL_TYPE (interface))
2817 ? DECL_ORIGINAL_TYPE (interface)
2818 : xref_tag (RECORD_TYPE, type));
2822 /* This case happens when we are given an 'interface' which
2823 is not a valid class name. For example if a typedef was
2824 used, and 'interface' really is the identifier of the
2825 typedef, but when you resolve it you don't get an
2826 Objective-C class, but something else, such as 'int'.
2827 This is an error; protocols make no sense unless you use
2828 them with Objective-C objects. */
2829 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2831 /* Try to recover. Ignore the invalid class name, and treat
2832 the object as an 'id' to silence further warnings about
2834 type = objc_object_type;
2841 type = build_variant_type_copy (type);
2843 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2847 tree orig_pointee_type = TREE_TYPE (type);
2848 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2850 /* Set up the canonical type information. */
2851 TYPE_CANONICAL (type)
2852 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2854 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2855 type = TREE_TYPE (type);
2858 /* Look up protocols and install in lang specific list. */
2859 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2860 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
2862 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2863 return the pointer to the new pointee variant. */
2865 type = TYPE_POINTER_TO (type);
2867 TYPE_OBJC_INTERFACE (type)
2868 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2874 /* Check for circular dependencies in protocols. The arguments are
2875 PROTO, the protocol to check, and LIST, a list of protocol it
2879 check_protocol_recursively (tree proto, tree list)
2883 for (p = list; p; p = TREE_CHAIN (p))
2885 tree pp = TREE_VALUE (p);
2887 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2888 pp = lookup_protocol (pp, /* warn if deprecated */ false);
2891 fatal_error ("protocol %qE has circular dependency",
2892 PROTOCOL_NAME (pp));
2894 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2898 /* Look up PROTOCOLS, and return a list of those that are found. If
2899 none are found, return NULL. Note that this function will emit a
2900 warning if a protocol is found and is deprecated. */
2903 lookup_and_install_protocols (tree protocols)
2906 tree return_value = NULL_TREE;
2908 if (protocols == error_mark_node)
2911 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2913 tree ident = TREE_VALUE (proto);
2914 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true);
2917 return_value = chainon (return_value,
2918 build_tree_list (NULL_TREE, p));
2919 else if (ident != error_mark_node)
2920 error ("cannot find protocol declaration for %qE",
2924 return return_value;
2927 /* Create a declaration for field NAME of a given TYPE. */
2930 create_field_decl (tree type, const char *name)
2932 return build_decl (input_location,
2933 FIELD_DECL, get_identifier (name), type);
2936 /* Create a global, static declaration for variable NAME of a given TYPE. The
2937 finish_var_decl() routine will need to be called on it afterwards. */
2940 start_var_decl (tree type, const char *name)
2942 tree var = build_decl (input_location,
2943 VAR_DECL, get_identifier (name), type);
2945 TREE_STATIC (var) = 1;
2946 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
2947 DECL_IGNORED_P (var) = 1;
2948 DECL_ARTIFICIAL (var) = 1;
2949 DECL_CONTEXT (var) = NULL_TREE;
2951 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
2957 /* Finish off the variable declaration created by start_var_decl(). */
2960 finish_var_decl (tree var, tree initializer)
2962 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
2965 /* Find the decl for the constant string class reference. This is only
2966 used for the NeXT runtime. */
2969 setup_string_decl (void)
2974 /* %s in format will provide room for terminating null */
2975 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
2976 + strlen (constant_string_class_name);
2977 name = XNEWVEC (char, length);
2978 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
2979 constant_string_class_name);
2980 constant_string_global_id = get_identifier (name);
2981 string_class_decl = lookup_name (constant_string_global_id);
2983 return string_class_decl;
2986 /* Purpose: "play" parser, creating/installing representations
2987 of the declarations that are required by Objective-C.
2991 type_spec--------->sc_spec
2992 (tree_list) (tree_list)
2995 identifier_node identifier_node */
2998 synth_module_prologue (void)
3001 enum debug_info_type save_write_symbols = write_symbols;
3002 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
3004 /* Suppress outputting debug symbols, because
3005 dbxout_init hasn't been called yet. */
3006 write_symbols = NO_DEBUG;
3007 debug_hooks = &do_nothing_debug_hooks;
3010 push_lang_context (lang_name_c); /* extern "C" */
3013 /* The following are also defined in <objc/objc.h> and friends. */
3015 objc_object_id = get_identifier (TAG_OBJECT);
3016 objc_class_id = get_identifier (TAG_CLASS);
3018 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
3019 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
3021 objc_object_type = build_pointer_type (objc_object_reference);
3022 objc_class_type = build_pointer_type (objc_class_reference);
3024 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
3025 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
3027 /* Declare the 'id' and 'Class' typedefs. */
3029 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3033 TREE_NO_WARNING (type) = 1;
3034 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3038 TREE_NO_WARNING (type) = 1;
3040 /* Forward-declare '@interface Protocol'. */
3042 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
3043 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
3044 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
3047 /* Declare type of selector-objects that represent an operation name. */
3049 if (flag_next_runtime)
3050 /* `struct objc_selector *' */
3052 = build_pointer_type (xref_tag (RECORD_TYPE,
3053 get_identifier (TAG_SELECTOR)));
3055 /* `const struct objc_selector *' */
3057 = build_pointer_type
3058 (build_qualified_type (xref_tag (RECORD_TYPE,
3059 get_identifier (TAG_SELECTOR)),
3062 /* Declare receiver type used for dispatching messages to 'super'. */
3064 /* `struct objc_super *' */
3065 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
3066 get_identifier (TAG_SUPER)));
3068 /* Declare pointers to method and ivar lists. */
3069 objc_method_list_ptr = build_pointer_type
3070 (xref_tag (RECORD_TYPE,
3071 get_identifier (UTAG_METHOD_LIST)));
3072 objc_method_proto_list_ptr
3073 = build_pointer_type (xref_tag (RECORD_TYPE,
3074 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3075 objc_ivar_list_ptr = build_pointer_type
3076 (xref_tag (RECORD_TYPE,
3077 get_identifier (UTAG_IVAR_LIST)));
3079 /* TREE_NOTHROW is cleared for the message-sending functions,
3080 because the function that gets called can throw in Obj-C++, or
3081 could itself call something that can throw even in Obj-C. */
3083 if (flag_next_runtime)
3085 /* NB: In order to call one of the ..._stret (struct-returning)
3086 functions, the function *MUST* first be cast to a signature that
3087 corresponds to the actual ObjC method being invoked. This is
3088 what is done by the build_objc_method_call() routine below. */
3090 /* id objc_msgSend (id, SEL, ...); */
3091 /* id objc_msgSendNonNil (id, SEL, ...); */
3092 /* id objc_msgSend_stret (id, SEL, ...); */
3093 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
3095 = build_varargs_function_type_list (objc_object_type,
3099 umsg_decl = add_builtin_function (TAG_MSGSEND,
3100 type, 0, NOT_BUILT_IN,
3102 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
3103 type, 0, NOT_BUILT_IN,
3105 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
3106 type, 0, NOT_BUILT_IN,
3108 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
3109 type, 0, NOT_BUILT_IN,
3112 /* These can throw, because the function that gets called can throw
3113 in Obj-C++, or could itself call something that can throw even
3115 TREE_NOTHROW (umsg_decl) = 0;
3116 TREE_NOTHROW (umsg_nonnil_decl) = 0;
3117 TREE_NOTHROW (umsg_stret_decl) = 0;
3118 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
3120 /* id objc_msgSend_Fast (id, SEL, ...)
3121 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
3122 #ifdef OFFS_MSGSEND_FAST
3123 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
3124 type, 0, NOT_BUILT_IN,
3126 TREE_NOTHROW (umsg_fast_decl) = 0;
3127 DECL_ATTRIBUTES (umsg_fast_decl)
3128 = tree_cons (get_identifier ("hard_coded_address"),
3129 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
3132 /* No direct dispatch available. */
3133 umsg_fast_decl = umsg_decl;
3136 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
3137 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
3139 = build_varargs_function_type_list (objc_object_type,
3143 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
3144 type, 0, NOT_BUILT_IN,
3146 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
3147 type, 0, NOT_BUILT_IN, 0,
3149 TREE_NOTHROW (umsg_super_decl) = 0;
3150 TREE_NOTHROW (umsg_super_stret_decl) = 0;
3154 /* GNU runtime messenger entry points. */
3156 /* typedef id (*IMP)(id, SEL, ...); */
3158 build_varargs_function_type_list (objc_object_type,
3162 tree IMP_type = build_pointer_type (ftype);
3164 /* IMP objc_msg_lookup (id, SEL); */
3165 type = build_function_type_list (IMP_type,
3169 umsg_decl = add_builtin_function (TAG_MSGSEND,
3170 type, 0, NOT_BUILT_IN,
3172 TREE_NOTHROW (umsg_decl) = 0;
3174 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
3176 = build_function_type_list (IMP_type,
3180 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
3181 type, 0, NOT_BUILT_IN,
3183 TREE_NOTHROW (umsg_super_decl) = 0;
3185 /* The following GNU runtime entry point is called to initialize
3188 __objc_exec_class (void *); */
3190 = build_function_type_list (void_type_node,
3193 execclass_decl = add_builtin_function (TAG_EXECCLASS,
3194 type, 0, NOT_BUILT_IN,
3198 /* id objc_getClass (const char *); */
3200 type = build_function_type_list (objc_object_type,
3201 const_string_type_node,
3205 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
3208 /* id objc_getMetaClass (const char *); */
3210 objc_get_meta_class_decl
3211 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3213 build_class_template ();
3214 build_super_template ();
3215 build_protocol_template ();
3216 build_category_template ();
3217 build_objc_exception_stuff ();
3219 /* Declare objc_getProperty, object_setProperty and other property
3220 accessor helpers. */
3221 build_objc_property_accessor_helpers ();
3223 if (flag_next_runtime)
3224 build_next_objc_exception_stuff ();
3226 /* static SEL _OBJC_SELECTOR_TABLE[]; */
3228 if (! flag_next_runtime)
3229 build_selector_table_decl ();
3231 /* Forward declare constant_string_id and constant_string_type. */
3232 if (!constant_string_class_name)
3233 constant_string_class_name = default_constant_string_class_name;
3235 constant_string_id = get_identifier (constant_string_class_name);
3236 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
3238 /* Pre-build the following entities - for speed/convenience. */
3239 self_id = get_identifier ("self");
3240 ucmd_id = get_identifier ("_cmd");
3242 /* Declare struct _objc_fast_enumeration_state { ... }; */
3243 build_fast_enumeration_state_template ();
3245 /* void objc_enumeration_mutation (id) */
3246 type = build_function_type (void_type_node,
3247 tree_cons (NULL_TREE, objc_object_type, NULL_TREE));
3248 objc_enumeration_mutation_decl
3249 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3251 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3254 pop_lang_context ();
3257 write_symbols = save_write_symbols;
3258 debug_hooks = save_hooks;
3261 /* Ensure that the ivar list for NSConstantString/NXConstantString
3262 (or whatever was specified via `-fconstant-string-class')
3263 contains fields at least as large as the following three, so that
3264 the runtime can stomp on them with confidence:
3266 struct STRING_OBJECT_CLASS_NAME
3270 unsigned int length;
3274 check_string_class_template (void)
3276 tree field_decl = objc_get_class_ivars (constant_string_id);
3278 #define AT_LEAST_AS_LARGE_AS(F, T) \
3279 (F && TREE_CODE (F) == FIELD_DECL \
3280 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3281 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3283 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3286 field_decl = DECL_CHAIN (field_decl);
3287 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3290 field_decl = DECL_CHAIN (field_decl);
3291 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3293 #undef AT_LEAST_AS_LARGE_AS
3296 /* Avoid calling `check_string_class_template ()' more than once. */
3297 static GTY(()) int string_layout_checked;
3299 /* Construct an internal string layout to be used as a template for
3300 creating NSConstantString/NXConstantString instances. */
3303 objc_build_internal_const_str_type (void)
3305 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3306 tree fields = build_decl (input_location,
3307 FIELD_DECL, NULL_TREE, ptr_type_node);
3308 tree field = build_decl (input_location,
3309 FIELD_DECL, NULL_TREE, ptr_type_node);
3311 DECL_CHAIN (field) = fields; fields = field;
3312 field = build_decl (input_location,
3313 FIELD_DECL, NULL_TREE, unsigned_type_node);
3314 DECL_CHAIN (field) = fields; fields = field;
3315 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3317 finish_builtin_struct (type, "__builtin_ObjCString",
3323 /* Custom build_string which sets TREE_TYPE! */
3326 my_build_string (int len, const char *str)
3328 return fix_string_type (build_string (len, str));
3331 /* Build a string with contents STR and length LEN and convert it to a
3335 my_build_string_pointer (int len, const char *str)
3337 tree string = my_build_string (len, str);
3338 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3339 return build1 (ADDR_EXPR, ptrtype, string);
3343 string_hash (const void *ptr)
3345 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
3346 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3347 int i, len = TREE_STRING_LENGTH (str);
3350 for (i = 0; i < len; i++)
3351 h = ((h * 613) + p[i]);
3357 string_eq (const void *ptr1, const void *ptr2)
3359 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
3360 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
3361 int len1 = TREE_STRING_LENGTH (str1);
3363 return (len1 == TREE_STRING_LENGTH (str2)
3364 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3368 /* Given a chain of STRING_CST's, build a static instance of
3369 NXConstantString which points at the concatenation of those
3370 strings. We place the string object in the __string_objects
3371 section of the __OBJC segment. The Objective-C runtime will
3372 initialize the isa pointers of the string objects to point at the
3373 NXConstantString class object. */
3376 objc_build_string_object (tree string)
3378 tree constant_string_class;
3381 struct string_descriptor *desc, key;
3384 /* Prep the string argument. */
3385 string = fix_string_type (string);
3386 TREE_SET_CODE (string, STRING_CST);
3387 length = TREE_STRING_LENGTH (string) - 1;
3389 /* The target may have different ideas on how to construct an ObjC string
3390 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3391 constant CFString reference instead.
3392 At present, this is only supported for the NeXT runtime. */
3393 if (flag_next_runtime && targetcm.objc_construct_string_object)
3395 tree constructor = (*targetcm.objc_construct_string_object) (string);
3397 return build1 (NOP_EXPR, objc_object_type, constructor);
3400 /* Check whether the string class being used actually exists and has the
3401 correct ivar layout. */
3402 if (!string_layout_checked)
3404 string_layout_checked = -1;
3405 constant_string_class = lookup_interface (constant_string_id);
3406 internal_const_str_type = objc_build_internal_const_str_type ();
3408 if (!constant_string_class
3409 || !(constant_string_type
3410 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3411 error ("cannot find interface declaration for %qE",
3412 constant_string_id);
3413 /* The NSConstantString/NXConstantString ivar layout is now known. */
3414 else if (!check_string_class_template ())
3415 error ("interface %qE does not have valid constant string layout",
3416 constant_string_id);
3417 /* For the NeXT runtime, we can generate a literal reference
3418 to the string class, don't need to run a constructor. */
3419 else if (flag_next_runtime && !setup_string_decl ())
3420 error ("cannot find reference tag for class %qE",
3421 constant_string_id);
3424 string_layout_checked = 1; /* Success! */
3425 add_class_reference (constant_string_id);
3429 if (string_layout_checked == -1)
3430 return error_mark_node;
3432 /* Perhaps we already constructed a constant string just like this one? */
3433 key.literal = string;
3434 loc = htab_find_slot (string_htab, &key, INSERT);
3435 desc = (struct string_descriptor *) *loc;
3439 tree var, constructor;
3440 VEC(constructor_elt,gc) *v = NULL;
3441 *loc = desc = ggc_alloc_string_descriptor ();
3442 desc->literal = string;
3444 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
3445 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
3446 fields = TYPE_FIELDS (internal_const_str_type);
3447 CONSTRUCTOR_APPEND_ELT (v, fields,
3449 ? build_unary_op (input_location,
3450 ADDR_EXPR, string_class_decl, 0)
3451 : build_int_cst (NULL_TREE, 0));
3452 fields = DECL_CHAIN (fields);
3453 CONSTRUCTOR_APPEND_ELT (v, fields,
3454 build_unary_op (input_location,
3455 ADDR_EXPR, string, 1));
3456 fields = DECL_CHAIN (fields);
3457 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
3458 constructor = objc_build_constructor (internal_const_str_type, v);
3460 if (!flag_next_runtime)
3462 = objc_add_static_instance (constructor, constant_string_type);
3465 var = build_decl (input_location,
3466 CONST_DECL, NULL, TREE_TYPE (constructor));
3467 DECL_INITIAL (var) = constructor;
3468 TREE_STATIC (var) = 1;
3469 pushdecl_top_level (var);
3472 desc->constructor = constructor;
3475 addr = convert (build_pointer_type (constant_string_type),
3476 build_unary_op (input_location,
3477 ADDR_EXPR, desc->constructor, 1));
3482 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
3484 static GTY(()) int num_static_inst;
3487 objc_add_static_instance (tree constructor, tree class_decl)
3492 /* Find the list of static instances for the CLASS_DECL. Create one if
3494 for (chain = &objc_static_instances;
3495 *chain && TREE_VALUE (*chain) != class_decl;
3496 chain = &TREE_CHAIN (*chain));
3499 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
3500 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
3503 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
3504 decl = build_decl (input_location,
3505 VAR_DECL, get_identifier (buf), class_decl);
3506 TREE_STATIC (decl) = 1;
3507 DECL_ARTIFICIAL (decl) = 1;
3508 TREE_USED (decl) = 1;
3509 DECL_INITIAL (decl) = constructor;
3511 /* We may be writing something else just now.
3512 Postpone till end of input. */
3513 DECL_DEFER_OUTPUT (decl) = 1;
3514 pushdecl_top_level (decl);
3515 rest_of_decl_compilation (decl, 1, 0);
3517 /* Add the DECL to the head of this CLASS' list. */
3518 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
3523 /* Build a static constant CONSTRUCTOR
3524 with type TYPE and elements ELTS. */
3527 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
3529 tree constructor = build_constructor (type, elts);
3531 TREE_CONSTANT (constructor) = 1;
3532 TREE_STATIC (constructor) = 1;
3533 TREE_READONLY (constructor) = 1;
3536 /* Adjust for impedance mismatch. We should figure out how to build
3537 CONSTRUCTORs that consistently please both the C and C++ gods. */
3538 if (!VEC_index (constructor_elt, elts, 0)->index)
3539 TREE_TYPE (constructor) = init_list_type_node;
3545 /* Take care of defining and initializing _OBJC_SYMBOLS. */
3547 /* Predefine the following data type:
3555 void *defs[cls_def_cnt + cat_def_cnt];
3559 build_objc_symtab_template (void)
3561 tree fields, *chain = NULL;
3563 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
3565 /* long sel_ref_cnt; */
3566 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
3569 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
3571 /* short cls_def_cnt; */
3572 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
3574 /* short cat_def_cnt; */
3575 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
3577 if (imp_count || cat_count || !flag_next_runtime)
3579 /* void *defs[imp_count + cat_count (+ 1)]; */
3580 /* NB: The index is one less than the size of the array. */
3581 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
3582 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
3583 add_field_decl (array_type, "defs", &chain);
3586 objc_finish_struct (objc_symtab_template, fields);
3589 /* Create the initial value for the `defs' field of _objc_symtab.
3590 This is a CONSTRUCTOR. */
3593 init_def_list (tree type)
3596 struct imp_entry *impent;
3597 VEC(constructor_elt,gc) *v = NULL;
3600 for (impent = imp_list; impent; impent = impent->next)
3602 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
3604 expr = build_unary_op (input_location,
3605 ADDR_EXPR, impent->class_decl, 0);
3606 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3611 for (impent = imp_list; impent; impent = impent->next)
3613 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3615 expr = build_unary_op (input_location,
3616 ADDR_EXPR, impent->class_decl, 0);
3617 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3621 if (!flag_next_runtime)
3623 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
3624 if (static_instances_decl)
3625 expr = build_unary_op (input_location,
3626 ADDR_EXPR, static_instances_decl, 0);
3628 expr = integer_zero_node;
3630 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3633 return objc_build_constructor (type, v);
3636 /* Construct the initial value for all of _objc_symtab. */
3639 init_objc_symtab (tree type)
3641 VEC(constructor_elt,gc) *v = NULL;
3643 /* sel_ref_cnt = { ..., 5, ... } */
3645 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3646 build_int_cst (long_integer_type_node, 0));
3648 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
3650 if (flag_next_runtime || ! sel_ref_chain)
3651 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
3652 build_pointer_type (objc_selector_type),
3653 integer_zero_node));
3656 tree expr = build_unary_op (input_location, ADDR_EXPR,
3657 UOBJC_SELECTOR_TABLE_decl, 1);
3659 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3660 convert (build_pointer_type (objc_selector_type),
3664 /* cls_def_cnt = { ..., 5, ... } */
3666 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3667 build_int_cst (short_integer_type_node, imp_count));
3669 /* cat_def_cnt = { ..., 5, ... } */
3671 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3672 build_int_cst (short_integer_type_node, cat_count));
3674 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
3676 if (imp_count || cat_count || !flag_next_runtime)
3679 tree field = TYPE_FIELDS (type);
3680 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
3682 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
3685 return objc_build_constructor (type, v);
3688 /* Generate forward declarations for metadata such as
3689 'OBJC_CLASS_...'. */
3692 build_metadata_decl (const char *name, tree type)
3696 /* struct TYPE NAME_<name>; */
3697 decl = start_var_decl (type, synth_id_with_class_suffix
3699 objc_implementation_context));
3704 /* Push forward-declarations of all the categories so that
3705 init_def_list can use them in a CONSTRUCTOR. */
3708 forward_declare_categories (void)
3710 struct imp_entry *impent;
3711 tree sav = objc_implementation_context;
3713 for (impent = imp_list; impent; impent = impent->next)
3715 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
3717 /* Set an invisible arg to synth_id_with_class_suffix. */
3718 objc_implementation_context = impent->imp_context;
3719 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
3720 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
3721 objc_category_template);
3724 objc_implementation_context = sav;
3727 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
3728 and initialized appropriately. */
3731 generate_objc_symtab_decl (void)
3734 build_objc_symtab_template ();
3735 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
3736 finish_var_decl (UOBJC_SYMBOLS_decl,
3737 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
3741 init_module_descriptor (tree type)
3744 VEC(constructor_elt,gc) *v = NULL;
3746 /* version = { 1, ... } */
3748 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
3749 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3751 /* size = { ..., sizeof (struct _objc_module), ... } */
3753 expr = convert (long_integer_type_node,
3754 size_in_bytes (objc_module_template));
3755 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3757 /* Don't provide any file name for security reasons. */
3758 /* name = { ..., "", ... } */
3760 expr = add_objc_string (get_identifier (""), class_names);
3761 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3763 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
3765 if (UOBJC_SYMBOLS_decl)
3766 expr = build_unary_op (input_location,
3767 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
3769 expr = null_pointer_node;
3770 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3772 return objc_build_constructor (type, v);
3775 /* Write out the data structures to describe Objective C classes defined.
3777 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
3780 build_module_descriptor (void)
3782 tree decls, *chain = NULL;
3785 push_lang_context (lang_name_c); /* extern "C" */
3788 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
3791 decls = add_field_decl (long_integer_type_node, "version", &chain);
3794 add_field_decl (long_integer_type_node, "size", &chain);
3797 add_field_decl (string_type_node, "name", &chain);
3799 /* struct _objc_symtab *symtab; */
3800 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
3801 get_identifier (UTAG_SYMTAB))),
3804 objc_finish_struct (objc_module_template, decls);
3806 /* Create an instance of "_objc_module". */
3807 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
3808 /* This is the root of the metadata for defined classes and categories, it
3809 is referenced by the runtime and, therefore, needed. */
3810 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
3811 finish_var_decl (UOBJC_MODULES_decl,
3812 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
3815 pop_lang_context ();
3819 /* The GNU runtime requires us to provide a static initializer function
3822 static void __objc_gnu_init (void) {
3823 __objc_exec_class (&L_OBJC_MODULES);
3827 build_module_initializer_routine (void)
3832 push_lang_context (lang_name_c); /* extern "C" */
3835 objc_push_parm (build_decl (input_location,
3836 PARM_DECL, NULL_TREE, void_type_node));
3838 objc_start_function (get_identifier (TAG_GNUINIT),
3839 build_function_type_list (void_type_node, NULL_TREE),
3840 NULL_TREE, NULL_TREE);
3842 objc_start_function (get_identifier (TAG_GNUINIT),
3843 build_function_type_list (void_type_node, NULL_TREE),
3844 NULL_TREE, objc_get_parm_info (0));
3846 body = c_begin_compound_stmt (true);
3847 add_stmt (build_function_call
3852 build_unary_op (input_location, ADDR_EXPR,
3853 UOBJC_MODULES_decl, 0))));
3854 add_stmt (c_end_compound_stmt (input_location, body, true));
3856 TREE_PUBLIC (current_function_decl) = 0;
3859 /* For Objective-C++, we will need to call __objc_gnu_init
3860 from objc_generate_static_init_call() below. */
3861 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
3864 GNU_INIT_decl = current_function_decl;
3868 pop_lang_context ();
3873 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
3874 to be called by the module initializer routine. */
3877 objc_static_init_needed_p (void)
3879 return (GNU_INIT_decl != NULL_TREE);
3882 /* Generate a call to the __objc_gnu_init initializer function. */
3885 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
3887 add_stmt (build_stmt (input_location, EXPR_STMT,
3888 build_function_call (input_location,
3889 GNU_INIT_decl, NULL_TREE)));
3893 #endif /* OBJCPLUS */
3895 /* Return the DECL of the string IDENT in the SECTION. */
3898 get_objc_string_decl (tree ident, enum string_section section)
3905 chain = class_names_chain;
3907 case meth_var_names:
3908 chain = meth_var_names_chain;
3910 case meth_var_types:
3911 chain = meth_var_types_chain;
3917 for (; chain != 0; chain = TREE_CHAIN (chain))
3918 if (TREE_VALUE (chain) == ident)
3919 return (TREE_PURPOSE (chain));
3925 /* Output references to all statically allocated objects. Return the DECL
3926 for the array built. */
3929 generate_static_references (void)
3931 tree expr = NULL_TREE;
3932 tree class_name, klass, decl;
3933 tree cl_chain, in_chain, type
3934 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
3935 int num_inst, num_class;
3937 VEC(constructor_elt,gc) *decls = NULL;
3939 if (flag_next_runtime)
3942 for (cl_chain = objc_static_instances, num_class = 0;
3943 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
3945 VEC(constructor_elt,gc) *v = NULL;
3947 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
3948 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
3950 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
3951 decl = start_var_decl (type, buf);
3953 /* Output {class_name, ...}. */
3954 klass = TREE_VALUE (cl_chain);
3955 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
3956 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
3957 build_unary_op (input_location,
3958 ADDR_EXPR, class_name, 1));
3960 /* Output {..., instance, ...}. */
3961 for (in_chain = TREE_PURPOSE (cl_chain);
3962 in_chain; in_chain = TREE_CHAIN (in_chain))
3964 expr = build_unary_op (input_location,
3965 ADDR_EXPR, TREE_VALUE (in_chain), 1);
3966 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
3969 /* Output {..., NULL}. */
3970 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
3972 expr = objc_build_constructor (TREE_TYPE (decl), v);
3973 finish_var_decl (decl, expr);
3974 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
3975 build_unary_op (input_location,
3976 ADDR_EXPR, decl, 1));
3979 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
3980 expr = objc_build_constructor (type, decls);
3981 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
3982 finish_var_decl (static_instances_decl, expr);
3985 static GTY(()) int selector_reference_idx;
3988 build_selector_reference_decl (void)
3993 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
3994 decl = start_var_decl (objc_selector_type, buf);
4000 build_selector_table_decl (void)
4004 if (flag_typed_selectors)
4006 build_selector_template ();
4007 temp = build_array_type (objc_selector_template, NULL_TREE);
4010 temp = build_array_type (objc_selector_type, NULL_TREE);
4012 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
4015 /* Just a handy wrapper for add_objc_string. */
4018 build_selector (tree ident)
4020 return convert (objc_selector_type,
4021 add_objc_string (ident, meth_var_names));