1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Steve Naroff.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-pragma.h"
39 #include "langhooks.h"
48 #include "diagnostic-core.h"
51 #include "tree-iterator.h"
53 #include "langhooks-def.h"
55 /* For enum gimplify_status */
58 #define OBJC_VOID_AT_END void_list_node
60 static unsigned int should_call_super_dealloc
= 0;
62 /* When building Objective-C++, we need in_late_binary_op. */
64 bool in_late_binary_op
= false;
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 /* I am not sure it is really correct.
76 Perhaps there's a danger that it will make name conflicts
77 if method names contain underscores. -- rms. */
78 #ifndef OBJC_GEN_METHOD_LABEL
79 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
82 sprintf ((BUF), "_%s_%s_%s_%s", \
83 ((IS_INST) ? "i" : "c"), \
85 ((CAT_NAME)? (CAT_NAME) : ""), \
87 for (temp = (BUF); *temp; temp++) \
88 if (*temp == ':') *temp = '_'; \
92 /* These need specifying. */
93 #ifndef OBJC_FORWARDING_STACK_OFFSET
94 #define OBJC_FORWARDING_STACK_OFFSET 0
97 #ifndef OBJC_FORWARDING_MIN_OFFSET
98 #define OBJC_FORWARDING_MIN_OFFSET 0
101 /* Set up for use of obstacks. */
105 /* This obstack is used to accumulate the encoding of a data type. */
106 static struct obstack util_obstack
;
108 /* This points to the beginning of obstack contents, so we can free
109 the whole contents. */
112 /* The version identifies which language generation and runtime
113 the module (file) was compiled for, and is recorded in the
114 module descriptor. */
116 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
117 #define PROTOCOL_VERSION 2
119 /* (Decide if these can ever be validly changed.) */
120 #define OBJC_ENCODE_INLINE_DEFS 0
121 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
123 /*** Private Interface (procedures) ***/
125 /* Used by compile_file. */
127 static void init_objc (void);
128 static void finish_objc (void);
130 /* Code generation. */
132 static tree
objc_build_constructor (tree
, VEC(constructor_elt
,gc
) *);
133 static tree
build_objc_method_call (location_t
, int, tree
, tree
, tree
, tree
);
134 static tree
get_proto_encoding (tree
);
135 static tree
lookup_interface (tree
);
136 static tree
objc_add_static_instance (tree
, tree
);
138 static tree
start_class (enum tree_code
, tree
, tree
, tree
);
139 static tree
continue_class (tree
);
140 static void finish_class (tree
);
141 static void start_method_def (tree
);
143 static void objc_start_function (tree
, tree
, tree
, tree
);
145 static void objc_start_function (tree
, tree
, tree
, struct c_arg_info
*);
147 static tree
start_protocol (enum tree_code
, tree
, tree
);
148 static tree
build_method_decl (enum tree_code
, tree
, tree
, tree
, bool);
149 static tree
objc_add_method (tree
, tree
, int);
150 static tree
add_instance_variable (tree
, int, tree
);
151 static tree
build_ivar_reference (tree
);
152 static tree
is_ivar (tree
, tree
);
154 static void build_objc_exception_stuff (void);
155 static void build_next_objc_exception_stuff (void);
157 /* We only need the following for ObjC; ObjC++ will use C++'s definition
158 of DERIVED_FROM_P. */
160 static bool objc_derived_from_p (tree
, tree
);
161 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
163 static void objc_xref_basetypes (tree
, tree
);
165 static void build_class_template (void);
166 static void build_selector_template (void);
167 static void build_category_template (void);
168 static void build_super_template (void);
169 static tree
build_protocol_initializer (tree
, tree
, tree
, tree
, tree
);
170 static tree
get_class_ivars (tree
, bool);
171 static tree
generate_protocol_list (tree
);
172 static void build_protocol_reference (tree
);
175 static void objc_generate_cxx_cdtors (void);
178 static const char *synth_id_with_class_suffix (const char *, tree
);
180 /* Hash tables to manage the global pool of method prototypes. */
182 hash
*nst_method_hash_list
= 0;
183 hash
*cls_method_hash_list
= 0;
185 static hash
hash_lookup (hash
*, tree
);
186 static tree
lookup_method (tree
, tree
);
187 static tree
lookup_method_static (tree
, tree
, int);
191 class_names
, /* class, category, protocol, module names */
192 meth_var_names
, /* method and variable names */
193 meth_var_types
/* method and variable type descriptors */
196 static tree
add_objc_string (tree
, enum string_section
);
197 static void build_selector_table_decl (void);
199 /* Protocol additions. */
201 static tree
lookup_protocol (tree
);
202 static tree
lookup_and_install_protocols (tree
);
206 static void encode_type_qualifiers (tree
);
207 static void encode_type (tree
, int, int);
208 static void encode_field_decl (tree
, int, int);
211 static void really_start_method (tree
, tree
);
213 static void really_start_method (tree
, struct c_arg_info
*);
215 static int comp_proto_with_proto (tree
, tree
, int);
216 static void objc_push_parm (tree
);
218 static tree
objc_get_parm_info (int);
220 static struct c_arg_info
*objc_get_parm_info (int);
223 /* Utilities for debugging and error diagnostics. */
225 static char *gen_type_name (tree
);
226 static char *gen_type_name_0 (tree
);
227 static char *gen_method_decl (tree
);
228 static char *gen_declaration (tree
);
230 /* Everything else. */
232 static tree
create_field_decl (tree
, const char *);
233 static void add_class_reference (tree
);
234 static void build_protocol_template (void);
235 static tree
encode_method_prototype (tree
);
236 static void generate_classref_translation_entry (tree
);
237 static void handle_class_ref (tree
);
238 static void generate_struct_by_value_array (void)
240 static void mark_referenced_methods (void);
241 static void generate_objc_image_info (void);
243 /*** Private Interface (data) ***/
245 /* Reserved tag definitions. */
247 #define OBJECT_TYPEDEF_NAME "id"
248 #define CLASS_TYPEDEF_NAME "Class"
250 #define TAG_OBJECT "objc_object"
251 #define TAG_CLASS "objc_class"
252 #define TAG_SUPER "objc_super"
253 #define TAG_SELECTOR "objc_selector"
255 #define UTAG_CLASS "_objc_class"
256 #define UTAG_IVAR "_objc_ivar"
257 #define UTAG_IVAR_LIST "_objc_ivar_list"
258 #define UTAG_METHOD "_objc_method"
259 #define UTAG_METHOD_LIST "_objc_method_list"
260 #define UTAG_CATEGORY "_objc_category"
261 #define UTAG_MODULE "_objc_module"
262 #define UTAG_SYMTAB "_objc_symtab"
263 #define UTAG_SUPER "_objc_super"
264 #define UTAG_SELECTOR "_objc_selector"
266 #define UTAG_PROTOCOL "_objc_protocol"
267 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
268 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
270 /* Note that the string object global name is only needed for the
272 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
274 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
276 static const char *TAG_GETCLASS
;
277 static const char *TAG_GETMETACLASS
;
278 static const char *TAG_MSGSEND
;
279 static const char *TAG_MSGSENDSUPER
;
280 /* The NeXT Objective-C messenger may have two extra entry points, for use
281 when returning a structure. */
282 static const char *TAG_MSGSEND_STRET
;
283 static const char *TAG_MSGSENDSUPER_STRET
;
284 static const char *default_constant_string_class_name
;
286 /* Runtime metadata flags. */
287 #define CLS_FACTORY 0x0001L
288 #define CLS_META 0x0002L
289 #define CLS_HAS_CXX_STRUCTORS 0x2000L
291 #define OBJC_MODIFIER_STATIC 0x00000001
292 #define OBJC_MODIFIER_FINAL 0x00000002
293 #define OBJC_MODIFIER_PUBLIC 0x00000004
294 #define OBJC_MODIFIER_PRIVATE 0x00000008
295 #define OBJC_MODIFIER_PROTECTED 0x00000010
296 #define OBJC_MODIFIER_NATIVE 0x00000020
297 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
298 #define OBJC_MODIFIER_ABSTRACT 0x00000080
299 #define OBJC_MODIFIER_VOLATILE 0x00000100
300 #define OBJC_MODIFIER_TRANSIENT 0x00000200
301 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
303 /* NeXT-specific tags. */
305 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
306 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
307 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
308 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
309 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
310 #define TAG_EXCEPTIONMATCH "objc_exception_match"
311 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
312 #define TAG_SYNCENTER "objc_sync_enter"
313 #define TAG_SYNCEXIT "objc_sync_exit"
314 #define TAG_SETJMP "_setjmp"
315 #define UTAG_EXCDATA "_objc_exception_data"
317 #define TAG_ASSIGNIVAR "objc_assign_ivar"
318 #define TAG_ASSIGNGLOBAL "objc_assign_global"
319 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
321 /* Branch entry points. All that matters here are the addresses;
322 functions with these names do not really exist in libobjc. */
324 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
325 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
327 #define TAG_CXX_CONSTRUCT ".cxx_construct"
328 #define TAG_CXX_DESTRUCT ".cxx_destruct"
330 /* GNU-specific tags. */
332 #define TAG_EXECCLASS "__objc_exec_class"
333 #define TAG_GNUINIT "__objc_gnu_init"
335 /* Flags for lookup_method_static(). */
336 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
337 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
339 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
340 tree objc_global_trees
[OCTI_MAX
];
342 static void handle_impent (struct imp_entry
*);
344 struct imp_entry
*imp_list
= 0;
345 int imp_count
= 0; /* `@implementation' */
346 int cat_count
= 0; /* `@category' */
348 enum tree_code objc_inherit_code
;
349 int objc_public_flag
;
351 /* Use to generate method labels. */
352 static int method_slot
= 0;
354 static int objc_collecting_ivars
= 0;
358 static char *errbuf
; /* Buffer for error diagnostics */
360 /* Data imported from tree.c. */
362 extern enum debug_info_type write_symbols
;
364 /* Data imported from toplev.c. */
366 extern const char *dump_base_name
;
368 static int flag_typed_selectors
;
370 /* Store all constructed constant strings in a hash table so that
371 they get uniqued properly. */
373 struct GTY(()) string_descriptor
{
374 /* The literal argument . */
377 /* The resulting constant string. */
381 static GTY((param_is (struct string_descriptor
))) htab_t string_htab
;
383 /* Store the EH-volatilized types in a hash table, for easy retrieval. */
384 struct GTY(()) volatilized_type
{
388 static GTY((param_is (struct volatilized_type
))) htab_t volatilized_htab
;
390 FILE *gen_declaration_file
;
392 /* Tells "encode_pointer/encode_aggregate" whether we are generating
393 type descriptors for instance variables (as opposed to methods).
394 Type descriptors for instance variables contain more information
395 than methods (for static typing and embedded structures). */
397 static int generating_instance_variables
= 0;
399 /* For building an objc struct. These may not be used when this file
400 is compiled as part of obj-c++. */
402 static bool objc_building_struct
;
403 static struct c_struct_parse_info
*objc_struct_info ATTRIBUTE_UNUSED
;
405 /* Start building a struct for objc. */
408 objc_start_struct (tree name
)
410 gcc_assert (!objc_building_struct
);
411 objc_building_struct
= true;
412 return start_struct (input_location
, RECORD_TYPE
, name
, &objc_struct_info
);
415 /* Finish building a struct for objc. */
418 objc_finish_struct (tree type
, tree fieldlist
)
420 gcc_assert (objc_building_struct
);
421 objc_building_struct
= false;
422 return finish_struct (input_location
, type
, fieldlist
, NULL_TREE
,
427 build_sized_array_type (tree base_type
, int size
)
429 tree index_type
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
430 return build_array_type (base_type
, index_type
);
434 add_field_decl (tree type
, const char *name
, tree
**chain
)
436 tree field
= create_field_decl (type
, name
);
440 *chain
= &DECL_CHAIN (field
);
445 /* Some platforms pass small structures through registers versus
446 through an invisible pointer. Determine at what size structure is
447 the transition point between the two possibilities. */
450 generate_struct_by_value_array (void)
455 int aggregate_in_mem
[32];
458 /* Presumably no platform passes 32 byte structures in a register. */
459 for (i
= 1; i
< 32; i
++)
464 /* Create an unnamed struct that has `i' character components */
465 type
= objc_start_struct (NULL_TREE
);
467 strcpy (buffer
, "c1");
468 decls
= add_field_decl (char_type_node
, buffer
, &chain
);
470 for (j
= 1; j
< i
; j
++)
472 sprintf (buffer
, "c%d", j
+ 1);
473 add_field_decl (char_type_node
, buffer
, &chain
);
475 objc_finish_struct (type
, decls
);
477 aggregate_in_mem
[i
] = aggregate_value_p (type
, 0);
478 if (!aggregate_in_mem
[i
])
482 /* We found some structures that are returned in registers instead of memory
483 so output the necessary data. */
486 for (i
= 31; i
>= 0; i
--)
487 if (!aggregate_in_mem
[i
])
489 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i
);
491 /* The first member of the structure is always 0 because we don't handle
492 structures with 0 members */
493 printf ("static int struct_forward_array[] = {\n 0");
495 for (j
= 1; j
<= i
; j
++)
496 printf (", %d", aggregate_in_mem
[j
]);
507 if (cxx_init () == false)
509 if (c_objc_common_init () == false)
513 /* If gen_declaration desired, open the output file. */
514 if (flag_gen_declaration
)
516 register char * const dumpname
= concat (dump_base_name
, ".decl", NULL
);
517 gen_declaration_file
= fopen (dumpname
, "w");
518 if (gen_declaration_file
== 0)
519 fatal_error ("can't open %s: %m", dumpname
);
523 if (flag_next_runtime
)
525 TAG_GETCLASS
= "objc_getClass";
526 TAG_GETMETACLASS
= "objc_getMetaClass";
527 TAG_MSGSEND
= "objc_msgSend";
528 TAG_MSGSENDSUPER
= "objc_msgSendSuper";
529 TAG_MSGSEND_STRET
= "objc_msgSend_stret";
530 TAG_MSGSENDSUPER_STRET
= "objc_msgSendSuper_stret";
531 default_constant_string_class_name
= "NSConstantString";
535 TAG_GETCLASS
= "objc_get_class";
536 TAG_GETMETACLASS
= "objc_get_meta_class";
537 TAG_MSGSEND
= "objc_msg_lookup";
538 TAG_MSGSENDSUPER
= "objc_msg_lookup_super";
539 /* GNU runtime does not provide special functions to support
540 structure-returning methods. */
541 default_constant_string_class_name
= "NXConstantString";
542 flag_typed_selectors
= 1;
543 /* GNU runtime does not need the compiler to change code
544 in order to do GC. */
547 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
554 if (print_struct_values
&& !flag_compare_debug
)
555 generate_struct_by_value_array ();
561 objc_finish_file (void)
563 mark_referenced_methods ();
566 /* We need to instantiate templates _before_ we emit ObjC metadata;
567 if we do not, some metadata (such as selectors) may go missing. */
569 instantiate_pending_templates (0);
572 /* Finalize Objective-C runtime data. No need to generate tables
573 and code if only checking syntax, or if generating a PCH file. */
574 if (!flag_syntax_only
&& !pch_file
)
577 if (gen_declaration_file
)
578 fclose (gen_declaration_file
);
581 /* Return the first occurrence of a method declaration corresponding
582 to sel_name in rproto_list. Search rproto_list recursively.
583 If is_class is 0, search for instance methods, otherwise for class
586 lookup_method_in_protocol_list (tree rproto_list
, tree sel_name
,
592 for (rproto
= rproto_list
; rproto
; rproto
= TREE_CHAIN (rproto
))
594 p
= TREE_VALUE (rproto
);
596 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
598 if ((fnd
= lookup_method (is_class
599 ? PROTOCOL_CLS_METHODS (p
)
600 : PROTOCOL_NST_METHODS (p
), sel_name
)))
602 else if (PROTOCOL_LIST (p
))
603 fnd
= lookup_method_in_protocol_list (PROTOCOL_LIST (p
),
608 ; /* An identifier...if we could not find a protocol. */
619 lookup_protocol_in_reflist (tree rproto_list
, tree lproto
)
623 /* Make sure the protocol is supported by the object on the rhs. */
624 if (TREE_CODE (lproto
) == PROTOCOL_INTERFACE_TYPE
)
627 for (rproto
= rproto_list
; rproto
; rproto
= TREE_CHAIN (rproto
))
629 p
= TREE_VALUE (rproto
);
631 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
636 else if (PROTOCOL_LIST (p
))
637 fnd
= lookup_protocol_in_reflist (PROTOCOL_LIST (p
), lproto
);
646 ; /* An identifier...if we could not find a protocol. */
653 objc_start_class_interface (tree klass
, tree super_class
, tree protos
)
655 objc_interface_context
657 = start_class (CLASS_INTERFACE_TYPE
, klass
, super_class
, protos
);
658 objc_public_flag
= 0;
662 objc_start_category_interface (tree klass
, tree categ
, tree protos
)
664 objc_interface_context
665 = start_class (CATEGORY_INTERFACE_TYPE
, klass
, categ
, protos
);
667 = continue_class (objc_interface_context
);
671 objc_start_protocol (tree name
, tree protos
)
673 objc_interface_context
674 = start_protocol (PROTOCOL_INTERFACE_TYPE
, name
, protos
);
678 objc_continue_interface (void)
681 = continue_class (objc_interface_context
);
685 objc_finish_interface (void)
687 finish_class (objc_interface_context
);
688 objc_interface_context
= NULL_TREE
;
692 objc_start_class_implementation (tree klass
, tree super_class
)
694 objc_implementation_context
696 = start_class (CLASS_IMPLEMENTATION_TYPE
, klass
, super_class
, NULL_TREE
);
697 objc_public_flag
= 0;
701 objc_start_category_implementation (tree klass
, tree categ
)
703 objc_implementation_context
704 = start_class (CATEGORY_IMPLEMENTATION_TYPE
, klass
, categ
, NULL_TREE
);
706 = continue_class (objc_implementation_context
);
710 objc_continue_implementation (void)
713 = continue_class (objc_implementation_context
);
717 objc_finish_implementation (void)
720 if (flag_objc_call_cxx_cdtors
)
721 objc_generate_cxx_cdtors ();
724 if (objc_implementation_context
)
726 finish_class (objc_implementation_context
);
727 objc_ivar_chain
= NULL_TREE
;
728 objc_implementation_context
= NULL_TREE
;
731 warning (0, "%<@end%> must appear in an @implementation context");
735 objc_set_visibility (int visibility
)
737 objc_public_flag
= visibility
;
741 objc_set_method_type (enum tree_code type
)
743 objc_inherit_code
= (type
== PLUS_EXPR
745 : INSTANCE_METHOD_DECL
);
749 objc_build_method_signature (tree rettype
, tree selector
,
750 tree optparms
, bool ellipsis
)
752 return build_method_decl (objc_inherit_code
, rettype
, selector
,
757 objc_add_method_declaration (tree decl
)
759 if (!objc_interface_context
)
761 /* PS: At the moment, due to how the parser works, it should be
762 impossible to get here. But it's good to have the check in
763 case the parser changes.
765 fatal_error ("method declaration not in @interface context");
768 objc_add_method (objc_interface_context
,
770 objc_inherit_code
== CLASS_METHOD_DECL
);
773 /* Return 'true' if the method definition could be started, and
774 'false' if not (because we are outside an @implementation context).
777 objc_start_method_definition (tree decl
)
779 if (!objc_implementation_context
)
781 error ("method definition not in @implementation context");
786 /* Indicate no valid break/continue context by setting these variables
787 to some non-null, non-label value. We'll notice and emit the proper
788 error message in c_finish_bc_stmt. */
789 c_break_label
= c_cont_label
= size_zero_node
;
792 objc_add_method (objc_implementation_context
,
794 objc_inherit_code
== CLASS_METHOD_DECL
);
795 start_method_def (decl
);
800 objc_add_instance_variable (tree decl
)
802 (void) add_instance_variable (objc_ivar_context
,
807 /* Return 1 if IDENT is an ObjC/ObjC++ reserved keyword in the context of
811 objc_is_reserved_word (tree ident
)
813 unsigned char code
= C_RID_CODE (ident
);
815 return (OBJC_IS_AT_KEYWORD (code
)
816 || code
== RID_CLASS
|| code
== RID_PUBLIC
817 || code
== RID_PROTECTED
|| code
== RID_PRIVATE
818 || code
== RID_TRY
|| code
== RID_THROW
|| code
== RID_CATCH
);
821 /* Return true if TYPE is 'id'. */
824 objc_is_object_id (tree type
)
826 return OBJC_TYPE_NAME (type
) == objc_object_id
;
830 objc_is_class_id (tree type
)
832 return OBJC_TYPE_NAME (type
) == objc_class_id
;
835 /* Construct a C struct with same name as KLASS, a base struct with tag
836 SUPER_NAME (if any), and FIELDS indicated. */
839 objc_build_struct (tree klass
, tree fields
, tree super_name
)
841 tree name
= CLASS_NAME (klass
);
842 tree s
= objc_start_struct (name
);
843 tree super
= (super_name
? xref_tag (RECORD_TYPE
, super_name
) : NULL_TREE
);
845 VEC(tree
,heap
) *objc_info
= NULL
;
850 /* Prepend a packed variant of the base class into the layout. This
851 is necessary to preserve ObjC ABI compatibility. */
852 tree base
= build_decl (input_location
,
853 FIELD_DECL
, NULL_TREE
, super
);
854 tree field
= TYPE_FIELDS (super
);
856 while (field
&& DECL_CHAIN (field
)
857 && TREE_CODE (DECL_CHAIN (field
)) == FIELD_DECL
)
858 field
= DECL_CHAIN (field
);
860 /* For ObjC ABI purposes, the "packed" size of a base class is
861 the sum of the offset and the size (in bits) of the last field
864 = (field
&& TREE_CODE (field
) == FIELD_DECL
865 ? size_binop (PLUS_EXPR
,
866 size_binop (PLUS_EXPR
,
869 convert (bitsizetype
,
870 DECL_FIELD_OFFSET (field
)),
871 bitsize_int (BITS_PER_UNIT
)),
872 DECL_FIELD_BIT_OFFSET (field
)),
874 : bitsize_zero_node
);
875 DECL_SIZE_UNIT (base
)
876 = size_binop (FLOOR_DIV_EXPR
, convert (sizetype
, DECL_SIZE (base
)),
877 size_int (BITS_PER_UNIT
));
878 DECL_ARTIFICIAL (base
) = 1;
879 DECL_ALIGN (base
) = 1;
880 DECL_FIELD_CONTEXT (base
) = s
;
882 DECL_FIELD_IS_BASE (base
) = 1;
885 TREE_NO_WARNING (fields
) = 1; /* Suppress C++ ABI warnings -- we */
886 #endif /* are following the ObjC ABI here. */
887 DECL_CHAIN (base
) = fields
;
891 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
892 in all variants of this RECORD_TYPE to be clobbered, but it is therein
893 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
894 Hence, we must squirrel away the ObjC-specific information before calling
895 finish_struct(), and then reinstate it afterwards. */
897 for (t
= TYPE_NEXT_VARIANT (s
); t
; t
= TYPE_NEXT_VARIANT (t
))
899 if (!TYPE_HAS_OBJC_INFO (t
))
901 INIT_TYPE_OBJC_INFO (t
);
902 TYPE_OBJC_INTERFACE (t
) = klass
;
904 VEC_safe_push (tree
, heap
, objc_info
, TYPE_OBJC_INFO (t
));
907 /* Point the struct at its related Objective-C class. */
908 INIT_TYPE_OBJC_INFO (s
);
909 TYPE_OBJC_INTERFACE (s
) = klass
;
911 s
= objc_finish_struct (s
, fields
);
913 for (i
= 0, t
= TYPE_NEXT_VARIANT (s
); t
; t
= TYPE_NEXT_VARIANT (t
), i
++)
915 TYPE_OBJC_INFO (t
) = VEC_index (tree
, objc_info
, i
);
916 /* Replace the IDENTIFIER_NODE with an actual @interface. */
917 TYPE_OBJC_INTERFACE (t
) = klass
;
919 VEC_free (tree
, heap
, objc_info
);
921 /* Use TYPE_BINFO structures to point at the super class, if any. */
922 objc_xref_basetypes (s
, super
);
924 /* Mark this struct as a class template. */
925 CLASS_STATIC_TEMPLATE (klass
) = s
;
930 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
931 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
934 objc_build_volatilized_type (tree type
)
938 /* Check if we have not constructed the desired variant already. */
939 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
941 /* The type qualifiers must (obviously) match up. */
942 if (!TYPE_VOLATILE (t
)
943 || (TYPE_READONLY (t
) != TYPE_READONLY (type
))
944 || (TYPE_RESTRICT (t
) != TYPE_RESTRICT (type
)))
947 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
948 info, if any) must match up. */
949 if (POINTER_TYPE_P (t
)
950 && (TREE_TYPE (t
) != TREE_TYPE (type
)))
953 /* Everything matches up! */
957 /* Ok, we could not re-use any of the pre-existing variants. Create
959 t
= build_variant_type_copy (type
);
960 TYPE_VOLATILE (t
) = 1;
962 /* Set up the canonical type information. */
963 if (TYPE_STRUCTURAL_EQUALITY_P (type
))
964 SET_TYPE_STRUCTURAL_EQUALITY (t
);
965 else if (TYPE_CANONICAL (type
) != type
)
966 TYPE_CANONICAL (t
) = objc_build_volatilized_type (TYPE_CANONICAL (type
));
968 TYPE_CANONICAL (t
) = t
;
973 /* Mark DECL as being 'volatile' for purposes of Darwin
974 _setjmp()/_longjmp() exception handling. Called from
975 objc_mark_locals_volatile(). */
977 objc_volatilize_decl (tree decl
)
979 /* Do not mess with variables that are 'static' or (already)
981 if (!TREE_THIS_VOLATILE (decl
) && !TREE_STATIC (decl
)
982 && (TREE_CODE (decl
) == VAR_DECL
983 || TREE_CODE (decl
) == PARM_DECL
))
985 tree t
= TREE_TYPE (decl
);
986 struct volatilized_type key
;
989 t
= objc_build_volatilized_type (t
);
991 loc
= htab_find_slot (volatilized_htab
, &key
, INSERT
);
995 *loc
= ggc_alloc_volatilized_type ();
996 ((struct volatilized_type
*) *loc
)->type
= t
;
999 TREE_TYPE (decl
) = t
;
1000 TREE_THIS_VOLATILE (decl
) = 1;
1001 TREE_SIDE_EFFECTS (decl
) = 1;
1002 DECL_REGISTER (decl
) = 0;
1004 C_DECL_REGISTER (decl
) = 0;
1009 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
1010 (including its categories and superclasses) or by object type TYP.
1011 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
1014 objc_lookup_protocol (tree proto
, tree cls
, tree typ
, bool warn
)
1016 bool class_type
= (cls
!= NULL_TREE
);
1022 /* Check protocols adopted by the class and its categories. */
1023 for (c
= cls
; c
; c
= CLASS_CATEGORY_LIST (c
))
1025 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c
), proto
))
1029 /* Repeat for superclasses. */
1030 cls
= lookup_interface (CLASS_SUPER_NAME (cls
));
1033 /* Check for any protocols attached directly to the object type. */
1034 if (TYPE_HAS_OBJC_INFO (typ
))
1036 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ
), proto
))
1043 gen_type_name_0 (class_type
? typ
: TYPE_POINTER_TO (typ
));
1044 /* NB: Types 'id' and 'Class' cannot reasonably be described as
1045 "implementing" a given protocol, since they do not have an
1048 warning (0, "class %qs does not implement the %qE protocol",
1049 identifier_to_locale (errbuf
), PROTOCOL_NAME (proto
));
1051 warning (0, "type %qs does not conform to the %qE protocol",
1052 identifier_to_locale (errbuf
), PROTOCOL_NAME (proto
));
1058 /* Check if class RCLS and instance struct type RTYP conform to at least the
1059 same protocols that LCLS and LTYP conform to. */
1062 objc_compare_protocols (tree lcls
, tree ltyp
, tree rcls
, tree rtyp
, bool warn
)
1065 bool have_lproto
= false;
1069 /* NB: We do _not_ look at categories defined for LCLS; these may or
1070 may not get loaded in, and therefore it is unreasonable to require
1071 that RCLS/RTYP must implement any of their protocols. */
1072 for (p
= CLASS_PROTOCOL_LIST (lcls
); p
; p
= TREE_CHAIN (p
))
1076 if (!objc_lookup_protocol (TREE_VALUE (p
), rcls
, rtyp
, warn
))
1080 /* Repeat for superclasses. */
1081 lcls
= lookup_interface (CLASS_SUPER_NAME (lcls
));
1084 /* Check for any protocols attached directly to the object type. */
1085 if (TYPE_HAS_OBJC_INFO (ltyp
))
1087 for (p
= TYPE_OBJC_PROTOCOL_LIST (ltyp
); p
; p
= TREE_CHAIN (p
))
1091 if (!objc_lookup_protocol (TREE_VALUE (p
), rcls
, rtyp
, warn
))
1096 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1097 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1098 away with simply checking for 'id' or 'Class' (!RCLS), since this
1099 routine will not get called in other cases. */
1100 return have_lproto
|| (rcls
!= NULL_TREE
);
1103 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1104 an instance of RTYP to an instance of LTYP or to compare the two
1105 (if ARGNO is equal to -3), per ObjC type system rules. Before
1106 returning 'true', this routine may issue warnings related to, e.g.,
1107 protocol conformance. When returning 'false', the routine must
1108 produce absolutely no warnings; the C or C++ front-end will do so
1109 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1110 the routine must return 'false'.
1112 The ARGNO parameter is encoded as follows:
1113 >= 1 Parameter number (CALLEE contains function being called);
1117 -3 Comparison (LTYP and RTYP may match in either direction). */
1120 objc_compare_types (tree ltyp
, tree rtyp
, int argno
, tree callee
)
1122 tree lcls
, rcls
, lproto
, rproto
;
1123 bool pointers_compatible
;
1125 /* We must be dealing with pointer types */
1126 if (!POINTER_TYPE_P (ltyp
) || !POINTER_TYPE_P (rtyp
))
1131 ltyp
= TREE_TYPE (ltyp
); /* Remove indirections. */
1132 rtyp
= TREE_TYPE (rtyp
);
1134 while (POINTER_TYPE_P (ltyp
) && POINTER_TYPE_P (rtyp
));
1136 /* We must also handle function pointers, since ObjC is a bit more
1137 lenient than C or C++ on this. */
1138 if (TREE_CODE (ltyp
) == FUNCTION_TYPE
&& TREE_CODE (rtyp
) == FUNCTION_TYPE
)
1140 /* Return types must be covariant. */
1141 if (!comptypes (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
))
1142 && !objc_compare_types (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
),
1146 /* Argument types must be contravariant. */
1147 for (ltyp
= TYPE_ARG_TYPES (ltyp
), rtyp
= TYPE_ARG_TYPES (rtyp
);
1148 ltyp
&& rtyp
; ltyp
= TREE_CHAIN (ltyp
), rtyp
= TREE_CHAIN (rtyp
))
1150 if (!comptypes (TREE_VALUE (rtyp
), TREE_VALUE (ltyp
))
1151 && !objc_compare_types (TREE_VALUE (rtyp
), TREE_VALUE (ltyp
),
1156 return (ltyp
== rtyp
);
1159 /* Past this point, we are only interested in ObjC class instances,
1160 or 'id' or 'Class'. */
1161 if (TREE_CODE (ltyp
) != RECORD_TYPE
|| TREE_CODE (rtyp
) != RECORD_TYPE
)
1164 if (!objc_is_object_id (ltyp
) && !objc_is_class_id (ltyp
)
1165 && !TYPE_HAS_OBJC_INFO (ltyp
))
1168 if (!objc_is_object_id (rtyp
) && !objc_is_class_id (rtyp
)
1169 && !TYPE_HAS_OBJC_INFO (rtyp
))
1172 /* Past this point, we are committed to returning 'true' to the caller.
1173 However, we can still warn about type and/or protocol mismatches. */
1175 if (TYPE_HAS_OBJC_INFO (ltyp
))
1177 lcls
= TYPE_OBJC_INTERFACE (ltyp
);
1178 lproto
= TYPE_OBJC_PROTOCOL_LIST (ltyp
);
1181 lcls
= lproto
= NULL_TREE
;
1183 if (TYPE_HAS_OBJC_INFO (rtyp
))
1185 rcls
= TYPE_OBJC_INTERFACE (rtyp
);
1186 rproto
= TYPE_OBJC_PROTOCOL_LIST (rtyp
);
1189 rcls
= rproto
= NULL_TREE
;
1191 /* If we could not find an @interface declaration, we must have
1192 only seen a @class declaration; for purposes of type comparison,
1193 treat it as a stand-alone (root) class. */
1195 if (lcls
&& TREE_CODE (lcls
) == IDENTIFIER_NODE
)
1198 if (rcls
&& TREE_CODE (rcls
) == IDENTIFIER_NODE
)
1201 /* If either type is an unqualified 'id', we're done. */
1202 if ((!lproto
&& objc_is_object_id (ltyp
))
1203 || (!rproto
&& objc_is_object_id (rtyp
)))
1206 pointers_compatible
= (TYPE_MAIN_VARIANT (ltyp
) == TYPE_MAIN_VARIANT (rtyp
));
1208 /* If the underlying types are the same, and at most one of them has
1209 a protocol list, we do not need to issue any diagnostics. */
1210 if (pointers_compatible
&& (!lproto
|| !rproto
))
1213 /* If exactly one of the types is 'Class', issue a diagnostic; any
1214 exceptions of this rule have already been handled. */
1215 if (objc_is_class_id (ltyp
) ^ objc_is_class_id (rtyp
))
1216 pointers_compatible
= false;
1217 /* Otherwise, check for inheritance relations. */
1220 if (!pointers_compatible
)
1222 = (objc_is_object_id (ltyp
) || objc_is_object_id (rtyp
));
1224 if (!pointers_compatible
)
1225 pointers_compatible
= DERIVED_FROM_P (ltyp
, rtyp
);
1227 if (!pointers_compatible
&& argno
== -3)
1228 pointers_compatible
= DERIVED_FROM_P (rtyp
, ltyp
);
1231 /* If the pointers match modulo protocols, check for protocol conformance
1233 if (pointers_compatible
)
1235 pointers_compatible
= objc_compare_protocols (lcls
, ltyp
, rcls
, rtyp
,
1238 if (!pointers_compatible
&& argno
== -3)
1239 pointers_compatible
= objc_compare_protocols (rcls
, rtyp
, lcls
, ltyp
,
1243 if (!pointers_compatible
)
1245 /* NB: For the time being, we shall make our warnings look like their
1246 C counterparts. In the future, we may wish to make them more
1251 warning (0, "comparison of distinct Objective-C types lacks a cast");
1255 warning (0, "initialization from distinct Objective-C type");
1259 warning (0, "assignment from distinct Objective-C type");
1263 warning (0, "distinct Objective-C type in return");
1267 warning (0, "passing argument %d of %qE from distinct "
1268 "Objective-C type", argno
, callee
);
1276 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1277 lives in the volatilized hash table, ignore the 'volatile' bit when
1278 making the comparison. */
1281 objc_type_quals_match (tree ltyp
, tree rtyp
)
1283 int lquals
= TYPE_QUALS (ltyp
), rquals
= TYPE_QUALS (rtyp
);
1284 struct volatilized_type key
;
1288 if (htab_find_slot (volatilized_htab
, &key
, NO_INSERT
))
1289 lquals
&= ~TYPE_QUAL_VOLATILE
;
1293 if (htab_find_slot (volatilized_htab
, &key
, NO_INSERT
))
1294 rquals
&= ~TYPE_QUAL_VOLATILE
;
1296 return (lquals
== rquals
);
1300 /* Determine if CHILD is derived from PARENT. The routine assumes that
1301 both parameters are RECORD_TYPEs, and is non-reflexive. */
1304 objc_derived_from_p (tree parent
, tree child
)
1306 parent
= TYPE_MAIN_VARIANT (parent
);
1308 for (child
= TYPE_MAIN_VARIANT (child
);
1309 TYPE_BINFO (child
) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child
));)
1311 child
= TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1312 (TYPE_BINFO (child
),
1315 if (child
== parent
)
1324 objc_build_component_ref (tree datum
, tree component
)
1326 /* If COMPONENT is NULL, the caller is referring to the anonymous
1327 base class field. */
1330 tree base
= TYPE_FIELDS (TREE_TYPE (datum
));
1332 return build3 (COMPONENT_REF
, TREE_TYPE (base
), datum
, base
, NULL_TREE
);
1335 /* The 'build_component_ref' routine has been removed from the C++
1336 front-end, but 'finish_class_member_access_expr' seems to be
1337 a worthy substitute. */
1339 return finish_class_member_access_expr (datum
, component
, false,
1340 tf_warning_or_error
);
1342 return build_component_ref (input_location
, datum
, component
);
1346 /* Recursively copy inheritance information rooted at BINFO. To do this,
1347 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1350 objc_copy_binfo (tree binfo
)
1352 tree btype
= BINFO_TYPE (binfo
);
1353 tree binfo2
= make_tree_binfo (BINFO_N_BASE_BINFOS (binfo
));
1357 BINFO_TYPE (binfo2
) = btype
;
1358 BINFO_OFFSET (binfo2
) = BINFO_OFFSET (binfo
);
1359 BINFO_BASE_ACCESSES (binfo2
) = BINFO_BASE_ACCESSES (binfo
);
1361 /* Recursively copy base binfos of BINFO. */
1362 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
1364 tree base_binfo2
= objc_copy_binfo (base_binfo
);
1366 BINFO_INHERITANCE_CHAIN (base_binfo2
) = binfo2
;
1367 BINFO_BASE_APPEND (binfo2
, base_binfo2
);
1373 /* Record superclass information provided in BASETYPE for ObjC class REF.
1374 This is loosely based on cp/decl.c:xref_basetypes(). */
1377 objc_xref_basetypes (tree ref
, tree basetype
)
1379 tree binfo
= make_tree_binfo (basetype
? 1 : 0);
1381 TYPE_BINFO (ref
) = binfo
;
1382 BINFO_OFFSET (binfo
) = size_zero_node
;
1383 BINFO_TYPE (binfo
) = ref
;
1387 tree base_binfo
= objc_copy_binfo (TYPE_BINFO (basetype
));
1389 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
1390 BINFO_BASE_ACCESSES (binfo
) = VEC_alloc (tree
, gc
, 1);
1391 BINFO_BASE_APPEND (binfo
, base_binfo
);
1392 BINFO_BASE_ACCESS_APPEND (binfo
, access_public_node
);
1397 volatilized_hash (const void *ptr
)
1399 const_tree
const typ
= ((const struct volatilized_type
*)ptr
)->type
;
1401 return htab_hash_pointer(typ
);
1405 volatilized_eq (const void *ptr1
, const void *ptr2
)
1407 const_tree
const typ1
= ((const struct volatilized_type
*)ptr1
)->type
;
1408 const_tree
const typ2
= ((const struct volatilized_type
*)ptr2
)->type
;
1410 return typ1
== typ2
;
1413 /* Called from finish_decl. */
1416 objc_check_decl (tree decl
)
1418 tree type
= TREE_TYPE (decl
);
1420 if (TREE_CODE (type
) != RECORD_TYPE
)
1422 if (OBJC_TYPE_NAME (type
) && (type
= objc_is_class_name (OBJC_TYPE_NAME (type
))))
1423 error ("statically allocated instance of Objective-C class %qE",
1427 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
1428 either name an Objective-C class, or refer to the special 'id' or 'Class'
1429 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
1432 objc_get_protocol_qualified_type (tree interface
, tree protocols
)
1434 /* If INTERFACE is not provided, default to 'id'. */
1435 tree type
= (interface
? objc_is_id (interface
) : objc_object_type
);
1436 bool is_ptr
= (type
!= NULL_TREE
);
1440 type
= objc_is_class_name (interface
);
1444 /* If looking at a typedef, retrieve the precise type it
1446 if (TREE_CODE (interface
) == IDENTIFIER_NODE
)
1447 interface
= identifier_global_value (interface
);
1449 type
= ((interface
&& TREE_CODE (interface
) == TYPE_DECL
1450 && DECL_ORIGINAL_TYPE (interface
))
1451 ? DECL_ORIGINAL_TYPE (interface
)
1452 : xref_tag (RECORD_TYPE
, type
));
1460 type
= build_variant_type_copy (type
);
1462 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
1466 tree orig_pointee_type
= TREE_TYPE (type
);
1467 TREE_TYPE (type
) = build_variant_type_copy (orig_pointee_type
);
1469 /* Set up the canonical type information. */
1470 TYPE_CANONICAL (type
)
1471 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type
));
1473 TYPE_POINTER_TO (TREE_TYPE (type
)) = type
;
1474 type
= TREE_TYPE (type
);
1477 /* Look up protocols and install in lang specific list. */
1478 DUP_TYPE_OBJC_INFO (type
, TYPE_MAIN_VARIANT (type
));
1479 TYPE_OBJC_PROTOCOL_LIST (type
) = lookup_and_install_protocols (protocols
);
1481 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
1482 return the pointer to the new pointee variant. */
1484 type
= TYPE_POINTER_TO (type
);
1486 TYPE_OBJC_INTERFACE (type
)
1487 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type
));
1493 /* Check for circular dependencies in protocols. The arguments are
1494 PROTO, the protocol to check, and LIST, a list of protocol it
1498 check_protocol_recursively (tree proto
, tree list
)
1502 for (p
= list
; p
; p
= TREE_CHAIN (p
))
1504 tree pp
= TREE_VALUE (p
);
1506 if (TREE_CODE (pp
) == IDENTIFIER_NODE
)
1507 pp
= lookup_protocol (pp
);
1510 fatal_error ("protocol %qE has circular dependency",
1511 PROTOCOL_NAME (pp
));
1513 check_protocol_recursively (proto
, PROTOCOL_LIST (pp
));
1517 /* Look up PROTOCOLS, and return a list of those that are found.
1518 If none are found, return NULL. */
1521 lookup_and_install_protocols (tree protocols
)
1524 tree return_value
= NULL_TREE
;
1526 for (proto
= protocols
; proto
; proto
= TREE_CHAIN (proto
))
1528 tree ident
= TREE_VALUE (proto
);
1529 tree p
= lookup_protocol (ident
);
1532 return_value
= chainon (return_value
,
1533 build_tree_list (NULL_TREE
, p
));
1534 else if (ident
!= error_mark_node
)
1535 error ("cannot find protocol declaration for %qE",
1539 return return_value
;
1542 /* Create a declaration for field NAME of a given TYPE. */
1545 create_field_decl (tree type
, const char *name
)
1547 return build_decl (input_location
,
1548 FIELD_DECL
, get_identifier (name
), type
);
1551 /* Create a global, static declaration for variable NAME of a given TYPE. The
1552 finish_var_decl() routine will need to be called on it afterwards. */
1555 start_var_decl (tree type
, const char *name
)
1557 tree var
= build_decl (input_location
,
1558 VAR_DECL
, get_identifier (name
), type
);
1560 TREE_STATIC (var
) = 1;
1561 DECL_INITIAL (var
) = error_mark_node
; /* A real initializer is coming... */
1562 DECL_IGNORED_P (var
) = 1;
1563 DECL_ARTIFICIAL (var
) = 1;
1564 DECL_CONTEXT (var
) = NULL_TREE
;
1566 DECL_THIS_STATIC (var
) = 1; /* squash redeclaration errors */
1572 /* Finish off the variable declaration created by start_var_decl(). */
1575 finish_var_decl (tree var
, tree initializer
)
1577 finish_decl (var
, input_location
, initializer
, NULL_TREE
, NULL_TREE
);
1580 /* Find the decl for the constant string class reference. This is only
1581 used for the NeXT runtime. */
1584 setup_string_decl (void)
1589 /* %s in format will provide room for terminating null */
1590 length
= strlen (STRING_OBJECT_GLOBAL_FORMAT
)
1591 + strlen (constant_string_class_name
);
1592 name
= XNEWVEC (char, length
);
1593 sprintf (name
, STRING_OBJECT_GLOBAL_FORMAT
,
1594 constant_string_class_name
);
1595 constant_string_global_id
= get_identifier (name
);
1596 string_class_decl
= lookup_name (constant_string_global_id
);
1598 return string_class_decl
;
1601 /* Purpose: "play" parser, creating/installing representations
1602 of the declarations that are required by Objective-C.
1606 type_spec--------->sc_spec
1607 (tree_list) (tree_list)
1610 identifier_node identifier_node */
1613 synth_module_prologue (void)
1616 enum debug_info_type save_write_symbols
= write_symbols
;
1617 const struct gcc_debug_hooks
*const save_hooks
= debug_hooks
;
1619 /* Suppress outputting debug symbols, because
1620 dbxout_init hasn't been called yet. */
1621 write_symbols
= NO_DEBUG
;
1622 debug_hooks
= &do_nothing_debug_hooks
;
1625 push_lang_context (lang_name_c
); /* extern "C" */
1628 /* The following are also defined in <objc/objc.h> and friends. */
1630 objc_object_id
= get_identifier (TAG_OBJECT
);
1631 objc_class_id
= get_identifier (TAG_CLASS
);
1633 objc_object_reference
= xref_tag (RECORD_TYPE
, objc_object_id
);
1634 objc_class_reference
= xref_tag (RECORD_TYPE
, objc_class_id
);
1636 objc_object_type
= build_pointer_type (objc_object_reference
);
1637 objc_class_type
= build_pointer_type (objc_class_reference
);
1639 objc_object_name
= get_identifier (OBJECT_TYPEDEF_NAME
);
1640 objc_class_name
= get_identifier (CLASS_TYPEDEF_NAME
);
1642 /* Declare the 'id' and 'Class' typedefs. */
1644 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
1648 TREE_NO_WARNING (type
) = 1;
1649 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
1653 TREE_NO_WARNING (type
) = 1;
1655 /* Forward-declare '@interface Protocol'. */
1657 type
= get_identifier (PROTOCOL_OBJECT_CLASS_NAME
);
1658 objc_declare_class (tree_cons (NULL_TREE
, type
, NULL_TREE
));
1659 objc_protocol_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
1662 /* Declare type of selector-objects that represent an operation name. */
1664 if (flag_next_runtime
)
1665 /* `struct objc_selector *' */
1667 = build_pointer_type (xref_tag (RECORD_TYPE
,
1668 get_identifier (TAG_SELECTOR
)));
1670 /* `const struct objc_selector *' */
1672 = build_pointer_type
1673 (build_qualified_type (xref_tag (RECORD_TYPE
,
1674 get_identifier (TAG_SELECTOR
)),
1677 /* Declare receiver type used for dispatching messages to 'super'. */
1679 /* `struct objc_super *' */
1680 objc_super_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
1681 get_identifier (TAG_SUPER
)));
1683 /* Declare pointers to method and ivar lists. */
1684 objc_method_list_ptr
= build_pointer_type
1685 (xref_tag (RECORD_TYPE
,
1686 get_identifier (UTAG_METHOD_LIST
)));
1687 objc_method_proto_list_ptr
1688 = build_pointer_type (xref_tag (RECORD_TYPE
,
1689 get_identifier (UTAG_METHOD_PROTOTYPE_LIST
)));
1690 objc_ivar_list_ptr
= build_pointer_type
1691 (xref_tag (RECORD_TYPE
,
1692 get_identifier (UTAG_IVAR_LIST
)));
1694 /* TREE_NOTHROW is cleared for the message-sending functions,
1695 because the function that gets called can throw in Obj-C++, or
1696 could itself call something that can throw even in Obj-C. */
1698 if (flag_next_runtime
)
1700 /* NB: In order to call one of the ..._stret (struct-returning)
1701 functions, the function *MUST* first be cast to a signature that
1702 corresponds to the actual ObjC method being invoked. This is
1703 what is done by the build_objc_method_call() routine below. */
1705 /* id objc_msgSend (id, SEL, ...); */
1706 /* id objc_msgSendNonNil (id, SEL, ...); */
1707 /* id objc_msgSend_stret (id, SEL, ...); */
1708 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
1710 = build_varargs_function_type_list (objc_object_type
,
1714 umsg_decl
= add_builtin_function (TAG_MSGSEND
,
1715 type
, 0, NOT_BUILT_IN
,
1717 umsg_nonnil_decl
= add_builtin_function (TAG_MSGSEND_NONNIL
,
1718 type
, 0, NOT_BUILT_IN
,
1720 umsg_stret_decl
= add_builtin_function (TAG_MSGSEND_STRET
,
1721 type
, 0, NOT_BUILT_IN
,
1723 umsg_nonnil_stret_decl
= add_builtin_function (TAG_MSGSEND_NONNIL_STRET
,
1724 type
, 0, NOT_BUILT_IN
,
1727 /* These can throw, because the function that gets called can throw
1728 in Obj-C++, or could itself call something that can throw even
1730 TREE_NOTHROW (umsg_decl
) = 0;
1731 TREE_NOTHROW (umsg_nonnil_decl
) = 0;
1732 TREE_NOTHROW (umsg_stret_decl
) = 0;
1733 TREE_NOTHROW (umsg_nonnil_stret_decl
) = 0;
1735 /* id objc_msgSend_Fast (id, SEL, ...)
1736 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
1737 #ifdef OFFS_MSGSEND_FAST
1738 umsg_fast_decl
= add_builtin_function (TAG_MSGSEND_FAST
,
1739 type
, 0, NOT_BUILT_IN
,
1741 TREE_NOTHROW (umsg_fast_decl
) = 0;
1742 DECL_ATTRIBUTES (umsg_fast_decl
)
1743 = tree_cons (get_identifier ("hard_coded_address"),
1744 build_int_cst (NULL_TREE
, OFFS_MSGSEND_FAST
),
1747 /* No direct dispatch available. */
1748 umsg_fast_decl
= umsg_decl
;
1751 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
1752 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
1754 = build_varargs_function_type_list (objc_object_type
,
1758 umsg_super_decl
= add_builtin_function (TAG_MSGSENDSUPER
,
1759 type
, 0, NOT_BUILT_IN
,
1761 umsg_super_stret_decl
= add_builtin_function (TAG_MSGSENDSUPER_STRET
,
1762 type
, 0, NOT_BUILT_IN
, 0,
1764 TREE_NOTHROW (umsg_super_decl
) = 0;
1765 TREE_NOTHROW (umsg_super_stret_decl
) = 0;
1769 /* GNU runtime messenger entry points. */
1771 /* typedef id (*IMP)(id, SEL, ...); */
1773 build_varargs_function_type_list (objc_object_type
,
1777 tree IMP_type
= build_pointer_type (ftype
);
1779 /* IMP objc_msg_lookup (id, SEL); */
1780 type
= build_function_type_list (IMP_type
,
1784 umsg_decl
= add_builtin_function (TAG_MSGSEND
,
1785 type
, 0, NOT_BUILT_IN
,
1787 TREE_NOTHROW (umsg_decl
) = 0;
1789 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
1791 = build_function_type_list (IMP_type
,
1795 umsg_super_decl
= add_builtin_function (TAG_MSGSENDSUPER
,
1796 type
, 0, NOT_BUILT_IN
,
1798 TREE_NOTHROW (umsg_super_decl
) = 0;
1800 /* The following GNU runtime entry point is called to initialize
1803 __objc_exec_class (void *); */
1805 = build_function_type_list (void_type_node
,
1808 execclass_decl
= add_builtin_function (TAG_EXECCLASS
,
1809 type
, 0, NOT_BUILT_IN
,
1813 /* id objc_getClass (const char *); */
1815 type
= build_function_type_list (objc_object_type
,
1816 const_string_type_node
,
1820 = add_builtin_function (TAG_GETCLASS
, type
, 0, NOT_BUILT_IN
,
1823 /* id objc_getMetaClass (const char *); */
1825 objc_get_meta_class_decl
1826 = add_builtin_function (TAG_GETMETACLASS
, type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
1828 build_class_template ();
1829 build_super_template ();
1830 build_protocol_template ();
1831 build_category_template ();
1832 build_objc_exception_stuff ();
1834 if (flag_next_runtime
)
1835 build_next_objc_exception_stuff ();
1837 /* static SEL _OBJC_SELECTOR_TABLE[]; */
1839 if (! flag_next_runtime
)
1840 build_selector_table_decl ();
1842 /* Forward declare constant_string_id and constant_string_type. */
1843 if (!constant_string_class_name
)
1844 constant_string_class_name
= default_constant_string_class_name
;
1846 constant_string_id
= get_identifier (constant_string_class_name
);
1847 objc_declare_class (tree_cons (NULL_TREE
, constant_string_id
, NULL_TREE
));
1849 /* Pre-build the following entities - for speed/convenience. */
1850 self_id
= get_identifier ("self");
1851 ucmd_id
= get_identifier ("_cmd");
1854 pop_lang_context ();
1857 write_symbols
= save_write_symbols
;
1858 debug_hooks
= save_hooks
;
1861 /* Ensure that the ivar list for NSConstantString/NXConstantString
1862 (or whatever was specified via `-fconstant-string-class')
1863 contains fields at least as large as the following three, so that
1864 the runtime can stomp on them with confidence:
1866 struct STRING_OBJECT_CLASS_NAME
1870 unsigned int length;
1874 check_string_class_template (void)
1876 tree field_decl
= objc_get_class_ivars (constant_string_id
);
1878 #define AT_LEAST_AS_LARGE_AS(F, T) \
1879 (F && TREE_CODE (F) == FIELD_DECL \
1880 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
1881 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
1883 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
1886 field_decl
= DECL_CHAIN (field_decl
);
1887 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
1890 field_decl
= DECL_CHAIN (field_decl
);
1891 return AT_LEAST_AS_LARGE_AS (field_decl
, unsigned_type_node
);
1893 #undef AT_LEAST_AS_LARGE_AS
1896 /* Avoid calling `check_string_class_template ()' more than once. */
1897 static GTY(()) int string_layout_checked
;
1899 /* Construct an internal string layout to be used as a template for
1900 creating NSConstantString/NXConstantString instances. */
1903 objc_build_internal_const_str_type (void)
1905 tree type
= (*lang_hooks
.types
.make_type
) (RECORD_TYPE
);
1906 tree fields
= build_decl (input_location
,
1907 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
1908 tree field
= build_decl (input_location
,
1909 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
1911 DECL_CHAIN (field
) = fields
; fields
= field
;
1912 field
= build_decl (input_location
,
1913 FIELD_DECL
, NULL_TREE
, unsigned_type_node
);
1914 DECL_CHAIN (field
) = fields
; fields
= field
;
1915 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
1917 finish_builtin_struct (type
, "__builtin_ObjCString",
1923 /* Custom build_string which sets TREE_TYPE! */
1926 my_build_string (int len
, const char *str
)
1928 return fix_string_type (build_string (len
, str
));
1931 /* Build a string with contents STR and length LEN and convert it to a
1935 my_build_string_pointer (int len
, const char *str
)
1937 tree string
= my_build_string (len
, str
);
1938 tree ptrtype
= build_pointer_type (TREE_TYPE (TREE_TYPE (string
)));
1939 return build1 (ADDR_EXPR
, ptrtype
, string
);
1943 string_hash (const void *ptr
)
1945 const_tree
const str
= ((const struct string_descriptor
*)ptr
)->literal
;
1946 const unsigned char *p
= (const unsigned char *) TREE_STRING_POINTER (str
);
1947 int i
, len
= TREE_STRING_LENGTH (str
);
1950 for (i
= 0; i
< len
; i
++)
1951 h
= ((h
* 613) + p
[i
]);
1957 string_eq (const void *ptr1
, const void *ptr2
)
1959 const_tree
const str1
= ((const struct string_descriptor
*)ptr1
)->literal
;
1960 const_tree
const str2
= ((const struct string_descriptor
*)ptr2
)->literal
;
1961 int len1
= TREE_STRING_LENGTH (str1
);
1963 return (len1
== TREE_STRING_LENGTH (str2
)
1964 && !memcmp (TREE_STRING_POINTER (str1
), TREE_STRING_POINTER (str2
),
1968 /* Given a chain of STRING_CST's, build a static instance of
1969 NXConstantString which points at the concatenation of those
1970 strings. We place the string object in the __string_objects
1971 section of the __OBJC segment. The Objective-C runtime will
1972 initialize the isa pointers of the string objects to point at the
1973 NXConstantString class object. */
1976 objc_build_string_object (tree string
)
1978 tree constructor
, constant_string_class
;
1981 struct string_descriptor
*desc
, key
;
1984 /* Prep the string argument. */
1985 string
= fix_string_type (string
);
1986 TREE_SET_CODE (string
, STRING_CST
);
1987 length
= TREE_STRING_LENGTH (string
) - 1;
1989 /* Check whether the string class being used actually exists and has the
1990 correct ivar layout. */
1991 if (!string_layout_checked
)
1993 string_layout_checked
= -1;
1994 constant_string_class
= lookup_interface (constant_string_id
);
1995 internal_const_str_type
= objc_build_internal_const_str_type ();
1997 if (!constant_string_class
1998 || !(constant_string_type
1999 = CLASS_STATIC_TEMPLATE (constant_string_class
)))
2000 error ("cannot find interface declaration for %qE",
2001 constant_string_id
);
2002 /* The NSConstantString/NXConstantString ivar layout is now known. */
2003 else if (!check_string_class_template ())
2004 error ("interface %qE does not have valid constant string layout",
2005 constant_string_id
);
2006 /* For the NeXT runtime, we can generate a literal reference
2007 to the string class, don't need to run a constructor. */
2008 else if (flag_next_runtime
&& !setup_string_decl ())
2009 error ("cannot find reference tag for class %qE",
2010 constant_string_id
);
2013 string_layout_checked
= 1; /* Success! */
2014 add_class_reference (constant_string_id
);
2018 if (string_layout_checked
== -1)
2019 return error_mark_node
;
2021 /* Perhaps we already constructed a constant string just like this one? */
2022 key
.literal
= string
;
2023 loc
= htab_find_slot (string_htab
, &key
, INSERT
);
2024 desc
= (struct string_descriptor
*) *loc
;
2029 VEC(constructor_elt
,gc
) *v
= NULL
;
2030 *loc
= desc
= ggc_alloc_string_descriptor ();
2031 desc
->literal
= string
;
2033 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
2034 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
2035 fields
= TYPE_FIELDS (internal_const_str_type
);
2036 CONSTRUCTOR_APPEND_ELT (v
, fields
,
2038 ? build_unary_op (input_location
,
2039 ADDR_EXPR
, string_class_decl
, 0)
2040 : build_int_cst (NULL_TREE
, 0));
2041 fields
= DECL_CHAIN (fields
);
2042 CONSTRUCTOR_APPEND_ELT (v
, fields
,
2043 build_unary_op (input_location
,
2044 ADDR_EXPR
, string
, 1));
2045 fields
= DECL_CHAIN (fields
);
2046 CONSTRUCTOR_APPEND_ELT (v
, fields
, build_int_cst (NULL_TREE
, length
));
2047 constructor
= objc_build_constructor (internal_const_str_type
, v
);
2049 if (!flag_next_runtime
)
2051 = objc_add_static_instance (constructor
, constant_string_type
);
2054 var
= build_decl (input_location
,
2055 CONST_DECL
, NULL
, TREE_TYPE (constructor
));
2056 DECL_INITIAL (var
) = constructor
;
2057 TREE_STATIC (var
) = 1;
2058 pushdecl_top_level (var
);
2061 desc
->constructor
= constructor
;
2064 addr
= convert (build_pointer_type (constant_string_type
),
2065 build_unary_op (input_location
,
2066 ADDR_EXPR
, desc
->constructor
, 1));
2071 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2073 static GTY(()) int num_static_inst
;
2076 objc_add_static_instance (tree constructor
, tree class_decl
)
2081 /* Find the list of static instances for the CLASS_DECL. Create one if
2083 for (chain
= &objc_static_instances
;
2084 *chain
&& TREE_VALUE (*chain
) != class_decl
;
2085 chain
= &TREE_CHAIN (*chain
));
2088 *chain
= tree_cons (NULL_TREE
, class_decl
, NULL_TREE
);
2089 add_objc_string (OBJC_TYPE_NAME (class_decl
), class_names
);
2092 sprintf (buf
, "_OBJC_INSTANCE_%d", num_static_inst
++);
2093 decl
= build_decl (input_location
,
2094 VAR_DECL
, get_identifier (buf
), class_decl
);
2095 TREE_STATIC (decl
) = 1;
2096 DECL_ARTIFICIAL (decl
) = 1;
2097 TREE_USED (decl
) = 1;
2098 DECL_INITIAL (decl
) = constructor
;
2100 /* We may be writing something else just now.
2101 Postpone till end of input. */
2102 DECL_DEFER_OUTPUT (decl
) = 1;
2103 pushdecl_top_level (decl
);
2104 rest_of_decl_compilation (decl
, 1, 0);
2106 /* Add the DECL to the head of this CLASS' list. */
2107 TREE_PURPOSE (*chain
) = tree_cons (NULL_TREE
, decl
, TREE_PURPOSE (*chain
));
2112 /* Build a static constant CONSTRUCTOR
2113 with type TYPE and elements ELTS. */
2116 objc_build_constructor (tree type
, VEC(constructor_elt
,gc
) *elts
)
2118 tree constructor
= build_constructor (type
, elts
);
2120 TREE_CONSTANT (constructor
) = 1;
2121 TREE_STATIC (constructor
) = 1;
2122 TREE_READONLY (constructor
) = 1;
2125 /* Adjust for impedance mismatch. We should figure out how to build
2126 CONSTRUCTORs that consistently please both the C and C++ gods. */
2127 if (!VEC_index (constructor_elt
, elts
, 0)->index
)
2128 TREE_TYPE (constructor
) = init_list_type_node
;
2134 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2136 /* Predefine the following data type:
2144 void *defs[cls_def_cnt + cat_def_cnt];
2148 build_objc_symtab_template (void)
2150 tree fields
, *chain
= NULL
;
2152 objc_symtab_template
= objc_start_struct (get_identifier (UTAG_SYMTAB
));
2154 /* long sel_ref_cnt; */
2155 fields
= add_field_decl (long_integer_type_node
, "sel_ref_cnt", &chain
);
2158 add_field_decl (build_pointer_type (objc_selector_type
), "refs", &chain
);
2160 /* short cls_def_cnt; */
2161 add_field_decl (short_integer_type_node
, "cls_def_cnt", &chain
);
2163 /* short cat_def_cnt; */
2164 add_field_decl (short_integer_type_node
, "cat_def_cnt", &chain
);
2166 if (imp_count
|| cat_count
|| !flag_next_runtime
)
2168 /* void *defs[imp_count + cat_count (+ 1)]; */
2169 /* NB: The index is one less than the size of the array. */
2170 int index
= imp_count
+ cat_count
+ (flag_next_runtime
? -1: 0);
2171 tree array_type
= build_sized_array_type (ptr_type_node
, index
+ 1);
2172 add_field_decl (array_type
, "defs", &chain
);
2175 objc_finish_struct (objc_symtab_template
, fields
);
2178 /* Create the initial value for the `defs' field of _objc_symtab.
2179 This is a CONSTRUCTOR. */
2182 init_def_list (tree type
)
2185 struct imp_entry
*impent
;
2186 VEC(constructor_elt
,gc
) *v
= NULL
;
2189 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2191 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
2193 expr
= build_unary_op (input_location
,
2194 ADDR_EXPR
, impent
->class_decl
, 0);
2195 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2200 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2202 if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
2204 expr
= build_unary_op (input_location
,
2205 ADDR_EXPR
, impent
->class_decl
, 0);
2206 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2210 if (!flag_next_runtime
)
2212 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2213 if (static_instances_decl
)
2214 expr
= build_unary_op (input_location
,
2215 ADDR_EXPR
, static_instances_decl
, 0);
2217 expr
= integer_zero_node
;
2219 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2222 return objc_build_constructor (type
, v
);
2225 /* Construct the initial value for all of _objc_symtab. */
2228 init_objc_symtab (tree type
)
2230 VEC(constructor_elt
,gc
) *v
= NULL
;
2232 /* sel_ref_cnt = { ..., 5, ... } */
2234 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2235 build_int_cst (long_integer_type_node
, 0));
2237 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2239 if (flag_next_runtime
|| ! sel_ref_chain
)
2240 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, convert (
2241 build_pointer_type (objc_selector_type
),
2242 integer_zero_node
));
2245 tree expr
= build_unary_op (input_location
, ADDR_EXPR
,
2246 UOBJC_SELECTOR_TABLE_decl
, 1);
2248 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2249 convert (build_pointer_type (objc_selector_type
),
2253 /* cls_def_cnt = { ..., 5, ... } */
2255 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2256 build_int_cst (short_integer_type_node
, imp_count
));
2258 /* cat_def_cnt = { ..., 5, ... } */
2260 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2261 build_int_cst (short_integer_type_node
, cat_count
));
2263 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2265 if (imp_count
|| cat_count
|| !flag_next_runtime
)
2268 tree field
= TYPE_FIELDS (type
);
2269 field
= DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field
))));
2271 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init_def_list (TREE_TYPE (field
)));
2274 return objc_build_constructor (type
, v
);
2277 /* Generate forward declarations for metadata such as
2278 'OBJC_CLASS_...'. */
2281 build_metadata_decl (const char *name
, tree type
)
2285 /* struct TYPE NAME_<name>; */
2286 decl
= start_var_decl (type
, synth_id_with_class_suffix
2288 objc_implementation_context
));
2293 /* Push forward-declarations of all the categories so that
2294 init_def_list can use them in a CONSTRUCTOR. */
2297 forward_declare_categories (void)
2299 struct imp_entry
*impent
;
2300 tree sav
= objc_implementation_context
;
2302 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2304 if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
2306 /* Set an invisible arg to synth_id_with_class_suffix. */
2307 objc_implementation_context
= impent
->imp_context
;
2308 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2309 impent
->class_decl
= build_metadata_decl ("_OBJC_CATEGORY",
2310 objc_category_template
);
2313 objc_implementation_context
= sav
;
2316 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2317 and initialized appropriately. */
2320 generate_objc_symtab_decl (void)
2323 build_objc_symtab_template ();
2324 UOBJC_SYMBOLS_decl
= start_var_decl (objc_symtab_template
, "_OBJC_SYMBOLS");
2325 finish_var_decl (UOBJC_SYMBOLS_decl
,
2326 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl
)));
2330 init_module_descriptor (tree type
)
2333 VEC(constructor_elt
,gc
) *v
= NULL
;
2335 /* version = { 1, ... } */
2337 expr
= build_int_cst (long_integer_type_node
, OBJC_VERSION
);
2338 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2340 /* size = { ..., sizeof (struct _objc_module), ... } */
2342 expr
= convert (long_integer_type_node
,
2343 size_in_bytes (objc_module_template
));
2344 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2346 /* Don't provide any file name for security reasons. */
2347 /* name = { ..., "", ... } */
2349 expr
= add_objc_string (get_identifier (""), class_names
);
2350 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2352 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2354 if (UOBJC_SYMBOLS_decl
)
2355 expr
= build_unary_op (input_location
,
2356 ADDR_EXPR
, UOBJC_SYMBOLS_decl
, 0);
2358 expr
= null_pointer_node
;
2359 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2361 return objc_build_constructor (type
, v
);
2364 /* Write out the data structures to describe Objective C classes defined.
2366 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2369 build_module_descriptor (void)
2371 tree decls
, *chain
= NULL
;
2374 push_lang_context (lang_name_c
); /* extern "C" */
2377 objc_module_template
= objc_start_struct (get_identifier (UTAG_MODULE
));
2380 decls
= add_field_decl (long_integer_type_node
, "version", &chain
);
2383 add_field_decl (long_integer_type_node
, "size", &chain
);
2386 add_field_decl (string_type_node
, "name", &chain
);
2388 /* struct _objc_symtab *symtab; */
2389 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE
,
2390 get_identifier (UTAG_SYMTAB
))),
2393 objc_finish_struct (objc_module_template
, decls
);
2395 /* Create an instance of "_objc_module". */
2396 UOBJC_MODULES_decl
= start_var_decl (objc_module_template
, "_OBJC_MODULES");
2397 /* This is the root of the metadata for defined classes and categories, it
2398 is referenced by the runtime and, therefore, needed. */
2399 DECL_PRESERVE_P (UOBJC_MODULES_decl
) = 1;
2400 finish_var_decl (UOBJC_MODULES_decl
,
2401 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl
)));
2404 pop_lang_context ();
2408 /* The GNU runtime requires us to provide a static initializer function
2411 static void __objc_gnu_init (void) {
2412 __objc_exec_class (&L_OBJC_MODULES);
2416 build_module_initializer_routine (void)
2421 push_lang_context (lang_name_c
); /* extern "C" */
2424 objc_push_parm (build_decl (input_location
,
2425 PARM_DECL
, NULL_TREE
, void_type_node
));
2427 objc_start_function (get_identifier (TAG_GNUINIT
),
2428 build_function_type_list (void_type_node
, NULL_TREE
),
2429 NULL_TREE
, NULL_TREE
);
2431 objc_start_function (get_identifier (TAG_GNUINIT
),
2432 build_function_type_list (void_type_node
, NULL_TREE
),
2433 NULL_TREE
, objc_get_parm_info (0));
2435 body
= c_begin_compound_stmt (true);
2436 add_stmt (build_function_call
2441 build_unary_op (input_location
, ADDR_EXPR
,
2442 UOBJC_MODULES_decl
, 0))));
2443 add_stmt (c_end_compound_stmt (input_location
, body
, true));
2445 TREE_PUBLIC (current_function_decl
) = 0;
2448 /* For Objective-C++, we will need to call __objc_gnu_init
2449 from objc_generate_static_init_call() below. */
2450 DECL_STATIC_CONSTRUCTOR (current_function_decl
) = 1;
2453 GNU_INIT_decl
= current_function_decl
;
2457 pop_lang_context ();
2462 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
2463 to be called by the module initializer routine. */
2466 objc_static_init_needed_p (void)
2468 return (GNU_INIT_decl
!= NULL_TREE
);
2471 /* Generate a call to the __objc_gnu_init initializer function. */
2474 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED
)
2476 add_stmt (build_stmt (input_location
, EXPR_STMT
,
2477 build_function_call (input_location
,
2478 GNU_INIT_decl
, NULL_TREE
)));
2482 #endif /* OBJCPLUS */
2484 /* Return the DECL of the string IDENT in the SECTION. */
2487 get_objc_string_decl (tree ident
, enum string_section section
)
2491 if (section
== class_names
)
2492 chain
= class_names_chain
;
2493 else if (section
== meth_var_names
)
2494 chain
= meth_var_names_chain
;
2495 else if (section
== meth_var_types
)
2496 chain
= meth_var_types_chain
;
2500 for (; chain
!= 0; chain
= TREE_CHAIN (chain
))
2501 if (TREE_VALUE (chain
) == ident
)
2502 return (TREE_PURPOSE (chain
));
2508 /* Output references to all statically allocated objects. Return the DECL
2509 for the array built. */
2512 generate_static_references (void)
2514 tree expr
= NULL_TREE
;
2515 tree class_name
, klass
, decl
;
2516 tree cl_chain
, in_chain
, type
2517 = build_array_type (build_pointer_type (void_type_node
), NULL_TREE
);
2518 int num_inst
, num_class
;
2520 VEC(constructor_elt
,gc
) *decls
= NULL
;
2522 if (flag_next_runtime
)
2525 for (cl_chain
= objc_static_instances
, num_class
= 0;
2526 cl_chain
; cl_chain
= TREE_CHAIN (cl_chain
), num_class
++)
2528 VEC(constructor_elt
,gc
) *v
= NULL
;
2530 for (num_inst
= 0, in_chain
= TREE_PURPOSE (cl_chain
);
2531 in_chain
; num_inst
++, in_chain
= TREE_CHAIN (in_chain
));
2533 sprintf (buf
, "_OBJC_STATIC_INSTANCES_%d", num_class
);
2534 decl
= start_var_decl (type
, buf
);
2536 /* Output {class_name, ...}. */
2537 klass
= TREE_VALUE (cl_chain
);
2538 class_name
= get_objc_string_decl (OBJC_TYPE_NAME (klass
), class_names
);
2539 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2540 build_unary_op (input_location
,
2541 ADDR_EXPR
, class_name
, 1));
2543 /* Output {..., instance, ...}. */
2544 for (in_chain
= TREE_PURPOSE (cl_chain
);
2545 in_chain
; in_chain
= TREE_CHAIN (in_chain
))
2547 expr
= build_unary_op (input_location
,
2548 ADDR_EXPR
, TREE_VALUE (in_chain
), 1);
2549 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2552 /* Output {..., NULL}. */
2553 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
2555 expr
= objc_build_constructor (TREE_TYPE (decl
), v
);
2556 finish_var_decl (decl
, expr
);
2557 CONSTRUCTOR_APPEND_ELT (decls
, NULL_TREE
,
2558 build_unary_op (input_location
,
2559 ADDR_EXPR
, decl
, 1));
2562 CONSTRUCTOR_APPEND_ELT (decls
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
2563 expr
= objc_build_constructor (type
, decls
);
2564 static_instances_decl
= start_var_decl (type
, "_OBJC_STATIC_INSTANCES");
2565 finish_var_decl (static_instances_decl
, expr
);
2568 static GTY(()) int selector_reference_idx
;
2571 build_selector_reference_decl (void)
2576 sprintf (buf
, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx
++);
2577 decl
= start_var_decl (objc_selector_type
, buf
);
2583 build_selector_table_decl (void)
2587 if (flag_typed_selectors
)
2589 build_selector_template ();
2590 temp
= build_array_type (objc_selector_template
, NULL_TREE
);
2593 temp
= build_array_type (objc_selector_type
, NULL_TREE
);
2595 UOBJC_SELECTOR_TABLE_decl
= start_var_decl (temp
, "_OBJC_SELECTOR_TABLE");
2598 /* Just a handy wrapper for add_objc_string. */
2601 build_selector (tree ident
)
2603 return convert (objc_selector_type
,
2604 add_objc_string (ident
, meth_var_names
));
2607 /* Used only by build_*_selector_translation_table (). */
2609 diagnose_missing_method (tree meth
, location_t here
)
2613 for (method_chain
= meth_var_names_chain
;
2615 method_chain
= TREE_CHAIN (method_chain
))
2617 if (TREE_VALUE (method_chain
) == meth
)
2625 warning_at (here
, 0, "creating selector for nonexistent method %qE",
2630 build_next_selector_translation_table (void)
2633 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
2636 tree decl
= TREE_PURPOSE (chain
);
2637 if (warn_selector
&& objc_implementation_context
)
2641 loc
= DECL_SOURCE_LOCATION (decl
);
2643 loc
= input_location
;
2644 diagnose_missing_method (TREE_VALUE (chain
), loc
);
2647 expr
= build_selector (TREE_VALUE (chain
));
2651 /* Entries of this form are used for references to methods.
2652 The runtime re-writes these on start-up, but the compiler can't see
2653 that and optimizes it away unless we force it. */
2654 DECL_PRESERVE_P (decl
) = 1;
2655 finish_var_decl (decl
, expr
);
2661 build_gnu_selector_translation_table (void)
2665 tree decl = NULL_TREE;*/
2666 VEC(constructor_elt
,gc
) *inits
= NULL
;
2668 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
2672 if (warn_selector
&& objc_implementation_context
)
2673 diagnose_missing_method (TREE_VALUE (chain
), input_location
);
2675 expr
= build_selector (TREE_VALUE (chain
));
2676 /* add one for the '\0' character
2677 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
2680 if (flag_typed_selectors
)
2682 VEC(constructor_elt
,gc
) *v
= NULL
;
2683 tree encoding
= get_proto_encoding (TREE_PURPOSE (chain
));
2684 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2685 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, encoding
);
2686 expr
= objc_build_constructor (objc_selector_template
, v
);
2689 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
2691 } /* each element in the chain */
2694 /* Cause the selector table (previously forward-declared)
2695 to be actually output. */
2698 if (flag_typed_selectors
)
2700 VEC(constructor_elt
,gc
) *v
= NULL
;
2701 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
2702 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
2703 expr
= objc_build_constructor (objc_selector_template
, v
);
2706 expr
= integer_zero_node
;
2708 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
2709 expr
= objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl
),
2711 finish_var_decl (UOBJC_SELECTOR_TABLE_decl
, expr
);
2716 get_proto_encoding (tree proto
)
2721 if (! METHOD_ENCODING (proto
))
2723 encoding
= encode_method_prototype (proto
);
2724 METHOD_ENCODING (proto
) = encoding
;
2727 encoding
= METHOD_ENCODING (proto
);
2729 return add_objc_string (encoding
, meth_var_types
);
2732 return build_int_cst (NULL_TREE
, 0);
2735 /* sel_ref_chain is a list whose "value" fields will be instances of
2736 identifier_node that represent the selector. LOC is the location of
2740 build_typed_selector_reference (location_t loc
, tree ident
, tree prototype
)
2742 tree
*chain
= &sel_ref_chain
;
2748 if (TREE_PURPOSE (*chain
) == prototype
&& TREE_VALUE (*chain
) == ident
)
2749 goto return_at_index
;
2752 chain
= &TREE_CHAIN (*chain
);
2755 *chain
= tree_cons (prototype
, ident
, NULL_TREE
);
2758 expr
= build_unary_op (loc
, ADDR_EXPR
,
2759 build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2760 build_int_cst (NULL_TREE
, index
)),
2762 return convert (objc_selector_type
, expr
);
2766 build_selector_reference (location_t loc
, tree ident
)
2768 tree
*chain
= &sel_ref_chain
;
2774 if (TREE_VALUE (*chain
) == ident
)
2775 return (flag_next_runtime
2776 ? TREE_PURPOSE (*chain
)
2777 : build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2778 build_int_cst (NULL_TREE
, index
)));
2781 chain
= &TREE_CHAIN (*chain
);
2784 expr
= (flag_next_runtime
? build_selector_reference_decl (): NULL_TREE
);
2786 *chain
= tree_cons (expr
, ident
, NULL_TREE
);
2788 return (flag_next_runtime
2790 : build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2791 build_int_cst (NULL_TREE
, index
)));
2794 static GTY(()) int class_reference_idx
;
2797 build_class_reference_decl (void)
2802 sprintf (buf
, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx
++);
2803 decl
= start_var_decl (objc_class_type
, buf
);
2808 /* Create a class reference, but don't create a variable to reference
2812 add_class_reference (tree ident
)
2816 if ((chain
= cls_ref_chain
))
2821 if (ident
== TREE_VALUE (chain
))
2825 chain
= TREE_CHAIN (chain
);
2829 /* Append to the end of the list */
2830 TREE_CHAIN (tail
) = tree_cons (NULL_TREE
, ident
, NULL_TREE
);
2833 cls_ref_chain
= tree_cons (NULL_TREE
, ident
, NULL_TREE
);
2836 /* Get a class reference, creating it if necessary. Also create the
2837 reference variable. */
2840 objc_get_class_reference (tree ident
)
2842 tree orig_ident
= (DECL_P (ident
)
2845 ? OBJC_TYPE_NAME (ident
)
2847 bool local_scope
= false;
2850 if (processing_template_decl
)
2851 /* Must wait until template instantiation time. */
2852 return build_min_nt (CLASS_REFERENCE_EXPR
, ident
);
2855 if (TREE_CODE (ident
) == TYPE_DECL
)
2856 ident
= (DECL_ORIGINAL_TYPE (ident
)
2857 ? DECL_ORIGINAL_TYPE (ident
)
2858 : TREE_TYPE (ident
));
2862 && CP_TYPE_CONTEXT (ident
) != global_namespace
)
2866 if (local_scope
|| !(ident
= objc_is_class_name (ident
)))
2868 error ("%qE is not an Objective-C class name or alias",
2870 return error_mark_node
;
2873 if (flag_next_runtime
&& !flag_zero_link
)
2878 for (chain
= &cls_ref_chain
; *chain
; chain
= &TREE_CHAIN (*chain
))
2879 if (TREE_VALUE (*chain
) == ident
)
2881 if (! TREE_PURPOSE (*chain
))
2882 TREE_PURPOSE (*chain
) = build_class_reference_decl ();
2884 return TREE_PURPOSE (*chain
);
2887 decl
= build_class_reference_decl ();
2888 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
2895 add_class_reference (ident
);
2897 params
= build_tree_list (NULL_TREE
,
2898 my_build_string_pointer
2899 (IDENTIFIER_LENGTH (ident
) + 1,
2900 IDENTIFIER_POINTER (ident
)));
2902 assemble_external (objc_get_class_decl
);
2903 return build_function_call (input_location
, objc_get_class_decl
, params
);
2907 /* For each string section we have a chain which maps identifier nodes
2908 to decls for the strings. */
2910 static GTY(()) int class_names_idx
;
2911 static GTY(()) int meth_var_names_idx
;
2912 static GTY(()) int meth_var_types_idx
;
2915 add_objc_string (tree ident
, enum string_section section
)
2917 tree
*chain
, decl
, type
, string_expr
;
2921 if (section
== class_names
)
2923 chain
= &class_names_chain
;
2924 sprintf (buf
, "_OBJC_CLASS_NAME_%d", class_names_idx
++);
2926 else if (section
== meth_var_names
)
2928 chain
= &meth_var_names_chain
;
2929 sprintf (buf
, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx
++);
2931 else if (section
== meth_var_types
)
2933 chain
= &meth_var_types_chain
;
2934 sprintf (buf
, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx
++);
2941 if (TREE_VALUE (*chain
) == ident
)
2942 return convert (string_type_node
,
2943 build_unary_op (input_location
,
2944 ADDR_EXPR
, TREE_PURPOSE (*chain
), 1));
2946 chain
= &TREE_CHAIN (*chain
);
2949 type
= build_sized_array_type (char_type_node
, IDENTIFIER_LENGTH (ident
) + 1);
2950 decl
= start_var_decl (type
, buf
);
2951 string_expr
= my_build_string (IDENTIFIER_LENGTH (ident
) + 1,
2952 IDENTIFIER_POINTER (ident
));
2953 TREE_CONSTANT (decl
) = 1;
2954 finish_var_decl (decl
, string_expr
);
2956 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
2958 return convert (string_type_node
, build_unary_op (input_location
,
2959 ADDR_EXPR
, decl
, 1));
2963 objc_declare_alias (tree alias_ident
, tree class_ident
)
2965 tree underlying_class
;
2968 if (current_namespace
!= global_namespace
) {
2969 error ("Objective-C declarations may only appear in global scope");
2971 #endif /* OBJCPLUS */
2973 if (!(underlying_class
= objc_is_class_name (class_ident
)))
2974 warning (0, "cannot find class %qE", class_ident
);
2975 else if (objc_is_class_name (alias_ident
))
2976 warning (0, "class %qE already exists", alias_ident
);
2979 /* Implement @compatibility_alias as a typedef. */
2981 push_lang_context (lang_name_c
); /* extern "C" */
2983 lang_hooks
.decls
.pushdecl (build_decl
2987 xref_tag (RECORD_TYPE
, underlying_class
)));
2989 pop_lang_context ();
2991 alias_chain
= tree_cons (underlying_class
, alias_ident
, alias_chain
);
2996 objc_declare_class (tree ident_list
)
3000 if (current_namespace
!= global_namespace
) {
3001 error ("Objective-C declarations may only appear in global scope");
3003 #endif /* OBJCPLUS */
3005 for (list
= ident_list
; list
; list
= TREE_CHAIN (list
))
3007 tree ident
= TREE_VALUE (list
);
3009 if (! objc_is_class_name (ident
))
3011 tree record
= lookup_name (ident
), type
= record
;
3015 if (TREE_CODE (record
) == TYPE_DECL
)
3016 type
= DECL_ORIGINAL_TYPE (record
);
3018 if (!TYPE_HAS_OBJC_INFO (type
)
3019 || !TYPE_OBJC_INTERFACE (type
))
3021 error ("%qE redeclared as different kind of symbol",
3023 error ("previous declaration of %q+D",
3028 record
= xref_tag (RECORD_TYPE
, ident
);
3029 INIT_TYPE_OBJC_INFO (record
);
3030 TYPE_OBJC_INTERFACE (record
) = ident
;
3031 class_chain
= tree_cons (NULL_TREE
, ident
, class_chain
);
3037 objc_is_class_name (tree ident
)
3041 if (ident
&& TREE_CODE (ident
) == IDENTIFIER_NODE
3042 && identifier_global_value (ident
))
3043 ident
= identifier_global_value (ident
);
3044 while (ident
&& TREE_CODE (ident
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (ident
))
3045 ident
= OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident
));
3047 if (ident
&& TREE_CODE (ident
) == RECORD_TYPE
)
3048 ident
= OBJC_TYPE_NAME (ident
);
3050 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3051 ident
= DECL_NAME (ident
);
3053 if (!ident
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3056 if (lookup_interface (ident
))
3059 for (chain
= class_chain
; chain
; chain
= TREE_CHAIN (chain
))
3061 if (ident
== TREE_VALUE (chain
))
3065 for (chain
= alias_chain
; chain
; chain
= TREE_CHAIN (chain
))
3067 if (ident
== TREE_VALUE (chain
))
3068 return TREE_PURPOSE (chain
);
3074 /* Check whether TYPE is either 'id' or 'Class'. */
3077 objc_is_id (tree type
)
3079 if (type
&& TREE_CODE (type
) == IDENTIFIER_NODE
3080 && identifier_global_value (type
))
3081 type
= identifier_global_value (type
);
3083 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
3084 type
= TREE_TYPE (type
);
3086 /* NB: This function may be called before the ObjC front-end has
3087 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3088 return (objc_object_type
&& type
3089 && (IS_ID (type
) || IS_CLASS (type
) || IS_SUPER (type
))
3094 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3095 class instance. This is needed by other parts of the compiler to
3096 handle ObjC types gracefully. */
3099 objc_is_object_ptr (tree type
)
3103 type
= TYPE_MAIN_VARIANT (type
);
3104 if (!POINTER_TYPE_P (type
))
3107 ret
= objc_is_id (type
);
3109 ret
= objc_is_class_name (TREE_TYPE (type
));
3115 objc_is_gcable_type (tree type
, int or_strong_p
)
3121 if (objc_is_id (TYPE_MAIN_VARIANT (type
)))
3123 if (or_strong_p
&& lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type
)))
3125 if (TREE_CODE (type
) != POINTER_TYPE
&& TREE_CODE (type
) != INDIRECT_REF
)
3127 type
= TREE_TYPE (type
);
3128 if (TREE_CODE (type
) != RECORD_TYPE
)
3130 name
= TYPE_NAME (type
);
3131 return (objc_is_class_name (name
) != NULL_TREE
);
3135 objc_substitute_decl (tree expr
, tree oldexpr
, tree newexpr
)
3137 if (expr
== oldexpr
)
3140 switch (TREE_CODE (expr
))
3143 return objc_build_component_ref
3144 (objc_substitute_decl (TREE_OPERAND (expr
, 0),
3147 DECL_NAME (TREE_OPERAND (expr
, 1)));
3149 return build_array_ref (input_location
,
3150 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3153 TREE_OPERAND (expr
, 1));
3155 return build_indirect_ref (input_location
,
3156 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3158 newexpr
), RO_ARROW
);
3165 objc_build_ivar_assignment (tree outervar
, tree lhs
, tree rhs
)
3168 /* The LHS parameter contains the expression 'outervar->memberspec';
3169 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3170 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3173 = objc_substitute_decl
3174 (lhs
, outervar
, convert (TREE_TYPE (outervar
), integer_zero_node
));
3176 = (flag_objc_direct_dispatch
3177 ? objc_assign_ivar_fast_decl
3178 : objc_assign_ivar_decl
);
3180 offs
= convert (integer_type_node
, build_unary_op (input_location
,
3181 ADDR_EXPR
, offs
, 0));
3183 func_params
= tree_cons (NULL_TREE
,
3184 convert (objc_object_type
, rhs
),
3185 tree_cons (NULL_TREE
, convert (objc_object_type
, outervar
),
3186 tree_cons (NULL_TREE
, offs
,
3189 assemble_external (func
);
3190 return build_function_call (input_location
, func
, func_params
);
3194 objc_build_global_assignment (tree lhs
, tree rhs
)
3196 tree func_params
= tree_cons (NULL_TREE
,
3197 convert (objc_object_type
, rhs
),
3198 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3199 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3202 assemble_external (objc_assign_global_decl
);
3203 return build_function_call (input_location
,
3204 objc_assign_global_decl
, func_params
);
3208 objc_build_strong_cast_assignment (tree lhs
, tree rhs
)
3210 tree func_params
= tree_cons (NULL_TREE
,
3211 convert (objc_object_type
, rhs
),
3212 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3213 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3216 assemble_external (objc_assign_strong_cast_decl
);
3217 return build_function_call (input_location
,
3218 objc_assign_strong_cast_decl
, func_params
);
3222 objc_is_gcable_p (tree expr
)
3224 return (TREE_CODE (expr
) == COMPONENT_REF
3225 ? objc_is_gcable_p (TREE_OPERAND (expr
, 1))
3226 : TREE_CODE (expr
) == ARRAY_REF
3227 ? (objc_is_gcable_p (TREE_TYPE (expr
))
3228 || objc_is_gcable_p (TREE_OPERAND (expr
, 0)))
3229 : TREE_CODE (expr
) == ARRAY_TYPE
3230 ? objc_is_gcable_p (TREE_TYPE (expr
))
3232 ? objc_is_gcable_type (expr
, 1)
3233 : (objc_is_gcable_p (TREE_TYPE (expr
))
3235 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr
)))));
3239 objc_is_ivar_reference_p (tree expr
)
3241 return (TREE_CODE (expr
) == ARRAY_REF
3242 ? objc_is_ivar_reference_p (TREE_OPERAND (expr
, 0))
3243 : TREE_CODE (expr
) == COMPONENT_REF
3244 ? TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
3249 objc_is_global_reference_p (tree expr
)
3251 return (TREE_CODE (expr
) == INDIRECT_REF
|| TREE_CODE (expr
) == PLUS_EXPR
3252 ? objc_is_global_reference_p (TREE_OPERAND (expr
, 0))
3254 ? (DECL_FILE_SCOPE_P (expr
) || TREE_STATIC (expr
))
3259 objc_generate_write_barrier (tree lhs
, enum tree_code modifycode
, tree rhs
)
3261 tree result
= NULL_TREE
, outer
;
3262 int strong_cast_p
= 0, outer_gc_p
= 0, indirect_p
= 0;
3264 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3265 will have been transformed to the form '*(type *)&expr'. */
3266 if (TREE_CODE (lhs
) == INDIRECT_REF
)
3268 outer
= TREE_OPERAND (lhs
, 0);
3270 while (!strong_cast_p
3271 && (CONVERT_EXPR_P (outer
)
3272 || TREE_CODE (outer
) == NON_LVALUE_EXPR
))
3274 tree lhstype
= TREE_TYPE (outer
);
3276 /* Descend down the cast chain, and record the first objc_gc
3278 if (POINTER_TYPE_P (lhstype
))
3281 = lookup_attribute ("objc_gc",
3282 TYPE_ATTRIBUTES (TREE_TYPE (lhstype
)));
3288 outer
= TREE_OPERAND (outer
, 0);
3292 /* If we have a __strong cast, it trumps all else. */
3295 if (modifycode
!= NOP_EXPR
)
3296 goto invalid_pointer_arithmetic
;
3298 if (warn_assign_intercept
)
3299 warning (0, "strong-cast assignment has been intercepted");
3301 result
= objc_build_strong_cast_assignment (lhs
, rhs
);
3306 /* the lhs must be of a suitable type, regardless of its underlying
3308 if (!objc_is_gcable_p (lhs
))
3314 && (TREE_CODE (outer
) == COMPONENT_REF
3315 || TREE_CODE (outer
) == ARRAY_REF
))
3316 outer
= TREE_OPERAND (outer
, 0);
3318 if (TREE_CODE (outer
) == INDIRECT_REF
)
3320 outer
= TREE_OPERAND (outer
, 0);
3324 outer_gc_p
= objc_is_gcable_p (outer
);
3326 /* Handle ivar assignments. */
3327 if (objc_is_ivar_reference_p (lhs
))
3329 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3330 doesn't cut it here), the best we can do here is suggest a cast. */
3331 if (!objc_is_gcable_type (TREE_TYPE (outer
), 0))
3333 /* We may still be able to use the global write barrier... */
3334 if (!indirect_p
&& objc_is_global_reference_p (outer
))
3335 goto global_reference
;
3338 if (modifycode
== NOP_EXPR
)
3340 if (warn_assign_intercept
)
3341 warning (0, "strong-cast may possibly be needed");
3347 if (modifycode
!= NOP_EXPR
)
3348 goto invalid_pointer_arithmetic
;
3350 if (warn_assign_intercept
)
3351 warning (0, "instance variable assignment has been intercepted");
3353 result
= objc_build_ivar_assignment (outer
, lhs
, rhs
);
3358 /* Likewise, intercept assignment to global/static variables if their type is
3360 if (objc_is_global_reference_p (outer
))
3366 if (modifycode
!= NOP_EXPR
)
3368 invalid_pointer_arithmetic
:
3370 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3375 if (warn_assign_intercept
)
3376 warning (0, "global/static variable assignment has been intercepted");
3378 result
= objc_build_global_assignment (lhs
, rhs
);
3381 /* In all other cases, fall back to the normal mechanism. */
3386 struct GTY(()) interface_tuple
{
3391 static GTY ((param_is (struct interface_tuple
))) htab_t interface_htab
;
3394 hash_interface (const void *p
)
3396 const struct interface_tuple
*d
= (const struct interface_tuple
*) p
;
3397 return IDENTIFIER_HASH_VALUE (d
->id
);
3401 eq_interface (const void *p1
, const void *p2
)
3403 const struct interface_tuple
*d
= (const struct interface_tuple
*) p1
;
3408 lookup_interface (tree ident
)
3411 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3412 ident
= DECL_NAME (ident
);
3415 if (ident
== NULL_TREE
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3419 struct interface_tuple
**slot
;
3424 slot
= (struct interface_tuple
**)
3425 htab_find_slot_with_hash (interface_htab
, ident
,
3426 IDENTIFIER_HASH_VALUE (ident
),
3429 i
= (*slot
)->class_name
;
3435 /* Implement @defs (<classname>) within struct bodies. */
3438 objc_get_class_ivars (tree class_name
)
3440 tree interface
= lookup_interface (class_name
);
3443 return get_class_ivars (interface
, true);
3445 error ("cannot find interface declaration for %qE",
3448 return error_mark_node
;
3451 /* Called when checking the variables in a struct. If we are not
3452 doing the ivars list inside an @interface context, then returns
3453 fieldlist unchanged. Else, returns the list of class ivars.
3456 objc_get_interface_ivars (tree fieldlist
)
3458 if (!objc_collecting_ivars
|| !objc_interface_context
3459 || TREE_CODE (objc_interface_context
) != CLASS_INTERFACE_TYPE
3460 || CLASS_SUPER_NAME (objc_interface_context
) == NULL_TREE
)
3463 return get_class_ivars (objc_interface_context
, true);
3466 /* Used by: build_private_template, continue_class,
3467 and for @defs constructs. */
3470 get_class_ivars (tree interface
, bool inherited
)
3472 tree ivar_chain
= copy_list (CLASS_RAW_IVARS (interface
));
3474 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
3475 by the current class (i.e., they do not include super-class ivars).
3476 However, the CLASS_IVARS list will be side-effected by a call to
3477 finish_struct(), which will fill in field offsets. */
3478 if (!CLASS_IVARS (interface
))
3479 CLASS_IVARS (interface
) = ivar_chain
;
3484 while (CLASS_SUPER_NAME (interface
))
3486 /* Prepend super-class ivars. */
3487 interface
= lookup_interface (CLASS_SUPER_NAME (interface
));
3488 ivar_chain
= chainon (copy_list (CLASS_RAW_IVARS (interface
)),
3496 objc_create_temporary_var (tree type
)
3500 decl
= build_decl (input_location
,
3501 VAR_DECL
, NULL_TREE
, type
);
3502 TREE_USED (decl
) = 1;
3503 DECL_ARTIFICIAL (decl
) = 1;
3504 DECL_IGNORED_P (decl
) = 1;
3505 DECL_CONTEXT (decl
) = current_function_decl
;
3510 /* Exception handling constructs. We begin by having the parser do most
3511 of the work and passing us blocks. What we do next depends on whether
3512 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
3513 We abstract all of this in a handful of appropriately named routines. */
3515 /* Stack of open try blocks. */
3517 struct objc_try_context
3519 struct objc_try_context
*outer
;
3521 /* Statements (or statement lists) as processed by the parser. */
3525 /* Some file position locations. */
3526 location_t try_locus
;
3527 location_t end_try_locus
;
3528 location_t end_catch_locus
;
3529 location_t finally_locus
;
3530 location_t end_finally_locus
;
3532 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
3533 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
3536 /* The CATCH_EXPR of an open @catch clause. */
3539 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
3545 static struct objc_try_context
*cur_try_context
;
3547 static GTY(()) tree objc_eh_personality_decl
;
3549 /* This hook, called via lang_eh_runtime_type, generates a runtime object
3550 that represents TYPE. For Objective-C, this is just the class name. */
3551 /* ??? Isn't there a class object or some such? Is it easy to get? */
3555 objc_eh_runtime_type (tree type
)
3557 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type
)), class_names
);
3561 objc_eh_personality (void)
3563 if (!flag_objc_sjlj_exceptions
3564 && !objc_eh_personality_decl
)
3565 objc_eh_personality_decl
3566 = build_personality_function (USING_SJLJ_EXCEPTIONS
3567 ? "__gnu_objc_personality_sj0"
3568 : "__gnu_objc_personality_v0");
3570 return objc_eh_personality_decl
;
3574 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
3575 of Darwin, we'll arrange for it to be initialized (and associated
3576 with a binding) later. */
3579 objc_build_exc_ptr (void)
3581 if (flag_objc_sjlj_exceptions
)
3583 tree var
= cur_try_context
->caught_decl
;
3586 var
= objc_create_temporary_var (objc_object_type
);
3587 cur_try_context
->caught_decl
= var
;
3594 t
= built_in_decls
[BUILT_IN_EH_POINTER
];
3595 t
= build_call_expr (t
, 1, integer_zero_node
);
3596 return fold_convert (objc_object_type
, t
);
3600 /* Build "objc_exception_try_exit(&_stack)". */
3603 next_sjlj_build_try_exit (void)
3606 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3607 t
= tree_cons (NULL
, t
, NULL
);
3608 t
= build_function_call (input_location
,
3609 objc_exception_try_exit_decl
, t
);
3614 objc_exception_try_enter (&_stack);
3615 if (_setjmp(&_stack.buf))
3619 Return the COND_EXPR. Note that the THEN and ELSE fields are left
3620 empty, ready for the caller to fill them in. */
3623 next_sjlj_build_enter_and_setjmp (void)
3625 tree t
, enter
, sj
, cond
;
3627 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3628 t
= tree_cons (NULL
, t
, NULL
);
3629 enter
= build_function_call (input_location
,
3630 objc_exception_try_enter_decl
, t
);
3632 t
= objc_build_component_ref (cur_try_context
->stack_decl
,
3633 get_identifier ("buf"));
3634 t
= build_fold_addr_expr_loc (input_location
, t
);
3636 /* Convert _setjmp argument to type that is expected. */
3637 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl
)))
3638 t
= convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl
))), t
);
3640 t
= convert (ptr_type_node
, t
);
3642 t
= convert (ptr_type_node
, t
);
3644 t
= tree_cons (NULL
, t
, NULL
);
3645 sj
= build_function_call (input_location
,
3646 objc_setjmp_decl
, t
);
3648 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (sj
), enter
, sj
);
3649 cond
= c_common_truthvalue_conversion (input_location
, cond
);
3651 return build3 (COND_EXPR
, void_type_node
, cond
, NULL
, NULL
);
3656 DECL = objc_exception_extract(&_stack); */
3659 next_sjlj_build_exc_extract (tree decl
)
3663 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3664 t
= tree_cons (NULL
, t
, NULL
);
3665 t
= build_function_call (input_location
,
3666 objc_exception_extract_decl
, t
);
3667 t
= convert (TREE_TYPE (decl
), t
);
3668 t
= build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
3674 if (objc_exception_match(obj_get_class(TYPE), _caught)
3681 objc_exception_try_exit(&_stack);
3683 from the sequence of CATCH_EXPRs in the current try context. */
3686 next_sjlj_build_catch_list (void)
3688 tree_stmt_iterator i
= tsi_start (cur_try_context
->catch_list
);
3690 tree
*last
= &catch_seq
;
3691 bool saw_id
= false;
3693 for (; !tsi_end_p (i
); tsi_next (&i
))
3695 tree stmt
= tsi_stmt (i
);
3696 tree type
= CATCH_TYPES (stmt
);
3697 tree body
= CATCH_BODY (stmt
);
3709 if (type
== error_mark_node
)
3710 cond
= error_mark_node
;
3713 args
= tree_cons (NULL
, cur_try_context
->caught_decl
, NULL
);
3714 t
= objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type
)));
3715 args
= tree_cons (NULL
, t
, args
);
3716 t
= build_function_call (input_location
,
3717 objc_exception_match_decl
, args
);
3718 cond
= c_common_truthvalue_conversion (input_location
, t
);
3720 t
= build3 (COND_EXPR
, void_type_node
, cond
, body
, NULL
);
3721 SET_EXPR_LOCATION (t
, EXPR_LOCATION (stmt
));
3724 last
= &COND_EXPR_ELSE (t
);
3730 t
= build2 (MODIFY_EXPR
, void_type_node
, cur_try_context
->rethrow_decl
,
3731 cur_try_context
->caught_decl
);
3732 SET_EXPR_LOCATION (t
, cur_try_context
->end_catch_locus
);
3733 append_to_statement_list (t
, last
);
3735 t
= next_sjlj_build_try_exit ();
3736 SET_EXPR_LOCATION (t
, cur_try_context
->end_catch_locus
);
3737 append_to_statement_list (t
, last
);
3743 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
3744 exception handling. We aim to build:
3747 struct _objc_exception_data _stack;
3751 objc_exception_try_enter (&_stack);
3752 if (_setjmp(&_stack.buf))
3754 id _caught = objc_exception_extract(&_stack);
3755 objc_exception_try_enter (&_stack);
3756 if (_setjmp(&_stack.buf))
3757 _rethrow = objc_exception_extract(&_stack);
3767 objc_exception_try_exit(&_stack);
3770 objc_exception_throw(_rethrow);
3774 If CATCH-LIST is empty, we can omit all of the block containing
3775 "_caught" except for the setting of _rethrow. Note the use of
3776 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
3777 but handles goto and other exits from the block. */
3780 next_sjlj_build_try_catch_finally (void)
3782 tree rethrow_decl
, stack_decl
, t
;
3783 tree catch_seq
, try_fin
, bind
;
3785 /* Create the declarations involved. */
3786 t
= xref_tag (RECORD_TYPE
, get_identifier (UTAG_EXCDATA
));
3787 stack_decl
= objc_create_temporary_var (t
);
3788 cur_try_context
->stack_decl
= stack_decl
;
3790 rethrow_decl
= objc_create_temporary_var (objc_object_type
);
3791 cur_try_context
->rethrow_decl
= rethrow_decl
;
3792 TREE_CHAIN (rethrow_decl
) = stack_decl
;
3794 /* Build the outermost variable binding level. */
3795 bind
= build3 (BIND_EXPR
, void_type_node
, rethrow_decl
, NULL
, NULL
);
3796 SET_EXPR_LOCATION (bind
, cur_try_context
->try_locus
);
3797 TREE_SIDE_EFFECTS (bind
) = 1;
3799 /* Initialize rethrow_decl. */
3800 t
= build2 (MODIFY_EXPR
, void_type_node
, rethrow_decl
,
3801 convert (objc_object_type
, null_pointer_node
));
3802 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
3803 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
3805 /* Build the outermost TRY_FINALLY_EXPR. */
3806 try_fin
= build2 (TRY_FINALLY_EXPR
, void_type_node
, NULL
, NULL
);
3807 SET_EXPR_LOCATION (try_fin
, cur_try_context
->try_locus
);
3808 TREE_SIDE_EFFECTS (try_fin
) = 1;
3809 append_to_statement_list (try_fin
, &BIND_EXPR_BODY (bind
));
3811 /* Create the complete catch sequence. */
3812 if (cur_try_context
->catch_list
)
3814 tree caught_decl
= objc_build_exc_ptr ();
3815 catch_seq
= build_stmt (input_location
, BIND_EXPR
, caught_decl
, NULL
, NULL
);
3816 TREE_SIDE_EFFECTS (catch_seq
) = 1;
3818 t
= next_sjlj_build_exc_extract (caught_decl
);
3819 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
3821 t
= next_sjlj_build_enter_and_setjmp ();
3822 COND_EXPR_THEN (t
) = next_sjlj_build_exc_extract (rethrow_decl
);
3823 COND_EXPR_ELSE (t
) = next_sjlj_build_catch_list ();
3824 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
3827 catch_seq
= next_sjlj_build_exc_extract (rethrow_decl
);
3828 SET_EXPR_LOCATION (catch_seq
, cur_try_context
->end_try_locus
);
3830 /* Build the main register-and-try if statement. */
3831 t
= next_sjlj_build_enter_and_setjmp ();
3832 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
3833 COND_EXPR_THEN (t
) = catch_seq
;
3834 COND_EXPR_ELSE (t
) = cur_try_context
->try_body
;
3835 TREE_OPERAND (try_fin
, 0) = t
;
3837 /* Build the complete FINALLY statement list. */
3838 t
= next_sjlj_build_try_exit ();
3839 t
= build_stmt (input_location
, COND_EXPR
,
3840 c_common_truthvalue_conversion
3841 (input_location
, rethrow_decl
),
3843 SET_EXPR_LOCATION (t
, cur_try_context
->finally_locus
);
3844 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
3846 append_to_statement_list (cur_try_context
->finally_body
,
3847 &TREE_OPERAND (try_fin
, 1));
3849 t
= tree_cons (NULL
, rethrow_decl
, NULL
);
3850 t
= build_function_call (input_location
,
3851 objc_exception_throw_decl
, t
);
3852 t
= build_stmt (input_location
, COND_EXPR
,
3853 c_common_truthvalue_conversion (input_location
,
3856 SET_EXPR_LOCATION (t
, cur_try_context
->end_finally_locus
);
3857 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
3862 /* Called just after parsing the @try and its associated BODY. We now
3863 must prepare for the tricky bits -- handling the catches and finally. */
3866 objc_begin_try_stmt (location_t try_locus
, tree body
)
3868 struct objc_try_context
*c
= XCNEW (struct objc_try_context
);
3869 c
->outer
= cur_try_context
;
3871 c
->try_locus
= try_locus
;
3872 c
->end_try_locus
= input_location
;
3873 cur_try_context
= c
;
3875 /* -fobjc-exceptions is required to enable Objective-C exceptions.
3876 For example, on Darwin, ObjC exceptions require a sufficiently
3877 recent version of the runtime, so the user must ask for them
3878 explicitly. On other platforms, at the moment -fobjc-exceptions
3879 triggers -fexceptions which again is required for exceptions to
3882 if (!flag_objc_exceptions
)
3884 error_at (try_locus
, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
3887 if (flag_objc_sjlj_exceptions
)
3888 objc_mark_locals_volatile (NULL
);
3891 /* Called just after parsing "@catch (parm)". Open a binding level,
3892 enter DECL into the binding level, and initialize it. Leave the
3893 binding level open while the body of the compound statement is parsed. */
3896 objc_begin_catch_clause (tree decl
)
3898 tree compound
, type
, t
;
3900 /* Begin a new scope that the entire catch clause will live in. */
3901 compound
= c_begin_compound_stmt (true);
3903 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
3904 decl
= build_decl (input_location
,
3905 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
3906 lang_hooks
.decls
.pushdecl (decl
);
3908 /* Since a decl is required here by syntax, don't warn if its unused. */
3909 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
3910 be what the previous objc implementation did. */
3911 TREE_USED (decl
) = 1;
3912 DECL_READ_P (decl
) = 1;
3914 /* Verify that the type of the catch is valid. It must be a pointer
3915 to an Objective-C class, or "id" (which is catch-all). */
3916 type
= TREE_TYPE (decl
);
3918 if (POINTER_TYPE_P (type
) && objc_is_object_id (TREE_TYPE (type
)))
3920 else if (!POINTER_TYPE_P (type
) || !TYPED_OBJECT (TREE_TYPE (type
)))
3922 error ("@catch parameter is not a known Objective-C class type");
3923 type
= error_mark_node
;
3925 else if (cur_try_context
->catch_list
)
3927 /* Examine previous @catch clauses and see if we've already
3928 caught the type in question. */
3929 tree_stmt_iterator i
= tsi_start (cur_try_context
->catch_list
);
3930 for (; !tsi_end_p (i
); tsi_next (&i
))
3932 tree stmt
= tsi_stmt (i
);
3933 t
= CATCH_TYPES (stmt
);
3934 if (t
== error_mark_node
)
3936 if (!t
|| DERIVED_FROM_P (TREE_TYPE (t
), TREE_TYPE (type
)))
3938 warning (0, "exception of type %<%T%> will be caught",
3940 warning_at (EXPR_LOCATION (stmt
), 0, " by earlier handler for %<%T%>",
3941 TREE_TYPE (t
? t
: objc_object_type
));
3947 /* Record the data for the catch in the try context so that we can
3948 finalize it later. */
3949 t
= build_stmt (input_location
, CATCH_EXPR
, type
, compound
);
3950 cur_try_context
->current_catch
= t
;
3952 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
3953 t
= objc_build_exc_ptr ();
3954 t
= convert (TREE_TYPE (decl
), t
);
3955 t
= build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
3959 /* Called just after parsing the closing brace of a @catch clause. Close
3960 the open binding level, and record a CATCH_EXPR for it. */
3963 objc_finish_catch_clause (void)
3965 tree c
= cur_try_context
->current_catch
;
3966 cur_try_context
->current_catch
= NULL
;
3967 cur_try_context
->end_catch_locus
= input_location
;
3969 CATCH_BODY (c
) = c_end_compound_stmt (input_location
, CATCH_BODY (c
), 1);
3970 append_to_statement_list (c
, &cur_try_context
->catch_list
);
3973 /* Called after parsing a @finally clause and its associated BODY.
3974 Record the body for later placement. */
3977 objc_build_finally_clause (location_t finally_locus
, tree body
)
3979 cur_try_context
->finally_body
= body
;
3980 cur_try_context
->finally_locus
= finally_locus
;
3981 cur_try_context
->end_finally_locus
= input_location
;
3984 /* Called to finalize a @try construct. */
3987 objc_finish_try_stmt (void)
3989 struct objc_try_context
*c
= cur_try_context
;
3992 if (c
->catch_list
== NULL
&& c
->finally_body
== NULL
)
3993 error ("%<@try%> without %<@catch%> or %<@finally%>");
3995 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
3996 if (flag_objc_sjlj_exceptions
)
3998 bool save
= in_late_binary_op
;
3999 in_late_binary_op
= true;
4000 if (!cur_try_context
->finally_body
)
4002 cur_try_context
->finally_locus
= input_location
;
4003 cur_try_context
->end_finally_locus
= input_location
;
4005 stmt
= next_sjlj_build_try_catch_finally ();
4006 in_late_binary_op
= save
;
4010 /* Otherwise, nest the CATCH inside a FINALLY. */
4014 stmt
= build_stmt (input_location
, TRY_CATCH_EXPR
, stmt
, c
->catch_list
);
4015 SET_EXPR_LOCATION (stmt
, cur_try_context
->try_locus
);
4017 if (c
->finally_body
)
4019 stmt
= build_stmt (input_location
, TRY_FINALLY_EXPR
, stmt
, c
->finally_body
);
4020 SET_EXPR_LOCATION (stmt
, cur_try_context
->try_locus
);
4025 cur_try_context
= c
->outer
;
4031 objc_build_throw_stmt (location_t loc
, tree throw_expr
)
4035 if (!flag_objc_exceptions
)
4037 error_at (loc
, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4040 if (throw_expr
== NULL
)
4042 /* If we're not inside a @catch block, there is no "current
4043 exception" to be rethrown. */
4044 if (cur_try_context
== NULL
4045 || cur_try_context
->current_catch
== NULL
)
4047 error_at (loc
, "%<@throw%> (rethrow) used outside of a @catch block");
4051 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4052 value that we get from the runtime. */
4053 throw_expr
= objc_build_exc_ptr ();
4056 /* A throw is just a call to the runtime throw function with the
4057 object as a parameter. */
4058 args
= tree_cons (NULL
, throw_expr
, NULL
);
4059 return add_stmt (build_function_call (loc
,
4060 objc_exception_throw_decl
, args
));
4064 objc_build_synchronized (location_t start_locus
, tree mutex
, tree body
)
4068 /* First lock the mutex. */
4069 mutex
= save_expr (mutex
);
4070 args
= tree_cons (NULL
, mutex
, NULL
);
4071 call
= build_function_call (input_location
,
4072 objc_sync_enter_decl
, args
);
4073 SET_EXPR_LOCATION (call
, start_locus
);
4076 /* Build the mutex unlock. */
4077 args
= tree_cons (NULL
, mutex
, NULL
);
4078 call
= build_function_call (input_location
,
4079 objc_sync_exit_decl
, args
);
4080 SET_EXPR_LOCATION (call
, input_location
);
4082 /* Put the that and the body in a TRY_FINALLY. */
4083 objc_begin_try_stmt (start_locus
, body
);
4084 objc_build_finally_clause (input_location
, call
);
4085 return objc_finish_try_stmt ();
4089 /* Predefine the following data type:
4091 struct _objc_exception_data
4093 int buf[OBJC_JBLEN];
4097 /* The following yuckiness should prevent users from having to #include
4098 <setjmp.h> in their code... */
4100 /* Define to a harmless positive value so the below code doesn't die. */
4102 #define OBJC_JBLEN 18
4106 build_next_objc_exception_stuff (void)
4108 tree decls
, temp_type
, *chain
= NULL
;
4110 objc_exception_data_template
4111 = objc_start_struct (get_identifier (UTAG_EXCDATA
));
4113 /* int buf[OBJC_JBLEN]; */
4115 temp_type
= build_sized_array_type (integer_type_node
, OBJC_JBLEN
);
4116 decls
= add_field_decl (temp_type
, "buf", &chain
);
4118 /* void *pointers[4]; */
4120 temp_type
= build_sized_array_type (ptr_type_node
, 4);
4121 add_field_decl (temp_type
, "pointers", &chain
);
4123 objc_finish_struct (objc_exception_data_template
, decls
);
4125 /* int _setjmp(...); */
4126 /* If the user includes <setjmp.h>, this shall be superseded by
4127 'int _setjmp(jmp_buf);' */
4128 temp_type
= build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
4130 = add_builtin_function (TAG_SETJMP
, temp_type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
4132 /* id objc_exception_extract(struct _objc_exception_data *); */
4134 = build_function_type_list (objc_object_type
,
4135 build_pointer_type (objc_exception_data_template
),
4137 objc_exception_extract_decl
4138 = add_builtin_function (TAG_EXCEPTIONEXTRACT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4140 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4141 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4143 = build_function_type_list (void_type_node
,
4144 build_pointer_type (objc_exception_data_template
),
4146 objc_exception_try_enter_decl
4147 = add_builtin_function (TAG_EXCEPTIONTRYENTER
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4149 objc_exception_try_exit_decl
4150 = add_builtin_function (TAG_EXCEPTIONTRYEXIT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4153 /* int objc_exception_match(id, id); */
4155 = build_function_type_list (integer_type_node
,
4156 objc_object_type
, objc_object_type
, NULL_TREE
);
4157 objc_exception_match_decl
4158 = add_builtin_function (TAG_EXCEPTIONMATCH
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4161 /* id objc_assign_ivar (id, id, unsigned int); */
4162 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4163 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4165 = build_function_type_list (objc_object_type
,
4170 objc_assign_ivar_decl
4171 = add_builtin_function (TAG_ASSIGNIVAR
, temp_type
, 0, NOT_BUILT_IN
,
4173 #ifdef OFFS_ASSIGNIVAR_FAST
4174 objc_assign_ivar_fast_decl
4175 = add_builtin_function (TAG_ASSIGNIVAR_FAST
, temp_type
, 0,
4176 NOT_BUILT_IN
, NULL
, NULL_TREE
);
4177 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl
)
4178 = tree_cons (get_identifier ("hard_coded_address"),
4179 build_int_cst (NULL_TREE
, OFFS_ASSIGNIVAR_FAST
),
4182 /* Default to slower ivar method. */
4183 objc_assign_ivar_fast_decl
= objc_assign_ivar_decl
;
4186 /* id objc_assign_global (id, id *); */
4187 /* id objc_assign_strongCast (id, id *); */
4188 temp_type
= build_function_type_list (objc_object_type
,
4190 build_pointer_type (objc_object_type
),
4192 objc_assign_global_decl
4193 = add_builtin_function (TAG_ASSIGNGLOBAL
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4195 objc_assign_strong_cast_decl
4196 = add_builtin_function (TAG_ASSIGNSTRONGCAST
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4201 build_objc_exception_stuff (void)
4203 tree noreturn_list
, nothrow_list
, temp_type
;
4205 noreturn_list
= tree_cons (get_identifier ("noreturn"), NULL
, NULL
);
4206 nothrow_list
= tree_cons (get_identifier ("nothrow"), NULL
, NULL
);
4208 /* void objc_exception_throw(id) __attribute__((noreturn)); */
4209 /* void objc_sync_enter(id); */
4210 /* void objc_sync_exit(id); */
4211 temp_type
= build_function_type_list (void_type_node
,
4214 objc_exception_throw_decl
4215 = add_builtin_function (TAG_EXCEPTIONTHROW
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4217 objc_sync_enter_decl
4218 = add_builtin_function (TAG_SYNCENTER
, temp_type
, 0, NOT_BUILT_IN
,
4219 NULL
, nothrow_list
);
4221 = add_builtin_function (TAG_SYNCEXIT
, temp_type
, 0, NOT_BUILT_IN
,
4222 NULL
, nothrow_list
);
4225 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4228 struct <classname> {
4229 struct _objc_class *isa;
4234 build_private_template (tree klass
)
4236 if (!CLASS_STATIC_TEMPLATE (klass
))
4238 tree record
= objc_build_struct (klass
,
4239 get_class_ivars (klass
, false),
4240 CLASS_SUPER_NAME (klass
));
4242 /* Set the TREE_USED bit for this struct, so that stab generator
4243 can emit stabs for this struct type. */
4244 if (flag_debug_only_used_symbols
&& TYPE_STUB_DECL (record
))
4245 TREE_USED (TYPE_STUB_DECL (record
)) = 1;
4249 /* Begin code generation for protocols... */
4251 /* struct _objc_protocol {
4252 struct _objc_class *isa;
4253 char *protocol_name;
4254 struct _objc_protocol **protocol_list;
4255 struct _objc__method_prototype_list *instance_methods;
4256 struct _objc__method_prototype_list *class_methods;
4260 build_protocol_template (void)
4262 tree ptype
, decls
, *chain
= NULL
;
4264 objc_protocol_template
= objc_start_struct (get_identifier (UTAG_PROTOCOL
));
4266 /* struct _objc_class *isa; */
4267 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
4268 get_identifier (UTAG_CLASS
)));
4269 decls
= add_field_decl (ptype
, "isa", &chain
);
4271 /* char *protocol_name; */
4272 add_field_decl (string_type_node
, "protocol_name", &chain
);
4274 /* struct _objc_protocol **protocol_list; */
4275 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
4276 add_field_decl (ptype
, "protocol_list", &chain
);
4278 /* struct _objc__method_prototype_list *instance_methods; */
4279 add_field_decl (objc_method_proto_list_ptr
, "instance_methods", &chain
);
4281 /* struct _objc__method_prototype_list *class_methods; */
4282 add_field_decl (objc_method_proto_list_ptr
, "class_methods", &chain
);
4284 objc_finish_struct (objc_protocol_template
, decls
);
4288 build_descriptor_table_initializer (tree type
, tree entries
)
4290 VEC(constructor_elt
,gc
) *inits
= NULL
;
4294 VEC(constructor_elt
,gc
) *elts
= NULL
;
4296 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
4297 build_selector (METHOD_SEL_NAME (entries
)));
4298 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
4299 add_objc_string (METHOD_ENCODING (entries
),
4302 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
4303 objc_build_constructor (type
, elts
));
4305 entries
= DECL_CHAIN (entries
);
4309 return objc_build_constructor (build_array_type (type
, 0), inits
);
4312 /* struct objc_method_prototype_list {
4314 struct objc_method_prototype {
4321 build_method_prototype_list_template (tree list_type
, int size
)
4323 tree objc_ivar_list_record
;
4324 tree array_type
, decls
, *chain
= NULL
;
4326 /* Generate an unnamed struct definition. */
4328 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
4330 /* int method_count; */
4331 decls
= add_field_decl (integer_type_node
, "method_count", &chain
);
4333 /* struct objc_method method_list[]; */
4334 array_type
= build_sized_array_type (list_type
, size
);
4335 add_field_decl (array_type
, "method_list", &chain
);
4337 objc_finish_struct (objc_ivar_list_record
, decls
);
4339 return objc_ivar_list_record
;
4343 build_method_prototype_template (void)
4346 tree decls
, *chain
= NULL
;
4348 proto_record
= objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE
));
4351 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
4353 /* char *method_types; */
4354 add_field_decl (string_type_node
, "method_types", &chain
);
4356 objc_finish_struct (proto_record
, decls
);
4358 return proto_record
;
4362 objc_method_parm_type (tree type
)
4364 type
= TREE_VALUE (TREE_TYPE (type
));
4365 if (TREE_CODE (type
) == TYPE_DECL
)
4366 type
= TREE_TYPE (type
);
4371 objc_encoded_type_size (tree type
)
4373 int sz
= int_size_in_bytes (type
);
4375 /* Make all integer and enum types at least as large
4377 if (sz
> 0 && INTEGRAL_TYPE_P (type
))
4378 sz
= MAX (sz
, int_size_in_bytes (integer_type_node
));
4379 /* Treat arrays as pointers, since that's how they're
4381 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4382 sz
= int_size_in_bytes (ptr_type_node
);
4387 encode_method_prototype (tree method_decl
)
4394 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
4395 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl
)));
4397 /* Encode return type. */
4398 encode_type (objc_method_parm_type (method_decl
),
4399 obstack_object_size (&util_obstack
),
4400 OBJC_ENCODE_INLINE_DEFS
);
4403 /* The first two arguments (self and _cmd) are pointers; account for
4405 i
= int_size_in_bytes (ptr_type_node
);
4406 parm_offset
= 2 * i
;
4407 for (parms
= METHOD_SEL_ARGS (method_decl
); parms
;
4408 parms
= DECL_CHAIN (parms
))
4410 tree type
= objc_method_parm_type (parms
);
4411 int sz
= objc_encoded_type_size (type
);
4413 /* If a type size is not known, bail out. */
4416 error ("type %q+D does not have a known size",
4418 /* Pretend that the encoding succeeded; the compilation will
4419 fail nevertheless. */
4420 goto finish_encoding
;
4425 sprintf (buf
, "%d@0:%d", parm_offset
, i
);
4426 obstack_grow (&util_obstack
, buf
, strlen (buf
));
4428 /* Argument types. */
4429 parm_offset
= 2 * i
;
4430 for (parms
= METHOD_SEL_ARGS (method_decl
); parms
;
4431 parms
= DECL_CHAIN (parms
))
4433 tree type
= objc_method_parm_type (parms
);
4435 /* Process argument qualifiers for user supplied arguments. */
4436 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms
)));
4439 encode_type (type
, obstack_object_size (&util_obstack
),
4440 OBJC_ENCODE_INLINE_DEFS
);
4442 /* Compute offset. */
4443 sprintf (buf
, "%d", parm_offset
);
4444 parm_offset
+= objc_encoded_type_size (type
);
4446 obstack_grow (&util_obstack
, buf
, strlen (buf
));
4450 obstack_1grow (&util_obstack
, '\0');
4451 result
= get_identifier (XOBFINISH (&util_obstack
, char *));
4452 obstack_free (&util_obstack
, util_firstobj
);
4457 generate_descriptor_table (tree type
, const char *name
, int size
, tree list
,
4461 VEC(constructor_elt
,gc
) *v
= NULL
;
4463 decl
= start_var_decl (type
, synth_id_with_class_suffix (name
, proto
));
4465 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
4466 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, list
);
4468 finish_var_decl (decl
, objc_build_constructor (type
, v
));
4474 generate_method_descriptors (tree protocol
)
4476 tree initlist
, chain
, method_list_template
;
4479 if (!objc_method_prototype_template
)
4480 objc_method_prototype_template
= build_method_prototype_template ();
4482 chain
= PROTOCOL_CLS_METHODS (protocol
);
4485 size
= list_length (chain
);
4487 method_list_template
4488 = build_method_prototype_list_template (objc_method_prototype_template
,
4492 = build_descriptor_table_initializer (objc_method_prototype_template
,
4495 UOBJC_CLASS_METHODS_decl
4496 = generate_descriptor_table (method_list_template
,
4497 "_OBJC_PROTOCOL_CLASS_METHODS",
4498 size
, initlist
, protocol
);
4501 UOBJC_CLASS_METHODS_decl
= 0;
4503 chain
= PROTOCOL_NST_METHODS (protocol
);
4506 size
= list_length (chain
);
4508 method_list_template
4509 = build_method_prototype_list_template (objc_method_prototype_template
,
4512 = build_descriptor_table_initializer (objc_method_prototype_template
,
4515 UOBJC_INSTANCE_METHODS_decl
4516 = generate_descriptor_table (method_list_template
,
4517 "_OBJC_PROTOCOL_INSTANCE_METHODS",
4518 size
, initlist
, protocol
);
4521 UOBJC_INSTANCE_METHODS_decl
= 0;
4525 generate_protocol_references (tree plist
)
4529 /* Forward declare protocols referenced. */
4530 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
4532 tree proto
= TREE_VALUE (lproto
);
4534 if (TREE_CODE (proto
) == PROTOCOL_INTERFACE_TYPE
4535 && PROTOCOL_NAME (proto
))
4537 if (! PROTOCOL_FORWARD_DECL (proto
))
4538 build_protocol_reference (proto
);
4540 if (PROTOCOL_LIST (proto
))
4541 generate_protocol_references (PROTOCOL_LIST (proto
));
4546 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4550 objc_generate_cxx_ctor_or_dtor (bool dtor
)
4552 tree fn
, body
, compound_stmt
, ivar
;
4554 /* - (id) .cxx_construct { ... return self; } */
4555 /* - (void) .cxx_construct { ... } */
4557 objc_set_method_type (MINUS_EXPR
);
4558 objc_start_method_definition
4559 (objc_build_method_signature (build_tree_list (NULL_TREE
,
4562 : objc_object_type
),
4563 get_identifier (dtor
4565 : TAG_CXX_CONSTRUCT
),
4566 make_node (TREE_LIST
),
4568 body
= begin_function_body ();
4569 compound_stmt
= begin_compound_stmt (0);
4571 ivar
= CLASS_IVARS (implementation_template
);
4572 /* Destroy ivars in reverse order. */
4574 ivar
= nreverse (copy_list (ivar
));
4576 for (; ivar
; ivar
= TREE_CHAIN (ivar
))
4578 if (TREE_CODE (ivar
) == FIELD_DECL
)
4580 tree type
= TREE_TYPE (ivar
);
4582 /* Call the ivar's default constructor or destructor. Do not
4583 call the destructor unless a corresponding constructor call
4584 has also been made (or is not needed). */
4585 if (MAYBE_CLASS_TYPE_P (type
)
4587 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4588 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4589 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4590 : (TYPE_NEEDS_CONSTRUCTING (type
)
4591 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))))
4593 (build_special_member_call
4594 (build_ivar_reference (DECL_NAME (ivar
)),
4595 dtor
? complete_dtor_identifier
: complete_ctor_identifier
,
4596 NULL
, type
, LOOKUP_NORMAL
, tf_warning_or_error
));
4600 /* The constructor returns 'self'. */
4602 finish_return_stmt (self_decl
);
4604 finish_compound_stmt (compound_stmt
);
4605 finish_function_body (body
);
4606 fn
= current_function_decl
;
4608 objc_finish_method_definition (fn
);
4611 /* The following routine will examine the current @interface for any
4612 non-POD C++ ivars requiring non-trivial construction and/or
4613 destruction, and then synthesize special '- .cxx_construct' and/or
4614 '- .cxx_destruct' methods which will run the appropriate
4615 construction or destruction code. Note that ivars inherited from
4616 super-classes are _not_ considered. */
4618 objc_generate_cxx_cdtors (void)
4620 bool need_ctor
= false, need_dtor
= false;
4623 /* We do not want to do this for categories, since they do not have
4626 if (TREE_CODE (objc_implementation_context
) != CLASS_IMPLEMENTATION_TYPE
)
4629 /* First, determine if we even need a constructor and/or destructor. */
4631 for (ivar
= CLASS_IVARS (implementation_template
); ivar
;
4632 ivar
= TREE_CHAIN (ivar
))
4634 if (TREE_CODE (ivar
) == FIELD_DECL
)
4636 tree type
= TREE_TYPE (ivar
);
4638 if (MAYBE_CLASS_TYPE_P (type
))
4640 if (TYPE_NEEDS_CONSTRUCTING (type
)
4641 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
4642 /* NB: If a default constructor is not available, we will not
4643 be able to initialize this ivar; the add_instance_variable()
4644 routine will already have warned about this. */
4647 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4648 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4649 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4650 /* NB: If a default constructor is not available, we will not
4651 call the destructor either, for symmetry. */
4657 /* Generate '- .cxx_construct' if needed. */
4660 objc_generate_cxx_ctor_or_dtor (false);
4662 /* Generate '- .cxx_destruct' if needed. */
4665 objc_generate_cxx_ctor_or_dtor (true);
4667 /* The 'imp_list' variable points at an imp_entry record for the current
4668 @implementation. Record the existence of '- .cxx_construct' and/or
4669 '- .cxx_destruct' methods therein; it will be included in the
4670 metadata for the class. */
4671 if (flag_next_runtime
)
4672 imp_list
->has_cxx_cdtors
= (need_ctor
|| need_dtor
);
4676 /* For each protocol which was referenced either from a @protocol()
4677 expression, or because a class/category implements it (then a
4678 pointer to the protocol is stored in the struct describing the
4679 class/category), we create a statically allocated instance of the
4680 Protocol class. The code is written in such a way as to generate
4681 as few Protocol objects as possible; we generate a unique Protocol
4682 instance for each protocol, and we don't generate a Protocol
4683 instance if the protocol is never referenced (either from a
4684 @protocol() or from a class/category implementation). These
4685 statically allocated objects can be referred to via the static
4686 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
4688 The statically allocated Protocol objects that we generate here
4689 need to be fixed up at runtime in order to be used: the 'isa'
4690 pointer of the objects need to be set up to point to the 'Protocol'
4691 class, as known at runtime.
4693 The NeXT runtime fixes up all protocols at program startup time,
4694 before main() is entered. It uses a low-level trick to look up all
4695 those symbols, then loops on them and fixes them up.
4697 The GNU runtime as well fixes up all protocols before user code
4698 from the module is executed; it requires pointers to those symbols
4699 to be put in the objc_symtab (which is then passed as argument to
4700 the function __objc_exec_class() which the compiler sets up to be
4701 executed automatically when the module is loaded); setup of those
4702 Protocol objects happen in two ways in the GNU runtime: all
4703 Protocol objects referred to by a class or category implementation
4704 are fixed up when the class/category is loaded; all Protocol
4705 objects referred to by a @protocol() expression are added by the
4706 compiler to the list of statically allocated instances to fixup
4707 (the same list holding the statically allocated constant string
4708 objects). Because, as explained above, the compiler generates as
4709 few Protocol objects as possible, some Protocol object might end up
4710 being referenced multiple times when compiled with the GNU runtime,
4711 and end up being fixed up multiple times at runtime initialization.
4712 But that doesn't hurt, it's just a little inefficient. */
4715 generate_protocols (void)
4719 tree initlist
, protocol_name_expr
, refs_decl
, refs_expr
;
4721 /* If a protocol was directly referenced, pull in indirect references. */
4722 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
4723 if (PROTOCOL_FORWARD_DECL (p
) && PROTOCOL_LIST (p
))
4724 generate_protocol_references (PROTOCOL_LIST (p
));
4726 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
4728 tree nst_methods
= PROTOCOL_NST_METHODS (p
);
4729 tree cls_methods
= PROTOCOL_CLS_METHODS (p
);
4731 /* If protocol wasn't referenced, don't generate any code. */
4732 decl
= PROTOCOL_FORWARD_DECL (p
);
4737 /* Make sure we link in the Protocol class. */
4738 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME
));
4742 if (! METHOD_ENCODING (nst_methods
))
4744 encoding
= encode_method_prototype (nst_methods
);
4745 METHOD_ENCODING (nst_methods
) = encoding
;
4747 nst_methods
= DECL_CHAIN (nst_methods
);
4752 if (! METHOD_ENCODING (cls_methods
))
4754 encoding
= encode_method_prototype (cls_methods
);
4755 METHOD_ENCODING (cls_methods
) = encoding
;
4758 cls_methods
= DECL_CHAIN (cls_methods
);
4760 generate_method_descriptors (p
);
4762 if (PROTOCOL_LIST (p
))
4763 refs_decl
= generate_protocol_list (p
);
4767 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
4768 protocol_name_expr
= add_objc_string (PROTOCOL_NAME (p
), class_names
);
4771 refs_expr
= convert (build_pointer_type (build_pointer_type
4772 (objc_protocol_template
)),
4773 build_unary_op (input_location
,
4774 ADDR_EXPR
, refs_decl
, 0));
4776 refs_expr
= build_int_cst (NULL_TREE
, 0);
4778 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
4779 by generate_method_descriptors, which is called above. */
4780 initlist
= build_protocol_initializer (TREE_TYPE (decl
),
4781 protocol_name_expr
, refs_expr
,
4782 UOBJC_INSTANCE_METHODS_decl
,
4783 UOBJC_CLASS_METHODS_decl
);
4784 finish_var_decl (decl
, initlist
);
4789 build_protocol_initializer (tree type
, tree protocol_name
,
4790 tree protocol_list
, tree instance_methods
,
4794 tree cast_type
= build_pointer_type
4795 (xref_tag (RECORD_TYPE
,
4796 get_identifier (UTAG_CLASS
)));
4797 VEC(constructor_elt
,gc
) *inits
= NULL
;
4799 /* Filling the "isa" in with one allows the runtime system to
4800 detect that the version change...should remove before final release. */
4802 expr
= build_int_cst (cast_type
, PROTOCOL_VERSION
);
4803 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4804 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_name
);
4805 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_list
);
4807 if (!instance_methods
)
4808 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
4811 expr
= convert (objc_method_proto_list_ptr
,
4812 build_unary_op (input_location
,
4813 ADDR_EXPR
, instance_methods
, 0));
4814 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4818 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
4821 expr
= convert (objc_method_proto_list_ptr
,
4822 build_unary_op (input_location
,
4823 ADDR_EXPR
, class_methods
, 0));
4824 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4827 return objc_build_constructor (type
, inits
);
4830 /* struct _objc_category {
4831 char *category_name;
4833 struct _objc_method_list *instance_methods;
4834 struct _objc_method_list *class_methods;
4835 struct _objc_protocol_list *protocols;
4839 build_category_template (void)
4841 tree ptype
, decls
, *chain
= NULL
;
4843 objc_category_template
= objc_start_struct (get_identifier (UTAG_CATEGORY
));
4845 /* char *category_name; */
4846 decls
= add_field_decl (string_type_node
, "category_name", &chain
);
4848 /* char *class_name; */
4849 add_field_decl (string_type_node
, "class_name", &chain
);
4851 /* struct _objc_method_list *instance_methods; */
4852 add_field_decl (objc_method_list_ptr
, "instance_methods", &chain
);
4854 /* struct _objc_method_list *class_methods; */
4855 add_field_decl (objc_method_list_ptr
, "class_methods", &chain
);
4857 /* struct _objc_protocol **protocol_list; */
4858 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
4859 add_field_decl (ptype
, "protocol_list", &chain
);
4861 objc_finish_struct (objc_category_template
, decls
);
4864 /* struct _objc_selector {
4870 build_selector_template (void)
4872 tree decls
, *chain
= NULL
;
4874 objc_selector_template
= objc_start_struct (get_identifier (UTAG_SELECTOR
));
4877 decls
= add_field_decl (objc_selector_type
, "sel_id", &chain
);
4879 /* char *sel_type; */
4880 add_field_decl (string_type_node
, "sel_type", &chain
);
4882 objc_finish_struct (objc_selector_template
, decls
);
4885 /* struct _objc_class {
4886 struct _objc_class *isa;
4887 struct _objc_class *super_class;
4892 struct _objc_ivar_list *ivars;
4893 struct _objc_method_list *methods;
4894 #ifdef __NEXT_RUNTIME__
4895 struct objc_cache *cache;
4897 struct sarray *dtable;
4898 struct _objc_class *subclass_list;
4899 struct _objc_class *sibling_class;
4901 struct _objc_protocol_list *protocols;
4902 #ifdef __NEXT_RUNTIME__
4905 void *gc_object_type;
4908 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
4909 the NeXT/Apple runtime; still, the compiler must generate them to
4910 maintain backward binary compatibility (and to allow for future
4914 build_class_template (void)
4916 tree ptype
, decls
, *chain
= NULL
;
4918 objc_class_template
= objc_start_struct (get_identifier (UTAG_CLASS
));
4920 /* struct _objc_class *isa; */
4921 decls
= add_field_decl (build_pointer_type (objc_class_template
),
4924 /* struct _objc_class *super_class; */
4925 add_field_decl (build_pointer_type (objc_class_template
),
4926 "super_class", &chain
);
4929 add_field_decl (string_type_node
, "name", &chain
);
4932 add_field_decl (long_integer_type_node
, "version", &chain
);
4935 add_field_decl (long_integer_type_node
, "info", &chain
);
4937 /* long instance_size; */
4938 add_field_decl (long_integer_type_node
, "instance_size", &chain
);
4940 /* struct _objc_ivar_list *ivars; */
4941 add_field_decl (objc_ivar_list_ptr
,"ivars", &chain
);
4943 /* struct _objc_method_list *methods; */
4944 add_field_decl (objc_method_list_ptr
, "methods", &chain
);
4946 if (flag_next_runtime
)
4948 /* struct objc_cache *cache; */
4949 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
4950 get_identifier ("objc_cache")));
4951 add_field_decl (ptype
, "cache", &chain
);
4955 /* struct sarray *dtable; */
4956 ptype
= build_pointer_type(xref_tag (RECORD_TYPE
,
4957 get_identifier ("sarray")));
4958 add_field_decl (ptype
, "dtable", &chain
);
4960 /* struct objc_class *subclass_list; */
4961 ptype
= build_pointer_type (objc_class_template
);
4962 add_field_decl (ptype
, "subclass_list", &chain
);
4964 /* struct objc_class *sibling_class; */
4965 ptype
= build_pointer_type (objc_class_template
);
4966 add_field_decl (ptype
, "sibling_class", &chain
);
4969 /* struct _objc_protocol **protocol_list; */
4970 ptype
= build_pointer_type (build_pointer_type
4971 (xref_tag (RECORD_TYPE
,
4972 get_identifier (UTAG_PROTOCOL
))));
4973 add_field_decl (ptype
, "protocol_list", &chain
);
4975 if (flag_next_runtime
)
4978 add_field_decl (build_pointer_type (void_type_node
), "sel_id", &chain
);
4981 /* void *gc_object_type; */
4982 add_field_decl (build_pointer_type (void_type_node
),
4983 "gc_object_type", &chain
);
4985 objc_finish_struct (objc_class_template
, decls
);
4988 /* Generate appropriate forward declarations for an implementation. */
4991 synth_forward_declarations (void)
4995 /* static struct objc_class _OBJC_CLASS_<my_name>; */
4996 UOBJC_CLASS_decl
= build_metadata_decl ("_OBJC_CLASS",
4997 objc_class_template
);
4999 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
5000 UOBJC_METACLASS_decl
= build_metadata_decl ("_OBJC_METACLASS",
5001 objc_class_template
);
5003 /* Pre-build the following entities - for speed/convenience. */
5005 an_id
= get_identifier ("super_class");
5006 ucls_super_ref
= objc_build_component_ref (UOBJC_CLASS_decl
, an_id
);
5007 uucls_super_ref
= objc_build_component_ref (UOBJC_METACLASS_decl
, an_id
);
5011 error_with_ivar (const char *message
, tree decl
)
5013 error_at (DECL_SOURCE_LOCATION (decl
), "%s %qs",
5014 message
, identifier_to_locale (gen_declaration (decl
)));
5019 check_ivars (tree inter
, tree imp
)
5021 tree intdecls
= CLASS_RAW_IVARS (inter
);
5022 tree impdecls
= CLASS_RAW_IVARS (imp
);
5029 if (intdecls
&& TREE_CODE (intdecls
) == TYPE_DECL
)
5030 intdecls
= TREE_CHAIN (intdecls
);
5032 if (intdecls
== 0 && impdecls
== 0)
5034 if (intdecls
== 0 || impdecls
== 0)
5036 error ("inconsistent instance variable specification");
5040 t1
= TREE_TYPE (intdecls
); t2
= TREE_TYPE (impdecls
);
5042 if (!comptypes (t1
, t2
)
5043 || !tree_int_cst_equal (DECL_INITIAL (intdecls
),
5044 DECL_INITIAL (impdecls
)))
5046 if (DECL_NAME (intdecls
) == DECL_NAME (impdecls
))
5048 error_with_ivar ("conflicting instance variable type",
5050 error_with_ivar ("previous declaration of",
5053 else /* both the type and the name don't match */
5055 error ("inconsistent instance variable specification");
5060 else if (DECL_NAME (intdecls
) != DECL_NAME (impdecls
))
5062 error_with_ivar ("conflicting instance variable name",
5064 error_with_ivar ("previous declaration of",
5068 intdecls
= DECL_CHAIN (intdecls
);
5069 impdecls
= DECL_CHAIN (impdecls
);
5073 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
5074 This needs to be done just once per compilation. */
5076 /* struct _objc_super {
5077 struct _objc_object *self;
5078 struct _objc_class *super_class;
5082 build_super_template (void)
5084 tree decls
, *chain
= NULL
;
5086 objc_super_template
= objc_start_struct (get_identifier (UTAG_SUPER
));
5088 /* struct _objc_object *self; */
5089 decls
= add_field_decl (objc_object_type
, "self", &chain
);
5091 /* struct _objc_class *super_class; */
5092 add_field_decl (build_pointer_type (objc_class_template
),
5093 "super_class", &chain
);
5095 objc_finish_struct (objc_super_template
, decls
);
5098 /* struct _objc_ivar {
5105 build_ivar_template (void)
5107 tree objc_ivar_id
, objc_ivar_record
;
5108 tree decls
, *chain
= NULL
;
5110 objc_ivar_id
= get_identifier (UTAG_IVAR
);
5111 objc_ivar_record
= objc_start_struct (objc_ivar_id
);
5113 /* char *ivar_name; */
5114 decls
= add_field_decl (string_type_node
, "ivar_name", &chain
);
5116 /* char *ivar_type; */
5117 add_field_decl (string_type_node
, "ivar_type", &chain
);
5119 /* int ivar_offset; */
5120 add_field_decl (integer_type_node
, "ivar_offset", &chain
);
5122 objc_finish_struct (objc_ivar_record
, decls
);
5124 return objc_ivar_record
;
5129 struct objc_ivar ivar_list[ivar_count];
5133 build_ivar_list_template (tree list_type
, int size
)
5135 tree objc_ivar_list_record
;
5136 tree array_type
, decls
, *chain
= NULL
;
5138 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
5140 /* int ivar_count; */
5141 decls
= add_field_decl (integer_type_node
, "ivar_count", &chain
);
5143 /* struct objc_ivar ivar_list[]; */
5144 array_type
= build_sized_array_type (list_type
, size
);
5145 add_field_decl (array_type
, "ivar_list", &chain
);
5147 objc_finish_struct (objc_ivar_list_record
, decls
);
5149 return objc_ivar_list_record
;
5153 struct _objc__method_prototype_list *method_next;
5155 struct objc_method method_list[method_count];
5159 build_method_list_template (tree list_type
, int size
)
5161 tree objc_ivar_list_record
;
5162 tree array_type
, decls
, *chain
= NULL
;
5164 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
5166 /* struct _objc__method_prototype_list *method_next; */
5167 decls
= add_field_decl (objc_method_proto_list_ptr
, "method_next", &chain
);
5169 /* int method_count; */
5170 add_field_decl (integer_type_node
, "method_count", &chain
);
5172 /* struct objc_method method_list[]; */
5173 array_type
= build_sized_array_type (list_type
, size
);
5174 add_field_decl (array_type
, "method_list", &chain
);
5176 objc_finish_struct (objc_ivar_list_record
, decls
);
5178 return objc_ivar_list_record
;
5182 build_ivar_list_initializer (tree type
, tree field_decl
)
5184 VEC(constructor_elt
,gc
) *inits
= NULL
;
5188 VEC(constructor_elt
,gc
) *ivar
= NULL
;
5192 if (DECL_NAME (field_decl
))
5193 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
,
5194 add_objc_string (DECL_NAME (field_decl
),
5197 /* Unnamed bit-field ivar (yuck). */
5198 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5201 encode_field_decl (field_decl
,
5202 obstack_object_size (&util_obstack
),
5203 OBJC_ENCODE_DONT_INLINE_DEFS
);
5205 /* Null terminate string. */
5206 obstack_1grow (&util_obstack
, 0);
5207 id
= add_objc_string (get_identifier (XOBFINISH (&util_obstack
, char *)),
5209 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, id
);
5210 obstack_free (&util_obstack
, util_firstobj
);
5213 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, byte_position (field_decl
));
5214 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
5215 objc_build_constructor (type
, ivar
));
5217 field_decl
= DECL_CHAIN (field_decl
);
5218 while (field_decl
&& TREE_CODE (field_decl
) != FIELD_DECL
);
5222 return objc_build_constructor (build_array_type (type
, 0), inits
);
5226 generate_ivars_list (tree type
, const char *name
, int size
, tree list
)
5229 VEC(constructor_elt
,gc
) *inits
= NULL
;
5231 decl
= start_var_decl (type
, synth_id_with_class_suffix
5232 (name
, objc_implementation_context
));
5234 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
5235 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, list
);
5237 finish_var_decl (decl
,
5238 objc_build_constructor (TREE_TYPE (decl
), inits
));
5243 /* Count only the fields occurring in T. */
5246 ivar_list_length (tree t
)
5250 for (; t
; t
= DECL_CHAIN (t
))
5251 if (TREE_CODE (t
) == FIELD_DECL
)
5258 generate_ivar_lists (void)
5260 tree initlist
, ivar_list_template
, chain
;
5263 generating_instance_variables
= 1;
5265 if (!objc_ivar_template
)
5266 objc_ivar_template
= build_ivar_template ();
5268 /* Only generate class variables for the root of the inheritance
5269 hierarchy since these will be the same for every class. */
5271 if (CLASS_SUPER_NAME (implementation_template
) == NULL_TREE
5272 && (chain
= TYPE_FIELDS (objc_class_template
)))
5274 size
= ivar_list_length (chain
);
5276 ivar_list_template
= build_ivar_list_template (objc_ivar_template
, size
);
5277 initlist
= build_ivar_list_initializer (objc_ivar_template
, chain
);
5279 UOBJC_CLASS_VARIABLES_decl
5280 = generate_ivars_list (ivar_list_template
, "_OBJC_CLASS_VARIABLES",
5284 UOBJC_CLASS_VARIABLES_decl
= 0;
5286 chain
= CLASS_IVARS (implementation_template
);
5289 size
= ivar_list_length (chain
);
5290 ivar_list_template
= build_ivar_list_template (objc_ivar_template
, size
);
5291 initlist
= build_ivar_list_initializer (objc_ivar_template
, chain
);
5293 UOBJC_INSTANCE_VARIABLES_decl
5294 = generate_ivars_list (ivar_list_template
, "_OBJC_INSTANCE_VARIABLES",
5298 UOBJC_INSTANCE_VARIABLES_decl
= 0;
5300 generating_instance_variables
= 0;
5304 build_dispatch_table_initializer (tree type
, tree entries
)
5306 VEC(constructor_elt
,gc
) *inits
= NULL
;
5310 VEC(constructor_elt
,gc
) *elems
= NULL
;
5313 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
5314 build_selector (METHOD_SEL_NAME (entries
)));
5316 /* Generate the method encoding if we don't have one already. */
5317 if (! METHOD_ENCODING (entries
))
5318 METHOD_ENCODING (entries
) =
5319 encode_method_prototype (entries
);
5321 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
5322 add_objc_string (METHOD_ENCODING (entries
),
5325 expr
= convert (ptr_type_node
,
5326 build_unary_op (input_location
, ADDR_EXPR
,
5327 METHOD_DEFINITION (entries
), 1));
5328 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
, expr
);
5330 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
5331 objc_build_constructor (type
, elems
));
5333 entries
= DECL_CHAIN (entries
);
5337 return objc_build_constructor (build_array_type (type
, 0), inits
);
5340 /* To accomplish method prototyping without generating all kinds of
5341 inane warnings, the definition of the dispatch table entries were
5344 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
5346 struct objc_method { SEL _cmd; ...; void *_imp; }; */
5349 build_method_template (void)
5352 tree decls
, *chain
= NULL
;
5354 _SLT_record
= objc_start_struct (get_identifier (UTAG_METHOD
));
5357 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
5359 /* char *method_types; */
5360 add_field_decl (string_type_node
, "method_types", &chain
);
5363 add_field_decl (build_pointer_type (void_type_node
), "_imp", &chain
);
5365 objc_finish_struct (_SLT_record
, decls
);
5372 generate_dispatch_table (tree type
, const char *name
, int size
, tree list
)
5375 VEC(constructor_elt
,gc
) *v
= NULL
;
5377 decl
= start_var_decl (type
, synth_id_with_class_suffix
5378 (name
, objc_implementation_context
));
5380 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
5381 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (integer_type_node
, size
));
5382 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, list
);
5384 finish_var_decl (decl
,
5385 objc_build_constructor (TREE_TYPE (decl
), v
));
5391 mark_referenced_methods (void)
5393 struct imp_entry
*impent
;
5396 for (impent
= imp_list
; impent
; impent
= impent
->next
)
5398 chain
= CLASS_CLS_METHODS (impent
->imp_context
);
5401 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain
)));
5402 chain
= DECL_CHAIN (chain
);
5405 chain
= CLASS_NST_METHODS (impent
->imp_context
);
5408 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain
)));
5409 chain
= DECL_CHAIN (chain
);
5415 generate_dispatch_tables (void)
5417 tree initlist
, chain
, method_list_template
;
5420 if (!objc_method_template
)
5421 objc_method_template
= build_method_template ();
5423 chain
= CLASS_CLS_METHODS (objc_implementation_context
);
5426 size
= list_length (chain
);
5428 method_list_template
5429 = build_method_list_template (objc_method_template
, size
);
5431 = build_dispatch_table_initializer (objc_method_template
, chain
);
5433 UOBJC_CLASS_METHODS_decl
5434 = generate_dispatch_table (method_list_template
,
5435 ((TREE_CODE (objc_implementation_context
)
5436 == CLASS_IMPLEMENTATION_TYPE
)
5437 ? "_OBJC_CLASS_METHODS"
5438 : "_OBJC_CATEGORY_CLASS_METHODS"),
5442 UOBJC_CLASS_METHODS_decl
= 0;
5444 chain
= CLASS_NST_METHODS (objc_implementation_context
);
5447 size
= list_length (chain
);
5449 method_list_template
5450 = build_method_list_template (objc_method_template
, size
);
5452 = build_dispatch_table_initializer (objc_method_template
, chain
);
5454 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
5455 UOBJC_INSTANCE_METHODS_decl
5456 = generate_dispatch_table (method_list_template
,
5457 "_OBJC_INSTANCE_METHODS",
5460 /* We have a category. */
5461 UOBJC_INSTANCE_METHODS_decl
5462 = generate_dispatch_table (method_list_template
,
5463 "_OBJC_CATEGORY_INSTANCE_METHODS",
5467 UOBJC_INSTANCE_METHODS_decl
= 0;
5471 generate_protocol_list (tree i_or_p
)
5473 tree array_type
, ptype
, refs_decl
, lproto
, e
, plist
;
5475 const char *ref_name
;
5476 VEC(constructor_elt
,gc
) *v
= NULL
;
5478 if (TREE_CODE (i_or_p
) == CLASS_INTERFACE_TYPE
5479 || TREE_CODE (i_or_p
) == CATEGORY_INTERFACE_TYPE
)
5480 plist
= CLASS_PROTOCOL_LIST (i_or_p
);
5481 else if (TREE_CODE (i_or_p
) == PROTOCOL_INTERFACE_TYPE
)
5482 plist
= PROTOCOL_LIST (i_or_p
);
5487 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
5488 if (TREE_CODE (TREE_VALUE (lproto
)) == PROTOCOL_INTERFACE_TYPE
5489 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto
)))
5492 /* Build initializer. */
5493 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5494 e
= build_int_cst (build_pointer_type (objc_protocol_template
), size
);
5495 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
5497 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
5499 tree pval
= TREE_VALUE (lproto
);
5501 if (TREE_CODE (pval
) == PROTOCOL_INTERFACE_TYPE
5502 && PROTOCOL_FORWARD_DECL (pval
))
5504 e
= build_unary_op (input_location
, ADDR_EXPR
,
5505 PROTOCOL_FORWARD_DECL (pval
), 0);
5506 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
5510 /* static struct objc_protocol *refs[n]; */
5512 if (TREE_CODE (i_or_p
) == PROTOCOL_INTERFACE_TYPE
)
5513 ref_name
= synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p
);
5514 else if (TREE_CODE (i_or_p
) == CLASS_INTERFACE_TYPE
)
5515 ref_name
= synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p
);
5516 else if (TREE_CODE (i_or_p
) == CATEGORY_INTERFACE_TYPE
)
5517 ref_name
= synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p
);
5521 ptype
= build_pointer_type (objc_protocol_template
);
5522 array_type
= build_sized_array_type (ptype
, size
+ 3);
5523 refs_decl
= start_var_decl (array_type
, ref_name
);
5525 finish_var_decl (refs_decl
,
5526 objc_build_constructor (TREE_TYPE (refs_decl
), v
));
5532 build_category_initializer (tree type
, tree cat_name
, tree class_name
,
5533 tree instance_methods
, tree class_methods
,
5537 VEC(constructor_elt
,gc
) *v
= NULL
;
5539 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, cat_name
);
5540 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, class_name
);
5542 if (!instance_methods
)
5543 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5546 expr
= convert (objc_method_list_ptr
,
5547 build_unary_op (input_location
, ADDR_EXPR
,
5548 instance_methods
, 0));
5549 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5552 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5555 expr
= convert (objc_method_list_ptr
,
5556 build_unary_op (input_location
, ADDR_EXPR
,
5558 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5561 /* protocol_list = */
5563 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5566 expr
= convert (build_pointer_type
5568 (objc_protocol_template
)),
5569 build_unary_op (input_location
, ADDR_EXPR
,
5571 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5574 return objc_build_constructor (type
, v
);
5577 /* struct _objc_class {
5578 struct objc_class *isa;
5579 struct objc_class *super_class;
5584 struct objc_ivar_list *ivars;
5585 struct objc_method_list *methods;
5586 if (flag_next_runtime)
5587 struct objc_cache *cache;
5589 struct sarray *dtable;
5590 struct objc_class *subclass_list;
5591 struct objc_class *sibling_class;
5593 struct objc_protocol_list *protocols;
5594 if (flag_next_runtime)
5596 void *gc_object_type;
5600 build_shared_structure_initializer (tree type
, tree isa
, tree super
,
5601 tree name
, tree size
, int status
,
5602 tree dispatch_table
, tree ivar_list
,
5606 VEC(constructor_elt
,gc
) *v
= NULL
;
5609 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, isa
);
5612 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, super
);
5615 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, default_conversion (name
));
5618 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5619 build_int_cst (long_integer_type_node
, 0));
5622 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5623 build_int_cst (long_integer_type_node
, status
));
5625 /* instance_size = */
5626 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5627 convert (long_integer_type_node
, size
));
5629 /* objc_ivar_list = */
5631 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5634 expr
= convert (objc_ivar_list_ptr
,
5635 build_unary_op (input_location
, ADDR_EXPR
,
5637 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5640 /* objc_method_list = */
5641 if (!dispatch_table
)
5642 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5645 expr
= convert (objc_method_list_ptr
,
5646 build_unary_op (input_location
, ADDR_EXPR
,
5647 dispatch_table
, 0));
5648 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5651 if (flag_next_runtime
)
5652 /* method_cache = */
5653 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5657 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5659 /* subclass_list = */
5660 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5662 /* sibling_class = */
5663 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5666 /* protocol_list = */
5667 if (! protocol_list
)
5668 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5671 expr
= convert (build_pointer_type
5673 (objc_protocol_template
)),
5674 build_unary_op (input_location
, ADDR_EXPR
,
5676 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5679 if (flag_next_runtime
)
5681 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5683 /* gc_object_type = NULL */
5684 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5686 return objc_build_constructor (type
, v
);
5689 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
5692 lookup_category (tree klass
, tree cat_name
)
5694 tree category
= CLASS_CATEGORY_LIST (klass
);
5696 while (category
&& CLASS_SUPER_NAME (category
) != cat_name
)
5697 category
= CLASS_CATEGORY_LIST (category
);
5701 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
5704 generate_category (struct imp_entry
*impent
)
5706 tree initlist
, cat_name_expr
, class_name_expr
;
5707 tree protocol_decl
, category
;
5708 tree cat
= impent
->imp_context
;
5710 implementation_template
= impent
->imp_template
;
5711 UOBJC_CLASS_decl
= impent
->class_decl
;
5712 UOBJC_METACLASS_decl
= impent
->meta_decl
;
5714 add_class_reference (CLASS_NAME (cat
));
5715 cat_name_expr
= add_objc_string (CLASS_SUPER_NAME (cat
), class_names
);
5717 class_name_expr
= add_objc_string (CLASS_NAME (cat
), class_names
);
5719 category
= lookup_category (implementation_template
,
5720 CLASS_SUPER_NAME (cat
));
5722 if (category
&& CLASS_PROTOCOL_LIST (category
))
5724 generate_protocol_references (CLASS_PROTOCOL_LIST (category
));
5725 protocol_decl
= generate_protocol_list (category
);
5730 initlist
= build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl
),
5731 cat_name_expr
, class_name_expr
,
5732 UOBJC_INSTANCE_METHODS_decl
,
5733 UOBJC_CLASS_METHODS_decl
,
5735 /* Finish and initialize the forward decl. */
5736 finish_var_decl (UOBJC_CLASS_decl
, initlist
);
5739 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
5740 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5743 generate_shared_structures (struct imp_entry
*impent
)
5745 tree name_expr
, super_expr
, root_expr
;
5746 tree my_root_id
, my_super_id
;
5747 tree cast_type
, initlist
, protocol_decl
;
5750 objc_implementation_context
= impent
->imp_context
;
5751 implementation_template
= impent
->imp_template
;
5752 UOBJC_CLASS_decl
= impent
->class_decl
;
5753 UOBJC_METACLASS_decl
= impent
->meta_decl
;
5754 cls_flags
= impent
->has_cxx_cdtors
? CLS_HAS_CXX_STRUCTORS
: 0 ;
5756 my_super_id
= CLASS_SUPER_NAME (implementation_template
);
5759 add_class_reference (my_super_id
);
5761 /* Compute "my_root_id" - this is required for code generation.
5762 the "isa" for all meta class structures points to the root of
5763 the inheritance hierarchy (e.g. "__Object")... */
5764 my_root_id
= my_super_id
;
5767 tree my_root_int
= lookup_interface (my_root_id
);
5769 if (my_root_int
&& CLASS_SUPER_NAME (my_root_int
))
5770 my_root_id
= CLASS_SUPER_NAME (my_root_int
);
5777 /* No super class. */
5778 my_root_id
= CLASS_NAME (implementation_template
);
5780 cast_type
= build_pointer_type (objc_class_template
);
5781 name_expr
= add_objc_string (CLASS_NAME (implementation_template
),
5784 /* Install class `isa' and `super' pointers at runtime. */
5786 super_expr
= add_objc_string (my_super_id
, class_names
);
5788 super_expr
= integer_zero_node
;
5790 super_expr
= build_c_cast (input_location
,
5791 cast_type
, super_expr
); /* cast! */
5793 root_expr
= add_objc_string (my_root_id
, class_names
);
5794 root_expr
= build_c_cast (input_location
, cast_type
, root_expr
); /* cast! */
5796 if (CLASS_PROTOCOL_LIST (implementation_template
))
5798 generate_protocol_references
5799 (CLASS_PROTOCOL_LIST (implementation_template
));
5800 protocol_decl
= generate_protocol_list (implementation_template
);
5805 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
5808 = build_shared_structure_initializer
5809 (TREE_TYPE (UOBJC_METACLASS_decl
),
5810 root_expr
, super_expr
, name_expr
,
5811 convert (integer_type_node
, TYPE_SIZE_UNIT (objc_class_template
)),
5813 UOBJC_CLASS_METHODS_decl
,
5814 UOBJC_CLASS_VARIABLES_decl
,
5817 finish_var_decl (UOBJC_METACLASS_decl
, initlist
);
5819 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5822 = build_shared_structure_initializer
5823 (TREE_TYPE (UOBJC_CLASS_decl
),
5824 build_unary_op (input_location
, ADDR_EXPR
, UOBJC_METACLASS_decl
, 0),
5825 super_expr
, name_expr
,
5826 convert (integer_type_node
,
5827 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
5828 (implementation_template
))),
5829 1 /*CLS_FACTORY*/ | cls_flags
,
5830 UOBJC_INSTANCE_METHODS_decl
,
5831 UOBJC_INSTANCE_VARIABLES_decl
,
5834 finish_var_decl (UOBJC_CLASS_decl
, initlist
);
5839 synth_id_with_class_suffix (const char *preamble
, tree ctxt
)
5841 static char string
[BUFSIZE
];
5843 if (TREE_CODE (ctxt
) == CLASS_IMPLEMENTATION_TYPE
5844 || TREE_CODE (ctxt
) == CLASS_INTERFACE_TYPE
)
5846 sprintf (string
, "%s_%s", preamble
,
5847 IDENTIFIER_POINTER (CLASS_NAME (ctxt
)));
5849 else if (TREE_CODE (ctxt
) == CATEGORY_IMPLEMENTATION_TYPE
5850 || TREE_CODE (ctxt
) == CATEGORY_INTERFACE_TYPE
)
5852 /* We have a category. */
5853 const char *const class_name
5854 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context
));
5855 const char *const class_super_name
5856 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context
));
5857 sprintf (string
, "%s_%s_%s", preamble
, class_name
, class_super_name
);
5859 else if (TREE_CODE (ctxt
) == PROTOCOL_INTERFACE_TYPE
)
5861 const char *protocol_name
= IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt
));
5862 sprintf (string
, "%s_%s", preamble
, protocol_name
);
5870 /* If type is empty or only type qualifiers are present, add default
5871 type of id (otherwise grokdeclarator will default to int). */
5874 adjust_type_for_id_default (tree type
)
5877 type
= make_node (TREE_LIST
);
5879 if (!TREE_VALUE (type
))
5880 TREE_VALUE (type
) = objc_object_type
;
5881 else if (TREE_CODE (TREE_VALUE (type
)) == RECORD_TYPE
5882 && TYPED_OBJECT (TREE_VALUE (type
)))
5883 error ("can not use an object as parameter to a method");
5890 selector ':' '(' typename ')' identifier
5893 Transform an Objective-C keyword argument into
5894 the C equivalent parameter declarator.
5896 In: key_name, an "identifier_node" (optional).
5897 arg_type, a "tree_list" (optional).
5898 arg_name, an "identifier_node".
5900 Note: It would be really nice to strongly type the preceding
5901 arguments in the function prototype; however, then I
5902 could not use the "accessor" macros defined in "tree.h".
5904 Out: an instance of "keyword_decl". */
5907 objc_build_keyword_decl (tree key_name
, tree arg_type
, tree arg_name
)
5911 /* If no type is specified, default to "id". */
5912 arg_type
= adjust_type_for_id_default (arg_type
);
5914 keyword_decl
= make_node (KEYWORD_DECL
);
5916 TREE_TYPE (keyword_decl
) = arg_type
;
5917 KEYWORD_ARG_NAME (keyword_decl
) = arg_name
;
5918 KEYWORD_KEY_NAME (keyword_decl
) = key_name
;
5920 return keyword_decl
;
5923 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
5926 build_keyword_selector (tree selector
)
5929 tree key_chain
, key_name
;
5932 /* Scan the selector to see how much space we'll need. */
5933 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
5935 if (TREE_CODE (selector
) == KEYWORD_DECL
)
5936 key_name
= KEYWORD_KEY_NAME (key_chain
);
5937 else if (TREE_CODE (selector
) == TREE_LIST
)
5938 key_name
= TREE_PURPOSE (key_chain
);
5943 len
+= IDENTIFIER_LENGTH (key_name
) + 1;
5945 /* Just a ':' arg. */
5949 buf
= (char *) alloca (len
+ 1);
5950 /* Start the buffer out as an empty string. */
5953 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
5955 if (TREE_CODE (selector
) == KEYWORD_DECL
)
5956 key_name
= KEYWORD_KEY_NAME (key_chain
);
5957 else if (TREE_CODE (selector
) == TREE_LIST
)
5959 key_name
= TREE_PURPOSE (key_chain
);
5960 /* The keyword decl chain will later be used as a function argument
5961 chain. Unhook the selector itself so as to not confuse other
5962 parts of the compiler. */
5963 TREE_PURPOSE (key_chain
) = NULL_TREE
;
5969 strcat (buf
, IDENTIFIER_POINTER (key_name
));
5973 return get_identifier (buf
);
5976 /* Used for declarations and definitions. */
5979 build_method_decl (enum tree_code code
, tree ret_type
, tree selector
,
5980 tree add_args
, bool ellipsis
)
5984 /* If no type is specified, default to "id". */
5985 ret_type
= adjust_type_for_id_default (ret_type
);
5987 method_decl
= make_node (code
);
5988 TREE_TYPE (method_decl
) = ret_type
;
5990 /* If we have a keyword selector, create an identifier_node that
5991 represents the full selector name (`:' included)... */
5992 if (TREE_CODE (selector
) == KEYWORD_DECL
)
5994 METHOD_SEL_NAME (method_decl
) = build_keyword_selector (selector
);
5995 METHOD_SEL_ARGS (method_decl
) = selector
;
5996 METHOD_ADD_ARGS (method_decl
) = add_args
;
5997 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl
) = ellipsis
;
6001 METHOD_SEL_NAME (method_decl
) = selector
;
6002 METHOD_SEL_ARGS (method_decl
) = NULL_TREE
;
6003 METHOD_ADD_ARGS (method_decl
) = NULL_TREE
;
6009 #define METHOD_DEF 0
6010 #define METHOD_REF 1
6012 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
6013 an argument list for method METH. CONTEXT is either METHOD_DEF or
6014 METHOD_REF, saying whether we are trying to define a method or call
6015 one. SUPERFLAG says this is for a send to super; this makes a
6016 difference for the NeXT calling sequence in which the lookup and
6017 the method call are done together. If METH is null, user-defined
6018 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
6021 get_arg_type_list (tree meth
, int context
, int superflag
)
6025 /* Receiver type. */
6026 if (flag_next_runtime
&& superflag
)
6027 arglist
= build_tree_list (NULL_TREE
, objc_super_type
);
6028 else if (context
== METHOD_DEF
&& TREE_CODE (meth
) == INSTANCE_METHOD_DECL
)
6029 arglist
= build_tree_list (NULL_TREE
, objc_instance_type
);
6031 arglist
= build_tree_list (NULL_TREE
, objc_object_type
);
6033 /* Selector type - will eventually change to `int'. */
6034 chainon (arglist
, build_tree_list (NULL_TREE
, objc_selector_type
));
6036 /* No actual method prototype given -- assume that remaining arguments
6041 /* Build a list of argument types. */
6042 for (akey
= METHOD_SEL_ARGS (meth
); akey
; akey
= DECL_CHAIN (akey
))
6044 tree arg_type
= TREE_VALUE (TREE_TYPE (akey
));
6046 /* Decay arrays and functions into pointers. */
6047 if (TREE_CODE (arg_type
) == ARRAY_TYPE
)
6048 arg_type
= build_pointer_type (TREE_TYPE (arg_type
));
6049 else if (TREE_CODE (arg_type
) == FUNCTION_TYPE
)
6050 arg_type
= build_pointer_type (arg_type
);
6052 chainon (arglist
, build_tree_list (NULL_TREE
, arg_type
));
6055 if (METHOD_ADD_ARGS (meth
))
6057 for (akey
= TREE_CHAIN (METHOD_ADD_ARGS (meth
));
6058 akey
; akey
= TREE_CHAIN (akey
))
6060 tree arg_type
= TREE_TYPE (TREE_VALUE (akey
));
6062 chainon (arglist
, build_tree_list (NULL_TREE
, arg_type
));
6065 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth
))
6066 goto lack_of_ellipsis
;
6071 chainon (arglist
, OBJC_VOID_AT_END
);
6078 check_duplicates (hash hsh
, int methods
, int is_class
)
6080 tree meth
= NULL_TREE
;
6088 /* We have two or more methods with the same name but
6092 /* But just how different are those types? If
6093 -Wno-strict-selector-match is specified, we shall not
6094 complain if the differences are solely among types with
6095 identical size and alignment. */
6096 if (!warn_strict_selector_match
)
6098 for (loop
= hsh
->list
; loop
; loop
= loop
->next
)
6099 if (!comp_proto_with_proto (meth
, loop
->value
, 0))
6108 bool type
= TREE_CODE (meth
) == INSTANCE_METHOD_DECL
;
6110 warning_at (input_location
, 0,
6111 "multiple methods named %<%c%E%> found",
6112 (is_class
? '+' : '-'),
6113 METHOD_SEL_NAME (meth
));
6114 inform (DECL_SOURCE_LOCATION (meth
), "using %<%c%s%>",
6116 identifier_to_locale (gen_method_decl (meth
)));
6120 bool type
= TREE_CODE (meth
) == INSTANCE_METHOD_DECL
;
6122 warning_at (input_location
, 0,
6123 "multiple selectors named %<%c%E%> found",
6124 (is_class
? '+' : '-'),
6125 METHOD_SEL_NAME (meth
));
6126 inform (DECL_SOURCE_LOCATION (meth
), "found %<%c%s%>",
6128 identifier_to_locale (gen_method_decl (meth
)));
6131 for (loop
= hsh
->list
; loop
; loop
= loop
->next
)
6133 bool type
= TREE_CODE (loop
->value
) == INSTANCE_METHOD_DECL
;
6135 inform (DECL_SOURCE_LOCATION (loop
->value
), "also found %<%c%s%>",
6137 identifier_to_locale (gen_method_decl (loop
->value
)));
6144 /* If RECEIVER is a class reference, return the identifier node for
6145 the referenced class. RECEIVER is created by objc_get_class_reference,
6146 so we check the exact form created depending on which runtimes are
6150 receiver_is_class_object (tree receiver
, int self
, int super
)
6152 tree chain
, exp
, arg
;
6154 /* The receiver is 'self' or 'super' in the context of a class method. */
6155 if (objc_method_context
6156 && TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
6159 ? CLASS_SUPER_NAME (implementation_template
)
6160 : CLASS_NAME (implementation_template
));
6162 if (flag_next_runtime
)
6164 /* The receiver is a variable created by
6165 build_class_reference_decl. */
6166 if (TREE_CODE (receiver
) == VAR_DECL
&& IS_CLASS (TREE_TYPE (receiver
)))
6167 /* Look up the identifier. */
6168 for (chain
= cls_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
6169 if (TREE_PURPOSE (chain
) == receiver
)
6170 return TREE_VALUE (chain
);
6173 /* The receiver is a function call that returns an id. Check if
6174 it is a call to objc_getClass, if so, pick up the class name. */
6175 if (TREE_CODE (receiver
) == CALL_EXPR
6176 && (exp
= CALL_EXPR_FN (receiver
))
6177 && TREE_CODE (exp
) == ADDR_EXPR
6178 && (exp
= TREE_OPERAND (exp
, 0))
6179 && TREE_CODE (exp
) == FUNCTION_DECL
6180 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
6181 prototypes for objc_get_class(). Thankfully, they seem to share the
6182 same function type. */
6183 && TREE_TYPE (exp
) == TREE_TYPE (objc_get_class_decl
)
6184 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp
)), TAG_GETCLASS
)
6185 /* We have a call to objc_get_class/objc_getClass! */
6186 && (arg
= CALL_EXPR_ARG (receiver
, 0)))
6189 if (TREE_CODE (arg
) == ADDR_EXPR
6190 && (arg
= TREE_OPERAND (arg
, 0))
6191 && TREE_CODE (arg
) == STRING_CST
)
6192 /* Finally, we have the class name. */
6193 return get_identifier (TREE_STRING_POINTER (arg
));
6198 /* If we are currently building a message expr, this holds
6199 the identifier of the selector of the message. This is
6200 used when printing warnings about argument mismatches. */
6202 static tree current_objc_message_selector
= 0;
6205 objc_message_selector (void)
6207 return current_objc_message_selector
;
6210 /* Construct an expression for sending a message.
6211 MESS has the object to send to in TREE_PURPOSE
6212 and the argument list (including selector) in TREE_VALUE.
6214 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
6215 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
6218 objc_build_message_expr (tree mess
)
6220 tree receiver
= TREE_PURPOSE (mess
);
6223 tree args
= TREE_PURPOSE (TREE_VALUE (mess
));
6225 tree args
= TREE_VALUE (mess
);
6227 tree method_params
= NULL_TREE
;
6229 if (TREE_CODE (receiver
) == ERROR_MARK
|| TREE_CODE (args
) == ERROR_MARK
)
6230 return error_mark_node
;
6232 /* Obtain the full selector name. */
6233 if (TREE_CODE (args
) == IDENTIFIER_NODE
)
6234 /* A unary selector. */
6236 else if (TREE_CODE (args
) == TREE_LIST
)
6237 sel_name
= build_keyword_selector (args
);
6241 /* Build the parameter list to give to the method. */
6242 if (TREE_CODE (args
) == TREE_LIST
)
6244 method_params
= chainon (args
, TREE_VALUE (TREE_VALUE (mess
)));
6247 tree chain
= args
, prev
= NULL_TREE
;
6249 /* We have a keyword selector--check for comma expressions. */
6252 tree element
= TREE_VALUE (chain
);
6254 /* We have a comma expression, must collapse... */
6255 if (TREE_CODE (element
) == TREE_LIST
)
6258 TREE_CHAIN (prev
) = element
;
6263 chain
= TREE_CHAIN (chain
);
6265 method_params
= args
;
6270 if (processing_template_decl
)
6271 /* Must wait until template instantiation time. */
6272 return build_min_nt (MESSAGE_SEND_EXPR
, receiver
, sel_name
,
6276 return objc_finish_message_expr (receiver
, sel_name
, method_params
);
6279 /* Look up method SEL_NAME that would be suitable for receiver
6280 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
6281 nonzero), and report on any duplicates. */
6284 lookup_method_in_hash_lists (tree sel_name
, int is_class
)
6286 hash method_prototype
= NULL
;
6289 method_prototype
= hash_lookup (nst_method_hash_list
,
6292 if (!method_prototype
)
6294 method_prototype
= hash_lookup (cls_method_hash_list
,
6299 return check_duplicates (method_prototype
, 1, is_class
);
6302 /* The 'objc_finish_message_expr' routine is called from within
6303 'objc_build_message_expr' for non-template functions. In the case of
6304 C++ template functions, it is called from 'build_expr_from_tree'
6305 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
6308 objc_finish_message_expr (tree receiver
, tree sel_name
, tree method_params
)
6310 tree method_prototype
= NULL_TREE
, rprotos
= NULL_TREE
, rtype
;
6311 tree selector
, retval
, class_tree
;
6312 int self
, super
, have_cast
;
6314 /* Extract the receiver of the message, as well as its type
6315 (where the latter may take the form of a cast or be inferred
6316 from the implementation context). */
6318 while (TREE_CODE (rtype
) == COMPOUND_EXPR
6319 || TREE_CODE (rtype
) == MODIFY_EXPR
6320 || CONVERT_EXPR_P (rtype
)
6321 || TREE_CODE (rtype
) == COMPONENT_REF
)
6322 rtype
= TREE_OPERAND (rtype
, 0);
6323 self
= (rtype
== self_decl
);
6324 super
= (rtype
== UOBJC_SUPER_decl
);
6325 rtype
= TREE_TYPE (receiver
);
6326 have_cast
= (TREE_CODE (receiver
) == NOP_EXPR
6327 || (TREE_CODE (receiver
) == COMPOUND_EXPR
6328 && !IS_SUPER (rtype
)));
6330 /* If we are calling [super dealloc], reset our warning flag. */
6331 if (super
&& !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name
)))
6332 should_call_super_dealloc
= 0;
6334 /* If the receiver is a class object, retrieve the corresponding
6335 @interface, if one exists. */
6336 class_tree
= receiver_is_class_object (receiver
, self
, super
);
6338 /* Now determine the receiver type (if an explicit cast has not been
6343 rtype
= lookup_interface (class_tree
);
6344 /* Handle `self' and `super'. */
6347 if (!CLASS_SUPER_NAME (implementation_template
))
6349 error ("no super class declared in @interface for %qE",
6350 CLASS_NAME (implementation_template
));
6351 return error_mark_node
;
6353 rtype
= lookup_interface (CLASS_SUPER_NAME (implementation_template
));
6356 rtype
= lookup_interface (CLASS_NAME (implementation_template
));
6359 /* If receiver is of type `id' or `Class' (or if the @interface for a
6360 class is not visible), we shall be satisfied with the existence of
6361 any instance or class method. */
6362 if (objc_is_id (rtype
))
6364 class_tree
= (IS_CLASS (rtype
) ? objc_class_name
: NULL_TREE
);
6365 rprotos
= (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype
))
6366 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype
))
6372 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
6373 in protocols themselves for the method prototype. */
6375 = lookup_method_in_protocol_list (rprotos
, sel_name
,
6376 class_tree
!= NULL_TREE
);
6378 /* If messaging 'Class <Proto>' but did not find a class method
6379 prototype, search for an instance method instead, and warn
6380 about having done so. */
6381 if (!method_prototype
&& !rtype
&& class_tree
!= NULL_TREE
)
6384 = lookup_method_in_protocol_list (rprotos
, sel_name
, 0);
6386 if (method_prototype
)
6387 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
6388 sel_name
, sel_name
);
6394 tree orig_rtype
= rtype
;
6396 if (TREE_CODE (rtype
) == POINTER_TYPE
)
6397 rtype
= TREE_TYPE (rtype
);
6398 /* Traverse typedef aliases */
6399 while (TREE_CODE (rtype
) == RECORD_TYPE
&& OBJC_TYPE_NAME (rtype
)
6400 && TREE_CODE (OBJC_TYPE_NAME (rtype
)) == TYPE_DECL
6401 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype
)))
6402 rtype
= DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype
));
6403 if (TYPED_OBJECT (rtype
))
6405 rprotos
= TYPE_OBJC_PROTOCOL_LIST (rtype
);
6406 rtype
= TYPE_OBJC_INTERFACE (rtype
);
6408 /* If we could not find an @interface declaration, we must have
6409 only seen a @class declaration; so, we cannot say anything
6410 more intelligent about which methods the receiver will
6412 if (!rtype
|| TREE_CODE (rtype
) == IDENTIFIER_NODE
)
6415 /* We could not find an @interface declaration, yet Message maybe in a
6416 @class's protocol. */
6417 if (!method_prototype
&& rprotos
)
6419 = lookup_method_in_protocol_list (rprotos
, sel_name
, 0);
6421 else if (TREE_CODE (rtype
) == CLASS_INTERFACE_TYPE
6422 || TREE_CODE (rtype
) == CLASS_IMPLEMENTATION_TYPE
)
6424 /* We have a valid ObjC class name. Look up the method name
6425 in the published @interface for the class (and its
6428 = lookup_method_static (rtype
, sel_name
, class_tree
!= NULL_TREE
);
6430 /* If the method was not found in the @interface, it may still
6431 exist locally as part of the @implementation. */
6432 if (!method_prototype
&& objc_implementation_context
6433 && CLASS_NAME (objc_implementation_context
)
6434 == OBJC_TYPE_NAME (rtype
))
6438 ? CLASS_CLS_METHODS (objc_implementation_context
)
6439 : CLASS_NST_METHODS (objc_implementation_context
)),
6442 /* If we haven't found a candidate method by now, try looking for
6443 it in the protocol list. */
6444 if (!method_prototype
&& rprotos
)
6446 = lookup_method_in_protocol_list (rprotos
, sel_name
,
6447 class_tree
!= NULL_TREE
);
6451 warning (0, "invalid receiver type %qs",
6452 identifier_to_locale (gen_type_name (orig_rtype
)));
6453 /* After issuing the "invalid receiver" warning, perform method
6454 lookup as if we were messaging 'id'. */
6455 rtype
= rprotos
= NULL_TREE
;
6460 /* For 'id' or 'Class' receivers, search in the global hash table
6461 as a last resort. For all receivers, warn if protocol searches
6463 if (!method_prototype
)
6466 warning (0, "%<%c%E%> not found in protocol(s)",
6467 (class_tree
? '+' : '-'),
6472 = lookup_method_in_hash_lists (sel_name
, class_tree
!= NULL_TREE
);
6475 if (!method_prototype
)
6477 static bool warn_missing_methods
= false;
6480 warning (0, "%qE may not respond to %<%c%E%>",
6481 OBJC_TYPE_NAME (rtype
),
6482 (class_tree
? '+' : '-'),
6484 /* If we are messaging an 'id' or 'Class' object and made it here,
6485 then we have failed to find _any_ instance or class method,
6488 warning (0, "no %<%c%E%> method found",
6489 (class_tree
? '+' : '-'),
6492 if (!warn_missing_methods
)
6494 warning_at (input_location
,
6495 0, "(Messages without a matching method signature");
6496 warning_at (input_location
,
6497 0, "will be assumed to return %<id%> and accept");
6498 warning_at (input_location
,
6499 0, "%<...%> as arguments.)");
6500 warn_missing_methods
= true;
6504 /* Save the selector name for printing error messages. */
6505 current_objc_message_selector
= sel_name
;
6507 /* Build the parameters list for looking up the method.
6508 These are the object itself and the selector. */
6510 if (flag_typed_selectors
)
6511 selector
= build_typed_selector_reference (input_location
,
6512 sel_name
, method_prototype
);
6514 selector
= build_selector_reference (input_location
, sel_name
);
6516 retval
= build_objc_method_call (input_location
, super
, method_prototype
,
6518 selector
, method_params
);
6520 current_objc_message_selector
= 0;
6525 /* Build a tree expression to send OBJECT the operation SELECTOR,
6526 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
6527 assuming the method has prototype METHOD_PROTOTYPE.
6528 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
6529 LOC is the location of the expression to build.
6530 Use METHOD_PARAMS as list of args to pass to the method.
6531 If SUPER_FLAG is nonzero, we look up the superclass's method. */
6534 build_objc_method_call (location_t loc
, int super_flag
, tree method_prototype
,
6535 tree lookup_object
, tree selector
,
6538 tree sender
= (super_flag
? umsg_super_decl
:
6539 (!flag_next_runtime
|| flag_nil_receivers
6540 ? (flag_objc_direct_dispatch
6543 : umsg_nonnil_decl
));
6544 tree rcv_p
= (super_flag
? objc_super_type
: objc_object_type
);
6546 /* If a prototype for the method to be called exists, then cast
6547 the sender's return type and arguments to match that of the method.
6548 Otherwise, leave sender as is. */
6551 ? TREE_VALUE (TREE_TYPE (method_prototype
))
6552 : objc_object_type
);
6554 = build_pointer_type
6555 (build_function_type
6558 (method_prototype
, METHOD_REF
, super_flag
)));
6561 lookup_object
= build_c_cast (loc
, rcv_p
, lookup_object
);
6563 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
6564 lookup_object
= save_expr (lookup_object
);
6566 if (flag_next_runtime
)
6568 /* If we are returning a struct in memory, and the address
6569 of that memory location is passed as a hidden first
6570 argument, then change which messenger entry point this
6571 expr will call. NB: Note that sender_cast remains
6572 unchanged (it already has a struct return type). */
6573 if (!targetm
.calls
.struct_value_rtx (0, 0)
6574 && (TREE_CODE (ret_type
) == RECORD_TYPE
6575 || TREE_CODE (ret_type
) == UNION_TYPE
)
6576 && targetm
.calls
.return_in_memory (ret_type
, 0))
6577 sender
= (super_flag
? umsg_super_stret_decl
:
6578 flag_nil_receivers
? umsg_stret_decl
: umsg_nonnil_stret_decl
);
6580 method_params
= tree_cons (NULL_TREE
, lookup_object
,
6581 tree_cons (NULL_TREE
, selector
,
6583 method
= build_fold_addr_expr_loc (input_location
, sender
);
6587 /* This is the portable (GNU) way. */
6590 /* First, call the lookup function to get a pointer to the method,
6591 then cast the pointer, then call it with the method arguments. */
6593 object
= (super_flag
? self_decl
: lookup_object
);
6595 t
= tree_cons (NULL_TREE
, selector
, NULL_TREE
);
6596 t
= tree_cons (NULL_TREE
, lookup_object
, t
);
6597 method
= build_function_call (loc
, sender
, t
);
6599 /* Pass the object to the method. */
6600 method_params
= tree_cons (NULL_TREE
, object
,
6601 tree_cons (NULL_TREE
, selector
,
6605 /* ??? Selector is not at this point something we can use inside
6606 the compiler itself. Set it to garbage for the nonce. */
6607 t
= build3 (OBJ_TYPE_REF
, sender_cast
, method
, lookup_object
, size_zero_node
);
6608 return build_function_call (loc
,
6613 build_protocol_reference (tree p
)
6616 const char *proto_name
;
6618 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
6620 proto_name
= synth_id_with_class_suffix ("_OBJC_PROTOCOL", p
);
6621 decl
= start_var_decl (objc_protocol_template
, proto_name
);
6623 PROTOCOL_FORWARD_DECL (p
) = decl
;
6626 /* This function is called by the parser when (and only when) a
6627 @protocol() expression is found, in order to compile it. */
6629 objc_build_protocol_expr (tree protoname
)
6632 tree p
= lookup_protocol (protoname
);
6636 error ("cannot find protocol declaration for %qE",
6638 return error_mark_node
;
6641 if (!PROTOCOL_FORWARD_DECL (p
))
6642 build_protocol_reference (p
);
6644 expr
= build_unary_op (input_location
,
6645 ADDR_EXPR
, PROTOCOL_FORWARD_DECL (p
), 0);
6647 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
6648 if we have it, rather than converting it here. */
6649 expr
= convert (objc_protocol_type
, expr
);
6651 /* The @protocol() expression is being compiled into a pointer to a
6652 statically allocated instance of the Protocol class. To become
6653 usable at runtime, the 'isa' pointer of the instance need to be
6654 fixed up at runtime by the runtime library, to point to the
6655 actual 'Protocol' class. */
6657 /* For the GNU runtime, put the static Protocol instance in the list
6658 of statically allocated instances, so that we make sure that its
6659 'isa' pointer is fixed up at runtime by the GNU runtime library
6660 to point to the Protocol class (at runtime, when loading the
6661 module, the GNU runtime library loops on the statically allocated
6662 instances (as found in the defs field in objc_symtab) and fixups
6663 all the 'isa' pointers of those objects). */
6664 if (! flag_next_runtime
)
6666 /* This type is a struct containing the fields of a Protocol
6667 object. (Cfr. objc_protocol_type instead is the type of a pointer
6668 to such a struct). */
6669 tree protocol_struct_type
= xref_tag
6670 (RECORD_TYPE
, get_identifier (PROTOCOL_OBJECT_CLASS_NAME
));
6673 /* Look for the list of Protocol statically allocated instances
6674 to fixup at runtime. Create a new list to hold Protocol
6675 statically allocated instances, if the list is not found. At
6676 present there is only another list, holding NSConstantString
6677 static instances to be fixed up at runtime. */
6678 for (chain
= &objc_static_instances
;
6679 *chain
&& TREE_VALUE (*chain
) != protocol_struct_type
;
6680 chain
= &TREE_CHAIN (*chain
));
6683 *chain
= tree_cons (NULL_TREE
, protocol_struct_type
, NULL_TREE
);
6684 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type
),
6688 /* Add this statically allocated instance to the Protocol list. */
6689 TREE_PURPOSE (*chain
) = tree_cons (NULL_TREE
,
6690 PROTOCOL_FORWARD_DECL (p
),
6691 TREE_PURPOSE (*chain
));
6698 /* This function is called by the parser when a @selector() expression
6699 is found, in order to compile it. It is only called by the parser
6700 and only to compile a @selector(). LOC is the location of the
6703 objc_build_selector_expr (location_t loc
, tree selnamelist
)
6707 /* Obtain the full selector name. */
6708 if (TREE_CODE (selnamelist
) == IDENTIFIER_NODE
)
6709 /* A unary selector. */
6710 selname
= selnamelist
;
6711 else if (TREE_CODE (selnamelist
) == TREE_LIST
)
6712 selname
= build_keyword_selector (selnamelist
);
6716 /* If we are required to check @selector() expressions as they
6717 are found, check that the selector has been declared. */
6718 if (warn_undeclared_selector
)
6720 /* Look the selector up in the list of all known class and
6721 instance methods (up to this line) to check that the selector
6725 /* First try with instance methods. */
6726 hsh
= hash_lookup (nst_method_hash_list
, selname
);
6728 /* If not found, try with class methods. */
6731 hsh
= hash_lookup (cls_method_hash_list
, selname
);
6734 /* If still not found, print out a warning. */
6737 warning (0, "undeclared selector %qE", selname
);
6742 if (flag_typed_selectors
)
6743 return build_typed_selector_reference (loc
, selname
, 0);
6745 return build_selector_reference (loc
, selname
);
6749 objc_build_encode_expr (tree type
)
6754 encode_type (type
, obstack_object_size (&util_obstack
),
6755 OBJC_ENCODE_INLINE_DEFS
);
6756 obstack_1grow (&util_obstack
, 0); /* null terminate string */
6757 string
= XOBFINISH (&util_obstack
, const char *);
6759 /* Synthesize a string that represents the encoded struct/union. */
6760 result
= my_build_string (strlen (string
) + 1, string
);
6761 obstack_free (&util_obstack
, util_firstobj
);
6766 build_ivar_reference (tree id
)
6768 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
)
6770 /* Historically, a class method that produced objects (factory
6771 method) would assign `self' to the instance that it
6772 allocated. This would effectively turn the class method into
6773 an instance method. Following this assignment, the instance
6774 variables could be accessed. That practice, while safe,
6775 violates the simple rule that a class method should not refer
6776 to an instance variable. It's better to catch the cases
6777 where this is done unknowingly than to support the above
6779 warning (0, "instance variable %qE accessed in class method",
6781 self_decl
= convert (objc_instance_type
, self_decl
); /* cast */
6784 return objc_build_component_ref (build_indirect_ref (input_location
,
6785 self_decl
, RO_ARROW
),
6789 /* Compute a hash value for a given method SEL_NAME. */
6792 hash_func (tree sel_name
)
6794 const unsigned char *s
6795 = (const unsigned char *)IDENTIFIER_POINTER (sel_name
);
6799 h
= h
* 67 + *s
++ - 113;
6806 nst_method_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
6807 cls_method_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
6809 /* Initialize the hash table used to hold the constant string objects. */
6810 string_htab
= htab_create_ggc (31, string_hash
,
6813 /* Initialize the hash table used to hold EH-volatilized types. */
6814 volatilized_htab
= htab_create_ggc (31, volatilized_hash
,
6815 volatilized_eq
, NULL
);
6818 /* WARNING!!!! hash_enter is called with a method, and will peek
6819 inside to find its selector! But hash_lookup is given a selector
6820 directly, and looks for the selector that's inside the found
6821 entry's key (method) for comparison. */
6824 hash_enter (hash
*hashlist
, tree method
)
6827 int slot
= hash_func (METHOD_SEL_NAME (method
)) % SIZEHASHTABLE
;
6829 obj
= ggc_alloc_hashed_entry ();
6831 obj
->next
= hashlist
[slot
];
6834 hashlist
[slot
] = obj
; /* append to front */
6838 hash_lookup (hash
*hashlist
, tree sel_name
)
6842 target
= hashlist
[hash_func (sel_name
) % SIZEHASHTABLE
];
6846 if (sel_name
== METHOD_SEL_NAME (target
->key
))
6849 target
= target
->next
;
6855 hash_add_attr (hash entry
, tree value
)
6859 obj
= ggc_alloc_hashed_attribute ();
6860 obj
->next
= entry
->list
;
6863 entry
->list
= obj
; /* append to front */
6867 lookup_method (tree mchain
, tree method
)
6871 if (TREE_CODE (method
) == IDENTIFIER_NODE
)
6874 key
= METHOD_SEL_NAME (method
);
6878 if (METHOD_SEL_NAME (mchain
) == key
)
6881 mchain
= DECL_CHAIN (mchain
);
6886 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance method
6887 in INTERFACE, along with any categories and protocols attached thereto.
6888 If method is not found, and the OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS,
6889 recursively examine the INTERFACE's superclass. If OBJC_LOOKUP_CLASS is
6890 set, OBJC_LOOKUP_NO_SUPER is cleared, and no suitable class method could
6891 be found in INTERFACE or any of its superclasses, look for an _instance_
6892 method of the same name in the root class as a last resort.
6894 If a suitable method cannot be found, return NULL_TREE. */
6897 lookup_method_static (tree interface
, tree ident
, int flags
)
6899 tree meth
= NULL_TREE
, root_inter
= NULL_TREE
;
6900 tree inter
= interface
;
6901 int is_class
= (flags
& OBJC_LOOKUP_CLASS
);
6902 int no_superclasses
= (flags
& OBJC_LOOKUP_NO_SUPER
);
6906 tree chain
= is_class
? CLASS_CLS_METHODS (inter
) : CLASS_NST_METHODS (inter
);
6907 tree category
= inter
;
6909 /* First, look up the method in the class itself. */
6910 if ((meth
= lookup_method (chain
, ident
)))
6913 /* Failing that, look for the method in each category of the class. */
6914 while ((category
= CLASS_CATEGORY_LIST (category
)))
6916 chain
= is_class
? CLASS_CLS_METHODS (category
) : CLASS_NST_METHODS (category
);
6918 /* Check directly in each category. */
6919 if ((meth
= lookup_method (chain
, ident
)))
6922 /* Failing that, check in each category's protocols. */
6923 if (CLASS_PROTOCOL_LIST (category
))
6925 if ((meth
= (lookup_method_in_protocol_list
6926 (CLASS_PROTOCOL_LIST (category
), ident
, is_class
))))
6931 /* If not found in categories, check in protocols of the main class. */
6932 if (CLASS_PROTOCOL_LIST (inter
))
6934 if ((meth
= (lookup_method_in_protocol_list
6935 (CLASS_PROTOCOL_LIST (inter
), ident
, is_class
))))
6939 /* If we were instructed not to look in superclasses, don't. */
6940 if (no_superclasses
)
6943 /* Failing that, climb up the inheritance hierarchy. */
6945 inter
= lookup_interface (CLASS_SUPER_NAME (inter
));
6949 /* If no class (factory) method was found, check if an _instance_
6950 method of the same name exists in the root class. This is what
6951 the Objective-C runtime will do. If an instance method was not
6953 return is_class
? lookup_method_static (root_inter
, ident
, 0): NULL_TREE
;
6956 /* Add the method to the hash list if it doesn't contain an identical
6960 add_method_to_hash_list (hash
*hash_list
, tree method
)
6964 if (!(hsh
= hash_lookup (hash_list
, METHOD_SEL_NAME (method
))))
6966 /* Install on a global chain. */
6967 hash_enter (hash_list
, method
);
6971 /* Check types against those; if different, add to a list. */
6973 int already_there
= comp_proto_with_proto (method
, hsh
->key
, 1);
6974 for (loop
= hsh
->list
; !already_there
&& loop
; loop
= loop
->next
)
6975 already_there
|= comp_proto_with_proto (method
, loop
->value
, 1);
6977 hash_add_attr (hsh
, method
);
6982 objc_add_method (tree klass
, tree method
, int is_class
)
6986 if (!(mth
= lookup_method (is_class
6987 ? CLASS_CLS_METHODS (klass
)
6988 : CLASS_NST_METHODS (klass
), method
)))
6990 /* put method on list in reverse order */
6993 DECL_CHAIN (method
) = CLASS_CLS_METHODS (klass
);
6994 CLASS_CLS_METHODS (klass
) = method
;
6998 DECL_CHAIN (method
) = CLASS_NST_METHODS (klass
);
6999 CLASS_NST_METHODS (klass
) = method
;
7004 /* When processing an @interface for a class or category, give hard
7005 errors on methods with identical selectors but differing argument
7006 and/or return types. We do not do this for @implementations, because
7007 C/C++ will do it for us (i.e., there will be duplicate function
7008 definition errors). */
7009 if ((TREE_CODE (klass
) == CLASS_INTERFACE_TYPE
7010 || TREE_CODE (klass
) == CATEGORY_INTERFACE_TYPE
)
7011 && !comp_proto_with_proto (method
, mth
, 1))
7012 error ("duplicate declaration of method %<%c%E%>",
7013 is_class
? '+' : '-',
7014 METHOD_SEL_NAME (mth
));
7018 add_method_to_hash_list (cls_method_hash_list
, method
);
7021 add_method_to_hash_list (nst_method_hash_list
, method
);
7023 /* Instance methods in root classes (and categories thereof)
7024 may act as class methods as a last resort. We also add
7025 instance methods listed in @protocol declarations to
7026 the class hash table, on the assumption that @protocols
7027 may be adopted by root classes or categories. */
7028 if (TREE_CODE (klass
) == CATEGORY_INTERFACE_TYPE
7029 || TREE_CODE (klass
) == CATEGORY_IMPLEMENTATION_TYPE
)
7030 klass
= lookup_interface (CLASS_NAME (klass
));
7032 if (TREE_CODE (klass
) == PROTOCOL_INTERFACE_TYPE
7033 || !CLASS_SUPER_NAME (klass
))
7034 add_method_to_hash_list (cls_method_hash_list
, method
);
7041 add_class (tree class_name
, tree name
)
7043 struct interface_tuple
**slot
;
7045 /* Put interfaces on list in reverse order. */
7046 TREE_CHAIN (class_name
) = interface_chain
;
7047 interface_chain
= class_name
;
7049 if (interface_htab
== NULL
)
7050 interface_htab
= htab_create_ggc (31, hash_interface
, eq_interface
, NULL
);
7051 slot
= (struct interface_tuple
**)
7052 htab_find_slot_with_hash (interface_htab
, name
,
7053 IDENTIFIER_HASH_VALUE (name
),
7057 *slot
= ggc_alloc_cleared_interface_tuple ();
7060 (*slot
)->class_name
= class_name
;
7062 return interface_chain
;
7066 add_category (tree klass
, tree category
)
7068 /* Put categories on list in reverse order. */
7069 tree cat
= lookup_category (klass
, CLASS_SUPER_NAME (category
));
7073 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
7075 CLASS_SUPER_NAME (category
));
7079 CLASS_CATEGORY_LIST (category
) = CLASS_CATEGORY_LIST (klass
);
7080 CLASS_CATEGORY_LIST (klass
) = category
;
7084 /* Called after parsing each instance variable declaration. Necessary to
7085 preserve typedefs and implement public/private...
7087 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
7090 add_instance_variable (tree klass
, int visibility
, tree field_decl
)
7092 tree field_type
= TREE_TYPE (field_decl
);
7093 const char *ivar_name
= DECL_NAME (field_decl
)
7094 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl
)))
7098 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
7100 error ("illegal reference type specified for instance variable %qs",
7102 /* Return class as is without adding this ivar. */
7107 if (field_type
== error_mark_node
|| !TYPE_SIZE (field_type
)
7108 || TYPE_SIZE (field_type
) == error_mark_node
)
7109 /* 'type[0]' is allowed, but 'type[]' is not! */
7111 error ("instance variable %qs has unknown size", ivar_name
);
7112 /* Return class as is without adding this ivar. */
7117 /* Check if the ivar being added has a non-POD C++ type. If so, we will
7118 need to either (1) warn the user about it or (2) generate suitable
7119 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
7120 methods (if '-fobjc-call-cxx-cdtors' was specified). */
7121 if (MAYBE_CLASS_TYPE_P (field_type
)
7122 && (TYPE_NEEDS_CONSTRUCTING (field_type
)
7123 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
)
7124 || TYPE_POLYMORPHIC_P (field_type
)))
7126 tree type_name
= OBJC_TYPE_NAME (field_type
);
7128 if (flag_objc_call_cxx_cdtors
)
7130 /* Since the ObjC runtime will be calling the constructors and
7131 destructors for us, the only thing we can't handle is the lack
7132 of a default constructor. */
7133 if (TYPE_NEEDS_CONSTRUCTING (field_type
)
7134 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type
))
7136 warning (0, "type %qE has no default constructor to call",
7139 /* If we cannot call a constructor, we should also avoid
7140 calling the destructor, for symmetry. */
7141 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
))
7142 warning (0, "destructor for %qE shall not be run either",
7148 static bool warn_cxx_ivars
= false;
7150 if (TYPE_POLYMORPHIC_P (field_type
))
7152 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
7154 error ("type %qE has virtual member functions", type_name
);
7155 error ("illegal aggregate type %qE specified "
7156 "for instance variable %qs",
7157 type_name
, ivar_name
);
7158 /* Return class as is without adding this ivar. */
7162 /* User-defined constructors and destructors are not known to Obj-C
7163 and hence will not be called. This may or may not be a problem. */
7164 if (TYPE_NEEDS_CONSTRUCTING (field_type
))
7165 warning (0, "type %qE has a user-defined constructor", type_name
);
7166 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
))
7167 warning (0, "type %qE has a user-defined destructor", type_name
);
7169 if (!warn_cxx_ivars
)
7171 warning (0, "C++ constructors and destructors will not "
7172 "be invoked for Objective-C fields");
7173 warn_cxx_ivars
= true;
7179 /* Overload the public attribute, it is not used for FIELD_DECLs. */
7183 TREE_PUBLIC (field_decl
) = 0;
7184 TREE_PRIVATE (field_decl
) = 0;
7185 TREE_PROTECTED (field_decl
) = 1;
7189 TREE_PUBLIC (field_decl
) = 1;
7190 TREE_PRIVATE (field_decl
) = 0;
7191 TREE_PROTECTED (field_decl
) = 0;
7195 TREE_PUBLIC (field_decl
) = 0;
7196 TREE_PRIVATE (field_decl
) = 1;
7197 TREE_PROTECTED (field_decl
) = 0;
7202 CLASS_RAW_IVARS (klass
) = chainon (CLASS_RAW_IVARS (klass
), field_decl
);
7208 is_ivar (tree decl_chain
, tree ident
)
7210 for ( ; decl_chain
; decl_chain
= DECL_CHAIN (decl_chain
))
7211 if (DECL_NAME (decl_chain
) == ident
)
7216 /* True if the ivar is private and we are not in its implementation. */
7219 is_private (tree decl
)
7221 return (TREE_PRIVATE (decl
)
7222 && ! is_ivar (CLASS_IVARS (implementation_template
),
7226 /* We have an instance variable reference;, check to see if it is public. */
7229 objc_is_public (tree expr
, tree identifier
)
7231 tree basetype
, decl
;
7234 if (processing_template_decl
)
7238 if (TREE_TYPE (expr
) == error_mark_node
)
7241 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
7243 if (basetype
&& TREE_CODE (basetype
) == RECORD_TYPE
)
7245 if (TYPE_HAS_OBJC_INFO (basetype
) && TYPE_OBJC_INTERFACE (basetype
))
7247 tree klass
= lookup_interface (OBJC_TYPE_NAME (basetype
));
7251 error ("cannot find interface declaration for %qE",
7252 OBJC_TYPE_NAME (basetype
));
7256 if ((decl
= is_ivar (get_class_ivars (klass
, true), identifier
)))
7258 if (TREE_PUBLIC (decl
))
7261 /* Important difference between the Stepstone translator:
7262 all instance variables should be public within the context
7263 of the implementation. */
7264 if (objc_implementation_context
7265 && ((TREE_CODE (objc_implementation_context
)
7266 == CLASS_IMPLEMENTATION_TYPE
)
7267 || (TREE_CODE (objc_implementation_context
)
7268 == CATEGORY_IMPLEMENTATION_TYPE
)))
7270 tree curtype
= TYPE_MAIN_VARIANT
7271 (CLASS_STATIC_TEMPLATE
7272 (implementation_template
));
7274 if (basetype
== curtype
7275 || DERIVED_FROM_P (basetype
, curtype
))
7277 int priv
= is_private (decl
);
7280 error ("instance variable %qE is declared private",
7287 /* The 2.95.2 compiler sometimes allowed C functions to access
7288 non-@public ivars. We will let this slide for now... */
7289 if (!objc_method_context
)
7291 warning (0, "instance variable %qE is %s; "
7292 "this will be a hard error in the future",
7294 TREE_PRIVATE (decl
) ? "@private" : "@protected");
7298 error ("instance variable %qE is declared %s",
7300 TREE_PRIVATE (decl
) ? "private" : "protected");
7309 /* Make sure all entries in CHAIN are also in LIST. */
7312 check_methods (tree chain
, tree list
, int mtype
)
7318 if (!lookup_method (list
, chain
))
7322 if (TREE_CODE (objc_implementation_context
)
7323 == CLASS_IMPLEMENTATION_TYPE
)
7324 warning (0, "incomplete implementation of class %qE",
7325 CLASS_NAME (objc_implementation_context
));
7326 else if (TREE_CODE (objc_implementation_context
)
7327 == CATEGORY_IMPLEMENTATION_TYPE
)
7328 warning (0, "incomplete implementation of category %qE",
7329 CLASS_SUPER_NAME (objc_implementation_context
));
7333 warning (0, "method definition for %<%c%E%> not found",
7334 mtype
, METHOD_SEL_NAME (chain
));
7337 chain
= DECL_CHAIN (chain
);
7343 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
7346 conforms_to_protocol (tree klass
, tree protocol
)
7348 if (TREE_CODE (protocol
) == PROTOCOL_INTERFACE_TYPE
)
7350 tree p
= CLASS_PROTOCOL_LIST (klass
);
7351 while (p
&& TREE_VALUE (p
) != protocol
)
7356 tree super
= (CLASS_SUPER_NAME (klass
)
7357 ? lookup_interface (CLASS_SUPER_NAME (klass
))
7359 int tmp
= super
? conforms_to_protocol (super
, protocol
) : 0;
7368 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
7369 CONTEXT. This is one of two mechanisms to check protocol integrity. */
7372 check_methods_accessible (tree chain
, tree context
, int mtype
)
7376 tree base_context
= context
;
7380 context
= base_context
;
7384 list
= CLASS_CLS_METHODS (context
);
7386 list
= CLASS_NST_METHODS (context
);
7388 if (lookup_method (list
, chain
))
7391 else if (TREE_CODE (context
) == CLASS_IMPLEMENTATION_TYPE
7392 || TREE_CODE (context
) == CLASS_INTERFACE_TYPE
)
7393 context
= (CLASS_SUPER_NAME (context
)
7394 ? lookup_interface (CLASS_SUPER_NAME (context
))
7397 else if (TREE_CODE (context
) == CATEGORY_IMPLEMENTATION_TYPE
7398 || TREE_CODE (context
) == CATEGORY_INTERFACE_TYPE
)
7399 context
= (CLASS_NAME (context
)
7400 ? lookup_interface (CLASS_NAME (context
))
7406 if (context
== NULL_TREE
)
7410 if (TREE_CODE (objc_implementation_context
)
7411 == CLASS_IMPLEMENTATION_TYPE
)
7412 warning (0, "incomplete implementation of class %qE",
7413 CLASS_NAME (objc_implementation_context
));
7414 else if (TREE_CODE (objc_implementation_context
)
7415 == CATEGORY_IMPLEMENTATION_TYPE
)
7416 warning (0, "incomplete implementation of category %qE",
7417 CLASS_SUPER_NAME (objc_implementation_context
));
7420 warning (0, "method definition for %<%c%E%> not found",
7421 mtype
, METHOD_SEL_NAME (chain
));
7424 chain
= TREE_CHAIN (chain
); /* next method... */
7429 /* Check whether the current interface (accessible via
7430 'objc_implementation_context') actually implements protocol P, along
7431 with any protocols that P inherits. */
7434 check_protocol (tree p
, const char *type
, tree name
)
7436 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
7440 /* Ensure that all protocols have bodies! */
7443 f1
= check_methods (PROTOCOL_CLS_METHODS (p
),
7444 CLASS_CLS_METHODS (objc_implementation_context
),
7446 f2
= check_methods (PROTOCOL_NST_METHODS (p
),
7447 CLASS_NST_METHODS (objc_implementation_context
),
7452 f1
= check_methods_accessible (PROTOCOL_CLS_METHODS (p
),
7453 objc_implementation_context
,
7455 f2
= check_methods_accessible (PROTOCOL_NST_METHODS (p
),
7456 objc_implementation_context
,
7461 warning (0, "%s %qE does not fully implement the %qE protocol",
7462 type
, name
, PROTOCOL_NAME (p
));
7465 /* Check protocols recursively. */
7466 if (PROTOCOL_LIST (p
))
7468 tree subs
= PROTOCOL_LIST (p
);
7470 lookup_interface (CLASS_SUPER_NAME (implementation_template
));
7474 tree sub
= TREE_VALUE (subs
);
7476 /* If the superclass does not conform to the protocols
7477 inherited by P, then we must! */
7478 if (!super_class
|| !conforms_to_protocol (super_class
, sub
))
7479 check_protocol (sub
, type
, name
);
7480 subs
= TREE_CHAIN (subs
);
7485 /* Check whether the current interface (accessible via
7486 'objc_implementation_context') actually implements the protocols listed
7490 check_protocols (tree proto_list
, const char *type
, tree name
)
7492 for ( ; proto_list
; proto_list
= TREE_CHAIN (proto_list
))
7494 tree p
= TREE_VALUE (proto_list
);
7496 check_protocol (p
, type
, name
);
7500 /* Make sure that the class CLASS_NAME is defined
7501 CODE says which kind of thing CLASS_NAME ought to be.
7502 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
7503 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
7506 start_class (enum tree_code code
, tree class_name
, tree super_name
,
7512 if (current_namespace
!= global_namespace
) {
7513 error ("Objective-C declarations may only appear in global scope");
7515 #endif /* OBJCPLUS */
7517 if (objc_implementation_context
)
7519 warning (0, "%<@end%> missing in implementation context");
7520 finish_class (objc_implementation_context
);
7521 objc_ivar_chain
= NULL_TREE
;
7522 objc_implementation_context
= NULL_TREE
;
7525 klass
= make_node (code
);
7526 TYPE_LANG_SLOT_1 (klass
) = make_tree_vec (CLASS_LANG_SLOT_ELTS
);
7528 /* Check for existence of the super class, if one was specified. Note
7529 that we must have seen an @interface, not just a @class. If we
7530 are looking at a @compatibility_alias, traverse it first. */
7531 if ((code
== CLASS_INTERFACE_TYPE
|| code
== CLASS_IMPLEMENTATION_TYPE
)
7534 tree super
= objc_is_class_name (super_name
);
7536 if (!super
|| !lookup_interface (super
))
7538 error ("cannot find interface declaration for %qE, superclass of %qE",
7539 super
? super
: super_name
,
7541 super_name
= NULL_TREE
;
7547 CLASS_NAME (klass
) = class_name
;
7548 CLASS_SUPER_NAME (klass
) = super_name
;
7549 CLASS_CLS_METHODS (klass
) = NULL_TREE
;
7551 if (! objc_is_class_name (class_name
)
7552 && (decl
= lookup_name (class_name
)))
7554 error ("%qE redeclared as different kind of symbol",
7556 error ("previous declaration of %q+D",
7560 if (code
== CLASS_IMPLEMENTATION_TYPE
)
7565 for (chain
= implemented_classes
; chain
; chain
= TREE_CHAIN (chain
))
7566 if (TREE_VALUE (chain
) == class_name
)
7568 error ("reimplementation of class %qE",
7570 return error_mark_node
;
7572 implemented_classes
= tree_cons (NULL_TREE
, class_name
,
7573 implemented_classes
);
7576 /* Reset for multiple classes per file. */
7579 objc_implementation_context
= klass
;
7581 /* Lookup the interface for this implementation. */
7583 if (!(implementation_template
= lookup_interface (class_name
)))
7585 warning (0, "cannot find interface declaration for %qE",
7587 add_class (implementation_template
= objc_implementation_context
,
7591 /* If a super class has been specified in the implementation,
7592 insure it conforms to the one specified in the interface. */
7595 && (super_name
!= CLASS_SUPER_NAME (implementation_template
)))
7597 tree previous_name
= CLASS_SUPER_NAME (implementation_template
);
7598 error ("conflicting super class name %qE",
7601 error ("previous declaration of %qE", previous_name
);
7603 error ("previous declaration");
7606 else if (! super_name
)
7608 CLASS_SUPER_NAME (objc_implementation_context
)
7609 = CLASS_SUPER_NAME (implementation_template
);
7613 else if (code
== CLASS_INTERFACE_TYPE
)
7615 if (lookup_interface (class_name
))
7617 error ("duplicate interface declaration for class %qE",
7619 warning (0, "duplicate interface declaration for class %qE",
7623 add_class (klass
, class_name
);
7626 CLASS_PROTOCOL_LIST (klass
)
7627 = lookup_and_install_protocols (protocol_list
);
7630 else if (code
== CATEGORY_INTERFACE_TYPE
)
7632 tree class_category_is_assoc_with
;
7634 /* For a category, class_name is really the name of the class that
7635 the following set of methods will be associated with. We must
7636 find the interface so that can derive the objects template. */
7638 if (!(class_category_is_assoc_with
= lookup_interface (class_name
)))
7640 error ("cannot find interface declaration for %qE",
7642 exit (FATAL_EXIT_CODE
);
7645 add_category (class_category_is_assoc_with
, klass
);
7648 CLASS_PROTOCOL_LIST (klass
)
7649 = lookup_and_install_protocols (protocol_list
);
7652 else if (code
== CATEGORY_IMPLEMENTATION_TYPE
)
7654 /* Reset for multiple classes per file. */
7657 objc_implementation_context
= klass
;
7659 /* For a category, class_name is really the name of the class that
7660 the following set of methods will be associated with. We must
7661 find the interface so that can derive the objects template. */
7663 if (!(implementation_template
= lookup_interface (class_name
)))
7665 error ("cannot find interface declaration for %qE",
7667 exit (FATAL_EXIT_CODE
);
7674 continue_class (tree klass
)
7676 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
7677 || TREE_CODE (klass
) == CATEGORY_IMPLEMENTATION_TYPE
)
7679 struct imp_entry
*imp_entry
;
7681 /* Check consistency of the instance variables. */
7683 if (CLASS_RAW_IVARS (klass
))
7684 check_ivars (implementation_template
, klass
);
7686 /* code generation */
7689 push_lang_context (lang_name_c
);
7692 build_private_template (implementation_template
);
7693 uprivate_record
= CLASS_STATIC_TEMPLATE (implementation_template
);
7694 objc_instance_type
= build_pointer_type (uprivate_record
);
7696 imp_entry
= ggc_alloc_imp_entry ();
7698 imp_entry
->next
= imp_list
;
7699 imp_entry
->imp_context
= klass
;
7700 imp_entry
->imp_template
= implementation_template
;
7702 synth_forward_declarations ();
7703 imp_entry
->class_decl
= UOBJC_CLASS_decl
;
7704 imp_entry
->meta_decl
= UOBJC_METACLASS_decl
;
7705 imp_entry
->has_cxx_cdtors
= 0;
7707 /* Append to front and increment count. */
7708 imp_list
= imp_entry
;
7709 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
)
7715 pop_lang_context ();
7716 #endif /* OBJCPLUS */
7718 return get_class_ivars (implementation_template
, true);
7721 else if (TREE_CODE (klass
) == CLASS_INTERFACE_TYPE
)
7724 push_lang_context (lang_name_c
);
7725 #endif /* OBJCPLUS */
7727 objc_collecting_ivars
= 1;
7728 build_private_template (klass
);
7729 objc_collecting_ivars
= 0;
7732 pop_lang_context ();
7733 #endif /* OBJCPLUS */
7739 return error_mark_node
;
7742 /* This is called once we see the "@end" in an interface/implementation. */
7745 finish_class (tree klass
)
7747 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
)
7749 /* All code generation is done in finish_objc. */
7751 if (implementation_template
!= objc_implementation_context
)
7753 /* Ensure that all method listed in the interface contain bodies. */
7754 check_methods (CLASS_CLS_METHODS (implementation_template
),
7755 CLASS_CLS_METHODS (objc_implementation_context
), '+');
7756 check_methods (CLASS_NST_METHODS (implementation_template
),
7757 CLASS_NST_METHODS (objc_implementation_context
), '-');
7759 if (CLASS_PROTOCOL_LIST (implementation_template
))
7760 check_protocols (CLASS_PROTOCOL_LIST (implementation_template
),
7762 CLASS_NAME (objc_implementation_context
));
7766 else if (TREE_CODE (klass
) == CATEGORY_IMPLEMENTATION_TYPE
)
7768 tree category
= lookup_category (implementation_template
, CLASS_SUPER_NAME (klass
));
7772 /* Ensure all method listed in the interface contain bodies. */
7773 check_methods (CLASS_CLS_METHODS (category
),
7774 CLASS_CLS_METHODS (objc_implementation_context
), '+');
7775 check_methods (CLASS_NST_METHODS (category
),
7776 CLASS_NST_METHODS (objc_implementation_context
), '-');
7778 if (CLASS_PROTOCOL_LIST (category
))
7779 check_protocols (CLASS_PROTOCOL_LIST (category
),
7781 CLASS_SUPER_NAME (objc_implementation_context
));
7787 add_protocol (tree protocol
)
7789 /* Put protocol on list in reverse order. */
7790 TREE_CHAIN (protocol
) = protocol_chain
;
7791 protocol_chain
= protocol
;
7792 return protocol_chain
;
7796 lookup_protocol (tree ident
)
7800 for (chain
= protocol_chain
; chain
; chain
= TREE_CHAIN (chain
))
7801 if (ident
== PROTOCOL_NAME (chain
))
7807 /* This function forward declares the protocols named by NAMES. If
7808 they are already declared or defined, the function has no effect. */
7811 objc_declare_protocols (tree names
)
7816 if (current_namespace
!= global_namespace
) {
7817 error ("Objective-C declarations may only appear in global scope");
7819 #endif /* OBJCPLUS */
7821 for (list
= names
; list
; list
= TREE_CHAIN (list
))
7823 tree name
= TREE_VALUE (list
);
7825 if (lookup_protocol (name
) == NULL_TREE
)
7827 tree protocol
= make_node (PROTOCOL_INTERFACE_TYPE
);
7829 TYPE_LANG_SLOT_1 (protocol
)
7830 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS
);
7831 PROTOCOL_NAME (protocol
) = name
;
7832 PROTOCOL_LIST (protocol
) = NULL_TREE
;
7833 add_protocol (protocol
);
7834 PROTOCOL_DEFINED (protocol
) = 0;
7835 PROTOCOL_FORWARD_DECL (protocol
) = NULL_TREE
;
7841 start_protocol (enum tree_code code
, tree name
, tree list
)
7846 if (current_namespace
!= global_namespace
) {
7847 error ("Objective-C declarations may only appear in global scope");
7849 #endif /* OBJCPLUS */
7851 protocol
= lookup_protocol (name
);
7855 protocol
= make_node (code
);
7856 TYPE_LANG_SLOT_1 (protocol
) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS
);
7858 PROTOCOL_NAME (protocol
) = name
;
7859 PROTOCOL_LIST (protocol
) = lookup_and_install_protocols (list
);
7860 add_protocol (protocol
);
7861 PROTOCOL_DEFINED (protocol
) = 1;
7862 PROTOCOL_FORWARD_DECL (protocol
) = NULL_TREE
;
7864 check_protocol_recursively (protocol
, list
);
7866 else if (! PROTOCOL_DEFINED (protocol
))
7868 PROTOCOL_DEFINED (protocol
) = 1;
7869 PROTOCOL_LIST (protocol
) = lookup_and_install_protocols (list
);
7871 check_protocol_recursively (protocol
, list
);
7875 warning (0, "duplicate declaration for protocol %qE",
7882 /* "Encode" a data type into a string, which grows in util_obstack.
7883 ??? What is the FORMAT? Someone please document this! */
7886 encode_type_qualifiers (tree declspecs
)
7890 for (spec
= declspecs
; spec
; spec
= TREE_CHAIN (spec
))
7892 if (ridpointers
[(int) RID_IN
] == TREE_VALUE (spec
))
7893 obstack_1grow (&util_obstack
, 'n');
7894 else if (ridpointers
[(int) RID_INOUT
] == TREE_VALUE (spec
))
7895 obstack_1grow (&util_obstack
, 'N');
7896 else if (ridpointers
[(int) RID_OUT
] == TREE_VALUE (spec
))
7897 obstack_1grow (&util_obstack
, 'o');
7898 else if (ridpointers
[(int) RID_BYCOPY
] == TREE_VALUE (spec
))
7899 obstack_1grow (&util_obstack
, 'O');
7900 else if (ridpointers
[(int) RID_BYREF
] == TREE_VALUE (spec
))
7901 obstack_1grow (&util_obstack
, 'R');
7902 else if (ridpointers
[(int) RID_ONEWAY
] == TREE_VALUE (spec
))
7903 obstack_1grow (&util_obstack
, 'V');
7907 /* Encode a pointer type. */
7910 encode_pointer (tree type
, int curtype
, int format
)
7912 tree pointer_to
= TREE_TYPE (type
);
7914 if (TREE_CODE (pointer_to
) == RECORD_TYPE
)
7916 if (OBJC_TYPE_NAME (pointer_to
)
7917 && TREE_CODE (OBJC_TYPE_NAME (pointer_to
)) == IDENTIFIER_NODE
)
7919 const char *name
= IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to
));
7921 if (strcmp (name
, TAG_OBJECT
) == 0) /* '@' */
7923 obstack_1grow (&util_obstack
, '@');
7926 else if (TYPE_HAS_OBJC_INFO (pointer_to
)
7927 && TYPE_OBJC_INTERFACE (pointer_to
))
7929 if (generating_instance_variables
)
7931 obstack_1grow (&util_obstack
, '@');
7932 obstack_1grow (&util_obstack
, '"');
7933 obstack_grow (&util_obstack
, name
, strlen (name
));
7934 obstack_1grow (&util_obstack
, '"');
7939 obstack_1grow (&util_obstack
, '@');
7943 else if (strcmp (name
, TAG_CLASS
) == 0) /* '#' */
7945 obstack_1grow (&util_obstack
, '#');
7948 else if (strcmp (name
, TAG_SELECTOR
) == 0) /* ':' */
7950 obstack_1grow (&util_obstack
, ':');
7955 else if (TREE_CODE (pointer_to
) == INTEGER_TYPE
7956 && TYPE_MODE (pointer_to
) == QImode
)
7958 tree pname
= TREE_CODE (OBJC_TYPE_NAME (pointer_to
)) == IDENTIFIER_NODE
7959 ? OBJC_TYPE_NAME (pointer_to
)
7960 : DECL_NAME (OBJC_TYPE_NAME (pointer_to
));
7962 if (!flag_next_runtime
|| strcmp (IDENTIFIER_POINTER (pname
), "BOOL"))
7964 /* It appears that "r*" means "const char *" rather than
7966 if (TYPE_READONLY (pointer_to
))
7967 obstack_1grow (&util_obstack
, 'r');
7969 obstack_1grow (&util_obstack
, '*');
7974 /* We have a type that does not get special treatment. */
7976 /* NeXT extension */
7977 obstack_1grow (&util_obstack
, '^');
7978 encode_type (pointer_to
, curtype
, format
);
7982 encode_array (tree type
, int curtype
, int format
)
7984 tree an_int_cst
= TYPE_SIZE (type
);
7985 tree array_of
= TREE_TYPE (type
);
7988 /* An incomplete array is treated like a pointer. */
7989 if (an_int_cst
== NULL
)
7991 encode_pointer (type
, curtype
, format
);
7995 if (TREE_INT_CST_LOW (TYPE_SIZE (array_of
)) == 0)
7996 sprintf (buffer
, "[" HOST_WIDE_INT_PRINT_DEC
, (HOST_WIDE_INT
)0);
7998 sprintf (buffer
, "[" HOST_WIDE_INT_PRINT_DEC
,
7999 TREE_INT_CST_LOW (an_int_cst
)
8000 / TREE_INT_CST_LOW (TYPE_SIZE (array_of
)));
8002 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8003 encode_type (array_of
, curtype
, format
);
8004 obstack_1grow (&util_obstack
, ']');
8009 encode_aggregate_fields (tree type
, int pointed_to
, int curtype
, int format
)
8011 tree field
= TYPE_FIELDS (type
);
8013 for (; field
; field
= DECL_CHAIN (field
))
8016 /* C++ static members, and things that are not field at all,
8017 should not appear in the encoding. */
8018 if (TREE_CODE (field
) != FIELD_DECL
|| TREE_STATIC (field
))
8022 /* Recursively encode fields of embedded base classes. */
8023 if (DECL_ARTIFICIAL (field
) && !DECL_NAME (field
)
8024 && TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
)
8026 encode_aggregate_fields (TREE_TYPE (field
),
8027 pointed_to
, curtype
, format
);
8031 if (generating_instance_variables
&& !pointed_to
)
8033 tree fname
= DECL_NAME (field
);
8035 obstack_1grow (&util_obstack
, '"');
8037 if (fname
&& TREE_CODE (fname
) == IDENTIFIER_NODE
)
8038 obstack_grow (&util_obstack
,
8039 IDENTIFIER_POINTER (fname
),
8040 strlen (IDENTIFIER_POINTER (fname
)));
8042 obstack_1grow (&util_obstack
, '"');
8045 encode_field_decl (field
, curtype
, format
);
8050 encode_aggregate_within (tree type
, int curtype
, int format
, int left
,
8054 /* NB: aggregates that are pointed to have slightly different encoding
8055 rules in that you never encode the names of instance variables. */
8056 int ob_size
= obstack_object_size (&util_obstack
);
8057 char c1
= ob_size
> 1 ? *(obstack_next_free (&util_obstack
) - 2) : 0;
8058 char c0
= ob_size
> 0 ? *(obstack_next_free (&util_obstack
) - 1) : 0;
8059 int pointed_to
= (c0
== '^' || (c1
== '^' && c0
== 'r'));
8061 = ((format
== OBJC_ENCODE_INLINE_DEFS
|| generating_instance_variables
)
8062 && (!pointed_to
|| ob_size
- curtype
== (c1
== 'r' ? 2 : 1)));
8064 /* Traverse struct aliases; it is important to get the
8065 original struct and its tag name (if any). */
8066 type
= TYPE_MAIN_VARIANT (type
);
8067 name
= OBJC_TYPE_NAME (type
);
8068 /* Open parenth/bracket. */
8069 obstack_1grow (&util_obstack
, left
);
8071 /* Encode the struct/union tag name, or '?' if a tag was
8072 not provided. Typedef aliases do not qualify. */
8074 /* For compatibility with the NeXT runtime, ObjC++ encodes template
8075 args as a composite struct tag name. */
8076 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
8077 /* Did this struct have a tag? */
8078 && !TYPE_WAS_ANONYMOUS (type
))
8079 obstack_grow (&util_obstack
,
8080 decl_as_string (type
, TFF_DECL_SPECIFIERS
| TFF_UNQUALIFIED_NAME
),
8081 strlen (decl_as_string (type
, TFF_DECL_SPECIFIERS
| TFF_UNQUALIFIED_NAME
)));
8083 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
8084 obstack_grow (&util_obstack
,
8085 IDENTIFIER_POINTER (name
),
8086 strlen (IDENTIFIER_POINTER (name
)));
8089 obstack_1grow (&util_obstack
, '?');
8091 /* Encode the types (and possibly names) of the inner fields,
8093 if (inline_contents
)
8095 obstack_1grow (&util_obstack
, '=');
8096 encode_aggregate_fields (type
, pointed_to
, curtype
, format
);
8098 /* Close parenth/bracket. */
8099 obstack_1grow (&util_obstack
, right
);
8103 encode_aggregate (tree type
, int curtype
, int format
)
8105 enum tree_code code
= TREE_CODE (type
);
8111 encode_aggregate_within (type
, curtype
, format
, '{', '}');
8116 encode_aggregate_within (type
, curtype
, format
, '(', ')');
8121 obstack_1grow (&util_obstack
, 'i');
8129 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
8133 encode_next_bitfield (int width
)
8136 sprintf (buffer
, "b%d", width
);
8137 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8140 /* FORMAT will be OBJC_ENCODE_INLINE_DEFS or OBJC_ENCODE_DONT_INLINE_DEFS. */
8142 encode_type (tree type
, int curtype
, int format
)
8144 enum tree_code code
= TREE_CODE (type
);
8147 if (type
== error_mark_node
)
8150 if (TYPE_READONLY (type
))
8151 obstack_1grow (&util_obstack
, 'r');
8153 if (code
== INTEGER_TYPE
)
8155 switch (GET_MODE_BITSIZE (TYPE_MODE (type
)))
8157 case 8: c
= TYPE_UNSIGNED (type
) ? 'C' : 'c'; break;
8158 case 16: c
= TYPE_UNSIGNED (type
) ? 'S' : 's'; break;
8160 if (type
== long_unsigned_type_node
8161 || type
== long_integer_type_node
)
8162 c
= TYPE_UNSIGNED (type
) ? 'L' : 'l';
8164 c
= TYPE_UNSIGNED (type
) ? 'I' : 'i';
8166 case 64: c
= TYPE_UNSIGNED (type
) ? 'Q' : 'q'; break;
8169 obstack_1grow (&util_obstack
, c
);
8172 else if (code
== REAL_TYPE
)
8174 /* Floating point types. */
8175 switch (GET_MODE_BITSIZE (TYPE_MODE (type
)))
8177 case 32: c
= 'f'; break;
8180 case 128: c
= 'd'; break;
8183 obstack_1grow (&util_obstack
, c
);
8186 else if (code
== VOID_TYPE
)
8187 obstack_1grow (&util_obstack
, 'v');
8189 else if (code
== BOOLEAN_TYPE
)
8190 obstack_1grow (&util_obstack
, 'B');
8192 else if (code
== ARRAY_TYPE
)
8193 encode_array (type
, curtype
, format
);
8195 else if (code
== POINTER_TYPE
)
8196 encode_pointer (type
, curtype
, format
);
8198 else if (code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
8199 encode_aggregate (type
, curtype
, format
);
8201 else if (code
== FUNCTION_TYPE
) /* '?' */
8202 obstack_1grow (&util_obstack
, '?');
8204 else if (code
== COMPLEX_TYPE
)
8206 obstack_1grow (&util_obstack
, 'j');
8207 encode_type (TREE_TYPE (type
), curtype
, format
);
8212 encode_gnu_bitfield (int position
, tree type
, int size
)
8214 enum tree_code code
= TREE_CODE (type
);
8216 char charType
= '?';
8218 if (code
== INTEGER_TYPE
)
8220 if (integer_zerop (TYPE_MIN_VALUE (type
)))
8222 /* Unsigned integer types. */
8224 if (TYPE_MODE (type
) == QImode
)
8226 else if (TYPE_MODE (type
) == HImode
)
8228 else if (TYPE_MODE (type
) == SImode
)
8230 if (type
== long_unsigned_type_node
)
8235 else if (TYPE_MODE (type
) == DImode
)
8240 /* Signed integer types. */
8242 if (TYPE_MODE (type
) == QImode
)
8244 else if (TYPE_MODE (type
) == HImode
)
8246 else if (TYPE_MODE (type
) == SImode
)
8248 if (type
== long_integer_type_node
)
8254 else if (TYPE_MODE (type
) == DImode
)
8258 else if (code
== ENUMERAL_TYPE
)
8263 sprintf (buffer
, "b%d%c%d", position
, charType
, size
);
8264 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8268 encode_field_decl (tree field_decl
, int curtype
, int format
)
8271 /* C++ static members, and things that are not fields at all,
8272 should not appear in the encoding. */
8273 if (TREE_CODE (field_decl
) != FIELD_DECL
|| TREE_STATIC (field_decl
))
8277 /* Generate the bitfield typing information, if needed. Note the difference
8278 between GNU and NeXT runtimes. */
8279 if (DECL_BIT_FIELD_TYPE (field_decl
))
8281 int size
= tree_low_cst (DECL_SIZE (field_decl
), 1);
8283 if (flag_next_runtime
)
8284 encode_next_bitfield (size
);
8286 encode_gnu_bitfield (int_bit_position (field_decl
),
8287 DECL_BIT_FIELD_TYPE (field_decl
), size
);
8290 encode_type (TREE_TYPE (field_decl
), curtype
, format
);
8293 static GTY(()) tree objc_parmlist
= NULL_TREE
;
8295 /* Append PARM to a list of formal parameters of a method, making a necessary
8296 array-to-pointer adjustment along the way. */
8299 objc_push_parm (tree parm
)
8301 bool relayout_needed
= false;
8303 if (TREE_TYPE (parm
) == error_mark_node
)
8305 objc_parmlist
= chainon (objc_parmlist
, parm
);
8309 /* Decay arrays and functions into pointers. */
8310 if (TREE_CODE (TREE_TYPE (parm
)) == ARRAY_TYPE
)
8312 TREE_TYPE (parm
) = build_pointer_type (TREE_TYPE (TREE_TYPE (parm
)));
8313 relayout_needed
= true;
8315 else if (TREE_CODE (TREE_TYPE (parm
)) == FUNCTION_TYPE
)
8317 TREE_TYPE (parm
) = build_pointer_type (TREE_TYPE (parm
));
8318 relayout_needed
= true;
8321 if (relayout_needed
)
8322 relayout_decl (parm
);
8325 DECL_ARG_TYPE (parm
)
8326 = lang_hooks
.types
.type_promotes_to (TREE_TYPE (parm
));
8328 /* Record constancy and volatility. */
8329 c_apply_type_quals_to_decl
8330 ((TYPE_READONLY (TREE_TYPE (parm
)) ? TYPE_QUAL_CONST
: 0)
8331 | (TYPE_RESTRICT (TREE_TYPE (parm
)) ? TYPE_QUAL_RESTRICT
: 0)
8332 | (TYPE_VOLATILE (TREE_TYPE (parm
)) ? TYPE_QUAL_VOLATILE
: 0), parm
);
8334 objc_parmlist
= chainon (objc_parmlist
, parm
);
8337 /* Retrieve the formal parameter list constructed via preceding calls to
8338 objc_push_parm(). */
8342 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED
)
8344 static struct c_arg_info
*
8345 objc_get_parm_info (int have_ellipsis
)
8349 tree parm_info
= objc_parmlist
;
8350 objc_parmlist
= NULL_TREE
;
8354 tree parm_info
= objc_parmlist
;
8355 struct c_arg_info
*arg_info
;
8356 /* The C front-end requires an elaborate song and dance at
8359 declare_parm_level ();
8362 tree next
= DECL_CHAIN (parm_info
);
8364 DECL_CHAIN (parm_info
) = NULL_TREE
;
8365 parm_info
= pushdecl (parm_info
);
8366 finish_decl (parm_info
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
8369 arg_info
= get_parm_info (have_ellipsis
);
8371 objc_parmlist
= NULL_TREE
;
8376 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8377 method definitions. In the case of instance methods, we can be more
8378 specific as to the type of 'self'. */
8381 synth_self_and_ucmd_args (void)
8385 if (objc_method_context
8386 && TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
)
8387 self_type
= objc_instance_type
;
8389 /* Really a `struct objc_class *'. However, we allow people to
8390 assign to self, which changes its type midstream. */
8391 self_type
= objc_object_type
;
8394 objc_push_parm (build_decl (input_location
,
8395 PARM_DECL
, self_id
, self_type
));
8398 objc_push_parm (build_decl (input_location
,
8399 PARM_DECL
, ucmd_id
, objc_selector_type
));
8402 /* Transform an Objective-C method definition into a static C function
8403 definition, synthesizing the first two arguments, "self" and "_cmd",
8407 start_method_def (tree method
)
8413 struct c_arg_info
*parm_info
;
8415 int have_ellipsis
= 0;
8417 /* If we are defining a "dealloc" method in a non-root class, we
8418 will need to check if a [super dealloc] is missing, and warn if
8420 if(CLASS_SUPER_NAME (objc_implementation_context
)
8421 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method
))))
8422 should_call_super_dealloc
= 1;
8424 should_call_super_dealloc
= 0;
8426 /* Required to implement _msgSuper. */
8427 objc_method_context
= method
;
8428 UOBJC_SUPER_decl
= NULL_TREE
;
8430 /* Generate prototype declarations for arguments..."new-style". */
8431 synth_self_and_ucmd_args ();
8433 /* Generate argument declarations if a keyword_decl. */
8434 parmlist
= METHOD_SEL_ARGS (method
);
8437 tree type
= TREE_VALUE (TREE_TYPE (parmlist
)), parm
;
8439 parm
= build_decl (input_location
,
8440 PARM_DECL
, KEYWORD_ARG_NAME (parmlist
), type
);
8441 objc_push_parm (parm
);
8442 parmlist
= DECL_CHAIN (parmlist
);
8445 if (METHOD_ADD_ARGS (method
))
8449 for (akey
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
8450 akey
; akey
= TREE_CHAIN (akey
))
8452 objc_push_parm (TREE_VALUE (akey
));
8455 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
8459 parm_info
= objc_get_parm_info (have_ellipsis
);
8461 really_start_method (objc_method_context
, parm_info
);
8464 /* Return 1 if TYPE1 is equivalent to TYPE2
8465 for purposes of method overloading. */
8468 objc_types_are_equivalent (tree type1
, tree type2
)
8473 /* Strip away indirections. */
8474 while ((TREE_CODE (type1
) == ARRAY_TYPE
|| TREE_CODE (type1
) == POINTER_TYPE
)
8475 && (TREE_CODE (type1
) == TREE_CODE (type2
)))
8476 type1
= TREE_TYPE (type1
), type2
= TREE_TYPE (type2
);
8477 if (TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
8480 type1
= (TYPE_HAS_OBJC_INFO (type1
)
8481 ? TYPE_OBJC_PROTOCOL_LIST (type1
)
8483 type2
= (TYPE_HAS_OBJC_INFO (type2
)
8484 ? TYPE_OBJC_PROTOCOL_LIST (type2
)
8487 if (list_length (type1
) == list_length (type2
))
8489 for (; type2
; type2
= TREE_CHAIN (type2
))
8490 if (!lookup_protocol_in_reflist (type1
, TREE_VALUE (type2
)))
8497 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8500 objc_types_share_size_and_alignment (tree type1
, tree type2
)
8502 return (simple_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
8503 && TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
));
8506 /* Return 1 if PROTO1 is equivalent to PROTO2
8507 for purposes of method overloading. Ordinarily, the type signatures
8508 should match up exactly, unless STRICT is zero, in which case we
8509 shall allow differences in which the size and alignment of a type
8513 comp_proto_with_proto (tree proto1
, tree proto2
, int strict
)
8517 /* The following test is needed in case there are hashing
8519 if (METHOD_SEL_NAME (proto1
) != METHOD_SEL_NAME (proto2
))
8522 /* Compare return types. */
8523 type1
= TREE_VALUE (TREE_TYPE (proto1
));
8524 type2
= TREE_VALUE (TREE_TYPE (proto2
));
8526 if (!objc_types_are_equivalent (type1
, type2
)
8527 && (strict
|| !objc_types_share_size_and_alignment (type1
, type2
)))
8530 /* Compare argument types. */
8531 for (type1
= get_arg_type_list (proto1
, METHOD_REF
, 0),
8532 type2
= get_arg_type_list (proto2
, METHOD_REF
, 0);
8534 type1
= TREE_CHAIN (type1
), type2
= TREE_CHAIN (type2
))
8536 if (!objc_types_are_equivalent (TREE_VALUE (type1
), TREE_VALUE (type2
))
8538 || !objc_types_share_size_and_alignment (TREE_VALUE (type1
),
8539 TREE_VALUE (type2
))))
8543 return (!type1
&& !type2
);
8546 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
8547 this occurs. ObjC method dispatches are _not_ like C++ virtual
8548 member function dispatches, and we account for the difference here. */
8551 objc_fold_obj_type_ref (tree ref
, tree known_type
)
8553 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED
,
8554 tree known_type ATTRIBUTE_UNUSED
)
8558 tree v
= BINFO_VIRTUALS (TYPE_BINFO (known_type
));
8560 /* If the receiver does not have virtual member functions, there
8561 is nothing we can (or need to) do here. */
8565 /* Let C++ handle C++ virtual functions. */
8566 return cp_fold_obj_type_ref (ref
, known_type
);
8568 /* For plain ObjC, we currently do not need to do anything. */
8574 objc_start_function (tree name
, tree type
, tree attrs
,
8578 struct c_arg_info
*params
8582 tree fndecl
= build_decl (input_location
,
8583 FUNCTION_DECL
, name
, type
);
8586 DECL_ARGUMENTS (fndecl
) = params
;
8587 DECL_INITIAL (fndecl
) = error_mark_node
;
8588 DECL_EXTERNAL (fndecl
) = 0;
8589 TREE_STATIC (fndecl
) = 1;
8590 retrofit_lang_decl (fndecl
);
8591 cplus_decl_attributes (&fndecl
, attrs
, 0);
8592 start_preparsed_function (fndecl
, attrs
, /*flags=*/SF_DEFAULT
);
8594 current_function_returns_value
= 0; /* Assume, until we see it does. */
8595 current_function_returns_null
= 0;
8597 decl_attributes (&fndecl
, attrs
, 0);
8598 announce_function (fndecl
);
8599 DECL_INITIAL (fndecl
) = error_mark_node
;
8600 DECL_EXTERNAL (fndecl
) = 0;
8601 TREE_STATIC (fndecl
) = 1;
8602 current_function_decl
= pushdecl (fndecl
);
8604 declare_parm_level ();
8605 DECL_RESULT (current_function_decl
)
8606 = build_decl (input_location
,
8607 RESULT_DECL
, NULL_TREE
,
8608 TREE_TYPE (TREE_TYPE (current_function_decl
)));
8609 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl
)) = 1;
8610 DECL_IGNORED_P (DECL_RESULT (current_function_decl
)) = 1;
8611 start_fname_decls ();
8612 store_parm_decls_from (params
);
8615 TREE_USED (current_function_decl
) = 1;
8618 /* - Generate an identifier for the function. the format is "_n_cls",
8619 where 1 <= n <= nMethods, and cls is the name the implementation we
8621 - Install the return type from the method declaration.
8622 - If we have a prototype, check for type consistency. */
8625 really_start_method (tree method
,
8629 struct c_arg_info
*parmlist
8633 tree ret_type
, meth_type
;
8635 const char *sel_name
, *class_name
, *cat_name
;
8638 /* Synth the storage class & assemble the return type. */
8639 ret_type
= TREE_VALUE (TREE_TYPE (method
));
8641 sel_name
= IDENTIFIER_POINTER (METHOD_SEL_NAME (method
));
8642 class_name
= IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context
));
8643 cat_name
= ((TREE_CODE (objc_implementation_context
)
8644 == CLASS_IMPLEMENTATION_TYPE
)
8646 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context
)));
8649 /* Make sure this is big enough for any plausible method label. */
8650 buf
= (char *) alloca (50 + strlen (sel_name
) + strlen (class_name
)
8651 + (cat_name
? strlen (cat_name
) : 0));
8653 OBJC_GEN_METHOD_LABEL (buf
, TREE_CODE (method
) == INSTANCE_METHOD_DECL
,
8654 class_name
, cat_name
, sel_name
, method_slot
);
8656 method_id
= get_identifier (buf
);
8659 /* Objective-C methods cannot be overloaded, so we don't need
8660 the type encoding appended. It looks bad anyway... */
8661 push_lang_context (lang_name_c
);
8665 = build_function_type (ret_type
,
8666 get_arg_type_list (method
, METHOD_DEF
, 0));
8667 objc_start_function (method_id
, meth_type
, NULL_TREE
, parmlist
);
8669 /* Set self_decl from the first argument. */
8670 self_decl
= DECL_ARGUMENTS (current_function_decl
);
8672 /* Suppress unused warnings. */
8673 TREE_USED (self_decl
) = 1;
8674 DECL_READ_P (self_decl
) = 1;
8675 TREE_USED (DECL_CHAIN (self_decl
)) = 1;
8676 DECL_READ_P (DECL_CHAIN (self_decl
)) = 1;
8678 pop_lang_context ();
8681 METHOD_DEFINITION (method
) = current_function_decl
;
8683 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8685 if (implementation_template
!= objc_implementation_context
)
8688 = lookup_method_static (implementation_template
,
8689 METHOD_SEL_NAME (method
),
8690 ((TREE_CODE (method
) == CLASS_METHOD_DECL
)
8691 | OBJC_LOOKUP_NO_SUPER
));
8695 if (!comp_proto_with_proto (method
, proto
, 1))
8697 bool type
= TREE_CODE (method
) == INSTANCE_METHOD_DECL
;
8699 warning_at (DECL_SOURCE_LOCATION (method
), 0,
8700 "conflicting types for %<%c%s%>",
8702 identifier_to_locale (gen_method_decl (method
)));
8703 inform (DECL_SOURCE_LOCATION (proto
),
8704 "previous declaration of %<%c%s%>",
8706 identifier_to_locale (gen_method_decl (proto
)));
8711 /* We have a method @implementation even though we did not
8712 see a corresponding @interface declaration (which is allowed
8713 by Objective-C rules). Go ahead and place the method in
8714 the @interface anyway, so that message dispatch lookups
8716 tree interface
= implementation_template
;
8718 if (TREE_CODE (objc_implementation_context
)
8719 == CATEGORY_IMPLEMENTATION_TYPE
)
8720 interface
= lookup_category
8722 CLASS_SUPER_NAME (objc_implementation_context
));
8725 objc_add_method (interface
, copy_node (method
),
8726 TREE_CODE (method
) == CLASS_METHOD_DECL
);
8731 static void *UOBJC_SUPER_scope
= 0;
8733 /* _n_Method (id self, SEL sel, ...)
8735 struct objc_super _S;
8736 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8740 get_super_receiver (void)
8742 if (objc_method_context
)
8744 tree super_expr
, super_expr_list
;
8746 if (!UOBJC_SUPER_decl
)
8748 UOBJC_SUPER_decl
= build_decl (input_location
,
8749 VAR_DECL
, get_identifier (TAG_SUPER
),
8750 objc_super_template
);
8751 /* This prevents `unused variable' warnings when compiling with -Wall. */
8752 TREE_USED (UOBJC_SUPER_decl
) = 1;
8753 DECL_READ_P (UOBJC_SUPER_decl
) = 1;
8754 lang_hooks
.decls
.pushdecl (UOBJC_SUPER_decl
);
8755 finish_decl (UOBJC_SUPER_decl
, input_location
, NULL_TREE
, NULL_TREE
,
8757 UOBJC_SUPER_scope
= objc_get_current_scope ();
8760 /* Set receiver to self. */
8761 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
, self_id
);
8762 super_expr
= build_modify_expr (input_location
, super_expr
, NULL_TREE
,
8763 NOP_EXPR
, input_location
, self_decl
,
8765 super_expr_list
= super_expr
;
8767 /* Set class to begin searching. */
8768 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
,
8769 get_identifier ("super_class"));
8771 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
8773 /* [_cls, __cls]Super are "pre-built" in
8774 synth_forward_declarations. */
8776 super_expr
= build_modify_expr (input_location
, super_expr
,
8777 NULL_TREE
, NOP_EXPR
,
8779 ((TREE_CODE (objc_method_context
)
8780 == INSTANCE_METHOD_DECL
)
8787 /* We have a category. */
8789 tree super_name
= CLASS_SUPER_NAME (implementation_template
);
8792 /* Barf if super used in a category of Object. */
8795 error ("no super class declared in interface for %qE",
8796 CLASS_NAME (implementation_template
));
8797 return error_mark_node
;
8800 if (flag_next_runtime
&& !flag_zero_link
)
8802 super_class
= objc_get_class_reference (super_name
);
8803 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
)
8804 /* If we are in a class method, we must retrieve the
8805 _metaclass_ for the current class, pointed at by
8806 the class's "isa" pointer. The following assumes that
8807 "isa" is the first ivar in a class (which it must be). */
8809 = build_indirect_ref
8811 build_c_cast (input_location
,
8812 build_pointer_type (objc_class_type
),
8813 super_class
), RO_UNARY_STAR
);
8817 add_class_reference (super_name
);
8818 super_class
= (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
8819 ? objc_get_class_decl
: objc_get_meta_class_decl
);
8820 assemble_external (super_class
);
8822 = build_function_call
8827 my_build_string_pointer
8828 (IDENTIFIER_LENGTH (super_name
) + 1,
8829 IDENTIFIER_POINTER (super_name
))));
8833 = build_modify_expr (input_location
, super_expr
, NULL_TREE
,
8836 build_c_cast (input_location
,
8837 TREE_TYPE (super_expr
),
8842 super_expr_list
= build_compound_expr (input_location
,
8843 super_expr_list
, super_expr
);
8845 super_expr
= build_unary_op (input_location
,
8846 ADDR_EXPR
, UOBJC_SUPER_decl
, 0);
8847 super_expr_list
= build_compound_expr (input_location
,
8848 super_expr_list
, super_expr
);
8850 return super_expr_list
;
8854 error ("[super ...] must appear in a method context");
8855 return error_mark_node
;
8859 /* When exiting a scope, sever links to a 'super' declaration (if any)
8860 therein contained. */
8863 objc_clear_super_receiver (void)
8865 if (objc_method_context
8866 && UOBJC_SUPER_scope
== objc_get_current_scope ()) {
8867 UOBJC_SUPER_decl
= 0;
8868 UOBJC_SUPER_scope
= 0;
8873 objc_finish_method_definition (tree fndecl
)
8875 /* We cannot validly inline ObjC methods, at least not without a language
8876 extension to declare that a method need not be dynamically
8877 dispatched, so suppress all thoughts of doing so. */
8878 DECL_UNINLINABLE (fndecl
) = 1;
8881 /* The C++ front-end will have called finish_function() for us. */
8885 METHOD_ENCODING (objc_method_context
)
8886 = encode_method_prototype (objc_method_context
);
8888 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8889 since the optimizer may find "may be used before set" errors. */
8890 objc_method_context
= NULL_TREE
;
8892 if (should_call_super_dealloc
)
8893 warning (0, "method possibly missing a [super dealloc] call");
8896 /* Given a tree DECL node, produce a printable description of it in the given
8897 buffer, overwriting the buffer. */
8900 gen_declaration (tree decl
)
8906 gen_type_name_0 (TREE_TYPE (decl
));
8908 if (DECL_NAME (decl
))
8910 if (!POINTER_TYPE_P (TREE_TYPE (decl
)))
8911 strcat (errbuf
, " ");
8913 strcat (errbuf
, IDENTIFIER_POINTER (DECL_NAME (decl
)));
8916 if (DECL_INITIAL (decl
)
8917 && TREE_CODE (DECL_INITIAL (decl
)) == INTEGER_CST
)
8918 sprintf (errbuf
+ strlen (errbuf
), ": " HOST_WIDE_INT_PRINT_DEC
,
8919 TREE_INT_CST_LOW (DECL_INITIAL (decl
)));
8925 /* Given a tree TYPE node, produce a printable description of it in the given
8926 buffer, overwriting the buffer. */
8929 gen_type_name_0 (tree type
)
8931 tree orig
= type
, proto
;
8933 if (TYPE_P (type
) && TYPE_NAME (type
))
8934 type
= TYPE_NAME (type
);
8935 else if (POINTER_TYPE_P (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
8937 tree inner
= TREE_TYPE (type
);
8939 while (TREE_CODE (inner
) == ARRAY_TYPE
)
8940 inner
= TREE_TYPE (inner
);
8942 gen_type_name_0 (inner
);
8944 if (!POINTER_TYPE_P (inner
))
8945 strcat (errbuf
, " ");
8947 if (POINTER_TYPE_P (type
))
8948 strcat (errbuf
, "*");
8950 while (type
!= inner
)
8952 strcat (errbuf
, "[");
8954 if (TYPE_DOMAIN (type
))
8958 sprintf (sz
, HOST_WIDE_INT_PRINT_DEC
,
8960 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1));
8961 strcat (errbuf
, sz
);
8964 strcat (errbuf
, "]");
8965 type
= TREE_TYPE (type
);
8971 if (TREE_CODE (type
) == TYPE_DECL
&& DECL_NAME (type
))
8972 type
= DECL_NAME (type
);
8974 strcat (errbuf
, TREE_CODE (type
) == IDENTIFIER_NODE
8975 ? IDENTIFIER_POINTER (type
)
8978 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8979 if (objc_is_id (orig
))
8980 orig
= TREE_TYPE (orig
);
8982 proto
= TYPE_HAS_OBJC_INFO (orig
) ? TYPE_OBJC_PROTOCOL_LIST (orig
) : NULL_TREE
;
8986 strcat (errbuf
, " <");
8990 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto
))));
8991 proto
= TREE_CHAIN (proto
);
8992 strcat (errbuf
, proto
? ", " : ">");
9001 gen_type_name (tree type
)
9005 return gen_type_name_0 (type
);
9008 /* Given a method tree, put a printable description into the given
9009 buffer (overwriting) and return a pointer to the buffer. */
9012 gen_method_decl (tree method
)
9016 strcpy (errbuf
, "("); /* NB: Do _not_ call strcat() here. */
9017 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method
)));
9018 strcat (errbuf
, ")");
9019 chain
= METHOD_SEL_ARGS (method
);
9023 /* We have a chain of keyword_decls. */
9026 if (KEYWORD_KEY_NAME (chain
))
9027 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain
)));
9029 strcat (errbuf
, ":(");
9030 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain
)));
9031 strcat (errbuf
, ")");
9033 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain
)));
9034 if ((chain
= DECL_CHAIN (chain
)))
9035 strcat (errbuf
, " ");
9039 if (METHOD_ADD_ARGS (method
))
9041 chain
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
9043 /* Know we have a chain of parm_decls. */
9046 strcat (errbuf
, ", ");
9047 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain
)));
9048 chain
= TREE_CHAIN (chain
);
9051 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
9052 strcat (errbuf
, ", ...");
9057 /* We have a unary selector. */
9058 strcat (errbuf
, IDENTIFIER_POINTER (METHOD_SEL_NAME (method
)));
9066 /* Dump an @interface declaration of the supplied class CHAIN to the
9067 supplied file FP. Used to implement the -gen-decls option (which
9068 prints out an @interface declaration of all classes compiled in
9069 this run); potentially useful for debugging the compiler too. */
9071 dump_interface (FILE *fp
, tree chain
)
9073 /* FIXME: A heap overflow here whenever a method (or ivar)
9074 declaration is so long that it doesn't fit in the buffer. The
9075 code and all the related functions should be rewritten to avoid
9076 using fixed size buffers. */
9077 const char *my_name
= IDENTIFIER_POINTER (CLASS_NAME (chain
));
9078 tree ivar_decls
= CLASS_RAW_IVARS (chain
);
9079 tree nst_methods
= CLASS_NST_METHODS (chain
);
9080 tree cls_methods
= CLASS_CLS_METHODS (chain
);
9082 fprintf (fp
, "\n@interface %s", my_name
);
9084 /* CLASS_SUPER_NAME is used to store the superclass name for
9085 classes, and the category name for categories. */
9086 if (CLASS_SUPER_NAME (chain
))
9088 const char *name
= IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain
));
9090 if (TREE_CODE (chain
) == CATEGORY_IMPLEMENTATION_TYPE
9091 || TREE_CODE (chain
) == CATEGORY_INTERFACE_TYPE
)
9093 fprintf (fp
, " (%s)\n", name
);
9097 fprintf (fp
, " : %s\n", name
);
9103 /* FIXME - the following doesn't seem to work at the moment. */
9106 fprintf (fp
, "{\n");
9109 fprintf (fp
, "\t%s;\n", gen_declaration (ivar_decls
));
9110 ivar_decls
= TREE_CHAIN (ivar_decls
);
9113 fprintf (fp
, "}\n");
9118 fprintf (fp
, "- %s;\n", gen_method_decl (nst_methods
));
9119 nst_methods
= TREE_CHAIN (nst_methods
);
9124 fprintf (fp
, "+ %s;\n", gen_method_decl (cls_methods
));
9125 cls_methods
= TREE_CHAIN (cls_methods
);
9128 fprintf (fp
, "@end\n");
9131 /* Demangle function for Objective-C */
9133 objc_demangle (const char *mangled
)
9135 char *demangled
, *cp
;
9137 if (mangled
[0] == '_' &&
9138 (mangled
[1] == 'i' || mangled
[1] == 'c') &&
9141 cp
= demangled
= XNEWVEC (char, strlen(mangled
) + 2);
9142 if (mangled
[1] == 'i')
9143 *cp
++ = '-'; /* for instance method */
9145 *cp
++ = '+'; /* for class method */
9146 *cp
++ = '['; /* opening left brace */
9147 strcpy(cp
, mangled
+3); /* tack on the rest of the mangled name */
9148 while (*cp
&& *cp
== '_')
9149 cp
++; /* skip any initial underbars in class name */
9150 cp
= strchr(cp
, '_'); /* find first non-initial underbar */
9153 free(demangled
); /* not mangled name */
9156 if (cp
[1] == '_') /* easy case: no category name */
9158 *cp
++ = ' '; /* replace two '_' with one ' ' */
9159 strcpy(cp
, mangled
+ (cp
- demangled
) + 2);
9163 *cp
++ = '('; /* less easy case: category name */
9164 cp
= strchr(cp
, '_');
9167 free(demangled
); /* not mangled name */
9171 *cp
++ = ' '; /* overwriting 1st char of method name... */
9172 strcpy(cp
, mangled
+ (cp
- demangled
)); /* get it back */
9174 while (*cp
&& *cp
== '_')
9175 cp
++; /* skip any initial underbars in method name */
9178 *cp
= ':'; /* replace remaining '_' with ':' */
9179 *cp
++ = ']'; /* closing right brace */
9180 *cp
++ = 0; /* string terminator */
9184 return mangled
; /* not an objc mangled name */
9188 objc_printable_name (tree decl
, int kind ATTRIBUTE_UNUSED
)
9190 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl
)));
9196 gcc_obstack_init (&util_obstack
);
9197 util_firstobj
= (char *) obstack_finish (&util_obstack
);
9199 errbuf
= XNEWVEC (char, 1024 * 10);
9201 synth_module_prologue ();
9207 struct imp_entry
*impent
;
9209 /* The internally generated initializers appear to have missing braces.
9210 Don't warn about this. */
9211 int save_warn_missing_braces
= warn_missing_braces
;
9212 warn_missing_braces
= 0;
9214 /* A missing @end may not be detected by the parser. */
9215 if (objc_implementation_context
)
9217 warning (0, "%<@end%> missing in implementation context");
9218 finish_class (objc_implementation_context
);
9219 objc_ivar_chain
= NULL_TREE
;
9220 objc_implementation_context
= NULL_TREE
;
9223 /* Process the static instances here because initialization of objc_symtab
9225 if (objc_static_instances
)
9226 generate_static_references ();
9228 /* forward declare categories */
9230 forward_declare_categories ();
9232 for (impent
= imp_list
; impent
; impent
= impent
->next
)
9234 objc_implementation_context
= impent
->imp_context
;
9235 implementation_template
= impent
->imp_template
;
9237 /* FIXME: This needs reworking to be more obvious. */
9239 UOBJC_CLASS_decl
= impent
->class_decl
;
9240 UOBJC_METACLASS_decl
= impent
->meta_decl
;
9242 /* Dump the @interface of each class as we compile it, if the
9243 -gen-decls option is in use. TODO: Dump the classes in the
9244 order they were found, rather than in reverse order as we
9246 if (flag_gen_declaration
)
9248 dump_interface (gen_declaration_file
, objc_implementation_context
);
9251 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
9253 /* all of the following reference the string pool... */
9254 generate_ivar_lists ();
9255 generate_dispatch_tables ();
9256 generate_shared_structures (impent
);
9260 generate_dispatch_tables ();
9261 generate_category (impent
);
9264 impent
->class_decl
= UOBJC_CLASS_decl
;
9265 impent
->meta_decl
= UOBJC_METACLASS_decl
;
9268 /* If we are using an array of selectors, we must always
9269 finish up the array decl even if no selectors were used. */
9270 if (flag_next_runtime
)
9271 build_next_selector_translation_table ();
9273 build_gnu_selector_translation_table ();
9276 generate_protocols ();
9278 if (flag_next_runtime
)
9279 generate_objc_image_info ();
9281 if (imp_list
|| class_names_chain
9282 || meth_var_names_chain
|| meth_var_types_chain
|| sel_ref_chain
)
9283 generate_objc_symtab_decl ();
9285 /* Arrange for ObjC data structures to be initialized at run time. */
9286 if (objc_implementation_context
|| class_names_chain
|| objc_static_instances
9287 || meth_var_names_chain
|| meth_var_types_chain
|| sel_ref_chain
)
9289 build_module_descriptor ();
9291 if (!flag_next_runtime
)
9292 build_module_initializer_routine ();
9295 /* Dump the class references. This forces the appropriate classes
9296 to be linked into the executable image, preserving unix archive
9297 semantics. This can be removed when we move to a more dynamically
9298 linked environment. */
9300 for (chain
= cls_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
9302 handle_class_ref (chain
);
9303 if (TREE_PURPOSE (chain
))
9304 generate_classref_translation_entry (chain
);
9307 for (impent
= imp_list
; impent
; impent
= impent
->next
)
9308 handle_impent (impent
);
9315 /* Run through the selector hash tables and print a warning for any
9316 selector which has multiple methods. */
9318 for (slot
= 0; slot
< SIZEHASHTABLE
; slot
++)
9320 for (hsh
= cls_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
9321 check_duplicates (hsh
, 0, 1);
9322 for (hsh
= nst_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
9323 check_duplicates (hsh
, 0, 1);
9327 warn_missing_braces
= save_warn_missing_braces
;
9330 /* Subroutines of finish_objc. */
9333 generate_classref_translation_entry (tree chain
)
9335 tree expr
, decl
, type
;
9337 decl
= TREE_PURPOSE (chain
);
9338 type
= TREE_TYPE (decl
);
9340 expr
= add_objc_string (TREE_VALUE (chain
), class_names
);
9341 expr
= convert (type
, expr
); /* cast! */
9343 /* This is a class reference. It is re-written by the runtime,
9344 but will be optimized away unless we force it. */
9345 DECL_PRESERVE_P (decl
) = 1;
9346 finish_var_decl (decl
, expr
);
9351 handle_class_ref (tree chain
)
9353 const char *name
= IDENTIFIER_POINTER (TREE_VALUE (chain
));
9354 char *string
= (char *) alloca (strlen (name
) + 30);
9358 sprintf (string
, "%sobjc_class_name_%s",
9359 (flag_next_runtime
? "." : "__"), name
);
9361 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
9362 if (flag_next_runtime
)
9364 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file
, string
);
9369 /* Make a decl for this name, so we can use its address in a tree. */
9370 decl
= build_decl (input_location
,
9371 VAR_DECL
, get_identifier (string
), TREE_TYPE (integer_zero_node
));
9372 DECL_EXTERNAL (decl
) = 1;
9373 TREE_PUBLIC (decl
) = 1;
9375 finish_var_decl (decl
, 0);
9377 /* Make a decl for the address. */
9378 sprintf (string
, "%sobjc_class_ref_%s",
9379 (flag_next_runtime
? "." : "__"), name
);
9380 exp
= build1 (ADDR_EXPR
, string_type_node
, decl
);
9381 decl
= build_decl (input_location
,
9382 VAR_DECL
, get_identifier (string
), string_type_node
);
9383 TREE_STATIC (decl
) = 1;
9384 TREE_USED (decl
) = 1;
9385 DECL_READ_P (decl
) = 1;
9386 DECL_ARTIFICIAL (decl
) = 1;
9387 DECL_INITIAL (decl
) = error_mark_node
;
9389 /* We must force the reference. */
9390 DECL_PRESERVE_P (decl
) = 1;
9393 finish_var_decl (decl
, exp
);
9397 handle_impent (struct imp_entry
*impent
)
9401 objc_implementation_context
= impent
->imp_context
;
9402 implementation_template
= impent
->imp_template
;
9404 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
9406 const char *const class_name
=
9407 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
));
9409 string
= (char *) alloca (strlen (class_name
) + 30);
9411 sprintf (string
, "%sobjc_class_name_%s",
9412 (flag_next_runtime
? "." : "__"), class_name
);
9414 else if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
9416 const char *const class_name
=
9417 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
));
9418 const char *const class_super_name
=
9419 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent
->imp_context
));
9421 string
= (char *) alloca (strlen (class_name
)
9422 + strlen (class_super_name
) + 30);
9424 /* Do the same for categories. Even though no references to
9425 these symbols are generated automatically by the compiler, it
9426 gives you a handle to pull them into an archive by hand. */
9427 sprintf (string
, "*%sobjc_category_name_%s_%s",
9428 (flag_next_runtime
? "." : "__"), class_name
, class_super_name
);
9433 #ifdef ASM_DECLARE_CLASS_REFERENCE
9434 if (flag_next_runtime
)
9436 ASM_DECLARE_CLASS_REFERENCE (asm_out_file
, string
);
9444 init
= integer_zero_node
;
9445 decl
= build_decl (input_location
,
9446 VAR_DECL
, get_identifier (string
), TREE_TYPE (init
));
9447 TREE_PUBLIC (decl
) = 1;
9448 TREE_READONLY (decl
) = 1;
9449 TREE_USED (decl
) = 1;
9450 TREE_CONSTANT (decl
) = 1;
9451 DECL_CONTEXT (decl
) = NULL_TREE
;
9452 DECL_ARTIFICIAL (decl
) = 1;
9453 TREE_STATIC (decl
) = 1;
9454 DECL_INITIAL (decl
) = error_mark_node
; /* A real initializer is coming... */
9455 /* We must force the reference. */
9456 DECL_PRESERVE_P (decl
) = 1;
9458 finish_var_decl(decl
, init
) ;
9462 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
9463 later requires that ObjC translation units participating in F&C be
9464 specially marked. The following routine accomplishes this. */
9466 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
9469 generate_objc_image_info (void)
9473 = ((flag_replace_objc_classes
&& imp_count
? 1 : 0)
9474 | (flag_objc_gc
? 2 : 0));
9475 VEC(constructor_elt
,gc
) *v
= NULL
;
9479 return; /* No need for an image_info entry. */
9481 array_type
= build_sized_array_type (integer_type_node
, 2);
9483 decl
= start_var_decl (array_type
, "_OBJC_IMAGE_INFO");
9485 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
9486 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (integer_type_node
, flags
));
9487 /* If we need this (determined above) it is because the runtime wants to
9488 refer to it in a manner hidden from the compiler. So we must force the
9490 DECL_PRESERVE_P (decl
) = 1;
9491 finish_var_decl (decl
, objc_build_constructor (TREE_TYPE (decl
), v
));
9494 /* Look up ID as an instance variable. OTHER contains the result of
9495 the C or C++ lookup, which we may want to use instead. */
9498 objc_lookup_ivar (tree other
, tree id
)
9502 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9503 if (!objc_method_context
)
9506 if (!strcmp (IDENTIFIER_POINTER (id
), "super"))
9507 /* We have a message to super. */
9508 return get_super_receiver ();
9510 /* In a class method, look up an instance variable only as a last
9512 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
9513 && other
&& other
!= error_mark_node
)
9516 /* Look up the ivar, but do not use it if it is not accessible. */
9517 ivar
= is_ivar (objc_ivar_chain
, id
);
9519 if (!ivar
|| is_private (ivar
))
9522 /* In an instance method, a local variable (or parameter) may hide the
9523 instance variable. */
9524 if (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
9525 && other
&& other
!= error_mark_node
9527 && CP_DECL_CONTEXT (other
) != global_namespace
)
9529 && !DECL_FILE_SCOPE_P (other
))
9532 warning (0, "local declaration of %qE hides instance variable",
9538 /* At this point, we are either in an instance method with no obscuring
9539 local definitions, or in a class method with no alternate definitions
9541 return build_ivar_reference (id
);
9544 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9545 needs to be done if we are calling a function through a cast. */
9548 objc_rewrite_function_call (tree function
, tree first_param
)
9550 if (TREE_CODE (function
) == NOP_EXPR
9551 && TREE_CODE (TREE_OPERAND (function
, 0)) == ADDR_EXPR
9552 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function
, 0), 0))
9555 function
= build3 (OBJ_TYPE_REF
, TREE_TYPE (function
),
9556 TREE_OPERAND (function
, 0),
9557 first_param
, size_zero_node
);
9563 /* Look for the special case of OBJC_TYPE_REF with the address of
9564 a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend or one
9568 objc_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
9570 enum gimplify_status r0
, r1
;
9571 if (TREE_CODE (*expr_p
) == OBJ_TYPE_REF
9572 && TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p
)) == ADDR_EXPR
9573 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p
), 0))
9576 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9577 value of the OBJ_TYPE_REF, so force them to be emitted
9578 during subexpression evaluation rather than after the
9579 OBJ_TYPE_REF. This permits objc_msgSend calls in Objective
9580 C to use direct rather than indirect calls when the
9581 object expression has a postincrement. */
9582 r0
= gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p
), pre_p
, NULL
,
9583 is_gimple_val
, fb_rvalue
);
9584 r1
= gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p
), pre_p
, post_p
,
9585 is_gimple_val
, fb_rvalue
);
9587 return MIN (r0
, r1
);
9591 return (enum gimplify_status
) cp_gimplify_expr (expr_p
, pre_p
, post_p
);
9593 return (enum gimplify_status
) c_gimplify_expr (expr_p
, pre_p
, post_p
);
9597 #include "gt-objc-objc-act.h"