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 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
1104 Both TYPE1 and TYPE2 must be pointers, and already determined to be
1105 compatible by objc_compare_types() below. */
1108 objc_common_type (tree type1
, tree type2
)
1110 tree inner1
= TREE_TYPE (type1
), inner2
= TREE_TYPE (type2
);
1112 while (POINTER_TYPE_P (inner1
))
1114 inner1
= TREE_TYPE (inner1
);
1115 inner2
= TREE_TYPE (inner2
);
1118 /* If one type is derived from another, return the base type. */
1119 if (DERIVED_FROM_P (inner1
, inner2
))
1121 else if (DERIVED_FROM_P (inner2
, inner1
))
1124 /* If both types are 'Class', return 'Class'. */
1125 if (objc_is_class_id (inner1
) && objc_is_class_id (inner2
))
1126 return objc_class_type
;
1128 /* Otherwise, return 'id'. */
1129 return objc_object_type
;
1132 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1133 an instance of RTYP to an instance of LTYP or to compare the two
1134 (if ARGNO is equal to -3), per ObjC type system rules. Before
1135 returning 'true', this routine may issue warnings related to, e.g.,
1136 protocol conformance. When returning 'false', the routine must
1137 produce absolutely no warnings; the C or C++ front-end will do so
1138 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1139 the routine must return 'false'.
1141 The ARGNO parameter is encoded as follows:
1142 >= 1 Parameter number (CALLEE contains function being called);
1146 -3 Comparison (LTYP and RTYP may match in either direction);
1147 -4 Silent comparison (for C++ overload resolution).
1151 objc_compare_types (tree ltyp
, tree rtyp
, int argno
, tree callee
)
1153 tree lcls
, rcls
, lproto
, rproto
;
1154 bool pointers_compatible
;
1156 /* We must be dealing with pointer types */
1157 if (!POINTER_TYPE_P (ltyp
) || !POINTER_TYPE_P (rtyp
))
1162 ltyp
= TREE_TYPE (ltyp
); /* Remove indirections. */
1163 rtyp
= TREE_TYPE (rtyp
);
1165 while (POINTER_TYPE_P (ltyp
) && POINTER_TYPE_P (rtyp
));
1167 /* We must also handle function pointers, since ObjC is a bit more
1168 lenient than C or C++ on this. */
1169 if (TREE_CODE (ltyp
) == FUNCTION_TYPE
&& TREE_CODE (rtyp
) == FUNCTION_TYPE
)
1171 /* Return types must be covariant. */
1172 if (!comptypes (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
))
1173 && !objc_compare_types (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
),
1177 /* Argument types must be contravariant. */
1178 for (ltyp
= TYPE_ARG_TYPES (ltyp
), rtyp
= TYPE_ARG_TYPES (rtyp
);
1179 ltyp
&& rtyp
; ltyp
= TREE_CHAIN (ltyp
), rtyp
= TREE_CHAIN (rtyp
))
1181 if (!comptypes (TREE_VALUE (rtyp
), TREE_VALUE (ltyp
))
1182 && !objc_compare_types (TREE_VALUE (rtyp
), TREE_VALUE (ltyp
),
1187 return (ltyp
== rtyp
);
1190 /* Past this point, we are only interested in ObjC class instances,
1191 or 'id' or 'Class'. */
1192 if (TREE_CODE (ltyp
) != RECORD_TYPE
|| TREE_CODE (rtyp
) != RECORD_TYPE
)
1195 if (!objc_is_object_id (ltyp
) && !objc_is_class_id (ltyp
)
1196 && !TYPE_HAS_OBJC_INFO (ltyp
))
1199 if (!objc_is_object_id (rtyp
) && !objc_is_class_id (rtyp
)
1200 && !TYPE_HAS_OBJC_INFO (rtyp
))
1203 /* Past this point, we are committed to returning 'true' to the caller
1204 (unless performing a silent comparison; see below). However, we can
1205 still warn about type and/or protocol mismatches. */
1207 if (TYPE_HAS_OBJC_INFO (ltyp
))
1209 lcls
= TYPE_OBJC_INTERFACE (ltyp
);
1210 lproto
= TYPE_OBJC_PROTOCOL_LIST (ltyp
);
1213 lcls
= lproto
= NULL_TREE
;
1215 if (TYPE_HAS_OBJC_INFO (rtyp
))
1217 rcls
= TYPE_OBJC_INTERFACE (rtyp
);
1218 rproto
= TYPE_OBJC_PROTOCOL_LIST (rtyp
);
1221 rcls
= rproto
= NULL_TREE
;
1223 /* If we could not find an @interface declaration, we must have
1224 only seen a @class declaration; for purposes of type comparison,
1225 treat it as a stand-alone (root) class. */
1227 if (lcls
&& TREE_CODE (lcls
) == IDENTIFIER_NODE
)
1230 if (rcls
&& TREE_CODE (rcls
) == IDENTIFIER_NODE
)
1233 /* If either type is an unqualified 'id', we're done. */
1234 if ((!lproto
&& objc_is_object_id (ltyp
))
1235 || (!rproto
&& objc_is_object_id (rtyp
)))
1238 pointers_compatible
= (TYPE_MAIN_VARIANT (ltyp
) == TYPE_MAIN_VARIANT (rtyp
));
1240 /* If the underlying types are the same, and at most one of them has
1241 a protocol list, we do not need to issue any diagnostics. */
1242 if (pointers_compatible
&& (!lproto
|| !rproto
))
1245 /* If exactly one of the types is 'Class', issue a diagnostic; any
1246 exceptions of this rule have already been handled. */
1247 if (objc_is_class_id (ltyp
) ^ objc_is_class_id (rtyp
))
1248 pointers_compatible
= false;
1249 /* Otherwise, check for inheritance relations. */
1252 if (!pointers_compatible
)
1254 = (objc_is_object_id (ltyp
) || objc_is_object_id (rtyp
));
1256 if (!pointers_compatible
)
1257 pointers_compatible
= DERIVED_FROM_P (ltyp
, rtyp
);
1259 if (!pointers_compatible
&& argno
<= -3)
1260 pointers_compatible
= DERIVED_FROM_P (rtyp
, ltyp
);
1263 /* If the pointers match modulo protocols, check for protocol conformance
1265 if (pointers_compatible
)
1267 pointers_compatible
= objc_compare_protocols (lcls
, ltyp
, rcls
, rtyp
,
1270 if (!pointers_compatible
&& argno
== -3)
1271 pointers_compatible
= objc_compare_protocols (rcls
, rtyp
, lcls
, ltyp
,
1275 if (!pointers_compatible
)
1277 /* The two pointers are not exactly compatible. Issue a warning, unless
1278 we are performing a silent comparison, in which case return 'false'
1280 /* NB: For the time being, we shall make our warnings look like their
1281 C counterparts. In the future, we may wish to make them more
1289 warning (0, "comparison of distinct Objective-C types lacks a cast");
1293 warning (0, "initialization from distinct Objective-C type");
1297 warning (0, "assignment from distinct Objective-C type");
1301 warning (0, "distinct Objective-C type in return");
1305 warning (0, "passing argument %d of %qE from distinct "
1306 "Objective-C type", argno
, callee
);
1314 /* This routine is similar to objc_compare_types except that function-pointers are
1315 excluded. This is because, caller assumes that common types are of (id, Object*)
1316 variety and calls objc_common_type to obtain a common type. There is no commonolty
1317 between two function-pointers in this regard. */
1320 objc_have_common_type (tree ltyp
, tree rtyp
, int argno
, tree callee
)
1322 if (objc_compare_types (ltyp
, rtyp
, argno
, callee
))
1324 /* exclude function-pointer types. */
1327 ltyp
= TREE_TYPE (ltyp
); /* Remove indirections. */
1328 rtyp
= TREE_TYPE (rtyp
);
1330 while (POINTER_TYPE_P (ltyp
) && POINTER_TYPE_P (rtyp
));
1331 return !(TREE_CODE (ltyp
) == FUNCTION_TYPE
&& TREE_CODE (rtyp
) == FUNCTION_TYPE
);
1336 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1337 lives in the volatilized hash table, ignore the 'volatile' bit when
1338 making the comparison. */
1341 objc_type_quals_match (tree ltyp
, tree rtyp
)
1343 int lquals
= TYPE_QUALS (ltyp
), rquals
= TYPE_QUALS (rtyp
);
1344 struct volatilized_type key
;
1348 if (htab_find_slot (volatilized_htab
, &key
, NO_INSERT
))
1349 lquals
&= ~TYPE_QUAL_VOLATILE
;
1353 if (htab_find_slot (volatilized_htab
, &key
, NO_INSERT
))
1354 rquals
&= ~TYPE_QUAL_VOLATILE
;
1356 return (lquals
== rquals
);
1360 /* Determine if CHILD is derived from PARENT. The routine assumes that
1361 both parameters are RECORD_TYPEs, and is non-reflexive. */
1364 objc_derived_from_p (tree parent
, tree child
)
1366 parent
= TYPE_MAIN_VARIANT (parent
);
1368 for (child
= TYPE_MAIN_VARIANT (child
);
1369 TYPE_BINFO (child
) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child
));)
1371 child
= TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1372 (TYPE_BINFO (child
),
1375 if (child
== parent
)
1384 objc_build_component_ref (tree datum
, tree component
)
1386 /* If COMPONENT is NULL, the caller is referring to the anonymous
1387 base class field. */
1390 tree base
= TYPE_FIELDS (TREE_TYPE (datum
));
1392 return build3 (COMPONENT_REF
, TREE_TYPE (base
), datum
, base
, NULL_TREE
);
1395 /* The 'build_component_ref' routine has been removed from the C++
1396 front-end, but 'finish_class_member_access_expr' seems to be
1397 a worthy substitute. */
1399 return finish_class_member_access_expr (datum
, component
, false,
1400 tf_warning_or_error
);
1402 return build_component_ref (input_location
, datum
, component
);
1406 /* Recursively copy inheritance information rooted at BINFO. To do this,
1407 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1410 objc_copy_binfo (tree binfo
)
1412 tree btype
= BINFO_TYPE (binfo
);
1413 tree binfo2
= make_tree_binfo (BINFO_N_BASE_BINFOS (binfo
));
1417 BINFO_TYPE (binfo2
) = btype
;
1418 BINFO_OFFSET (binfo2
) = BINFO_OFFSET (binfo
);
1419 BINFO_BASE_ACCESSES (binfo2
) = BINFO_BASE_ACCESSES (binfo
);
1421 /* Recursively copy base binfos of BINFO. */
1422 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
1424 tree base_binfo2
= objc_copy_binfo (base_binfo
);
1426 BINFO_INHERITANCE_CHAIN (base_binfo2
) = binfo2
;
1427 BINFO_BASE_APPEND (binfo2
, base_binfo2
);
1433 /* Record superclass information provided in BASETYPE for ObjC class REF.
1434 This is loosely based on cp/decl.c:xref_basetypes(). */
1437 objc_xref_basetypes (tree ref
, tree basetype
)
1439 tree binfo
= make_tree_binfo (basetype
? 1 : 0);
1441 TYPE_BINFO (ref
) = binfo
;
1442 BINFO_OFFSET (binfo
) = size_zero_node
;
1443 BINFO_TYPE (binfo
) = ref
;
1447 tree base_binfo
= objc_copy_binfo (TYPE_BINFO (basetype
));
1449 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
1450 BINFO_BASE_ACCESSES (binfo
) = VEC_alloc (tree
, gc
, 1);
1451 BINFO_BASE_APPEND (binfo
, base_binfo
);
1452 BINFO_BASE_ACCESS_APPEND (binfo
, access_public_node
);
1457 volatilized_hash (const void *ptr
)
1459 const_tree
const typ
= ((const struct volatilized_type
*)ptr
)->type
;
1461 return htab_hash_pointer(typ
);
1465 volatilized_eq (const void *ptr1
, const void *ptr2
)
1467 const_tree
const typ1
= ((const struct volatilized_type
*)ptr1
)->type
;
1468 const_tree
const typ2
= ((const struct volatilized_type
*)ptr2
)->type
;
1470 return typ1
== typ2
;
1473 /* Called from finish_decl. */
1476 objc_check_decl (tree decl
)
1478 tree type
= TREE_TYPE (decl
);
1480 if (TREE_CODE (type
) != RECORD_TYPE
)
1482 if (OBJC_TYPE_NAME (type
) && (type
= objc_is_class_name (OBJC_TYPE_NAME (type
))))
1483 error ("statically allocated instance of Objective-C class %qE",
1487 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
1488 either name an Objective-C class, or refer to the special 'id' or 'Class'
1489 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
1492 objc_get_protocol_qualified_type (tree interface
, tree protocols
)
1494 /* If INTERFACE is not provided, default to 'id'. */
1495 tree type
= (interface
? objc_is_id (interface
) : objc_object_type
);
1496 bool is_ptr
= (type
!= NULL_TREE
);
1500 type
= objc_is_class_name (interface
);
1504 /* If looking at a typedef, retrieve the precise type it
1506 if (TREE_CODE (interface
) == IDENTIFIER_NODE
)
1507 interface
= identifier_global_value (interface
);
1509 type
= ((interface
&& TREE_CODE (interface
) == TYPE_DECL
1510 && DECL_ORIGINAL_TYPE (interface
))
1511 ? DECL_ORIGINAL_TYPE (interface
)
1512 : xref_tag (RECORD_TYPE
, type
));
1520 type
= build_variant_type_copy (type
);
1522 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
1526 tree orig_pointee_type
= TREE_TYPE (type
);
1527 TREE_TYPE (type
) = build_variant_type_copy (orig_pointee_type
);
1529 /* Set up the canonical type information. */
1530 TYPE_CANONICAL (type
)
1531 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type
));
1533 TYPE_POINTER_TO (TREE_TYPE (type
)) = type
;
1534 type
= TREE_TYPE (type
);
1537 /* Look up protocols and install in lang specific list. */
1538 DUP_TYPE_OBJC_INFO (type
, TYPE_MAIN_VARIANT (type
));
1539 TYPE_OBJC_PROTOCOL_LIST (type
) = lookup_and_install_protocols (protocols
);
1541 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
1542 return the pointer to the new pointee variant. */
1544 type
= TYPE_POINTER_TO (type
);
1546 TYPE_OBJC_INTERFACE (type
)
1547 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type
));
1553 /* Check for circular dependencies in protocols. The arguments are
1554 PROTO, the protocol to check, and LIST, a list of protocol it
1558 check_protocol_recursively (tree proto
, tree list
)
1562 for (p
= list
; p
; p
= TREE_CHAIN (p
))
1564 tree pp
= TREE_VALUE (p
);
1566 if (TREE_CODE (pp
) == IDENTIFIER_NODE
)
1567 pp
= lookup_protocol (pp
);
1570 fatal_error ("protocol %qE has circular dependency",
1571 PROTOCOL_NAME (pp
));
1573 check_protocol_recursively (proto
, PROTOCOL_LIST (pp
));
1577 /* Look up PROTOCOLS, and return a list of those that are found.
1578 If none are found, return NULL. */
1581 lookup_and_install_protocols (tree protocols
)
1584 tree return_value
= NULL_TREE
;
1586 for (proto
= protocols
; proto
; proto
= TREE_CHAIN (proto
))
1588 tree ident
= TREE_VALUE (proto
);
1589 tree p
= lookup_protocol (ident
);
1592 return_value
= chainon (return_value
,
1593 build_tree_list (NULL_TREE
, p
));
1594 else if (ident
!= error_mark_node
)
1595 error ("cannot find protocol declaration for %qE",
1599 return return_value
;
1602 /* Create a declaration for field NAME of a given TYPE. */
1605 create_field_decl (tree type
, const char *name
)
1607 return build_decl (input_location
,
1608 FIELD_DECL
, get_identifier (name
), type
);
1611 /* Create a global, static declaration for variable NAME of a given TYPE. The
1612 finish_var_decl() routine will need to be called on it afterwards. */
1615 start_var_decl (tree type
, const char *name
)
1617 tree var
= build_decl (input_location
,
1618 VAR_DECL
, get_identifier (name
), type
);
1620 TREE_STATIC (var
) = 1;
1621 DECL_INITIAL (var
) = error_mark_node
; /* A real initializer is coming... */
1622 DECL_IGNORED_P (var
) = 1;
1623 DECL_ARTIFICIAL (var
) = 1;
1624 DECL_CONTEXT (var
) = NULL_TREE
;
1626 DECL_THIS_STATIC (var
) = 1; /* squash redeclaration errors */
1632 /* Finish off the variable declaration created by start_var_decl(). */
1635 finish_var_decl (tree var
, tree initializer
)
1637 finish_decl (var
, input_location
, initializer
, NULL_TREE
, NULL_TREE
);
1640 /* Find the decl for the constant string class reference. This is only
1641 used for the NeXT runtime. */
1644 setup_string_decl (void)
1649 /* %s in format will provide room for terminating null */
1650 length
= strlen (STRING_OBJECT_GLOBAL_FORMAT
)
1651 + strlen (constant_string_class_name
);
1652 name
= XNEWVEC (char, length
);
1653 sprintf (name
, STRING_OBJECT_GLOBAL_FORMAT
,
1654 constant_string_class_name
);
1655 constant_string_global_id
= get_identifier (name
);
1656 string_class_decl
= lookup_name (constant_string_global_id
);
1658 return string_class_decl
;
1661 /* Purpose: "play" parser, creating/installing representations
1662 of the declarations that are required by Objective-C.
1666 type_spec--------->sc_spec
1667 (tree_list) (tree_list)
1670 identifier_node identifier_node */
1673 synth_module_prologue (void)
1676 enum debug_info_type save_write_symbols
= write_symbols
;
1677 const struct gcc_debug_hooks
*const save_hooks
= debug_hooks
;
1679 /* Suppress outputting debug symbols, because
1680 dbxout_init hasn't been called yet. */
1681 write_symbols
= NO_DEBUG
;
1682 debug_hooks
= &do_nothing_debug_hooks
;
1685 push_lang_context (lang_name_c
); /* extern "C" */
1688 /* The following are also defined in <objc/objc.h> and friends. */
1690 objc_object_id
= get_identifier (TAG_OBJECT
);
1691 objc_class_id
= get_identifier (TAG_CLASS
);
1693 objc_object_reference
= xref_tag (RECORD_TYPE
, objc_object_id
);
1694 objc_class_reference
= xref_tag (RECORD_TYPE
, objc_class_id
);
1696 objc_object_type
= build_pointer_type (objc_object_reference
);
1697 objc_class_type
= build_pointer_type (objc_class_reference
);
1699 objc_object_name
= get_identifier (OBJECT_TYPEDEF_NAME
);
1700 objc_class_name
= get_identifier (CLASS_TYPEDEF_NAME
);
1702 /* Declare the 'id' and 'Class' typedefs. */
1704 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
1708 TREE_NO_WARNING (type
) = 1;
1709 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
1713 TREE_NO_WARNING (type
) = 1;
1715 /* Forward-declare '@interface Protocol'. */
1717 type
= get_identifier (PROTOCOL_OBJECT_CLASS_NAME
);
1718 objc_declare_class (tree_cons (NULL_TREE
, type
, NULL_TREE
));
1719 objc_protocol_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
1722 /* Declare type of selector-objects that represent an operation name. */
1724 if (flag_next_runtime
)
1725 /* `struct objc_selector *' */
1727 = build_pointer_type (xref_tag (RECORD_TYPE
,
1728 get_identifier (TAG_SELECTOR
)));
1730 /* `const struct objc_selector *' */
1732 = build_pointer_type
1733 (build_qualified_type (xref_tag (RECORD_TYPE
,
1734 get_identifier (TAG_SELECTOR
)),
1737 /* Declare receiver type used for dispatching messages to 'super'. */
1739 /* `struct objc_super *' */
1740 objc_super_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
1741 get_identifier (TAG_SUPER
)));
1743 /* Declare pointers to method and ivar lists. */
1744 objc_method_list_ptr
= build_pointer_type
1745 (xref_tag (RECORD_TYPE
,
1746 get_identifier (UTAG_METHOD_LIST
)));
1747 objc_method_proto_list_ptr
1748 = build_pointer_type (xref_tag (RECORD_TYPE
,
1749 get_identifier (UTAG_METHOD_PROTOTYPE_LIST
)));
1750 objc_ivar_list_ptr
= build_pointer_type
1751 (xref_tag (RECORD_TYPE
,
1752 get_identifier (UTAG_IVAR_LIST
)));
1754 /* TREE_NOTHROW is cleared for the message-sending functions,
1755 because the function that gets called can throw in Obj-C++, or
1756 could itself call something that can throw even in Obj-C. */
1758 if (flag_next_runtime
)
1760 /* NB: In order to call one of the ..._stret (struct-returning)
1761 functions, the function *MUST* first be cast to a signature that
1762 corresponds to the actual ObjC method being invoked. This is
1763 what is done by the build_objc_method_call() routine below. */
1765 /* id objc_msgSend (id, SEL, ...); */
1766 /* id objc_msgSendNonNil (id, SEL, ...); */
1767 /* id objc_msgSend_stret (id, SEL, ...); */
1768 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
1770 = build_varargs_function_type_list (objc_object_type
,
1774 umsg_decl
= add_builtin_function (TAG_MSGSEND
,
1775 type
, 0, NOT_BUILT_IN
,
1777 umsg_nonnil_decl
= add_builtin_function (TAG_MSGSEND_NONNIL
,
1778 type
, 0, NOT_BUILT_IN
,
1780 umsg_stret_decl
= add_builtin_function (TAG_MSGSEND_STRET
,
1781 type
, 0, NOT_BUILT_IN
,
1783 umsg_nonnil_stret_decl
= add_builtin_function (TAG_MSGSEND_NONNIL_STRET
,
1784 type
, 0, NOT_BUILT_IN
,
1787 /* These can throw, because the function that gets called can throw
1788 in Obj-C++, or could itself call something that can throw even
1790 TREE_NOTHROW (umsg_decl
) = 0;
1791 TREE_NOTHROW (umsg_nonnil_decl
) = 0;
1792 TREE_NOTHROW (umsg_stret_decl
) = 0;
1793 TREE_NOTHROW (umsg_nonnil_stret_decl
) = 0;
1795 /* id objc_msgSend_Fast (id, SEL, ...)
1796 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
1797 #ifdef OFFS_MSGSEND_FAST
1798 umsg_fast_decl
= add_builtin_function (TAG_MSGSEND_FAST
,
1799 type
, 0, NOT_BUILT_IN
,
1801 TREE_NOTHROW (umsg_fast_decl
) = 0;
1802 DECL_ATTRIBUTES (umsg_fast_decl
)
1803 = tree_cons (get_identifier ("hard_coded_address"),
1804 build_int_cst (NULL_TREE
, OFFS_MSGSEND_FAST
),
1807 /* No direct dispatch available. */
1808 umsg_fast_decl
= umsg_decl
;
1811 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
1812 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
1814 = build_varargs_function_type_list (objc_object_type
,
1818 umsg_super_decl
= add_builtin_function (TAG_MSGSENDSUPER
,
1819 type
, 0, NOT_BUILT_IN
,
1821 umsg_super_stret_decl
= add_builtin_function (TAG_MSGSENDSUPER_STRET
,
1822 type
, 0, NOT_BUILT_IN
, 0,
1824 TREE_NOTHROW (umsg_super_decl
) = 0;
1825 TREE_NOTHROW (umsg_super_stret_decl
) = 0;
1829 /* GNU runtime messenger entry points. */
1831 /* typedef id (*IMP)(id, SEL, ...); */
1833 build_varargs_function_type_list (objc_object_type
,
1837 tree IMP_type
= build_pointer_type (ftype
);
1839 /* IMP objc_msg_lookup (id, SEL); */
1840 type
= build_function_type_list (IMP_type
,
1844 umsg_decl
= add_builtin_function (TAG_MSGSEND
,
1845 type
, 0, NOT_BUILT_IN
,
1847 TREE_NOTHROW (umsg_decl
) = 0;
1849 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
1851 = build_function_type_list (IMP_type
,
1855 umsg_super_decl
= add_builtin_function (TAG_MSGSENDSUPER
,
1856 type
, 0, NOT_BUILT_IN
,
1858 TREE_NOTHROW (umsg_super_decl
) = 0;
1860 /* The following GNU runtime entry point is called to initialize
1863 __objc_exec_class (void *); */
1865 = build_function_type_list (void_type_node
,
1868 execclass_decl
= add_builtin_function (TAG_EXECCLASS
,
1869 type
, 0, NOT_BUILT_IN
,
1873 /* id objc_getClass (const char *); */
1875 type
= build_function_type_list (objc_object_type
,
1876 const_string_type_node
,
1880 = add_builtin_function (TAG_GETCLASS
, type
, 0, NOT_BUILT_IN
,
1883 /* id objc_getMetaClass (const char *); */
1885 objc_get_meta_class_decl
1886 = add_builtin_function (TAG_GETMETACLASS
, type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
1888 build_class_template ();
1889 build_super_template ();
1890 build_protocol_template ();
1891 build_category_template ();
1892 build_objc_exception_stuff ();
1894 if (flag_next_runtime
)
1895 build_next_objc_exception_stuff ();
1897 /* static SEL _OBJC_SELECTOR_TABLE[]; */
1899 if (! flag_next_runtime
)
1900 build_selector_table_decl ();
1902 /* Forward declare constant_string_id and constant_string_type. */
1903 if (!constant_string_class_name
)
1904 constant_string_class_name
= default_constant_string_class_name
;
1906 constant_string_id
= get_identifier (constant_string_class_name
);
1907 objc_declare_class (tree_cons (NULL_TREE
, constant_string_id
, NULL_TREE
));
1909 /* Pre-build the following entities - for speed/convenience. */
1910 self_id
= get_identifier ("self");
1911 ucmd_id
= get_identifier ("_cmd");
1914 pop_lang_context ();
1917 write_symbols
= save_write_symbols
;
1918 debug_hooks
= save_hooks
;
1921 /* Ensure that the ivar list for NSConstantString/NXConstantString
1922 (or whatever was specified via `-fconstant-string-class')
1923 contains fields at least as large as the following three, so that
1924 the runtime can stomp on them with confidence:
1926 struct STRING_OBJECT_CLASS_NAME
1930 unsigned int length;
1934 check_string_class_template (void)
1936 tree field_decl
= objc_get_class_ivars (constant_string_id
);
1938 #define AT_LEAST_AS_LARGE_AS(F, T) \
1939 (F && TREE_CODE (F) == FIELD_DECL \
1940 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
1941 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
1943 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
1946 field_decl
= DECL_CHAIN (field_decl
);
1947 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
1950 field_decl
= DECL_CHAIN (field_decl
);
1951 return AT_LEAST_AS_LARGE_AS (field_decl
, unsigned_type_node
);
1953 #undef AT_LEAST_AS_LARGE_AS
1956 /* Avoid calling `check_string_class_template ()' more than once. */
1957 static GTY(()) int string_layout_checked
;
1959 /* Construct an internal string layout to be used as a template for
1960 creating NSConstantString/NXConstantString instances. */
1963 objc_build_internal_const_str_type (void)
1965 tree type
= (*lang_hooks
.types
.make_type
) (RECORD_TYPE
);
1966 tree fields
= build_decl (input_location
,
1967 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
1968 tree field
= build_decl (input_location
,
1969 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
1971 DECL_CHAIN (field
) = fields
; fields
= field
;
1972 field
= build_decl (input_location
,
1973 FIELD_DECL
, NULL_TREE
, unsigned_type_node
);
1974 DECL_CHAIN (field
) = fields
; fields
= field
;
1975 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
1977 finish_builtin_struct (type
, "__builtin_ObjCString",
1983 /* Custom build_string which sets TREE_TYPE! */
1986 my_build_string (int len
, const char *str
)
1988 return fix_string_type (build_string (len
, str
));
1991 /* Build a string with contents STR and length LEN and convert it to a
1995 my_build_string_pointer (int len
, const char *str
)
1997 tree string
= my_build_string (len
, str
);
1998 tree ptrtype
= build_pointer_type (TREE_TYPE (TREE_TYPE (string
)));
1999 return build1 (ADDR_EXPR
, ptrtype
, string
);
2003 string_hash (const void *ptr
)
2005 const_tree
const str
= ((const struct string_descriptor
*)ptr
)->literal
;
2006 const unsigned char *p
= (const unsigned char *) TREE_STRING_POINTER (str
);
2007 int i
, len
= TREE_STRING_LENGTH (str
);
2010 for (i
= 0; i
< len
; i
++)
2011 h
= ((h
* 613) + p
[i
]);
2017 string_eq (const void *ptr1
, const void *ptr2
)
2019 const_tree
const str1
= ((const struct string_descriptor
*)ptr1
)->literal
;
2020 const_tree
const str2
= ((const struct string_descriptor
*)ptr2
)->literal
;
2021 int len1
= TREE_STRING_LENGTH (str1
);
2023 return (len1
== TREE_STRING_LENGTH (str2
)
2024 && !memcmp (TREE_STRING_POINTER (str1
), TREE_STRING_POINTER (str2
),
2028 /* Given a chain of STRING_CST's, build a static instance of
2029 NXConstantString which points at the concatenation of those
2030 strings. We place the string object in the __string_objects
2031 section of the __OBJC segment. The Objective-C runtime will
2032 initialize the isa pointers of the string objects to point at the
2033 NXConstantString class object. */
2036 objc_build_string_object (tree string
)
2038 tree constructor
, constant_string_class
;
2041 struct string_descriptor
*desc
, key
;
2044 /* Prep the string argument. */
2045 string
= fix_string_type (string
);
2046 TREE_SET_CODE (string
, STRING_CST
);
2047 length
= TREE_STRING_LENGTH (string
) - 1;
2049 /* Check whether the string class being used actually exists and has the
2050 correct ivar layout. */
2051 if (!string_layout_checked
)
2053 string_layout_checked
= -1;
2054 constant_string_class
= lookup_interface (constant_string_id
);
2055 internal_const_str_type
= objc_build_internal_const_str_type ();
2057 if (!constant_string_class
2058 || !(constant_string_type
2059 = CLASS_STATIC_TEMPLATE (constant_string_class
)))
2060 error ("cannot find interface declaration for %qE",
2061 constant_string_id
);
2062 /* The NSConstantString/NXConstantString ivar layout is now known. */
2063 else if (!check_string_class_template ())
2064 error ("interface %qE does not have valid constant string layout",
2065 constant_string_id
);
2066 /* For the NeXT runtime, we can generate a literal reference
2067 to the string class, don't need to run a constructor. */
2068 else if (flag_next_runtime
&& !setup_string_decl ())
2069 error ("cannot find reference tag for class %qE",
2070 constant_string_id
);
2073 string_layout_checked
= 1; /* Success! */
2074 add_class_reference (constant_string_id
);
2078 if (string_layout_checked
== -1)
2079 return error_mark_node
;
2081 /* Perhaps we already constructed a constant string just like this one? */
2082 key
.literal
= string
;
2083 loc
= htab_find_slot (string_htab
, &key
, INSERT
);
2084 desc
= (struct string_descriptor
*) *loc
;
2089 VEC(constructor_elt
,gc
) *v
= NULL
;
2090 *loc
= desc
= ggc_alloc_string_descriptor ();
2091 desc
->literal
= string
;
2093 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
2094 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
2095 fields
= TYPE_FIELDS (internal_const_str_type
);
2096 CONSTRUCTOR_APPEND_ELT (v
, fields
,
2098 ? build_unary_op (input_location
,
2099 ADDR_EXPR
, string_class_decl
, 0)
2100 : build_int_cst (NULL_TREE
, 0));
2101 fields
= DECL_CHAIN (fields
);
2102 CONSTRUCTOR_APPEND_ELT (v
, fields
,
2103 build_unary_op (input_location
,
2104 ADDR_EXPR
, string
, 1));
2105 fields
= DECL_CHAIN (fields
);
2106 CONSTRUCTOR_APPEND_ELT (v
, fields
, build_int_cst (NULL_TREE
, length
));
2107 constructor
= objc_build_constructor (internal_const_str_type
, v
);
2109 if (!flag_next_runtime
)
2111 = objc_add_static_instance (constructor
, constant_string_type
);
2114 var
= build_decl (input_location
,
2115 CONST_DECL
, NULL
, TREE_TYPE (constructor
));
2116 DECL_INITIAL (var
) = constructor
;
2117 TREE_STATIC (var
) = 1;
2118 pushdecl_top_level (var
);
2121 desc
->constructor
= constructor
;
2124 addr
= convert (build_pointer_type (constant_string_type
),
2125 build_unary_op (input_location
,
2126 ADDR_EXPR
, desc
->constructor
, 1));
2131 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2133 static GTY(()) int num_static_inst
;
2136 objc_add_static_instance (tree constructor
, tree class_decl
)
2141 /* Find the list of static instances for the CLASS_DECL. Create one if
2143 for (chain
= &objc_static_instances
;
2144 *chain
&& TREE_VALUE (*chain
) != class_decl
;
2145 chain
= &TREE_CHAIN (*chain
));
2148 *chain
= tree_cons (NULL_TREE
, class_decl
, NULL_TREE
);
2149 add_objc_string (OBJC_TYPE_NAME (class_decl
), class_names
);
2152 sprintf (buf
, "_OBJC_INSTANCE_%d", num_static_inst
++);
2153 decl
= build_decl (input_location
,
2154 VAR_DECL
, get_identifier (buf
), class_decl
);
2155 TREE_STATIC (decl
) = 1;
2156 DECL_ARTIFICIAL (decl
) = 1;
2157 TREE_USED (decl
) = 1;
2158 DECL_INITIAL (decl
) = constructor
;
2160 /* We may be writing something else just now.
2161 Postpone till end of input. */
2162 DECL_DEFER_OUTPUT (decl
) = 1;
2163 pushdecl_top_level (decl
);
2164 rest_of_decl_compilation (decl
, 1, 0);
2166 /* Add the DECL to the head of this CLASS' list. */
2167 TREE_PURPOSE (*chain
) = tree_cons (NULL_TREE
, decl
, TREE_PURPOSE (*chain
));
2172 /* Build a static constant CONSTRUCTOR
2173 with type TYPE and elements ELTS. */
2176 objc_build_constructor (tree type
, VEC(constructor_elt
,gc
) *elts
)
2178 tree constructor
= build_constructor (type
, elts
);
2180 TREE_CONSTANT (constructor
) = 1;
2181 TREE_STATIC (constructor
) = 1;
2182 TREE_READONLY (constructor
) = 1;
2185 /* Adjust for impedance mismatch. We should figure out how to build
2186 CONSTRUCTORs that consistently please both the C and C++ gods. */
2187 if (!VEC_index (constructor_elt
, elts
, 0)->index
)
2188 TREE_TYPE (constructor
) = init_list_type_node
;
2194 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2196 /* Predefine the following data type:
2204 void *defs[cls_def_cnt + cat_def_cnt];
2208 build_objc_symtab_template (void)
2210 tree fields
, *chain
= NULL
;
2212 objc_symtab_template
= objc_start_struct (get_identifier (UTAG_SYMTAB
));
2214 /* long sel_ref_cnt; */
2215 fields
= add_field_decl (long_integer_type_node
, "sel_ref_cnt", &chain
);
2218 add_field_decl (build_pointer_type (objc_selector_type
), "refs", &chain
);
2220 /* short cls_def_cnt; */
2221 add_field_decl (short_integer_type_node
, "cls_def_cnt", &chain
);
2223 /* short cat_def_cnt; */
2224 add_field_decl (short_integer_type_node
, "cat_def_cnt", &chain
);
2226 if (imp_count
|| cat_count
|| !flag_next_runtime
)
2228 /* void *defs[imp_count + cat_count (+ 1)]; */
2229 /* NB: The index is one less than the size of the array. */
2230 int index
= imp_count
+ cat_count
+ (flag_next_runtime
? -1: 0);
2231 tree array_type
= build_sized_array_type (ptr_type_node
, index
+ 1);
2232 add_field_decl (array_type
, "defs", &chain
);
2235 objc_finish_struct (objc_symtab_template
, fields
);
2238 /* Create the initial value for the `defs' field of _objc_symtab.
2239 This is a CONSTRUCTOR. */
2242 init_def_list (tree type
)
2245 struct imp_entry
*impent
;
2246 VEC(constructor_elt
,gc
) *v
= NULL
;
2249 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2251 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
2253 expr
= build_unary_op (input_location
,
2254 ADDR_EXPR
, impent
->class_decl
, 0);
2255 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2260 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2262 if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
2264 expr
= build_unary_op (input_location
,
2265 ADDR_EXPR
, impent
->class_decl
, 0);
2266 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2270 if (!flag_next_runtime
)
2272 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2273 if (static_instances_decl
)
2274 expr
= build_unary_op (input_location
,
2275 ADDR_EXPR
, static_instances_decl
, 0);
2277 expr
= integer_zero_node
;
2279 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2282 return objc_build_constructor (type
, v
);
2285 /* Construct the initial value for all of _objc_symtab. */
2288 init_objc_symtab (tree type
)
2290 VEC(constructor_elt
,gc
) *v
= NULL
;
2292 /* sel_ref_cnt = { ..., 5, ... } */
2294 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2295 build_int_cst (long_integer_type_node
, 0));
2297 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2299 if (flag_next_runtime
|| ! sel_ref_chain
)
2300 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, convert (
2301 build_pointer_type (objc_selector_type
),
2302 integer_zero_node
));
2305 tree expr
= build_unary_op (input_location
, ADDR_EXPR
,
2306 UOBJC_SELECTOR_TABLE_decl
, 1);
2308 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2309 convert (build_pointer_type (objc_selector_type
),
2313 /* cls_def_cnt = { ..., 5, ... } */
2315 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2316 build_int_cst (short_integer_type_node
, imp_count
));
2318 /* cat_def_cnt = { ..., 5, ... } */
2320 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2321 build_int_cst (short_integer_type_node
, cat_count
));
2323 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2325 if (imp_count
|| cat_count
|| !flag_next_runtime
)
2328 tree field
= TYPE_FIELDS (type
);
2329 field
= DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field
))));
2331 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init_def_list (TREE_TYPE (field
)));
2334 return objc_build_constructor (type
, v
);
2337 /* Generate forward declarations for metadata such as
2338 'OBJC_CLASS_...'. */
2341 build_metadata_decl (const char *name
, tree type
)
2345 /* struct TYPE NAME_<name>; */
2346 decl
= start_var_decl (type
, synth_id_with_class_suffix
2348 objc_implementation_context
));
2353 /* Push forward-declarations of all the categories so that
2354 init_def_list can use them in a CONSTRUCTOR. */
2357 forward_declare_categories (void)
2359 struct imp_entry
*impent
;
2360 tree sav
= objc_implementation_context
;
2362 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2364 if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
2366 /* Set an invisible arg to synth_id_with_class_suffix. */
2367 objc_implementation_context
= impent
->imp_context
;
2368 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2369 impent
->class_decl
= build_metadata_decl ("_OBJC_CATEGORY",
2370 objc_category_template
);
2373 objc_implementation_context
= sav
;
2376 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2377 and initialized appropriately. */
2380 generate_objc_symtab_decl (void)
2383 build_objc_symtab_template ();
2384 UOBJC_SYMBOLS_decl
= start_var_decl (objc_symtab_template
, "_OBJC_SYMBOLS");
2385 finish_var_decl (UOBJC_SYMBOLS_decl
,
2386 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl
)));
2390 init_module_descriptor (tree type
)
2393 VEC(constructor_elt
,gc
) *v
= NULL
;
2395 /* version = { 1, ... } */
2397 expr
= build_int_cst (long_integer_type_node
, OBJC_VERSION
);
2398 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2400 /* size = { ..., sizeof (struct _objc_module), ... } */
2402 expr
= convert (long_integer_type_node
,
2403 size_in_bytes (objc_module_template
));
2404 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2406 /* Don't provide any file name for security reasons. */
2407 /* name = { ..., "", ... } */
2409 expr
= add_objc_string (get_identifier (""), class_names
);
2410 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2412 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2414 if (UOBJC_SYMBOLS_decl
)
2415 expr
= build_unary_op (input_location
,
2416 ADDR_EXPR
, UOBJC_SYMBOLS_decl
, 0);
2418 expr
= null_pointer_node
;
2419 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2421 return objc_build_constructor (type
, v
);
2424 /* Write out the data structures to describe Objective C classes defined.
2426 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2429 build_module_descriptor (void)
2431 tree decls
, *chain
= NULL
;
2434 push_lang_context (lang_name_c
); /* extern "C" */
2437 objc_module_template
= objc_start_struct (get_identifier (UTAG_MODULE
));
2440 decls
= add_field_decl (long_integer_type_node
, "version", &chain
);
2443 add_field_decl (long_integer_type_node
, "size", &chain
);
2446 add_field_decl (string_type_node
, "name", &chain
);
2448 /* struct _objc_symtab *symtab; */
2449 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE
,
2450 get_identifier (UTAG_SYMTAB
))),
2453 objc_finish_struct (objc_module_template
, decls
);
2455 /* Create an instance of "_objc_module". */
2456 UOBJC_MODULES_decl
= start_var_decl (objc_module_template
, "_OBJC_MODULES");
2457 /* This is the root of the metadata for defined classes and categories, it
2458 is referenced by the runtime and, therefore, needed. */
2459 DECL_PRESERVE_P (UOBJC_MODULES_decl
) = 1;
2460 finish_var_decl (UOBJC_MODULES_decl
,
2461 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl
)));
2464 pop_lang_context ();
2468 /* The GNU runtime requires us to provide a static initializer function
2471 static void __objc_gnu_init (void) {
2472 __objc_exec_class (&L_OBJC_MODULES);
2476 build_module_initializer_routine (void)
2481 push_lang_context (lang_name_c
); /* extern "C" */
2484 objc_push_parm (build_decl (input_location
,
2485 PARM_DECL
, NULL_TREE
, void_type_node
));
2487 objc_start_function (get_identifier (TAG_GNUINIT
),
2488 build_function_type_list (void_type_node
, NULL_TREE
),
2489 NULL_TREE
, NULL_TREE
);
2491 objc_start_function (get_identifier (TAG_GNUINIT
),
2492 build_function_type_list (void_type_node
, NULL_TREE
),
2493 NULL_TREE
, objc_get_parm_info (0));
2495 body
= c_begin_compound_stmt (true);
2496 add_stmt (build_function_call
2501 build_unary_op (input_location
, ADDR_EXPR
,
2502 UOBJC_MODULES_decl
, 0))));
2503 add_stmt (c_end_compound_stmt (input_location
, body
, true));
2505 TREE_PUBLIC (current_function_decl
) = 0;
2508 /* For Objective-C++, we will need to call __objc_gnu_init
2509 from objc_generate_static_init_call() below. */
2510 DECL_STATIC_CONSTRUCTOR (current_function_decl
) = 1;
2513 GNU_INIT_decl
= current_function_decl
;
2517 pop_lang_context ();
2522 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
2523 to be called by the module initializer routine. */
2526 objc_static_init_needed_p (void)
2528 return (GNU_INIT_decl
!= NULL_TREE
);
2531 /* Generate a call to the __objc_gnu_init initializer function. */
2534 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED
)
2536 add_stmt (build_stmt (input_location
, EXPR_STMT
,
2537 build_function_call (input_location
,
2538 GNU_INIT_decl
, NULL_TREE
)));
2542 #endif /* OBJCPLUS */
2544 /* Return the DECL of the string IDENT in the SECTION. */
2547 get_objc_string_decl (tree ident
, enum string_section section
)
2551 if (section
== class_names
)
2552 chain
= class_names_chain
;
2553 else if (section
== meth_var_names
)
2554 chain
= meth_var_names_chain
;
2555 else if (section
== meth_var_types
)
2556 chain
= meth_var_types_chain
;
2560 for (; chain
!= 0; chain
= TREE_CHAIN (chain
))
2561 if (TREE_VALUE (chain
) == ident
)
2562 return (TREE_PURPOSE (chain
));
2568 /* Output references to all statically allocated objects. Return the DECL
2569 for the array built. */
2572 generate_static_references (void)
2574 tree expr
= NULL_TREE
;
2575 tree class_name
, klass
, decl
;
2576 tree cl_chain
, in_chain
, type
2577 = build_array_type (build_pointer_type (void_type_node
), NULL_TREE
);
2578 int num_inst
, num_class
;
2580 VEC(constructor_elt
,gc
) *decls
= NULL
;
2582 if (flag_next_runtime
)
2585 for (cl_chain
= objc_static_instances
, num_class
= 0;
2586 cl_chain
; cl_chain
= TREE_CHAIN (cl_chain
), num_class
++)
2588 VEC(constructor_elt
,gc
) *v
= NULL
;
2590 for (num_inst
= 0, in_chain
= TREE_PURPOSE (cl_chain
);
2591 in_chain
; num_inst
++, in_chain
= TREE_CHAIN (in_chain
));
2593 sprintf (buf
, "_OBJC_STATIC_INSTANCES_%d", num_class
);
2594 decl
= start_var_decl (type
, buf
);
2596 /* Output {class_name, ...}. */
2597 klass
= TREE_VALUE (cl_chain
);
2598 class_name
= get_objc_string_decl (OBJC_TYPE_NAME (klass
), class_names
);
2599 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2600 build_unary_op (input_location
,
2601 ADDR_EXPR
, class_name
, 1));
2603 /* Output {..., instance, ...}. */
2604 for (in_chain
= TREE_PURPOSE (cl_chain
);
2605 in_chain
; in_chain
= TREE_CHAIN (in_chain
))
2607 expr
= build_unary_op (input_location
,
2608 ADDR_EXPR
, TREE_VALUE (in_chain
), 1);
2609 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2612 /* Output {..., NULL}. */
2613 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
2615 expr
= objc_build_constructor (TREE_TYPE (decl
), v
);
2616 finish_var_decl (decl
, expr
);
2617 CONSTRUCTOR_APPEND_ELT (decls
, NULL_TREE
,
2618 build_unary_op (input_location
,
2619 ADDR_EXPR
, decl
, 1));
2622 CONSTRUCTOR_APPEND_ELT (decls
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
2623 expr
= objc_build_constructor (type
, decls
);
2624 static_instances_decl
= start_var_decl (type
, "_OBJC_STATIC_INSTANCES");
2625 finish_var_decl (static_instances_decl
, expr
);
2628 static GTY(()) int selector_reference_idx
;
2631 build_selector_reference_decl (void)
2636 sprintf (buf
, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx
++);
2637 decl
= start_var_decl (objc_selector_type
, buf
);
2643 build_selector_table_decl (void)
2647 if (flag_typed_selectors
)
2649 build_selector_template ();
2650 temp
= build_array_type (objc_selector_template
, NULL_TREE
);
2653 temp
= build_array_type (objc_selector_type
, NULL_TREE
);
2655 UOBJC_SELECTOR_TABLE_decl
= start_var_decl (temp
, "_OBJC_SELECTOR_TABLE");
2658 /* Just a handy wrapper for add_objc_string. */
2661 build_selector (tree ident
)
2663 return convert (objc_selector_type
,
2664 add_objc_string (ident
, meth_var_names
));
2667 /* Used only by build_*_selector_translation_table (). */
2669 diagnose_missing_method (tree meth
, location_t here
)
2673 for (method_chain
= meth_var_names_chain
;
2675 method_chain
= TREE_CHAIN (method_chain
))
2677 if (TREE_VALUE (method_chain
) == meth
)
2685 warning_at (here
, 0, "creating selector for nonexistent method %qE",
2690 build_next_selector_translation_table (void)
2693 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
2696 tree decl
= TREE_PURPOSE (chain
);
2697 if (warn_selector
&& objc_implementation_context
)
2701 loc
= DECL_SOURCE_LOCATION (decl
);
2703 loc
= input_location
;
2704 diagnose_missing_method (TREE_VALUE (chain
), loc
);
2707 expr
= build_selector (TREE_VALUE (chain
));
2711 /* Entries of this form are used for references to methods.
2712 The runtime re-writes these on start-up, but the compiler can't see
2713 that and optimizes it away unless we force it. */
2714 DECL_PRESERVE_P (decl
) = 1;
2715 finish_var_decl (decl
, expr
);
2721 build_gnu_selector_translation_table (void)
2725 tree decl = NULL_TREE;*/
2726 VEC(constructor_elt
,gc
) *inits
= NULL
;
2728 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
2732 if (warn_selector
&& objc_implementation_context
)
2733 diagnose_missing_method (TREE_VALUE (chain
), input_location
);
2735 expr
= build_selector (TREE_VALUE (chain
));
2736 /* add one for the '\0' character
2737 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
2740 if (flag_typed_selectors
)
2742 VEC(constructor_elt
,gc
) *v
= NULL
;
2743 tree encoding
= get_proto_encoding (TREE_PURPOSE (chain
));
2744 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2745 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, encoding
);
2746 expr
= objc_build_constructor (objc_selector_template
, v
);
2749 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
2751 } /* each element in the chain */
2754 /* Cause the selector table (previously forward-declared)
2755 to be actually output. */
2758 if (flag_typed_selectors
)
2760 VEC(constructor_elt
,gc
) *v
= NULL
;
2761 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
2762 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
2763 expr
= objc_build_constructor (objc_selector_template
, v
);
2766 expr
= integer_zero_node
;
2768 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
2769 expr
= objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl
),
2771 finish_var_decl (UOBJC_SELECTOR_TABLE_decl
, expr
);
2776 get_proto_encoding (tree proto
)
2781 if (! METHOD_ENCODING (proto
))
2783 encoding
= encode_method_prototype (proto
);
2784 METHOD_ENCODING (proto
) = encoding
;
2787 encoding
= METHOD_ENCODING (proto
);
2789 return add_objc_string (encoding
, meth_var_types
);
2792 return build_int_cst (NULL_TREE
, 0);
2795 /* sel_ref_chain is a list whose "value" fields will be instances of
2796 identifier_node that represent the selector. LOC is the location of
2800 build_typed_selector_reference (location_t loc
, tree ident
, tree prototype
)
2802 tree
*chain
= &sel_ref_chain
;
2808 if (TREE_PURPOSE (*chain
) == prototype
&& TREE_VALUE (*chain
) == ident
)
2809 goto return_at_index
;
2812 chain
= &TREE_CHAIN (*chain
);
2815 *chain
= tree_cons (prototype
, ident
, NULL_TREE
);
2818 expr
= build_unary_op (loc
, ADDR_EXPR
,
2819 build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2820 build_int_cst (NULL_TREE
, index
)),
2822 return convert (objc_selector_type
, expr
);
2826 build_selector_reference (location_t loc
, tree ident
)
2828 tree
*chain
= &sel_ref_chain
;
2834 if (TREE_VALUE (*chain
) == ident
)
2835 return (flag_next_runtime
2836 ? TREE_PURPOSE (*chain
)
2837 : build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2838 build_int_cst (NULL_TREE
, index
)));
2841 chain
= &TREE_CHAIN (*chain
);
2844 expr
= (flag_next_runtime
? build_selector_reference_decl (): NULL_TREE
);
2846 *chain
= tree_cons (expr
, ident
, NULL_TREE
);
2848 return (flag_next_runtime
2850 : build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2851 build_int_cst (NULL_TREE
, index
)));
2854 static GTY(()) int class_reference_idx
;
2857 build_class_reference_decl (void)
2862 sprintf (buf
, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx
++);
2863 decl
= start_var_decl (objc_class_type
, buf
);
2868 /* Create a class reference, but don't create a variable to reference
2872 add_class_reference (tree ident
)
2876 if ((chain
= cls_ref_chain
))
2881 if (ident
== TREE_VALUE (chain
))
2885 chain
= TREE_CHAIN (chain
);
2889 /* Append to the end of the list */
2890 TREE_CHAIN (tail
) = tree_cons (NULL_TREE
, ident
, NULL_TREE
);
2893 cls_ref_chain
= tree_cons (NULL_TREE
, ident
, NULL_TREE
);
2896 /* Get a class reference, creating it if necessary. Also create the
2897 reference variable. */
2900 objc_get_class_reference (tree ident
)
2902 tree orig_ident
= (DECL_P (ident
)
2905 ? OBJC_TYPE_NAME (ident
)
2907 bool local_scope
= false;
2910 if (processing_template_decl
)
2911 /* Must wait until template instantiation time. */
2912 return build_min_nt (CLASS_REFERENCE_EXPR
, ident
);
2915 if (TREE_CODE (ident
) == TYPE_DECL
)
2916 ident
= (DECL_ORIGINAL_TYPE (ident
)
2917 ? DECL_ORIGINAL_TYPE (ident
)
2918 : TREE_TYPE (ident
));
2922 && CP_TYPE_CONTEXT (ident
) != global_namespace
)
2926 if (local_scope
|| !(ident
= objc_is_class_name (ident
)))
2928 error ("%qE is not an Objective-C class name or alias",
2930 return error_mark_node
;
2933 if (flag_next_runtime
&& !flag_zero_link
)
2938 for (chain
= &cls_ref_chain
; *chain
; chain
= &TREE_CHAIN (*chain
))
2939 if (TREE_VALUE (*chain
) == ident
)
2941 if (! TREE_PURPOSE (*chain
))
2942 TREE_PURPOSE (*chain
) = build_class_reference_decl ();
2944 return TREE_PURPOSE (*chain
);
2947 decl
= build_class_reference_decl ();
2948 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
2955 add_class_reference (ident
);
2957 params
= build_tree_list (NULL_TREE
,
2958 my_build_string_pointer
2959 (IDENTIFIER_LENGTH (ident
) + 1,
2960 IDENTIFIER_POINTER (ident
)));
2962 assemble_external (objc_get_class_decl
);
2963 return build_function_call (input_location
, objc_get_class_decl
, params
);
2967 /* For each string section we have a chain which maps identifier nodes
2968 to decls for the strings. */
2970 static GTY(()) int class_names_idx
;
2971 static GTY(()) int meth_var_names_idx
;
2972 static GTY(()) int meth_var_types_idx
;
2975 add_objc_string (tree ident
, enum string_section section
)
2977 tree
*chain
, decl
, type
, string_expr
;
2981 if (section
== class_names
)
2983 chain
= &class_names_chain
;
2984 sprintf (buf
, "_OBJC_CLASS_NAME_%d", class_names_idx
++);
2986 else if (section
== meth_var_names
)
2988 chain
= &meth_var_names_chain
;
2989 sprintf (buf
, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx
++);
2991 else if (section
== meth_var_types
)
2993 chain
= &meth_var_types_chain
;
2994 sprintf (buf
, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx
++);
3001 if (TREE_VALUE (*chain
) == ident
)
3002 return convert (string_type_node
,
3003 build_unary_op (input_location
,
3004 ADDR_EXPR
, TREE_PURPOSE (*chain
), 1));
3006 chain
= &TREE_CHAIN (*chain
);
3009 type
= build_sized_array_type (char_type_node
, IDENTIFIER_LENGTH (ident
) + 1);
3010 decl
= start_var_decl (type
, buf
);
3011 string_expr
= my_build_string (IDENTIFIER_LENGTH (ident
) + 1,
3012 IDENTIFIER_POINTER (ident
));
3013 TREE_CONSTANT (decl
) = 1;
3014 finish_var_decl (decl
, string_expr
);
3016 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
3018 return convert (string_type_node
, build_unary_op (input_location
,
3019 ADDR_EXPR
, decl
, 1));
3023 objc_declare_alias (tree alias_ident
, tree class_ident
)
3025 tree underlying_class
;
3028 if (current_namespace
!= global_namespace
) {
3029 error ("Objective-C declarations may only appear in global scope");
3031 #endif /* OBJCPLUS */
3033 if (!(underlying_class
= objc_is_class_name (class_ident
)))
3034 warning (0, "cannot find class %qE", class_ident
);
3035 else if (objc_is_class_name (alias_ident
))
3036 warning (0, "class %qE already exists", alias_ident
);
3039 /* Implement @compatibility_alias as a typedef. */
3041 push_lang_context (lang_name_c
); /* extern "C" */
3043 lang_hooks
.decls
.pushdecl (build_decl
3047 xref_tag (RECORD_TYPE
, underlying_class
)));
3049 pop_lang_context ();
3051 alias_chain
= tree_cons (underlying_class
, alias_ident
, alias_chain
);
3056 objc_declare_class (tree ident_list
)
3060 if (current_namespace
!= global_namespace
) {
3061 error ("Objective-C declarations may only appear in global scope");
3063 #endif /* OBJCPLUS */
3065 for (list
= ident_list
; list
; list
= TREE_CHAIN (list
))
3067 tree ident
= TREE_VALUE (list
);
3069 if (! objc_is_class_name (ident
))
3071 tree record
= lookup_name (ident
), type
= record
;
3075 if (TREE_CODE (record
) == TYPE_DECL
)
3076 type
= DECL_ORIGINAL_TYPE (record
);
3078 if (!TYPE_HAS_OBJC_INFO (type
)
3079 || !TYPE_OBJC_INTERFACE (type
))
3081 error ("%qE redeclared as different kind of symbol",
3083 error ("previous declaration of %q+D",
3088 record
= xref_tag (RECORD_TYPE
, ident
);
3089 INIT_TYPE_OBJC_INFO (record
);
3090 TYPE_OBJC_INTERFACE (record
) = ident
;
3091 class_chain
= tree_cons (NULL_TREE
, ident
, class_chain
);
3097 objc_is_class_name (tree ident
)
3101 if (ident
&& TREE_CODE (ident
) == IDENTIFIER_NODE
3102 && identifier_global_value (ident
))
3103 ident
= identifier_global_value (ident
);
3104 while (ident
&& TREE_CODE (ident
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (ident
))
3105 ident
= OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident
));
3107 if (ident
&& TREE_CODE (ident
) == RECORD_TYPE
)
3108 ident
= OBJC_TYPE_NAME (ident
);
3110 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3111 ident
= DECL_NAME (ident
);
3113 if (!ident
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3116 if (lookup_interface (ident
))
3119 for (chain
= class_chain
; chain
; chain
= TREE_CHAIN (chain
))
3121 if (ident
== TREE_VALUE (chain
))
3125 for (chain
= alias_chain
; chain
; chain
= TREE_CHAIN (chain
))
3127 if (ident
== TREE_VALUE (chain
))
3128 return TREE_PURPOSE (chain
);
3134 /* Check whether TYPE is either 'id' or 'Class'. */
3137 objc_is_id (tree type
)
3139 if (type
&& TREE_CODE (type
) == IDENTIFIER_NODE
3140 && identifier_global_value (type
))
3141 type
= identifier_global_value (type
);
3143 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
3144 type
= TREE_TYPE (type
);
3146 /* NB: This function may be called before the ObjC front-end has
3147 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3148 return (objc_object_type
&& type
3149 && (IS_ID (type
) || IS_CLASS (type
) || IS_SUPER (type
))
3154 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3155 class instance. This is needed by other parts of the compiler to
3156 handle ObjC types gracefully. */
3159 objc_is_object_ptr (tree type
)
3163 type
= TYPE_MAIN_VARIANT (type
);
3164 if (!POINTER_TYPE_P (type
))
3167 ret
= objc_is_id (type
);
3169 ret
= objc_is_class_name (TREE_TYPE (type
));
3175 objc_is_gcable_type (tree type
, int or_strong_p
)
3181 if (objc_is_id (TYPE_MAIN_VARIANT (type
)))
3183 if (or_strong_p
&& lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type
)))
3185 if (TREE_CODE (type
) != POINTER_TYPE
&& TREE_CODE (type
) != INDIRECT_REF
)
3187 type
= TREE_TYPE (type
);
3188 if (TREE_CODE (type
) != RECORD_TYPE
)
3190 name
= TYPE_NAME (type
);
3191 return (objc_is_class_name (name
) != NULL_TREE
);
3195 objc_substitute_decl (tree expr
, tree oldexpr
, tree newexpr
)
3197 if (expr
== oldexpr
)
3200 switch (TREE_CODE (expr
))
3203 return objc_build_component_ref
3204 (objc_substitute_decl (TREE_OPERAND (expr
, 0),
3207 DECL_NAME (TREE_OPERAND (expr
, 1)));
3209 return build_array_ref (input_location
,
3210 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3213 TREE_OPERAND (expr
, 1));
3215 return build_indirect_ref (input_location
,
3216 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3218 newexpr
), RO_ARROW
);
3225 objc_build_ivar_assignment (tree outervar
, tree lhs
, tree rhs
)
3228 /* The LHS parameter contains the expression 'outervar->memberspec';
3229 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3230 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3233 = objc_substitute_decl
3234 (lhs
, outervar
, convert (TREE_TYPE (outervar
), integer_zero_node
));
3236 = (flag_objc_direct_dispatch
3237 ? objc_assign_ivar_fast_decl
3238 : objc_assign_ivar_decl
);
3240 offs
= convert (integer_type_node
, build_unary_op (input_location
,
3241 ADDR_EXPR
, offs
, 0));
3243 func_params
= tree_cons (NULL_TREE
,
3244 convert (objc_object_type
, rhs
),
3245 tree_cons (NULL_TREE
, convert (objc_object_type
, outervar
),
3246 tree_cons (NULL_TREE
, offs
,
3249 assemble_external (func
);
3250 return build_function_call (input_location
, func
, func_params
);
3254 objc_build_global_assignment (tree lhs
, tree rhs
)
3256 tree func_params
= tree_cons (NULL_TREE
,
3257 convert (objc_object_type
, rhs
),
3258 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3259 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3262 assemble_external (objc_assign_global_decl
);
3263 return build_function_call (input_location
,
3264 objc_assign_global_decl
, func_params
);
3268 objc_build_strong_cast_assignment (tree lhs
, tree rhs
)
3270 tree func_params
= tree_cons (NULL_TREE
,
3271 convert (objc_object_type
, rhs
),
3272 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3273 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3276 assemble_external (objc_assign_strong_cast_decl
);
3277 return build_function_call (input_location
,
3278 objc_assign_strong_cast_decl
, func_params
);
3282 objc_is_gcable_p (tree expr
)
3284 return (TREE_CODE (expr
) == COMPONENT_REF
3285 ? objc_is_gcable_p (TREE_OPERAND (expr
, 1))
3286 : TREE_CODE (expr
) == ARRAY_REF
3287 ? (objc_is_gcable_p (TREE_TYPE (expr
))
3288 || objc_is_gcable_p (TREE_OPERAND (expr
, 0)))
3289 : TREE_CODE (expr
) == ARRAY_TYPE
3290 ? objc_is_gcable_p (TREE_TYPE (expr
))
3292 ? objc_is_gcable_type (expr
, 1)
3293 : (objc_is_gcable_p (TREE_TYPE (expr
))
3295 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr
)))));
3299 objc_is_ivar_reference_p (tree expr
)
3301 return (TREE_CODE (expr
) == ARRAY_REF
3302 ? objc_is_ivar_reference_p (TREE_OPERAND (expr
, 0))
3303 : TREE_CODE (expr
) == COMPONENT_REF
3304 ? TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
3309 objc_is_global_reference_p (tree expr
)
3311 return (TREE_CODE (expr
) == INDIRECT_REF
|| TREE_CODE (expr
) == PLUS_EXPR
3312 ? objc_is_global_reference_p (TREE_OPERAND (expr
, 0))
3314 ? (DECL_FILE_SCOPE_P (expr
) || TREE_STATIC (expr
))
3319 objc_generate_write_barrier (tree lhs
, enum tree_code modifycode
, tree rhs
)
3321 tree result
= NULL_TREE
, outer
;
3322 int strong_cast_p
= 0, outer_gc_p
= 0, indirect_p
= 0;
3324 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3325 will have been transformed to the form '*(type *)&expr'. */
3326 if (TREE_CODE (lhs
) == INDIRECT_REF
)
3328 outer
= TREE_OPERAND (lhs
, 0);
3330 while (!strong_cast_p
3331 && (CONVERT_EXPR_P (outer
)
3332 || TREE_CODE (outer
) == NON_LVALUE_EXPR
))
3334 tree lhstype
= TREE_TYPE (outer
);
3336 /* Descend down the cast chain, and record the first objc_gc
3338 if (POINTER_TYPE_P (lhstype
))
3341 = lookup_attribute ("objc_gc",
3342 TYPE_ATTRIBUTES (TREE_TYPE (lhstype
)));
3348 outer
= TREE_OPERAND (outer
, 0);
3352 /* If we have a __strong cast, it trumps all else. */
3355 if (modifycode
!= NOP_EXPR
)
3356 goto invalid_pointer_arithmetic
;
3358 if (warn_assign_intercept
)
3359 warning (0, "strong-cast assignment has been intercepted");
3361 result
= objc_build_strong_cast_assignment (lhs
, rhs
);
3366 /* the lhs must be of a suitable type, regardless of its underlying
3368 if (!objc_is_gcable_p (lhs
))
3374 && (TREE_CODE (outer
) == COMPONENT_REF
3375 || TREE_CODE (outer
) == ARRAY_REF
))
3376 outer
= TREE_OPERAND (outer
, 0);
3378 if (TREE_CODE (outer
) == INDIRECT_REF
)
3380 outer
= TREE_OPERAND (outer
, 0);
3384 outer_gc_p
= objc_is_gcable_p (outer
);
3386 /* Handle ivar assignments. */
3387 if (objc_is_ivar_reference_p (lhs
))
3389 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3390 doesn't cut it here), the best we can do here is suggest a cast. */
3391 if (!objc_is_gcable_type (TREE_TYPE (outer
), 0))
3393 /* We may still be able to use the global write barrier... */
3394 if (!indirect_p
&& objc_is_global_reference_p (outer
))
3395 goto global_reference
;
3398 if (modifycode
== NOP_EXPR
)
3400 if (warn_assign_intercept
)
3401 warning (0, "strong-cast may possibly be needed");
3407 if (modifycode
!= NOP_EXPR
)
3408 goto invalid_pointer_arithmetic
;
3410 if (warn_assign_intercept
)
3411 warning (0, "instance variable assignment has been intercepted");
3413 result
= objc_build_ivar_assignment (outer
, lhs
, rhs
);
3418 /* Likewise, intercept assignment to global/static variables if their type is
3420 if (objc_is_global_reference_p (outer
))
3426 if (modifycode
!= NOP_EXPR
)
3428 invalid_pointer_arithmetic
:
3430 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3435 if (warn_assign_intercept
)
3436 warning (0, "global/static variable assignment has been intercepted");
3438 result
= objc_build_global_assignment (lhs
, rhs
);
3441 /* In all other cases, fall back to the normal mechanism. */
3446 struct GTY(()) interface_tuple
{
3451 static GTY ((param_is (struct interface_tuple
))) htab_t interface_htab
;
3454 hash_interface (const void *p
)
3456 const struct interface_tuple
*d
= (const struct interface_tuple
*) p
;
3457 return IDENTIFIER_HASH_VALUE (d
->id
);
3461 eq_interface (const void *p1
, const void *p2
)
3463 const struct interface_tuple
*d
= (const struct interface_tuple
*) p1
;
3468 lookup_interface (tree ident
)
3471 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3472 ident
= DECL_NAME (ident
);
3475 if (ident
== NULL_TREE
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3479 struct interface_tuple
**slot
;
3484 slot
= (struct interface_tuple
**)
3485 htab_find_slot_with_hash (interface_htab
, ident
,
3486 IDENTIFIER_HASH_VALUE (ident
),
3489 i
= (*slot
)->class_name
;
3495 /* Implement @defs (<classname>) within struct bodies. */
3498 objc_get_class_ivars (tree class_name
)
3500 tree interface
= lookup_interface (class_name
);
3503 return get_class_ivars (interface
, true);
3505 error ("cannot find interface declaration for %qE",
3508 return error_mark_node
;
3511 /* Called when checking the variables in a struct. If we are not
3512 doing the ivars list inside an @interface context, then returns
3513 fieldlist unchanged. Else, returns the list of class ivars.
3516 objc_get_interface_ivars (tree fieldlist
)
3518 if (!objc_collecting_ivars
|| !objc_interface_context
3519 || TREE_CODE (objc_interface_context
) != CLASS_INTERFACE_TYPE
3520 || CLASS_SUPER_NAME (objc_interface_context
) == NULL_TREE
)
3523 return get_class_ivars (objc_interface_context
, true);
3526 /* Used by: build_private_template, continue_class,
3527 and for @defs constructs. */
3530 get_class_ivars (tree interface
, bool inherited
)
3532 tree ivar_chain
= copy_list (CLASS_RAW_IVARS (interface
));
3534 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
3535 by the current class (i.e., they do not include super-class ivars).
3536 However, the CLASS_IVARS list will be side-effected by a call to
3537 finish_struct(), which will fill in field offsets. */
3538 if (!CLASS_IVARS (interface
))
3539 CLASS_IVARS (interface
) = ivar_chain
;
3544 while (CLASS_SUPER_NAME (interface
))
3546 /* Prepend super-class ivars. */
3547 interface
= lookup_interface (CLASS_SUPER_NAME (interface
));
3548 ivar_chain
= chainon (copy_list (CLASS_RAW_IVARS (interface
)),
3556 objc_create_temporary_var (tree type
)
3560 decl
= build_decl (input_location
,
3561 VAR_DECL
, NULL_TREE
, type
);
3562 TREE_USED (decl
) = 1;
3563 DECL_ARTIFICIAL (decl
) = 1;
3564 DECL_IGNORED_P (decl
) = 1;
3565 DECL_CONTEXT (decl
) = current_function_decl
;
3570 /* Exception handling constructs. We begin by having the parser do most
3571 of the work and passing us blocks. What we do next depends on whether
3572 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
3573 We abstract all of this in a handful of appropriately named routines. */
3575 /* Stack of open try blocks. */
3577 struct objc_try_context
3579 struct objc_try_context
*outer
;
3581 /* Statements (or statement lists) as processed by the parser. */
3585 /* Some file position locations. */
3586 location_t try_locus
;
3587 location_t end_try_locus
;
3588 location_t end_catch_locus
;
3589 location_t finally_locus
;
3590 location_t end_finally_locus
;
3592 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
3593 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
3596 /* The CATCH_EXPR of an open @catch clause. */
3599 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
3605 static struct objc_try_context
*cur_try_context
;
3607 static GTY(()) tree objc_eh_personality_decl
;
3609 /* This hook, called via lang_eh_runtime_type, generates a runtime object
3610 that represents TYPE. For Objective-C, this is just the class name. */
3611 /* ??? Isn't there a class object or some such? Is it easy to get? */
3615 objc_eh_runtime_type (tree type
)
3617 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type
)), class_names
);
3621 objc_eh_personality (void)
3623 if (!flag_objc_sjlj_exceptions
3624 && !objc_eh_personality_decl
)
3625 objc_eh_personality_decl
3626 = build_personality_function (USING_SJLJ_EXCEPTIONS
3627 ? "__gnu_objc_personality_sj0"
3628 : "__gnu_objc_personality_v0");
3630 return objc_eh_personality_decl
;
3634 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
3635 of Darwin, we'll arrange for it to be initialized (and associated
3636 with a binding) later. */
3639 objc_build_exc_ptr (void)
3641 if (flag_objc_sjlj_exceptions
)
3643 tree var
= cur_try_context
->caught_decl
;
3646 var
= objc_create_temporary_var (objc_object_type
);
3647 cur_try_context
->caught_decl
= var
;
3654 t
= built_in_decls
[BUILT_IN_EH_POINTER
];
3655 t
= build_call_expr (t
, 1, integer_zero_node
);
3656 return fold_convert (objc_object_type
, t
);
3660 /* Build "objc_exception_try_exit(&_stack)". */
3663 next_sjlj_build_try_exit (void)
3666 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3667 t
= tree_cons (NULL
, t
, NULL
);
3668 t
= build_function_call (input_location
,
3669 objc_exception_try_exit_decl
, t
);
3674 objc_exception_try_enter (&_stack);
3675 if (_setjmp(&_stack.buf))
3679 Return the COND_EXPR. Note that the THEN and ELSE fields are left
3680 empty, ready for the caller to fill them in. */
3683 next_sjlj_build_enter_and_setjmp (void)
3685 tree t
, enter
, sj
, cond
;
3687 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3688 t
= tree_cons (NULL
, t
, NULL
);
3689 enter
= build_function_call (input_location
,
3690 objc_exception_try_enter_decl
, t
);
3692 t
= objc_build_component_ref (cur_try_context
->stack_decl
,
3693 get_identifier ("buf"));
3694 t
= build_fold_addr_expr_loc (input_location
, t
);
3696 /* Convert _setjmp argument to type that is expected. */
3697 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl
)))
3698 t
= convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl
))), t
);
3700 t
= convert (ptr_type_node
, t
);
3702 t
= convert (ptr_type_node
, t
);
3704 t
= tree_cons (NULL
, t
, NULL
);
3705 sj
= build_function_call (input_location
,
3706 objc_setjmp_decl
, t
);
3708 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (sj
), enter
, sj
);
3709 cond
= c_common_truthvalue_conversion (input_location
, cond
);
3711 return build3 (COND_EXPR
, void_type_node
, cond
, NULL
, NULL
);
3716 DECL = objc_exception_extract(&_stack); */
3719 next_sjlj_build_exc_extract (tree decl
)
3723 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3724 t
= tree_cons (NULL
, t
, NULL
);
3725 t
= build_function_call (input_location
,
3726 objc_exception_extract_decl
, t
);
3727 t
= convert (TREE_TYPE (decl
), t
);
3728 t
= build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
3734 if (objc_exception_match(obj_get_class(TYPE), _caught)
3741 objc_exception_try_exit(&_stack);
3743 from the sequence of CATCH_EXPRs in the current try context. */
3746 next_sjlj_build_catch_list (void)
3748 tree_stmt_iterator i
= tsi_start (cur_try_context
->catch_list
);
3750 tree
*last
= &catch_seq
;
3751 bool saw_id
= false;
3753 for (; !tsi_end_p (i
); tsi_next (&i
))
3755 tree stmt
= tsi_stmt (i
);
3756 tree type
= CATCH_TYPES (stmt
);
3757 tree body
= CATCH_BODY (stmt
);
3769 if (type
== error_mark_node
)
3770 cond
= error_mark_node
;
3773 args
= tree_cons (NULL
, cur_try_context
->caught_decl
, NULL
);
3774 t
= objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type
)));
3775 args
= tree_cons (NULL
, t
, args
);
3776 t
= build_function_call (input_location
,
3777 objc_exception_match_decl
, args
);
3778 cond
= c_common_truthvalue_conversion (input_location
, t
);
3780 t
= build3 (COND_EXPR
, void_type_node
, cond
, body
, NULL
);
3781 SET_EXPR_LOCATION (t
, EXPR_LOCATION (stmt
));
3784 last
= &COND_EXPR_ELSE (t
);
3790 t
= build2 (MODIFY_EXPR
, void_type_node
, cur_try_context
->rethrow_decl
,
3791 cur_try_context
->caught_decl
);
3792 SET_EXPR_LOCATION (t
, cur_try_context
->end_catch_locus
);
3793 append_to_statement_list (t
, last
);
3795 t
= next_sjlj_build_try_exit ();
3796 SET_EXPR_LOCATION (t
, cur_try_context
->end_catch_locus
);
3797 append_to_statement_list (t
, last
);
3803 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
3804 exception handling. We aim to build:
3807 struct _objc_exception_data _stack;
3811 objc_exception_try_enter (&_stack);
3812 if (_setjmp(&_stack.buf))
3814 id _caught = objc_exception_extract(&_stack);
3815 objc_exception_try_enter (&_stack);
3816 if (_setjmp(&_stack.buf))
3817 _rethrow = objc_exception_extract(&_stack);
3827 objc_exception_try_exit(&_stack);
3830 objc_exception_throw(_rethrow);
3834 If CATCH-LIST is empty, we can omit all of the block containing
3835 "_caught" except for the setting of _rethrow. Note the use of
3836 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
3837 but handles goto and other exits from the block. */
3840 next_sjlj_build_try_catch_finally (void)
3842 tree rethrow_decl
, stack_decl
, t
;
3843 tree catch_seq
, try_fin
, bind
;
3845 /* Create the declarations involved. */
3846 t
= xref_tag (RECORD_TYPE
, get_identifier (UTAG_EXCDATA
));
3847 stack_decl
= objc_create_temporary_var (t
);
3848 cur_try_context
->stack_decl
= stack_decl
;
3850 rethrow_decl
= objc_create_temporary_var (objc_object_type
);
3851 cur_try_context
->rethrow_decl
= rethrow_decl
;
3852 TREE_CHAIN (rethrow_decl
) = stack_decl
;
3854 /* Build the outermost variable binding level. */
3855 bind
= build3 (BIND_EXPR
, void_type_node
, rethrow_decl
, NULL
, NULL
);
3856 SET_EXPR_LOCATION (bind
, cur_try_context
->try_locus
);
3857 TREE_SIDE_EFFECTS (bind
) = 1;
3859 /* Initialize rethrow_decl. */
3860 t
= build2 (MODIFY_EXPR
, void_type_node
, rethrow_decl
,
3861 convert (objc_object_type
, null_pointer_node
));
3862 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
3863 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
3865 /* Build the outermost TRY_FINALLY_EXPR. */
3866 try_fin
= build2 (TRY_FINALLY_EXPR
, void_type_node
, NULL
, NULL
);
3867 SET_EXPR_LOCATION (try_fin
, cur_try_context
->try_locus
);
3868 TREE_SIDE_EFFECTS (try_fin
) = 1;
3869 append_to_statement_list (try_fin
, &BIND_EXPR_BODY (bind
));
3871 /* Create the complete catch sequence. */
3872 if (cur_try_context
->catch_list
)
3874 tree caught_decl
= objc_build_exc_ptr ();
3875 catch_seq
= build_stmt (input_location
, BIND_EXPR
, caught_decl
, NULL
, NULL
);
3876 TREE_SIDE_EFFECTS (catch_seq
) = 1;
3878 t
= next_sjlj_build_exc_extract (caught_decl
);
3879 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
3881 t
= next_sjlj_build_enter_and_setjmp ();
3882 COND_EXPR_THEN (t
) = next_sjlj_build_exc_extract (rethrow_decl
);
3883 COND_EXPR_ELSE (t
) = next_sjlj_build_catch_list ();
3884 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
3887 catch_seq
= next_sjlj_build_exc_extract (rethrow_decl
);
3888 SET_EXPR_LOCATION (catch_seq
, cur_try_context
->end_try_locus
);
3890 /* Build the main register-and-try if statement. */
3891 t
= next_sjlj_build_enter_and_setjmp ();
3892 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
3893 COND_EXPR_THEN (t
) = catch_seq
;
3894 COND_EXPR_ELSE (t
) = cur_try_context
->try_body
;
3895 TREE_OPERAND (try_fin
, 0) = t
;
3897 /* Build the complete FINALLY statement list. */
3898 t
= next_sjlj_build_try_exit ();
3899 t
= build_stmt (input_location
, COND_EXPR
,
3900 c_common_truthvalue_conversion
3901 (input_location
, rethrow_decl
),
3903 SET_EXPR_LOCATION (t
, cur_try_context
->finally_locus
);
3904 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
3906 append_to_statement_list (cur_try_context
->finally_body
,
3907 &TREE_OPERAND (try_fin
, 1));
3909 t
= tree_cons (NULL
, rethrow_decl
, NULL
);
3910 t
= build_function_call (input_location
,
3911 objc_exception_throw_decl
, t
);
3912 t
= build_stmt (input_location
, COND_EXPR
,
3913 c_common_truthvalue_conversion (input_location
,
3916 SET_EXPR_LOCATION (t
, cur_try_context
->end_finally_locus
);
3917 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
3922 /* Called just after parsing the @try and its associated BODY. We now
3923 must prepare for the tricky bits -- handling the catches and finally. */
3926 objc_begin_try_stmt (location_t try_locus
, tree body
)
3928 struct objc_try_context
*c
= XCNEW (struct objc_try_context
);
3929 c
->outer
= cur_try_context
;
3931 c
->try_locus
= try_locus
;
3932 c
->end_try_locus
= input_location
;
3933 cur_try_context
= c
;
3935 /* -fobjc-exceptions is required to enable Objective-C exceptions.
3936 For example, on Darwin, ObjC exceptions require a sufficiently
3937 recent version of the runtime, so the user must ask for them
3938 explicitly. On other platforms, at the moment -fobjc-exceptions
3939 triggers -fexceptions which again is required for exceptions to
3942 if (!flag_objc_exceptions
)
3944 error_at (try_locus
, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
3947 if (flag_objc_sjlj_exceptions
)
3948 objc_mark_locals_volatile (NULL
);
3951 /* Called just after parsing "@catch (parm)". Open a binding level,
3952 enter DECL into the binding level, and initialize it. Leave the
3953 binding level open while the body of the compound statement is parsed. */
3956 objc_begin_catch_clause (tree decl
)
3958 tree compound
, type
, t
;
3960 /* Begin a new scope that the entire catch clause will live in. */
3961 compound
= c_begin_compound_stmt (true);
3963 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
3964 decl
= build_decl (input_location
,
3965 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
3966 lang_hooks
.decls
.pushdecl (decl
);
3968 /* Since a decl is required here by syntax, don't warn if its unused. */
3969 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
3970 be what the previous objc implementation did. */
3971 TREE_USED (decl
) = 1;
3972 DECL_READ_P (decl
) = 1;
3974 /* Verify that the type of the catch is valid. It must be a pointer
3975 to an Objective-C class, or "id" (which is catch-all). */
3976 type
= TREE_TYPE (decl
);
3978 if (POINTER_TYPE_P (type
) && objc_is_object_id (TREE_TYPE (type
)))
3980 else if (!POINTER_TYPE_P (type
) || !TYPED_OBJECT (TREE_TYPE (type
)))
3982 error ("@catch parameter is not a known Objective-C class type");
3983 type
= error_mark_node
;
3985 else if (cur_try_context
->catch_list
)
3987 /* Examine previous @catch clauses and see if we've already
3988 caught the type in question. */
3989 tree_stmt_iterator i
= tsi_start (cur_try_context
->catch_list
);
3990 for (; !tsi_end_p (i
); tsi_next (&i
))
3992 tree stmt
= tsi_stmt (i
);
3993 t
= CATCH_TYPES (stmt
);
3994 if (t
== error_mark_node
)
3996 if (!t
|| DERIVED_FROM_P (TREE_TYPE (t
), TREE_TYPE (type
)))
3998 warning (0, "exception of type %<%T%> will be caught",
4000 warning_at (EXPR_LOCATION (stmt
), 0, " by earlier handler for %<%T%>",
4001 TREE_TYPE (t
? t
: objc_object_type
));
4007 /* Record the data for the catch in the try context so that we can
4008 finalize it later. */
4009 t
= build_stmt (input_location
, CATCH_EXPR
, type
, compound
);
4010 cur_try_context
->current_catch
= t
;
4012 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
4013 t
= objc_build_exc_ptr ();
4014 t
= convert (TREE_TYPE (decl
), t
);
4015 t
= build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
4019 /* Called just after parsing the closing brace of a @catch clause. Close
4020 the open binding level, and record a CATCH_EXPR for it. */
4023 objc_finish_catch_clause (void)
4025 tree c
= cur_try_context
->current_catch
;
4026 cur_try_context
->current_catch
= NULL
;
4027 cur_try_context
->end_catch_locus
= input_location
;
4029 CATCH_BODY (c
) = c_end_compound_stmt (input_location
, CATCH_BODY (c
), 1);
4030 append_to_statement_list (c
, &cur_try_context
->catch_list
);
4033 /* Called after parsing a @finally clause and its associated BODY.
4034 Record the body for later placement. */
4037 objc_build_finally_clause (location_t finally_locus
, tree body
)
4039 cur_try_context
->finally_body
= body
;
4040 cur_try_context
->finally_locus
= finally_locus
;
4041 cur_try_context
->end_finally_locus
= input_location
;
4044 /* Called to finalize a @try construct. */
4047 objc_finish_try_stmt (void)
4049 struct objc_try_context
*c
= cur_try_context
;
4052 if (c
->catch_list
== NULL
&& c
->finally_body
== NULL
)
4053 error ("%<@try%> without %<@catch%> or %<@finally%>");
4055 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
4056 if (flag_objc_sjlj_exceptions
)
4058 bool save
= in_late_binary_op
;
4059 in_late_binary_op
= true;
4060 if (!cur_try_context
->finally_body
)
4062 cur_try_context
->finally_locus
= input_location
;
4063 cur_try_context
->end_finally_locus
= input_location
;
4065 stmt
= next_sjlj_build_try_catch_finally ();
4066 in_late_binary_op
= save
;
4070 /* Otherwise, nest the CATCH inside a FINALLY. */
4074 stmt
= build_stmt (input_location
, TRY_CATCH_EXPR
, stmt
, c
->catch_list
);
4075 SET_EXPR_LOCATION (stmt
, cur_try_context
->try_locus
);
4077 if (c
->finally_body
)
4079 stmt
= build_stmt (input_location
, TRY_FINALLY_EXPR
, stmt
, c
->finally_body
);
4080 SET_EXPR_LOCATION (stmt
, cur_try_context
->try_locus
);
4085 cur_try_context
= c
->outer
;
4091 objc_build_throw_stmt (location_t loc
, tree throw_expr
)
4095 if (!flag_objc_exceptions
)
4097 error_at (loc
, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4100 if (throw_expr
== NULL
)
4102 /* If we're not inside a @catch block, there is no "current
4103 exception" to be rethrown. */
4104 if (cur_try_context
== NULL
4105 || cur_try_context
->current_catch
== NULL
)
4107 error_at (loc
, "%<@throw%> (rethrow) used outside of a @catch block");
4111 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4112 value that we get from the runtime. */
4113 throw_expr
= objc_build_exc_ptr ();
4116 /* A throw is just a call to the runtime throw function with the
4117 object as a parameter. */
4118 args
= tree_cons (NULL
, throw_expr
, NULL
);
4119 return add_stmt (build_function_call (loc
,
4120 objc_exception_throw_decl
, args
));
4124 objc_build_synchronized (location_t start_locus
, tree mutex
, tree body
)
4128 /* First lock the mutex. */
4129 mutex
= save_expr (mutex
);
4130 args
= tree_cons (NULL
, mutex
, NULL
);
4131 call
= build_function_call (input_location
,
4132 objc_sync_enter_decl
, args
);
4133 SET_EXPR_LOCATION (call
, start_locus
);
4136 /* Build the mutex unlock. */
4137 args
= tree_cons (NULL
, mutex
, NULL
);
4138 call
= build_function_call (input_location
,
4139 objc_sync_exit_decl
, args
);
4140 SET_EXPR_LOCATION (call
, input_location
);
4142 /* Put the that and the body in a TRY_FINALLY. */
4143 objc_begin_try_stmt (start_locus
, body
);
4144 objc_build_finally_clause (input_location
, call
);
4145 return objc_finish_try_stmt ();
4149 /* Predefine the following data type:
4151 struct _objc_exception_data
4153 int buf[OBJC_JBLEN];
4157 /* The following yuckiness should prevent users from having to #include
4158 <setjmp.h> in their code... */
4160 /* Define to a harmless positive value so the below code doesn't die. */
4162 #define OBJC_JBLEN 18
4166 build_next_objc_exception_stuff (void)
4168 tree decls
, temp_type
, *chain
= NULL
;
4170 objc_exception_data_template
4171 = objc_start_struct (get_identifier (UTAG_EXCDATA
));
4173 /* int buf[OBJC_JBLEN]; */
4175 temp_type
= build_sized_array_type (integer_type_node
, OBJC_JBLEN
);
4176 decls
= add_field_decl (temp_type
, "buf", &chain
);
4178 /* void *pointers[4]; */
4180 temp_type
= build_sized_array_type (ptr_type_node
, 4);
4181 add_field_decl (temp_type
, "pointers", &chain
);
4183 objc_finish_struct (objc_exception_data_template
, decls
);
4185 /* int _setjmp(...); */
4186 /* If the user includes <setjmp.h>, this shall be superseded by
4187 'int _setjmp(jmp_buf);' */
4188 temp_type
= build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
4190 = add_builtin_function (TAG_SETJMP
, temp_type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
4192 /* id objc_exception_extract(struct _objc_exception_data *); */
4194 = build_function_type_list (objc_object_type
,
4195 build_pointer_type (objc_exception_data_template
),
4197 objc_exception_extract_decl
4198 = add_builtin_function (TAG_EXCEPTIONEXTRACT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4200 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4201 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4203 = build_function_type_list (void_type_node
,
4204 build_pointer_type (objc_exception_data_template
),
4206 objc_exception_try_enter_decl
4207 = add_builtin_function (TAG_EXCEPTIONTRYENTER
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4209 objc_exception_try_exit_decl
4210 = add_builtin_function (TAG_EXCEPTIONTRYEXIT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4213 /* int objc_exception_match(id, id); */
4215 = build_function_type_list (integer_type_node
,
4216 objc_object_type
, objc_object_type
, NULL_TREE
);
4217 objc_exception_match_decl
4218 = add_builtin_function (TAG_EXCEPTIONMATCH
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4221 /* id objc_assign_ivar (id, id, unsigned int); */
4222 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4223 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4225 = build_function_type_list (objc_object_type
,
4230 objc_assign_ivar_decl
4231 = add_builtin_function (TAG_ASSIGNIVAR
, temp_type
, 0, NOT_BUILT_IN
,
4233 #ifdef OFFS_ASSIGNIVAR_FAST
4234 objc_assign_ivar_fast_decl
4235 = add_builtin_function (TAG_ASSIGNIVAR_FAST
, temp_type
, 0,
4236 NOT_BUILT_IN
, NULL
, NULL_TREE
);
4237 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl
)
4238 = tree_cons (get_identifier ("hard_coded_address"),
4239 build_int_cst (NULL_TREE
, OFFS_ASSIGNIVAR_FAST
),
4242 /* Default to slower ivar method. */
4243 objc_assign_ivar_fast_decl
= objc_assign_ivar_decl
;
4246 /* id objc_assign_global (id, id *); */
4247 /* id objc_assign_strongCast (id, id *); */
4248 temp_type
= build_function_type_list (objc_object_type
,
4250 build_pointer_type (objc_object_type
),
4252 objc_assign_global_decl
4253 = add_builtin_function (TAG_ASSIGNGLOBAL
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4255 objc_assign_strong_cast_decl
4256 = add_builtin_function (TAG_ASSIGNSTRONGCAST
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4261 build_objc_exception_stuff (void)
4263 tree noreturn_list
, nothrow_list
, temp_type
;
4265 noreturn_list
= tree_cons (get_identifier ("noreturn"), NULL
, NULL
);
4266 nothrow_list
= tree_cons (get_identifier ("nothrow"), NULL
, NULL
);
4268 /* void objc_exception_throw(id) __attribute__((noreturn)); */
4269 /* void objc_sync_enter(id); */
4270 /* void objc_sync_exit(id); */
4271 temp_type
= build_function_type_list (void_type_node
,
4274 objc_exception_throw_decl
4275 = add_builtin_function (TAG_EXCEPTIONTHROW
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4277 objc_sync_enter_decl
4278 = add_builtin_function (TAG_SYNCENTER
, temp_type
, 0, NOT_BUILT_IN
,
4279 NULL
, nothrow_list
);
4281 = add_builtin_function (TAG_SYNCEXIT
, temp_type
, 0, NOT_BUILT_IN
,
4282 NULL
, nothrow_list
);
4285 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4288 struct <classname> {
4289 struct _objc_class *isa;
4294 build_private_template (tree klass
)
4296 if (!CLASS_STATIC_TEMPLATE (klass
))
4298 tree record
= objc_build_struct (klass
,
4299 get_class_ivars (klass
, false),
4300 CLASS_SUPER_NAME (klass
));
4302 /* Set the TREE_USED bit for this struct, so that stab generator
4303 can emit stabs for this struct type. */
4304 if (flag_debug_only_used_symbols
&& TYPE_STUB_DECL (record
))
4305 TREE_USED (TYPE_STUB_DECL (record
)) = 1;
4309 /* Begin code generation for protocols... */
4311 /* struct _objc_protocol {
4312 struct _objc_class *isa;
4313 char *protocol_name;
4314 struct _objc_protocol **protocol_list;
4315 struct _objc__method_prototype_list *instance_methods;
4316 struct _objc__method_prototype_list *class_methods;
4320 build_protocol_template (void)
4322 tree ptype
, decls
, *chain
= NULL
;
4324 objc_protocol_template
= objc_start_struct (get_identifier (UTAG_PROTOCOL
));
4326 /* struct _objc_class *isa; */
4327 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
4328 get_identifier (UTAG_CLASS
)));
4329 decls
= add_field_decl (ptype
, "isa", &chain
);
4331 /* char *protocol_name; */
4332 add_field_decl (string_type_node
, "protocol_name", &chain
);
4334 /* struct _objc_protocol **protocol_list; */
4335 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
4336 add_field_decl (ptype
, "protocol_list", &chain
);
4338 /* struct _objc__method_prototype_list *instance_methods; */
4339 add_field_decl (objc_method_proto_list_ptr
, "instance_methods", &chain
);
4341 /* struct _objc__method_prototype_list *class_methods; */
4342 add_field_decl (objc_method_proto_list_ptr
, "class_methods", &chain
);
4344 objc_finish_struct (objc_protocol_template
, decls
);
4348 build_descriptor_table_initializer (tree type
, tree entries
)
4350 VEC(constructor_elt
,gc
) *inits
= NULL
;
4354 VEC(constructor_elt
,gc
) *elts
= NULL
;
4356 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
4357 build_selector (METHOD_SEL_NAME (entries
)));
4358 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
4359 add_objc_string (METHOD_ENCODING (entries
),
4362 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
4363 objc_build_constructor (type
, elts
));
4365 entries
= DECL_CHAIN (entries
);
4369 return objc_build_constructor (build_array_type (type
, 0), inits
);
4372 /* struct objc_method_prototype_list {
4374 struct objc_method_prototype {
4381 build_method_prototype_list_template (tree list_type
, int size
)
4383 tree objc_ivar_list_record
;
4384 tree array_type
, decls
, *chain
= NULL
;
4386 /* Generate an unnamed struct definition. */
4388 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
4390 /* int method_count; */
4391 decls
= add_field_decl (integer_type_node
, "method_count", &chain
);
4393 /* struct objc_method method_list[]; */
4394 array_type
= build_sized_array_type (list_type
, size
);
4395 add_field_decl (array_type
, "method_list", &chain
);
4397 objc_finish_struct (objc_ivar_list_record
, decls
);
4399 return objc_ivar_list_record
;
4403 build_method_prototype_template (void)
4406 tree decls
, *chain
= NULL
;
4408 proto_record
= objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE
));
4411 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
4413 /* char *method_types; */
4414 add_field_decl (string_type_node
, "method_types", &chain
);
4416 objc_finish_struct (proto_record
, decls
);
4418 return proto_record
;
4422 objc_method_parm_type (tree type
)
4424 type
= TREE_VALUE (TREE_TYPE (type
));
4425 if (TREE_CODE (type
) == TYPE_DECL
)
4426 type
= TREE_TYPE (type
);
4431 objc_encoded_type_size (tree type
)
4433 int sz
= int_size_in_bytes (type
);
4435 /* Make all integer and enum types at least as large
4437 if (sz
> 0 && INTEGRAL_TYPE_P (type
))
4438 sz
= MAX (sz
, int_size_in_bytes (integer_type_node
));
4439 /* Treat arrays as pointers, since that's how they're
4441 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4442 sz
= int_size_in_bytes (ptr_type_node
);
4446 /* Encode a method prototype.
4448 The format is described in gcc/doc/objc.texi, section 'Method
4452 encode_method_prototype (tree method_decl
)
4459 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
4460 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl
)));
4462 /* Encode return type. */
4463 encode_type (objc_method_parm_type (method_decl
),
4464 obstack_object_size (&util_obstack
),
4465 OBJC_ENCODE_INLINE_DEFS
);
4468 /* The first two arguments (self and _cmd) are pointers; account for
4470 i
= int_size_in_bytes (ptr_type_node
);
4471 parm_offset
= 2 * i
;
4472 for (parms
= METHOD_SEL_ARGS (method_decl
); parms
;
4473 parms
= DECL_CHAIN (parms
))
4475 tree type
= objc_method_parm_type (parms
);
4476 int sz
= objc_encoded_type_size (type
);
4478 /* If a type size is not known, bail out. */
4481 error ("type %q+D does not have a known size",
4483 /* Pretend that the encoding succeeded; the compilation will
4484 fail nevertheless. */
4485 goto finish_encoding
;
4490 sprintf (buf
, "%d@0:%d", parm_offset
, i
);
4491 obstack_grow (&util_obstack
, buf
, strlen (buf
));
4493 /* Argument types. */
4494 parm_offset
= 2 * i
;
4495 for (parms
= METHOD_SEL_ARGS (method_decl
); parms
;
4496 parms
= DECL_CHAIN (parms
))
4498 tree type
= objc_method_parm_type (parms
);
4500 /* Process argument qualifiers for user supplied arguments. */
4501 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms
)));
4504 encode_type (type
, obstack_object_size (&util_obstack
),
4505 OBJC_ENCODE_INLINE_DEFS
);
4507 /* Compute offset. */
4508 sprintf (buf
, "%d", parm_offset
);
4509 parm_offset
+= objc_encoded_type_size (type
);
4511 obstack_grow (&util_obstack
, buf
, strlen (buf
));
4515 obstack_1grow (&util_obstack
, '\0');
4516 result
= get_identifier (XOBFINISH (&util_obstack
, char *));
4517 obstack_free (&util_obstack
, util_firstobj
);
4522 generate_descriptor_table (tree type
, const char *name
, int size
, tree list
,
4526 VEC(constructor_elt
,gc
) *v
= NULL
;
4528 decl
= start_var_decl (type
, synth_id_with_class_suffix (name
, proto
));
4530 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
4531 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, list
);
4533 finish_var_decl (decl
, objc_build_constructor (type
, v
));
4539 generate_method_descriptors (tree protocol
)
4541 tree initlist
, chain
, method_list_template
;
4544 if (!objc_method_prototype_template
)
4545 objc_method_prototype_template
= build_method_prototype_template ();
4547 chain
= PROTOCOL_CLS_METHODS (protocol
);
4550 size
= list_length (chain
);
4552 method_list_template
4553 = build_method_prototype_list_template (objc_method_prototype_template
,
4557 = build_descriptor_table_initializer (objc_method_prototype_template
,
4560 UOBJC_CLASS_METHODS_decl
4561 = generate_descriptor_table (method_list_template
,
4562 "_OBJC_PROTOCOL_CLASS_METHODS",
4563 size
, initlist
, protocol
);
4566 UOBJC_CLASS_METHODS_decl
= 0;
4568 chain
= PROTOCOL_NST_METHODS (protocol
);
4571 size
= list_length (chain
);
4573 method_list_template
4574 = build_method_prototype_list_template (objc_method_prototype_template
,
4577 = build_descriptor_table_initializer (objc_method_prototype_template
,
4580 UOBJC_INSTANCE_METHODS_decl
4581 = generate_descriptor_table (method_list_template
,
4582 "_OBJC_PROTOCOL_INSTANCE_METHODS",
4583 size
, initlist
, protocol
);
4586 UOBJC_INSTANCE_METHODS_decl
= 0;
4590 generate_protocol_references (tree plist
)
4594 /* Forward declare protocols referenced. */
4595 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
4597 tree proto
= TREE_VALUE (lproto
);
4599 if (TREE_CODE (proto
) == PROTOCOL_INTERFACE_TYPE
4600 && PROTOCOL_NAME (proto
))
4602 if (! PROTOCOL_FORWARD_DECL (proto
))
4603 build_protocol_reference (proto
);
4605 if (PROTOCOL_LIST (proto
))
4606 generate_protocol_references (PROTOCOL_LIST (proto
));
4611 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4615 objc_generate_cxx_ctor_or_dtor (bool dtor
)
4617 tree fn
, body
, compound_stmt
, ivar
;
4619 /* - (id) .cxx_construct { ... return self; } */
4620 /* - (void) .cxx_construct { ... } */
4622 objc_set_method_type (MINUS_EXPR
);
4623 objc_start_method_definition
4624 (objc_build_method_signature (build_tree_list (NULL_TREE
,
4627 : objc_object_type
),
4628 get_identifier (dtor
4630 : TAG_CXX_CONSTRUCT
),
4631 make_node (TREE_LIST
),
4633 body
= begin_function_body ();
4634 compound_stmt
= begin_compound_stmt (0);
4636 ivar
= CLASS_IVARS (implementation_template
);
4637 /* Destroy ivars in reverse order. */
4639 ivar
= nreverse (copy_list (ivar
));
4641 for (; ivar
; ivar
= TREE_CHAIN (ivar
))
4643 if (TREE_CODE (ivar
) == FIELD_DECL
)
4645 tree type
= TREE_TYPE (ivar
);
4647 /* Call the ivar's default constructor or destructor. Do not
4648 call the destructor unless a corresponding constructor call
4649 has also been made (or is not needed). */
4650 if (MAYBE_CLASS_TYPE_P (type
)
4652 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4653 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4654 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4655 : (TYPE_NEEDS_CONSTRUCTING (type
)
4656 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))))
4658 (build_special_member_call
4659 (build_ivar_reference (DECL_NAME (ivar
)),
4660 dtor
? complete_dtor_identifier
: complete_ctor_identifier
,
4661 NULL
, type
, LOOKUP_NORMAL
, tf_warning_or_error
));
4665 /* The constructor returns 'self'. */
4667 finish_return_stmt (self_decl
);
4669 finish_compound_stmt (compound_stmt
);
4670 finish_function_body (body
);
4671 fn
= current_function_decl
;
4673 objc_finish_method_definition (fn
);
4676 /* The following routine will examine the current @interface for any
4677 non-POD C++ ivars requiring non-trivial construction and/or
4678 destruction, and then synthesize special '- .cxx_construct' and/or
4679 '- .cxx_destruct' methods which will run the appropriate
4680 construction or destruction code. Note that ivars inherited from
4681 super-classes are _not_ considered. */
4683 objc_generate_cxx_cdtors (void)
4685 bool need_ctor
= false, need_dtor
= false;
4688 /* We do not want to do this for categories, since they do not have
4691 if (TREE_CODE (objc_implementation_context
) != CLASS_IMPLEMENTATION_TYPE
)
4694 /* First, determine if we even need a constructor and/or destructor. */
4696 for (ivar
= CLASS_IVARS (implementation_template
); ivar
;
4697 ivar
= TREE_CHAIN (ivar
))
4699 if (TREE_CODE (ivar
) == FIELD_DECL
)
4701 tree type
= TREE_TYPE (ivar
);
4703 if (MAYBE_CLASS_TYPE_P (type
))
4705 if (TYPE_NEEDS_CONSTRUCTING (type
)
4706 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
4707 /* NB: If a default constructor is not available, we will not
4708 be able to initialize this ivar; the add_instance_variable()
4709 routine will already have warned about this. */
4712 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4713 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4714 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4715 /* NB: If a default constructor is not available, we will not
4716 call the destructor either, for symmetry. */
4722 /* Generate '- .cxx_construct' if needed. */
4725 objc_generate_cxx_ctor_or_dtor (false);
4727 /* Generate '- .cxx_destruct' if needed. */
4730 objc_generate_cxx_ctor_or_dtor (true);
4732 /* The 'imp_list' variable points at an imp_entry record for the current
4733 @implementation. Record the existence of '- .cxx_construct' and/or
4734 '- .cxx_destruct' methods therein; it will be included in the
4735 metadata for the class. */
4736 if (flag_next_runtime
)
4737 imp_list
->has_cxx_cdtors
= (need_ctor
|| need_dtor
);
4741 /* For each protocol which was referenced either from a @protocol()
4742 expression, or because a class/category implements it (then a
4743 pointer to the protocol is stored in the struct describing the
4744 class/category), we create a statically allocated instance of the
4745 Protocol class. The code is written in such a way as to generate
4746 as few Protocol objects as possible; we generate a unique Protocol
4747 instance for each protocol, and we don't generate a Protocol
4748 instance if the protocol is never referenced (either from a
4749 @protocol() or from a class/category implementation). These
4750 statically allocated objects can be referred to via the static
4751 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
4753 The statically allocated Protocol objects that we generate here
4754 need to be fixed up at runtime in order to be used: the 'isa'
4755 pointer of the objects need to be set up to point to the 'Protocol'
4756 class, as known at runtime.
4758 The NeXT runtime fixes up all protocols at program startup time,
4759 before main() is entered. It uses a low-level trick to look up all
4760 those symbols, then loops on them and fixes them up.
4762 The GNU runtime as well fixes up all protocols before user code
4763 from the module is executed; it requires pointers to those symbols
4764 to be put in the objc_symtab (which is then passed as argument to
4765 the function __objc_exec_class() which the compiler sets up to be
4766 executed automatically when the module is loaded); setup of those
4767 Protocol objects happen in two ways in the GNU runtime: all
4768 Protocol objects referred to by a class or category implementation
4769 are fixed up when the class/category is loaded; all Protocol
4770 objects referred to by a @protocol() expression are added by the
4771 compiler to the list of statically allocated instances to fixup
4772 (the same list holding the statically allocated constant string
4773 objects). Because, as explained above, the compiler generates as
4774 few Protocol objects as possible, some Protocol object might end up
4775 being referenced multiple times when compiled with the GNU runtime,
4776 and end up being fixed up multiple times at runtime initialization.
4777 But that doesn't hurt, it's just a little inefficient. */
4780 generate_protocols (void)
4784 tree initlist
, protocol_name_expr
, refs_decl
, refs_expr
;
4786 /* If a protocol was directly referenced, pull in indirect references. */
4787 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
4788 if (PROTOCOL_FORWARD_DECL (p
) && PROTOCOL_LIST (p
))
4789 generate_protocol_references (PROTOCOL_LIST (p
));
4791 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
4793 tree nst_methods
= PROTOCOL_NST_METHODS (p
);
4794 tree cls_methods
= PROTOCOL_CLS_METHODS (p
);
4796 /* If protocol wasn't referenced, don't generate any code. */
4797 decl
= PROTOCOL_FORWARD_DECL (p
);
4802 /* Make sure we link in the Protocol class. */
4803 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME
));
4807 if (! METHOD_ENCODING (nst_methods
))
4809 encoding
= encode_method_prototype (nst_methods
);
4810 METHOD_ENCODING (nst_methods
) = encoding
;
4812 nst_methods
= DECL_CHAIN (nst_methods
);
4817 if (! METHOD_ENCODING (cls_methods
))
4819 encoding
= encode_method_prototype (cls_methods
);
4820 METHOD_ENCODING (cls_methods
) = encoding
;
4823 cls_methods
= DECL_CHAIN (cls_methods
);
4825 generate_method_descriptors (p
);
4827 if (PROTOCOL_LIST (p
))
4828 refs_decl
= generate_protocol_list (p
);
4832 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
4833 protocol_name_expr
= add_objc_string (PROTOCOL_NAME (p
), class_names
);
4836 refs_expr
= convert (build_pointer_type (build_pointer_type
4837 (objc_protocol_template
)),
4838 build_unary_op (input_location
,
4839 ADDR_EXPR
, refs_decl
, 0));
4841 refs_expr
= build_int_cst (NULL_TREE
, 0);
4843 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
4844 by generate_method_descriptors, which is called above. */
4845 initlist
= build_protocol_initializer (TREE_TYPE (decl
),
4846 protocol_name_expr
, refs_expr
,
4847 UOBJC_INSTANCE_METHODS_decl
,
4848 UOBJC_CLASS_METHODS_decl
);
4849 finish_var_decl (decl
, initlist
);
4854 build_protocol_initializer (tree type
, tree protocol_name
,
4855 tree protocol_list
, tree instance_methods
,
4859 tree cast_type
= build_pointer_type
4860 (xref_tag (RECORD_TYPE
,
4861 get_identifier (UTAG_CLASS
)));
4862 VEC(constructor_elt
,gc
) *inits
= NULL
;
4864 /* Filling the "isa" in with one allows the runtime system to
4865 detect that the version change...should remove before final release. */
4867 expr
= build_int_cst (cast_type
, PROTOCOL_VERSION
);
4868 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4869 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_name
);
4870 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_list
);
4872 if (!instance_methods
)
4873 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
4876 expr
= convert (objc_method_proto_list_ptr
,
4877 build_unary_op (input_location
,
4878 ADDR_EXPR
, instance_methods
, 0));
4879 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4883 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
4886 expr
= convert (objc_method_proto_list_ptr
,
4887 build_unary_op (input_location
,
4888 ADDR_EXPR
, class_methods
, 0));
4889 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4892 return objc_build_constructor (type
, inits
);
4895 /* struct _objc_category {
4896 char *category_name;
4898 struct _objc_method_list *instance_methods;
4899 struct _objc_method_list *class_methods;
4900 struct _objc_protocol_list *protocols;
4904 build_category_template (void)
4906 tree ptype
, decls
, *chain
= NULL
;
4908 objc_category_template
= objc_start_struct (get_identifier (UTAG_CATEGORY
));
4910 /* char *category_name; */
4911 decls
= add_field_decl (string_type_node
, "category_name", &chain
);
4913 /* char *class_name; */
4914 add_field_decl (string_type_node
, "class_name", &chain
);
4916 /* struct _objc_method_list *instance_methods; */
4917 add_field_decl (objc_method_list_ptr
, "instance_methods", &chain
);
4919 /* struct _objc_method_list *class_methods; */
4920 add_field_decl (objc_method_list_ptr
, "class_methods", &chain
);
4922 /* struct _objc_protocol **protocol_list; */
4923 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
4924 add_field_decl (ptype
, "protocol_list", &chain
);
4926 objc_finish_struct (objc_category_template
, decls
);
4929 /* struct _objc_selector {
4935 build_selector_template (void)
4937 tree decls
, *chain
= NULL
;
4939 objc_selector_template
= objc_start_struct (get_identifier (UTAG_SELECTOR
));
4942 decls
= add_field_decl (objc_selector_type
, "sel_id", &chain
);
4944 /* char *sel_type; */
4945 add_field_decl (string_type_node
, "sel_type", &chain
);
4947 objc_finish_struct (objc_selector_template
, decls
);
4950 /* struct _objc_class {
4951 struct _objc_class *isa;
4952 struct _objc_class *super_class;
4957 struct _objc_ivar_list *ivars;
4958 struct _objc_method_list *methods;
4959 #ifdef __NEXT_RUNTIME__
4960 struct objc_cache *cache;
4962 struct sarray *dtable;
4963 struct _objc_class *subclass_list;
4964 struct _objc_class *sibling_class;
4966 struct _objc_protocol_list *protocols;
4967 #ifdef __NEXT_RUNTIME__
4970 void *gc_object_type;
4973 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
4974 the NeXT/Apple runtime; still, the compiler must generate them to
4975 maintain backward binary compatibility (and to allow for future
4979 build_class_template (void)
4981 tree ptype
, decls
, *chain
= NULL
;
4983 objc_class_template
= objc_start_struct (get_identifier (UTAG_CLASS
));
4985 /* struct _objc_class *isa; */
4986 decls
= add_field_decl (build_pointer_type (objc_class_template
),
4989 /* struct _objc_class *super_class; */
4990 add_field_decl (build_pointer_type (objc_class_template
),
4991 "super_class", &chain
);
4994 add_field_decl (string_type_node
, "name", &chain
);
4997 add_field_decl (long_integer_type_node
, "version", &chain
);
5000 add_field_decl (long_integer_type_node
, "info", &chain
);
5002 /* long instance_size; */
5003 add_field_decl (long_integer_type_node
, "instance_size", &chain
);
5005 /* struct _objc_ivar_list *ivars; */
5006 add_field_decl (objc_ivar_list_ptr
,"ivars", &chain
);
5008 /* struct _objc_method_list *methods; */
5009 add_field_decl (objc_method_list_ptr
, "methods", &chain
);
5011 if (flag_next_runtime
)
5013 /* struct objc_cache *cache; */
5014 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
5015 get_identifier ("objc_cache")));
5016 add_field_decl (ptype
, "cache", &chain
);
5020 /* struct sarray *dtable; */
5021 ptype
= build_pointer_type(xref_tag (RECORD_TYPE
,
5022 get_identifier ("sarray")));
5023 add_field_decl (ptype
, "dtable", &chain
);
5025 /* struct objc_class *subclass_list; */
5026 ptype
= build_pointer_type (objc_class_template
);
5027 add_field_decl (ptype
, "subclass_list", &chain
);
5029 /* struct objc_class *sibling_class; */
5030 ptype
= build_pointer_type (objc_class_template
);
5031 add_field_decl (ptype
, "sibling_class", &chain
);
5034 /* struct _objc_protocol **protocol_list; */
5035 ptype
= build_pointer_type (build_pointer_type
5036 (xref_tag (RECORD_TYPE
,
5037 get_identifier (UTAG_PROTOCOL
))));
5038 add_field_decl (ptype
, "protocol_list", &chain
);
5040 if (flag_next_runtime
)
5043 add_field_decl (build_pointer_type (void_type_node
), "sel_id", &chain
);
5046 /* void *gc_object_type; */
5047 add_field_decl (build_pointer_type (void_type_node
),
5048 "gc_object_type", &chain
);
5050 objc_finish_struct (objc_class_template
, decls
);
5053 /* Generate appropriate forward declarations for an implementation. */
5056 synth_forward_declarations (void)
5060 /* static struct objc_class _OBJC_CLASS_<my_name>; */
5061 UOBJC_CLASS_decl
= build_metadata_decl ("_OBJC_CLASS",
5062 objc_class_template
);
5064 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
5065 UOBJC_METACLASS_decl
= build_metadata_decl ("_OBJC_METACLASS",
5066 objc_class_template
);
5068 /* Pre-build the following entities - for speed/convenience. */
5070 an_id
= get_identifier ("super_class");
5071 ucls_super_ref
= objc_build_component_ref (UOBJC_CLASS_decl
, an_id
);
5072 uucls_super_ref
= objc_build_component_ref (UOBJC_METACLASS_decl
, an_id
);
5076 error_with_ivar (const char *message
, tree decl
)
5078 error_at (DECL_SOURCE_LOCATION (decl
), "%s %qs",
5079 message
, identifier_to_locale (gen_declaration (decl
)));
5084 check_ivars (tree inter
, tree imp
)
5086 tree intdecls
= CLASS_RAW_IVARS (inter
);
5087 tree impdecls
= CLASS_RAW_IVARS (imp
);
5094 if (intdecls
&& TREE_CODE (intdecls
) == TYPE_DECL
)
5095 intdecls
= TREE_CHAIN (intdecls
);
5097 if (intdecls
== 0 && impdecls
== 0)
5099 if (intdecls
== 0 || impdecls
== 0)
5101 error ("inconsistent instance variable specification");
5105 t1
= TREE_TYPE (intdecls
); t2
= TREE_TYPE (impdecls
);
5107 if (!comptypes (t1
, t2
)
5108 || !tree_int_cst_equal (DECL_INITIAL (intdecls
),
5109 DECL_INITIAL (impdecls
)))
5111 if (DECL_NAME (intdecls
) == DECL_NAME (impdecls
))
5113 error_with_ivar ("conflicting instance variable type",
5115 error_with_ivar ("previous declaration of",
5118 else /* both the type and the name don't match */
5120 error ("inconsistent instance variable specification");
5125 else if (DECL_NAME (intdecls
) != DECL_NAME (impdecls
))
5127 error_with_ivar ("conflicting instance variable name",
5129 error_with_ivar ("previous declaration of",
5133 intdecls
= DECL_CHAIN (intdecls
);
5134 impdecls
= DECL_CHAIN (impdecls
);
5138 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
5139 This needs to be done just once per compilation. */
5141 /* struct _objc_super {
5142 struct _objc_object *self;
5143 struct _objc_class *super_class;
5147 build_super_template (void)
5149 tree decls
, *chain
= NULL
;
5151 objc_super_template
= objc_start_struct (get_identifier (UTAG_SUPER
));
5153 /* struct _objc_object *self; */
5154 decls
= add_field_decl (objc_object_type
, "self", &chain
);
5156 /* struct _objc_class *super_class; */
5157 add_field_decl (build_pointer_type (objc_class_template
),
5158 "super_class", &chain
);
5160 objc_finish_struct (objc_super_template
, decls
);
5163 /* struct _objc_ivar {
5170 build_ivar_template (void)
5172 tree objc_ivar_id
, objc_ivar_record
;
5173 tree decls
, *chain
= NULL
;
5175 objc_ivar_id
= get_identifier (UTAG_IVAR
);
5176 objc_ivar_record
= objc_start_struct (objc_ivar_id
);
5178 /* char *ivar_name; */
5179 decls
= add_field_decl (string_type_node
, "ivar_name", &chain
);
5181 /* char *ivar_type; */
5182 add_field_decl (string_type_node
, "ivar_type", &chain
);
5184 /* int ivar_offset; */
5185 add_field_decl (integer_type_node
, "ivar_offset", &chain
);
5187 objc_finish_struct (objc_ivar_record
, decls
);
5189 return objc_ivar_record
;
5194 struct objc_ivar ivar_list[ivar_count];
5198 build_ivar_list_template (tree list_type
, int size
)
5200 tree objc_ivar_list_record
;
5201 tree array_type
, decls
, *chain
= NULL
;
5203 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
5205 /* int ivar_count; */
5206 decls
= add_field_decl (integer_type_node
, "ivar_count", &chain
);
5208 /* struct objc_ivar ivar_list[]; */
5209 array_type
= build_sized_array_type (list_type
, size
);
5210 add_field_decl (array_type
, "ivar_list", &chain
);
5212 objc_finish_struct (objc_ivar_list_record
, decls
);
5214 return objc_ivar_list_record
;
5218 struct _objc__method_prototype_list *method_next;
5220 struct objc_method method_list[method_count];
5224 build_method_list_template (tree list_type
, int size
)
5226 tree objc_ivar_list_record
;
5227 tree array_type
, decls
, *chain
= NULL
;
5229 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
5231 /* struct _objc__method_prototype_list *method_next; */
5232 decls
= add_field_decl (objc_method_proto_list_ptr
, "method_next", &chain
);
5234 /* int method_count; */
5235 add_field_decl (integer_type_node
, "method_count", &chain
);
5237 /* struct objc_method method_list[]; */
5238 array_type
= build_sized_array_type (list_type
, size
);
5239 add_field_decl (array_type
, "method_list", &chain
);
5241 objc_finish_struct (objc_ivar_list_record
, decls
);
5243 return objc_ivar_list_record
;
5247 build_ivar_list_initializer (tree type
, tree field_decl
)
5249 VEC(constructor_elt
,gc
) *inits
= NULL
;
5253 VEC(constructor_elt
,gc
) *ivar
= NULL
;
5257 if (DECL_NAME (field_decl
))
5258 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
,
5259 add_objc_string (DECL_NAME (field_decl
),
5262 /* Unnamed bit-field ivar (yuck). */
5263 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5266 encode_field_decl (field_decl
,
5267 obstack_object_size (&util_obstack
),
5268 OBJC_ENCODE_DONT_INLINE_DEFS
);
5270 /* Null terminate string. */
5271 obstack_1grow (&util_obstack
, 0);
5272 id
= add_objc_string (get_identifier (XOBFINISH (&util_obstack
, char *)),
5274 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, id
);
5275 obstack_free (&util_obstack
, util_firstobj
);
5278 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, byte_position (field_decl
));
5279 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
5280 objc_build_constructor (type
, ivar
));
5282 field_decl
= DECL_CHAIN (field_decl
);
5283 while (field_decl
&& TREE_CODE (field_decl
) != FIELD_DECL
);
5287 return objc_build_constructor (build_array_type (type
, 0), inits
);
5291 generate_ivars_list (tree type
, const char *name
, int size
, tree list
)
5294 VEC(constructor_elt
,gc
) *inits
= NULL
;
5296 decl
= start_var_decl (type
, synth_id_with_class_suffix
5297 (name
, objc_implementation_context
));
5299 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
5300 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, list
);
5302 finish_var_decl (decl
,
5303 objc_build_constructor (TREE_TYPE (decl
), inits
));
5308 /* Count only the fields occurring in T. */
5311 ivar_list_length (tree t
)
5315 for (; t
; t
= DECL_CHAIN (t
))
5316 if (TREE_CODE (t
) == FIELD_DECL
)
5323 generate_ivar_lists (void)
5325 tree initlist
, ivar_list_template
, chain
;
5328 generating_instance_variables
= 1;
5330 if (!objc_ivar_template
)
5331 objc_ivar_template
= build_ivar_template ();
5333 /* Only generate class variables for the root of the inheritance
5334 hierarchy since these will be the same for every class. */
5336 if (CLASS_SUPER_NAME (implementation_template
) == NULL_TREE
5337 && (chain
= TYPE_FIELDS (objc_class_template
)))
5339 size
= ivar_list_length (chain
);
5341 ivar_list_template
= build_ivar_list_template (objc_ivar_template
, size
);
5342 initlist
= build_ivar_list_initializer (objc_ivar_template
, chain
);
5344 UOBJC_CLASS_VARIABLES_decl
5345 = generate_ivars_list (ivar_list_template
, "_OBJC_CLASS_VARIABLES",
5349 UOBJC_CLASS_VARIABLES_decl
= 0;
5351 chain
= CLASS_IVARS (implementation_template
);
5354 size
= ivar_list_length (chain
);
5355 ivar_list_template
= build_ivar_list_template (objc_ivar_template
, size
);
5356 initlist
= build_ivar_list_initializer (objc_ivar_template
, chain
);
5358 UOBJC_INSTANCE_VARIABLES_decl
5359 = generate_ivars_list (ivar_list_template
, "_OBJC_INSTANCE_VARIABLES",
5363 UOBJC_INSTANCE_VARIABLES_decl
= 0;
5365 generating_instance_variables
= 0;
5369 build_dispatch_table_initializer (tree type
, tree entries
)
5371 VEC(constructor_elt
,gc
) *inits
= NULL
;
5375 VEC(constructor_elt
,gc
) *elems
= NULL
;
5378 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
5379 build_selector (METHOD_SEL_NAME (entries
)));
5381 /* Generate the method encoding if we don't have one already. */
5382 if (! METHOD_ENCODING (entries
))
5383 METHOD_ENCODING (entries
) =
5384 encode_method_prototype (entries
);
5386 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
5387 add_objc_string (METHOD_ENCODING (entries
),
5390 expr
= convert (ptr_type_node
,
5391 build_unary_op (input_location
, ADDR_EXPR
,
5392 METHOD_DEFINITION (entries
), 1));
5393 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
, expr
);
5395 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
5396 objc_build_constructor (type
, elems
));
5398 entries
= DECL_CHAIN (entries
);
5402 return objc_build_constructor (build_array_type (type
, 0), inits
);
5405 /* To accomplish method prototyping without generating all kinds of
5406 inane warnings, the definition of the dispatch table entries were
5409 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
5411 struct objc_method { SEL _cmd; ...; void *_imp; }; */
5414 build_method_template (void)
5417 tree decls
, *chain
= NULL
;
5419 _SLT_record
= objc_start_struct (get_identifier (UTAG_METHOD
));
5422 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
5424 /* char *method_types; */
5425 add_field_decl (string_type_node
, "method_types", &chain
);
5428 add_field_decl (build_pointer_type (void_type_node
), "_imp", &chain
);
5430 objc_finish_struct (_SLT_record
, decls
);
5437 generate_dispatch_table (tree type
, const char *name
, int size
, tree list
)
5440 VEC(constructor_elt
,gc
) *v
= NULL
;
5442 decl
= start_var_decl (type
, synth_id_with_class_suffix
5443 (name
, objc_implementation_context
));
5445 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
5446 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (integer_type_node
, size
));
5447 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, list
);
5449 finish_var_decl (decl
,
5450 objc_build_constructor (TREE_TYPE (decl
), v
));
5456 mark_referenced_methods (void)
5458 struct imp_entry
*impent
;
5461 for (impent
= imp_list
; impent
; impent
= impent
->next
)
5463 chain
= CLASS_CLS_METHODS (impent
->imp_context
);
5466 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain
)));
5467 chain
= DECL_CHAIN (chain
);
5470 chain
= CLASS_NST_METHODS (impent
->imp_context
);
5473 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain
)));
5474 chain
= DECL_CHAIN (chain
);
5480 generate_dispatch_tables (void)
5482 tree initlist
, chain
, method_list_template
;
5485 if (!objc_method_template
)
5486 objc_method_template
= build_method_template ();
5488 chain
= CLASS_CLS_METHODS (objc_implementation_context
);
5491 size
= list_length (chain
);
5493 method_list_template
5494 = build_method_list_template (objc_method_template
, size
);
5496 = build_dispatch_table_initializer (objc_method_template
, chain
);
5498 UOBJC_CLASS_METHODS_decl
5499 = generate_dispatch_table (method_list_template
,
5500 ((TREE_CODE (objc_implementation_context
)
5501 == CLASS_IMPLEMENTATION_TYPE
)
5502 ? "_OBJC_CLASS_METHODS"
5503 : "_OBJC_CATEGORY_CLASS_METHODS"),
5507 UOBJC_CLASS_METHODS_decl
= 0;
5509 chain
= CLASS_NST_METHODS (objc_implementation_context
);
5512 size
= list_length (chain
);
5514 method_list_template
5515 = build_method_list_template (objc_method_template
, size
);
5517 = build_dispatch_table_initializer (objc_method_template
, chain
);
5519 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
5520 UOBJC_INSTANCE_METHODS_decl
5521 = generate_dispatch_table (method_list_template
,
5522 "_OBJC_INSTANCE_METHODS",
5525 /* We have a category. */
5526 UOBJC_INSTANCE_METHODS_decl
5527 = generate_dispatch_table (method_list_template
,
5528 "_OBJC_CATEGORY_INSTANCE_METHODS",
5532 UOBJC_INSTANCE_METHODS_decl
= 0;
5536 generate_protocol_list (tree i_or_p
)
5538 tree array_type
, ptype
, refs_decl
, lproto
, e
, plist
;
5540 const char *ref_name
;
5541 VEC(constructor_elt
,gc
) *v
= NULL
;
5543 if (TREE_CODE (i_or_p
) == CLASS_INTERFACE_TYPE
5544 || TREE_CODE (i_or_p
) == CATEGORY_INTERFACE_TYPE
)
5545 plist
= CLASS_PROTOCOL_LIST (i_or_p
);
5546 else if (TREE_CODE (i_or_p
) == PROTOCOL_INTERFACE_TYPE
)
5547 plist
= PROTOCOL_LIST (i_or_p
);
5552 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
5553 if (TREE_CODE (TREE_VALUE (lproto
)) == PROTOCOL_INTERFACE_TYPE
5554 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto
)))
5557 /* Build initializer. */
5558 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5559 e
= build_int_cst (build_pointer_type (objc_protocol_template
), size
);
5560 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
5562 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
5564 tree pval
= TREE_VALUE (lproto
);
5566 if (TREE_CODE (pval
) == PROTOCOL_INTERFACE_TYPE
5567 && PROTOCOL_FORWARD_DECL (pval
))
5569 e
= build_unary_op (input_location
, ADDR_EXPR
,
5570 PROTOCOL_FORWARD_DECL (pval
), 0);
5571 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
5575 /* static struct objc_protocol *refs[n]; */
5577 if (TREE_CODE (i_or_p
) == PROTOCOL_INTERFACE_TYPE
)
5578 ref_name
= synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p
);
5579 else if (TREE_CODE (i_or_p
) == CLASS_INTERFACE_TYPE
)
5580 ref_name
= synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p
);
5581 else if (TREE_CODE (i_or_p
) == CATEGORY_INTERFACE_TYPE
)
5582 ref_name
= synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p
);
5586 ptype
= build_pointer_type (objc_protocol_template
);
5587 array_type
= build_sized_array_type (ptype
, size
+ 3);
5588 refs_decl
= start_var_decl (array_type
, ref_name
);
5590 finish_var_decl (refs_decl
,
5591 objc_build_constructor (TREE_TYPE (refs_decl
), v
));
5597 build_category_initializer (tree type
, tree cat_name
, tree class_name
,
5598 tree instance_methods
, tree class_methods
,
5602 VEC(constructor_elt
,gc
) *v
= NULL
;
5604 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, cat_name
);
5605 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, class_name
);
5607 if (!instance_methods
)
5608 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5611 expr
= convert (objc_method_list_ptr
,
5612 build_unary_op (input_location
, ADDR_EXPR
,
5613 instance_methods
, 0));
5614 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5617 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5620 expr
= convert (objc_method_list_ptr
,
5621 build_unary_op (input_location
, ADDR_EXPR
,
5623 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5626 /* protocol_list = */
5628 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5631 expr
= convert (build_pointer_type
5633 (objc_protocol_template
)),
5634 build_unary_op (input_location
, ADDR_EXPR
,
5636 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5639 return objc_build_constructor (type
, v
);
5642 /* struct _objc_class {
5643 struct objc_class *isa;
5644 struct objc_class *super_class;
5649 struct objc_ivar_list *ivars;
5650 struct objc_method_list *methods;
5651 if (flag_next_runtime)
5652 struct objc_cache *cache;
5654 struct sarray *dtable;
5655 struct objc_class *subclass_list;
5656 struct objc_class *sibling_class;
5658 struct objc_protocol_list *protocols;
5659 if (flag_next_runtime)
5661 void *gc_object_type;
5665 build_shared_structure_initializer (tree type
, tree isa
, tree super
,
5666 tree name
, tree size
, int status
,
5667 tree dispatch_table
, tree ivar_list
,
5671 VEC(constructor_elt
,gc
) *v
= NULL
;
5674 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, isa
);
5677 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, super
);
5680 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, default_conversion (name
));
5683 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5684 build_int_cst (long_integer_type_node
, 0));
5687 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5688 build_int_cst (long_integer_type_node
, status
));
5690 /* instance_size = */
5691 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5692 convert (long_integer_type_node
, size
));
5694 /* objc_ivar_list = */
5696 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5699 expr
= convert (objc_ivar_list_ptr
,
5700 build_unary_op (input_location
, ADDR_EXPR
,
5702 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5705 /* objc_method_list = */
5706 if (!dispatch_table
)
5707 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5710 expr
= convert (objc_method_list_ptr
,
5711 build_unary_op (input_location
, ADDR_EXPR
,
5712 dispatch_table
, 0));
5713 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5716 if (flag_next_runtime
)
5717 /* method_cache = */
5718 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5722 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5724 /* subclass_list = */
5725 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5727 /* sibling_class = */
5728 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5731 /* protocol_list = */
5732 if (! protocol_list
)
5733 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5736 expr
= convert (build_pointer_type
5738 (objc_protocol_template
)),
5739 build_unary_op (input_location
, ADDR_EXPR
,
5741 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5744 if (flag_next_runtime
)
5746 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5748 /* gc_object_type = NULL */
5749 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5751 return objc_build_constructor (type
, v
);
5754 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
5757 lookup_category (tree klass
, tree cat_name
)
5759 tree category
= CLASS_CATEGORY_LIST (klass
);
5761 while (category
&& CLASS_SUPER_NAME (category
) != cat_name
)
5762 category
= CLASS_CATEGORY_LIST (category
);
5766 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
5769 generate_category (struct imp_entry
*impent
)
5771 tree initlist
, cat_name_expr
, class_name_expr
;
5772 tree protocol_decl
, category
;
5773 tree cat
= impent
->imp_context
;
5775 implementation_template
= impent
->imp_template
;
5776 UOBJC_CLASS_decl
= impent
->class_decl
;
5777 UOBJC_METACLASS_decl
= impent
->meta_decl
;
5779 add_class_reference (CLASS_NAME (cat
));
5780 cat_name_expr
= add_objc_string (CLASS_SUPER_NAME (cat
), class_names
);
5782 class_name_expr
= add_objc_string (CLASS_NAME (cat
), class_names
);
5784 category
= lookup_category (implementation_template
,
5785 CLASS_SUPER_NAME (cat
));
5787 if (category
&& CLASS_PROTOCOL_LIST (category
))
5789 generate_protocol_references (CLASS_PROTOCOL_LIST (category
));
5790 protocol_decl
= generate_protocol_list (category
);
5795 initlist
= build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl
),
5796 cat_name_expr
, class_name_expr
,
5797 UOBJC_INSTANCE_METHODS_decl
,
5798 UOBJC_CLASS_METHODS_decl
,
5800 /* Finish and initialize the forward decl. */
5801 finish_var_decl (UOBJC_CLASS_decl
, initlist
);
5804 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
5805 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5808 generate_shared_structures (struct imp_entry
*impent
)
5810 tree name_expr
, super_expr
, root_expr
;
5811 tree my_root_id
, my_super_id
;
5812 tree cast_type
, initlist
, protocol_decl
;
5815 objc_implementation_context
= impent
->imp_context
;
5816 implementation_template
= impent
->imp_template
;
5817 UOBJC_CLASS_decl
= impent
->class_decl
;
5818 UOBJC_METACLASS_decl
= impent
->meta_decl
;
5819 cls_flags
= impent
->has_cxx_cdtors
? CLS_HAS_CXX_STRUCTORS
: 0 ;
5821 my_super_id
= CLASS_SUPER_NAME (implementation_template
);
5824 add_class_reference (my_super_id
);
5826 /* Compute "my_root_id" - this is required for code generation.
5827 the "isa" for all meta class structures points to the root of
5828 the inheritance hierarchy (e.g. "__Object")... */
5829 my_root_id
= my_super_id
;
5832 tree my_root_int
= lookup_interface (my_root_id
);
5834 if (my_root_int
&& CLASS_SUPER_NAME (my_root_int
))
5835 my_root_id
= CLASS_SUPER_NAME (my_root_int
);
5842 /* No super class. */
5843 my_root_id
= CLASS_NAME (implementation_template
);
5845 cast_type
= build_pointer_type (objc_class_template
);
5846 name_expr
= add_objc_string (CLASS_NAME (implementation_template
),
5849 /* Install class `isa' and `super' pointers at runtime. */
5851 super_expr
= add_objc_string (my_super_id
, class_names
);
5853 super_expr
= integer_zero_node
;
5855 super_expr
= build_c_cast (input_location
,
5856 cast_type
, super_expr
); /* cast! */
5858 root_expr
= add_objc_string (my_root_id
, class_names
);
5859 root_expr
= build_c_cast (input_location
, cast_type
, root_expr
); /* cast! */
5861 if (CLASS_PROTOCOL_LIST (implementation_template
))
5863 generate_protocol_references
5864 (CLASS_PROTOCOL_LIST (implementation_template
));
5865 protocol_decl
= generate_protocol_list (implementation_template
);
5870 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
5873 = build_shared_structure_initializer
5874 (TREE_TYPE (UOBJC_METACLASS_decl
),
5875 root_expr
, super_expr
, name_expr
,
5876 convert (integer_type_node
, TYPE_SIZE_UNIT (objc_class_template
)),
5878 UOBJC_CLASS_METHODS_decl
,
5879 UOBJC_CLASS_VARIABLES_decl
,
5882 finish_var_decl (UOBJC_METACLASS_decl
, initlist
);
5884 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5887 = build_shared_structure_initializer
5888 (TREE_TYPE (UOBJC_CLASS_decl
),
5889 build_unary_op (input_location
, ADDR_EXPR
, UOBJC_METACLASS_decl
, 0),
5890 super_expr
, name_expr
,
5891 convert (integer_type_node
,
5892 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
5893 (implementation_template
))),
5894 1 /*CLS_FACTORY*/ | cls_flags
,
5895 UOBJC_INSTANCE_METHODS_decl
,
5896 UOBJC_INSTANCE_VARIABLES_decl
,
5899 finish_var_decl (UOBJC_CLASS_decl
, initlist
);
5904 synth_id_with_class_suffix (const char *preamble
, tree ctxt
)
5906 static char string
[BUFSIZE
];
5908 if (TREE_CODE (ctxt
) == CLASS_IMPLEMENTATION_TYPE
5909 || TREE_CODE (ctxt
) == CLASS_INTERFACE_TYPE
)
5911 sprintf (string
, "%s_%s", preamble
,
5912 IDENTIFIER_POINTER (CLASS_NAME (ctxt
)));
5914 else if (TREE_CODE (ctxt
) == CATEGORY_IMPLEMENTATION_TYPE
5915 || TREE_CODE (ctxt
) == CATEGORY_INTERFACE_TYPE
)
5917 /* We have a category. */
5918 const char *const class_name
5919 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context
));
5920 const char *const class_super_name
5921 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context
));
5922 sprintf (string
, "%s_%s_%s", preamble
, class_name
, class_super_name
);
5924 else if (TREE_CODE (ctxt
) == PROTOCOL_INTERFACE_TYPE
)
5926 const char *protocol_name
= IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt
));
5927 sprintf (string
, "%s_%s", preamble
, protocol_name
);
5935 /* If type is empty or only type qualifiers are present, add default
5936 type of id (otherwise grokdeclarator will default to int). */
5939 adjust_type_for_id_default (tree type
)
5942 type
= make_node (TREE_LIST
);
5944 if (!TREE_VALUE (type
))
5945 TREE_VALUE (type
) = objc_object_type
;
5946 else if (TREE_CODE (TREE_VALUE (type
)) == RECORD_TYPE
5947 && TYPED_OBJECT (TREE_VALUE (type
)))
5948 error ("can not use an object as parameter to a method");
5955 selector ':' '(' typename ')' identifier
5958 Transform an Objective-C keyword argument into
5959 the C equivalent parameter declarator.
5961 In: key_name, an "identifier_node" (optional).
5962 arg_type, a "tree_list" (optional).
5963 arg_name, an "identifier_node".
5965 Note: It would be really nice to strongly type the preceding
5966 arguments in the function prototype; however, then I
5967 could not use the "accessor" macros defined in "tree.h".
5969 Out: an instance of "keyword_decl". */
5972 objc_build_keyword_decl (tree key_name
, tree arg_type
, tree arg_name
)
5976 /* If no type is specified, default to "id". */
5977 arg_type
= adjust_type_for_id_default (arg_type
);
5979 keyword_decl
= make_node (KEYWORD_DECL
);
5981 TREE_TYPE (keyword_decl
) = arg_type
;
5982 KEYWORD_ARG_NAME (keyword_decl
) = arg_name
;
5983 KEYWORD_KEY_NAME (keyword_decl
) = key_name
;
5985 return keyword_decl
;
5988 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
5991 build_keyword_selector (tree selector
)
5994 tree key_chain
, key_name
;
5997 /* Scan the selector to see how much space we'll need. */
5998 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
6000 if (TREE_CODE (selector
) == KEYWORD_DECL
)
6001 key_name
= KEYWORD_KEY_NAME (key_chain
);
6002 else if (TREE_CODE (selector
) == TREE_LIST
)
6003 key_name
= TREE_PURPOSE (key_chain
);
6008 len
+= IDENTIFIER_LENGTH (key_name
) + 1;
6010 /* Just a ':' arg. */
6014 buf
= (char *) alloca (len
+ 1);
6015 /* Start the buffer out as an empty string. */
6018 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
6020 if (TREE_CODE (selector
) == KEYWORD_DECL
)
6021 key_name
= KEYWORD_KEY_NAME (key_chain
);
6022 else if (TREE_CODE (selector
) == TREE_LIST
)
6024 key_name
= TREE_PURPOSE (key_chain
);
6025 /* The keyword decl chain will later be used as a function argument
6026 chain. Unhook the selector itself so as to not confuse other
6027 parts of the compiler. */
6028 TREE_PURPOSE (key_chain
) = NULL_TREE
;
6034 strcat (buf
, IDENTIFIER_POINTER (key_name
));
6038 return get_identifier (buf
);
6041 /* Used for declarations and definitions. */
6044 build_method_decl (enum tree_code code
, tree ret_type
, tree selector
,
6045 tree add_args
, bool ellipsis
)
6049 /* If no type is specified, default to "id". */
6050 ret_type
= adjust_type_for_id_default (ret_type
);
6052 method_decl
= make_node (code
);
6053 TREE_TYPE (method_decl
) = ret_type
;
6055 /* If we have a keyword selector, create an identifier_node that
6056 represents the full selector name (`:' included)... */
6057 if (TREE_CODE (selector
) == KEYWORD_DECL
)
6059 METHOD_SEL_NAME (method_decl
) = build_keyword_selector (selector
);
6060 METHOD_SEL_ARGS (method_decl
) = selector
;
6061 METHOD_ADD_ARGS (method_decl
) = add_args
;
6062 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl
) = ellipsis
;
6066 METHOD_SEL_NAME (method_decl
) = selector
;
6067 METHOD_SEL_ARGS (method_decl
) = NULL_TREE
;
6068 METHOD_ADD_ARGS (method_decl
) = NULL_TREE
;
6074 #define METHOD_DEF 0
6075 #define METHOD_REF 1
6077 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
6078 an argument list for method METH. CONTEXT is either METHOD_DEF or
6079 METHOD_REF, saying whether we are trying to define a method or call
6080 one. SUPERFLAG says this is for a send to super; this makes a
6081 difference for the NeXT calling sequence in which the lookup and
6082 the method call are done together. If METH is null, user-defined
6083 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
6086 get_arg_type_list (tree meth
, int context
, int superflag
)
6090 /* Receiver type. */
6091 if (flag_next_runtime
&& superflag
)
6092 arglist
= build_tree_list (NULL_TREE
, objc_super_type
);
6093 else if (context
== METHOD_DEF
&& TREE_CODE (meth
) == INSTANCE_METHOD_DECL
)
6094 arglist
= build_tree_list (NULL_TREE
, objc_instance_type
);
6096 arglist
= build_tree_list (NULL_TREE
, objc_object_type
);
6098 /* Selector type - will eventually change to `int'. */
6099 chainon (arglist
, build_tree_list (NULL_TREE
, objc_selector_type
));
6101 /* No actual method prototype given -- assume that remaining arguments
6106 /* Build a list of argument types. */
6107 for (akey
= METHOD_SEL_ARGS (meth
); akey
; akey
= DECL_CHAIN (akey
))
6109 tree arg_type
= TREE_VALUE (TREE_TYPE (akey
));
6111 /* Decay arrays and functions into pointers. */
6112 if (TREE_CODE (arg_type
) == ARRAY_TYPE
)
6113 arg_type
= build_pointer_type (TREE_TYPE (arg_type
));
6114 else if (TREE_CODE (arg_type
) == FUNCTION_TYPE
)
6115 arg_type
= build_pointer_type (arg_type
);
6117 chainon (arglist
, build_tree_list (NULL_TREE
, arg_type
));
6120 if (METHOD_ADD_ARGS (meth
))
6122 for (akey
= TREE_CHAIN (METHOD_ADD_ARGS (meth
));
6123 akey
; akey
= TREE_CHAIN (akey
))
6125 tree arg_type
= TREE_TYPE (TREE_VALUE (akey
));
6127 chainon (arglist
, build_tree_list (NULL_TREE
, arg_type
));
6130 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth
))
6131 goto lack_of_ellipsis
;
6136 chainon (arglist
, OBJC_VOID_AT_END
);
6143 check_duplicates (hash hsh
, int methods
, int is_class
)
6145 tree meth
= NULL_TREE
;
6153 /* We have two or more methods with the same name but
6157 /* But just how different are those types? If
6158 -Wno-strict-selector-match is specified, we shall not
6159 complain if the differences are solely among types with
6160 identical size and alignment. */
6161 if (!warn_strict_selector_match
)
6163 for (loop
= hsh
->list
; loop
; loop
= loop
->next
)
6164 if (!comp_proto_with_proto (meth
, loop
->value
, 0))
6173 bool type
= TREE_CODE (meth
) == INSTANCE_METHOD_DECL
;
6175 warning_at (input_location
, 0,
6176 "multiple methods named %<%c%E%> found",
6177 (is_class
? '+' : '-'),
6178 METHOD_SEL_NAME (meth
));
6179 inform (DECL_SOURCE_LOCATION (meth
), "using %<%c%s%>",
6181 identifier_to_locale (gen_method_decl (meth
)));
6185 bool type
= TREE_CODE (meth
) == INSTANCE_METHOD_DECL
;
6187 warning_at (input_location
, 0,
6188 "multiple selectors named %<%c%E%> found",
6189 (is_class
? '+' : '-'),
6190 METHOD_SEL_NAME (meth
));
6191 inform (DECL_SOURCE_LOCATION (meth
), "found %<%c%s%>",
6193 identifier_to_locale (gen_method_decl (meth
)));
6196 for (loop
= hsh
->list
; loop
; loop
= loop
->next
)
6198 bool type
= TREE_CODE (loop
->value
) == INSTANCE_METHOD_DECL
;
6200 inform (DECL_SOURCE_LOCATION (loop
->value
), "also found %<%c%s%>",
6202 identifier_to_locale (gen_method_decl (loop
->value
)));
6209 /* If RECEIVER is a class reference, return the identifier node for
6210 the referenced class. RECEIVER is created by objc_get_class_reference,
6211 so we check the exact form created depending on which runtimes are
6215 receiver_is_class_object (tree receiver
, int self
, int super
)
6217 tree chain
, exp
, arg
;
6219 /* The receiver is 'self' or 'super' in the context of a class method. */
6220 if (objc_method_context
6221 && TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
6224 ? CLASS_SUPER_NAME (implementation_template
)
6225 : CLASS_NAME (implementation_template
));
6227 if (flag_next_runtime
)
6229 /* The receiver is a variable created by
6230 build_class_reference_decl. */
6231 if (TREE_CODE (receiver
) == VAR_DECL
&& IS_CLASS (TREE_TYPE (receiver
)))
6232 /* Look up the identifier. */
6233 for (chain
= cls_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
6234 if (TREE_PURPOSE (chain
) == receiver
)
6235 return TREE_VALUE (chain
);
6238 /* The receiver is a function call that returns an id. Check if
6239 it is a call to objc_getClass, if so, pick up the class name. */
6240 if (TREE_CODE (receiver
) == CALL_EXPR
6241 && (exp
= CALL_EXPR_FN (receiver
))
6242 && TREE_CODE (exp
) == ADDR_EXPR
6243 && (exp
= TREE_OPERAND (exp
, 0))
6244 && TREE_CODE (exp
) == FUNCTION_DECL
6245 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
6246 prototypes for objc_get_class(). Thankfully, they seem to share the
6247 same function type. */
6248 && TREE_TYPE (exp
) == TREE_TYPE (objc_get_class_decl
)
6249 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp
)), TAG_GETCLASS
)
6250 /* We have a call to objc_get_class/objc_getClass! */
6251 && (arg
= CALL_EXPR_ARG (receiver
, 0)))
6254 if (TREE_CODE (arg
) == ADDR_EXPR
6255 && (arg
= TREE_OPERAND (arg
, 0))
6256 && TREE_CODE (arg
) == STRING_CST
)
6257 /* Finally, we have the class name. */
6258 return get_identifier (TREE_STRING_POINTER (arg
));
6263 /* If we are currently building a message expr, this holds
6264 the identifier of the selector of the message. This is
6265 used when printing warnings about argument mismatches. */
6267 static tree current_objc_message_selector
= 0;
6270 objc_message_selector (void)
6272 return current_objc_message_selector
;
6275 /* Construct an expression for sending a message.
6276 MESS has the object to send to in TREE_PURPOSE
6277 and the argument list (including selector) in TREE_VALUE.
6279 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
6280 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
6283 objc_build_message_expr (tree mess
)
6285 tree receiver
= TREE_PURPOSE (mess
);
6288 tree args
= TREE_PURPOSE (TREE_VALUE (mess
));
6290 tree args
= TREE_VALUE (mess
);
6292 tree method_params
= NULL_TREE
;
6294 if (TREE_CODE (receiver
) == ERROR_MARK
|| TREE_CODE (args
) == ERROR_MARK
)
6295 return error_mark_node
;
6297 /* Obtain the full selector name. */
6298 if (TREE_CODE (args
) == IDENTIFIER_NODE
)
6299 /* A unary selector. */
6301 else if (TREE_CODE (args
) == TREE_LIST
)
6302 sel_name
= build_keyword_selector (args
);
6306 /* Build the parameter list to give to the method. */
6307 if (TREE_CODE (args
) == TREE_LIST
)
6309 method_params
= chainon (args
, TREE_VALUE (TREE_VALUE (mess
)));
6312 tree chain
= args
, prev
= NULL_TREE
;
6314 /* We have a keyword selector--check for comma expressions. */
6317 tree element
= TREE_VALUE (chain
);
6319 /* We have a comma expression, must collapse... */
6320 if (TREE_CODE (element
) == TREE_LIST
)
6323 TREE_CHAIN (prev
) = element
;
6328 chain
= TREE_CHAIN (chain
);
6330 method_params
= args
;
6335 if (processing_template_decl
)
6336 /* Must wait until template instantiation time. */
6337 return build_min_nt (MESSAGE_SEND_EXPR
, receiver
, sel_name
,
6341 return objc_finish_message_expr (receiver
, sel_name
, method_params
);
6344 /* Look up method SEL_NAME that would be suitable for receiver
6345 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
6346 nonzero), and report on any duplicates. */
6349 lookup_method_in_hash_lists (tree sel_name
, int is_class
)
6351 hash method_prototype
= NULL
;
6354 method_prototype
= hash_lookup (nst_method_hash_list
,
6357 if (!method_prototype
)
6359 method_prototype
= hash_lookup (cls_method_hash_list
,
6364 return check_duplicates (method_prototype
, 1, is_class
);
6367 /* The 'objc_finish_message_expr' routine is called from within
6368 'objc_build_message_expr' for non-template functions. In the case of
6369 C++ template functions, it is called from 'build_expr_from_tree'
6370 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
6373 objc_finish_message_expr (tree receiver
, tree sel_name
, tree method_params
)
6375 tree method_prototype
= NULL_TREE
, rprotos
= NULL_TREE
, rtype
;
6376 tree selector
, retval
, class_tree
;
6377 int self
, super
, have_cast
;
6379 /* Extract the receiver of the message, as well as its type
6380 (where the latter may take the form of a cast or be inferred
6381 from the implementation context). */
6383 while (TREE_CODE (rtype
) == COMPOUND_EXPR
6384 || TREE_CODE (rtype
) == MODIFY_EXPR
6385 || CONVERT_EXPR_P (rtype
)
6386 || TREE_CODE (rtype
) == COMPONENT_REF
)
6387 rtype
= TREE_OPERAND (rtype
, 0);
6388 self
= (rtype
== self_decl
);
6389 super
= (rtype
== UOBJC_SUPER_decl
);
6390 rtype
= TREE_TYPE (receiver
);
6391 have_cast
= (TREE_CODE (receiver
) == NOP_EXPR
6392 || (TREE_CODE (receiver
) == COMPOUND_EXPR
6393 && !IS_SUPER (rtype
)));
6395 /* If we are calling [super dealloc], reset our warning flag. */
6396 if (super
&& !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name
)))
6397 should_call_super_dealloc
= 0;
6399 /* If the receiver is a class object, retrieve the corresponding
6400 @interface, if one exists. */
6401 class_tree
= receiver_is_class_object (receiver
, self
, super
);
6403 /* Now determine the receiver type (if an explicit cast has not been
6408 rtype
= lookup_interface (class_tree
);
6409 /* Handle `self' and `super'. */
6412 if (!CLASS_SUPER_NAME (implementation_template
))
6414 error ("no super class declared in @interface for %qE",
6415 CLASS_NAME (implementation_template
));
6416 return error_mark_node
;
6418 rtype
= lookup_interface (CLASS_SUPER_NAME (implementation_template
));
6421 rtype
= lookup_interface (CLASS_NAME (implementation_template
));
6424 /* If receiver is of type `id' or `Class' (or if the @interface for a
6425 class is not visible), we shall be satisfied with the existence of
6426 any instance or class method. */
6427 if (objc_is_id (rtype
))
6429 class_tree
= (IS_CLASS (rtype
) ? objc_class_name
: NULL_TREE
);
6430 rprotos
= (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype
))
6431 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype
))
6437 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
6438 in protocols themselves for the method prototype. */
6440 = lookup_method_in_protocol_list (rprotos
, sel_name
,
6441 class_tree
!= NULL_TREE
);
6443 /* If messaging 'Class <Proto>' but did not find a class method
6444 prototype, search for an instance method instead, and warn
6445 about having done so. */
6446 if (!method_prototype
&& !rtype
&& class_tree
!= NULL_TREE
)
6449 = lookup_method_in_protocol_list (rprotos
, sel_name
, 0);
6451 if (method_prototype
)
6452 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
6453 sel_name
, sel_name
);
6459 tree orig_rtype
= rtype
;
6461 if (TREE_CODE (rtype
) == POINTER_TYPE
)
6462 rtype
= TREE_TYPE (rtype
);
6463 /* Traverse typedef aliases */
6464 while (TREE_CODE (rtype
) == RECORD_TYPE
&& OBJC_TYPE_NAME (rtype
)
6465 && TREE_CODE (OBJC_TYPE_NAME (rtype
)) == TYPE_DECL
6466 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype
)))
6467 rtype
= DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype
));
6468 if (TYPED_OBJECT (rtype
))
6470 rprotos
= TYPE_OBJC_PROTOCOL_LIST (rtype
);
6471 rtype
= TYPE_OBJC_INTERFACE (rtype
);
6473 /* If we could not find an @interface declaration, we must have
6474 only seen a @class declaration; so, we cannot say anything
6475 more intelligent about which methods the receiver will
6477 if (!rtype
|| TREE_CODE (rtype
) == IDENTIFIER_NODE
)
6480 /* We could not find an @interface declaration, yet Message maybe in a
6481 @class's protocol. */
6482 if (!method_prototype
&& rprotos
)
6484 = lookup_method_in_protocol_list (rprotos
, sel_name
, 0);
6486 else if (TREE_CODE (rtype
) == CLASS_INTERFACE_TYPE
6487 || TREE_CODE (rtype
) == CLASS_IMPLEMENTATION_TYPE
)
6489 /* We have a valid ObjC class name. Look up the method name
6490 in the published @interface for the class (and its
6493 = lookup_method_static (rtype
, sel_name
, class_tree
!= NULL_TREE
);
6495 /* If the method was not found in the @interface, it may still
6496 exist locally as part of the @implementation. */
6497 if (!method_prototype
&& objc_implementation_context
6498 && CLASS_NAME (objc_implementation_context
)
6499 == OBJC_TYPE_NAME (rtype
))
6503 ? CLASS_CLS_METHODS (objc_implementation_context
)
6504 : CLASS_NST_METHODS (objc_implementation_context
)),
6507 /* If we haven't found a candidate method by now, try looking for
6508 it in the protocol list. */
6509 if (!method_prototype
&& rprotos
)
6511 = lookup_method_in_protocol_list (rprotos
, sel_name
,
6512 class_tree
!= NULL_TREE
);
6516 warning (0, "invalid receiver type %qs",
6517 identifier_to_locale (gen_type_name (orig_rtype
)));
6518 /* After issuing the "invalid receiver" warning, perform method
6519 lookup as if we were messaging 'id'. */
6520 rtype
= rprotos
= NULL_TREE
;
6525 /* For 'id' or 'Class' receivers, search in the global hash table
6526 as a last resort. For all receivers, warn if protocol searches
6528 if (!method_prototype
)
6531 warning (0, "%<%c%E%> not found in protocol(s)",
6532 (class_tree
? '+' : '-'),
6537 = lookup_method_in_hash_lists (sel_name
, class_tree
!= NULL_TREE
);
6540 if (!method_prototype
)
6542 static bool warn_missing_methods
= false;
6545 warning (0, "%qE may not respond to %<%c%E%>",
6546 OBJC_TYPE_NAME (rtype
),
6547 (class_tree
? '+' : '-'),
6549 /* If we are messaging an 'id' or 'Class' object and made it here,
6550 then we have failed to find _any_ instance or class method,
6553 warning (0, "no %<%c%E%> method found",
6554 (class_tree
? '+' : '-'),
6557 if (!warn_missing_methods
)
6559 warning_at (input_location
,
6560 0, "(Messages without a matching method signature");
6561 warning_at (input_location
,
6562 0, "will be assumed to return %<id%> and accept");
6563 warning_at (input_location
,
6564 0, "%<...%> as arguments.)");
6565 warn_missing_methods
= true;
6569 /* Save the selector name for printing error messages. */
6570 current_objc_message_selector
= sel_name
;
6572 /* Build the parameters list for looking up the method.
6573 These are the object itself and the selector. */
6575 if (flag_typed_selectors
)
6576 selector
= build_typed_selector_reference (input_location
,
6577 sel_name
, method_prototype
);
6579 selector
= build_selector_reference (input_location
, sel_name
);
6581 retval
= build_objc_method_call (input_location
, super
, method_prototype
,
6583 selector
, method_params
);
6585 current_objc_message_selector
= 0;
6590 /* Build a tree expression to send OBJECT the operation SELECTOR,
6591 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
6592 assuming the method has prototype METHOD_PROTOTYPE.
6593 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
6594 LOC is the location of the expression to build.
6595 Use METHOD_PARAMS as list of args to pass to the method.
6596 If SUPER_FLAG is nonzero, we look up the superclass's method. */
6599 build_objc_method_call (location_t loc
, int super_flag
, tree method_prototype
,
6600 tree lookup_object
, tree selector
,
6603 tree sender
= (super_flag
? umsg_super_decl
:
6604 (!flag_next_runtime
|| flag_nil_receivers
6605 ? (flag_objc_direct_dispatch
6608 : umsg_nonnil_decl
));
6609 tree rcv_p
= (super_flag
? objc_super_type
: objc_object_type
);
6611 /* If a prototype for the method to be called exists, then cast
6612 the sender's return type and arguments to match that of the method.
6613 Otherwise, leave sender as is. */
6616 ? TREE_VALUE (TREE_TYPE (method_prototype
))
6617 : objc_object_type
);
6619 = build_pointer_type
6620 (build_function_type
6623 (method_prototype
, METHOD_REF
, super_flag
)));
6626 lookup_object
= build_c_cast (loc
, rcv_p
, lookup_object
);
6628 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
6629 lookup_object
= save_expr (lookup_object
);
6631 if (flag_next_runtime
)
6633 /* If we are returning a struct in memory, and the address
6634 of that memory location is passed as a hidden first
6635 argument, then change which messenger entry point this
6636 expr will call. NB: Note that sender_cast remains
6637 unchanged (it already has a struct return type). */
6638 if (!targetm
.calls
.struct_value_rtx (0, 0)
6639 && (TREE_CODE (ret_type
) == RECORD_TYPE
6640 || TREE_CODE (ret_type
) == UNION_TYPE
)
6641 && targetm
.calls
.return_in_memory (ret_type
, 0))
6642 sender
= (super_flag
? umsg_super_stret_decl
:
6643 flag_nil_receivers
? umsg_stret_decl
: umsg_nonnil_stret_decl
);
6645 method_params
= tree_cons (NULL_TREE
, lookup_object
,
6646 tree_cons (NULL_TREE
, selector
,
6648 method
= build_fold_addr_expr_loc (input_location
, sender
);
6652 /* This is the portable (GNU) way. */
6655 /* First, call the lookup function to get a pointer to the method,
6656 then cast the pointer, then call it with the method arguments. */
6658 object
= (super_flag
? self_decl
: lookup_object
);
6660 t
= tree_cons (NULL_TREE
, selector
, NULL_TREE
);
6661 t
= tree_cons (NULL_TREE
, lookup_object
, t
);
6662 method
= build_function_call (loc
, sender
, t
);
6664 /* Pass the object to the method. */
6665 method_params
= tree_cons (NULL_TREE
, object
,
6666 tree_cons (NULL_TREE
, selector
,
6670 /* ??? Selector is not at this point something we can use inside
6671 the compiler itself. Set it to garbage for the nonce. */
6672 t
= build3 (OBJ_TYPE_REF
, sender_cast
, method
, lookup_object
, size_zero_node
);
6673 return build_function_call (loc
,
6678 build_protocol_reference (tree p
)
6681 const char *proto_name
;
6683 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
6685 proto_name
= synth_id_with_class_suffix ("_OBJC_PROTOCOL", p
);
6686 decl
= start_var_decl (objc_protocol_template
, proto_name
);
6688 PROTOCOL_FORWARD_DECL (p
) = decl
;
6691 /* This function is called by the parser when (and only when) a
6692 @protocol() expression is found, in order to compile it. */
6694 objc_build_protocol_expr (tree protoname
)
6697 tree p
= lookup_protocol (protoname
);
6701 error ("cannot find protocol declaration for %qE",
6703 return error_mark_node
;
6706 if (!PROTOCOL_FORWARD_DECL (p
))
6707 build_protocol_reference (p
);
6709 expr
= build_unary_op (input_location
,
6710 ADDR_EXPR
, PROTOCOL_FORWARD_DECL (p
), 0);
6712 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
6713 if we have it, rather than converting it here. */
6714 expr
= convert (objc_protocol_type
, expr
);
6716 /* The @protocol() expression is being compiled into a pointer to a
6717 statically allocated instance of the Protocol class. To become
6718 usable at runtime, the 'isa' pointer of the instance need to be
6719 fixed up at runtime by the runtime library, to point to the
6720 actual 'Protocol' class. */
6722 /* For the GNU runtime, put the static Protocol instance in the list
6723 of statically allocated instances, so that we make sure that its
6724 'isa' pointer is fixed up at runtime by the GNU runtime library
6725 to point to the Protocol class (at runtime, when loading the
6726 module, the GNU runtime library loops on the statically allocated
6727 instances (as found in the defs field in objc_symtab) and fixups
6728 all the 'isa' pointers of those objects). */
6729 if (! flag_next_runtime
)
6731 /* This type is a struct containing the fields of a Protocol
6732 object. (Cfr. objc_protocol_type instead is the type of a pointer
6733 to such a struct). */
6734 tree protocol_struct_type
= xref_tag
6735 (RECORD_TYPE
, get_identifier (PROTOCOL_OBJECT_CLASS_NAME
));
6738 /* Look for the list of Protocol statically allocated instances
6739 to fixup at runtime. Create a new list to hold Protocol
6740 statically allocated instances, if the list is not found. At
6741 present there is only another list, holding NSConstantString
6742 static instances to be fixed up at runtime. */
6743 for (chain
= &objc_static_instances
;
6744 *chain
&& TREE_VALUE (*chain
) != protocol_struct_type
;
6745 chain
= &TREE_CHAIN (*chain
));
6748 *chain
= tree_cons (NULL_TREE
, protocol_struct_type
, NULL_TREE
);
6749 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type
),
6753 /* Add this statically allocated instance to the Protocol list. */
6754 TREE_PURPOSE (*chain
) = tree_cons (NULL_TREE
,
6755 PROTOCOL_FORWARD_DECL (p
),
6756 TREE_PURPOSE (*chain
));
6763 /* This function is called by the parser when a @selector() expression
6764 is found, in order to compile it. It is only called by the parser
6765 and only to compile a @selector(). LOC is the location of the
6768 objc_build_selector_expr (location_t loc
, tree selnamelist
)
6772 /* Obtain the full selector name. */
6773 if (TREE_CODE (selnamelist
) == IDENTIFIER_NODE
)
6774 /* A unary selector. */
6775 selname
= selnamelist
;
6776 else if (TREE_CODE (selnamelist
) == TREE_LIST
)
6777 selname
= build_keyword_selector (selnamelist
);
6781 /* If we are required to check @selector() expressions as they
6782 are found, check that the selector has been declared. */
6783 if (warn_undeclared_selector
)
6785 /* Look the selector up in the list of all known class and
6786 instance methods (up to this line) to check that the selector
6790 /* First try with instance methods. */
6791 hsh
= hash_lookup (nst_method_hash_list
, selname
);
6793 /* If not found, try with class methods. */
6796 hsh
= hash_lookup (cls_method_hash_list
, selname
);
6799 /* If still not found, print out a warning. */
6802 warning (0, "undeclared selector %qE", selname
);
6807 if (flag_typed_selectors
)
6808 return build_typed_selector_reference (loc
, selname
, 0);
6810 return build_selector_reference (loc
, selname
);
6813 /* This is used to implement @encode(). See gcc/doc/objc.texi,
6814 section '@encode'. */
6816 objc_build_encode_expr (tree type
)
6821 encode_type (type
, obstack_object_size (&util_obstack
),
6822 OBJC_ENCODE_INLINE_DEFS
);
6823 obstack_1grow (&util_obstack
, 0); /* null terminate string */
6824 string
= XOBFINISH (&util_obstack
, const char *);
6826 /* Synthesize a string that represents the encoded struct/union. */
6827 result
= my_build_string (strlen (string
) + 1, string
);
6828 obstack_free (&util_obstack
, util_firstobj
);
6833 build_ivar_reference (tree id
)
6835 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
)
6837 /* Historically, a class method that produced objects (factory
6838 method) would assign `self' to the instance that it
6839 allocated. This would effectively turn the class method into
6840 an instance method. Following this assignment, the instance
6841 variables could be accessed. That practice, while safe,
6842 violates the simple rule that a class method should not refer
6843 to an instance variable. It's better to catch the cases
6844 where this is done unknowingly than to support the above
6846 warning (0, "instance variable %qE accessed in class method",
6848 self_decl
= convert (objc_instance_type
, self_decl
); /* cast */
6851 return objc_build_component_ref (build_indirect_ref (input_location
,
6852 self_decl
, RO_ARROW
),
6856 /* Compute a hash value for a given method SEL_NAME. */
6859 hash_func (tree sel_name
)
6861 const unsigned char *s
6862 = (const unsigned char *)IDENTIFIER_POINTER (sel_name
);
6866 h
= h
* 67 + *s
++ - 113;
6873 nst_method_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
6874 cls_method_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
6876 /* Initialize the hash table used to hold the constant string objects. */
6877 string_htab
= htab_create_ggc (31, string_hash
,
6880 /* Initialize the hash table used to hold EH-volatilized types. */
6881 volatilized_htab
= htab_create_ggc (31, volatilized_hash
,
6882 volatilized_eq
, NULL
);
6885 /* WARNING!!!! hash_enter is called with a method, and will peek
6886 inside to find its selector! But hash_lookup is given a selector
6887 directly, and looks for the selector that's inside the found
6888 entry's key (method) for comparison. */
6891 hash_enter (hash
*hashlist
, tree method
)
6894 int slot
= hash_func (METHOD_SEL_NAME (method
)) % SIZEHASHTABLE
;
6896 obj
= ggc_alloc_hashed_entry ();
6898 obj
->next
= hashlist
[slot
];
6901 hashlist
[slot
] = obj
; /* append to front */
6905 hash_lookup (hash
*hashlist
, tree sel_name
)
6909 target
= hashlist
[hash_func (sel_name
) % SIZEHASHTABLE
];
6913 if (sel_name
== METHOD_SEL_NAME (target
->key
))
6916 target
= target
->next
;
6922 hash_add_attr (hash entry
, tree value
)
6926 obj
= ggc_alloc_hashed_attribute ();
6927 obj
->next
= entry
->list
;
6930 entry
->list
= obj
; /* append to front */
6934 lookup_method (tree mchain
, tree method
)
6938 if (TREE_CODE (method
) == IDENTIFIER_NODE
)
6941 key
= METHOD_SEL_NAME (method
);
6945 if (METHOD_SEL_NAME (mchain
) == key
)
6948 mchain
= DECL_CHAIN (mchain
);
6953 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance method
6954 in INTERFACE, along with any categories and protocols attached thereto.
6955 If method is not found, and the OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS,
6956 recursively examine the INTERFACE's superclass. If OBJC_LOOKUP_CLASS is
6957 set, OBJC_LOOKUP_NO_SUPER is cleared, and no suitable class method could
6958 be found in INTERFACE or any of its superclasses, look for an _instance_
6959 method of the same name in the root class as a last resort.
6961 If a suitable method cannot be found, return NULL_TREE. */
6964 lookup_method_static (tree interface
, tree ident
, int flags
)
6966 tree meth
= NULL_TREE
, root_inter
= NULL_TREE
;
6967 tree inter
= interface
;
6968 int is_class
= (flags
& OBJC_LOOKUP_CLASS
);
6969 int no_superclasses
= (flags
& OBJC_LOOKUP_NO_SUPER
);
6973 tree chain
= is_class
? CLASS_CLS_METHODS (inter
) : CLASS_NST_METHODS (inter
);
6974 tree category
= inter
;
6976 /* First, look up the method in the class itself. */
6977 if ((meth
= lookup_method (chain
, ident
)))
6980 /* Failing that, look for the method in each category of the class. */
6981 while ((category
= CLASS_CATEGORY_LIST (category
)))
6983 chain
= is_class
? CLASS_CLS_METHODS (category
) : CLASS_NST_METHODS (category
);
6985 /* Check directly in each category. */
6986 if ((meth
= lookup_method (chain
, ident
)))
6989 /* Failing that, check in each category's protocols. */
6990 if (CLASS_PROTOCOL_LIST (category
))
6992 if ((meth
= (lookup_method_in_protocol_list
6993 (CLASS_PROTOCOL_LIST (category
), ident
, is_class
))))
6998 /* If not found in categories, check in protocols of the main class. */
6999 if (CLASS_PROTOCOL_LIST (inter
))
7001 if ((meth
= (lookup_method_in_protocol_list
7002 (CLASS_PROTOCOL_LIST (inter
), ident
, is_class
))))
7006 /* If we were instructed not to look in superclasses, don't. */
7007 if (no_superclasses
)
7010 /* Failing that, climb up the inheritance hierarchy. */
7012 inter
= lookup_interface (CLASS_SUPER_NAME (inter
));
7016 /* If no class (factory) method was found, check if an _instance_
7017 method of the same name exists in the root class. This is what
7018 the Objective-C runtime will do. If an instance method was not
7020 return is_class
? lookup_method_static (root_inter
, ident
, 0): NULL_TREE
;
7023 /* Add the method to the hash list if it doesn't contain an identical
7027 add_method_to_hash_list (hash
*hash_list
, tree method
)
7031 if (!(hsh
= hash_lookup (hash_list
, METHOD_SEL_NAME (method
))))
7033 /* Install on a global chain. */
7034 hash_enter (hash_list
, method
);
7038 /* Check types against those; if different, add to a list. */
7040 int already_there
= comp_proto_with_proto (method
, hsh
->key
, 1);
7041 for (loop
= hsh
->list
; !already_there
&& loop
; loop
= loop
->next
)
7042 already_there
|= comp_proto_with_proto (method
, loop
->value
, 1);
7044 hash_add_attr (hsh
, method
);
7049 objc_add_method (tree klass
, tree method
, int is_class
)
7053 if (!(mth
= lookup_method (is_class
7054 ? CLASS_CLS_METHODS (klass
)
7055 : CLASS_NST_METHODS (klass
), method
)))
7057 /* put method on list in reverse order */
7060 DECL_CHAIN (method
) = CLASS_CLS_METHODS (klass
);
7061 CLASS_CLS_METHODS (klass
) = method
;
7065 DECL_CHAIN (method
) = CLASS_NST_METHODS (klass
);
7066 CLASS_NST_METHODS (klass
) = method
;
7071 /* When processing an @interface for a class or category, give hard
7072 errors on methods with identical selectors but differing argument
7073 and/or return types. We do not do this for @implementations, because
7074 C/C++ will do it for us (i.e., there will be duplicate function
7075 definition errors). */
7076 if ((TREE_CODE (klass
) == CLASS_INTERFACE_TYPE
7077 || TREE_CODE (klass
) == CATEGORY_INTERFACE_TYPE
)
7078 && !comp_proto_with_proto (method
, mth
, 1))
7079 error ("duplicate declaration of method %<%c%E%>",
7080 is_class
? '+' : '-',
7081 METHOD_SEL_NAME (mth
));
7085 add_method_to_hash_list (cls_method_hash_list
, method
);
7088 add_method_to_hash_list (nst_method_hash_list
, method
);
7090 /* Instance methods in root classes (and categories thereof)
7091 may act as class methods as a last resort. We also add
7092 instance methods listed in @protocol declarations to
7093 the class hash table, on the assumption that @protocols
7094 may be adopted by root classes or categories. */
7095 if (TREE_CODE (klass
) == CATEGORY_INTERFACE_TYPE
7096 || TREE_CODE (klass
) == CATEGORY_IMPLEMENTATION_TYPE
)
7097 klass
= lookup_interface (CLASS_NAME (klass
));
7099 if (TREE_CODE (klass
) == PROTOCOL_INTERFACE_TYPE
7100 || !CLASS_SUPER_NAME (klass
))
7101 add_method_to_hash_list (cls_method_hash_list
, method
);
7108 add_class (tree class_name
, tree name
)
7110 struct interface_tuple
**slot
;
7112 /* Put interfaces on list in reverse order. */
7113 TREE_CHAIN (class_name
) = interface_chain
;
7114 interface_chain
= class_name
;
7116 if (interface_htab
== NULL
)
7117 interface_htab
= htab_create_ggc (31, hash_interface
, eq_interface
, NULL
);
7118 slot
= (struct interface_tuple
**)
7119 htab_find_slot_with_hash (interface_htab
, name
,
7120 IDENTIFIER_HASH_VALUE (name
),
7124 *slot
= ggc_alloc_cleared_interface_tuple ();
7127 (*slot
)->class_name
= class_name
;
7129 return interface_chain
;
7133 add_category (tree klass
, tree category
)
7135 /* Put categories on list in reverse order. */
7136 tree cat
= lookup_category (klass
, CLASS_SUPER_NAME (category
));
7140 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
7142 CLASS_SUPER_NAME (category
));
7146 CLASS_CATEGORY_LIST (category
) = CLASS_CATEGORY_LIST (klass
);
7147 CLASS_CATEGORY_LIST (klass
) = category
;
7151 /* Called after parsing each instance variable declaration. Necessary to
7152 preserve typedefs and implement public/private...
7154 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
7157 add_instance_variable (tree klass
, int visibility
, tree field_decl
)
7159 tree field_type
= TREE_TYPE (field_decl
);
7160 const char *ivar_name
= DECL_NAME (field_decl
)
7161 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl
)))
7165 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
7167 error ("illegal reference type specified for instance variable %qs",
7169 /* Return class as is without adding this ivar. */
7174 if (field_type
== error_mark_node
|| !TYPE_SIZE (field_type
)
7175 || TYPE_SIZE (field_type
) == error_mark_node
)
7176 /* 'type[0]' is allowed, but 'type[]' is not! */
7178 error ("instance variable %qs has unknown size", ivar_name
);
7179 /* Return class as is without adding this ivar. */
7184 /* Check if the ivar being added has a non-POD C++ type. If so, we will
7185 need to either (1) warn the user about it or (2) generate suitable
7186 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
7187 methods (if '-fobjc-call-cxx-cdtors' was specified). */
7188 if (MAYBE_CLASS_TYPE_P (field_type
)
7189 && (TYPE_NEEDS_CONSTRUCTING (field_type
)
7190 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
)
7191 || TYPE_POLYMORPHIC_P (field_type
)))
7193 tree type_name
= OBJC_TYPE_NAME (field_type
);
7195 if (flag_objc_call_cxx_cdtors
)
7197 /* Since the ObjC runtime will be calling the constructors and
7198 destructors for us, the only thing we can't handle is the lack
7199 of a default constructor. */
7200 if (TYPE_NEEDS_CONSTRUCTING (field_type
)
7201 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type
))
7203 warning (0, "type %qE has no default constructor to call",
7206 /* If we cannot call a constructor, we should also avoid
7207 calling the destructor, for symmetry. */
7208 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
))
7209 warning (0, "destructor for %qE shall not be run either",
7215 static bool warn_cxx_ivars
= false;
7217 if (TYPE_POLYMORPHIC_P (field_type
))
7219 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
7221 error ("type %qE has virtual member functions", type_name
);
7222 error ("illegal aggregate type %qE specified "
7223 "for instance variable %qs",
7224 type_name
, ivar_name
);
7225 /* Return class as is without adding this ivar. */
7229 /* User-defined constructors and destructors are not known to Obj-C
7230 and hence will not be called. This may or may not be a problem. */
7231 if (TYPE_NEEDS_CONSTRUCTING (field_type
))
7232 warning (0, "type %qE has a user-defined constructor", type_name
);
7233 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
))
7234 warning (0, "type %qE has a user-defined destructor", type_name
);
7236 if (!warn_cxx_ivars
)
7238 warning (0, "C++ constructors and destructors will not "
7239 "be invoked for Objective-C fields");
7240 warn_cxx_ivars
= true;
7246 /* Overload the public attribute, it is not used for FIELD_DECLs. */
7250 TREE_PUBLIC (field_decl
) = 0;
7251 TREE_PRIVATE (field_decl
) = 0;
7252 TREE_PROTECTED (field_decl
) = 1;
7256 TREE_PUBLIC (field_decl
) = 1;
7257 TREE_PRIVATE (field_decl
) = 0;
7258 TREE_PROTECTED (field_decl
) = 0;
7262 TREE_PUBLIC (field_decl
) = 0;
7263 TREE_PRIVATE (field_decl
) = 1;
7264 TREE_PROTECTED (field_decl
) = 0;
7269 CLASS_RAW_IVARS (klass
) = chainon (CLASS_RAW_IVARS (klass
), field_decl
);
7275 is_ivar (tree decl_chain
, tree ident
)
7277 for ( ; decl_chain
; decl_chain
= DECL_CHAIN (decl_chain
))
7278 if (DECL_NAME (decl_chain
) == ident
)
7283 /* True if the ivar is private and we are not in its implementation. */
7286 is_private (tree decl
)
7288 return (TREE_PRIVATE (decl
)
7289 && ! is_ivar (CLASS_IVARS (implementation_template
),
7293 /* We have an instance variable reference;, check to see if it is public. */
7296 objc_is_public (tree expr
, tree identifier
)
7298 tree basetype
, decl
;
7301 if (processing_template_decl
)
7305 if (TREE_TYPE (expr
) == error_mark_node
)
7308 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
7310 if (basetype
&& TREE_CODE (basetype
) == RECORD_TYPE
)
7312 if (TYPE_HAS_OBJC_INFO (basetype
) && TYPE_OBJC_INTERFACE (basetype
))
7314 tree klass
= lookup_interface (OBJC_TYPE_NAME (basetype
));
7318 error ("cannot find interface declaration for %qE",
7319 OBJC_TYPE_NAME (basetype
));
7323 if ((decl
= is_ivar (get_class_ivars (klass
, true), identifier
)))
7325 if (TREE_PUBLIC (decl
))
7328 /* Important difference between the Stepstone translator:
7329 all instance variables should be public within the context
7330 of the implementation. */
7331 if (objc_implementation_context
7332 && ((TREE_CODE (objc_implementation_context
)
7333 == CLASS_IMPLEMENTATION_TYPE
)
7334 || (TREE_CODE (objc_implementation_context
)
7335 == CATEGORY_IMPLEMENTATION_TYPE
)))
7337 tree curtype
= TYPE_MAIN_VARIANT
7338 (CLASS_STATIC_TEMPLATE
7339 (implementation_template
));
7341 if (basetype
== curtype
7342 || DERIVED_FROM_P (basetype
, curtype
))
7344 int priv
= is_private (decl
);
7347 error ("instance variable %qE is declared private",
7354 /* The 2.95.2 compiler sometimes allowed C functions to access
7355 non-@public ivars. We will let this slide for now... */
7356 if (!objc_method_context
)
7358 warning (0, "instance variable %qE is %s; "
7359 "this will be a hard error in the future",
7361 TREE_PRIVATE (decl
) ? "@private" : "@protected");
7365 error ("instance variable %qE is declared %s",
7367 TREE_PRIVATE (decl
) ? "private" : "protected");
7376 /* Make sure all entries in CHAIN are also in LIST. */
7379 check_methods (tree chain
, tree list
, int mtype
)
7385 if (!lookup_method (list
, chain
))
7389 if (TREE_CODE (objc_implementation_context
)
7390 == CLASS_IMPLEMENTATION_TYPE
)
7391 warning (0, "incomplete implementation of class %qE",
7392 CLASS_NAME (objc_implementation_context
));
7393 else if (TREE_CODE (objc_implementation_context
)
7394 == CATEGORY_IMPLEMENTATION_TYPE
)
7395 warning (0, "incomplete implementation of category %qE",
7396 CLASS_SUPER_NAME (objc_implementation_context
));
7400 warning (0, "method definition for %<%c%E%> not found",
7401 mtype
, METHOD_SEL_NAME (chain
));
7404 chain
= DECL_CHAIN (chain
);
7410 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
7413 conforms_to_protocol (tree klass
, tree protocol
)
7415 if (TREE_CODE (protocol
) == PROTOCOL_INTERFACE_TYPE
)
7417 tree p
= CLASS_PROTOCOL_LIST (klass
);
7418 while (p
&& TREE_VALUE (p
) != protocol
)
7423 tree super
= (CLASS_SUPER_NAME (klass
)
7424 ? lookup_interface (CLASS_SUPER_NAME (klass
))
7426 int tmp
= super
? conforms_to_protocol (super
, protocol
) : 0;
7435 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
7436 CONTEXT. This is one of two mechanisms to check protocol integrity. */
7439 check_methods_accessible (tree chain
, tree context
, int mtype
)
7443 tree base_context
= context
;
7447 context
= base_context
;
7451 list
= CLASS_CLS_METHODS (context
);
7453 list
= CLASS_NST_METHODS (context
);
7455 if (lookup_method (list
, chain
))
7458 else if (TREE_CODE (context
) == CLASS_IMPLEMENTATION_TYPE
7459 || TREE_CODE (context
) == CLASS_INTERFACE_TYPE
)
7460 context
= (CLASS_SUPER_NAME (context
)
7461 ? lookup_interface (CLASS_SUPER_NAME (context
))
7464 else if (TREE_CODE (context
) == CATEGORY_IMPLEMENTATION_TYPE
7465 || TREE_CODE (context
) == CATEGORY_INTERFACE_TYPE
)
7466 context
= (CLASS_NAME (context
)
7467 ? lookup_interface (CLASS_NAME (context
))
7473 if (context
== NULL_TREE
)
7477 if (TREE_CODE (objc_implementation_context
)
7478 == CLASS_IMPLEMENTATION_TYPE
)
7479 warning (0, "incomplete implementation of class %qE",
7480 CLASS_NAME (objc_implementation_context
));
7481 else if (TREE_CODE (objc_implementation_context
)
7482 == CATEGORY_IMPLEMENTATION_TYPE
)
7483 warning (0, "incomplete implementation of category %qE",
7484 CLASS_SUPER_NAME (objc_implementation_context
));
7487 warning (0, "method definition for %<%c%E%> not found",
7488 mtype
, METHOD_SEL_NAME (chain
));
7491 chain
= TREE_CHAIN (chain
); /* next method... */
7496 /* Check whether the current interface (accessible via
7497 'objc_implementation_context') actually implements protocol P, along
7498 with any protocols that P inherits. */
7501 check_protocol (tree p
, const char *type
, tree name
)
7503 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
7507 /* Ensure that all protocols have bodies! */
7510 f1
= check_methods (PROTOCOL_CLS_METHODS (p
),
7511 CLASS_CLS_METHODS (objc_implementation_context
),
7513 f2
= check_methods (PROTOCOL_NST_METHODS (p
),
7514 CLASS_NST_METHODS (objc_implementation_context
),
7519 f1
= check_methods_accessible (PROTOCOL_CLS_METHODS (p
),
7520 objc_implementation_context
,
7522 f2
= check_methods_accessible (PROTOCOL_NST_METHODS (p
),
7523 objc_implementation_context
,
7528 warning (0, "%s %qE does not fully implement the %qE protocol",
7529 type
, name
, PROTOCOL_NAME (p
));
7532 /* Check protocols recursively. */
7533 if (PROTOCOL_LIST (p
))
7535 tree subs
= PROTOCOL_LIST (p
);
7537 lookup_interface (CLASS_SUPER_NAME (implementation_template
));
7541 tree sub
= TREE_VALUE (subs
);
7543 /* If the superclass does not conform to the protocols
7544 inherited by P, then we must! */
7545 if (!super_class
|| !conforms_to_protocol (super_class
, sub
))
7546 check_protocol (sub
, type
, name
);
7547 subs
= TREE_CHAIN (subs
);
7552 /* Check whether the current interface (accessible via
7553 'objc_implementation_context') actually implements the protocols listed
7557 check_protocols (tree proto_list
, const char *type
, tree name
)
7559 for ( ; proto_list
; proto_list
= TREE_CHAIN (proto_list
))
7561 tree p
= TREE_VALUE (proto_list
);
7563 check_protocol (p
, type
, name
);
7567 /* Make sure that the class CLASS_NAME is defined
7568 CODE says which kind of thing CLASS_NAME ought to be.
7569 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
7570 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
7573 start_class (enum tree_code code
, tree class_name
, tree super_name
,
7579 if (current_namespace
!= global_namespace
) {
7580 error ("Objective-C declarations may only appear in global scope");
7582 #endif /* OBJCPLUS */
7584 if (objc_implementation_context
)
7586 warning (0, "%<@end%> missing in implementation context");
7587 finish_class (objc_implementation_context
);
7588 objc_ivar_chain
= NULL_TREE
;
7589 objc_implementation_context
= NULL_TREE
;
7592 klass
= make_node (code
);
7593 TYPE_LANG_SLOT_1 (klass
) = make_tree_vec (CLASS_LANG_SLOT_ELTS
);
7595 /* Check for existence of the super class, if one was specified. Note
7596 that we must have seen an @interface, not just a @class. If we
7597 are looking at a @compatibility_alias, traverse it first. */
7598 if ((code
== CLASS_INTERFACE_TYPE
|| code
== CLASS_IMPLEMENTATION_TYPE
)
7601 tree super
= objc_is_class_name (super_name
);
7603 if (!super
|| !lookup_interface (super
))
7605 error ("cannot find interface declaration for %qE, superclass of %qE",
7606 super
? super
: super_name
,
7608 super_name
= NULL_TREE
;
7614 CLASS_NAME (klass
) = class_name
;
7615 CLASS_SUPER_NAME (klass
) = super_name
;
7616 CLASS_CLS_METHODS (klass
) = NULL_TREE
;
7618 if (! objc_is_class_name (class_name
)
7619 && (decl
= lookup_name (class_name
)))
7621 error ("%qE redeclared as different kind of symbol",
7623 error ("previous declaration of %q+D",
7627 if (code
== CLASS_IMPLEMENTATION_TYPE
)
7632 for (chain
= implemented_classes
; chain
; chain
= TREE_CHAIN (chain
))
7633 if (TREE_VALUE (chain
) == class_name
)
7635 error ("reimplementation of class %qE",
7637 return error_mark_node
;
7639 implemented_classes
= tree_cons (NULL_TREE
, class_name
,
7640 implemented_classes
);
7643 /* Reset for multiple classes per file. */
7646 objc_implementation_context
= klass
;
7648 /* Lookup the interface for this implementation. */
7650 if (!(implementation_template
= lookup_interface (class_name
)))
7652 warning (0, "cannot find interface declaration for %qE",
7654 add_class (implementation_template
= objc_implementation_context
,
7658 /* If a super class has been specified in the implementation,
7659 insure it conforms to the one specified in the interface. */
7662 && (super_name
!= CLASS_SUPER_NAME (implementation_template
)))
7664 tree previous_name
= CLASS_SUPER_NAME (implementation_template
);
7665 error ("conflicting super class name %qE",
7668 error ("previous declaration of %qE", previous_name
);
7670 error ("previous declaration");
7673 else if (! super_name
)
7675 CLASS_SUPER_NAME (objc_implementation_context
)
7676 = CLASS_SUPER_NAME (implementation_template
);
7680 else if (code
== CLASS_INTERFACE_TYPE
)
7682 if (lookup_interface (class_name
))
7684 error ("duplicate interface declaration for class %qE",
7686 warning (0, "duplicate interface declaration for class %qE",
7690 add_class (klass
, class_name
);
7693 CLASS_PROTOCOL_LIST (klass
)
7694 = lookup_and_install_protocols (protocol_list
);
7697 else if (code
== CATEGORY_INTERFACE_TYPE
)
7699 tree class_category_is_assoc_with
;
7701 /* For a category, class_name is really the name of the class that
7702 the following set of methods will be associated with. We must
7703 find the interface so that can derive the objects template. */
7705 if (!(class_category_is_assoc_with
= lookup_interface (class_name
)))
7707 error ("cannot find interface declaration for %qE",
7709 exit (FATAL_EXIT_CODE
);
7712 add_category (class_category_is_assoc_with
, klass
);
7715 CLASS_PROTOCOL_LIST (klass
)
7716 = lookup_and_install_protocols (protocol_list
);
7719 else if (code
== CATEGORY_IMPLEMENTATION_TYPE
)
7721 /* Reset for multiple classes per file. */
7724 objc_implementation_context
= klass
;
7726 /* For a category, class_name is really the name of the class that
7727 the following set of methods will be associated with. We must
7728 find the interface so that can derive the objects template. */
7730 if (!(implementation_template
= lookup_interface (class_name
)))
7732 error ("cannot find interface declaration for %qE",
7734 exit (FATAL_EXIT_CODE
);
7741 continue_class (tree klass
)
7743 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
7744 || TREE_CODE (klass
) == CATEGORY_IMPLEMENTATION_TYPE
)
7746 struct imp_entry
*imp_entry
;
7748 /* Check consistency of the instance variables. */
7750 if (CLASS_RAW_IVARS (klass
))
7751 check_ivars (implementation_template
, klass
);
7753 /* code generation */
7756 push_lang_context (lang_name_c
);
7759 build_private_template (implementation_template
);
7760 uprivate_record
= CLASS_STATIC_TEMPLATE (implementation_template
);
7761 objc_instance_type
= build_pointer_type (uprivate_record
);
7763 imp_entry
= ggc_alloc_imp_entry ();
7765 imp_entry
->next
= imp_list
;
7766 imp_entry
->imp_context
= klass
;
7767 imp_entry
->imp_template
= implementation_template
;
7769 synth_forward_declarations ();
7770 imp_entry
->class_decl
= UOBJC_CLASS_decl
;
7771 imp_entry
->meta_decl
= UOBJC_METACLASS_decl
;
7772 imp_entry
->has_cxx_cdtors
= 0;
7774 /* Append to front and increment count. */
7775 imp_list
= imp_entry
;
7776 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
)
7782 pop_lang_context ();
7783 #endif /* OBJCPLUS */
7785 return get_class_ivars (implementation_template
, true);
7788 else if (TREE_CODE (klass
) == CLASS_INTERFACE_TYPE
)
7791 push_lang_context (lang_name_c
);
7792 #endif /* OBJCPLUS */
7794 objc_collecting_ivars
= 1;
7795 build_private_template (klass
);
7796 objc_collecting_ivars
= 0;
7799 pop_lang_context ();
7800 #endif /* OBJCPLUS */
7806 return error_mark_node
;
7809 /* This is called once we see the "@end" in an interface/implementation. */
7812 finish_class (tree klass
)
7814 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
)
7816 /* All code generation is done in finish_objc. */
7818 if (implementation_template
!= objc_implementation_context
)
7820 /* Ensure that all method listed in the interface contain bodies. */
7821 check_methods (CLASS_CLS_METHODS (implementation_template
),
7822 CLASS_CLS_METHODS (objc_implementation_context
), '+');
7823 check_methods (CLASS_NST_METHODS (implementation_template
),
7824 CLASS_NST_METHODS (objc_implementation_context
), '-');
7826 if (CLASS_PROTOCOL_LIST (implementation_template
))
7827 check_protocols (CLASS_PROTOCOL_LIST (implementation_template
),
7829 CLASS_NAME (objc_implementation_context
));
7833 else if (TREE_CODE (klass
) == CATEGORY_IMPLEMENTATION_TYPE
)
7835 tree category
= lookup_category (implementation_template
, CLASS_SUPER_NAME (klass
));
7839 /* Ensure all method listed in the interface contain bodies. */
7840 check_methods (CLASS_CLS_METHODS (category
),
7841 CLASS_CLS_METHODS (objc_implementation_context
), '+');
7842 check_methods (CLASS_NST_METHODS (category
),
7843 CLASS_NST_METHODS (objc_implementation_context
), '-');
7845 if (CLASS_PROTOCOL_LIST (category
))
7846 check_protocols (CLASS_PROTOCOL_LIST (category
),
7848 CLASS_SUPER_NAME (objc_implementation_context
));
7854 add_protocol (tree protocol
)
7856 /* Put protocol on list in reverse order. */
7857 TREE_CHAIN (protocol
) = protocol_chain
;
7858 protocol_chain
= protocol
;
7859 return protocol_chain
;
7863 lookup_protocol (tree ident
)
7867 for (chain
= protocol_chain
; chain
; chain
= TREE_CHAIN (chain
))
7868 if (ident
== PROTOCOL_NAME (chain
))
7874 /* This function forward declares the protocols named by NAMES. If
7875 they are already declared or defined, the function has no effect. */
7878 objc_declare_protocols (tree names
)
7883 if (current_namespace
!= global_namespace
) {
7884 error ("Objective-C declarations may only appear in global scope");
7886 #endif /* OBJCPLUS */
7888 for (list
= names
; list
; list
= TREE_CHAIN (list
))
7890 tree name
= TREE_VALUE (list
);
7892 if (lookup_protocol (name
) == NULL_TREE
)
7894 tree protocol
= make_node (PROTOCOL_INTERFACE_TYPE
);
7896 TYPE_LANG_SLOT_1 (protocol
)
7897 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS
);
7898 PROTOCOL_NAME (protocol
) = name
;
7899 PROTOCOL_LIST (protocol
) = NULL_TREE
;
7900 add_protocol (protocol
);
7901 PROTOCOL_DEFINED (protocol
) = 0;
7902 PROTOCOL_FORWARD_DECL (protocol
) = NULL_TREE
;
7908 start_protocol (enum tree_code code
, tree name
, tree list
)
7913 if (current_namespace
!= global_namespace
) {
7914 error ("Objective-C declarations may only appear in global scope");
7916 #endif /* OBJCPLUS */
7918 protocol
= lookup_protocol (name
);
7922 protocol
= make_node (code
);
7923 TYPE_LANG_SLOT_1 (protocol
) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS
);
7925 PROTOCOL_NAME (protocol
) = name
;
7926 PROTOCOL_LIST (protocol
) = lookup_and_install_protocols (list
);
7927 add_protocol (protocol
);
7928 PROTOCOL_DEFINED (protocol
) = 1;
7929 PROTOCOL_FORWARD_DECL (protocol
) = NULL_TREE
;
7931 check_protocol_recursively (protocol
, list
);
7933 else if (! PROTOCOL_DEFINED (protocol
))
7935 PROTOCOL_DEFINED (protocol
) = 1;
7936 PROTOCOL_LIST (protocol
) = lookup_and_install_protocols (list
);
7938 check_protocol_recursively (protocol
, list
);
7942 warning (0, "duplicate declaration for protocol %qE",
7949 /* "Encode" a data type into a string, which grows in util_obstack.
7951 The format is described in gcc/doc/objc.texi, section 'Type
7954 Most of the encode_xxx functions have a 'type' argument, which is
7955 the type to encode, and an integer 'curtype' argument, which is the
7956 index in the encoding string of the beginning of the encoding of
7957 the current type, and allows you to find what characters have
7958 already been written for the current type (they are the ones in the
7959 current encoding string starting from 'curtype').
7961 For example, if we are encoding a method which returns 'int' and
7962 takes a 'char **' argument, then when we get to the point of
7963 encoding the 'char **' argument, the encoded string already
7964 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
7965 'curtype' will be set to 7 when starting to encode 'char **'.
7966 During the whole of the encoding of 'char **', 'curtype' will be
7967 fixed at 7, so the routine encoding the second pointer can find out
7968 that it's actually encoding a pointer to a pointer by looking
7969 backwards at what has already been encoded for the current type,
7970 and seeing there is a "^" (meaning a pointer) in there.
7974 /* Encode type qualifiers encodes one of the "PQ" Objective-C
7975 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
7976 'const', instead, is encoded directly as part of the type.
7980 encode_type_qualifiers (tree declspecs
)
7984 for (spec
= declspecs
; spec
; spec
= TREE_CHAIN (spec
))
7986 /* FIXME: Shouldn't we use token->keyword here ? */
7987 if (ridpointers
[(int) RID_IN
] == TREE_VALUE (spec
))
7988 obstack_1grow (&util_obstack
, 'n');
7989 else if (ridpointers
[(int) RID_INOUT
] == TREE_VALUE (spec
))
7990 obstack_1grow (&util_obstack
, 'N');
7991 else if (ridpointers
[(int) RID_OUT
] == TREE_VALUE (spec
))
7992 obstack_1grow (&util_obstack
, 'o');
7993 else if (ridpointers
[(int) RID_BYCOPY
] == TREE_VALUE (spec
))
7994 obstack_1grow (&util_obstack
, 'O');
7995 else if (ridpointers
[(int) RID_BYREF
] == TREE_VALUE (spec
))
7996 obstack_1grow (&util_obstack
, 'R');
7997 else if (ridpointers
[(int) RID_ONEWAY
] == TREE_VALUE (spec
))
7998 obstack_1grow (&util_obstack
, 'V');
8002 /* Determine if a pointee is marked read-only. Only used by the NeXT
8003 runtime to be compatible with gcc-3.3. */
8006 pointee_is_readonly (tree pointee
)
8008 while (POINTER_TYPE_P (pointee
))
8009 pointee
= TREE_TYPE (pointee
);
8011 return TYPE_READONLY (pointee
);
8014 /* Encode a pointer type. */
8017 encode_pointer (tree type
, int curtype
, int format
)
8019 tree pointer_to
= TREE_TYPE (type
);
8021 if (flag_next_runtime
)
8023 /* This code is used to be compatible with gcc-3.3. */
8024 /* For historical/compatibility reasons, the read-only qualifier
8025 of the pointee gets emitted _before_ the '^'. The read-only
8026 qualifier of the pointer itself gets ignored, _unless_ we are
8027 looking at a typedef! Also, do not emit the 'r' for anything
8028 but the outermost type! */
8029 if (!generating_instance_variables
8030 && (obstack_object_size (&util_obstack
) - curtype
<= 1)
8031 && (TYPE_NAME (type
) && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
8032 ? TYPE_READONLY (type
)
8033 : pointee_is_readonly (pointer_to
)))
8034 obstack_1grow (&util_obstack
, 'r');
8037 if (TREE_CODE (pointer_to
) == RECORD_TYPE
)
8039 if (OBJC_TYPE_NAME (pointer_to
)
8040 && TREE_CODE (OBJC_TYPE_NAME (pointer_to
)) == IDENTIFIER_NODE
)
8042 const char *name
= IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to
));
8044 if (strcmp (name
, TAG_OBJECT
) == 0) /* '@' */
8046 obstack_1grow (&util_obstack
, '@');
8049 else if (TYPE_HAS_OBJC_INFO (pointer_to
)
8050 && TYPE_OBJC_INTERFACE (pointer_to
))
8052 if (generating_instance_variables
)
8054 obstack_1grow (&util_obstack
, '@');
8055 obstack_1grow (&util_obstack
, '"');
8056 obstack_grow (&util_obstack
, name
, strlen (name
));
8057 obstack_1grow (&util_obstack
, '"');
8062 obstack_1grow (&util_obstack
, '@');
8066 else if (strcmp (name
, TAG_CLASS
) == 0) /* '#' */
8068 obstack_1grow (&util_obstack
, '#');
8071 else if (strcmp (name
, TAG_SELECTOR
) == 0) /* ':' */
8073 obstack_1grow (&util_obstack
, ':');
8078 else if (TREE_CODE (pointer_to
) == INTEGER_TYPE
8079 && TYPE_MODE (pointer_to
) == QImode
)
8081 tree pname
= TREE_CODE (OBJC_TYPE_NAME (pointer_to
)) == IDENTIFIER_NODE
8082 ? OBJC_TYPE_NAME (pointer_to
)
8083 : DECL_NAME (OBJC_TYPE_NAME (pointer_to
));
8085 /* (BOOL *) are an exception and are encoded as ^c, while all
8086 other pointers to char are encoded as *. */
8087 if (strcmp (IDENTIFIER_POINTER (pname
), "BOOL"))
8089 if (!flag_next_runtime
)
8091 /* The NeXT runtime adds the 'r' before getting here. */
8093 /* It appears that "r*" means "const char *" rather than
8094 "char *const". "char *const" is encoded as "*",
8095 which is identical to "char *", so the "const" is
8096 unfortunately lost. */
8097 if (TYPE_READONLY (pointer_to
))
8098 obstack_1grow (&util_obstack
, 'r');
8101 obstack_1grow (&util_obstack
, '*');
8106 /* We have a normal pointer type that does not get special treatment. */
8107 obstack_1grow (&util_obstack
, '^');
8108 encode_type (pointer_to
, curtype
, format
);
8112 encode_array (tree type
, int curtype
, int format
)
8114 tree an_int_cst
= TYPE_SIZE (type
);
8115 tree array_of
= TREE_TYPE (type
);
8118 if (an_int_cst
== NULL
)
8120 /* We are trying to encode an incomplete array. An incomplete
8121 array is forbidden as part of an instance variable. */
8122 if (generating_instance_variables
)
8124 /* TODO: Detect this error earlier. */
8125 error ("instance variable has unknown size");
8129 /* So the only case in which an incomplete array could occur is
8130 if we are encoding the arguments or return value of a method.
8131 In that case, an incomplete array argument or return value
8132 (eg, -(void)display: (char[])string) is treated like a
8133 pointer because that is how the compiler does the function
8134 call. A special, more complicated case, is when the
8135 incomplete array is the last member of a struct (eg, if we
8136 are encoding "struct { unsigned long int a;double b[];}"),
8137 which is again part of a method argument/return value. In
8138 that case, we really need to communicate to the runtime that
8139 there is an incomplete array (not a pointer!) there. So, we
8140 detect that special case and encode it as a zero-length
8143 Try to detect that we are part of a struct. We do this by
8144 searching for '=' in the type encoding for the current type.
8145 NB: This hack assumes that you can't use '=' as part of a C
8149 char *enc
= obstack_base (&util_obstack
) + curtype
;
8150 if (memchr (enc
, '=',
8151 obstack_object_size (&util_obstack
) - curtype
) == NULL
)
8153 /* We are not inside a struct. Encode the array as a
8155 encode_pointer (type
, curtype
, format
);
8160 /* Else, we are in a struct, and we encode it as a zero-length
8162 sprintf (buffer
, "[" HOST_WIDE_INT_PRINT_DEC
, (HOST_WIDE_INT
)0);
8164 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of
)) == 0)
8165 sprintf (buffer
, "[" HOST_WIDE_INT_PRINT_DEC
, (HOST_WIDE_INT
)0);
8167 sprintf (buffer
, "[" HOST_WIDE_INT_PRINT_DEC
,
8168 TREE_INT_CST_LOW (an_int_cst
)
8169 / TREE_INT_CST_LOW (TYPE_SIZE (array_of
)));
8171 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8172 encode_type (array_of
, curtype
, format
);
8173 obstack_1grow (&util_obstack
, ']');
8177 /* Encode a vector. The vector type is a GCC extension to C. */
8179 encode_vector (tree type
, int curtype
, int format
)
8181 tree vector_of
= TREE_TYPE (type
);
8184 /* Vectors are like simple fixed-size arrays. */
8186 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
8187 alignment of the vector, and <code> is the base type. Eg, int
8188 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
8189 assuming that the alignment is 32 bytes. We include size and
8190 alignment in bytes so that the runtime does not have to have any
8191 knowledge of the actual types.
8193 sprintf (buffer
, "![" HOST_WIDE_INT_PRINT_DEC
",%d",
8194 /* We want to compute the equivalent of sizeof (<vector>).
8195 Code inspired by c_sizeof_or_alignof_type. */
8196 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
))
8197 / (TYPE_PRECISION (char_type_node
) / BITS_PER_UNIT
))),
8198 /* We want to compute the equivalent of __alignof__
8199 (<vector>). Code inspired by
8200 c_sizeof_or_alignof_type. */
8201 TYPE_ALIGN_UNIT (type
));
8202 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8203 encode_type (vector_of
, curtype
, format
);
8204 obstack_1grow (&util_obstack
, ']');
8209 encode_aggregate_fields (tree type
, bool pointed_to
, int curtype
, int format
)
8211 tree field
= TYPE_FIELDS (type
);
8213 for (; field
; field
= DECL_CHAIN (field
))
8216 /* C++ static members, and things that are not field at all,
8217 should not appear in the encoding. */
8218 if (TREE_CODE (field
) != FIELD_DECL
|| TREE_STATIC (field
))
8222 /* Recursively encode fields of embedded base classes. */
8223 if (DECL_ARTIFICIAL (field
) && !DECL_NAME (field
)
8224 && TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
)
8226 encode_aggregate_fields (TREE_TYPE (field
),
8227 pointed_to
, curtype
, format
);
8231 if (generating_instance_variables
&& !pointed_to
)
8233 tree fname
= DECL_NAME (field
);
8235 obstack_1grow (&util_obstack
, '"');
8237 if (fname
&& TREE_CODE (fname
) == IDENTIFIER_NODE
)
8238 obstack_grow (&util_obstack
,
8239 IDENTIFIER_POINTER (fname
),
8240 strlen (IDENTIFIER_POINTER (fname
)));
8242 obstack_1grow (&util_obstack
, '"');
8245 encode_field_decl (field
, curtype
, format
);
8250 encode_aggregate_within (tree type
, int curtype
, int format
, int left
,
8254 /* NB: aggregates that are pointed to have slightly different encoding
8255 rules in that you never encode the names of instance variables. */
8256 int ob_size
= obstack_object_size (&util_obstack
);
8257 bool inline_contents
= false;
8258 bool pointed_to
= false;
8260 if (flag_next_runtime
)
8262 if (ob_size
> 0 && *(obstack_next_free (&util_obstack
) - 1) == '^')
8265 if ((format
== OBJC_ENCODE_INLINE_DEFS
|| generating_instance_variables
)
8266 && (!pointed_to
|| ob_size
- curtype
== 1
8267 || (ob_size
- curtype
== 2
8268 && *(obstack_next_free (&util_obstack
) - 2) == 'r')))
8269 inline_contents
= true;
8273 /* c0 and c1 are the last two characters in the encoding of the
8274 current type; if the last two characters were '^' or '^r',
8275 then we are encoding an aggregate that is "pointed to". The
8276 comment above applies: in that case we should avoid encoding
8277 the names of instance variables.
8279 char c1
= ob_size
> 1 ? *(obstack_next_free (&util_obstack
) - 2) : 0;
8280 char c0
= ob_size
> 0 ? *(obstack_next_free (&util_obstack
) - 1) : 0;
8282 if (c0
== '^' || (c1
== '^' && c0
== 'r'))
8285 if (format
== OBJC_ENCODE_INLINE_DEFS
|| generating_instance_variables
)
8288 inline_contents
= true;
8291 /* Note that the check (ob_size - curtype < 2) prevents
8292 infinite recursion when encoding a structure which is
8293 a linked list (eg, struct node { struct node *next;
8294 }). Each time we follow a pointer, we add one
8295 character to ob_size, and curtype is fixed, so after
8296 at most two pointers we stop inlining contents and
8299 The other case where we don't inline is "^r", which
8300 is a pointer to a constant struct.
8302 if ((ob_size
- curtype
<= 2) && !(c0
== 'r'))
8303 inline_contents
= true;
8308 /* Traverse struct aliases; it is important to get the
8309 original struct and its tag name (if any). */
8310 type
= TYPE_MAIN_VARIANT (type
);
8311 name
= OBJC_TYPE_NAME (type
);
8312 /* Open parenth/bracket. */
8313 obstack_1grow (&util_obstack
, left
);
8315 /* Encode the struct/union tag name, or '?' if a tag was
8316 not provided. Typedef aliases do not qualify. */
8318 /* For compatibility with the NeXT runtime, ObjC++ encodes template
8319 args as a composite struct tag name. */
8320 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
8321 /* Did this struct have a tag? */
8322 && !TYPE_WAS_ANONYMOUS (type
))
8323 obstack_grow (&util_obstack
,
8324 decl_as_string (type
, TFF_DECL_SPECIFIERS
| TFF_UNQUALIFIED_NAME
),
8325 strlen (decl_as_string (type
, TFF_DECL_SPECIFIERS
| TFF_UNQUALIFIED_NAME
)));
8327 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
8328 obstack_grow (&util_obstack
,
8329 IDENTIFIER_POINTER (name
),
8330 strlen (IDENTIFIER_POINTER (name
)));
8333 obstack_1grow (&util_obstack
, '?');
8335 /* Encode the types (and possibly names) of the inner fields,
8337 if (inline_contents
)
8339 obstack_1grow (&util_obstack
, '=');
8340 encode_aggregate_fields (type
, pointed_to
, curtype
, format
);
8342 /* Close parenth/bracket. */
8343 obstack_1grow (&util_obstack
, right
);
8346 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
8350 encode_next_bitfield (int width
)
8353 sprintf (buffer
, "b%d", width
);
8354 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8358 /* Encodes 'type', ignoring type qualifiers (which you should encode
8359 beforehand if needed) with the exception of 'const', which is
8360 encoded by encode_type. See above for the explanation of
8361 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
8362 OBJC_ENCODE_DONT_INLINE_DEFS.
8365 encode_type (tree type
, int curtype
, int format
)
8367 enum tree_code code
= TREE_CODE (type
);
8370 /* Ignore type qualifiers other than 'const' when encoding a
8373 if (type
== error_mark_node
)
8376 if (!flag_next_runtime
)
8378 if (TYPE_READONLY (type
))
8379 obstack_1grow (&util_obstack
, 'r');
8385 if (flag_next_runtime
)
8387 /* Kludge for backwards-compatibility with gcc-3.3: enums
8388 are always encoded as 'i' no matter what type they
8389 actually are (!). */
8393 /* Else, they are encoded exactly like the integer type that is
8394 used by the compiler to store them. */
8397 switch (GET_MODE_BITSIZE (TYPE_MODE (type
)))
8399 case 8: c
= TYPE_UNSIGNED (type
) ? 'C' : 'c'; break;
8400 case 16: c
= TYPE_UNSIGNED (type
) ? 'S' : 's'; break;
8402 if (flag_next_runtime
)
8405 /* Another legacy kludge for compatiblity with
8406 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
8407 but not always. For typedefs, we need to use 'i'
8408 or 'I' instead if encoding a struct field, or a
8410 int_type
= ((!generating_instance_variables
8411 && (obstack_object_size (&util_obstack
)
8412 == (unsigned) curtype
))
8413 ? TYPE_MAIN_VARIANT (type
)
8416 if (int_type
== long_unsigned_type_node
8417 || int_type
== long_integer_type_node
)
8418 c
= TYPE_UNSIGNED (type
) ? 'L' : 'l';
8420 c
= TYPE_UNSIGNED (type
) ? 'I' : 'i';
8424 if (type
== long_unsigned_type_node
8425 || type
== long_integer_type_node
)
8426 c
= TYPE_UNSIGNED (type
) ? 'L' : 'l';
8428 c
= TYPE_UNSIGNED (type
) ? 'I' : 'i';
8431 case 64: c
= TYPE_UNSIGNED (type
) ? 'Q' : 'q'; break;
8432 case 128: c
= TYPE_UNSIGNED (type
) ? 'T' : 't'; break;
8435 obstack_1grow (&util_obstack
, c
);
8440 /* Floating point types. */
8441 switch (GET_MODE_BITSIZE (TYPE_MODE (type
)))
8443 case 32: c
= 'f'; break;
8444 case 64: c
= 'd'; break;
8446 case 128: c
= 'D'; break;
8449 obstack_1grow (&util_obstack
, c
);
8453 obstack_1grow (&util_obstack
, 'v');
8457 obstack_1grow (&util_obstack
, 'B');
8461 encode_array (type
, curtype
, format
);
8466 case REFERENCE_TYPE
:
8468 encode_pointer (type
, curtype
, format
);
8472 encode_aggregate_within (type
, curtype
, format
, '{', '}');
8476 encode_aggregate_within (type
, curtype
, format
, '(', ')');
8479 case FUNCTION_TYPE
: /* '?' means an unknown type. */
8480 obstack_1grow (&util_obstack
, '?');
8484 /* A complex is encoded as 'j' followed by the inner type (eg,
8485 "_Complex int" is encoded as 'ji'). */
8486 obstack_1grow (&util_obstack
, 'j');
8487 encode_type (TREE_TYPE (type
), curtype
, format
);
8491 encode_vector (type
, curtype
, format
);
8495 warning (0, "unknown type %s found during Objective-C encoding",
8496 gen_type_name (type
));
8497 obstack_1grow (&util_obstack
, '?');
8501 if (flag_next_runtime
)
8503 /* Super-kludge. Some ObjC qualifier and type combinations need
8504 to be rearranged for compatibility with gcc-3.3. */
8505 if (code
== POINTER_TYPE
&& obstack_object_size (&util_obstack
) >= 3)
8507 char *enc
= obstack_base (&util_obstack
) + curtype
;
8509 /* Rewrite "in const" from "nr" to "rn". */
8510 if (curtype
>= 1 && !strncmp (enc
- 1, "nr", 2))
8511 strncpy (enc
- 1, "rn", 2);
8517 encode_gnu_bitfield (int position
, tree type
, int size
)
8519 enum tree_code code
= TREE_CODE (type
);
8521 char charType
= '?';
8523 /* This code is only executed for the GNU runtime, so we can ignore
8524 the NeXT runtime kludge of always encoding enums as 'i' no matter
8525 what integers they actually are. */
8526 if (code
== INTEGER_TYPE
|| code
== ENUMERAL_TYPE
)
8528 if (integer_zerop (TYPE_MIN_VALUE (type
)))
8529 /* Unsigned integer types. */
8531 if (TYPE_MODE (type
) == QImode
)
8533 else if (TYPE_MODE (type
) == HImode
)
8535 else if (TYPE_MODE (type
) == SImode
)
8537 if (type
== long_unsigned_type_node
)
8542 else if (TYPE_MODE (type
) == DImode
)
8546 /* Signed integer types. */
8548 if (TYPE_MODE (type
) == QImode
)
8550 else if (TYPE_MODE (type
) == HImode
)
8552 else if (TYPE_MODE (type
) == SImode
)
8554 if (type
== long_integer_type_node
)
8560 else if (TYPE_MODE (type
) == DImode
)
8566 /* Do not do any encoding, produce an error and keep going. */
8567 error ("trying to encode non-integer type as a bitfield");
8571 sprintf (buffer
, "b%d%c%d", position
, charType
, size
);
8572 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8576 encode_field_decl (tree field_decl
, int curtype
, int format
)
8579 /* C++ static members, and things that are not fields at all,
8580 should not appear in the encoding. */
8581 if (TREE_CODE (field_decl
) != FIELD_DECL
|| TREE_STATIC (field_decl
))
8585 /* Generate the bitfield typing information, if needed. Note the difference
8586 between GNU and NeXT runtimes. */
8587 if (DECL_BIT_FIELD_TYPE (field_decl
))
8589 int size
= tree_low_cst (DECL_SIZE (field_decl
), 1);
8591 if (flag_next_runtime
)
8592 encode_next_bitfield (size
);
8594 encode_gnu_bitfield (int_bit_position (field_decl
),
8595 DECL_BIT_FIELD_TYPE (field_decl
), size
);
8598 encode_type (TREE_TYPE (field_decl
), curtype
, format
);
8601 static GTY(()) tree objc_parmlist
= NULL_TREE
;
8603 /* Append PARM to a list of formal parameters of a method, making a necessary
8604 array-to-pointer adjustment along the way. */
8607 objc_push_parm (tree parm
)
8609 bool relayout_needed
= false;
8611 if (TREE_TYPE (parm
) == error_mark_node
)
8613 objc_parmlist
= chainon (objc_parmlist
, parm
);
8617 /* Decay arrays and functions into pointers. */
8618 if (TREE_CODE (TREE_TYPE (parm
)) == ARRAY_TYPE
)
8620 TREE_TYPE (parm
) = build_pointer_type (TREE_TYPE (TREE_TYPE (parm
)));
8621 relayout_needed
= true;
8623 else if (TREE_CODE (TREE_TYPE (parm
)) == FUNCTION_TYPE
)
8625 TREE_TYPE (parm
) = build_pointer_type (TREE_TYPE (parm
));
8626 relayout_needed
= true;
8629 if (relayout_needed
)
8630 relayout_decl (parm
);
8633 DECL_ARG_TYPE (parm
)
8634 = lang_hooks
.types
.type_promotes_to (TREE_TYPE (parm
));
8636 /* Record constancy and volatility. */
8637 c_apply_type_quals_to_decl
8638 ((TYPE_READONLY (TREE_TYPE (parm
)) ? TYPE_QUAL_CONST
: 0)
8639 | (TYPE_RESTRICT (TREE_TYPE (parm
)) ? TYPE_QUAL_RESTRICT
: 0)
8640 | (TYPE_VOLATILE (TREE_TYPE (parm
)) ? TYPE_QUAL_VOLATILE
: 0), parm
);
8642 objc_parmlist
= chainon (objc_parmlist
, parm
);
8645 /* Retrieve the formal parameter list constructed via preceding calls to
8646 objc_push_parm(). */
8650 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED
)
8652 static struct c_arg_info
*
8653 objc_get_parm_info (int have_ellipsis
)
8657 tree parm_info
= objc_parmlist
;
8658 objc_parmlist
= NULL_TREE
;
8662 tree parm_info
= objc_parmlist
;
8663 struct c_arg_info
*arg_info
;
8664 /* The C front-end requires an elaborate song and dance at
8667 declare_parm_level ();
8670 tree next
= DECL_CHAIN (parm_info
);
8672 DECL_CHAIN (parm_info
) = NULL_TREE
;
8673 parm_info
= pushdecl (parm_info
);
8674 finish_decl (parm_info
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
8677 arg_info
= get_parm_info (have_ellipsis
);
8679 objc_parmlist
= NULL_TREE
;
8684 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8685 method definitions. In the case of instance methods, we can be more
8686 specific as to the type of 'self'. */
8689 synth_self_and_ucmd_args (void)
8693 if (objc_method_context
8694 && TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
)
8695 self_type
= objc_instance_type
;
8697 /* Really a `struct objc_class *'. However, we allow people to
8698 assign to self, which changes its type midstream. */
8699 self_type
= objc_object_type
;
8702 objc_push_parm (build_decl (input_location
,
8703 PARM_DECL
, self_id
, self_type
));
8706 objc_push_parm (build_decl (input_location
,
8707 PARM_DECL
, ucmd_id
, objc_selector_type
));
8710 /* Transform an Objective-C method definition into a static C function
8711 definition, synthesizing the first two arguments, "self" and "_cmd",
8715 start_method_def (tree method
)
8721 struct c_arg_info
*parm_info
;
8723 int have_ellipsis
= 0;
8725 /* If we are defining a "dealloc" method in a non-root class, we
8726 will need to check if a [super dealloc] is missing, and warn if
8728 if(CLASS_SUPER_NAME (objc_implementation_context
)
8729 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method
))))
8730 should_call_super_dealloc
= 1;
8732 should_call_super_dealloc
= 0;
8734 /* Required to implement _msgSuper. */
8735 objc_method_context
= method
;
8736 UOBJC_SUPER_decl
= NULL_TREE
;
8738 /* Generate prototype declarations for arguments..."new-style". */
8739 synth_self_and_ucmd_args ();
8741 /* Generate argument declarations if a keyword_decl. */
8742 parmlist
= METHOD_SEL_ARGS (method
);
8745 tree type
= TREE_VALUE (TREE_TYPE (parmlist
)), parm
;
8747 parm
= build_decl (input_location
,
8748 PARM_DECL
, KEYWORD_ARG_NAME (parmlist
), type
);
8749 objc_push_parm (parm
);
8750 parmlist
= DECL_CHAIN (parmlist
);
8753 if (METHOD_ADD_ARGS (method
))
8757 for (akey
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
8758 akey
; akey
= TREE_CHAIN (akey
))
8760 objc_push_parm (TREE_VALUE (akey
));
8763 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
8767 parm_info
= objc_get_parm_info (have_ellipsis
);
8769 really_start_method (objc_method_context
, parm_info
);
8772 /* Return 1 if TYPE1 is equivalent to TYPE2
8773 for purposes of method overloading. */
8776 objc_types_are_equivalent (tree type1
, tree type2
)
8781 /* Strip away indirections. */
8782 while ((TREE_CODE (type1
) == ARRAY_TYPE
|| TREE_CODE (type1
) == POINTER_TYPE
)
8783 && (TREE_CODE (type1
) == TREE_CODE (type2
)))
8784 type1
= TREE_TYPE (type1
), type2
= TREE_TYPE (type2
);
8785 if (TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
8788 type1
= (TYPE_HAS_OBJC_INFO (type1
)
8789 ? TYPE_OBJC_PROTOCOL_LIST (type1
)
8791 type2
= (TYPE_HAS_OBJC_INFO (type2
)
8792 ? TYPE_OBJC_PROTOCOL_LIST (type2
)
8795 if (list_length (type1
) == list_length (type2
))
8797 for (; type2
; type2
= TREE_CHAIN (type2
))
8798 if (!lookup_protocol_in_reflist (type1
, TREE_VALUE (type2
)))
8805 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8808 objc_types_share_size_and_alignment (tree type1
, tree type2
)
8810 return (simple_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
8811 && TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
));
8814 /* Return 1 if PROTO1 is equivalent to PROTO2
8815 for purposes of method overloading. Ordinarily, the type signatures
8816 should match up exactly, unless STRICT is zero, in which case we
8817 shall allow differences in which the size and alignment of a type
8821 comp_proto_with_proto (tree proto1
, tree proto2
, int strict
)
8825 /* The following test is needed in case there are hashing
8827 if (METHOD_SEL_NAME (proto1
) != METHOD_SEL_NAME (proto2
))
8830 /* Compare return types. */
8831 type1
= TREE_VALUE (TREE_TYPE (proto1
));
8832 type2
= TREE_VALUE (TREE_TYPE (proto2
));
8834 if (!objc_types_are_equivalent (type1
, type2
)
8835 && (strict
|| !objc_types_share_size_and_alignment (type1
, type2
)))
8838 /* Compare argument types. */
8839 for (type1
= get_arg_type_list (proto1
, METHOD_REF
, 0),
8840 type2
= get_arg_type_list (proto2
, METHOD_REF
, 0);
8842 type1
= TREE_CHAIN (type1
), type2
= TREE_CHAIN (type2
))
8844 if (!objc_types_are_equivalent (TREE_VALUE (type1
), TREE_VALUE (type2
))
8846 || !objc_types_share_size_and_alignment (TREE_VALUE (type1
),
8847 TREE_VALUE (type2
))))
8851 return (!type1
&& !type2
);
8854 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
8855 this occurs. ObjC method dispatches are _not_ like C++ virtual
8856 member function dispatches, and we account for the difference here. */
8859 objc_fold_obj_type_ref (tree ref
, tree known_type
)
8861 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED
,
8862 tree known_type ATTRIBUTE_UNUSED
)
8866 tree v
= BINFO_VIRTUALS (TYPE_BINFO (known_type
));
8868 /* If the receiver does not have virtual member functions, there
8869 is nothing we can (or need to) do here. */
8873 /* Let C++ handle C++ virtual functions. */
8874 return cp_fold_obj_type_ref (ref
, known_type
);
8876 /* For plain ObjC, we currently do not need to do anything. */
8882 objc_start_function (tree name
, tree type
, tree attrs
,
8886 struct c_arg_info
*params
8890 tree fndecl
= build_decl (input_location
,
8891 FUNCTION_DECL
, name
, type
);
8894 DECL_ARGUMENTS (fndecl
) = params
;
8895 DECL_INITIAL (fndecl
) = error_mark_node
;
8896 DECL_EXTERNAL (fndecl
) = 0;
8897 TREE_STATIC (fndecl
) = 1;
8898 retrofit_lang_decl (fndecl
);
8899 cplus_decl_attributes (&fndecl
, attrs
, 0);
8900 start_preparsed_function (fndecl
, attrs
, /*flags=*/SF_DEFAULT
);
8902 current_function_returns_value
= 0; /* Assume, until we see it does. */
8903 current_function_returns_null
= 0;
8905 decl_attributes (&fndecl
, attrs
, 0);
8906 announce_function (fndecl
);
8907 DECL_INITIAL (fndecl
) = error_mark_node
;
8908 DECL_EXTERNAL (fndecl
) = 0;
8909 TREE_STATIC (fndecl
) = 1;
8910 current_function_decl
= pushdecl (fndecl
);
8912 declare_parm_level ();
8913 DECL_RESULT (current_function_decl
)
8914 = build_decl (input_location
,
8915 RESULT_DECL
, NULL_TREE
,
8916 TREE_TYPE (TREE_TYPE (current_function_decl
)));
8917 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl
)) = 1;
8918 DECL_IGNORED_P (DECL_RESULT (current_function_decl
)) = 1;
8919 start_fname_decls ();
8920 store_parm_decls_from (params
);
8923 TREE_USED (current_function_decl
) = 1;
8926 /* - Generate an identifier for the function. the format is "_n_cls",
8927 where 1 <= n <= nMethods, and cls is the name the implementation we
8929 - Install the return type from the method declaration.
8930 - If we have a prototype, check for type consistency. */
8933 really_start_method (tree method
,
8937 struct c_arg_info
*parmlist
8941 tree ret_type
, meth_type
;
8943 const char *sel_name
, *class_name
, *cat_name
;
8946 /* Synth the storage class & assemble the return type. */
8947 ret_type
= TREE_VALUE (TREE_TYPE (method
));
8949 sel_name
= IDENTIFIER_POINTER (METHOD_SEL_NAME (method
));
8950 class_name
= IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context
));
8951 cat_name
= ((TREE_CODE (objc_implementation_context
)
8952 == CLASS_IMPLEMENTATION_TYPE
)
8954 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context
)));
8957 /* Make sure this is big enough for any plausible method label. */
8958 buf
= (char *) alloca (50 + strlen (sel_name
) + strlen (class_name
)
8959 + (cat_name
? strlen (cat_name
) : 0));
8961 OBJC_GEN_METHOD_LABEL (buf
, TREE_CODE (method
) == INSTANCE_METHOD_DECL
,
8962 class_name
, cat_name
, sel_name
, method_slot
);
8964 method_id
= get_identifier (buf
);
8967 /* Objective-C methods cannot be overloaded, so we don't need
8968 the type encoding appended. It looks bad anyway... */
8969 push_lang_context (lang_name_c
);
8973 = build_function_type (ret_type
,
8974 get_arg_type_list (method
, METHOD_DEF
, 0));
8975 objc_start_function (method_id
, meth_type
, NULL_TREE
, parmlist
);
8977 /* Set self_decl from the first argument. */
8978 self_decl
= DECL_ARGUMENTS (current_function_decl
);
8980 /* Suppress unused warnings. */
8981 TREE_USED (self_decl
) = 1;
8982 DECL_READ_P (self_decl
) = 1;
8983 TREE_USED (DECL_CHAIN (self_decl
)) = 1;
8984 DECL_READ_P (DECL_CHAIN (self_decl
)) = 1;
8986 pop_lang_context ();
8989 METHOD_DEFINITION (method
) = current_function_decl
;
8991 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8993 if (implementation_template
!= objc_implementation_context
)
8996 = lookup_method_static (implementation_template
,
8997 METHOD_SEL_NAME (method
),
8998 ((TREE_CODE (method
) == CLASS_METHOD_DECL
)
8999 | OBJC_LOOKUP_NO_SUPER
));
9003 if (!comp_proto_with_proto (method
, proto
, 1))
9005 bool type
= TREE_CODE (method
) == INSTANCE_METHOD_DECL
;
9007 warning_at (DECL_SOURCE_LOCATION (method
), 0,
9008 "conflicting types for %<%c%s%>",
9010 identifier_to_locale (gen_method_decl (method
)));
9011 inform (DECL_SOURCE_LOCATION (proto
),
9012 "previous declaration of %<%c%s%>",
9014 identifier_to_locale (gen_method_decl (proto
)));
9019 /* We have a method @implementation even though we did not
9020 see a corresponding @interface declaration (which is allowed
9021 by Objective-C rules). Go ahead and place the method in
9022 the @interface anyway, so that message dispatch lookups
9024 tree interface
= implementation_template
;
9026 if (TREE_CODE (objc_implementation_context
)
9027 == CATEGORY_IMPLEMENTATION_TYPE
)
9028 interface
= lookup_category
9030 CLASS_SUPER_NAME (objc_implementation_context
));
9033 objc_add_method (interface
, copy_node (method
),
9034 TREE_CODE (method
) == CLASS_METHOD_DECL
);
9039 static void *UOBJC_SUPER_scope
= 0;
9041 /* _n_Method (id self, SEL sel, ...)
9043 struct objc_super _S;
9044 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
9048 get_super_receiver (void)
9050 if (objc_method_context
)
9052 tree super_expr
, super_expr_list
;
9054 if (!UOBJC_SUPER_decl
)
9056 UOBJC_SUPER_decl
= build_decl (input_location
,
9057 VAR_DECL
, get_identifier (TAG_SUPER
),
9058 objc_super_template
);
9059 /* This prevents `unused variable' warnings when compiling with -Wall. */
9060 TREE_USED (UOBJC_SUPER_decl
) = 1;
9061 DECL_READ_P (UOBJC_SUPER_decl
) = 1;
9062 lang_hooks
.decls
.pushdecl (UOBJC_SUPER_decl
);
9063 finish_decl (UOBJC_SUPER_decl
, input_location
, NULL_TREE
, NULL_TREE
,
9065 UOBJC_SUPER_scope
= objc_get_current_scope ();
9068 /* Set receiver to self. */
9069 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
, self_id
);
9070 super_expr
= build_modify_expr (input_location
, super_expr
, NULL_TREE
,
9071 NOP_EXPR
, input_location
, self_decl
,
9073 super_expr_list
= super_expr
;
9075 /* Set class to begin searching. */
9076 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
,
9077 get_identifier ("super_class"));
9079 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
9081 /* [_cls, __cls]Super are "pre-built" in
9082 synth_forward_declarations. */
9084 super_expr
= build_modify_expr (input_location
, super_expr
,
9085 NULL_TREE
, NOP_EXPR
,
9087 ((TREE_CODE (objc_method_context
)
9088 == INSTANCE_METHOD_DECL
)
9095 /* We have a category. */
9097 tree super_name
= CLASS_SUPER_NAME (implementation_template
);
9100 /* Barf if super used in a category of Object. */
9103 error ("no super class declared in interface for %qE",
9104 CLASS_NAME (implementation_template
));
9105 return error_mark_node
;
9108 if (flag_next_runtime
&& !flag_zero_link
)
9110 super_class
= objc_get_class_reference (super_name
);
9111 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
)
9112 /* If we are in a class method, we must retrieve the
9113 _metaclass_ for the current class, pointed at by
9114 the class's "isa" pointer. The following assumes that
9115 "isa" is the first ivar in a class (which it must be). */
9117 = build_indirect_ref
9119 build_c_cast (input_location
,
9120 build_pointer_type (objc_class_type
),
9121 super_class
), RO_UNARY_STAR
);
9125 add_class_reference (super_name
);
9126 super_class
= (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
9127 ? objc_get_class_decl
: objc_get_meta_class_decl
);
9128 assemble_external (super_class
);
9130 = build_function_call
9135 my_build_string_pointer
9136 (IDENTIFIER_LENGTH (super_name
) + 1,
9137 IDENTIFIER_POINTER (super_name
))));
9141 = build_modify_expr (input_location
, super_expr
, NULL_TREE
,
9144 build_c_cast (input_location
,
9145 TREE_TYPE (super_expr
),
9150 super_expr_list
= build_compound_expr (input_location
,
9151 super_expr_list
, super_expr
);
9153 super_expr
= build_unary_op (input_location
,
9154 ADDR_EXPR
, UOBJC_SUPER_decl
, 0);
9155 super_expr_list
= build_compound_expr (input_location
,
9156 super_expr_list
, super_expr
);
9158 return super_expr_list
;
9162 error ("[super ...] must appear in a method context");
9163 return error_mark_node
;
9167 /* When exiting a scope, sever links to a 'super' declaration (if any)
9168 therein contained. */
9171 objc_clear_super_receiver (void)
9173 if (objc_method_context
9174 && UOBJC_SUPER_scope
== objc_get_current_scope ()) {
9175 UOBJC_SUPER_decl
= 0;
9176 UOBJC_SUPER_scope
= 0;
9181 objc_finish_method_definition (tree fndecl
)
9183 /* We cannot validly inline ObjC methods, at least not without a language
9184 extension to declare that a method need not be dynamically
9185 dispatched, so suppress all thoughts of doing so. */
9186 DECL_UNINLINABLE (fndecl
) = 1;
9189 /* The C++ front-end will have called finish_function() for us. */
9193 METHOD_ENCODING (objc_method_context
)
9194 = encode_method_prototype (objc_method_context
);
9196 /* Required to implement _msgSuper. This must be done AFTER finish_function,
9197 since the optimizer may find "may be used before set" errors. */
9198 objc_method_context
= NULL_TREE
;
9200 if (should_call_super_dealloc
)
9201 warning (0, "method possibly missing a [super dealloc] call");
9204 /* Given a tree DECL node, produce a printable description of it in the given
9205 buffer, overwriting the buffer. */
9208 gen_declaration (tree decl
)
9214 gen_type_name_0 (TREE_TYPE (decl
));
9216 if (DECL_NAME (decl
))
9218 if (!POINTER_TYPE_P (TREE_TYPE (decl
)))
9219 strcat (errbuf
, " ");
9221 strcat (errbuf
, IDENTIFIER_POINTER (DECL_NAME (decl
)));
9224 if (DECL_INITIAL (decl
)
9225 && TREE_CODE (DECL_INITIAL (decl
)) == INTEGER_CST
)
9226 sprintf (errbuf
+ strlen (errbuf
), ": " HOST_WIDE_INT_PRINT_DEC
,
9227 TREE_INT_CST_LOW (DECL_INITIAL (decl
)));
9233 /* Given a tree TYPE node, produce a printable description of it in the given
9234 buffer, overwriting the buffer. */
9237 gen_type_name_0 (tree type
)
9239 tree orig
= type
, proto
;
9241 if (TYPE_P (type
) && TYPE_NAME (type
))
9242 type
= TYPE_NAME (type
);
9243 else if (POINTER_TYPE_P (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
9245 tree inner
= TREE_TYPE (type
);
9247 while (TREE_CODE (inner
) == ARRAY_TYPE
)
9248 inner
= TREE_TYPE (inner
);
9250 gen_type_name_0 (inner
);
9252 if (!POINTER_TYPE_P (inner
))
9253 strcat (errbuf
, " ");
9255 if (POINTER_TYPE_P (type
))
9256 strcat (errbuf
, "*");
9258 while (type
!= inner
)
9260 strcat (errbuf
, "[");
9262 if (TYPE_DOMAIN (type
))
9266 sprintf (sz
, HOST_WIDE_INT_PRINT_DEC
,
9268 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1));
9269 strcat (errbuf
, sz
);
9272 strcat (errbuf
, "]");
9273 type
= TREE_TYPE (type
);
9279 if (TREE_CODE (type
) == TYPE_DECL
&& DECL_NAME (type
))
9280 type
= DECL_NAME (type
);
9282 strcat (errbuf
, TREE_CODE (type
) == IDENTIFIER_NODE
9283 ? IDENTIFIER_POINTER (type
)
9286 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
9287 if (objc_is_id (orig
))
9288 orig
= TREE_TYPE (orig
);
9290 proto
= TYPE_HAS_OBJC_INFO (orig
) ? TYPE_OBJC_PROTOCOL_LIST (orig
) : NULL_TREE
;
9294 strcat (errbuf
, " <");
9298 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto
))));
9299 proto
= TREE_CHAIN (proto
);
9300 strcat (errbuf
, proto
? ", " : ">");
9309 gen_type_name (tree type
)
9313 return gen_type_name_0 (type
);
9316 /* Given a method tree, put a printable description into the given
9317 buffer (overwriting) and return a pointer to the buffer. */
9320 gen_method_decl (tree method
)
9324 strcpy (errbuf
, "("); /* NB: Do _not_ call strcat() here. */
9325 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method
)));
9326 strcat (errbuf
, ")");
9327 chain
= METHOD_SEL_ARGS (method
);
9331 /* We have a chain of keyword_decls. */
9334 if (KEYWORD_KEY_NAME (chain
))
9335 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain
)));
9337 strcat (errbuf
, ":(");
9338 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain
)));
9339 strcat (errbuf
, ")");
9341 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain
)));
9342 if ((chain
= DECL_CHAIN (chain
)))
9343 strcat (errbuf
, " ");
9347 if (METHOD_ADD_ARGS (method
))
9349 chain
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
9351 /* Know we have a chain of parm_decls. */
9354 strcat (errbuf
, ", ");
9355 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain
)));
9356 chain
= TREE_CHAIN (chain
);
9359 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
9360 strcat (errbuf
, ", ...");
9365 /* We have a unary selector. */
9366 strcat (errbuf
, IDENTIFIER_POINTER (METHOD_SEL_NAME (method
)));
9374 /* Dump an @interface declaration of the supplied class CHAIN to the
9375 supplied file FP. Used to implement the -gen-decls option (which
9376 prints out an @interface declaration of all classes compiled in
9377 this run); potentially useful for debugging the compiler too. */
9379 dump_interface (FILE *fp
, tree chain
)
9381 /* FIXME: A heap overflow here whenever a method (or ivar)
9382 declaration is so long that it doesn't fit in the buffer. The
9383 code and all the related functions should be rewritten to avoid
9384 using fixed size buffers. */
9385 const char *my_name
= IDENTIFIER_POINTER (CLASS_NAME (chain
));
9386 tree ivar_decls
= CLASS_RAW_IVARS (chain
);
9387 tree nst_methods
= CLASS_NST_METHODS (chain
);
9388 tree cls_methods
= CLASS_CLS_METHODS (chain
);
9390 fprintf (fp
, "\n@interface %s", my_name
);
9392 /* CLASS_SUPER_NAME is used to store the superclass name for
9393 classes, and the category name for categories. */
9394 if (CLASS_SUPER_NAME (chain
))
9396 const char *name
= IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain
));
9398 if (TREE_CODE (chain
) == CATEGORY_IMPLEMENTATION_TYPE
9399 || TREE_CODE (chain
) == CATEGORY_INTERFACE_TYPE
)
9401 fprintf (fp
, " (%s)\n", name
);
9405 fprintf (fp
, " : %s\n", name
);
9411 /* FIXME - the following doesn't seem to work at the moment. */
9414 fprintf (fp
, "{\n");
9417 fprintf (fp
, "\t%s;\n", gen_declaration (ivar_decls
));
9418 ivar_decls
= TREE_CHAIN (ivar_decls
);
9421 fprintf (fp
, "}\n");
9426 fprintf (fp
, "- %s;\n", gen_method_decl (nst_methods
));
9427 nst_methods
= TREE_CHAIN (nst_methods
);
9432 fprintf (fp
, "+ %s;\n", gen_method_decl (cls_methods
));
9433 cls_methods
= TREE_CHAIN (cls_methods
);
9436 fprintf (fp
, "@end\n");
9439 /* Demangle function for Objective-C */
9441 objc_demangle (const char *mangled
)
9443 char *demangled
, *cp
;
9445 if (mangled
[0] == '_' &&
9446 (mangled
[1] == 'i' || mangled
[1] == 'c') &&
9449 cp
= demangled
= XNEWVEC (char, strlen(mangled
) + 2);
9450 if (mangled
[1] == 'i')
9451 *cp
++ = '-'; /* for instance method */
9453 *cp
++ = '+'; /* for class method */
9454 *cp
++ = '['; /* opening left brace */
9455 strcpy(cp
, mangled
+3); /* tack on the rest of the mangled name */
9456 while (*cp
&& *cp
== '_')
9457 cp
++; /* skip any initial underbars in class name */
9458 cp
= strchr(cp
, '_'); /* find first non-initial underbar */
9461 free(demangled
); /* not mangled name */
9464 if (cp
[1] == '_') /* easy case: no category name */
9466 *cp
++ = ' '; /* replace two '_' with one ' ' */
9467 strcpy(cp
, mangled
+ (cp
- demangled
) + 2);
9471 *cp
++ = '('; /* less easy case: category name */
9472 cp
= strchr(cp
, '_');
9475 free(demangled
); /* not mangled name */
9479 *cp
++ = ' '; /* overwriting 1st char of method name... */
9480 strcpy(cp
, mangled
+ (cp
- demangled
)); /* get it back */
9482 while (*cp
&& *cp
== '_')
9483 cp
++; /* skip any initial underbars in method name */
9486 *cp
= ':'; /* replace remaining '_' with ':' */
9487 *cp
++ = ']'; /* closing right brace */
9488 *cp
++ = 0; /* string terminator */
9492 return mangled
; /* not an objc mangled name */
9496 objc_printable_name (tree decl
, int kind ATTRIBUTE_UNUSED
)
9498 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl
)));
9504 gcc_obstack_init (&util_obstack
);
9505 util_firstobj
= (char *) obstack_finish (&util_obstack
);
9507 errbuf
= XNEWVEC (char, 1024 * 10);
9509 synth_module_prologue ();
9515 struct imp_entry
*impent
;
9517 /* The internally generated initializers appear to have missing braces.
9518 Don't warn about this. */
9519 int save_warn_missing_braces
= warn_missing_braces
;
9520 warn_missing_braces
= 0;
9522 /* A missing @end may not be detected by the parser. */
9523 if (objc_implementation_context
)
9525 warning (0, "%<@end%> missing in implementation context");
9526 finish_class (objc_implementation_context
);
9527 objc_ivar_chain
= NULL_TREE
;
9528 objc_implementation_context
= NULL_TREE
;
9531 /* Process the static instances here because initialization of objc_symtab
9533 if (objc_static_instances
)
9534 generate_static_references ();
9536 /* forward declare categories */
9538 forward_declare_categories ();
9540 for (impent
= imp_list
; impent
; impent
= impent
->next
)
9542 objc_implementation_context
= impent
->imp_context
;
9543 implementation_template
= impent
->imp_template
;
9545 /* FIXME: This needs reworking to be more obvious. */
9547 UOBJC_CLASS_decl
= impent
->class_decl
;
9548 UOBJC_METACLASS_decl
= impent
->meta_decl
;
9550 /* Dump the @interface of each class as we compile it, if the
9551 -gen-decls option is in use. TODO: Dump the classes in the
9552 order they were found, rather than in reverse order as we
9554 if (flag_gen_declaration
)
9556 dump_interface (gen_declaration_file
, objc_implementation_context
);
9559 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
9561 /* all of the following reference the string pool... */
9562 generate_ivar_lists ();
9563 generate_dispatch_tables ();
9564 generate_shared_structures (impent
);
9568 generate_dispatch_tables ();
9569 generate_category (impent
);
9572 impent
->class_decl
= UOBJC_CLASS_decl
;
9573 impent
->meta_decl
= UOBJC_METACLASS_decl
;
9576 /* If we are using an array of selectors, we must always
9577 finish up the array decl even if no selectors were used. */
9578 if (flag_next_runtime
)
9579 build_next_selector_translation_table ();
9581 build_gnu_selector_translation_table ();
9584 generate_protocols ();
9586 if (flag_next_runtime
)
9587 generate_objc_image_info ();
9589 if (imp_list
|| class_names_chain
9590 || meth_var_names_chain
|| meth_var_types_chain
|| sel_ref_chain
)
9591 generate_objc_symtab_decl ();
9593 /* Arrange for ObjC data structures to be initialized at run time. */
9594 if (objc_implementation_context
|| class_names_chain
|| objc_static_instances
9595 || meth_var_names_chain
|| meth_var_types_chain
|| sel_ref_chain
)
9597 build_module_descriptor ();
9599 if (!flag_next_runtime
)
9600 build_module_initializer_routine ();
9603 /* Dump the class references. This forces the appropriate classes
9604 to be linked into the executable image, preserving unix archive
9605 semantics. This can be removed when we move to a more dynamically
9606 linked environment. */
9608 for (chain
= cls_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
9610 handle_class_ref (chain
);
9611 if (TREE_PURPOSE (chain
))
9612 generate_classref_translation_entry (chain
);
9615 for (impent
= imp_list
; impent
; impent
= impent
->next
)
9616 handle_impent (impent
);
9623 /* Run through the selector hash tables and print a warning for any
9624 selector which has multiple methods. */
9626 for (slot
= 0; slot
< SIZEHASHTABLE
; slot
++)
9628 for (hsh
= cls_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
9629 check_duplicates (hsh
, 0, 1);
9630 for (hsh
= nst_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
9631 check_duplicates (hsh
, 0, 1);
9635 warn_missing_braces
= save_warn_missing_braces
;
9638 /* Subroutines of finish_objc. */
9641 generate_classref_translation_entry (tree chain
)
9643 tree expr
, decl
, type
;
9645 decl
= TREE_PURPOSE (chain
);
9646 type
= TREE_TYPE (decl
);
9648 expr
= add_objc_string (TREE_VALUE (chain
), class_names
);
9649 expr
= convert (type
, expr
); /* cast! */
9651 /* This is a class reference. It is re-written by the runtime,
9652 but will be optimized away unless we force it. */
9653 DECL_PRESERVE_P (decl
) = 1;
9654 finish_var_decl (decl
, expr
);
9659 handle_class_ref (tree chain
)
9661 const char *name
= IDENTIFIER_POINTER (TREE_VALUE (chain
));
9662 char *string
= (char *) alloca (strlen (name
) + 30);
9666 sprintf (string
, "%sobjc_class_name_%s",
9667 (flag_next_runtime
? "." : "__"), name
);
9669 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
9670 if (flag_next_runtime
)
9672 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file
, string
);
9677 /* Make a decl for this name, so we can use its address in a tree. */
9678 decl
= build_decl (input_location
,
9679 VAR_DECL
, get_identifier (string
), TREE_TYPE (integer_zero_node
));
9680 DECL_EXTERNAL (decl
) = 1;
9681 TREE_PUBLIC (decl
) = 1;
9683 finish_var_decl (decl
, 0);
9685 /* Make a decl for the address. */
9686 sprintf (string
, "%sobjc_class_ref_%s",
9687 (flag_next_runtime
? "." : "__"), name
);
9688 exp
= build1 (ADDR_EXPR
, string_type_node
, decl
);
9689 decl
= build_decl (input_location
,
9690 VAR_DECL
, get_identifier (string
), string_type_node
);
9691 TREE_STATIC (decl
) = 1;
9692 TREE_USED (decl
) = 1;
9693 DECL_READ_P (decl
) = 1;
9694 DECL_ARTIFICIAL (decl
) = 1;
9695 DECL_INITIAL (decl
) = error_mark_node
;
9697 /* We must force the reference. */
9698 DECL_PRESERVE_P (decl
) = 1;
9701 finish_var_decl (decl
, exp
);
9705 handle_impent (struct imp_entry
*impent
)
9709 objc_implementation_context
= impent
->imp_context
;
9710 implementation_template
= impent
->imp_template
;
9712 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
9714 const char *const class_name
=
9715 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
));
9717 string
= (char *) alloca (strlen (class_name
) + 30);
9719 sprintf (string
, "%sobjc_class_name_%s",
9720 (flag_next_runtime
? "." : "__"), class_name
);
9722 else if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
9724 const char *const class_name
=
9725 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
));
9726 const char *const class_super_name
=
9727 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent
->imp_context
));
9729 string
= (char *) alloca (strlen (class_name
)
9730 + strlen (class_super_name
) + 30);
9732 /* Do the same for categories. Even though no references to
9733 these symbols are generated automatically by the compiler, it
9734 gives you a handle to pull them into an archive by hand. */
9735 sprintf (string
, "*%sobjc_category_name_%s_%s",
9736 (flag_next_runtime
? "." : "__"), class_name
, class_super_name
);
9741 #ifdef ASM_DECLARE_CLASS_REFERENCE
9742 if (flag_next_runtime
)
9744 ASM_DECLARE_CLASS_REFERENCE (asm_out_file
, string
);
9752 init
= integer_zero_node
;
9753 decl
= build_decl (input_location
,
9754 VAR_DECL
, get_identifier (string
), TREE_TYPE (init
));
9755 TREE_PUBLIC (decl
) = 1;
9756 TREE_READONLY (decl
) = 1;
9757 TREE_USED (decl
) = 1;
9758 TREE_CONSTANT (decl
) = 1;
9759 DECL_CONTEXT (decl
) = NULL_TREE
;
9760 DECL_ARTIFICIAL (decl
) = 1;
9761 TREE_STATIC (decl
) = 1;
9762 DECL_INITIAL (decl
) = error_mark_node
; /* A real initializer is coming... */
9763 /* We must force the reference. */
9764 DECL_PRESERVE_P (decl
) = 1;
9766 finish_var_decl(decl
, init
) ;
9770 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
9771 later requires that ObjC translation units participating in F&C be
9772 specially marked. The following routine accomplishes this. */
9774 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
9777 generate_objc_image_info (void)
9781 = ((flag_replace_objc_classes
&& imp_count
? 1 : 0)
9782 | (flag_objc_gc
? 2 : 0));
9783 VEC(constructor_elt
,gc
) *v
= NULL
;
9787 return; /* No need for an image_info entry. */
9789 array_type
= build_sized_array_type (integer_type_node
, 2);
9791 decl
= start_var_decl (array_type
, "_OBJC_IMAGE_INFO");
9793 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
9794 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (integer_type_node
, flags
));
9795 /* If we need this (determined above) it is because the runtime wants to
9796 refer to it in a manner hidden from the compiler. So we must force the
9798 DECL_PRESERVE_P (decl
) = 1;
9799 finish_var_decl (decl
, objc_build_constructor (TREE_TYPE (decl
), v
));
9802 /* Look up ID as an instance variable. OTHER contains the result of
9803 the C or C++ lookup, which we may want to use instead. */
9806 objc_lookup_ivar (tree other
, tree id
)
9810 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9811 if (!objc_method_context
)
9814 if (!strcmp (IDENTIFIER_POINTER (id
), "super"))
9815 /* We have a message to super. */
9816 return get_super_receiver ();
9818 /* In a class method, look up an instance variable only as a last
9820 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
9821 && other
&& other
!= error_mark_node
)
9824 /* Look up the ivar, but do not use it if it is not accessible. */
9825 ivar
= is_ivar (objc_ivar_chain
, id
);
9827 if (!ivar
|| is_private (ivar
))
9830 /* In an instance method, a local variable (or parameter) may hide the
9831 instance variable. */
9832 if (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
9833 && other
&& other
!= error_mark_node
9835 && CP_DECL_CONTEXT (other
) != global_namespace
)
9837 && !DECL_FILE_SCOPE_P (other
))
9840 warning (0, "local declaration of %qE hides instance variable",
9846 /* At this point, we are either in an instance method with no obscuring
9847 local definitions, or in a class method with no alternate definitions
9849 return build_ivar_reference (id
);
9852 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9853 needs to be done if we are calling a function through a cast. */
9856 objc_rewrite_function_call (tree function
, tree first_param
)
9858 if (TREE_CODE (function
) == NOP_EXPR
9859 && TREE_CODE (TREE_OPERAND (function
, 0)) == ADDR_EXPR
9860 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function
, 0), 0))
9863 function
= build3 (OBJ_TYPE_REF
, TREE_TYPE (function
),
9864 TREE_OPERAND (function
, 0),
9865 first_param
, size_zero_node
);
9871 /* Look for the special case of OBJC_TYPE_REF with the address of
9872 a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend or one
9876 objc_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
9878 enum gimplify_status r0
, r1
;
9879 if (TREE_CODE (*expr_p
) == OBJ_TYPE_REF
9880 && TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p
)) == ADDR_EXPR
9881 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p
), 0))
9884 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9885 value of the OBJ_TYPE_REF, so force them to be emitted
9886 during subexpression evaluation rather than after the
9887 OBJ_TYPE_REF. This permits objc_msgSend calls in Objective
9888 C to use direct rather than indirect calls when the
9889 object expression has a postincrement. */
9890 r0
= gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p
), pre_p
, NULL
,
9891 is_gimple_val
, fb_rvalue
);
9892 r1
= gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p
), pre_p
, post_p
,
9893 is_gimple_val
, fb_rvalue
);
9895 return MIN (r0
, r1
);
9899 return (enum gimplify_status
) cp_gimplify_expr (expr_p
, pre_p
, post_p
);
9901 return (enum gimplify_status
) c_gimplify_expr (expr_p
, pre_p
, post_p
);
9905 #include "gt-objc-objc-act.h"