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 tree
objc_decay_parm_type (tree
);
217 static void objc_push_parm (tree
);
219 static tree
objc_get_parm_info (int);
221 static struct c_arg_info
*objc_get_parm_info (int);
224 /* Utilities for debugging and error diagnostics. */
226 static char *gen_type_name (tree
);
227 static char *gen_type_name_0 (tree
);
228 static char *gen_method_decl (tree
);
229 static char *gen_declaration (tree
);
231 /* Everything else. */
233 static tree
create_field_decl (tree
, const char *);
234 static void add_class_reference (tree
);
235 static void build_protocol_template (void);
236 static tree
encode_method_prototype (tree
);
237 static void generate_classref_translation_entry (tree
);
238 static void handle_class_ref (tree
);
239 static void generate_struct_by_value_array (void)
241 static void mark_referenced_methods (void);
242 static void generate_objc_image_info (void);
244 /*** Private Interface (data) ***/
246 /* Reserved tag definitions. */
248 #define OBJECT_TYPEDEF_NAME "id"
249 #define CLASS_TYPEDEF_NAME "Class"
251 #define TAG_OBJECT "objc_object"
252 #define TAG_CLASS "objc_class"
253 #define TAG_SUPER "objc_super"
254 #define TAG_SELECTOR "objc_selector"
256 #define UTAG_CLASS "_objc_class"
257 #define UTAG_IVAR "_objc_ivar"
258 #define UTAG_IVAR_LIST "_objc_ivar_list"
259 #define UTAG_METHOD "_objc_method"
260 #define UTAG_METHOD_LIST "_objc_method_list"
261 #define UTAG_CATEGORY "_objc_category"
262 #define UTAG_MODULE "_objc_module"
263 #define UTAG_SYMTAB "_objc_symtab"
264 #define UTAG_SUPER "_objc_super"
265 #define UTAG_SELECTOR "_objc_selector"
267 #define UTAG_PROTOCOL "_objc_protocol"
268 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
269 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
271 /* Note that the string object global name is only needed for the
273 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
275 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
277 static const char *TAG_GETCLASS
;
278 static const char *TAG_GETMETACLASS
;
279 static const char *TAG_MSGSEND
;
280 static const char *TAG_MSGSENDSUPER
;
281 /* The NeXT Objective-C messenger may have two extra entry points, for use
282 when returning a structure. */
283 static const char *TAG_MSGSEND_STRET
;
284 static const char *TAG_MSGSENDSUPER_STRET
;
285 static const char *default_constant_string_class_name
;
287 /* Runtime metadata flags. */
288 #define CLS_FACTORY 0x0001L
289 #define CLS_META 0x0002L
290 #define CLS_HAS_CXX_STRUCTORS 0x2000L
292 #define OBJC_MODIFIER_STATIC 0x00000001
293 #define OBJC_MODIFIER_FINAL 0x00000002
294 #define OBJC_MODIFIER_PUBLIC 0x00000004
295 #define OBJC_MODIFIER_PRIVATE 0x00000008
296 #define OBJC_MODIFIER_PROTECTED 0x00000010
297 #define OBJC_MODIFIER_NATIVE 0x00000020
298 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
299 #define OBJC_MODIFIER_ABSTRACT 0x00000080
300 #define OBJC_MODIFIER_VOLATILE 0x00000100
301 #define OBJC_MODIFIER_TRANSIENT 0x00000200
302 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
304 /* NeXT-specific tags. */
306 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
307 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
308 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
309 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
310 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
311 #define TAG_EXCEPTIONMATCH "objc_exception_match"
312 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
313 #define TAG_SYNCENTER "objc_sync_enter"
314 #define TAG_SYNCEXIT "objc_sync_exit"
315 #define TAG_SETJMP "_setjmp"
316 #define UTAG_EXCDATA "_objc_exception_data"
318 #define TAG_ASSIGNIVAR "objc_assign_ivar"
319 #define TAG_ASSIGNGLOBAL "objc_assign_global"
320 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
322 /* Branch entry points. All that matters here are the addresses;
323 functions with these names do not really exist in libobjc. */
325 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
326 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
328 #define TAG_CXX_CONSTRUCT ".cxx_construct"
329 #define TAG_CXX_DESTRUCT ".cxx_destruct"
331 /* GNU-specific tags. */
333 #define TAG_EXECCLASS "__objc_exec_class"
334 #define TAG_GNUINIT "__objc_gnu_init"
336 /* Flags for lookup_method_static(). */
337 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
338 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
340 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
341 tree objc_global_trees
[OCTI_MAX
];
343 static void handle_impent (struct imp_entry
*);
345 struct imp_entry
*imp_list
= 0;
346 int imp_count
= 0; /* `@implementation' */
347 int cat_count
= 0; /* `@category' */
349 enum tree_code objc_inherit_code
;
350 int objc_public_flag
;
352 /* Use to generate method labels. */
353 static int method_slot
= 0;
355 static int objc_collecting_ivars
= 0;
359 static char *errbuf
; /* Buffer for error diagnostics */
361 /* Data imported from tree.c. */
363 extern enum debug_info_type write_symbols
;
365 /* Data imported from toplev.c. */
367 extern const char *dump_base_name
;
369 static int flag_typed_selectors
;
371 /* Store all constructed constant strings in a hash table so that
372 they get uniqued properly. */
374 struct GTY(()) string_descriptor
{
375 /* The literal argument . */
378 /* The resulting constant string. */
382 static GTY((param_is (struct string_descriptor
))) htab_t string_htab
;
384 /* Store the EH-volatilized types in a hash table, for easy retrieval. */
385 struct GTY(()) volatilized_type
{
389 static GTY((param_is (struct volatilized_type
))) htab_t volatilized_htab
;
391 FILE *gen_declaration_file
;
393 /* Tells "encode_pointer/encode_aggregate" whether we are generating
394 type descriptors for instance variables (as opposed to methods).
395 Type descriptors for instance variables contain more information
396 than methods (for static typing and embedded structures). */
398 static int generating_instance_variables
= 0;
400 /* For building an objc struct. These may not be used when this file
401 is compiled as part of obj-c++. */
403 static bool objc_building_struct
;
404 static struct c_struct_parse_info
*objc_struct_info ATTRIBUTE_UNUSED
;
406 /* Start building a struct for objc. */
409 objc_start_struct (tree name
)
411 gcc_assert (!objc_building_struct
);
412 objc_building_struct
= true;
413 return start_struct (input_location
, RECORD_TYPE
, name
, &objc_struct_info
);
416 /* Finish building a struct for objc. */
419 objc_finish_struct (tree type
, tree fieldlist
)
421 gcc_assert (objc_building_struct
);
422 objc_building_struct
= false;
423 return finish_struct (input_location
, type
, fieldlist
, NULL_TREE
,
428 build_sized_array_type (tree base_type
, int size
)
430 tree index_type
= build_index_type (build_int_cst (NULL_TREE
, size
- 1));
431 return build_array_type (base_type
, index_type
);
435 add_field_decl (tree type
, const char *name
, tree
**chain
)
437 tree field
= create_field_decl (type
, name
);
441 *chain
= &DECL_CHAIN (field
);
446 /* Some platforms pass small structures through registers versus
447 through an invisible pointer. Determine at what size structure is
448 the transition point between the two possibilities. */
451 generate_struct_by_value_array (void)
456 int aggregate_in_mem
[32];
459 /* Presumably no platform passes 32 byte structures in a register. */
460 for (i
= 1; i
< 32; i
++)
465 /* Create an unnamed struct that has `i' character components */
466 type
= objc_start_struct (NULL_TREE
);
468 strcpy (buffer
, "c1");
469 decls
= add_field_decl (char_type_node
, buffer
, &chain
);
471 for (j
= 1; j
< i
; j
++)
473 sprintf (buffer
, "c%d", j
+ 1);
474 add_field_decl (char_type_node
, buffer
, &chain
);
476 objc_finish_struct (type
, decls
);
478 aggregate_in_mem
[i
] = aggregate_value_p (type
, 0);
479 if (!aggregate_in_mem
[i
])
483 /* We found some structures that are returned in registers instead of memory
484 so output the necessary data. */
487 for (i
= 31; i
>= 0; i
--)
488 if (!aggregate_in_mem
[i
])
490 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i
);
492 /* The first member of the structure is always 0 because we don't handle
493 structures with 0 members */
494 printf ("static int struct_forward_array[] = {\n 0");
496 for (j
= 1; j
<= i
; j
++)
497 printf (", %d", aggregate_in_mem
[j
]);
508 if (cxx_init () == false)
510 if (c_objc_common_init () == false)
514 /* If gen_declaration desired, open the output file. */
515 if (flag_gen_declaration
)
517 register char * const dumpname
= concat (dump_base_name
, ".decl", NULL
);
518 gen_declaration_file
= fopen (dumpname
, "w");
519 if (gen_declaration_file
== 0)
520 fatal_error ("can't open %s: %m", dumpname
);
524 if (flag_next_runtime
)
526 TAG_GETCLASS
= "objc_getClass";
527 TAG_GETMETACLASS
= "objc_getMetaClass";
528 TAG_MSGSEND
= "objc_msgSend";
529 TAG_MSGSENDSUPER
= "objc_msgSendSuper";
530 TAG_MSGSEND_STRET
= "objc_msgSend_stret";
531 TAG_MSGSENDSUPER_STRET
= "objc_msgSendSuper_stret";
532 default_constant_string_class_name
= "NSConstantString";
536 TAG_GETCLASS
= "objc_get_class";
537 TAG_GETMETACLASS
= "objc_get_meta_class";
538 TAG_MSGSEND
= "objc_msg_lookup";
539 TAG_MSGSENDSUPER
= "objc_msg_lookup_super";
540 /* GNU runtime does not provide special functions to support
541 structure-returning methods. */
542 default_constant_string_class_name
= "NXConstantString";
543 flag_typed_selectors
= 1;
544 /* GNU runtime does not need the compiler to change code
545 in order to do GC. */
548 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
555 if (print_struct_values
&& !flag_compare_debug
)
556 generate_struct_by_value_array ();
562 objc_finish_file (void)
564 mark_referenced_methods ();
567 /* We need to instantiate templates _before_ we emit ObjC metadata;
568 if we do not, some metadata (such as selectors) may go missing. */
570 instantiate_pending_templates (0);
573 /* Finalize Objective-C runtime data. No need to generate tables
574 and code if only checking syntax, or if generating a PCH file. */
575 if (!flag_syntax_only
&& !pch_file
)
578 if (gen_declaration_file
)
579 fclose (gen_declaration_file
);
582 /* Return the first occurrence of a method declaration corresponding
583 to sel_name in rproto_list. Search rproto_list recursively.
584 If is_class is 0, search for instance methods, otherwise for class
587 lookup_method_in_protocol_list (tree rproto_list
, tree sel_name
,
593 for (rproto
= rproto_list
; rproto
; rproto
= TREE_CHAIN (rproto
))
595 p
= TREE_VALUE (rproto
);
597 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
599 if ((fnd
= lookup_method (is_class
600 ? PROTOCOL_CLS_METHODS (p
)
601 : PROTOCOL_NST_METHODS (p
), sel_name
)))
603 else if (PROTOCOL_LIST (p
))
604 fnd
= lookup_method_in_protocol_list (PROTOCOL_LIST (p
),
609 ; /* An identifier...if we could not find a protocol. */
620 lookup_protocol_in_reflist (tree rproto_list
, tree lproto
)
624 /* Make sure the protocol is supported by the object on the rhs. */
625 if (TREE_CODE (lproto
) == PROTOCOL_INTERFACE_TYPE
)
628 for (rproto
= rproto_list
; rproto
; rproto
= TREE_CHAIN (rproto
))
630 p
= TREE_VALUE (rproto
);
632 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
637 else if (PROTOCOL_LIST (p
))
638 fnd
= lookup_protocol_in_reflist (PROTOCOL_LIST (p
), lproto
);
647 ; /* An identifier...if we could not find a protocol. */
654 objc_start_class_interface (tree klass
, tree super_class
, tree protos
)
656 objc_interface_context
658 = start_class (CLASS_INTERFACE_TYPE
, klass
, super_class
, protos
);
659 objc_public_flag
= 0;
663 objc_start_category_interface (tree klass
, tree categ
, tree protos
)
665 objc_interface_context
666 = start_class (CATEGORY_INTERFACE_TYPE
, klass
, categ
, protos
);
668 = continue_class (objc_interface_context
);
672 objc_start_protocol (tree name
, tree protos
)
674 objc_interface_context
675 = start_protocol (PROTOCOL_INTERFACE_TYPE
, name
, protos
);
679 objc_continue_interface (void)
682 = continue_class (objc_interface_context
);
686 objc_finish_interface (void)
688 finish_class (objc_interface_context
);
689 objc_interface_context
= NULL_TREE
;
693 objc_start_class_implementation (tree klass
, tree super_class
)
695 objc_implementation_context
697 = start_class (CLASS_IMPLEMENTATION_TYPE
, klass
, super_class
, NULL_TREE
);
698 objc_public_flag
= 0;
702 objc_start_category_implementation (tree klass
, tree categ
)
704 objc_implementation_context
705 = start_class (CATEGORY_IMPLEMENTATION_TYPE
, klass
, categ
, NULL_TREE
);
707 = continue_class (objc_implementation_context
);
711 objc_continue_implementation (void)
714 = continue_class (objc_implementation_context
);
718 objc_finish_implementation (void)
721 if (flag_objc_call_cxx_cdtors
)
722 objc_generate_cxx_cdtors ();
725 if (objc_implementation_context
)
727 finish_class (objc_implementation_context
);
728 objc_ivar_chain
= NULL_TREE
;
729 objc_implementation_context
= NULL_TREE
;
732 warning (0, "%<@end%> must appear in an @implementation context");
736 objc_set_visibility (int visibility
)
738 objc_public_flag
= visibility
;
742 objc_set_method_type (enum tree_code type
)
744 objc_inherit_code
= (type
== PLUS_EXPR
746 : INSTANCE_METHOD_DECL
);
750 objc_build_method_signature (tree rettype
, tree selector
,
751 tree optparms
, bool ellipsis
)
753 return build_method_decl (objc_inherit_code
, rettype
, selector
,
758 objc_add_method_declaration (tree decl
)
760 if (!objc_interface_context
)
762 /* PS: At the moment, due to how the parser works, it should be
763 impossible to get here. But it's good to have the check in
764 case the parser changes.
766 fatal_error ("method declaration not in @interface context");
769 objc_add_method (objc_interface_context
,
771 objc_inherit_code
== CLASS_METHOD_DECL
);
774 /* Return 'true' if the method definition could be started, and
775 'false' if not (because we are outside an @implementation context).
778 objc_start_method_definition (tree decl
)
780 if (!objc_implementation_context
)
782 error ("method definition not in @implementation context");
787 /* Indicate no valid break/continue context by setting these variables
788 to some non-null, non-label value. We'll notice and emit the proper
789 error message in c_finish_bc_stmt. */
790 c_break_label
= c_cont_label
= size_zero_node
;
793 objc_add_method (objc_implementation_context
,
795 objc_inherit_code
== CLASS_METHOD_DECL
);
796 start_method_def (decl
);
801 objc_add_instance_variable (tree decl
)
803 (void) add_instance_variable (objc_ivar_context
,
808 /* Return 1 if IDENT is an ObjC/ObjC++ reserved keyword in the context of
812 objc_is_reserved_word (tree ident
)
814 unsigned char code
= C_RID_CODE (ident
);
816 return (OBJC_IS_AT_KEYWORD (code
)
817 || code
== RID_CLASS
|| code
== RID_PUBLIC
818 || code
== RID_PROTECTED
|| code
== RID_PRIVATE
819 || code
== RID_TRY
|| code
== RID_THROW
|| code
== RID_CATCH
);
822 /* Return true if TYPE is 'id'. */
825 objc_is_object_id (tree type
)
827 return OBJC_TYPE_NAME (type
) == objc_object_id
;
831 objc_is_class_id (tree type
)
833 return OBJC_TYPE_NAME (type
) == objc_class_id
;
836 /* Construct a C struct with same name as KLASS, a base struct with tag
837 SUPER_NAME (if any), and FIELDS indicated. */
840 objc_build_struct (tree klass
, tree fields
, tree super_name
)
842 tree name
= CLASS_NAME (klass
);
843 tree s
= objc_start_struct (name
);
844 tree super
= (super_name
? xref_tag (RECORD_TYPE
, super_name
) : NULL_TREE
);
846 VEC(tree
,heap
) *objc_info
= NULL
;
851 /* Prepend a packed variant of the base class into the layout. This
852 is necessary to preserve ObjC ABI compatibility. */
853 tree base
= build_decl (input_location
,
854 FIELD_DECL
, NULL_TREE
, super
);
855 tree field
= TYPE_FIELDS (super
);
857 while (field
&& DECL_CHAIN (field
)
858 && TREE_CODE (DECL_CHAIN (field
)) == FIELD_DECL
)
859 field
= DECL_CHAIN (field
);
861 /* For ObjC ABI purposes, the "packed" size of a base class is
862 the sum of the offset and the size (in bits) of the last field
865 = (field
&& TREE_CODE (field
) == FIELD_DECL
866 ? size_binop (PLUS_EXPR
,
867 size_binop (PLUS_EXPR
,
870 convert (bitsizetype
,
871 DECL_FIELD_OFFSET (field
)),
872 bitsize_int (BITS_PER_UNIT
)),
873 DECL_FIELD_BIT_OFFSET (field
)),
875 : bitsize_zero_node
);
876 DECL_SIZE_UNIT (base
)
877 = size_binop (FLOOR_DIV_EXPR
, convert (sizetype
, DECL_SIZE (base
)),
878 size_int (BITS_PER_UNIT
));
879 DECL_ARTIFICIAL (base
) = 1;
880 DECL_ALIGN (base
) = 1;
881 DECL_FIELD_CONTEXT (base
) = s
;
883 DECL_FIELD_IS_BASE (base
) = 1;
886 TREE_NO_WARNING (fields
) = 1; /* Suppress C++ ABI warnings -- we */
887 #endif /* are following the ObjC ABI here. */
888 DECL_CHAIN (base
) = fields
;
892 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
893 in all variants of this RECORD_TYPE to be clobbered, but it is therein
894 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
895 Hence, we must squirrel away the ObjC-specific information before calling
896 finish_struct(), and then reinstate it afterwards. */
898 for (t
= TYPE_NEXT_VARIANT (s
); t
; t
= TYPE_NEXT_VARIANT (t
))
900 if (!TYPE_HAS_OBJC_INFO (t
))
902 INIT_TYPE_OBJC_INFO (t
);
903 TYPE_OBJC_INTERFACE (t
) = klass
;
905 VEC_safe_push (tree
, heap
, objc_info
, TYPE_OBJC_INFO (t
));
908 /* Point the struct at its related Objective-C class. */
909 INIT_TYPE_OBJC_INFO (s
);
910 TYPE_OBJC_INTERFACE (s
) = klass
;
912 s
= objc_finish_struct (s
, fields
);
914 for (i
= 0, t
= TYPE_NEXT_VARIANT (s
); t
; t
= TYPE_NEXT_VARIANT (t
), i
++)
916 TYPE_OBJC_INFO (t
) = VEC_index (tree
, objc_info
, i
);
917 /* Replace the IDENTIFIER_NODE with an actual @interface. */
918 TYPE_OBJC_INTERFACE (t
) = klass
;
920 VEC_free (tree
, heap
, objc_info
);
922 /* Use TYPE_BINFO structures to point at the super class, if any. */
923 objc_xref_basetypes (s
, super
);
925 /* Mark this struct as a class template. */
926 CLASS_STATIC_TEMPLATE (klass
) = s
;
931 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
932 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
935 objc_build_volatilized_type (tree type
)
939 /* Check if we have not constructed the desired variant already. */
940 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
942 /* The type qualifiers must (obviously) match up. */
943 if (!TYPE_VOLATILE (t
)
944 || (TYPE_READONLY (t
) != TYPE_READONLY (type
))
945 || (TYPE_RESTRICT (t
) != TYPE_RESTRICT (type
)))
948 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
949 info, if any) must match up. */
950 if (POINTER_TYPE_P (t
)
951 && (TREE_TYPE (t
) != TREE_TYPE (type
)))
954 /* Everything matches up! */
958 /* Ok, we could not re-use any of the pre-existing variants. Create
960 t
= build_variant_type_copy (type
);
961 TYPE_VOLATILE (t
) = 1;
963 /* Set up the canonical type information. */
964 if (TYPE_STRUCTURAL_EQUALITY_P (type
))
965 SET_TYPE_STRUCTURAL_EQUALITY (t
);
966 else if (TYPE_CANONICAL (type
) != type
)
967 TYPE_CANONICAL (t
) = objc_build_volatilized_type (TYPE_CANONICAL (type
));
969 TYPE_CANONICAL (t
) = t
;
974 /* Mark DECL as being 'volatile' for purposes of Darwin
975 _setjmp()/_longjmp() exception handling. Called from
976 objc_mark_locals_volatile(). */
978 objc_volatilize_decl (tree decl
)
980 /* Do not mess with variables that are 'static' or (already)
982 if (!TREE_THIS_VOLATILE (decl
) && !TREE_STATIC (decl
)
983 && (TREE_CODE (decl
) == VAR_DECL
984 || TREE_CODE (decl
) == PARM_DECL
))
986 tree t
= TREE_TYPE (decl
);
987 struct volatilized_type key
;
990 t
= objc_build_volatilized_type (t
);
992 loc
= htab_find_slot (volatilized_htab
, &key
, INSERT
);
996 *loc
= ggc_alloc_volatilized_type ();
997 ((struct volatilized_type
*) *loc
)->type
= t
;
1000 TREE_TYPE (decl
) = t
;
1001 TREE_THIS_VOLATILE (decl
) = 1;
1002 TREE_SIDE_EFFECTS (decl
) = 1;
1003 DECL_REGISTER (decl
) = 0;
1005 C_DECL_REGISTER (decl
) = 0;
1010 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
1011 (including its categories and superclasses) or by object type TYP.
1012 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
1015 objc_lookup_protocol (tree proto
, tree cls
, tree typ
, bool warn
)
1017 bool class_type
= (cls
!= NULL_TREE
);
1023 /* Check protocols adopted by the class and its categories. */
1024 for (c
= cls
; c
; c
= CLASS_CATEGORY_LIST (c
))
1026 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c
), proto
))
1030 /* Repeat for superclasses. */
1031 cls
= lookup_interface (CLASS_SUPER_NAME (cls
));
1034 /* Check for any protocols attached directly to the object type. */
1035 if (TYPE_HAS_OBJC_INFO (typ
))
1037 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ
), proto
))
1044 gen_type_name_0 (class_type
? typ
: TYPE_POINTER_TO (typ
));
1045 /* NB: Types 'id' and 'Class' cannot reasonably be described as
1046 "implementing" a given protocol, since they do not have an
1049 warning (0, "class %qs does not implement the %qE protocol",
1050 identifier_to_locale (errbuf
), PROTOCOL_NAME (proto
));
1052 warning (0, "type %qs does not conform to the %qE protocol",
1053 identifier_to_locale (errbuf
), PROTOCOL_NAME (proto
));
1059 /* Check if class RCLS and instance struct type RTYP conform to at least the
1060 same protocols that LCLS and LTYP conform to. */
1063 objc_compare_protocols (tree lcls
, tree ltyp
, tree rcls
, tree rtyp
, bool warn
)
1066 bool have_lproto
= false;
1070 /* NB: We do _not_ look at categories defined for LCLS; these may or
1071 may not get loaded in, and therefore it is unreasonable to require
1072 that RCLS/RTYP must implement any of their protocols. */
1073 for (p
= CLASS_PROTOCOL_LIST (lcls
); p
; p
= TREE_CHAIN (p
))
1077 if (!objc_lookup_protocol (TREE_VALUE (p
), rcls
, rtyp
, warn
))
1081 /* Repeat for superclasses. */
1082 lcls
= lookup_interface (CLASS_SUPER_NAME (lcls
));
1085 /* Check for any protocols attached directly to the object type. */
1086 if (TYPE_HAS_OBJC_INFO (ltyp
))
1088 for (p
= TYPE_OBJC_PROTOCOL_LIST (ltyp
); p
; p
= TREE_CHAIN (p
))
1092 if (!objc_lookup_protocol (TREE_VALUE (p
), rcls
, rtyp
, warn
))
1097 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1098 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1099 away with simply checking for 'id' or 'Class' (!RCLS), since this
1100 routine will not get called in other cases. */
1101 return have_lproto
|| (rcls
!= NULL_TREE
);
1104 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
1105 Both TYPE1 and TYPE2 must be pointers, and already determined to be
1106 compatible by objc_compare_types() below. */
1109 objc_common_type (tree type1
, tree type2
)
1111 tree inner1
= TREE_TYPE (type1
), inner2
= TREE_TYPE (type2
);
1113 while (POINTER_TYPE_P (inner1
))
1115 inner1
= TREE_TYPE (inner1
);
1116 inner2
= TREE_TYPE (inner2
);
1119 /* If one type is derived from another, return the base type. */
1120 if (DERIVED_FROM_P (inner1
, inner2
))
1122 else if (DERIVED_FROM_P (inner2
, inner1
))
1125 /* If both types are 'Class', return 'Class'. */
1126 if (objc_is_class_id (inner1
) && objc_is_class_id (inner2
))
1127 return objc_class_type
;
1129 /* Otherwise, return 'id'. */
1130 return objc_object_type
;
1133 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1134 an instance of RTYP to an instance of LTYP or to compare the two
1135 (if ARGNO is equal to -3), per ObjC type system rules. Before
1136 returning 'true', this routine may issue warnings related to, e.g.,
1137 protocol conformance. When returning 'false', the routine must
1138 produce absolutely no warnings; the C or C++ front-end will do so
1139 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1140 the routine must return 'false'.
1142 The ARGNO parameter is encoded as follows:
1143 >= 1 Parameter number (CALLEE contains function being called);
1147 -3 Comparison (LTYP and RTYP may match in either direction);
1148 -4 Silent comparison (for C++ overload resolution).
1152 objc_compare_types (tree ltyp
, tree rtyp
, int argno
, tree callee
)
1154 tree lcls
, rcls
, lproto
, rproto
;
1155 bool pointers_compatible
;
1157 /* We must be dealing with pointer types */
1158 if (!POINTER_TYPE_P (ltyp
) || !POINTER_TYPE_P (rtyp
))
1163 ltyp
= TREE_TYPE (ltyp
); /* Remove indirections. */
1164 rtyp
= TREE_TYPE (rtyp
);
1166 while (POINTER_TYPE_P (ltyp
) && POINTER_TYPE_P (rtyp
));
1168 /* We must also handle function pointers, since ObjC is a bit more
1169 lenient than C or C++ on this. */
1170 if (TREE_CODE (ltyp
) == FUNCTION_TYPE
&& TREE_CODE (rtyp
) == FUNCTION_TYPE
)
1172 /* Return types must be covariant. */
1173 if (!comptypes (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
))
1174 && !objc_compare_types (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
),
1178 /* Argument types must be contravariant. */
1179 for (ltyp
= TYPE_ARG_TYPES (ltyp
), rtyp
= TYPE_ARG_TYPES (rtyp
);
1180 ltyp
&& rtyp
; ltyp
= TREE_CHAIN (ltyp
), rtyp
= TREE_CHAIN (rtyp
))
1182 if (!comptypes (TREE_VALUE (rtyp
), TREE_VALUE (ltyp
))
1183 && !objc_compare_types (TREE_VALUE (rtyp
), TREE_VALUE (ltyp
),
1188 return (ltyp
== rtyp
);
1191 /* Past this point, we are only interested in ObjC class instances,
1192 or 'id' or 'Class'. */
1193 if (TREE_CODE (ltyp
) != RECORD_TYPE
|| TREE_CODE (rtyp
) != RECORD_TYPE
)
1196 if (!objc_is_object_id (ltyp
) && !objc_is_class_id (ltyp
)
1197 && !TYPE_HAS_OBJC_INFO (ltyp
))
1200 if (!objc_is_object_id (rtyp
) && !objc_is_class_id (rtyp
)
1201 && !TYPE_HAS_OBJC_INFO (rtyp
))
1204 /* Past this point, we are committed to returning 'true' to the caller
1205 (unless performing a silent comparison; see below). However, we can
1206 still warn about type and/or protocol mismatches. */
1208 if (TYPE_HAS_OBJC_INFO (ltyp
))
1210 lcls
= TYPE_OBJC_INTERFACE (ltyp
);
1211 lproto
= TYPE_OBJC_PROTOCOL_LIST (ltyp
);
1214 lcls
= lproto
= NULL_TREE
;
1216 if (TYPE_HAS_OBJC_INFO (rtyp
))
1218 rcls
= TYPE_OBJC_INTERFACE (rtyp
);
1219 rproto
= TYPE_OBJC_PROTOCOL_LIST (rtyp
);
1222 rcls
= rproto
= NULL_TREE
;
1224 /* If we could not find an @interface declaration, we must have
1225 only seen a @class declaration; for purposes of type comparison,
1226 treat it as a stand-alone (root) class. */
1228 if (lcls
&& TREE_CODE (lcls
) == IDENTIFIER_NODE
)
1231 if (rcls
&& TREE_CODE (rcls
) == IDENTIFIER_NODE
)
1234 /* If either type is an unqualified 'id', we're done. */
1235 if ((!lproto
&& objc_is_object_id (ltyp
))
1236 || (!rproto
&& objc_is_object_id (rtyp
)))
1239 pointers_compatible
= (TYPE_MAIN_VARIANT (ltyp
) == TYPE_MAIN_VARIANT (rtyp
));
1241 /* If the underlying types are the same, and at most one of them has
1242 a protocol list, we do not need to issue any diagnostics. */
1243 if (pointers_compatible
&& (!lproto
|| !rproto
))
1246 /* If exactly one of the types is 'Class', issue a diagnostic; any
1247 exceptions of this rule have already been handled. */
1248 if (objc_is_class_id (ltyp
) ^ objc_is_class_id (rtyp
))
1249 pointers_compatible
= false;
1250 /* Otherwise, check for inheritance relations. */
1253 if (!pointers_compatible
)
1255 = (objc_is_object_id (ltyp
) || objc_is_object_id (rtyp
));
1257 if (!pointers_compatible
)
1258 pointers_compatible
= DERIVED_FROM_P (ltyp
, rtyp
);
1260 if (!pointers_compatible
&& argno
<= -3)
1261 pointers_compatible
= DERIVED_FROM_P (rtyp
, ltyp
);
1264 /* If the pointers match modulo protocols, check for protocol conformance
1266 if (pointers_compatible
)
1268 pointers_compatible
= objc_compare_protocols (lcls
, ltyp
, rcls
, rtyp
,
1271 if (!pointers_compatible
&& argno
== -3)
1272 pointers_compatible
= objc_compare_protocols (rcls
, rtyp
, lcls
, ltyp
,
1276 if (!pointers_compatible
)
1278 /* The two pointers are not exactly compatible. Issue a warning, unless
1279 we are performing a silent comparison, in which case return 'false'
1281 /* NB: For the time being, we shall make our warnings look like their
1282 C counterparts. In the future, we may wish to make them more
1290 warning (0, "comparison of distinct Objective-C types lacks a cast");
1294 warning (0, "initialization from distinct Objective-C type");
1298 warning (0, "assignment from distinct Objective-C type");
1302 warning (0, "distinct Objective-C type in return");
1306 warning (0, "passing argument %d of %qE from distinct "
1307 "Objective-C type", argno
, callee
);
1315 /* This routine is similar to objc_compare_types except that function-pointers are
1316 excluded. This is because, caller assumes that common types are of (id, Object*)
1317 variety and calls objc_common_type to obtain a common type. There is no commonolty
1318 between two function-pointers in this regard. */
1321 objc_have_common_type (tree ltyp
, tree rtyp
, int argno
, tree callee
)
1323 if (objc_compare_types (ltyp
, rtyp
, argno
, callee
))
1325 /* exclude function-pointer types. */
1328 ltyp
= TREE_TYPE (ltyp
); /* Remove indirections. */
1329 rtyp
= TREE_TYPE (rtyp
);
1331 while (POINTER_TYPE_P (ltyp
) && POINTER_TYPE_P (rtyp
));
1332 return !(TREE_CODE (ltyp
) == FUNCTION_TYPE
&& TREE_CODE (rtyp
) == FUNCTION_TYPE
);
1337 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1338 lives in the volatilized hash table, ignore the 'volatile' bit when
1339 making the comparison. */
1342 objc_type_quals_match (tree ltyp
, tree rtyp
)
1344 int lquals
= TYPE_QUALS (ltyp
), rquals
= TYPE_QUALS (rtyp
);
1345 struct volatilized_type key
;
1349 if (htab_find_slot (volatilized_htab
, &key
, NO_INSERT
))
1350 lquals
&= ~TYPE_QUAL_VOLATILE
;
1354 if (htab_find_slot (volatilized_htab
, &key
, NO_INSERT
))
1355 rquals
&= ~TYPE_QUAL_VOLATILE
;
1357 return (lquals
== rquals
);
1361 /* Determine if CHILD is derived from PARENT. The routine assumes that
1362 both parameters are RECORD_TYPEs, and is non-reflexive. */
1365 objc_derived_from_p (tree parent
, tree child
)
1367 parent
= TYPE_MAIN_VARIANT (parent
);
1369 for (child
= TYPE_MAIN_VARIANT (child
);
1370 TYPE_BINFO (child
) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child
));)
1372 child
= TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1373 (TYPE_BINFO (child
),
1376 if (child
== parent
)
1385 objc_build_component_ref (tree datum
, tree component
)
1387 /* If COMPONENT is NULL, the caller is referring to the anonymous
1388 base class field. */
1391 tree base
= TYPE_FIELDS (TREE_TYPE (datum
));
1393 return build3 (COMPONENT_REF
, TREE_TYPE (base
), datum
, base
, NULL_TREE
);
1396 /* The 'build_component_ref' routine has been removed from the C++
1397 front-end, but 'finish_class_member_access_expr' seems to be
1398 a worthy substitute. */
1400 return finish_class_member_access_expr (datum
, component
, false,
1401 tf_warning_or_error
);
1403 return build_component_ref (input_location
, datum
, component
);
1407 /* Recursively copy inheritance information rooted at BINFO. To do this,
1408 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1411 objc_copy_binfo (tree binfo
)
1413 tree btype
= BINFO_TYPE (binfo
);
1414 tree binfo2
= make_tree_binfo (BINFO_N_BASE_BINFOS (binfo
));
1418 BINFO_TYPE (binfo2
) = btype
;
1419 BINFO_OFFSET (binfo2
) = BINFO_OFFSET (binfo
);
1420 BINFO_BASE_ACCESSES (binfo2
) = BINFO_BASE_ACCESSES (binfo
);
1422 /* Recursively copy base binfos of BINFO. */
1423 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
1425 tree base_binfo2
= objc_copy_binfo (base_binfo
);
1427 BINFO_INHERITANCE_CHAIN (base_binfo2
) = binfo2
;
1428 BINFO_BASE_APPEND (binfo2
, base_binfo2
);
1434 /* Record superclass information provided in BASETYPE for ObjC class REF.
1435 This is loosely based on cp/decl.c:xref_basetypes(). */
1438 objc_xref_basetypes (tree ref
, tree basetype
)
1440 tree binfo
= make_tree_binfo (basetype
? 1 : 0);
1442 TYPE_BINFO (ref
) = binfo
;
1443 BINFO_OFFSET (binfo
) = size_zero_node
;
1444 BINFO_TYPE (binfo
) = ref
;
1448 tree base_binfo
= objc_copy_binfo (TYPE_BINFO (basetype
));
1450 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
1451 BINFO_BASE_ACCESSES (binfo
) = VEC_alloc (tree
, gc
, 1);
1452 BINFO_BASE_APPEND (binfo
, base_binfo
);
1453 BINFO_BASE_ACCESS_APPEND (binfo
, access_public_node
);
1458 volatilized_hash (const void *ptr
)
1460 const_tree
const typ
= ((const struct volatilized_type
*)ptr
)->type
;
1462 return htab_hash_pointer(typ
);
1466 volatilized_eq (const void *ptr1
, const void *ptr2
)
1468 const_tree
const typ1
= ((const struct volatilized_type
*)ptr1
)->type
;
1469 const_tree
const typ2
= ((const struct volatilized_type
*)ptr2
)->type
;
1471 return typ1
== typ2
;
1474 /* Called from finish_decl. */
1477 objc_check_decl (tree decl
)
1479 tree type
= TREE_TYPE (decl
);
1481 if (TREE_CODE (type
) != RECORD_TYPE
)
1483 if (OBJC_TYPE_NAME (type
) && (type
= objc_is_class_name (OBJC_TYPE_NAME (type
))))
1484 error ("statically allocated instance of Objective-C class %qE",
1488 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
1489 either name an Objective-C class, or refer to the special 'id' or 'Class'
1490 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
1493 objc_get_protocol_qualified_type (tree interface
, tree protocols
)
1495 /* If INTERFACE is not provided, default to 'id'. */
1496 tree type
= (interface
? objc_is_id (interface
) : objc_object_type
);
1497 bool is_ptr
= (type
!= NULL_TREE
);
1501 type
= objc_is_class_name (interface
);
1505 /* If looking at a typedef, retrieve the precise type it
1507 if (TREE_CODE (interface
) == IDENTIFIER_NODE
)
1508 interface
= identifier_global_value (interface
);
1510 type
= ((interface
&& TREE_CODE (interface
) == TYPE_DECL
1511 && DECL_ORIGINAL_TYPE (interface
))
1512 ? DECL_ORIGINAL_TYPE (interface
)
1513 : xref_tag (RECORD_TYPE
, type
));
1521 type
= build_variant_type_copy (type
);
1523 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
1527 tree orig_pointee_type
= TREE_TYPE (type
);
1528 TREE_TYPE (type
) = build_variant_type_copy (orig_pointee_type
);
1530 /* Set up the canonical type information. */
1531 TYPE_CANONICAL (type
)
1532 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type
));
1534 TYPE_POINTER_TO (TREE_TYPE (type
)) = type
;
1535 type
= TREE_TYPE (type
);
1538 /* Look up protocols and install in lang specific list. */
1539 DUP_TYPE_OBJC_INFO (type
, TYPE_MAIN_VARIANT (type
));
1540 TYPE_OBJC_PROTOCOL_LIST (type
) = lookup_and_install_protocols (protocols
);
1542 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
1543 return the pointer to the new pointee variant. */
1545 type
= TYPE_POINTER_TO (type
);
1547 TYPE_OBJC_INTERFACE (type
)
1548 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type
));
1554 /* Check for circular dependencies in protocols. The arguments are
1555 PROTO, the protocol to check, and LIST, a list of protocol it
1559 check_protocol_recursively (tree proto
, tree list
)
1563 for (p
= list
; p
; p
= TREE_CHAIN (p
))
1565 tree pp
= TREE_VALUE (p
);
1567 if (TREE_CODE (pp
) == IDENTIFIER_NODE
)
1568 pp
= lookup_protocol (pp
);
1571 fatal_error ("protocol %qE has circular dependency",
1572 PROTOCOL_NAME (pp
));
1574 check_protocol_recursively (proto
, PROTOCOL_LIST (pp
));
1578 /* Look up PROTOCOLS, and return a list of those that are found.
1579 If none are found, return NULL. */
1582 lookup_and_install_protocols (tree protocols
)
1585 tree return_value
= NULL_TREE
;
1587 for (proto
= protocols
; proto
; proto
= TREE_CHAIN (proto
))
1589 tree ident
= TREE_VALUE (proto
);
1590 tree p
= lookup_protocol (ident
);
1593 return_value
= chainon (return_value
,
1594 build_tree_list (NULL_TREE
, p
));
1595 else if (ident
!= error_mark_node
)
1596 error ("cannot find protocol declaration for %qE",
1600 return return_value
;
1603 /* Create a declaration for field NAME of a given TYPE. */
1606 create_field_decl (tree type
, const char *name
)
1608 return build_decl (input_location
,
1609 FIELD_DECL
, get_identifier (name
), type
);
1612 /* Create a global, static declaration for variable NAME of a given TYPE. The
1613 finish_var_decl() routine will need to be called on it afterwards. */
1616 start_var_decl (tree type
, const char *name
)
1618 tree var
= build_decl (input_location
,
1619 VAR_DECL
, get_identifier (name
), type
);
1621 TREE_STATIC (var
) = 1;
1622 DECL_INITIAL (var
) = error_mark_node
; /* A real initializer is coming... */
1623 DECL_IGNORED_P (var
) = 1;
1624 DECL_ARTIFICIAL (var
) = 1;
1625 DECL_CONTEXT (var
) = NULL_TREE
;
1627 DECL_THIS_STATIC (var
) = 1; /* squash redeclaration errors */
1633 /* Finish off the variable declaration created by start_var_decl(). */
1636 finish_var_decl (tree var
, tree initializer
)
1638 finish_decl (var
, input_location
, initializer
, NULL_TREE
, NULL_TREE
);
1641 /* Find the decl for the constant string class reference. This is only
1642 used for the NeXT runtime. */
1645 setup_string_decl (void)
1650 /* %s in format will provide room for terminating null */
1651 length
= strlen (STRING_OBJECT_GLOBAL_FORMAT
)
1652 + strlen (constant_string_class_name
);
1653 name
= XNEWVEC (char, length
);
1654 sprintf (name
, STRING_OBJECT_GLOBAL_FORMAT
,
1655 constant_string_class_name
);
1656 constant_string_global_id
= get_identifier (name
);
1657 string_class_decl
= lookup_name (constant_string_global_id
);
1659 return string_class_decl
;
1662 /* Purpose: "play" parser, creating/installing representations
1663 of the declarations that are required by Objective-C.
1667 type_spec--------->sc_spec
1668 (tree_list) (tree_list)
1671 identifier_node identifier_node */
1674 synth_module_prologue (void)
1677 enum debug_info_type save_write_symbols
= write_symbols
;
1678 const struct gcc_debug_hooks
*const save_hooks
= debug_hooks
;
1680 /* Suppress outputting debug symbols, because
1681 dbxout_init hasn't been called yet. */
1682 write_symbols
= NO_DEBUG
;
1683 debug_hooks
= &do_nothing_debug_hooks
;
1686 push_lang_context (lang_name_c
); /* extern "C" */
1689 /* The following are also defined in <objc/objc.h> and friends. */
1691 objc_object_id
= get_identifier (TAG_OBJECT
);
1692 objc_class_id
= get_identifier (TAG_CLASS
);
1694 objc_object_reference
= xref_tag (RECORD_TYPE
, objc_object_id
);
1695 objc_class_reference
= xref_tag (RECORD_TYPE
, objc_class_id
);
1697 objc_object_type
= build_pointer_type (objc_object_reference
);
1698 objc_class_type
= build_pointer_type (objc_class_reference
);
1700 objc_object_name
= get_identifier (OBJECT_TYPEDEF_NAME
);
1701 objc_class_name
= get_identifier (CLASS_TYPEDEF_NAME
);
1703 /* Declare the 'id' and 'Class' typedefs. */
1705 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
1709 TREE_NO_WARNING (type
) = 1;
1710 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
1714 TREE_NO_WARNING (type
) = 1;
1716 /* Forward-declare '@interface Protocol'. */
1718 type
= get_identifier (PROTOCOL_OBJECT_CLASS_NAME
);
1719 objc_declare_class (tree_cons (NULL_TREE
, type
, NULL_TREE
));
1720 objc_protocol_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
1723 /* Declare type of selector-objects that represent an operation name. */
1725 if (flag_next_runtime
)
1726 /* `struct objc_selector *' */
1728 = build_pointer_type (xref_tag (RECORD_TYPE
,
1729 get_identifier (TAG_SELECTOR
)));
1731 /* `const struct objc_selector *' */
1733 = build_pointer_type
1734 (build_qualified_type (xref_tag (RECORD_TYPE
,
1735 get_identifier (TAG_SELECTOR
)),
1738 /* Declare receiver type used for dispatching messages to 'super'. */
1740 /* `struct objc_super *' */
1741 objc_super_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
1742 get_identifier (TAG_SUPER
)));
1744 /* Declare pointers to method and ivar lists. */
1745 objc_method_list_ptr
= build_pointer_type
1746 (xref_tag (RECORD_TYPE
,
1747 get_identifier (UTAG_METHOD_LIST
)));
1748 objc_method_proto_list_ptr
1749 = build_pointer_type (xref_tag (RECORD_TYPE
,
1750 get_identifier (UTAG_METHOD_PROTOTYPE_LIST
)));
1751 objc_ivar_list_ptr
= build_pointer_type
1752 (xref_tag (RECORD_TYPE
,
1753 get_identifier (UTAG_IVAR_LIST
)));
1755 /* TREE_NOTHROW is cleared for the message-sending functions,
1756 because the function that gets called can throw in Obj-C++, or
1757 could itself call something that can throw even in Obj-C. */
1759 if (flag_next_runtime
)
1761 /* NB: In order to call one of the ..._stret (struct-returning)
1762 functions, the function *MUST* first be cast to a signature that
1763 corresponds to the actual ObjC method being invoked. This is
1764 what is done by the build_objc_method_call() routine below. */
1766 /* id objc_msgSend (id, SEL, ...); */
1767 /* id objc_msgSendNonNil (id, SEL, ...); */
1768 /* id objc_msgSend_stret (id, SEL, ...); */
1769 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
1771 = build_varargs_function_type_list (objc_object_type
,
1775 umsg_decl
= add_builtin_function (TAG_MSGSEND
,
1776 type
, 0, NOT_BUILT_IN
,
1778 umsg_nonnil_decl
= add_builtin_function (TAG_MSGSEND_NONNIL
,
1779 type
, 0, NOT_BUILT_IN
,
1781 umsg_stret_decl
= add_builtin_function (TAG_MSGSEND_STRET
,
1782 type
, 0, NOT_BUILT_IN
,
1784 umsg_nonnil_stret_decl
= add_builtin_function (TAG_MSGSEND_NONNIL_STRET
,
1785 type
, 0, NOT_BUILT_IN
,
1788 /* These can throw, because the function that gets called can throw
1789 in Obj-C++, or could itself call something that can throw even
1791 TREE_NOTHROW (umsg_decl
) = 0;
1792 TREE_NOTHROW (umsg_nonnil_decl
) = 0;
1793 TREE_NOTHROW (umsg_stret_decl
) = 0;
1794 TREE_NOTHROW (umsg_nonnil_stret_decl
) = 0;
1796 /* id objc_msgSend_Fast (id, SEL, ...)
1797 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
1798 #ifdef OFFS_MSGSEND_FAST
1799 umsg_fast_decl
= add_builtin_function (TAG_MSGSEND_FAST
,
1800 type
, 0, NOT_BUILT_IN
,
1802 TREE_NOTHROW (umsg_fast_decl
) = 0;
1803 DECL_ATTRIBUTES (umsg_fast_decl
)
1804 = tree_cons (get_identifier ("hard_coded_address"),
1805 build_int_cst (NULL_TREE
, OFFS_MSGSEND_FAST
),
1808 /* No direct dispatch available. */
1809 umsg_fast_decl
= umsg_decl
;
1812 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
1813 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
1815 = build_varargs_function_type_list (objc_object_type
,
1819 umsg_super_decl
= add_builtin_function (TAG_MSGSENDSUPER
,
1820 type
, 0, NOT_BUILT_IN
,
1822 umsg_super_stret_decl
= add_builtin_function (TAG_MSGSENDSUPER_STRET
,
1823 type
, 0, NOT_BUILT_IN
, 0,
1825 TREE_NOTHROW (umsg_super_decl
) = 0;
1826 TREE_NOTHROW (umsg_super_stret_decl
) = 0;
1830 /* GNU runtime messenger entry points. */
1832 /* typedef id (*IMP)(id, SEL, ...); */
1834 build_varargs_function_type_list (objc_object_type
,
1838 tree IMP_type
= build_pointer_type (ftype
);
1840 /* IMP objc_msg_lookup (id, SEL); */
1841 type
= build_function_type_list (IMP_type
,
1845 umsg_decl
= add_builtin_function (TAG_MSGSEND
,
1846 type
, 0, NOT_BUILT_IN
,
1848 TREE_NOTHROW (umsg_decl
) = 0;
1850 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
1852 = build_function_type_list (IMP_type
,
1856 umsg_super_decl
= add_builtin_function (TAG_MSGSENDSUPER
,
1857 type
, 0, NOT_BUILT_IN
,
1859 TREE_NOTHROW (umsg_super_decl
) = 0;
1861 /* The following GNU runtime entry point is called to initialize
1864 __objc_exec_class (void *); */
1866 = build_function_type_list (void_type_node
,
1869 execclass_decl
= add_builtin_function (TAG_EXECCLASS
,
1870 type
, 0, NOT_BUILT_IN
,
1874 /* id objc_getClass (const char *); */
1876 type
= build_function_type_list (objc_object_type
,
1877 const_string_type_node
,
1881 = add_builtin_function (TAG_GETCLASS
, type
, 0, NOT_BUILT_IN
,
1884 /* id objc_getMetaClass (const char *); */
1886 objc_get_meta_class_decl
1887 = add_builtin_function (TAG_GETMETACLASS
, type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
1889 build_class_template ();
1890 build_super_template ();
1891 build_protocol_template ();
1892 build_category_template ();
1893 build_objc_exception_stuff ();
1895 if (flag_next_runtime
)
1896 build_next_objc_exception_stuff ();
1898 /* static SEL _OBJC_SELECTOR_TABLE[]; */
1900 if (! flag_next_runtime
)
1901 build_selector_table_decl ();
1903 /* Forward declare constant_string_id and constant_string_type. */
1904 if (!constant_string_class_name
)
1905 constant_string_class_name
= default_constant_string_class_name
;
1907 constant_string_id
= get_identifier (constant_string_class_name
);
1908 objc_declare_class (tree_cons (NULL_TREE
, constant_string_id
, NULL_TREE
));
1910 /* Pre-build the following entities - for speed/convenience. */
1911 self_id
= get_identifier ("self");
1912 ucmd_id
= get_identifier ("_cmd");
1915 pop_lang_context ();
1918 write_symbols
= save_write_symbols
;
1919 debug_hooks
= save_hooks
;
1922 /* Ensure that the ivar list for NSConstantString/NXConstantString
1923 (or whatever was specified via `-fconstant-string-class')
1924 contains fields at least as large as the following three, so that
1925 the runtime can stomp on them with confidence:
1927 struct STRING_OBJECT_CLASS_NAME
1931 unsigned int length;
1935 check_string_class_template (void)
1937 tree field_decl
= objc_get_class_ivars (constant_string_id
);
1939 #define AT_LEAST_AS_LARGE_AS(F, T) \
1940 (F && TREE_CODE (F) == FIELD_DECL \
1941 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
1942 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
1944 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
1947 field_decl
= DECL_CHAIN (field_decl
);
1948 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
1951 field_decl
= DECL_CHAIN (field_decl
);
1952 return AT_LEAST_AS_LARGE_AS (field_decl
, unsigned_type_node
);
1954 #undef AT_LEAST_AS_LARGE_AS
1957 /* Avoid calling `check_string_class_template ()' more than once. */
1958 static GTY(()) int string_layout_checked
;
1960 /* Construct an internal string layout to be used as a template for
1961 creating NSConstantString/NXConstantString instances. */
1964 objc_build_internal_const_str_type (void)
1966 tree type
= (*lang_hooks
.types
.make_type
) (RECORD_TYPE
);
1967 tree fields
= build_decl (input_location
,
1968 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
1969 tree field
= build_decl (input_location
,
1970 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
1972 DECL_CHAIN (field
) = fields
; fields
= field
;
1973 field
= build_decl (input_location
,
1974 FIELD_DECL
, NULL_TREE
, unsigned_type_node
);
1975 DECL_CHAIN (field
) = fields
; fields
= field
;
1976 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
1978 finish_builtin_struct (type
, "__builtin_ObjCString",
1984 /* Custom build_string which sets TREE_TYPE! */
1987 my_build_string (int len
, const char *str
)
1989 return fix_string_type (build_string (len
, str
));
1992 /* Build a string with contents STR and length LEN and convert it to a
1996 my_build_string_pointer (int len
, const char *str
)
1998 tree string
= my_build_string (len
, str
);
1999 tree ptrtype
= build_pointer_type (TREE_TYPE (TREE_TYPE (string
)));
2000 return build1 (ADDR_EXPR
, ptrtype
, string
);
2004 string_hash (const void *ptr
)
2006 const_tree
const str
= ((const struct string_descriptor
*)ptr
)->literal
;
2007 const unsigned char *p
= (const unsigned char *) TREE_STRING_POINTER (str
);
2008 int i
, len
= TREE_STRING_LENGTH (str
);
2011 for (i
= 0; i
< len
; i
++)
2012 h
= ((h
* 613) + p
[i
]);
2018 string_eq (const void *ptr1
, const void *ptr2
)
2020 const_tree
const str1
= ((const struct string_descriptor
*)ptr1
)->literal
;
2021 const_tree
const str2
= ((const struct string_descriptor
*)ptr2
)->literal
;
2022 int len1
= TREE_STRING_LENGTH (str1
);
2024 return (len1
== TREE_STRING_LENGTH (str2
)
2025 && !memcmp (TREE_STRING_POINTER (str1
), TREE_STRING_POINTER (str2
),
2029 /* Given a chain of STRING_CST's, build a static instance of
2030 NXConstantString which points at the concatenation of those
2031 strings. We place the string object in the __string_objects
2032 section of the __OBJC segment. The Objective-C runtime will
2033 initialize the isa pointers of the string objects to point at the
2034 NXConstantString class object. */
2037 objc_build_string_object (tree string
)
2039 tree constructor
, constant_string_class
;
2042 struct string_descriptor
*desc
, key
;
2045 /* Prep the string argument. */
2046 string
= fix_string_type (string
);
2047 TREE_SET_CODE (string
, STRING_CST
);
2048 length
= TREE_STRING_LENGTH (string
) - 1;
2050 /* Check whether the string class being used actually exists and has the
2051 correct ivar layout. */
2052 if (!string_layout_checked
)
2054 string_layout_checked
= -1;
2055 constant_string_class
= lookup_interface (constant_string_id
);
2056 internal_const_str_type
= objc_build_internal_const_str_type ();
2058 if (!constant_string_class
2059 || !(constant_string_type
2060 = CLASS_STATIC_TEMPLATE (constant_string_class
)))
2061 error ("cannot find interface declaration for %qE",
2062 constant_string_id
);
2063 /* The NSConstantString/NXConstantString ivar layout is now known. */
2064 else if (!check_string_class_template ())
2065 error ("interface %qE does not have valid constant string layout",
2066 constant_string_id
);
2067 /* For the NeXT runtime, we can generate a literal reference
2068 to the string class, don't need to run a constructor. */
2069 else if (flag_next_runtime
&& !setup_string_decl ())
2070 error ("cannot find reference tag for class %qE",
2071 constant_string_id
);
2074 string_layout_checked
= 1; /* Success! */
2075 add_class_reference (constant_string_id
);
2079 if (string_layout_checked
== -1)
2080 return error_mark_node
;
2082 /* Perhaps we already constructed a constant string just like this one? */
2083 key
.literal
= string
;
2084 loc
= htab_find_slot (string_htab
, &key
, INSERT
);
2085 desc
= (struct string_descriptor
*) *loc
;
2090 VEC(constructor_elt
,gc
) *v
= NULL
;
2091 *loc
= desc
= ggc_alloc_string_descriptor ();
2092 desc
->literal
= string
;
2094 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
2095 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
2096 fields
= TYPE_FIELDS (internal_const_str_type
);
2097 CONSTRUCTOR_APPEND_ELT (v
, fields
,
2099 ? build_unary_op (input_location
,
2100 ADDR_EXPR
, string_class_decl
, 0)
2101 : build_int_cst (NULL_TREE
, 0));
2102 fields
= DECL_CHAIN (fields
);
2103 CONSTRUCTOR_APPEND_ELT (v
, fields
,
2104 build_unary_op (input_location
,
2105 ADDR_EXPR
, string
, 1));
2106 fields
= DECL_CHAIN (fields
);
2107 CONSTRUCTOR_APPEND_ELT (v
, fields
, build_int_cst (NULL_TREE
, length
));
2108 constructor
= objc_build_constructor (internal_const_str_type
, v
);
2110 if (!flag_next_runtime
)
2112 = objc_add_static_instance (constructor
, constant_string_type
);
2115 var
= build_decl (input_location
,
2116 CONST_DECL
, NULL
, TREE_TYPE (constructor
));
2117 DECL_INITIAL (var
) = constructor
;
2118 TREE_STATIC (var
) = 1;
2119 pushdecl_top_level (var
);
2122 desc
->constructor
= constructor
;
2125 addr
= convert (build_pointer_type (constant_string_type
),
2126 build_unary_op (input_location
,
2127 ADDR_EXPR
, desc
->constructor
, 1));
2132 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2134 static GTY(()) int num_static_inst
;
2137 objc_add_static_instance (tree constructor
, tree class_decl
)
2142 /* Find the list of static instances for the CLASS_DECL. Create one if
2144 for (chain
= &objc_static_instances
;
2145 *chain
&& TREE_VALUE (*chain
) != class_decl
;
2146 chain
= &TREE_CHAIN (*chain
));
2149 *chain
= tree_cons (NULL_TREE
, class_decl
, NULL_TREE
);
2150 add_objc_string (OBJC_TYPE_NAME (class_decl
), class_names
);
2153 sprintf (buf
, "_OBJC_INSTANCE_%d", num_static_inst
++);
2154 decl
= build_decl (input_location
,
2155 VAR_DECL
, get_identifier (buf
), class_decl
);
2156 TREE_STATIC (decl
) = 1;
2157 DECL_ARTIFICIAL (decl
) = 1;
2158 TREE_USED (decl
) = 1;
2159 DECL_INITIAL (decl
) = constructor
;
2161 /* We may be writing something else just now.
2162 Postpone till end of input. */
2163 DECL_DEFER_OUTPUT (decl
) = 1;
2164 pushdecl_top_level (decl
);
2165 rest_of_decl_compilation (decl
, 1, 0);
2167 /* Add the DECL to the head of this CLASS' list. */
2168 TREE_PURPOSE (*chain
) = tree_cons (NULL_TREE
, decl
, TREE_PURPOSE (*chain
));
2173 /* Build a static constant CONSTRUCTOR
2174 with type TYPE and elements ELTS. */
2177 objc_build_constructor (tree type
, VEC(constructor_elt
,gc
) *elts
)
2179 tree constructor
= build_constructor (type
, elts
);
2181 TREE_CONSTANT (constructor
) = 1;
2182 TREE_STATIC (constructor
) = 1;
2183 TREE_READONLY (constructor
) = 1;
2186 /* Adjust for impedance mismatch. We should figure out how to build
2187 CONSTRUCTORs that consistently please both the C and C++ gods. */
2188 if (!VEC_index (constructor_elt
, elts
, 0)->index
)
2189 TREE_TYPE (constructor
) = init_list_type_node
;
2195 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2197 /* Predefine the following data type:
2205 void *defs[cls_def_cnt + cat_def_cnt];
2209 build_objc_symtab_template (void)
2211 tree fields
, *chain
= NULL
;
2213 objc_symtab_template
= objc_start_struct (get_identifier (UTAG_SYMTAB
));
2215 /* long sel_ref_cnt; */
2216 fields
= add_field_decl (long_integer_type_node
, "sel_ref_cnt", &chain
);
2219 add_field_decl (build_pointer_type (objc_selector_type
), "refs", &chain
);
2221 /* short cls_def_cnt; */
2222 add_field_decl (short_integer_type_node
, "cls_def_cnt", &chain
);
2224 /* short cat_def_cnt; */
2225 add_field_decl (short_integer_type_node
, "cat_def_cnt", &chain
);
2227 if (imp_count
|| cat_count
|| !flag_next_runtime
)
2229 /* void *defs[imp_count + cat_count (+ 1)]; */
2230 /* NB: The index is one less than the size of the array. */
2231 int index
= imp_count
+ cat_count
+ (flag_next_runtime
? -1: 0);
2232 tree array_type
= build_sized_array_type (ptr_type_node
, index
+ 1);
2233 add_field_decl (array_type
, "defs", &chain
);
2236 objc_finish_struct (objc_symtab_template
, fields
);
2239 /* Create the initial value for the `defs' field of _objc_symtab.
2240 This is a CONSTRUCTOR. */
2243 init_def_list (tree type
)
2246 struct imp_entry
*impent
;
2247 VEC(constructor_elt
,gc
) *v
= NULL
;
2250 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2252 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
2254 expr
= build_unary_op (input_location
,
2255 ADDR_EXPR
, impent
->class_decl
, 0);
2256 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2261 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2263 if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
2265 expr
= build_unary_op (input_location
,
2266 ADDR_EXPR
, impent
->class_decl
, 0);
2267 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2271 if (!flag_next_runtime
)
2273 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2274 if (static_instances_decl
)
2275 expr
= build_unary_op (input_location
,
2276 ADDR_EXPR
, static_instances_decl
, 0);
2278 expr
= integer_zero_node
;
2280 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2283 return objc_build_constructor (type
, v
);
2286 /* Construct the initial value for all of _objc_symtab. */
2289 init_objc_symtab (tree type
)
2291 VEC(constructor_elt
,gc
) *v
= NULL
;
2293 /* sel_ref_cnt = { ..., 5, ... } */
2295 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2296 build_int_cst (long_integer_type_node
, 0));
2298 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2300 if (flag_next_runtime
|| ! sel_ref_chain
)
2301 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, convert (
2302 build_pointer_type (objc_selector_type
),
2303 integer_zero_node
));
2306 tree expr
= build_unary_op (input_location
, ADDR_EXPR
,
2307 UOBJC_SELECTOR_TABLE_decl
, 1);
2309 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2310 convert (build_pointer_type (objc_selector_type
),
2314 /* cls_def_cnt = { ..., 5, ... } */
2316 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2317 build_int_cst (short_integer_type_node
, imp_count
));
2319 /* cat_def_cnt = { ..., 5, ... } */
2321 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2322 build_int_cst (short_integer_type_node
, cat_count
));
2324 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2326 if (imp_count
|| cat_count
|| !flag_next_runtime
)
2329 tree field
= TYPE_FIELDS (type
);
2330 field
= DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field
))));
2332 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init_def_list (TREE_TYPE (field
)));
2335 return objc_build_constructor (type
, v
);
2338 /* Generate forward declarations for metadata such as
2339 'OBJC_CLASS_...'. */
2342 build_metadata_decl (const char *name
, tree type
)
2346 /* struct TYPE NAME_<name>; */
2347 decl
= start_var_decl (type
, synth_id_with_class_suffix
2349 objc_implementation_context
));
2354 /* Push forward-declarations of all the categories so that
2355 init_def_list can use them in a CONSTRUCTOR. */
2358 forward_declare_categories (void)
2360 struct imp_entry
*impent
;
2361 tree sav
= objc_implementation_context
;
2363 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2365 if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
2367 /* Set an invisible arg to synth_id_with_class_suffix. */
2368 objc_implementation_context
= impent
->imp_context
;
2369 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2370 impent
->class_decl
= build_metadata_decl ("_OBJC_CATEGORY",
2371 objc_category_template
);
2374 objc_implementation_context
= sav
;
2377 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2378 and initialized appropriately. */
2381 generate_objc_symtab_decl (void)
2384 build_objc_symtab_template ();
2385 UOBJC_SYMBOLS_decl
= start_var_decl (objc_symtab_template
, "_OBJC_SYMBOLS");
2386 finish_var_decl (UOBJC_SYMBOLS_decl
,
2387 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl
)));
2391 init_module_descriptor (tree type
)
2394 VEC(constructor_elt
,gc
) *v
= NULL
;
2396 /* version = { 1, ... } */
2398 expr
= build_int_cst (long_integer_type_node
, OBJC_VERSION
);
2399 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2401 /* size = { ..., sizeof (struct _objc_module), ... } */
2403 expr
= convert (long_integer_type_node
,
2404 size_in_bytes (objc_module_template
));
2405 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2407 /* Don't provide any file name for security reasons. */
2408 /* name = { ..., "", ... } */
2410 expr
= add_objc_string (get_identifier (""), class_names
);
2411 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2413 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2415 if (UOBJC_SYMBOLS_decl
)
2416 expr
= build_unary_op (input_location
,
2417 ADDR_EXPR
, UOBJC_SYMBOLS_decl
, 0);
2419 expr
= null_pointer_node
;
2420 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2422 return objc_build_constructor (type
, v
);
2425 /* Write out the data structures to describe Objective C classes defined.
2427 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2430 build_module_descriptor (void)
2432 tree decls
, *chain
= NULL
;
2435 push_lang_context (lang_name_c
); /* extern "C" */
2438 objc_module_template
= objc_start_struct (get_identifier (UTAG_MODULE
));
2441 decls
= add_field_decl (long_integer_type_node
, "version", &chain
);
2444 add_field_decl (long_integer_type_node
, "size", &chain
);
2447 add_field_decl (string_type_node
, "name", &chain
);
2449 /* struct _objc_symtab *symtab; */
2450 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE
,
2451 get_identifier (UTAG_SYMTAB
))),
2454 objc_finish_struct (objc_module_template
, decls
);
2456 /* Create an instance of "_objc_module". */
2457 UOBJC_MODULES_decl
= start_var_decl (objc_module_template
, "_OBJC_MODULES");
2458 /* This is the root of the metadata for defined classes and categories, it
2459 is referenced by the runtime and, therefore, needed. */
2460 DECL_PRESERVE_P (UOBJC_MODULES_decl
) = 1;
2461 finish_var_decl (UOBJC_MODULES_decl
,
2462 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl
)));
2465 pop_lang_context ();
2469 /* The GNU runtime requires us to provide a static initializer function
2472 static void __objc_gnu_init (void) {
2473 __objc_exec_class (&L_OBJC_MODULES);
2477 build_module_initializer_routine (void)
2482 push_lang_context (lang_name_c
); /* extern "C" */
2485 objc_push_parm (build_decl (input_location
,
2486 PARM_DECL
, NULL_TREE
, void_type_node
));
2488 objc_start_function (get_identifier (TAG_GNUINIT
),
2489 build_function_type_list (void_type_node
, NULL_TREE
),
2490 NULL_TREE
, NULL_TREE
);
2492 objc_start_function (get_identifier (TAG_GNUINIT
),
2493 build_function_type_list (void_type_node
, NULL_TREE
),
2494 NULL_TREE
, objc_get_parm_info (0));
2496 body
= c_begin_compound_stmt (true);
2497 add_stmt (build_function_call
2502 build_unary_op (input_location
, ADDR_EXPR
,
2503 UOBJC_MODULES_decl
, 0))));
2504 add_stmt (c_end_compound_stmt (input_location
, body
, true));
2506 TREE_PUBLIC (current_function_decl
) = 0;
2509 /* For Objective-C++, we will need to call __objc_gnu_init
2510 from objc_generate_static_init_call() below. */
2511 DECL_STATIC_CONSTRUCTOR (current_function_decl
) = 1;
2514 GNU_INIT_decl
= current_function_decl
;
2518 pop_lang_context ();
2523 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
2524 to be called by the module initializer routine. */
2527 objc_static_init_needed_p (void)
2529 return (GNU_INIT_decl
!= NULL_TREE
);
2532 /* Generate a call to the __objc_gnu_init initializer function. */
2535 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED
)
2537 add_stmt (build_stmt (input_location
, EXPR_STMT
,
2538 build_function_call (input_location
,
2539 GNU_INIT_decl
, NULL_TREE
)));
2543 #endif /* OBJCPLUS */
2545 /* Return the DECL of the string IDENT in the SECTION. */
2548 get_objc_string_decl (tree ident
, enum string_section section
)
2552 if (section
== class_names
)
2553 chain
= class_names_chain
;
2554 else if (section
== meth_var_names
)
2555 chain
= meth_var_names_chain
;
2556 else if (section
== meth_var_types
)
2557 chain
= meth_var_types_chain
;
2561 for (; chain
!= 0; chain
= TREE_CHAIN (chain
))
2562 if (TREE_VALUE (chain
) == ident
)
2563 return (TREE_PURPOSE (chain
));
2569 /* Output references to all statically allocated objects. Return the DECL
2570 for the array built. */
2573 generate_static_references (void)
2575 tree expr
= NULL_TREE
;
2576 tree class_name
, klass
, decl
;
2577 tree cl_chain
, in_chain
, type
2578 = build_array_type (build_pointer_type (void_type_node
), NULL_TREE
);
2579 int num_inst
, num_class
;
2581 VEC(constructor_elt
,gc
) *decls
= NULL
;
2583 if (flag_next_runtime
)
2586 for (cl_chain
= objc_static_instances
, num_class
= 0;
2587 cl_chain
; cl_chain
= TREE_CHAIN (cl_chain
), num_class
++)
2589 VEC(constructor_elt
,gc
) *v
= NULL
;
2591 for (num_inst
= 0, in_chain
= TREE_PURPOSE (cl_chain
);
2592 in_chain
; num_inst
++, in_chain
= TREE_CHAIN (in_chain
));
2594 sprintf (buf
, "_OBJC_STATIC_INSTANCES_%d", num_class
);
2595 decl
= start_var_decl (type
, buf
);
2597 /* Output {class_name, ...}. */
2598 klass
= TREE_VALUE (cl_chain
);
2599 class_name
= get_objc_string_decl (OBJC_TYPE_NAME (klass
), class_names
);
2600 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2601 build_unary_op (input_location
,
2602 ADDR_EXPR
, class_name
, 1));
2604 /* Output {..., instance, ...}. */
2605 for (in_chain
= TREE_PURPOSE (cl_chain
);
2606 in_chain
; in_chain
= TREE_CHAIN (in_chain
))
2608 expr
= build_unary_op (input_location
,
2609 ADDR_EXPR
, TREE_VALUE (in_chain
), 1);
2610 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2613 /* Output {..., NULL}. */
2614 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
2616 expr
= objc_build_constructor (TREE_TYPE (decl
), v
);
2617 finish_var_decl (decl
, expr
);
2618 CONSTRUCTOR_APPEND_ELT (decls
, NULL_TREE
,
2619 build_unary_op (input_location
,
2620 ADDR_EXPR
, decl
, 1));
2623 CONSTRUCTOR_APPEND_ELT (decls
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
2624 expr
= objc_build_constructor (type
, decls
);
2625 static_instances_decl
= start_var_decl (type
, "_OBJC_STATIC_INSTANCES");
2626 finish_var_decl (static_instances_decl
, expr
);
2629 static GTY(()) int selector_reference_idx
;
2632 build_selector_reference_decl (void)
2637 sprintf (buf
, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx
++);
2638 decl
= start_var_decl (objc_selector_type
, buf
);
2644 build_selector_table_decl (void)
2648 if (flag_typed_selectors
)
2650 build_selector_template ();
2651 temp
= build_array_type (objc_selector_template
, NULL_TREE
);
2654 temp
= build_array_type (objc_selector_type
, NULL_TREE
);
2656 UOBJC_SELECTOR_TABLE_decl
= start_var_decl (temp
, "_OBJC_SELECTOR_TABLE");
2659 /* Just a handy wrapper for add_objc_string. */
2662 build_selector (tree ident
)
2664 return convert (objc_selector_type
,
2665 add_objc_string (ident
, meth_var_names
));
2668 /* Used only by build_*_selector_translation_table (). */
2670 diagnose_missing_method (tree meth
, location_t here
)
2674 for (method_chain
= meth_var_names_chain
;
2676 method_chain
= TREE_CHAIN (method_chain
))
2678 if (TREE_VALUE (method_chain
) == meth
)
2686 warning_at (here
, 0, "creating selector for nonexistent method %qE",
2691 build_next_selector_translation_table (void)
2694 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
2697 tree decl
= TREE_PURPOSE (chain
);
2698 if (warn_selector
&& objc_implementation_context
)
2702 loc
= DECL_SOURCE_LOCATION (decl
);
2704 loc
= input_location
;
2705 diagnose_missing_method (TREE_VALUE (chain
), loc
);
2708 expr
= build_selector (TREE_VALUE (chain
));
2712 /* Entries of this form are used for references to methods.
2713 The runtime re-writes these on start-up, but the compiler can't see
2714 that and optimizes it away unless we force it. */
2715 DECL_PRESERVE_P (decl
) = 1;
2716 finish_var_decl (decl
, expr
);
2722 build_gnu_selector_translation_table (void)
2726 tree decl = NULL_TREE;*/
2727 VEC(constructor_elt
,gc
) *inits
= NULL
;
2729 for (chain
= sel_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
2733 if (warn_selector
&& objc_implementation_context
)
2734 diagnose_missing_method (TREE_VALUE (chain
), input_location
);
2736 expr
= build_selector (TREE_VALUE (chain
));
2737 /* add one for the '\0' character
2738 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
2741 if (flag_typed_selectors
)
2743 VEC(constructor_elt
,gc
) *v
= NULL
;
2744 tree encoding
= get_proto_encoding (TREE_PURPOSE (chain
));
2745 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2746 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, encoding
);
2747 expr
= objc_build_constructor (objc_selector_template
, v
);
2750 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
2752 } /* each element in the chain */
2755 /* Cause the selector table (previously forward-declared)
2756 to be actually output. */
2759 if (flag_typed_selectors
)
2761 VEC(constructor_elt
,gc
) *v
= NULL
;
2762 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
2763 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
2764 expr
= objc_build_constructor (objc_selector_template
, v
);
2767 expr
= integer_zero_node
;
2769 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
2770 expr
= objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl
),
2772 finish_var_decl (UOBJC_SELECTOR_TABLE_decl
, expr
);
2777 get_proto_encoding (tree proto
)
2782 if (! METHOD_ENCODING (proto
))
2784 encoding
= encode_method_prototype (proto
);
2785 METHOD_ENCODING (proto
) = encoding
;
2788 encoding
= METHOD_ENCODING (proto
);
2790 return add_objc_string (encoding
, meth_var_types
);
2793 return build_int_cst (NULL_TREE
, 0);
2796 /* sel_ref_chain is a list whose "value" fields will be instances of
2797 identifier_node that represent the selector. LOC is the location of
2801 build_typed_selector_reference (location_t loc
, tree ident
, tree prototype
)
2803 tree
*chain
= &sel_ref_chain
;
2809 if (TREE_PURPOSE (*chain
) == prototype
&& TREE_VALUE (*chain
) == ident
)
2810 goto return_at_index
;
2813 chain
= &TREE_CHAIN (*chain
);
2816 *chain
= tree_cons (prototype
, ident
, NULL_TREE
);
2819 expr
= build_unary_op (loc
, ADDR_EXPR
,
2820 build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2821 build_int_cst (NULL_TREE
, index
)),
2823 return convert (objc_selector_type
, expr
);
2827 build_selector_reference (location_t loc
, tree ident
)
2829 tree
*chain
= &sel_ref_chain
;
2835 if (TREE_VALUE (*chain
) == ident
)
2836 return (flag_next_runtime
2837 ? TREE_PURPOSE (*chain
)
2838 : build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2839 build_int_cst (NULL_TREE
, index
)));
2842 chain
= &TREE_CHAIN (*chain
);
2845 expr
= (flag_next_runtime
? build_selector_reference_decl (): NULL_TREE
);
2847 *chain
= tree_cons (expr
, ident
, NULL_TREE
);
2849 return (flag_next_runtime
2851 : build_array_ref (loc
, UOBJC_SELECTOR_TABLE_decl
,
2852 build_int_cst (NULL_TREE
, index
)));
2855 static GTY(()) int class_reference_idx
;
2858 build_class_reference_decl (void)
2863 sprintf (buf
, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx
++);
2864 decl
= start_var_decl (objc_class_type
, buf
);
2869 /* Create a class reference, but don't create a variable to reference
2873 add_class_reference (tree ident
)
2877 if ((chain
= cls_ref_chain
))
2882 if (ident
== TREE_VALUE (chain
))
2886 chain
= TREE_CHAIN (chain
);
2890 /* Append to the end of the list */
2891 TREE_CHAIN (tail
) = tree_cons (NULL_TREE
, ident
, NULL_TREE
);
2894 cls_ref_chain
= tree_cons (NULL_TREE
, ident
, NULL_TREE
);
2897 /* Get a class reference, creating it if necessary. Also create the
2898 reference variable. */
2901 objc_get_class_reference (tree ident
)
2903 tree orig_ident
= (DECL_P (ident
)
2906 ? OBJC_TYPE_NAME (ident
)
2908 bool local_scope
= false;
2911 if (processing_template_decl
)
2912 /* Must wait until template instantiation time. */
2913 return build_min_nt (CLASS_REFERENCE_EXPR
, ident
);
2916 if (TREE_CODE (ident
) == TYPE_DECL
)
2917 ident
= (DECL_ORIGINAL_TYPE (ident
)
2918 ? DECL_ORIGINAL_TYPE (ident
)
2919 : TREE_TYPE (ident
));
2923 && CP_TYPE_CONTEXT (ident
) != global_namespace
)
2927 if (local_scope
|| !(ident
= objc_is_class_name (ident
)))
2929 error ("%qE is not an Objective-C class name or alias",
2931 return error_mark_node
;
2934 if (flag_next_runtime
&& !flag_zero_link
)
2939 for (chain
= &cls_ref_chain
; *chain
; chain
= &TREE_CHAIN (*chain
))
2940 if (TREE_VALUE (*chain
) == ident
)
2942 if (! TREE_PURPOSE (*chain
))
2943 TREE_PURPOSE (*chain
) = build_class_reference_decl ();
2945 return TREE_PURPOSE (*chain
);
2948 decl
= build_class_reference_decl ();
2949 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
2956 add_class_reference (ident
);
2958 params
= build_tree_list (NULL_TREE
,
2959 my_build_string_pointer
2960 (IDENTIFIER_LENGTH (ident
) + 1,
2961 IDENTIFIER_POINTER (ident
)));
2963 assemble_external (objc_get_class_decl
);
2964 return build_function_call (input_location
, objc_get_class_decl
, params
);
2968 /* For each string section we have a chain which maps identifier nodes
2969 to decls for the strings. */
2971 static GTY(()) int class_names_idx
;
2972 static GTY(()) int meth_var_names_idx
;
2973 static GTY(()) int meth_var_types_idx
;
2976 add_objc_string (tree ident
, enum string_section section
)
2978 tree
*chain
, decl
, type
, string_expr
;
2982 if (section
== class_names
)
2984 chain
= &class_names_chain
;
2985 sprintf (buf
, "_OBJC_CLASS_NAME_%d", class_names_idx
++);
2987 else if (section
== meth_var_names
)
2989 chain
= &meth_var_names_chain
;
2990 sprintf (buf
, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx
++);
2992 else if (section
== meth_var_types
)
2994 chain
= &meth_var_types_chain
;
2995 sprintf (buf
, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx
++);
3002 if (TREE_VALUE (*chain
) == ident
)
3003 return convert (string_type_node
,
3004 build_unary_op (input_location
,
3005 ADDR_EXPR
, TREE_PURPOSE (*chain
), 1));
3007 chain
= &TREE_CHAIN (*chain
);
3010 type
= build_sized_array_type (char_type_node
, IDENTIFIER_LENGTH (ident
) + 1);
3011 decl
= start_var_decl (type
, buf
);
3012 string_expr
= my_build_string (IDENTIFIER_LENGTH (ident
) + 1,
3013 IDENTIFIER_POINTER (ident
));
3014 TREE_CONSTANT (decl
) = 1;
3015 finish_var_decl (decl
, string_expr
);
3017 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
3019 return convert (string_type_node
, build_unary_op (input_location
,
3020 ADDR_EXPR
, decl
, 1));
3024 objc_declare_alias (tree alias_ident
, tree class_ident
)
3026 tree underlying_class
;
3029 if (current_namespace
!= global_namespace
) {
3030 error ("Objective-C declarations may only appear in global scope");
3032 #endif /* OBJCPLUS */
3034 if (!(underlying_class
= objc_is_class_name (class_ident
)))
3035 warning (0, "cannot find class %qE", class_ident
);
3036 else if (objc_is_class_name (alias_ident
))
3037 warning (0, "class %qE already exists", alias_ident
);
3040 /* Implement @compatibility_alias as a typedef. */
3042 push_lang_context (lang_name_c
); /* extern "C" */
3044 lang_hooks
.decls
.pushdecl (build_decl
3048 xref_tag (RECORD_TYPE
, underlying_class
)));
3050 pop_lang_context ();
3052 alias_chain
= tree_cons (underlying_class
, alias_ident
, alias_chain
);
3057 objc_declare_class (tree ident_list
)
3061 if (current_namespace
!= global_namespace
) {
3062 error ("Objective-C declarations may only appear in global scope");
3064 #endif /* OBJCPLUS */
3066 for (list
= ident_list
; list
; list
= TREE_CHAIN (list
))
3068 tree ident
= TREE_VALUE (list
);
3070 if (! objc_is_class_name (ident
))
3072 tree record
= lookup_name (ident
), type
= record
;
3076 if (TREE_CODE (record
) == TYPE_DECL
)
3077 type
= DECL_ORIGINAL_TYPE (record
);
3079 if (!TYPE_HAS_OBJC_INFO (type
)
3080 || !TYPE_OBJC_INTERFACE (type
))
3082 error ("%qE redeclared as different kind of symbol",
3084 error ("previous declaration of %q+D",
3089 record
= xref_tag (RECORD_TYPE
, ident
);
3090 INIT_TYPE_OBJC_INFO (record
);
3091 TYPE_OBJC_INTERFACE (record
) = ident
;
3092 class_chain
= tree_cons (NULL_TREE
, ident
, class_chain
);
3098 objc_is_class_name (tree ident
)
3102 if (ident
&& TREE_CODE (ident
) == IDENTIFIER_NODE
3103 && identifier_global_value (ident
))
3104 ident
= identifier_global_value (ident
);
3105 while (ident
&& TREE_CODE (ident
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (ident
))
3106 ident
= OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident
));
3108 if (ident
&& TREE_CODE (ident
) == RECORD_TYPE
)
3109 ident
= OBJC_TYPE_NAME (ident
);
3111 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3112 ident
= DECL_NAME (ident
);
3114 if (!ident
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3117 if (lookup_interface (ident
))
3120 for (chain
= class_chain
; chain
; chain
= TREE_CHAIN (chain
))
3122 if (ident
== TREE_VALUE (chain
))
3126 for (chain
= alias_chain
; chain
; chain
= TREE_CHAIN (chain
))
3128 if (ident
== TREE_VALUE (chain
))
3129 return TREE_PURPOSE (chain
);
3135 /* Check whether TYPE is either 'id' or 'Class'. */
3138 objc_is_id (tree type
)
3140 if (type
&& TREE_CODE (type
) == IDENTIFIER_NODE
3141 && identifier_global_value (type
))
3142 type
= identifier_global_value (type
);
3144 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
3145 type
= TREE_TYPE (type
);
3147 /* NB: This function may be called before the ObjC front-end has
3148 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3149 return (objc_object_type
&& type
3150 && (IS_ID (type
) || IS_CLASS (type
) || IS_SUPER (type
))
3155 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3156 class instance. This is needed by other parts of the compiler to
3157 handle ObjC types gracefully. */
3160 objc_is_object_ptr (tree type
)
3164 type
= TYPE_MAIN_VARIANT (type
);
3165 if (!POINTER_TYPE_P (type
))
3168 ret
= objc_is_id (type
);
3170 ret
= objc_is_class_name (TREE_TYPE (type
));
3176 objc_is_gcable_type (tree type
, int or_strong_p
)
3182 if (objc_is_id (TYPE_MAIN_VARIANT (type
)))
3184 if (or_strong_p
&& lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type
)))
3186 if (TREE_CODE (type
) != POINTER_TYPE
&& TREE_CODE (type
) != INDIRECT_REF
)
3188 type
= TREE_TYPE (type
);
3189 if (TREE_CODE (type
) != RECORD_TYPE
)
3191 name
= TYPE_NAME (type
);
3192 return (objc_is_class_name (name
) != NULL_TREE
);
3196 objc_substitute_decl (tree expr
, tree oldexpr
, tree newexpr
)
3198 if (expr
== oldexpr
)
3201 switch (TREE_CODE (expr
))
3204 return objc_build_component_ref
3205 (objc_substitute_decl (TREE_OPERAND (expr
, 0),
3208 DECL_NAME (TREE_OPERAND (expr
, 1)));
3210 return build_array_ref (input_location
,
3211 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3214 TREE_OPERAND (expr
, 1));
3216 return build_indirect_ref (input_location
,
3217 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3219 newexpr
), RO_ARROW
);
3226 objc_build_ivar_assignment (tree outervar
, tree lhs
, tree rhs
)
3229 /* The LHS parameter contains the expression 'outervar->memberspec';
3230 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3231 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3234 = objc_substitute_decl
3235 (lhs
, outervar
, convert (TREE_TYPE (outervar
), integer_zero_node
));
3237 = (flag_objc_direct_dispatch
3238 ? objc_assign_ivar_fast_decl
3239 : objc_assign_ivar_decl
);
3241 offs
= convert (integer_type_node
, build_unary_op (input_location
,
3242 ADDR_EXPR
, offs
, 0));
3244 func_params
= tree_cons (NULL_TREE
,
3245 convert (objc_object_type
, rhs
),
3246 tree_cons (NULL_TREE
, convert (objc_object_type
, outervar
),
3247 tree_cons (NULL_TREE
, offs
,
3250 assemble_external (func
);
3251 return build_function_call (input_location
, func
, func_params
);
3255 objc_build_global_assignment (tree lhs
, tree rhs
)
3257 tree func_params
= tree_cons (NULL_TREE
,
3258 convert (objc_object_type
, rhs
),
3259 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3260 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3263 assemble_external (objc_assign_global_decl
);
3264 return build_function_call (input_location
,
3265 objc_assign_global_decl
, func_params
);
3269 objc_build_strong_cast_assignment (tree lhs
, tree rhs
)
3271 tree func_params
= tree_cons (NULL_TREE
,
3272 convert (objc_object_type
, rhs
),
3273 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3274 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3277 assemble_external (objc_assign_strong_cast_decl
);
3278 return build_function_call (input_location
,
3279 objc_assign_strong_cast_decl
, func_params
);
3283 objc_is_gcable_p (tree expr
)
3285 return (TREE_CODE (expr
) == COMPONENT_REF
3286 ? objc_is_gcable_p (TREE_OPERAND (expr
, 1))
3287 : TREE_CODE (expr
) == ARRAY_REF
3288 ? (objc_is_gcable_p (TREE_TYPE (expr
))
3289 || objc_is_gcable_p (TREE_OPERAND (expr
, 0)))
3290 : TREE_CODE (expr
) == ARRAY_TYPE
3291 ? objc_is_gcable_p (TREE_TYPE (expr
))
3293 ? objc_is_gcable_type (expr
, 1)
3294 : (objc_is_gcable_p (TREE_TYPE (expr
))
3296 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr
)))));
3300 objc_is_ivar_reference_p (tree expr
)
3302 return (TREE_CODE (expr
) == ARRAY_REF
3303 ? objc_is_ivar_reference_p (TREE_OPERAND (expr
, 0))
3304 : TREE_CODE (expr
) == COMPONENT_REF
3305 ? TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
3310 objc_is_global_reference_p (tree expr
)
3312 return (TREE_CODE (expr
) == INDIRECT_REF
|| TREE_CODE (expr
) == PLUS_EXPR
3313 ? objc_is_global_reference_p (TREE_OPERAND (expr
, 0))
3315 ? (DECL_FILE_SCOPE_P (expr
) || TREE_STATIC (expr
))
3320 objc_generate_write_barrier (tree lhs
, enum tree_code modifycode
, tree rhs
)
3322 tree result
= NULL_TREE
, outer
;
3323 int strong_cast_p
= 0, outer_gc_p
= 0, indirect_p
= 0;
3325 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3326 will have been transformed to the form '*(type *)&expr'. */
3327 if (TREE_CODE (lhs
) == INDIRECT_REF
)
3329 outer
= TREE_OPERAND (lhs
, 0);
3331 while (!strong_cast_p
3332 && (CONVERT_EXPR_P (outer
)
3333 || TREE_CODE (outer
) == NON_LVALUE_EXPR
))
3335 tree lhstype
= TREE_TYPE (outer
);
3337 /* Descend down the cast chain, and record the first objc_gc
3339 if (POINTER_TYPE_P (lhstype
))
3342 = lookup_attribute ("objc_gc",
3343 TYPE_ATTRIBUTES (TREE_TYPE (lhstype
)));
3349 outer
= TREE_OPERAND (outer
, 0);
3353 /* If we have a __strong cast, it trumps all else. */
3356 if (modifycode
!= NOP_EXPR
)
3357 goto invalid_pointer_arithmetic
;
3359 if (warn_assign_intercept
)
3360 warning (0, "strong-cast assignment has been intercepted");
3362 result
= objc_build_strong_cast_assignment (lhs
, rhs
);
3367 /* the lhs must be of a suitable type, regardless of its underlying
3369 if (!objc_is_gcable_p (lhs
))
3375 && (TREE_CODE (outer
) == COMPONENT_REF
3376 || TREE_CODE (outer
) == ARRAY_REF
))
3377 outer
= TREE_OPERAND (outer
, 0);
3379 if (TREE_CODE (outer
) == INDIRECT_REF
)
3381 outer
= TREE_OPERAND (outer
, 0);
3385 outer_gc_p
= objc_is_gcable_p (outer
);
3387 /* Handle ivar assignments. */
3388 if (objc_is_ivar_reference_p (lhs
))
3390 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3391 doesn't cut it here), the best we can do here is suggest a cast. */
3392 if (!objc_is_gcable_type (TREE_TYPE (outer
), 0))
3394 /* We may still be able to use the global write barrier... */
3395 if (!indirect_p
&& objc_is_global_reference_p (outer
))
3396 goto global_reference
;
3399 if (modifycode
== NOP_EXPR
)
3401 if (warn_assign_intercept
)
3402 warning (0, "strong-cast may possibly be needed");
3408 if (modifycode
!= NOP_EXPR
)
3409 goto invalid_pointer_arithmetic
;
3411 if (warn_assign_intercept
)
3412 warning (0, "instance variable assignment has been intercepted");
3414 result
= objc_build_ivar_assignment (outer
, lhs
, rhs
);
3419 /* Likewise, intercept assignment to global/static variables if their type is
3421 if (objc_is_global_reference_p (outer
))
3427 if (modifycode
!= NOP_EXPR
)
3429 invalid_pointer_arithmetic
:
3431 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3436 if (warn_assign_intercept
)
3437 warning (0, "global/static variable assignment has been intercepted");
3439 result
= objc_build_global_assignment (lhs
, rhs
);
3442 /* In all other cases, fall back to the normal mechanism. */
3447 struct GTY(()) interface_tuple
{
3452 static GTY ((param_is (struct interface_tuple
))) htab_t interface_htab
;
3455 hash_interface (const void *p
)
3457 const struct interface_tuple
*d
= (const struct interface_tuple
*) p
;
3458 return IDENTIFIER_HASH_VALUE (d
->id
);
3462 eq_interface (const void *p1
, const void *p2
)
3464 const struct interface_tuple
*d
= (const struct interface_tuple
*) p1
;
3469 lookup_interface (tree ident
)
3472 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3473 ident
= DECL_NAME (ident
);
3476 if (ident
== NULL_TREE
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3480 struct interface_tuple
**slot
;
3485 slot
= (struct interface_tuple
**)
3486 htab_find_slot_with_hash (interface_htab
, ident
,
3487 IDENTIFIER_HASH_VALUE (ident
),
3490 i
= (*slot
)->class_name
;
3496 /* Implement @defs (<classname>) within struct bodies. */
3499 objc_get_class_ivars (tree class_name
)
3501 tree interface
= lookup_interface (class_name
);
3504 return get_class_ivars (interface
, true);
3506 error ("cannot find interface declaration for %qE",
3509 return error_mark_node
;
3512 /* Called when checking the variables in a struct. If we are not
3513 doing the ivars list inside an @interface context, then returns
3514 fieldlist unchanged. Else, returns the list of class ivars.
3517 objc_get_interface_ivars (tree fieldlist
)
3519 if (!objc_collecting_ivars
|| !objc_interface_context
3520 || TREE_CODE (objc_interface_context
) != CLASS_INTERFACE_TYPE
3521 || CLASS_SUPER_NAME (objc_interface_context
) == NULL_TREE
)
3524 return get_class_ivars (objc_interface_context
, true);
3527 /* Used by: build_private_template, continue_class,
3528 and for @defs constructs. */
3531 get_class_ivars (tree interface
, bool inherited
)
3533 tree ivar_chain
= copy_list (CLASS_RAW_IVARS (interface
));
3535 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
3536 by the current class (i.e., they do not include super-class ivars).
3537 However, the CLASS_IVARS list will be side-effected by a call to
3538 finish_struct(), which will fill in field offsets. */
3539 if (!CLASS_IVARS (interface
))
3540 CLASS_IVARS (interface
) = ivar_chain
;
3545 while (CLASS_SUPER_NAME (interface
))
3547 /* Prepend super-class ivars. */
3548 interface
= lookup_interface (CLASS_SUPER_NAME (interface
));
3549 ivar_chain
= chainon (copy_list (CLASS_RAW_IVARS (interface
)),
3557 objc_create_temporary_var (tree type
)
3561 decl
= build_decl (input_location
,
3562 VAR_DECL
, NULL_TREE
, type
);
3563 TREE_USED (decl
) = 1;
3564 DECL_ARTIFICIAL (decl
) = 1;
3565 DECL_IGNORED_P (decl
) = 1;
3566 DECL_CONTEXT (decl
) = current_function_decl
;
3571 /* Exception handling constructs. We begin by having the parser do most
3572 of the work and passing us blocks. What we do next depends on whether
3573 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
3574 We abstract all of this in a handful of appropriately named routines. */
3576 /* Stack of open try blocks. */
3578 struct objc_try_context
3580 struct objc_try_context
*outer
;
3582 /* Statements (or statement lists) as processed by the parser. */
3586 /* Some file position locations. */
3587 location_t try_locus
;
3588 location_t end_try_locus
;
3589 location_t end_catch_locus
;
3590 location_t finally_locus
;
3591 location_t end_finally_locus
;
3593 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
3594 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
3597 /* The CATCH_EXPR of an open @catch clause. */
3600 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
3606 static struct objc_try_context
*cur_try_context
;
3608 static GTY(()) tree objc_eh_personality_decl
;
3610 /* This hook, called via lang_eh_runtime_type, generates a runtime object
3611 that represents TYPE. For Objective-C, this is just the class name. */
3612 /* ??? Isn't there a class object or some such? Is it easy to get? */
3616 objc_eh_runtime_type (tree type
)
3618 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type
)), class_names
);
3622 objc_eh_personality (void)
3624 if (!flag_objc_sjlj_exceptions
3625 && !objc_eh_personality_decl
)
3626 objc_eh_personality_decl
3627 = build_personality_function (USING_SJLJ_EXCEPTIONS
3628 ? "__gnu_objc_personality_sj0"
3629 : "__gnu_objc_personality_v0");
3631 return objc_eh_personality_decl
;
3635 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
3636 of Darwin, we'll arrange for it to be initialized (and associated
3637 with a binding) later. */
3640 objc_build_exc_ptr (void)
3642 if (flag_objc_sjlj_exceptions
)
3644 tree var
= cur_try_context
->caught_decl
;
3647 var
= objc_create_temporary_var (objc_object_type
);
3648 cur_try_context
->caught_decl
= var
;
3655 t
= built_in_decls
[BUILT_IN_EH_POINTER
];
3656 t
= build_call_expr (t
, 1, integer_zero_node
);
3657 return fold_convert (objc_object_type
, t
);
3661 /* Build "objc_exception_try_exit(&_stack)". */
3664 next_sjlj_build_try_exit (void)
3667 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3668 t
= tree_cons (NULL
, t
, NULL
);
3669 t
= build_function_call (input_location
,
3670 objc_exception_try_exit_decl
, t
);
3675 objc_exception_try_enter (&_stack);
3676 if (_setjmp(&_stack.buf))
3680 Return the COND_EXPR. Note that the THEN and ELSE fields are left
3681 empty, ready for the caller to fill them in. */
3684 next_sjlj_build_enter_and_setjmp (void)
3686 tree t
, enter
, sj
, cond
;
3688 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3689 t
= tree_cons (NULL
, t
, NULL
);
3690 enter
= build_function_call (input_location
,
3691 objc_exception_try_enter_decl
, t
);
3693 t
= objc_build_component_ref (cur_try_context
->stack_decl
,
3694 get_identifier ("buf"));
3695 t
= build_fold_addr_expr_loc (input_location
, t
);
3697 /* Convert _setjmp argument to type that is expected. */
3698 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl
)))
3699 t
= convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl
))), t
);
3701 t
= convert (ptr_type_node
, t
);
3703 t
= convert (ptr_type_node
, t
);
3705 t
= tree_cons (NULL
, t
, NULL
);
3706 sj
= build_function_call (input_location
,
3707 objc_setjmp_decl
, t
);
3709 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (sj
), enter
, sj
);
3710 cond
= c_common_truthvalue_conversion (input_location
, cond
);
3712 return build3 (COND_EXPR
, void_type_node
, cond
, NULL
, NULL
);
3717 DECL = objc_exception_extract(&_stack); */
3720 next_sjlj_build_exc_extract (tree decl
)
3724 t
= build_fold_addr_expr_loc (input_location
, cur_try_context
->stack_decl
);
3725 t
= tree_cons (NULL
, t
, NULL
);
3726 t
= build_function_call (input_location
,
3727 objc_exception_extract_decl
, t
);
3728 t
= convert (TREE_TYPE (decl
), t
);
3729 t
= build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
3735 if (objc_exception_match(obj_get_class(TYPE), _caught)
3742 objc_exception_try_exit(&_stack);
3744 from the sequence of CATCH_EXPRs in the current try context. */
3747 next_sjlj_build_catch_list (void)
3749 tree_stmt_iterator i
= tsi_start (cur_try_context
->catch_list
);
3751 tree
*last
= &catch_seq
;
3752 bool saw_id
= false;
3754 for (; !tsi_end_p (i
); tsi_next (&i
))
3756 tree stmt
= tsi_stmt (i
);
3757 tree type
= CATCH_TYPES (stmt
);
3758 tree body
= CATCH_BODY (stmt
);
3770 if (type
== error_mark_node
)
3771 cond
= error_mark_node
;
3774 args
= tree_cons (NULL
, cur_try_context
->caught_decl
, NULL
);
3775 t
= objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type
)));
3776 args
= tree_cons (NULL
, t
, args
);
3777 t
= build_function_call (input_location
,
3778 objc_exception_match_decl
, args
);
3779 cond
= c_common_truthvalue_conversion (input_location
, t
);
3781 t
= build3 (COND_EXPR
, void_type_node
, cond
, body
, NULL
);
3782 SET_EXPR_LOCATION (t
, EXPR_LOCATION (stmt
));
3785 last
= &COND_EXPR_ELSE (t
);
3791 t
= build2 (MODIFY_EXPR
, void_type_node
, cur_try_context
->rethrow_decl
,
3792 cur_try_context
->caught_decl
);
3793 SET_EXPR_LOCATION (t
, cur_try_context
->end_catch_locus
);
3794 append_to_statement_list (t
, last
);
3796 t
= next_sjlj_build_try_exit ();
3797 SET_EXPR_LOCATION (t
, cur_try_context
->end_catch_locus
);
3798 append_to_statement_list (t
, last
);
3804 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
3805 exception handling. We aim to build:
3808 struct _objc_exception_data _stack;
3812 objc_exception_try_enter (&_stack);
3813 if (_setjmp(&_stack.buf))
3815 id _caught = objc_exception_extract(&_stack);
3816 objc_exception_try_enter (&_stack);
3817 if (_setjmp(&_stack.buf))
3818 _rethrow = objc_exception_extract(&_stack);
3828 objc_exception_try_exit(&_stack);
3831 objc_exception_throw(_rethrow);
3835 If CATCH-LIST is empty, we can omit all of the block containing
3836 "_caught" except for the setting of _rethrow. Note the use of
3837 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
3838 but handles goto and other exits from the block. */
3841 next_sjlj_build_try_catch_finally (void)
3843 tree rethrow_decl
, stack_decl
, t
;
3844 tree catch_seq
, try_fin
, bind
;
3846 /* Create the declarations involved. */
3847 t
= xref_tag (RECORD_TYPE
, get_identifier (UTAG_EXCDATA
));
3848 stack_decl
= objc_create_temporary_var (t
);
3849 cur_try_context
->stack_decl
= stack_decl
;
3851 rethrow_decl
= objc_create_temporary_var (objc_object_type
);
3852 cur_try_context
->rethrow_decl
= rethrow_decl
;
3853 TREE_CHAIN (rethrow_decl
) = stack_decl
;
3855 /* Build the outermost variable binding level. */
3856 bind
= build3 (BIND_EXPR
, void_type_node
, rethrow_decl
, NULL
, NULL
);
3857 SET_EXPR_LOCATION (bind
, cur_try_context
->try_locus
);
3858 TREE_SIDE_EFFECTS (bind
) = 1;
3860 /* Initialize rethrow_decl. */
3861 t
= build2 (MODIFY_EXPR
, void_type_node
, rethrow_decl
,
3862 convert (objc_object_type
, null_pointer_node
));
3863 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
3864 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
3866 /* Build the outermost TRY_FINALLY_EXPR. */
3867 try_fin
= build2 (TRY_FINALLY_EXPR
, void_type_node
, NULL
, NULL
);
3868 SET_EXPR_LOCATION (try_fin
, cur_try_context
->try_locus
);
3869 TREE_SIDE_EFFECTS (try_fin
) = 1;
3870 append_to_statement_list (try_fin
, &BIND_EXPR_BODY (bind
));
3872 /* Create the complete catch sequence. */
3873 if (cur_try_context
->catch_list
)
3875 tree caught_decl
= objc_build_exc_ptr ();
3876 catch_seq
= build_stmt (input_location
, BIND_EXPR
, caught_decl
, NULL
, NULL
);
3877 TREE_SIDE_EFFECTS (catch_seq
) = 1;
3879 t
= next_sjlj_build_exc_extract (caught_decl
);
3880 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
3882 t
= next_sjlj_build_enter_and_setjmp ();
3883 COND_EXPR_THEN (t
) = next_sjlj_build_exc_extract (rethrow_decl
);
3884 COND_EXPR_ELSE (t
) = next_sjlj_build_catch_list ();
3885 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
3888 catch_seq
= next_sjlj_build_exc_extract (rethrow_decl
);
3889 SET_EXPR_LOCATION (catch_seq
, cur_try_context
->end_try_locus
);
3891 /* Build the main register-and-try if statement. */
3892 t
= next_sjlj_build_enter_and_setjmp ();
3893 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
3894 COND_EXPR_THEN (t
) = catch_seq
;
3895 COND_EXPR_ELSE (t
) = cur_try_context
->try_body
;
3896 TREE_OPERAND (try_fin
, 0) = t
;
3898 /* Build the complete FINALLY statement list. */
3899 t
= next_sjlj_build_try_exit ();
3900 t
= build_stmt (input_location
, COND_EXPR
,
3901 c_common_truthvalue_conversion
3902 (input_location
, rethrow_decl
),
3904 SET_EXPR_LOCATION (t
, cur_try_context
->finally_locus
);
3905 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
3907 append_to_statement_list (cur_try_context
->finally_body
,
3908 &TREE_OPERAND (try_fin
, 1));
3910 t
= tree_cons (NULL
, rethrow_decl
, NULL
);
3911 t
= build_function_call (input_location
,
3912 objc_exception_throw_decl
, t
);
3913 t
= build_stmt (input_location
, COND_EXPR
,
3914 c_common_truthvalue_conversion (input_location
,
3917 SET_EXPR_LOCATION (t
, cur_try_context
->end_finally_locus
);
3918 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
3923 /* Called just after parsing the @try and its associated BODY. We now
3924 must prepare for the tricky bits -- handling the catches and finally. */
3927 objc_begin_try_stmt (location_t try_locus
, tree body
)
3929 struct objc_try_context
*c
= XCNEW (struct objc_try_context
);
3930 c
->outer
= cur_try_context
;
3932 c
->try_locus
= try_locus
;
3933 c
->end_try_locus
= input_location
;
3934 cur_try_context
= c
;
3936 /* -fobjc-exceptions is required to enable Objective-C exceptions.
3937 For example, on Darwin, ObjC exceptions require a sufficiently
3938 recent version of the runtime, so the user must ask for them
3939 explicitly. On other platforms, at the moment -fobjc-exceptions
3940 triggers -fexceptions which again is required for exceptions to
3943 if (!flag_objc_exceptions
)
3945 error_at (try_locus
, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
3948 if (flag_objc_sjlj_exceptions
)
3949 objc_mark_locals_volatile (NULL
);
3952 /* Called just after parsing "@catch (parm)". Open a binding level,
3953 enter DECL into the binding level, and initialize it. Leave the
3954 binding level open while the body of the compound statement is parsed. */
3957 objc_begin_catch_clause (tree decl
)
3959 tree compound
, type
, t
;
3961 /* Begin a new scope that the entire catch clause will live in. */
3962 compound
= c_begin_compound_stmt (true);
3964 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
3965 decl
= build_decl (input_location
,
3966 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
3967 lang_hooks
.decls
.pushdecl (decl
);
3969 /* Since a decl is required here by syntax, don't warn if its unused. */
3970 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
3971 be what the previous objc implementation did. */
3972 TREE_USED (decl
) = 1;
3973 DECL_READ_P (decl
) = 1;
3975 /* Verify that the type of the catch is valid. It must be a pointer
3976 to an Objective-C class, or "id" (which is catch-all). */
3977 type
= TREE_TYPE (decl
);
3979 if (POINTER_TYPE_P (type
) && objc_is_object_id (TREE_TYPE (type
)))
3981 else if (!POINTER_TYPE_P (type
) || !TYPED_OBJECT (TREE_TYPE (type
)))
3983 error ("@catch parameter is not a known Objective-C class type");
3984 type
= error_mark_node
;
3986 else if (cur_try_context
->catch_list
)
3988 /* Examine previous @catch clauses and see if we've already
3989 caught the type in question. */
3990 tree_stmt_iterator i
= tsi_start (cur_try_context
->catch_list
);
3991 for (; !tsi_end_p (i
); tsi_next (&i
))
3993 tree stmt
= tsi_stmt (i
);
3994 t
= CATCH_TYPES (stmt
);
3995 if (t
== error_mark_node
)
3997 if (!t
|| DERIVED_FROM_P (TREE_TYPE (t
), TREE_TYPE (type
)))
3999 warning (0, "exception of type %<%T%> will be caught",
4001 warning_at (EXPR_LOCATION (stmt
), 0, " by earlier handler for %<%T%>",
4002 TREE_TYPE (t
? t
: objc_object_type
));
4008 /* Record the data for the catch in the try context so that we can
4009 finalize it later. */
4010 t
= build_stmt (input_location
, CATCH_EXPR
, type
, compound
);
4011 cur_try_context
->current_catch
= t
;
4013 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
4014 t
= objc_build_exc_ptr ();
4015 t
= convert (TREE_TYPE (decl
), t
);
4016 t
= build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
4020 /* Called just after parsing the closing brace of a @catch clause. Close
4021 the open binding level, and record a CATCH_EXPR for it. */
4024 objc_finish_catch_clause (void)
4026 tree c
= cur_try_context
->current_catch
;
4027 cur_try_context
->current_catch
= NULL
;
4028 cur_try_context
->end_catch_locus
= input_location
;
4030 CATCH_BODY (c
) = c_end_compound_stmt (input_location
, CATCH_BODY (c
), 1);
4031 append_to_statement_list (c
, &cur_try_context
->catch_list
);
4034 /* Called after parsing a @finally clause and its associated BODY.
4035 Record the body for later placement. */
4038 objc_build_finally_clause (location_t finally_locus
, tree body
)
4040 cur_try_context
->finally_body
= body
;
4041 cur_try_context
->finally_locus
= finally_locus
;
4042 cur_try_context
->end_finally_locus
= input_location
;
4045 /* Called to finalize a @try construct. */
4048 objc_finish_try_stmt (void)
4050 struct objc_try_context
*c
= cur_try_context
;
4053 if (c
->catch_list
== NULL
&& c
->finally_body
== NULL
)
4054 error ("%<@try%> without %<@catch%> or %<@finally%>");
4056 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
4057 if (flag_objc_sjlj_exceptions
)
4059 bool save
= in_late_binary_op
;
4060 in_late_binary_op
= true;
4061 if (!cur_try_context
->finally_body
)
4063 cur_try_context
->finally_locus
= input_location
;
4064 cur_try_context
->end_finally_locus
= input_location
;
4066 stmt
= next_sjlj_build_try_catch_finally ();
4067 in_late_binary_op
= save
;
4071 /* Otherwise, nest the CATCH inside a FINALLY. */
4075 stmt
= build_stmt (input_location
, TRY_CATCH_EXPR
, stmt
, c
->catch_list
);
4076 SET_EXPR_LOCATION (stmt
, cur_try_context
->try_locus
);
4078 if (c
->finally_body
)
4080 stmt
= build_stmt (input_location
, TRY_FINALLY_EXPR
, stmt
, c
->finally_body
);
4081 SET_EXPR_LOCATION (stmt
, cur_try_context
->try_locus
);
4086 cur_try_context
= c
->outer
;
4092 objc_build_throw_stmt (location_t loc
, tree throw_expr
)
4096 if (!flag_objc_exceptions
)
4098 error_at (loc
, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4101 if (throw_expr
== NULL
)
4103 /* If we're not inside a @catch block, there is no "current
4104 exception" to be rethrown. */
4105 if (cur_try_context
== NULL
4106 || cur_try_context
->current_catch
== NULL
)
4108 error_at (loc
, "%<@throw%> (rethrow) used outside of a @catch block");
4112 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4113 value that we get from the runtime. */
4114 throw_expr
= objc_build_exc_ptr ();
4117 /* A throw is just a call to the runtime throw function with the
4118 object as a parameter. */
4119 args
= tree_cons (NULL
, throw_expr
, NULL
);
4120 return add_stmt (build_function_call (loc
,
4121 objc_exception_throw_decl
, args
));
4125 objc_build_synchronized (location_t start_locus
, tree mutex
, tree body
)
4129 /* First lock the mutex. */
4130 mutex
= save_expr (mutex
);
4131 args
= tree_cons (NULL
, mutex
, NULL
);
4132 call
= build_function_call (input_location
,
4133 objc_sync_enter_decl
, args
);
4134 SET_EXPR_LOCATION (call
, start_locus
);
4137 /* Build the mutex unlock. */
4138 args
= tree_cons (NULL
, mutex
, NULL
);
4139 call
= build_function_call (input_location
,
4140 objc_sync_exit_decl
, args
);
4141 SET_EXPR_LOCATION (call
, input_location
);
4143 /* Put the that and the body in a TRY_FINALLY. */
4144 objc_begin_try_stmt (start_locus
, body
);
4145 objc_build_finally_clause (input_location
, call
);
4146 return objc_finish_try_stmt ();
4150 /* Predefine the following data type:
4152 struct _objc_exception_data
4154 int buf[OBJC_JBLEN];
4158 /* The following yuckiness should prevent users from having to #include
4159 <setjmp.h> in their code... */
4161 /* Define to a harmless positive value so the below code doesn't die. */
4163 #define OBJC_JBLEN 18
4167 build_next_objc_exception_stuff (void)
4169 tree decls
, temp_type
, *chain
= NULL
;
4171 objc_exception_data_template
4172 = objc_start_struct (get_identifier (UTAG_EXCDATA
));
4174 /* int buf[OBJC_JBLEN]; */
4176 temp_type
= build_sized_array_type (integer_type_node
, OBJC_JBLEN
);
4177 decls
= add_field_decl (temp_type
, "buf", &chain
);
4179 /* void *pointers[4]; */
4181 temp_type
= build_sized_array_type (ptr_type_node
, 4);
4182 add_field_decl (temp_type
, "pointers", &chain
);
4184 objc_finish_struct (objc_exception_data_template
, decls
);
4186 /* int _setjmp(...); */
4187 /* If the user includes <setjmp.h>, this shall be superseded by
4188 'int _setjmp(jmp_buf);' */
4189 temp_type
= build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
4191 = add_builtin_function (TAG_SETJMP
, temp_type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
4193 /* id objc_exception_extract(struct _objc_exception_data *); */
4195 = build_function_type_list (objc_object_type
,
4196 build_pointer_type (objc_exception_data_template
),
4198 objc_exception_extract_decl
4199 = add_builtin_function (TAG_EXCEPTIONEXTRACT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4201 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4202 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4204 = build_function_type_list (void_type_node
,
4205 build_pointer_type (objc_exception_data_template
),
4207 objc_exception_try_enter_decl
4208 = add_builtin_function (TAG_EXCEPTIONTRYENTER
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4210 objc_exception_try_exit_decl
4211 = add_builtin_function (TAG_EXCEPTIONTRYEXIT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4214 /* int objc_exception_match(id, id); */
4216 = build_function_type_list (integer_type_node
,
4217 objc_object_type
, objc_object_type
, NULL_TREE
);
4218 objc_exception_match_decl
4219 = add_builtin_function (TAG_EXCEPTIONMATCH
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4222 /* id objc_assign_ivar (id, id, unsigned int); */
4223 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4224 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4226 = build_function_type_list (objc_object_type
,
4231 objc_assign_ivar_decl
4232 = add_builtin_function (TAG_ASSIGNIVAR
, temp_type
, 0, NOT_BUILT_IN
,
4234 #ifdef OFFS_ASSIGNIVAR_FAST
4235 objc_assign_ivar_fast_decl
4236 = add_builtin_function (TAG_ASSIGNIVAR_FAST
, temp_type
, 0,
4237 NOT_BUILT_IN
, NULL
, NULL_TREE
);
4238 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl
)
4239 = tree_cons (get_identifier ("hard_coded_address"),
4240 build_int_cst (NULL_TREE
, OFFS_ASSIGNIVAR_FAST
),
4243 /* Default to slower ivar method. */
4244 objc_assign_ivar_fast_decl
= objc_assign_ivar_decl
;
4247 /* id objc_assign_global (id, id *); */
4248 /* id objc_assign_strongCast (id, id *); */
4249 temp_type
= build_function_type_list (objc_object_type
,
4251 build_pointer_type (objc_object_type
),
4253 objc_assign_global_decl
4254 = add_builtin_function (TAG_ASSIGNGLOBAL
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4256 objc_assign_strong_cast_decl
4257 = add_builtin_function (TAG_ASSIGNSTRONGCAST
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4262 build_objc_exception_stuff (void)
4264 tree noreturn_list
, nothrow_list
, temp_type
;
4266 noreturn_list
= tree_cons (get_identifier ("noreturn"), NULL
, NULL
);
4267 nothrow_list
= tree_cons (get_identifier ("nothrow"), NULL
, NULL
);
4269 /* void objc_exception_throw(id) __attribute__((noreturn)); */
4270 /* void objc_sync_enter(id); */
4271 /* void objc_sync_exit(id); */
4272 temp_type
= build_function_type_list (void_type_node
,
4275 objc_exception_throw_decl
4276 = add_builtin_function (TAG_EXCEPTIONTHROW
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
4278 objc_sync_enter_decl
4279 = add_builtin_function (TAG_SYNCENTER
, temp_type
, 0, NOT_BUILT_IN
,
4280 NULL
, nothrow_list
);
4282 = add_builtin_function (TAG_SYNCEXIT
, temp_type
, 0, NOT_BUILT_IN
,
4283 NULL
, nothrow_list
);
4286 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4289 struct <classname> {
4290 struct _objc_class *isa;
4295 build_private_template (tree klass
)
4297 if (!CLASS_STATIC_TEMPLATE (klass
))
4299 tree record
= objc_build_struct (klass
,
4300 get_class_ivars (klass
, false),
4301 CLASS_SUPER_NAME (klass
));
4303 /* Set the TREE_USED bit for this struct, so that stab generator
4304 can emit stabs for this struct type. */
4305 if (flag_debug_only_used_symbols
&& TYPE_STUB_DECL (record
))
4306 TREE_USED (TYPE_STUB_DECL (record
)) = 1;
4310 /* Begin code generation for protocols... */
4312 /* struct _objc_protocol {
4313 struct _objc_class *isa;
4314 char *protocol_name;
4315 struct _objc_protocol **protocol_list;
4316 struct _objc__method_prototype_list *instance_methods;
4317 struct _objc__method_prototype_list *class_methods;
4321 build_protocol_template (void)
4323 tree ptype
, decls
, *chain
= NULL
;
4325 objc_protocol_template
= objc_start_struct (get_identifier (UTAG_PROTOCOL
));
4327 /* struct _objc_class *isa; */
4328 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
4329 get_identifier (UTAG_CLASS
)));
4330 decls
= add_field_decl (ptype
, "isa", &chain
);
4332 /* char *protocol_name; */
4333 add_field_decl (string_type_node
, "protocol_name", &chain
);
4335 /* struct _objc_protocol **protocol_list; */
4336 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
4337 add_field_decl (ptype
, "protocol_list", &chain
);
4339 /* struct _objc__method_prototype_list *instance_methods; */
4340 add_field_decl (objc_method_proto_list_ptr
, "instance_methods", &chain
);
4342 /* struct _objc__method_prototype_list *class_methods; */
4343 add_field_decl (objc_method_proto_list_ptr
, "class_methods", &chain
);
4345 objc_finish_struct (objc_protocol_template
, decls
);
4349 build_descriptor_table_initializer (tree type
, tree entries
)
4351 VEC(constructor_elt
,gc
) *inits
= NULL
;
4355 VEC(constructor_elt
,gc
) *elts
= NULL
;
4357 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
4358 build_selector (METHOD_SEL_NAME (entries
)));
4359 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
,
4360 add_objc_string (METHOD_ENCODING (entries
),
4363 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
4364 objc_build_constructor (type
, elts
));
4366 entries
= DECL_CHAIN (entries
);
4370 return objc_build_constructor (build_array_type (type
, 0), inits
);
4373 /* struct objc_method_prototype_list {
4375 struct objc_method_prototype {
4382 build_method_prototype_list_template (tree list_type
, int size
)
4384 tree objc_ivar_list_record
;
4385 tree array_type
, decls
, *chain
= NULL
;
4387 /* Generate an unnamed struct definition. */
4389 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
4391 /* int method_count; */
4392 decls
= add_field_decl (integer_type_node
, "method_count", &chain
);
4394 /* struct objc_method method_list[]; */
4395 array_type
= build_sized_array_type (list_type
, size
);
4396 add_field_decl (array_type
, "method_list", &chain
);
4398 objc_finish_struct (objc_ivar_list_record
, decls
);
4400 return objc_ivar_list_record
;
4404 build_method_prototype_template (void)
4407 tree decls
, *chain
= NULL
;
4409 proto_record
= objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE
));
4412 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
4414 /* char *method_types; */
4415 add_field_decl (string_type_node
, "method_types", &chain
);
4417 objc_finish_struct (proto_record
, decls
);
4419 return proto_record
;
4423 objc_method_parm_type (tree type
)
4425 type
= TREE_VALUE (TREE_TYPE (type
));
4426 if (TREE_CODE (type
) == TYPE_DECL
)
4427 type
= TREE_TYPE (type
);
4432 objc_encoded_type_size (tree type
)
4434 int sz
= int_size_in_bytes (type
);
4436 /* Make all integer and enum types at least as large
4438 if (sz
> 0 && INTEGRAL_TYPE_P (type
))
4439 sz
= MAX (sz
, int_size_in_bytes (integer_type_node
));
4440 /* Treat arrays as pointers, since that's how they're
4442 else if (TREE_CODE (type
) == ARRAY_TYPE
)
4443 sz
= int_size_in_bytes (ptr_type_node
);
4447 /* Encode a method prototype.
4449 The format is described in gcc/doc/objc.texi, section 'Method
4453 encode_method_prototype (tree method_decl
)
4460 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
4461 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl
)));
4463 /* Encode return type. */
4464 encode_type (objc_method_parm_type (method_decl
),
4465 obstack_object_size (&util_obstack
),
4466 OBJC_ENCODE_INLINE_DEFS
);
4469 /* The first two arguments (self and _cmd) are pointers; account for
4471 i
= int_size_in_bytes (ptr_type_node
);
4472 parm_offset
= 2 * i
;
4473 for (parms
= METHOD_SEL_ARGS (method_decl
); parms
;
4474 parms
= DECL_CHAIN (parms
))
4476 tree type
= objc_method_parm_type (parms
);
4477 int sz
= objc_encoded_type_size (type
);
4479 /* If a type size is not known, bail out. */
4482 error ("type %q+D does not have a known size",
4484 /* Pretend that the encoding succeeded; the compilation will
4485 fail nevertheless. */
4486 goto finish_encoding
;
4491 sprintf (buf
, "%d@0:%d", parm_offset
, i
);
4492 obstack_grow (&util_obstack
, buf
, strlen (buf
));
4494 /* Argument types. */
4495 parm_offset
= 2 * i
;
4496 for (parms
= METHOD_SEL_ARGS (method_decl
); parms
;
4497 parms
= DECL_CHAIN (parms
))
4499 tree type
= objc_method_parm_type (parms
);
4501 /* Process argument qualifiers for user supplied arguments. */
4502 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms
)));
4505 encode_type (type
, obstack_object_size (&util_obstack
),
4506 OBJC_ENCODE_INLINE_DEFS
);
4508 /* Compute offset. */
4509 sprintf (buf
, "%d", parm_offset
);
4510 parm_offset
+= objc_encoded_type_size (type
);
4512 obstack_grow (&util_obstack
, buf
, strlen (buf
));
4516 obstack_1grow (&util_obstack
, '\0');
4517 result
= get_identifier (XOBFINISH (&util_obstack
, char *));
4518 obstack_free (&util_obstack
, util_firstobj
);
4523 generate_descriptor_table (tree type
, const char *name
, int size
, tree list
,
4527 VEC(constructor_elt
,gc
) *v
= NULL
;
4529 decl
= start_var_decl (type
, synth_id_with_class_suffix (name
, proto
));
4531 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
4532 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, list
);
4534 finish_var_decl (decl
, objc_build_constructor (type
, v
));
4540 generate_method_descriptors (tree protocol
)
4542 tree initlist
, chain
, method_list_template
;
4545 if (!objc_method_prototype_template
)
4546 objc_method_prototype_template
= build_method_prototype_template ();
4548 chain
= PROTOCOL_CLS_METHODS (protocol
);
4551 size
= list_length (chain
);
4553 method_list_template
4554 = build_method_prototype_list_template (objc_method_prototype_template
,
4558 = build_descriptor_table_initializer (objc_method_prototype_template
,
4561 UOBJC_CLASS_METHODS_decl
4562 = generate_descriptor_table (method_list_template
,
4563 "_OBJC_PROTOCOL_CLASS_METHODS",
4564 size
, initlist
, protocol
);
4567 UOBJC_CLASS_METHODS_decl
= 0;
4569 chain
= PROTOCOL_NST_METHODS (protocol
);
4572 size
= list_length (chain
);
4574 method_list_template
4575 = build_method_prototype_list_template (objc_method_prototype_template
,
4578 = build_descriptor_table_initializer (objc_method_prototype_template
,
4581 UOBJC_INSTANCE_METHODS_decl
4582 = generate_descriptor_table (method_list_template
,
4583 "_OBJC_PROTOCOL_INSTANCE_METHODS",
4584 size
, initlist
, protocol
);
4587 UOBJC_INSTANCE_METHODS_decl
= 0;
4591 generate_protocol_references (tree plist
)
4595 /* Forward declare protocols referenced. */
4596 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
4598 tree proto
= TREE_VALUE (lproto
);
4600 if (TREE_CODE (proto
) == PROTOCOL_INTERFACE_TYPE
4601 && PROTOCOL_NAME (proto
))
4603 if (! PROTOCOL_FORWARD_DECL (proto
))
4604 build_protocol_reference (proto
);
4606 if (PROTOCOL_LIST (proto
))
4607 generate_protocol_references (PROTOCOL_LIST (proto
));
4612 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4616 objc_generate_cxx_ctor_or_dtor (bool dtor
)
4618 tree fn
, body
, compound_stmt
, ivar
;
4620 /* - (id) .cxx_construct { ... return self; } */
4621 /* - (void) .cxx_construct { ... } */
4623 objc_set_method_type (MINUS_EXPR
);
4624 objc_start_method_definition
4625 (objc_build_method_signature (build_tree_list (NULL_TREE
,
4628 : objc_object_type
),
4629 get_identifier (dtor
4631 : TAG_CXX_CONSTRUCT
),
4632 make_node (TREE_LIST
),
4634 body
= begin_function_body ();
4635 compound_stmt
= begin_compound_stmt (0);
4637 ivar
= CLASS_IVARS (implementation_template
);
4638 /* Destroy ivars in reverse order. */
4640 ivar
= nreverse (copy_list (ivar
));
4642 for (; ivar
; ivar
= TREE_CHAIN (ivar
))
4644 if (TREE_CODE (ivar
) == FIELD_DECL
)
4646 tree type
= TREE_TYPE (ivar
);
4648 /* Call the ivar's default constructor or destructor. Do not
4649 call the destructor unless a corresponding constructor call
4650 has also been made (or is not needed). */
4651 if (MAYBE_CLASS_TYPE_P (type
)
4653 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4654 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4655 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4656 : (TYPE_NEEDS_CONSTRUCTING (type
)
4657 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))))
4659 (build_special_member_call
4660 (build_ivar_reference (DECL_NAME (ivar
)),
4661 dtor
? complete_dtor_identifier
: complete_ctor_identifier
,
4662 NULL
, type
, LOOKUP_NORMAL
, tf_warning_or_error
));
4666 /* The constructor returns 'self'. */
4668 finish_return_stmt (self_decl
);
4670 finish_compound_stmt (compound_stmt
);
4671 finish_function_body (body
);
4672 fn
= current_function_decl
;
4674 objc_finish_method_definition (fn
);
4677 /* The following routine will examine the current @interface for any
4678 non-POD C++ ivars requiring non-trivial construction and/or
4679 destruction, and then synthesize special '- .cxx_construct' and/or
4680 '- .cxx_destruct' methods which will run the appropriate
4681 construction or destruction code. Note that ivars inherited from
4682 super-classes are _not_ considered. */
4684 objc_generate_cxx_cdtors (void)
4686 bool need_ctor
= false, need_dtor
= false;
4689 /* We do not want to do this for categories, since they do not have
4692 if (TREE_CODE (objc_implementation_context
) != CLASS_IMPLEMENTATION_TYPE
)
4695 /* First, determine if we even need a constructor and/or destructor. */
4697 for (ivar
= CLASS_IVARS (implementation_template
); ivar
;
4698 ivar
= TREE_CHAIN (ivar
))
4700 if (TREE_CODE (ivar
) == FIELD_DECL
)
4702 tree type
= TREE_TYPE (ivar
);
4704 if (MAYBE_CLASS_TYPE_P (type
))
4706 if (TYPE_NEEDS_CONSTRUCTING (type
)
4707 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
4708 /* NB: If a default constructor is not available, we will not
4709 be able to initialize this ivar; the add_instance_variable()
4710 routine will already have warned about this. */
4713 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4714 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4715 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4716 /* NB: If a default constructor is not available, we will not
4717 call the destructor either, for symmetry. */
4723 /* Generate '- .cxx_construct' if needed. */
4726 objc_generate_cxx_ctor_or_dtor (false);
4728 /* Generate '- .cxx_destruct' if needed. */
4731 objc_generate_cxx_ctor_or_dtor (true);
4733 /* The 'imp_list' variable points at an imp_entry record for the current
4734 @implementation. Record the existence of '- .cxx_construct' and/or
4735 '- .cxx_destruct' methods therein; it will be included in the
4736 metadata for the class. */
4737 if (flag_next_runtime
)
4738 imp_list
->has_cxx_cdtors
= (need_ctor
|| need_dtor
);
4742 /* For each protocol which was referenced either from a @protocol()
4743 expression, or because a class/category implements it (then a
4744 pointer to the protocol is stored in the struct describing the
4745 class/category), we create a statically allocated instance of the
4746 Protocol class. The code is written in such a way as to generate
4747 as few Protocol objects as possible; we generate a unique Protocol
4748 instance for each protocol, and we don't generate a Protocol
4749 instance if the protocol is never referenced (either from a
4750 @protocol() or from a class/category implementation). These
4751 statically allocated objects can be referred to via the static
4752 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
4754 The statically allocated Protocol objects that we generate here
4755 need to be fixed up at runtime in order to be used: the 'isa'
4756 pointer of the objects need to be set up to point to the 'Protocol'
4757 class, as known at runtime.
4759 The NeXT runtime fixes up all protocols at program startup time,
4760 before main() is entered. It uses a low-level trick to look up all
4761 those symbols, then loops on them and fixes them up.
4763 The GNU runtime as well fixes up all protocols before user code
4764 from the module is executed; it requires pointers to those symbols
4765 to be put in the objc_symtab (which is then passed as argument to
4766 the function __objc_exec_class() which the compiler sets up to be
4767 executed automatically when the module is loaded); setup of those
4768 Protocol objects happen in two ways in the GNU runtime: all
4769 Protocol objects referred to by a class or category implementation
4770 are fixed up when the class/category is loaded; all Protocol
4771 objects referred to by a @protocol() expression are added by the
4772 compiler to the list of statically allocated instances to fixup
4773 (the same list holding the statically allocated constant string
4774 objects). Because, as explained above, the compiler generates as
4775 few Protocol objects as possible, some Protocol object might end up
4776 being referenced multiple times when compiled with the GNU runtime,
4777 and end up being fixed up multiple times at runtime initialization.
4778 But that doesn't hurt, it's just a little inefficient. */
4781 generate_protocols (void)
4785 tree initlist
, protocol_name_expr
, refs_decl
, refs_expr
;
4787 /* If a protocol was directly referenced, pull in indirect references. */
4788 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
4789 if (PROTOCOL_FORWARD_DECL (p
) && PROTOCOL_LIST (p
))
4790 generate_protocol_references (PROTOCOL_LIST (p
));
4792 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
4794 tree nst_methods
= PROTOCOL_NST_METHODS (p
);
4795 tree cls_methods
= PROTOCOL_CLS_METHODS (p
);
4797 /* If protocol wasn't referenced, don't generate any code. */
4798 decl
= PROTOCOL_FORWARD_DECL (p
);
4803 /* Make sure we link in the Protocol class. */
4804 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME
));
4808 if (! METHOD_ENCODING (nst_methods
))
4810 encoding
= encode_method_prototype (nst_methods
);
4811 METHOD_ENCODING (nst_methods
) = encoding
;
4813 nst_methods
= DECL_CHAIN (nst_methods
);
4818 if (! METHOD_ENCODING (cls_methods
))
4820 encoding
= encode_method_prototype (cls_methods
);
4821 METHOD_ENCODING (cls_methods
) = encoding
;
4824 cls_methods
= DECL_CHAIN (cls_methods
);
4826 generate_method_descriptors (p
);
4828 if (PROTOCOL_LIST (p
))
4829 refs_decl
= generate_protocol_list (p
);
4833 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
4834 protocol_name_expr
= add_objc_string (PROTOCOL_NAME (p
), class_names
);
4837 refs_expr
= convert (build_pointer_type (build_pointer_type
4838 (objc_protocol_template
)),
4839 build_unary_op (input_location
,
4840 ADDR_EXPR
, refs_decl
, 0));
4842 refs_expr
= build_int_cst (NULL_TREE
, 0);
4844 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
4845 by generate_method_descriptors, which is called above. */
4846 initlist
= build_protocol_initializer (TREE_TYPE (decl
),
4847 protocol_name_expr
, refs_expr
,
4848 UOBJC_INSTANCE_METHODS_decl
,
4849 UOBJC_CLASS_METHODS_decl
);
4850 finish_var_decl (decl
, initlist
);
4855 build_protocol_initializer (tree type
, tree protocol_name
,
4856 tree protocol_list
, tree instance_methods
,
4860 tree cast_type
= build_pointer_type
4861 (xref_tag (RECORD_TYPE
,
4862 get_identifier (UTAG_CLASS
)));
4863 VEC(constructor_elt
,gc
) *inits
= NULL
;
4865 /* Filling the "isa" in with one allows the runtime system to
4866 detect that the version change...should remove before final release. */
4868 expr
= build_int_cst (cast_type
, PROTOCOL_VERSION
);
4869 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4870 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_name
);
4871 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_list
);
4873 if (!instance_methods
)
4874 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
4877 expr
= convert (objc_method_proto_list_ptr
,
4878 build_unary_op (input_location
,
4879 ADDR_EXPR
, instance_methods
, 0));
4880 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4884 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
4887 expr
= convert (objc_method_proto_list_ptr
,
4888 build_unary_op (input_location
,
4889 ADDR_EXPR
, class_methods
, 0));
4890 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
4893 return objc_build_constructor (type
, inits
);
4896 /* struct _objc_category {
4897 char *category_name;
4899 struct _objc_method_list *instance_methods;
4900 struct _objc_method_list *class_methods;
4901 struct _objc_protocol_list *protocols;
4905 build_category_template (void)
4907 tree ptype
, decls
, *chain
= NULL
;
4909 objc_category_template
= objc_start_struct (get_identifier (UTAG_CATEGORY
));
4911 /* char *category_name; */
4912 decls
= add_field_decl (string_type_node
, "category_name", &chain
);
4914 /* char *class_name; */
4915 add_field_decl (string_type_node
, "class_name", &chain
);
4917 /* struct _objc_method_list *instance_methods; */
4918 add_field_decl (objc_method_list_ptr
, "instance_methods", &chain
);
4920 /* struct _objc_method_list *class_methods; */
4921 add_field_decl (objc_method_list_ptr
, "class_methods", &chain
);
4923 /* struct _objc_protocol **protocol_list; */
4924 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
4925 add_field_decl (ptype
, "protocol_list", &chain
);
4927 objc_finish_struct (objc_category_template
, decls
);
4930 /* struct _objc_selector {
4936 build_selector_template (void)
4938 tree decls
, *chain
= NULL
;
4940 objc_selector_template
= objc_start_struct (get_identifier (UTAG_SELECTOR
));
4943 decls
= add_field_decl (objc_selector_type
, "sel_id", &chain
);
4945 /* char *sel_type; */
4946 add_field_decl (string_type_node
, "sel_type", &chain
);
4948 objc_finish_struct (objc_selector_template
, decls
);
4951 /* struct _objc_class {
4952 struct _objc_class *isa;
4953 struct _objc_class *super_class;
4958 struct _objc_ivar_list *ivars;
4959 struct _objc_method_list *methods;
4960 #ifdef __NEXT_RUNTIME__
4961 struct objc_cache *cache;
4963 struct sarray *dtable;
4964 struct _objc_class *subclass_list;
4965 struct _objc_class *sibling_class;
4967 struct _objc_protocol_list *protocols;
4968 #ifdef __NEXT_RUNTIME__
4971 void *gc_object_type;
4974 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
4975 the NeXT/Apple runtime; still, the compiler must generate them to
4976 maintain backward binary compatibility (and to allow for future
4980 build_class_template (void)
4982 tree ptype
, decls
, *chain
= NULL
;
4984 objc_class_template
= objc_start_struct (get_identifier (UTAG_CLASS
));
4986 /* struct _objc_class *isa; */
4987 decls
= add_field_decl (build_pointer_type (objc_class_template
),
4990 /* struct _objc_class *super_class; */
4991 add_field_decl (build_pointer_type (objc_class_template
),
4992 "super_class", &chain
);
4995 add_field_decl (string_type_node
, "name", &chain
);
4998 add_field_decl (long_integer_type_node
, "version", &chain
);
5001 add_field_decl (long_integer_type_node
, "info", &chain
);
5003 /* long instance_size; */
5004 add_field_decl (long_integer_type_node
, "instance_size", &chain
);
5006 /* struct _objc_ivar_list *ivars; */
5007 add_field_decl (objc_ivar_list_ptr
,"ivars", &chain
);
5009 /* struct _objc_method_list *methods; */
5010 add_field_decl (objc_method_list_ptr
, "methods", &chain
);
5012 if (flag_next_runtime
)
5014 /* struct objc_cache *cache; */
5015 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
5016 get_identifier ("objc_cache")));
5017 add_field_decl (ptype
, "cache", &chain
);
5021 /* struct sarray *dtable; */
5022 ptype
= build_pointer_type(xref_tag (RECORD_TYPE
,
5023 get_identifier ("sarray")));
5024 add_field_decl (ptype
, "dtable", &chain
);
5026 /* struct objc_class *subclass_list; */
5027 ptype
= build_pointer_type (objc_class_template
);
5028 add_field_decl (ptype
, "subclass_list", &chain
);
5030 /* struct objc_class *sibling_class; */
5031 ptype
= build_pointer_type (objc_class_template
);
5032 add_field_decl (ptype
, "sibling_class", &chain
);
5035 /* struct _objc_protocol **protocol_list; */
5036 ptype
= build_pointer_type (build_pointer_type
5037 (xref_tag (RECORD_TYPE
,
5038 get_identifier (UTAG_PROTOCOL
))));
5039 add_field_decl (ptype
, "protocol_list", &chain
);
5041 if (flag_next_runtime
)
5044 add_field_decl (build_pointer_type (void_type_node
), "sel_id", &chain
);
5047 /* void *gc_object_type; */
5048 add_field_decl (build_pointer_type (void_type_node
),
5049 "gc_object_type", &chain
);
5051 objc_finish_struct (objc_class_template
, decls
);
5054 /* Generate appropriate forward declarations for an implementation. */
5057 synth_forward_declarations (void)
5061 /* static struct objc_class _OBJC_CLASS_<my_name>; */
5062 UOBJC_CLASS_decl
= build_metadata_decl ("_OBJC_CLASS",
5063 objc_class_template
);
5065 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
5066 UOBJC_METACLASS_decl
= build_metadata_decl ("_OBJC_METACLASS",
5067 objc_class_template
);
5069 /* Pre-build the following entities - for speed/convenience. */
5071 an_id
= get_identifier ("super_class");
5072 ucls_super_ref
= objc_build_component_ref (UOBJC_CLASS_decl
, an_id
);
5073 uucls_super_ref
= objc_build_component_ref (UOBJC_METACLASS_decl
, an_id
);
5077 error_with_ivar (const char *message
, tree decl
)
5079 error_at (DECL_SOURCE_LOCATION (decl
), "%s %qs",
5080 message
, identifier_to_locale (gen_declaration (decl
)));
5085 check_ivars (tree inter
, tree imp
)
5087 tree intdecls
= CLASS_RAW_IVARS (inter
);
5088 tree impdecls
= CLASS_RAW_IVARS (imp
);
5095 if (intdecls
&& TREE_CODE (intdecls
) == TYPE_DECL
)
5096 intdecls
= TREE_CHAIN (intdecls
);
5098 if (intdecls
== 0 && impdecls
== 0)
5100 if (intdecls
== 0 || impdecls
== 0)
5102 error ("inconsistent instance variable specification");
5106 t1
= TREE_TYPE (intdecls
); t2
= TREE_TYPE (impdecls
);
5108 if (!comptypes (t1
, t2
)
5109 || !tree_int_cst_equal (DECL_INITIAL (intdecls
),
5110 DECL_INITIAL (impdecls
)))
5112 if (DECL_NAME (intdecls
) == DECL_NAME (impdecls
))
5114 error_with_ivar ("conflicting instance variable type",
5116 error_with_ivar ("previous declaration of",
5119 else /* both the type and the name don't match */
5121 error ("inconsistent instance variable specification");
5126 else if (DECL_NAME (intdecls
) != DECL_NAME (impdecls
))
5128 error_with_ivar ("conflicting instance variable name",
5130 error_with_ivar ("previous declaration of",
5134 intdecls
= DECL_CHAIN (intdecls
);
5135 impdecls
= DECL_CHAIN (impdecls
);
5139 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
5140 This needs to be done just once per compilation. */
5142 /* struct _objc_super {
5143 struct _objc_object *self;
5144 struct _objc_class *super_class;
5148 build_super_template (void)
5150 tree decls
, *chain
= NULL
;
5152 objc_super_template
= objc_start_struct (get_identifier (UTAG_SUPER
));
5154 /* struct _objc_object *self; */
5155 decls
= add_field_decl (objc_object_type
, "self", &chain
);
5157 /* struct _objc_class *super_class; */
5158 add_field_decl (build_pointer_type (objc_class_template
),
5159 "super_class", &chain
);
5161 objc_finish_struct (objc_super_template
, decls
);
5164 /* struct _objc_ivar {
5171 build_ivar_template (void)
5173 tree objc_ivar_id
, objc_ivar_record
;
5174 tree decls
, *chain
= NULL
;
5176 objc_ivar_id
= get_identifier (UTAG_IVAR
);
5177 objc_ivar_record
= objc_start_struct (objc_ivar_id
);
5179 /* char *ivar_name; */
5180 decls
= add_field_decl (string_type_node
, "ivar_name", &chain
);
5182 /* char *ivar_type; */
5183 add_field_decl (string_type_node
, "ivar_type", &chain
);
5185 /* int ivar_offset; */
5186 add_field_decl (integer_type_node
, "ivar_offset", &chain
);
5188 objc_finish_struct (objc_ivar_record
, decls
);
5190 return objc_ivar_record
;
5195 struct objc_ivar ivar_list[ivar_count];
5199 build_ivar_list_template (tree list_type
, int size
)
5201 tree objc_ivar_list_record
;
5202 tree array_type
, decls
, *chain
= NULL
;
5204 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
5206 /* int ivar_count; */
5207 decls
= add_field_decl (integer_type_node
, "ivar_count", &chain
);
5209 /* struct objc_ivar ivar_list[]; */
5210 array_type
= build_sized_array_type (list_type
, size
);
5211 add_field_decl (array_type
, "ivar_list", &chain
);
5213 objc_finish_struct (objc_ivar_list_record
, decls
);
5215 return objc_ivar_list_record
;
5219 struct _objc__method_prototype_list *method_next;
5221 struct objc_method method_list[method_count];
5225 build_method_list_template (tree list_type
, int size
)
5227 tree objc_ivar_list_record
;
5228 tree array_type
, decls
, *chain
= NULL
;
5230 objc_ivar_list_record
= objc_start_struct (NULL_TREE
);
5232 /* struct _objc__method_prototype_list *method_next; */
5233 decls
= add_field_decl (objc_method_proto_list_ptr
, "method_next", &chain
);
5235 /* int method_count; */
5236 add_field_decl (integer_type_node
, "method_count", &chain
);
5238 /* struct objc_method method_list[]; */
5239 array_type
= build_sized_array_type (list_type
, size
);
5240 add_field_decl (array_type
, "method_list", &chain
);
5242 objc_finish_struct (objc_ivar_list_record
, decls
);
5244 return objc_ivar_list_record
;
5248 build_ivar_list_initializer (tree type
, tree field_decl
)
5250 VEC(constructor_elt
,gc
) *inits
= NULL
;
5254 VEC(constructor_elt
,gc
) *ivar
= NULL
;
5258 if (DECL_NAME (field_decl
))
5259 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
,
5260 add_objc_string (DECL_NAME (field_decl
),
5263 /* Unnamed bit-field ivar (yuck). */
5264 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5267 encode_field_decl (field_decl
,
5268 obstack_object_size (&util_obstack
),
5269 OBJC_ENCODE_DONT_INLINE_DEFS
);
5271 /* Null terminate string. */
5272 obstack_1grow (&util_obstack
, 0);
5273 id
= add_objc_string (get_identifier (XOBFINISH (&util_obstack
, char *)),
5275 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, id
);
5276 obstack_free (&util_obstack
, util_firstobj
);
5279 CONSTRUCTOR_APPEND_ELT (ivar
, NULL_TREE
, byte_position (field_decl
));
5280 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
5281 objc_build_constructor (type
, ivar
));
5283 field_decl
= DECL_CHAIN (field_decl
);
5284 while (field_decl
&& TREE_CODE (field_decl
) != FIELD_DECL
);
5288 return objc_build_constructor (build_array_type (type
, 0), inits
);
5292 generate_ivars_list (tree type
, const char *name
, int size
, tree list
)
5295 VEC(constructor_elt
,gc
) *inits
= NULL
;
5297 decl
= start_var_decl (type
, synth_id_with_class_suffix
5298 (name
, objc_implementation_context
));
5300 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
5301 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, list
);
5303 finish_var_decl (decl
,
5304 objc_build_constructor (TREE_TYPE (decl
), inits
));
5309 /* Count only the fields occurring in T. */
5312 ivar_list_length (tree t
)
5316 for (; t
; t
= DECL_CHAIN (t
))
5317 if (TREE_CODE (t
) == FIELD_DECL
)
5324 generate_ivar_lists (void)
5326 tree initlist
, ivar_list_template
, chain
;
5329 generating_instance_variables
= 1;
5331 if (!objc_ivar_template
)
5332 objc_ivar_template
= build_ivar_template ();
5334 /* Only generate class variables for the root of the inheritance
5335 hierarchy since these will be the same for every class. */
5337 if (CLASS_SUPER_NAME (implementation_template
) == NULL_TREE
5338 && (chain
= TYPE_FIELDS (objc_class_template
)))
5340 size
= ivar_list_length (chain
);
5342 ivar_list_template
= build_ivar_list_template (objc_ivar_template
, size
);
5343 initlist
= build_ivar_list_initializer (objc_ivar_template
, chain
);
5345 UOBJC_CLASS_VARIABLES_decl
5346 = generate_ivars_list (ivar_list_template
, "_OBJC_CLASS_VARIABLES",
5350 UOBJC_CLASS_VARIABLES_decl
= 0;
5352 chain
= CLASS_IVARS (implementation_template
);
5355 size
= ivar_list_length (chain
);
5356 ivar_list_template
= build_ivar_list_template (objc_ivar_template
, size
);
5357 initlist
= build_ivar_list_initializer (objc_ivar_template
, chain
);
5359 UOBJC_INSTANCE_VARIABLES_decl
5360 = generate_ivars_list (ivar_list_template
, "_OBJC_INSTANCE_VARIABLES",
5364 UOBJC_INSTANCE_VARIABLES_decl
= 0;
5366 generating_instance_variables
= 0;
5370 build_dispatch_table_initializer (tree type
, tree entries
)
5372 VEC(constructor_elt
,gc
) *inits
= NULL
;
5376 VEC(constructor_elt
,gc
) *elems
= NULL
;
5379 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
5380 build_selector (METHOD_SEL_NAME (entries
)));
5382 /* Generate the method encoding if we don't have one already. */
5383 if (! METHOD_ENCODING (entries
))
5384 METHOD_ENCODING (entries
) =
5385 encode_method_prototype (entries
);
5387 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
,
5388 add_objc_string (METHOD_ENCODING (entries
),
5391 expr
= convert (ptr_type_node
,
5392 build_unary_op (input_location
, ADDR_EXPR
,
5393 METHOD_DEFINITION (entries
), 1));
5394 CONSTRUCTOR_APPEND_ELT (elems
, NULL_TREE
, expr
);
5396 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
5397 objc_build_constructor (type
, elems
));
5399 entries
= DECL_CHAIN (entries
);
5403 return objc_build_constructor (build_array_type (type
, 0), inits
);
5406 /* To accomplish method prototyping without generating all kinds of
5407 inane warnings, the definition of the dispatch table entries were
5410 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
5412 struct objc_method { SEL _cmd; ...; void *_imp; }; */
5415 build_method_template (void)
5418 tree decls
, *chain
= NULL
;
5420 _SLT_record
= objc_start_struct (get_identifier (UTAG_METHOD
));
5423 decls
= add_field_decl (objc_selector_type
, "_cmd", &chain
);
5425 /* char *method_types; */
5426 add_field_decl (string_type_node
, "method_types", &chain
);
5429 add_field_decl (build_pointer_type (void_type_node
), "_imp", &chain
);
5431 objc_finish_struct (_SLT_record
, decls
);
5438 generate_dispatch_table (tree type
, const char *name
, int size
, tree list
)
5441 VEC(constructor_elt
,gc
) *v
= NULL
;
5443 decl
= start_var_decl (type
, synth_id_with_class_suffix
5444 (name
, objc_implementation_context
));
5446 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
5447 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (integer_type_node
, size
));
5448 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, list
);
5450 finish_var_decl (decl
,
5451 objc_build_constructor (TREE_TYPE (decl
), v
));
5457 mark_referenced_methods (void)
5459 struct imp_entry
*impent
;
5462 for (impent
= imp_list
; impent
; impent
= impent
->next
)
5464 chain
= CLASS_CLS_METHODS (impent
->imp_context
);
5467 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain
)));
5468 chain
= DECL_CHAIN (chain
);
5471 chain
= CLASS_NST_METHODS (impent
->imp_context
);
5474 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain
)));
5475 chain
= DECL_CHAIN (chain
);
5481 generate_dispatch_tables (void)
5483 tree initlist
, chain
, method_list_template
;
5486 if (!objc_method_template
)
5487 objc_method_template
= build_method_template ();
5489 chain
= CLASS_CLS_METHODS (objc_implementation_context
);
5492 size
= list_length (chain
);
5494 method_list_template
5495 = build_method_list_template (objc_method_template
, size
);
5497 = build_dispatch_table_initializer (objc_method_template
, chain
);
5499 UOBJC_CLASS_METHODS_decl
5500 = generate_dispatch_table (method_list_template
,
5501 ((TREE_CODE (objc_implementation_context
)
5502 == CLASS_IMPLEMENTATION_TYPE
)
5503 ? "_OBJC_CLASS_METHODS"
5504 : "_OBJC_CATEGORY_CLASS_METHODS"),
5508 UOBJC_CLASS_METHODS_decl
= 0;
5510 chain
= CLASS_NST_METHODS (objc_implementation_context
);
5513 size
= list_length (chain
);
5515 method_list_template
5516 = build_method_list_template (objc_method_template
, size
);
5518 = build_dispatch_table_initializer (objc_method_template
, chain
);
5520 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
5521 UOBJC_INSTANCE_METHODS_decl
5522 = generate_dispatch_table (method_list_template
,
5523 "_OBJC_INSTANCE_METHODS",
5526 /* We have a category. */
5527 UOBJC_INSTANCE_METHODS_decl
5528 = generate_dispatch_table (method_list_template
,
5529 "_OBJC_CATEGORY_INSTANCE_METHODS",
5533 UOBJC_INSTANCE_METHODS_decl
= 0;
5537 generate_protocol_list (tree i_or_p
)
5539 tree array_type
, ptype
, refs_decl
, lproto
, e
, plist
;
5541 const char *ref_name
;
5542 VEC(constructor_elt
,gc
) *v
= NULL
;
5544 if (TREE_CODE (i_or_p
) == CLASS_INTERFACE_TYPE
5545 || TREE_CODE (i_or_p
) == CATEGORY_INTERFACE_TYPE
)
5546 plist
= CLASS_PROTOCOL_LIST (i_or_p
);
5547 else if (TREE_CODE (i_or_p
) == PROTOCOL_INTERFACE_TYPE
)
5548 plist
= PROTOCOL_LIST (i_or_p
);
5553 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
5554 if (TREE_CODE (TREE_VALUE (lproto
)) == PROTOCOL_INTERFACE_TYPE
5555 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto
)))
5558 /* Build initializer. */
5559 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5560 e
= build_int_cst (build_pointer_type (objc_protocol_template
), size
);
5561 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
5563 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
5565 tree pval
= TREE_VALUE (lproto
);
5567 if (TREE_CODE (pval
) == PROTOCOL_INTERFACE_TYPE
5568 && PROTOCOL_FORWARD_DECL (pval
))
5570 e
= build_unary_op (input_location
, ADDR_EXPR
,
5571 PROTOCOL_FORWARD_DECL (pval
), 0);
5572 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
5576 /* static struct objc_protocol *refs[n]; */
5578 if (TREE_CODE (i_or_p
) == PROTOCOL_INTERFACE_TYPE
)
5579 ref_name
= synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p
);
5580 else if (TREE_CODE (i_or_p
) == CLASS_INTERFACE_TYPE
)
5581 ref_name
= synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p
);
5582 else if (TREE_CODE (i_or_p
) == CATEGORY_INTERFACE_TYPE
)
5583 ref_name
= synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p
);
5587 ptype
= build_pointer_type (objc_protocol_template
);
5588 array_type
= build_sized_array_type (ptype
, size
+ 3);
5589 refs_decl
= start_var_decl (array_type
, ref_name
);
5591 finish_var_decl (refs_decl
,
5592 objc_build_constructor (TREE_TYPE (refs_decl
), v
));
5598 build_category_initializer (tree type
, tree cat_name
, tree class_name
,
5599 tree instance_methods
, tree class_methods
,
5603 VEC(constructor_elt
,gc
) *v
= NULL
;
5605 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, cat_name
);
5606 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, class_name
);
5608 if (!instance_methods
)
5609 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5612 expr
= convert (objc_method_list_ptr
,
5613 build_unary_op (input_location
, ADDR_EXPR
,
5614 instance_methods
, 0));
5615 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5618 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5621 expr
= convert (objc_method_list_ptr
,
5622 build_unary_op (input_location
, ADDR_EXPR
,
5624 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5627 /* protocol_list = */
5629 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5632 expr
= convert (build_pointer_type
5634 (objc_protocol_template
)),
5635 build_unary_op (input_location
, ADDR_EXPR
,
5637 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5640 return objc_build_constructor (type
, v
);
5643 /* struct _objc_class {
5644 struct objc_class *isa;
5645 struct objc_class *super_class;
5650 struct objc_ivar_list *ivars;
5651 struct objc_method_list *methods;
5652 if (flag_next_runtime)
5653 struct objc_cache *cache;
5655 struct sarray *dtable;
5656 struct objc_class *subclass_list;
5657 struct objc_class *sibling_class;
5659 struct objc_protocol_list *protocols;
5660 if (flag_next_runtime)
5662 void *gc_object_type;
5666 build_shared_structure_initializer (tree type
, tree isa
, tree super
,
5667 tree name
, tree size
, int status
,
5668 tree dispatch_table
, tree ivar_list
,
5672 VEC(constructor_elt
,gc
) *v
= NULL
;
5675 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, isa
);
5678 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, super
);
5681 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, default_conversion (name
));
5684 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5685 build_int_cst (long_integer_type_node
, 0));
5688 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5689 build_int_cst (long_integer_type_node
, status
));
5691 /* instance_size = */
5692 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
5693 convert (long_integer_type_node
, size
));
5695 /* objc_ivar_list = */
5697 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5700 expr
= convert (objc_ivar_list_ptr
,
5701 build_unary_op (input_location
, ADDR_EXPR
,
5703 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5706 /* objc_method_list = */
5707 if (!dispatch_table
)
5708 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5711 expr
= convert (objc_method_list_ptr
,
5712 build_unary_op (input_location
, ADDR_EXPR
,
5713 dispatch_table
, 0));
5714 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5717 if (flag_next_runtime
)
5718 /* method_cache = */
5719 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5723 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5725 /* subclass_list = */
5726 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5728 /* sibling_class = */
5729 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5732 /* protocol_list = */
5733 if (! protocol_list
)
5734 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5737 expr
= convert (build_pointer_type
5739 (objc_protocol_template
)),
5740 build_unary_op (input_location
, ADDR_EXPR
,
5742 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
5745 if (flag_next_runtime
)
5747 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5749 /* gc_object_type = NULL */
5750 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
5752 return objc_build_constructor (type
, v
);
5755 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
5758 lookup_category (tree klass
, tree cat_name
)
5760 tree category
= CLASS_CATEGORY_LIST (klass
);
5762 while (category
&& CLASS_SUPER_NAME (category
) != cat_name
)
5763 category
= CLASS_CATEGORY_LIST (category
);
5767 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
5770 generate_category (struct imp_entry
*impent
)
5772 tree initlist
, cat_name_expr
, class_name_expr
;
5773 tree protocol_decl
, category
;
5774 tree cat
= impent
->imp_context
;
5776 implementation_template
= impent
->imp_template
;
5777 UOBJC_CLASS_decl
= impent
->class_decl
;
5778 UOBJC_METACLASS_decl
= impent
->meta_decl
;
5780 add_class_reference (CLASS_NAME (cat
));
5781 cat_name_expr
= add_objc_string (CLASS_SUPER_NAME (cat
), class_names
);
5783 class_name_expr
= add_objc_string (CLASS_NAME (cat
), class_names
);
5785 category
= lookup_category (implementation_template
,
5786 CLASS_SUPER_NAME (cat
));
5788 if (category
&& CLASS_PROTOCOL_LIST (category
))
5790 generate_protocol_references (CLASS_PROTOCOL_LIST (category
));
5791 protocol_decl
= generate_protocol_list (category
);
5796 initlist
= build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl
),
5797 cat_name_expr
, class_name_expr
,
5798 UOBJC_INSTANCE_METHODS_decl
,
5799 UOBJC_CLASS_METHODS_decl
,
5801 /* Finish and initialize the forward decl. */
5802 finish_var_decl (UOBJC_CLASS_decl
, initlist
);
5805 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
5806 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5809 generate_shared_structures (struct imp_entry
*impent
)
5811 tree name_expr
, super_expr
, root_expr
;
5812 tree my_root_id
, my_super_id
;
5813 tree cast_type
, initlist
, protocol_decl
;
5816 objc_implementation_context
= impent
->imp_context
;
5817 implementation_template
= impent
->imp_template
;
5818 UOBJC_CLASS_decl
= impent
->class_decl
;
5819 UOBJC_METACLASS_decl
= impent
->meta_decl
;
5820 cls_flags
= impent
->has_cxx_cdtors
? CLS_HAS_CXX_STRUCTORS
: 0 ;
5822 my_super_id
= CLASS_SUPER_NAME (implementation_template
);
5825 add_class_reference (my_super_id
);
5827 /* Compute "my_root_id" - this is required for code generation.
5828 the "isa" for all meta class structures points to the root of
5829 the inheritance hierarchy (e.g. "__Object")... */
5830 my_root_id
= my_super_id
;
5833 tree my_root_int
= lookup_interface (my_root_id
);
5835 if (my_root_int
&& CLASS_SUPER_NAME (my_root_int
))
5836 my_root_id
= CLASS_SUPER_NAME (my_root_int
);
5843 /* No super class. */
5844 my_root_id
= CLASS_NAME (implementation_template
);
5846 cast_type
= build_pointer_type (objc_class_template
);
5847 name_expr
= add_objc_string (CLASS_NAME (implementation_template
),
5850 /* Install class `isa' and `super' pointers at runtime. */
5852 super_expr
= add_objc_string (my_super_id
, class_names
);
5854 super_expr
= integer_zero_node
;
5856 super_expr
= build_c_cast (input_location
,
5857 cast_type
, super_expr
); /* cast! */
5859 root_expr
= add_objc_string (my_root_id
, class_names
);
5860 root_expr
= build_c_cast (input_location
, cast_type
, root_expr
); /* cast! */
5862 if (CLASS_PROTOCOL_LIST (implementation_template
))
5864 generate_protocol_references
5865 (CLASS_PROTOCOL_LIST (implementation_template
));
5866 protocol_decl
= generate_protocol_list (implementation_template
);
5871 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
5874 = build_shared_structure_initializer
5875 (TREE_TYPE (UOBJC_METACLASS_decl
),
5876 root_expr
, super_expr
, name_expr
,
5877 convert (integer_type_node
, TYPE_SIZE_UNIT (objc_class_template
)),
5879 UOBJC_CLASS_METHODS_decl
,
5880 UOBJC_CLASS_VARIABLES_decl
,
5883 finish_var_decl (UOBJC_METACLASS_decl
, initlist
);
5885 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5888 = build_shared_structure_initializer
5889 (TREE_TYPE (UOBJC_CLASS_decl
),
5890 build_unary_op (input_location
, ADDR_EXPR
, UOBJC_METACLASS_decl
, 0),
5891 super_expr
, name_expr
,
5892 convert (integer_type_node
,
5893 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
5894 (implementation_template
))),
5895 1 /*CLS_FACTORY*/ | cls_flags
,
5896 UOBJC_INSTANCE_METHODS_decl
,
5897 UOBJC_INSTANCE_VARIABLES_decl
,
5900 finish_var_decl (UOBJC_CLASS_decl
, initlist
);
5905 synth_id_with_class_suffix (const char *preamble
, tree ctxt
)
5907 static char string
[BUFSIZE
];
5909 if (TREE_CODE (ctxt
) == CLASS_IMPLEMENTATION_TYPE
5910 || TREE_CODE (ctxt
) == CLASS_INTERFACE_TYPE
)
5912 sprintf (string
, "%s_%s", preamble
,
5913 IDENTIFIER_POINTER (CLASS_NAME (ctxt
)));
5915 else if (TREE_CODE (ctxt
) == CATEGORY_IMPLEMENTATION_TYPE
5916 || TREE_CODE (ctxt
) == CATEGORY_INTERFACE_TYPE
)
5918 /* We have a category. */
5919 const char *const class_name
5920 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context
));
5921 const char *const class_super_name
5922 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context
));
5923 sprintf (string
, "%s_%s_%s", preamble
, class_name
, class_super_name
);
5925 else if (TREE_CODE (ctxt
) == PROTOCOL_INTERFACE_TYPE
)
5927 const char *protocol_name
= IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt
));
5928 sprintf (string
, "%s_%s", preamble
, protocol_name
);
5936 /* If type is empty or only type qualifiers are present, add default
5937 type of id (otherwise grokdeclarator will default to int). */
5940 adjust_type_for_id_default (tree type
)
5943 type
= make_node (TREE_LIST
);
5945 if (!TREE_VALUE (type
))
5946 TREE_VALUE (type
) = objc_object_type
;
5947 else if (TREE_CODE (TREE_VALUE (type
)) == RECORD_TYPE
5948 && TYPED_OBJECT (TREE_VALUE (type
)))
5949 error ("can not use an object as parameter to a method");
5956 selector ':' '(' typename ')' identifier
5959 Transform an Objective-C keyword argument into
5960 the C equivalent parameter declarator.
5962 In: key_name, an "identifier_node" (optional).
5963 arg_type, a "tree_list" (optional).
5964 arg_name, an "identifier_node".
5966 Note: It would be really nice to strongly type the preceding
5967 arguments in the function prototype; however, then I
5968 could not use the "accessor" macros defined in "tree.h".
5970 Out: an instance of "keyword_decl". */
5973 objc_build_keyword_decl (tree key_name
, tree arg_type
, tree arg_name
)
5977 /* If no type is specified, default to "id". */
5978 arg_type
= adjust_type_for_id_default (arg_type
);
5980 keyword_decl
= make_node (KEYWORD_DECL
);
5982 TREE_TYPE (keyword_decl
) = arg_type
;
5983 KEYWORD_ARG_NAME (keyword_decl
) = arg_name
;
5984 KEYWORD_KEY_NAME (keyword_decl
) = key_name
;
5986 return keyword_decl
;
5989 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
5992 build_keyword_selector (tree selector
)
5995 tree key_chain
, key_name
;
5998 /* Scan the selector to see how much space we'll need. */
5999 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
6001 if (TREE_CODE (selector
) == KEYWORD_DECL
)
6002 key_name
= KEYWORD_KEY_NAME (key_chain
);
6003 else if (TREE_CODE (selector
) == TREE_LIST
)
6004 key_name
= TREE_PURPOSE (key_chain
);
6009 len
+= IDENTIFIER_LENGTH (key_name
) + 1;
6011 /* Just a ':' arg. */
6015 buf
= (char *) alloca (len
+ 1);
6016 /* Start the buffer out as an empty string. */
6019 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
6021 if (TREE_CODE (selector
) == KEYWORD_DECL
)
6022 key_name
= KEYWORD_KEY_NAME (key_chain
);
6023 else if (TREE_CODE (selector
) == TREE_LIST
)
6025 key_name
= TREE_PURPOSE (key_chain
);
6026 /* The keyword decl chain will later be used as a function argument
6027 chain. Unhook the selector itself so as to not confuse other
6028 parts of the compiler. */
6029 TREE_PURPOSE (key_chain
) = NULL_TREE
;
6035 strcat (buf
, IDENTIFIER_POINTER (key_name
));
6039 return get_identifier (buf
);
6042 /* Used for declarations and definitions. */
6045 build_method_decl (enum tree_code code
, tree ret_type
, tree selector
,
6046 tree add_args
, bool ellipsis
)
6050 /* If no type is specified, default to "id". */
6051 ret_type
= adjust_type_for_id_default (ret_type
);
6053 method_decl
= make_node (code
);
6054 TREE_TYPE (method_decl
) = ret_type
;
6056 /* If we have a keyword selector, create an identifier_node that
6057 represents the full selector name (`:' included)... */
6058 if (TREE_CODE (selector
) == KEYWORD_DECL
)
6060 METHOD_SEL_NAME (method_decl
) = build_keyword_selector (selector
);
6061 METHOD_SEL_ARGS (method_decl
) = selector
;
6062 METHOD_ADD_ARGS (method_decl
) = add_args
;
6063 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl
) = ellipsis
;
6067 METHOD_SEL_NAME (method_decl
) = selector
;
6068 METHOD_SEL_ARGS (method_decl
) = NULL_TREE
;
6069 METHOD_ADD_ARGS (method_decl
) = NULL_TREE
;
6075 #define METHOD_DEF 0
6076 #define METHOD_REF 1
6078 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
6079 an argument list for method METH. CONTEXT is either METHOD_DEF or
6080 METHOD_REF, saying whether we are trying to define a method or call
6081 one. SUPERFLAG says this is for a send to super; this makes a
6082 difference for the NeXT calling sequence in which the lookup and
6083 the method call are done together. If METH is null, user-defined
6084 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
6087 get_arg_type_list (tree meth
, int context
, int superflag
)
6091 /* Receiver type. */
6092 if (flag_next_runtime
&& superflag
)
6093 arglist
= build_tree_list (NULL_TREE
, objc_super_type
);
6094 else if (context
== METHOD_DEF
&& TREE_CODE (meth
) == INSTANCE_METHOD_DECL
)
6095 arglist
= build_tree_list (NULL_TREE
, objc_instance_type
);
6097 arglist
= build_tree_list (NULL_TREE
, objc_object_type
);
6099 /* Selector type - will eventually change to `int'. */
6100 chainon (arglist
, build_tree_list (NULL_TREE
, objc_selector_type
));
6102 /* No actual method prototype given -- assume that remaining arguments
6107 /* Build a list of argument types. */
6108 for (akey
= METHOD_SEL_ARGS (meth
); akey
; akey
= DECL_CHAIN (akey
))
6110 tree arg_type
= TREE_VALUE (TREE_TYPE (akey
));
6112 /* Decay argument types for the underlying C function as appropriate. */
6113 arg_type
= objc_decay_parm_type (arg_type
);
6115 chainon (arglist
, build_tree_list (NULL_TREE
, arg_type
));
6118 if (METHOD_ADD_ARGS (meth
))
6120 for (akey
= TREE_CHAIN (METHOD_ADD_ARGS (meth
));
6121 akey
; akey
= TREE_CHAIN (akey
))
6123 tree arg_type
= TREE_TYPE (TREE_VALUE (akey
));
6125 arg_type
= objc_decay_parm_type (arg_type
);
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
);
8369 /* Ignore type qualifiers other than 'const' when encoding a
8372 if (type
== error_mark_node
)
8375 if (!flag_next_runtime
)
8377 if (TYPE_READONLY (type
))
8378 obstack_1grow (&util_obstack
, 'r');
8384 if (flag_next_runtime
)
8386 /* Kludge for backwards-compatibility with gcc-3.3: enums
8387 are always encoded as 'i' no matter what type they
8388 actually are (!). */
8389 obstack_1grow (&util_obstack
, 'i');
8392 /* Else, they are encoded exactly like the integer type that is
8393 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
);
8441 /* Floating point types. */
8442 switch (GET_MODE_BITSIZE (TYPE_MODE (type
)))
8444 case 32: c
= 'f'; break;
8445 case 64: c
= 'd'; break;
8447 case 128: c
= 'D'; break;
8450 obstack_1grow (&util_obstack
, c
);
8454 obstack_1grow (&util_obstack
, 'v');
8458 obstack_1grow (&util_obstack
, 'B');
8462 encode_array (type
, curtype
, format
);
8467 case REFERENCE_TYPE
:
8469 encode_pointer (type
, curtype
, format
);
8473 encode_aggregate_within (type
, curtype
, format
, '{', '}');
8477 encode_aggregate_within (type
, curtype
, format
, '(', ')');
8480 case FUNCTION_TYPE
: /* '?' means an unknown type. */
8481 obstack_1grow (&util_obstack
, '?');
8485 /* A complex is encoded as 'j' followed by the inner type (eg,
8486 "_Complex int" is encoded as 'ji'). */
8487 obstack_1grow (&util_obstack
, 'j');
8488 encode_type (TREE_TYPE (type
), curtype
, format
);
8492 encode_vector (type
, curtype
, format
);
8496 warning (0, "unknown type %s found during Objective-C encoding",
8497 gen_type_name (type
));
8498 obstack_1grow (&util_obstack
, '?');
8502 if (flag_next_runtime
)
8504 /* Super-kludge. Some ObjC qualifier and type combinations need
8505 to be rearranged for compatibility with gcc-3.3. */
8506 if (code
== POINTER_TYPE
&& obstack_object_size (&util_obstack
) >= 3)
8508 char *enc
= obstack_base (&util_obstack
) + curtype
;
8510 /* Rewrite "in const" from "nr" to "rn". */
8511 if (curtype
>= 1 && !strncmp (enc
- 1, "nr", 2))
8512 strncpy (enc
- 1, "rn", 2);
8518 encode_gnu_bitfield (int position
, tree type
, int size
)
8520 enum tree_code code
= TREE_CODE (type
);
8522 char charType
= '?';
8524 /* This code is only executed for the GNU runtime, so we can ignore
8525 the NeXT runtime kludge of always encoding enums as 'i' no matter
8526 what integers they actually are. */
8527 if (code
== INTEGER_TYPE
|| code
== ENUMERAL_TYPE
)
8529 if (integer_zerop (TYPE_MIN_VALUE (type
)))
8530 /* Unsigned integer types. */
8532 if (TYPE_MODE (type
) == QImode
)
8534 else if (TYPE_MODE (type
) == HImode
)
8536 else if (TYPE_MODE (type
) == SImode
)
8538 if (type
== long_unsigned_type_node
)
8543 else if (TYPE_MODE (type
) == DImode
)
8547 /* Signed integer types. */
8549 if (TYPE_MODE (type
) == QImode
)
8551 else if (TYPE_MODE (type
) == HImode
)
8553 else if (TYPE_MODE (type
) == SImode
)
8555 if (type
== long_integer_type_node
)
8561 else if (TYPE_MODE (type
) == DImode
)
8567 /* Do not do any encoding, produce an error and keep going. */
8568 error ("trying to encode non-integer type as a bitfield");
8572 sprintf (buffer
, "b%d%c%d", position
, charType
, size
);
8573 obstack_grow (&util_obstack
, buffer
, strlen (buffer
));
8577 encode_field_decl (tree field_decl
, int curtype
, int format
)
8580 /* C++ static members, and things that are not fields at all,
8581 should not appear in the encoding. */
8582 if (TREE_CODE (field_decl
) != FIELD_DECL
|| TREE_STATIC (field_decl
))
8586 /* Generate the bitfield typing information, if needed. Note the difference
8587 between GNU and NeXT runtimes. */
8588 if (DECL_BIT_FIELD_TYPE (field_decl
))
8590 int size
= tree_low_cst (DECL_SIZE (field_decl
), 1);
8592 if (flag_next_runtime
)
8593 encode_next_bitfield (size
);
8595 encode_gnu_bitfield (int_bit_position (field_decl
),
8596 DECL_BIT_FIELD_TYPE (field_decl
), size
);
8599 encode_type (TREE_TYPE (field_decl
), curtype
, format
);
8602 /* Decay array and function parameters into pointers. */
8605 objc_decay_parm_type (tree type
)
8607 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == FUNCTION_TYPE
)
8608 type
= build_pointer_type (TREE_CODE (type
) == ARRAY_TYPE
8615 static GTY(()) tree objc_parmlist
= NULL_TREE
;
8617 /* Append PARM to a list of formal parameters of a method, making a necessary
8618 array-to-pointer adjustment along the way. */
8621 objc_push_parm (tree parm
)
8625 if (TREE_TYPE (parm
) == error_mark_node
)
8627 objc_parmlist
= chainon (objc_parmlist
, parm
);
8631 /* Decay arrays and functions into pointers. */
8632 type
= objc_decay_parm_type (TREE_TYPE (parm
));
8634 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8636 if (type
!= TREE_TYPE (parm
))
8637 parm
= build_decl (input_location
, PARM_DECL
, DECL_NAME (parm
), type
);
8639 DECL_ARG_TYPE (parm
)
8640 = lang_hooks
.types
.type_promotes_to (TREE_TYPE (parm
));
8642 /* Record constancy and volatility. */
8643 c_apply_type_quals_to_decl
8644 ((TYPE_READONLY (TREE_TYPE (parm
)) ? TYPE_QUAL_CONST
: 0)
8645 | (TYPE_RESTRICT (TREE_TYPE (parm
)) ? TYPE_QUAL_RESTRICT
: 0)
8646 | (TYPE_VOLATILE (TREE_TYPE (parm
)) ? TYPE_QUAL_VOLATILE
: 0), parm
);
8648 objc_parmlist
= chainon (objc_parmlist
, parm
);
8651 /* Retrieve the formal parameter list constructed via preceding calls to
8652 objc_push_parm(). */
8656 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED
)
8658 static struct c_arg_info
*
8659 objc_get_parm_info (int have_ellipsis
)
8663 tree parm_info
= objc_parmlist
;
8664 objc_parmlist
= NULL_TREE
;
8668 tree parm_info
= objc_parmlist
;
8669 struct c_arg_info
*arg_info
;
8670 /* The C front-end requires an elaborate song and dance at
8673 declare_parm_level ();
8676 tree next
= DECL_CHAIN (parm_info
);
8678 DECL_CHAIN (parm_info
) = NULL_TREE
;
8679 parm_info
= pushdecl (parm_info
);
8680 finish_decl (parm_info
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
8683 arg_info
= get_parm_info (have_ellipsis
);
8685 objc_parmlist
= NULL_TREE
;
8690 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8691 method definitions. In the case of instance methods, we can be more
8692 specific as to the type of 'self'. */
8695 synth_self_and_ucmd_args (void)
8699 if (objc_method_context
8700 && TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
)
8701 self_type
= objc_instance_type
;
8703 /* Really a `struct objc_class *'. However, we allow people to
8704 assign to self, which changes its type midstream. */
8705 self_type
= objc_object_type
;
8708 objc_push_parm (build_decl (input_location
,
8709 PARM_DECL
, self_id
, self_type
));
8712 objc_push_parm (build_decl (input_location
,
8713 PARM_DECL
, ucmd_id
, objc_selector_type
));
8716 /* Transform an Objective-C method definition into a static C function
8717 definition, synthesizing the first two arguments, "self" and "_cmd",
8721 start_method_def (tree method
)
8727 struct c_arg_info
*parm_info
;
8729 int have_ellipsis
= 0;
8731 /* If we are defining a "dealloc" method in a non-root class, we
8732 will need to check if a [super dealloc] is missing, and warn if
8734 if(CLASS_SUPER_NAME (objc_implementation_context
)
8735 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method
))))
8736 should_call_super_dealloc
= 1;
8738 should_call_super_dealloc
= 0;
8740 /* Required to implement _msgSuper. */
8741 objc_method_context
= method
;
8742 UOBJC_SUPER_decl
= NULL_TREE
;
8744 /* Generate prototype declarations for arguments..."new-style". */
8745 synth_self_and_ucmd_args ();
8747 /* Generate argument declarations if a keyword_decl. */
8748 parmlist
= METHOD_SEL_ARGS (method
);
8751 tree type
= TREE_VALUE (TREE_TYPE (parmlist
)), parm
;
8753 parm
= build_decl (input_location
,
8754 PARM_DECL
, KEYWORD_ARG_NAME (parmlist
), type
);
8755 objc_push_parm (parm
);
8756 parmlist
= DECL_CHAIN (parmlist
);
8759 if (METHOD_ADD_ARGS (method
))
8763 for (akey
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
8764 akey
; akey
= TREE_CHAIN (akey
))
8766 objc_push_parm (TREE_VALUE (akey
));
8769 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
8773 parm_info
= objc_get_parm_info (have_ellipsis
);
8775 really_start_method (objc_method_context
, parm_info
);
8778 /* Return 1 if TYPE1 is equivalent to TYPE2
8779 for purposes of method overloading. */
8782 objc_types_are_equivalent (tree type1
, tree type2
)
8787 /* Strip away indirections. */
8788 while ((TREE_CODE (type1
) == ARRAY_TYPE
|| TREE_CODE (type1
) == POINTER_TYPE
)
8789 && (TREE_CODE (type1
) == TREE_CODE (type2
)))
8790 type1
= TREE_TYPE (type1
), type2
= TREE_TYPE (type2
);
8791 if (TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
8794 type1
= (TYPE_HAS_OBJC_INFO (type1
)
8795 ? TYPE_OBJC_PROTOCOL_LIST (type1
)
8797 type2
= (TYPE_HAS_OBJC_INFO (type2
)
8798 ? TYPE_OBJC_PROTOCOL_LIST (type2
)
8801 if (list_length (type1
) == list_length (type2
))
8803 for (; type2
; type2
= TREE_CHAIN (type2
))
8804 if (!lookup_protocol_in_reflist (type1
, TREE_VALUE (type2
)))
8811 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8814 objc_types_share_size_and_alignment (tree type1
, tree type2
)
8816 return (simple_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
8817 && TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
));
8820 /* Return 1 if PROTO1 is equivalent to PROTO2
8821 for purposes of method overloading. Ordinarily, the type signatures
8822 should match up exactly, unless STRICT is zero, in which case we
8823 shall allow differences in which the size and alignment of a type
8827 comp_proto_with_proto (tree proto1
, tree proto2
, int strict
)
8831 /* The following test is needed in case there are hashing
8833 if (METHOD_SEL_NAME (proto1
) != METHOD_SEL_NAME (proto2
))
8836 /* Compare return types. */
8837 type1
= TREE_VALUE (TREE_TYPE (proto1
));
8838 type2
= TREE_VALUE (TREE_TYPE (proto2
));
8840 if (!objc_types_are_equivalent (type1
, type2
)
8841 && (strict
|| !objc_types_share_size_and_alignment (type1
, type2
)))
8844 /* Compare argument types. */
8845 for (type1
= get_arg_type_list (proto1
, METHOD_REF
, 0),
8846 type2
= get_arg_type_list (proto2
, METHOD_REF
, 0);
8848 type1
= TREE_CHAIN (type1
), type2
= TREE_CHAIN (type2
))
8850 if (!objc_types_are_equivalent (TREE_VALUE (type1
), TREE_VALUE (type2
))
8852 || !objc_types_share_size_and_alignment (TREE_VALUE (type1
),
8853 TREE_VALUE (type2
))))
8857 return (!type1
&& !type2
);
8860 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
8861 this occurs. ObjC method dispatches are _not_ like C++ virtual
8862 member function dispatches, and we account for the difference here. */
8865 objc_fold_obj_type_ref (tree ref
, tree known_type
)
8867 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED
,
8868 tree known_type ATTRIBUTE_UNUSED
)
8872 tree v
= BINFO_VIRTUALS (TYPE_BINFO (known_type
));
8874 /* If the receiver does not have virtual member functions, there
8875 is nothing we can (or need to) do here. */
8879 /* Let C++ handle C++ virtual functions. */
8880 return cp_fold_obj_type_ref (ref
, known_type
);
8882 /* For plain ObjC, we currently do not need to do anything. */
8888 objc_start_function (tree name
, tree type
, tree attrs
,
8892 struct c_arg_info
*params
8896 tree fndecl
= build_decl (input_location
,
8897 FUNCTION_DECL
, name
, type
);
8900 DECL_ARGUMENTS (fndecl
) = params
;
8901 DECL_INITIAL (fndecl
) = error_mark_node
;
8902 DECL_EXTERNAL (fndecl
) = 0;
8903 TREE_STATIC (fndecl
) = 1;
8904 retrofit_lang_decl (fndecl
);
8905 cplus_decl_attributes (&fndecl
, attrs
, 0);
8906 start_preparsed_function (fndecl
, attrs
, /*flags=*/SF_DEFAULT
);
8908 current_function_returns_value
= 0; /* Assume, until we see it does. */
8909 current_function_returns_null
= 0;
8911 decl_attributes (&fndecl
, attrs
, 0);
8912 announce_function (fndecl
);
8913 DECL_INITIAL (fndecl
) = error_mark_node
;
8914 DECL_EXTERNAL (fndecl
) = 0;
8915 TREE_STATIC (fndecl
) = 1;
8916 current_function_decl
= pushdecl (fndecl
);
8918 declare_parm_level ();
8919 DECL_RESULT (current_function_decl
)
8920 = build_decl (input_location
,
8921 RESULT_DECL
, NULL_TREE
,
8922 TREE_TYPE (TREE_TYPE (current_function_decl
)));
8923 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl
)) = 1;
8924 DECL_IGNORED_P (DECL_RESULT (current_function_decl
)) = 1;
8925 start_fname_decls ();
8926 store_parm_decls_from (params
);
8929 TREE_USED (current_function_decl
) = 1;
8932 /* - Generate an identifier for the function. the format is "_n_cls",
8933 where 1 <= n <= nMethods, and cls is the name the implementation we
8935 - Install the return type from the method declaration.
8936 - If we have a prototype, check for type consistency. */
8939 really_start_method (tree method
,
8943 struct c_arg_info
*parmlist
8947 tree ret_type
, meth_type
;
8949 const char *sel_name
, *class_name
, *cat_name
;
8952 /* Synth the storage class & assemble the return type. */
8953 ret_type
= TREE_VALUE (TREE_TYPE (method
));
8955 sel_name
= IDENTIFIER_POINTER (METHOD_SEL_NAME (method
));
8956 class_name
= IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context
));
8957 cat_name
= ((TREE_CODE (objc_implementation_context
)
8958 == CLASS_IMPLEMENTATION_TYPE
)
8960 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context
)));
8963 /* Make sure this is big enough for any plausible method label. */
8964 buf
= (char *) alloca (50 + strlen (sel_name
) + strlen (class_name
)
8965 + (cat_name
? strlen (cat_name
) : 0));
8967 OBJC_GEN_METHOD_LABEL (buf
, TREE_CODE (method
) == INSTANCE_METHOD_DECL
,
8968 class_name
, cat_name
, sel_name
, method_slot
);
8970 method_id
= get_identifier (buf
);
8973 /* Objective-C methods cannot be overloaded, so we don't need
8974 the type encoding appended. It looks bad anyway... */
8975 push_lang_context (lang_name_c
);
8979 = build_function_type (ret_type
,
8980 get_arg_type_list (method
, METHOD_DEF
, 0));
8981 objc_start_function (method_id
, meth_type
, NULL_TREE
, parmlist
);
8983 /* Set self_decl from the first argument. */
8984 self_decl
= DECL_ARGUMENTS (current_function_decl
);
8986 /* Suppress unused warnings. */
8987 TREE_USED (self_decl
) = 1;
8988 DECL_READ_P (self_decl
) = 1;
8989 TREE_USED (DECL_CHAIN (self_decl
)) = 1;
8990 DECL_READ_P (DECL_CHAIN (self_decl
)) = 1;
8992 pop_lang_context ();
8995 METHOD_DEFINITION (method
) = current_function_decl
;
8997 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8999 if (implementation_template
!= objc_implementation_context
)
9002 = lookup_method_static (implementation_template
,
9003 METHOD_SEL_NAME (method
),
9004 ((TREE_CODE (method
) == CLASS_METHOD_DECL
)
9005 | OBJC_LOOKUP_NO_SUPER
));
9009 if (!comp_proto_with_proto (method
, proto
, 1))
9011 bool type
= TREE_CODE (method
) == INSTANCE_METHOD_DECL
;
9013 warning_at (DECL_SOURCE_LOCATION (method
), 0,
9014 "conflicting types for %<%c%s%>",
9016 identifier_to_locale (gen_method_decl (method
)));
9017 inform (DECL_SOURCE_LOCATION (proto
),
9018 "previous declaration of %<%c%s%>",
9020 identifier_to_locale (gen_method_decl (proto
)));
9025 /* We have a method @implementation even though we did not
9026 see a corresponding @interface declaration (which is allowed
9027 by Objective-C rules). Go ahead and place the method in
9028 the @interface anyway, so that message dispatch lookups
9030 tree interface
= implementation_template
;
9032 if (TREE_CODE (objc_implementation_context
)
9033 == CATEGORY_IMPLEMENTATION_TYPE
)
9034 interface
= lookup_category
9036 CLASS_SUPER_NAME (objc_implementation_context
));
9039 objc_add_method (interface
, copy_node (method
),
9040 TREE_CODE (method
) == CLASS_METHOD_DECL
);
9045 static void *UOBJC_SUPER_scope
= 0;
9047 /* _n_Method (id self, SEL sel, ...)
9049 struct objc_super _S;
9050 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
9054 get_super_receiver (void)
9056 if (objc_method_context
)
9058 tree super_expr
, super_expr_list
;
9060 if (!UOBJC_SUPER_decl
)
9062 UOBJC_SUPER_decl
= build_decl (input_location
,
9063 VAR_DECL
, get_identifier (TAG_SUPER
),
9064 objc_super_template
);
9065 /* This prevents `unused variable' warnings when compiling with -Wall. */
9066 TREE_USED (UOBJC_SUPER_decl
) = 1;
9067 DECL_READ_P (UOBJC_SUPER_decl
) = 1;
9068 lang_hooks
.decls
.pushdecl (UOBJC_SUPER_decl
);
9069 finish_decl (UOBJC_SUPER_decl
, input_location
, NULL_TREE
, NULL_TREE
,
9071 UOBJC_SUPER_scope
= objc_get_current_scope ();
9074 /* Set receiver to self. */
9075 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
, self_id
);
9076 super_expr
= build_modify_expr (input_location
, super_expr
, NULL_TREE
,
9077 NOP_EXPR
, input_location
, self_decl
,
9079 super_expr_list
= super_expr
;
9081 /* Set class to begin searching. */
9082 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
,
9083 get_identifier ("super_class"));
9085 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
9087 /* [_cls, __cls]Super are "pre-built" in
9088 synth_forward_declarations. */
9090 super_expr
= build_modify_expr (input_location
, super_expr
,
9091 NULL_TREE
, NOP_EXPR
,
9093 ((TREE_CODE (objc_method_context
)
9094 == INSTANCE_METHOD_DECL
)
9101 /* We have a category. */
9103 tree super_name
= CLASS_SUPER_NAME (implementation_template
);
9106 /* Barf if super used in a category of Object. */
9109 error ("no super class declared in interface for %qE",
9110 CLASS_NAME (implementation_template
));
9111 return error_mark_node
;
9114 if (flag_next_runtime
&& !flag_zero_link
)
9116 super_class
= objc_get_class_reference (super_name
);
9117 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
)
9118 /* If we are in a class method, we must retrieve the
9119 _metaclass_ for the current class, pointed at by
9120 the class's "isa" pointer. The following assumes that
9121 "isa" is the first ivar in a class (which it must be). */
9123 = build_indirect_ref
9125 build_c_cast (input_location
,
9126 build_pointer_type (objc_class_type
),
9127 super_class
), RO_UNARY_STAR
);
9131 add_class_reference (super_name
);
9132 super_class
= (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
9133 ? objc_get_class_decl
: objc_get_meta_class_decl
);
9134 assemble_external (super_class
);
9136 = build_function_call
9141 my_build_string_pointer
9142 (IDENTIFIER_LENGTH (super_name
) + 1,
9143 IDENTIFIER_POINTER (super_name
))));
9147 = build_modify_expr (input_location
, super_expr
, NULL_TREE
,
9150 build_c_cast (input_location
,
9151 TREE_TYPE (super_expr
),
9156 super_expr_list
= build_compound_expr (input_location
,
9157 super_expr_list
, super_expr
);
9159 super_expr
= build_unary_op (input_location
,
9160 ADDR_EXPR
, UOBJC_SUPER_decl
, 0);
9161 super_expr_list
= build_compound_expr (input_location
,
9162 super_expr_list
, super_expr
);
9164 return super_expr_list
;
9168 error ("[super ...] must appear in a method context");
9169 return error_mark_node
;
9173 /* When exiting a scope, sever links to a 'super' declaration (if any)
9174 therein contained. */
9177 objc_clear_super_receiver (void)
9179 if (objc_method_context
9180 && UOBJC_SUPER_scope
== objc_get_current_scope ()) {
9181 UOBJC_SUPER_decl
= 0;
9182 UOBJC_SUPER_scope
= 0;
9187 objc_finish_method_definition (tree fndecl
)
9189 /* We cannot validly inline ObjC methods, at least not without a language
9190 extension to declare that a method need not be dynamically
9191 dispatched, so suppress all thoughts of doing so. */
9192 DECL_UNINLINABLE (fndecl
) = 1;
9195 /* The C++ front-end will have called finish_function() for us. */
9199 METHOD_ENCODING (objc_method_context
)
9200 = encode_method_prototype (objc_method_context
);
9202 /* Required to implement _msgSuper. This must be done AFTER finish_function,
9203 since the optimizer may find "may be used before set" errors. */
9204 objc_method_context
= NULL_TREE
;
9206 if (should_call_super_dealloc
)
9207 warning (0, "method possibly missing a [super dealloc] call");
9210 /* Given a tree DECL node, produce a printable description of it in the given
9211 buffer, overwriting the buffer. */
9214 gen_declaration (tree decl
)
9220 gen_type_name_0 (TREE_TYPE (decl
));
9222 if (DECL_NAME (decl
))
9224 if (!POINTER_TYPE_P (TREE_TYPE (decl
)))
9225 strcat (errbuf
, " ");
9227 strcat (errbuf
, IDENTIFIER_POINTER (DECL_NAME (decl
)));
9230 if (DECL_INITIAL (decl
)
9231 && TREE_CODE (DECL_INITIAL (decl
)) == INTEGER_CST
)
9232 sprintf (errbuf
+ strlen (errbuf
), ": " HOST_WIDE_INT_PRINT_DEC
,
9233 TREE_INT_CST_LOW (DECL_INITIAL (decl
)));
9239 /* Given a tree TYPE node, produce a printable description of it in the given
9240 buffer, overwriting the buffer. */
9243 gen_type_name_0 (tree type
)
9245 tree orig
= type
, proto
;
9247 if (TYPE_P (type
) && TYPE_NAME (type
))
9248 type
= TYPE_NAME (type
);
9249 else if (POINTER_TYPE_P (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
9251 tree inner
= TREE_TYPE (type
);
9253 while (TREE_CODE (inner
) == ARRAY_TYPE
)
9254 inner
= TREE_TYPE (inner
);
9256 gen_type_name_0 (inner
);
9258 if (!POINTER_TYPE_P (inner
))
9259 strcat (errbuf
, " ");
9261 if (POINTER_TYPE_P (type
))
9262 strcat (errbuf
, "*");
9264 while (type
!= inner
)
9266 strcat (errbuf
, "[");
9268 if (TYPE_DOMAIN (type
))
9272 sprintf (sz
, HOST_WIDE_INT_PRINT_DEC
,
9274 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1));
9275 strcat (errbuf
, sz
);
9278 strcat (errbuf
, "]");
9279 type
= TREE_TYPE (type
);
9285 if (TREE_CODE (type
) == TYPE_DECL
&& DECL_NAME (type
))
9286 type
= DECL_NAME (type
);
9288 strcat (errbuf
, TREE_CODE (type
) == IDENTIFIER_NODE
9289 ? IDENTIFIER_POINTER (type
)
9292 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
9293 if (objc_is_id (orig
))
9294 orig
= TREE_TYPE (orig
);
9296 proto
= TYPE_HAS_OBJC_INFO (orig
) ? TYPE_OBJC_PROTOCOL_LIST (orig
) : NULL_TREE
;
9300 strcat (errbuf
, " <");
9304 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto
))));
9305 proto
= TREE_CHAIN (proto
);
9306 strcat (errbuf
, proto
? ", " : ">");
9315 gen_type_name (tree type
)
9319 return gen_type_name_0 (type
);
9322 /* Given a method tree, put a printable description into the given
9323 buffer (overwriting) and return a pointer to the buffer. */
9326 gen_method_decl (tree method
)
9330 strcpy (errbuf
, "("); /* NB: Do _not_ call strcat() here. */
9331 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method
)));
9332 strcat (errbuf
, ")");
9333 chain
= METHOD_SEL_ARGS (method
);
9337 /* We have a chain of keyword_decls. */
9340 if (KEYWORD_KEY_NAME (chain
))
9341 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain
)));
9343 strcat (errbuf
, ":(");
9344 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain
)));
9345 strcat (errbuf
, ")");
9347 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain
)));
9348 if ((chain
= DECL_CHAIN (chain
)))
9349 strcat (errbuf
, " ");
9353 if (METHOD_ADD_ARGS (method
))
9355 chain
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
9357 /* Know we have a chain of parm_decls. */
9360 strcat (errbuf
, ", ");
9361 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain
)));
9362 chain
= TREE_CHAIN (chain
);
9365 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
9366 strcat (errbuf
, ", ...");
9371 /* We have a unary selector. */
9372 strcat (errbuf
, IDENTIFIER_POINTER (METHOD_SEL_NAME (method
)));
9380 /* Dump an @interface declaration of the supplied class CHAIN to the
9381 supplied file FP. Used to implement the -gen-decls option (which
9382 prints out an @interface declaration of all classes compiled in
9383 this run); potentially useful for debugging the compiler too. */
9385 dump_interface (FILE *fp
, tree chain
)
9387 /* FIXME: A heap overflow here whenever a method (or ivar)
9388 declaration is so long that it doesn't fit in the buffer. The
9389 code and all the related functions should be rewritten to avoid
9390 using fixed size buffers. */
9391 const char *my_name
= IDENTIFIER_POINTER (CLASS_NAME (chain
));
9392 tree ivar_decls
= CLASS_RAW_IVARS (chain
);
9393 tree nst_methods
= CLASS_NST_METHODS (chain
);
9394 tree cls_methods
= CLASS_CLS_METHODS (chain
);
9396 fprintf (fp
, "\n@interface %s", my_name
);
9398 /* CLASS_SUPER_NAME is used to store the superclass name for
9399 classes, and the category name for categories. */
9400 if (CLASS_SUPER_NAME (chain
))
9402 const char *name
= IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain
));
9404 if (TREE_CODE (chain
) == CATEGORY_IMPLEMENTATION_TYPE
9405 || TREE_CODE (chain
) == CATEGORY_INTERFACE_TYPE
)
9407 fprintf (fp
, " (%s)\n", name
);
9411 fprintf (fp
, " : %s\n", name
);
9417 /* FIXME - the following doesn't seem to work at the moment. */
9420 fprintf (fp
, "{\n");
9423 fprintf (fp
, "\t%s;\n", gen_declaration (ivar_decls
));
9424 ivar_decls
= TREE_CHAIN (ivar_decls
);
9427 fprintf (fp
, "}\n");
9432 fprintf (fp
, "- %s;\n", gen_method_decl (nst_methods
));
9433 nst_methods
= TREE_CHAIN (nst_methods
);
9438 fprintf (fp
, "+ %s;\n", gen_method_decl (cls_methods
));
9439 cls_methods
= TREE_CHAIN (cls_methods
);
9442 fprintf (fp
, "@end\n");
9445 /* Demangle function for Objective-C */
9447 objc_demangle (const char *mangled
)
9449 char *demangled
, *cp
;
9451 if (mangled
[0] == '_' &&
9452 (mangled
[1] == 'i' || mangled
[1] == 'c') &&
9455 cp
= demangled
= XNEWVEC (char, strlen(mangled
) + 2);
9456 if (mangled
[1] == 'i')
9457 *cp
++ = '-'; /* for instance method */
9459 *cp
++ = '+'; /* for class method */
9460 *cp
++ = '['; /* opening left brace */
9461 strcpy(cp
, mangled
+3); /* tack on the rest of the mangled name */
9462 while (*cp
&& *cp
== '_')
9463 cp
++; /* skip any initial underbars in class name */
9464 cp
= strchr(cp
, '_'); /* find first non-initial underbar */
9467 free(demangled
); /* not mangled name */
9470 if (cp
[1] == '_') /* easy case: no category name */
9472 *cp
++ = ' '; /* replace two '_' with one ' ' */
9473 strcpy(cp
, mangled
+ (cp
- demangled
) + 2);
9477 *cp
++ = '('; /* less easy case: category name */
9478 cp
= strchr(cp
, '_');
9481 free(demangled
); /* not mangled name */
9485 *cp
++ = ' '; /* overwriting 1st char of method name... */
9486 strcpy(cp
, mangled
+ (cp
- demangled
)); /* get it back */
9488 while (*cp
&& *cp
== '_')
9489 cp
++; /* skip any initial underbars in method name */
9492 *cp
= ':'; /* replace remaining '_' with ':' */
9493 *cp
++ = ']'; /* closing right brace */
9494 *cp
++ = 0; /* string terminator */
9498 return mangled
; /* not an objc mangled name */
9502 objc_printable_name (tree decl
, int kind ATTRIBUTE_UNUSED
)
9504 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl
)));
9510 gcc_obstack_init (&util_obstack
);
9511 util_firstobj
= (char *) obstack_finish (&util_obstack
);
9513 errbuf
= XNEWVEC (char, 1024 * 10);
9515 synth_module_prologue ();
9521 struct imp_entry
*impent
;
9523 /* The internally generated initializers appear to have missing braces.
9524 Don't warn about this. */
9525 int save_warn_missing_braces
= warn_missing_braces
;
9526 warn_missing_braces
= 0;
9528 /* A missing @end may not be detected by the parser. */
9529 if (objc_implementation_context
)
9531 warning (0, "%<@end%> missing in implementation context");
9532 finish_class (objc_implementation_context
);
9533 objc_ivar_chain
= NULL_TREE
;
9534 objc_implementation_context
= NULL_TREE
;
9537 /* Process the static instances here because initialization of objc_symtab
9539 if (objc_static_instances
)
9540 generate_static_references ();
9542 /* forward declare categories */
9544 forward_declare_categories ();
9546 for (impent
= imp_list
; impent
; impent
= impent
->next
)
9548 objc_implementation_context
= impent
->imp_context
;
9549 implementation_template
= impent
->imp_template
;
9551 /* FIXME: This needs reworking to be more obvious. */
9553 UOBJC_CLASS_decl
= impent
->class_decl
;
9554 UOBJC_METACLASS_decl
= impent
->meta_decl
;
9556 /* Dump the @interface of each class as we compile it, if the
9557 -gen-decls option is in use. TODO: Dump the classes in the
9558 order they were found, rather than in reverse order as we
9560 if (flag_gen_declaration
)
9562 dump_interface (gen_declaration_file
, objc_implementation_context
);
9565 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
9567 /* all of the following reference the string pool... */
9568 generate_ivar_lists ();
9569 generate_dispatch_tables ();
9570 generate_shared_structures (impent
);
9574 generate_dispatch_tables ();
9575 generate_category (impent
);
9578 impent
->class_decl
= UOBJC_CLASS_decl
;
9579 impent
->meta_decl
= UOBJC_METACLASS_decl
;
9582 /* If we are using an array of selectors, we must always
9583 finish up the array decl even if no selectors were used. */
9584 if (flag_next_runtime
)
9585 build_next_selector_translation_table ();
9587 build_gnu_selector_translation_table ();
9590 generate_protocols ();
9592 if (flag_next_runtime
)
9593 generate_objc_image_info ();
9595 if (imp_list
|| class_names_chain
9596 || meth_var_names_chain
|| meth_var_types_chain
|| sel_ref_chain
)
9597 generate_objc_symtab_decl ();
9599 /* Arrange for ObjC data structures to be initialized at run time. */
9600 if (objc_implementation_context
|| class_names_chain
|| objc_static_instances
9601 || meth_var_names_chain
|| meth_var_types_chain
|| sel_ref_chain
)
9603 build_module_descriptor ();
9605 if (!flag_next_runtime
)
9606 build_module_initializer_routine ();
9609 /* Dump the class references. This forces the appropriate classes
9610 to be linked into the executable image, preserving unix archive
9611 semantics. This can be removed when we move to a more dynamically
9612 linked environment. */
9614 for (chain
= cls_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
9616 handle_class_ref (chain
);
9617 if (TREE_PURPOSE (chain
))
9618 generate_classref_translation_entry (chain
);
9621 for (impent
= imp_list
; impent
; impent
= impent
->next
)
9622 handle_impent (impent
);
9629 /* Run through the selector hash tables and print a warning for any
9630 selector which has multiple methods. */
9632 for (slot
= 0; slot
< SIZEHASHTABLE
; slot
++)
9634 for (hsh
= cls_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
9635 check_duplicates (hsh
, 0, 1);
9636 for (hsh
= nst_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
9637 check_duplicates (hsh
, 0, 1);
9641 warn_missing_braces
= save_warn_missing_braces
;
9644 /* Subroutines of finish_objc. */
9647 generate_classref_translation_entry (tree chain
)
9649 tree expr
, decl
, type
;
9651 decl
= TREE_PURPOSE (chain
);
9652 type
= TREE_TYPE (decl
);
9654 expr
= add_objc_string (TREE_VALUE (chain
), class_names
);
9655 expr
= convert (type
, expr
); /* cast! */
9657 /* This is a class reference. It is re-written by the runtime,
9658 but will be optimized away unless we force it. */
9659 DECL_PRESERVE_P (decl
) = 1;
9660 finish_var_decl (decl
, expr
);
9665 handle_class_ref (tree chain
)
9667 const char *name
= IDENTIFIER_POINTER (TREE_VALUE (chain
));
9668 char *string
= (char *) alloca (strlen (name
) + 30);
9672 sprintf (string
, "%sobjc_class_name_%s",
9673 (flag_next_runtime
? "." : "__"), name
);
9675 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
9676 if (flag_next_runtime
)
9678 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file
, string
);
9683 /* Make a decl for this name, so we can use its address in a tree. */
9684 decl
= build_decl (input_location
,
9685 VAR_DECL
, get_identifier (string
), TREE_TYPE (integer_zero_node
));
9686 DECL_EXTERNAL (decl
) = 1;
9687 TREE_PUBLIC (decl
) = 1;
9689 finish_var_decl (decl
, 0);
9691 /* Make a decl for the address. */
9692 sprintf (string
, "%sobjc_class_ref_%s",
9693 (flag_next_runtime
? "." : "__"), name
);
9694 exp
= build1 (ADDR_EXPR
, string_type_node
, decl
);
9695 decl
= build_decl (input_location
,
9696 VAR_DECL
, get_identifier (string
), string_type_node
);
9697 TREE_STATIC (decl
) = 1;
9698 TREE_USED (decl
) = 1;
9699 DECL_READ_P (decl
) = 1;
9700 DECL_ARTIFICIAL (decl
) = 1;
9701 DECL_INITIAL (decl
) = error_mark_node
;
9703 /* We must force the reference. */
9704 DECL_PRESERVE_P (decl
) = 1;
9707 finish_var_decl (decl
, exp
);
9711 handle_impent (struct imp_entry
*impent
)
9715 objc_implementation_context
= impent
->imp_context
;
9716 implementation_template
= impent
->imp_template
;
9718 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
9720 const char *const class_name
=
9721 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
));
9723 string
= (char *) alloca (strlen (class_name
) + 30);
9725 sprintf (string
, "%sobjc_class_name_%s",
9726 (flag_next_runtime
? "." : "__"), class_name
);
9728 else if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
9730 const char *const class_name
=
9731 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
));
9732 const char *const class_super_name
=
9733 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent
->imp_context
));
9735 string
= (char *) alloca (strlen (class_name
)
9736 + strlen (class_super_name
) + 30);
9738 /* Do the same for categories. Even though no references to
9739 these symbols are generated automatically by the compiler, it
9740 gives you a handle to pull them into an archive by hand. */
9741 sprintf (string
, "*%sobjc_category_name_%s_%s",
9742 (flag_next_runtime
? "." : "__"), class_name
, class_super_name
);
9747 #ifdef ASM_DECLARE_CLASS_REFERENCE
9748 if (flag_next_runtime
)
9750 ASM_DECLARE_CLASS_REFERENCE (asm_out_file
, string
);
9758 init
= integer_zero_node
;
9759 decl
= build_decl (input_location
,
9760 VAR_DECL
, get_identifier (string
), TREE_TYPE (init
));
9761 TREE_PUBLIC (decl
) = 1;
9762 TREE_READONLY (decl
) = 1;
9763 TREE_USED (decl
) = 1;
9764 TREE_CONSTANT (decl
) = 1;
9765 DECL_CONTEXT (decl
) = NULL_TREE
;
9766 DECL_ARTIFICIAL (decl
) = 1;
9767 TREE_STATIC (decl
) = 1;
9768 DECL_INITIAL (decl
) = error_mark_node
; /* A real initializer is coming... */
9769 /* We must force the reference. */
9770 DECL_PRESERVE_P (decl
) = 1;
9772 finish_var_decl(decl
, init
) ;
9776 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
9777 later requires that ObjC translation units participating in F&C be
9778 specially marked. The following routine accomplishes this. */
9780 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
9783 generate_objc_image_info (void)
9787 = ((flag_replace_objc_classes
&& imp_count
? 1 : 0)
9788 | (flag_objc_gc
? 2 : 0));
9789 VEC(constructor_elt
,gc
) *v
= NULL
;
9793 return; /* No need for an image_info entry. */
9795 array_type
= build_sized_array_type (integer_type_node
, 2);
9797 decl
= start_var_decl (array_type
, "_OBJC_IMAGE_INFO");
9799 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
9800 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (integer_type_node
, flags
));
9801 /* If we need this (determined above) it is because the runtime wants to
9802 refer to it in a manner hidden from the compiler. So we must force the
9804 DECL_PRESERVE_P (decl
) = 1;
9805 finish_var_decl (decl
, objc_build_constructor (TREE_TYPE (decl
), v
));
9808 /* Look up ID as an instance variable. OTHER contains the result of
9809 the C or C++ lookup, which we may want to use instead. */
9812 objc_lookup_ivar (tree other
, tree id
)
9816 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9817 if (!objc_method_context
)
9820 if (!strcmp (IDENTIFIER_POINTER (id
), "super"))
9821 /* We have a message to super. */
9822 return get_super_receiver ();
9824 /* In a class method, look up an instance variable only as a last
9826 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
9827 && other
&& other
!= error_mark_node
)
9830 /* Look up the ivar, but do not use it if it is not accessible. */
9831 ivar
= is_ivar (objc_ivar_chain
, id
);
9833 if (!ivar
|| is_private (ivar
))
9836 /* In an instance method, a local variable (or parameter) may hide the
9837 instance variable. */
9838 if (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
9839 && other
&& other
!= error_mark_node
9841 && CP_DECL_CONTEXT (other
) != global_namespace
)
9843 && !DECL_FILE_SCOPE_P (other
))
9846 warning (0, "local declaration of %qE hides instance variable",
9852 /* At this point, we are either in an instance method with no obscuring
9853 local definitions, or in a class method with no alternate definitions
9855 return build_ivar_reference (id
);
9858 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9859 needs to be done if we are calling a function through a cast. */
9862 objc_rewrite_function_call (tree function
, tree first_param
)
9864 if (TREE_CODE (function
) == NOP_EXPR
9865 && TREE_CODE (TREE_OPERAND (function
, 0)) == ADDR_EXPR
9866 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function
, 0), 0))
9869 function
= build3 (OBJ_TYPE_REF
, TREE_TYPE (function
),
9870 TREE_OPERAND (function
, 0),
9871 first_param
, size_zero_node
);
9877 /* Look for the special case of OBJC_TYPE_REF with the address of
9878 a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend or one
9882 objc_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
9884 enum gimplify_status r0
, r1
;
9885 if (TREE_CODE (*expr_p
) == OBJ_TYPE_REF
9886 && TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p
)) == ADDR_EXPR
9887 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p
), 0))
9890 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9891 value of the OBJ_TYPE_REF, so force them to be emitted
9892 during subexpression evaluation rather than after the
9893 OBJ_TYPE_REF. This permits objc_msgSend calls in Objective
9894 C to use direct rather than indirect calls when the
9895 object expression has a postincrement. */
9896 r0
= gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p
), pre_p
, NULL
,
9897 is_gimple_val
, fb_rvalue
);
9898 r1
= gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p
), pre_p
, post_p
,
9899 is_gimple_val
, fb_rvalue
);
9901 return MIN (r0
, r1
);
9905 return (enum gimplify_status
) cp_gimplify_expr (expr_p
, pre_p
, post_p
);
9907 return (enum gimplify_status
) c_gimplify_expr (expr_p
, pre_p
, post_p
);
9911 #include "gt-objc-objc-act.h"