1 /* Next Runtime (ABI-0/1) private.
2 Copyright (C) 2011-2015 Free Software Foundation, Inc.
3 Contributed by Iain Sandoe (split from objc-act.c)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This implements the original NeXT ABI (0) used for m32 code and
22 indicated by module version 6. It also implements the small number
23 of additions made for properties and optional protocol methods as
24 ABI=1 (module version 7). */
28 #include "coretypes.h"
33 #include "fold-const.h"
34 #include "stringpool.h"
37 #include "cp/cp-tree.h"
42 #include "langhooks.h"
43 #include "c-family/c-objc.h"
46 /* When building Objective-C++, we are not linking against the C
47 front-end and so need to replicate the C tree-construction
48 functions in some way. */
50 #define OBJCP_REMAP_FUNCTIONS
51 #include "objcp-decl.h"
55 #include "c-family/c-target.h"
56 #include "tree-iterator.h"
58 #include "objc-runtime-hooks.h"
59 #include "objc-runtime-shared-support.h"
60 #include "objc-encoding.h"
62 /* NeXT ABI 0 and 1 private definitions. */
63 #define DEF_CONSTANT_STRING_CLASS_NAME "NSConstantString"
65 #define TAG_GETCLASS "objc_getClass"
66 #define TAG_GETMETACLASS "objc_getMetaClass"
68 #define TAG_MSGSEND "objc_msgSend"
69 #define TAG_MSGSENDSUPER "objc_msgSendSuper"
70 #define TAG_MSGSEND_STRET "objc_msgSend_stret"
71 #define TAG_MSGSENDSUPER_STRET "objc_msgSendSuper_stret"
73 /* NeXT-specific tags. */
75 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
76 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
77 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
78 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
79 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
80 #define TAG_EXCEPTIONMATCH "objc_exception_match"
81 #define TAG_SETJMP "_setjmp"
83 #define TAG_ASSIGNIVAR "objc_assign_ivar"
84 #define TAG_ASSIGNGLOBAL "objc_assign_global"
85 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
87 /* Branch entry points. All that matters here are the addresses;
88 functions with these names do not really exist in libobjc. */
90 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
91 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
93 /* The version identifies which language generation and runtime the
94 module (file) was compiled for, and is recorded in the module
96 #define OBJC_VERSION (flag_objc_abi >= 1 ? 7 : 6)
98 #define UTAG_CLASS_EXT "_objc_class_ext"
99 #define UTAG_PROPERTY_LIST "_prop_list_t"
100 #define UTAG_PROTOCOL_EXT "_objc_protocol_extension"
102 #define CLS_HAS_CXX_STRUCTORS 0x2000L
104 /* rt_trees identifiers - shared between NeXT implementations. These
105 allow the FE to tag meta-data in a manner that survives LTO and can
106 be used when the runtime requires that certain meta-data items
107 appear in particular named sections. */
109 #include "objc-next-metadata-tags.h"
110 extern GTY(()) tree objc_rt_trees
[OCTI_RT_META_MAX
];
112 static void next_runtime_01_initialize (void);
114 static tree
next_runtime_abi_01_super_superclassfield_id (void);
116 static tree
next_runtime_abi_01_class_decl (tree
);
117 static tree
next_runtime_abi_01_metaclass_decl (tree
);
118 static tree
next_runtime_abi_01_category_decl (tree
);
119 static tree
next_runtime_abi_01_protocol_decl (tree
);
120 static tree
next_runtime_abi_01_string_decl (tree
, const char *, string_section
);
122 static tree
next_runtime_abi_01_get_class_reference (tree
);
123 static tree
next_runtime_abi_01_build_selector_reference (location_t
, tree
, tree
);
124 static tree
next_runtime_abi_01_get_protocol_reference (location_t
, tree
);
125 static tree
next_runtime_abi_01_build_ivar_ref (location_t
, tree
, tree
);
126 static tree
next_runtime_abi_01_get_class_super_ref (location_t
, struct imp_entry
*, bool);
127 static tree
next_runtime_abi_01_get_category_super_ref (location_t
, struct imp_entry
*, bool);
129 static tree
next_runtime_abi_01_receiver_is_class_object (tree
);
130 static void next_runtime_abi_01_get_arg_type_list_base (vec
<tree
, va_gc
> **,
132 static tree
next_runtime_abi_01_build_objc_method_call (location_t
, tree
, tree
,
133 tree
, tree
, tree
, int);
134 static bool next_runtime_abi_01_setup_const_string_class_decl (void);
135 static tree
next_runtime_abi_01_build_const_string_constructor (location_t
, tree
, int);
137 static void objc_generate_v1_next_metadata (void);
139 static void build_next_objc_exception_stuff (void);
140 static tree
objc_eh_runtime_type (tree type
);
141 static tree
objc_eh_personality (void);
142 static tree
build_throw_stmt (location_t
, tree
, bool);
143 static tree
objc_build_exc_ptr (struct objc_try_context
**);
144 static tree
begin_catch (struct objc_try_context
**, tree
, tree
, tree
, bool);
145 static void finish_catch (struct objc_try_context
**, tree
);
146 static tree
finish_try_stmt (struct objc_try_context
**);
149 objc_next_runtime_abi_01_init (objc_runtime_hooks
*rthooks
)
151 if (flag_objc_exceptions
152 && !flag_objc_sjlj_exceptions
)
154 warning_at (UNKNOWN_LOCATION
, OPT_Wall
,
155 "%<-fobjc-sjlj-exceptions%> is the only supported exceptions "
156 "system for %<-fnext-runtime%> with %<-fobjc-abi-version%> < 2");
159 rthooks
->initialize
= next_runtime_01_initialize
;
160 rthooks
->default_constant_string_class_name
= DEF_CONSTANT_STRING_CLASS_NAME
;
161 rthooks
->tag_getclass
= TAG_GETCLASS
;
162 rthooks
->super_superclassfield_ident
= next_runtime_abi_01_super_superclassfield_id
;
164 rthooks
->class_decl
= next_runtime_abi_01_class_decl
;
165 rthooks
->metaclass_decl
= next_runtime_abi_01_metaclass_decl
;
166 rthooks
->category_decl
= next_runtime_abi_01_category_decl
;
167 rthooks
->protocol_decl
= next_runtime_abi_01_protocol_decl
;
168 rthooks
->string_decl
= next_runtime_abi_01_string_decl
;
170 rthooks
->get_class_reference
= next_runtime_abi_01_get_class_reference
;
171 rthooks
->build_selector_reference
= next_runtime_abi_01_build_selector_reference
;
172 rthooks
->get_protocol_reference
= next_runtime_abi_01_get_protocol_reference
;
173 rthooks
->build_ivar_reference
= next_runtime_abi_01_build_ivar_ref
;
174 rthooks
->get_class_super_ref
= next_runtime_abi_01_get_class_super_ref
;
175 rthooks
->get_category_super_ref
= next_runtime_abi_01_get_category_super_ref
;
177 rthooks
->receiver_is_class_object
= next_runtime_abi_01_receiver_is_class_object
;
178 rthooks
->get_arg_type_list_base
= next_runtime_abi_01_get_arg_type_list_base
;
179 rthooks
->build_objc_method_call
= next_runtime_abi_01_build_objc_method_call
;
181 rthooks
->setup_const_string_class_decl
=
182 next_runtime_abi_01_setup_const_string_class_decl
;
183 rthooks
->build_const_string_constructor
=
184 next_runtime_abi_01_build_const_string_constructor
;
186 rthooks
->build_throw_stmt
= build_throw_stmt
;
187 rthooks
->build_exc_ptr
= objc_build_exc_ptr
;
188 rthooks
->begin_catch
= begin_catch
;
189 rthooks
->finish_catch
= finish_catch
;
190 rthooks
->finish_try_stmt
= finish_try_stmt
;
192 rthooks
->generate_metadata
= objc_generate_v1_next_metadata
;
196 /* We need a way to convey what kind of meta-data are represented by a
197 given variable, since each type is expected (by the runtime) to be
198 found in a specific named section. The solution must be usable
201 The scheme used for NeXT ABI 0/1 (partial matching of variable
202 names) is not satisfactory for LTO & ABI-2. We now tag ObjC
203 meta-data with identification attributes in the front end. The
204 back-end may choose to act on these as it requires. */
207 next_runtime_abi_01_init_metadata_attributes (void)
210 objc_meta
= get_identifier ("OBJC1META");
213 meta_base
= get_identifier ("V1_BASE");
215 meta_class
= get_identifier ("V1_CLAS");
216 meta_metaclass
= get_identifier ("V1_META");
217 meta_category
= get_identifier ("V1_CATG");
218 meta_protocol
= get_identifier ("V1_PROT");
220 meta_clac_vars
= get_identifier ("V1_CLCV");
221 meta_clai_vars
= get_identifier ("V1_CLIV");
223 meta_clac_meth
= get_identifier ("V1_CLCM");
224 meta_clai_meth
= get_identifier ("V1_CLIM");
225 meta_catc_meth
= get_identifier ("V1_CACM");
226 meta_cati_meth
= get_identifier ("V1_CAIM");
227 meta_proto_cls_meth
= get_identifier ("V1_PCLM");
228 meta_proto_nst_meth
= get_identifier ("V1_PNSM");
230 meta_clas_prot
= get_identifier ("V1_CLPR");
231 meta_catg_prot
= get_identifier ("V1_CAPR");
233 meta_class_reference
= get_identifier ("V1_CLRF");
234 meta_proto_ref
= get_identifier ("V1_PRFS");
235 meta_sel_refs
= get_identifier ("V1_SRFS");
237 meta_class_name
= get_identifier ("V1_CLSN");
238 meta_meth_name
= get_identifier ("V1_METN");
239 meta_meth_type
= get_identifier ("V1_METT");
240 meta_prop_name_attr
= get_identifier ("V1_STRG");
242 meta_modules
= get_identifier ("V1_MODU");
243 meta_symtab
= get_identifier ("V1_SYMT");
244 meta_info
= get_identifier ("V1_INFO");
246 meta_proplist
= get_identifier ("V1_PLST");
247 meta_protocol_extension
= get_identifier ("V1_PEXT");
248 meta_class_extension
= get_identifier ("V1_CEXT");
250 meta_const_str
= get_identifier ("V1_CSTR");
253 static void build_v1_class_template (void);
254 static void build_v1_category_template (void);
255 static void build_v1_protocol_template (void);
257 static void next_runtime_01_initialize (void)
262 /* For all NeXT objc ABIs -fobjc-call-cxx-cdtors is on by
264 if (!global_options_set
.x_flag_objc_call_cxx_cdtors
)
265 global_options
.x_flag_objc_call_cxx_cdtors
= 1;
268 /* Set up attributes to be attached to the meta-data so that they
269 will be placed in the correct sections. */
270 next_runtime_abi_01_init_metadata_attributes ();
272 if (flag_objc_abi
>= 1)
273 objc_prop_list_ptr
= build_pointer_type (xref_tag (RECORD_TYPE
,
274 get_identifier ("_prop_list_t")));
276 /* Declare type of selector-objects that represent an operation
278 /* `struct objc_selector *' */
279 objc_selector_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
280 get_identifier (TAG_SELECTOR
)));
282 build_v1_class_template ();
283 build_super_template ();
284 build_v1_protocol_template ();
285 build_v1_category_template ();
287 /* NB: In order to call one of the ..._stret (struct-returning)
288 functions, the function *MUST* first be cast to a signature that
289 corresponds to the actual ObjC method being invoked. This is
290 what is done by the build_objc_method_call() routine below. */
292 /* id objc_msgSend (id, SEL, ...); */
293 /* id objc_msgSendNonNil (id, SEL, ...); */
294 /* id objc_msgSend_stret (id, SEL, ...); */
295 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
296 type
= build_varargs_function_type_list (objc_object_type
,
301 umsg_decl
= add_builtin_function (TAG_MSGSEND
,
302 type
, 0, NOT_BUILT_IN
,
305 umsg_nonnil_decl
= add_builtin_function (TAG_MSGSEND_NONNIL
,
306 type
, 0, NOT_BUILT_IN
,
309 umsg_stret_decl
= add_builtin_function (TAG_MSGSEND_STRET
,
310 type
, 0, NOT_BUILT_IN
,
313 umsg_nonnil_stret_decl
= add_builtin_function (TAG_MSGSEND_NONNIL_STRET
,
314 type
, 0, NOT_BUILT_IN
,
317 /* These can throw, because the function that gets called can throw
318 in Obj-C++, or could itself call something that can throw even in
320 TREE_NOTHROW (umsg_decl
) = 0;
321 TREE_NOTHROW (umsg_nonnil_decl
) = 0;
322 TREE_NOTHROW (umsg_stret_decl
) = 0;
323 TREE_NOTHROW (umsg_nonnil_stret_decl
) = 0;
325 /* id objc_msgSend_Fast (id, SEL, ...)
326 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
327 #ifdef OFFS_MSGSEND_FAST
328 umsg_fast_decl
= add_builtin_function (TAG_MSGSEND_FAST
,
329 type
, 0, NOT_BUILT_IN
,
331 TREE_NOTHROW (umsg_fast_decl
) = 0;
332 DECL_ATTRIBUTES (umsg_fast_decl
)
333 = tree_cons (get_identifier ("hard_coded_address"),
334 build_int_cst (NULL_TREE
, OFFS_MSGSEND_FAST
),
337 /* No direct dispatch available. */
338 umsg_fast_decl
= umsg_decl
;
341 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
342 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
343 type
= build_varargs_function_type_list (objc_object_type
,
347 umsg_super_decl
= add_builtin_function (TAG_MSGSENDSUPER
,
348 type
, 0, NOT_BUILT_IN
,
350 umsg_super_stret_decl
= add_builtin_function (TAG_MSGSENDSUPER_STRET
,
351 type
, 0, NOT_BUILT_IN
, 0,
353 TREE_NOTHROW (umsg_super_decl
) = 0;
354 TREE_NOTHROW (umsg_super_stret_decl
) = 0;
356 type
= build_function_type_list (objc_object_type
,
357 const_string_type_node
,
360 /* id objc_getClass (const char *); */
362 = add_builtin_function (TAG_GETCLASS
, type
, 0, NOT_BUILT_IN
,
365 /* id objc_getMetaClass (const char *); */
366 objc_get_meta_class_decl
367 = add_builtin_function (TAG_GETMETACLASS
, type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
369 /* This is the type of all of the following functions
370 objc_copyStruct(). */
371 type
= build_function_type_list (void_type_node
,
378 /* Declare the following function:
380 objc_copyStruct (void *destination, const void *source,
381 ptrdiff_t size, BOOL is_atomic, BOOL has_strong); */
382 objc_copyStruct_decl
= add_builtin_function ("objc_copyStruct",
383 type
, 0, NOT_BUILT_IN
,
385 TREE_NOTHROW (objc_copyStruct_decl
) = 0;
386 objc_getPropertyStruct_decl
= NULL_TREE
;
387 objc_setPropertyStruct_decl
= NULL_TREE
;
389 build_next_objc_exception_stuff ();
390 if (flag_objc_exceptions
&& !flag_objc_sjlj_exceptions
)
391 using_eh_for_cleanups ();
392 lang_hooks
.eh_runtime_type
= objc_eh_runtime_type
;
393 lang_hooks
.eh_personality
= objc_eh_personality
;
396 /* --- templates --- */
398 /* struct _objc_class
400 struct _objc_class *isa;
401 struct _objc_class *super_class;
406 struct _objc_ivar_list *ivars;
407 struct _objc_method_list *methods;
408 struct objc_cache *cache;
409 struct _objc_protocol_list *protocols;
411 const char *ivar_layout;
412 struct _objc_class_ext *ext;
415 void *gc_object_type;
419 /* The 'sel_id' & 'gc_object_type' fields are not used by the NeXT
420 runtime. We generate them for ABI==0 to maintain backward binary
424 build_v1_class_template (void)
426 tree ptype
, decls
, *chain
= NULL
;
428 objc_class_template
= objc_start_struct (get_identifier (UTAG_CLASS
));
430 /* struct _objc_class *isa; */
431 decls
= add_field_decl (build_pointer_type (objc_class_template
),
434 /* struct _objc_class *super_class; */
435 add_field_decl (build_pointer_type (objc_class_template
),
436 "super_class", &chain
);
439 add_field_decl (string_type_node
, "name", &chain
);
442 add_field_decl (long_integer_type_node
, "version", &chain
);
445 add_field_decl (long_integer_type_node
, "info", &chain
);
447 /* long instance_size; */
448 add_field_decl (long_integer_type_node
, "instance_size", &chain
);
450 /* struct _objc_ivar_list *ivars; */
451 add_field_decl (objc_ivar_list_ptr
,"ivars", &chain
);
453 /* struct _objc_method_list *methods; */
454 add_field_decl (objc_method_list_ptr
, "methods", &chain
);
456 /* struct objc_cache *cache; */
457 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
458 get_identifier ("objc_cache")));
459 add_field_decl (ptype
, "cache", &chain
);
461 /* struct _objc_protocol **protocol_list; */
462 ptype
= build_pointer_type (build_pointer_type
463 (xref_tag (RECORD_TYPE
,
464 get_identifier (UTAG_PROTOCOL
))));
465 add_field_decl (ptype
, "protocol_list", &chain
);
467 if (flag_objc_abi
>= 1)
469 /* const char *ivar_layout; */
470 add_field_decl (const_string_type_node
, "ivar_layout", &chain
);
472 /* struct _objc_class_ext *ext; */
473 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
474 get_identifier (UTAG_CLASS_EXT
)));
475 add_field_decl (ptype
, "ext", &chain
);
480 add_field_decl (build_pointer_type (void_type_node
), "sel_id", &chain
);
481 /* void *gc_object_type; */
482 add_field_decl (build_pointer_type (void_type_node
), "gc_object_type",
486 objc_finish_struct (objc_class_template
, decls
);
489 /* struct _objc_category
493 struct _objc_method_list *instance_methods;
494 struct _objc_method_list *class_methods;
495 struct _objc_protocol_list *protocols;
497 uint32_t size; // sizeof (struct _objc_category)
498 struct _objc_property_list *instance_properties; // category's own @property decl.
503 build_v1_category_template (void)
505 tree ptype
, decls
, *chain
= NULL
;
507 objc_category_template
= objc_start_struct (get_identifier (UTAG_CATEGORY
));
509 /* char *category_name; */
510 decls
= add_field_decl (string_type_node
, "category_name", &chain
);
512 /* char *class_name; */
513 add_field_decl (string_type_node
, "class_name", &chain
);
515 /* struct _objc_method_list *instance_methods; */
516 add_field_decl (objc_method_list_ptr
, "instance_methods", &chain
);
518 /* struct _objc_method_list *class_methods; */
519 add_field_decl (objc_method_list_ptr
, "class_methods", &chain
);
521 /* struct _objc_protocol **protocol_list; */
522 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
523 add_field_decl (ptype
, "protocol_list", &chain
);
525 if (flag_objc_abi
>= 1)
527 add_field_decl (integer_type_node
, "size", &chain
);
529 /* struct _objc_property_list *instance_properties;
530 This field describes a category's @property declarations.
531 Properties from inherited protocols are not included. */
532 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
533 get_identifier (UTAG_PROPERTY_LIST
)));
534 add_field_decl (ptype
, "instance_properties", &chain
);
536 objc_finish_struct (objc_category_template
, decls
);
539 /* Begin code generation for protocols...
540 Modified for ObjC #1 extensions. */
542 /* struct _objc_protocol
545 struct _objc_protocol_extension *isa;
547 struct _objc_class *isa;
551 struct _objc_protocol **protocol_list;
552 struct _objc__method_prototype_list *instance_methods;
553 struct _objc__method_prototype_list *class_methods;
557 build_v1_protocol_template (void)
559 tree ptype
, decls
, *chain
= NULL
;
561 objc_protocol_template
= objc_start_struct (get_identifier (UTAG_PROTOCOL
));
563 if (flag_objc_abi
>= 1)
564 /* struct _objc_protocol_extension *isa; */
565 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
566 get_identifier (UTAG_PROTOCOL_EXT
)));
568 /* struct _objc_class *isa; */
569 ptype
= build_pointer_type (xref_tag (RECORD_TYPE
,
570 get_identifier (UTAG_CLASS
)));
572 decls
= add_field_decl (ptype
, "isa", &chain
);
574 /* char *protocol_name; */
575 add_field_decl (string_type_node
, "protocol_name", &chain
);
577 /* struct _objc_protocol **protocol_list; */
578 ptype
= build_pointer_type (build_pointer_type (objc_protocol_template
));
579 add_field_decl (ptype
, "protocol_list", &chain
);
581 /* struct _objc__method_prototype_list *instance_methods; */
582 add_field_decl (objc_method_proto_list_ptr
, "instance_methods", &chain
);
584 /* struct _objc__method_prototype_list *class_methods; */
585 add_field_decl (objc_method_proto_list_ptr
, "class_methods", &chain
);
587 objc_finish_struct (objc_protocol_template
, decls
);
590 /* --- names, decls identifiers --- */
593 next_runtime_abi_01_super_superclassfield_id (void)
595 if (!super_superclassfield_id
)
596 super_superclassfield_id
= get_identifier ("super_class");
597 return super_superclassfield_id
;
601 next_runtime_abi_01_class_decl (tree klass
)
605 snprintf (buf
, BUFSIZE
, "_OBJC_Class_%s",
606 IDENTIFIER_POINTER (CLASS_NAME (klass
)));
607 decl
= start_var_decl (objc_class_template
, buf
);
608 OBJCMETA (decl
, objc_meta
, meta_class
);
613 next_runtime_abi_01_metaclass_decl (tree klass
)
617 snprintf (buf
, BUFSIZE
, "_OBJC_MetaClass_%s",
618 IDENTIFIER_POINTER (CLASS_NAME (klass
)));
619 decl
= start_var_decl (objc_class_template
, buf
);
620 OBJCMETA (decl
, objc_meta
, meta_metaclass
);
625 next_runtime_abi_01_category_decl (tree klass
)
629 snprintf (buf
, BUFSIZE
, "_OBJC_Category_%s_on_%s",
630 IDENTIFIER_POINTER (CLASS_SUPER_NAME (klass
)),
631 IDENTIFIER_POINTER (CLASS_NAME (klass
)));
632 decl
= start_var_decl (objc_category_template
, buf
);
633 OBJCMETA (decl
, objc_meta
, meta_category
);
638 next_runtime_abi_01_protocol_decl (tree p
)
643 /* static struct _objc_protocol _OBJC_Protocol_<mumble>; */
645 snprintf (buf
, BUFSIZE
, "_OBJC_Protocol_%s",
646 IDENTIFIER_POINTER (PROTOCOL_NAME (p
)));
647 decl
= start_var_decl (objc_protocol_template
, buf
);
648 OBJCMETA (decl
, objc_meta
, meta_protocol
);
653 next_runtime_abi_01_string_decl (tree type
, const char *name
, string_section where
)
655 tree var
= start_var_decl (type
, name
);
659 OBJCMETA (var
, objc_meta
, meta_class_name
);
662 OBJCMETA (var
, objc_meta
, meta_meth_name
);
665 OBJCMETA (var
, objc_meta
, meta_meth_type
);
667 case prop_names_attr
:
668 OBJCMETA (var
, objc_meta
, meta_prop_name_attr
);
671 OBJCMETA (var
, objc_meta
, meta_base
);
679 static GTY(()) int class_reference_idx
;
682 build_class_reference_decl (void)
687 sprintf (buf
, "_OBJC_ClassRefs_%d", class_reference_idx
++);
688 decl
= start_var_decl (objc_class_type
, buf
);
694 next_runtime_abi_01_get_class_reference (tree ident
)
701 for (chain
= &cls_ref_chain
; *chain
; chain
= &TREE_CHAIN (*chain
))
702 if (TREE_VALUE (*chain
) == ident
)
704 if (! TREE_PURPOSE (*chain
))
705 TREE_PURPOSE (*chain
) = build_class_reference_decl ();
707 return TREE_PURPOSE (*chain
);
710 decl
= build_class_reference_decl ();
711 *chain
= tree_cons (decl
, ident
, NULL_TREE
);
718 add_class_reference (ident
);
720 params
= build_tree_list (NULL_TREE
,
721 my_build_string_pointer
722 (IDENTIFIER_LENGTH (ident
) + 1,
723 IDENTIFIER_POINTER (ident
)));
725 return build_function_call (input_location
, objc_get_class_decl
, params
);
729 /* Used by build_function_type_for_method. Append the types for
730 receiver & _cmd at the start of a method argument list to ARGTYPES.
731 CONTEXT is either METHOD_DEF or METHOD_REF, saying whether we are
732 trying to define a method or call one. SUPERFLAG says this is for a
733 send to super. METH may be NULL, in the case that there is no
737 next_runtime_abi_01_get_arg_type_list_base (vec
<tree
, va_gc
> **argtypes
,
738 tree meth
, int context
,
744 receiver_type
= objc_super_type
;
745 else if (context
== METHOD_DEF
&& TREE_CODE (meth
) == INSTANCE_METHOD_DECL
)
746 receiver_type
= objc_instance_type
;
748 receiver_type
= objc_object_type
;
750 vec_safe_push (*argtypes
, receiver_type
);
751 /* Selector type - will eventually change to `int'. */
752 vec_safe_push (*argtypes
, objc_selector_type
);
756 next_runtime_abi_01_receiver_is_class_object (tree receiver
)
758 if (TREE_CODE (receiver
) == VAR_DECL
759 && IS_CLASS (TREE_TYPE (receiver
)))
761 /* The receiver is a variable created by build_class_reference_decl. */
762 tree chain
= cls_ref_chain
;
763 /* Look up the identifier in the relevant chain. */
764 for (; chain
; chain
= TREE_CHAIN (chain
))
765 if (TREE_PURPOSE (chain
) == receiver
)
766 return TREE_VALUE (chain
);
772 build_selector_reference_decl (tree ident
)
775 char *t
, buf
[BUFSIZE
];
777 snprintf (buf
, BUFSIZE
, "_OBJC_SelRef_%s", IDENTIFIER_POINTER (ident
));
782 *t
= '$'; /* Underscore would clash between foo:bar and foo_bar. */
785 decl
= start_var_decl (objc_selector_type
, buf
);
786 OBJCMETA (decl
, objc_meta
, meta_sel_refs
);
791 next_runtime_abi_01_build_selector_reference (location_t loc ATTRIBUTE_UNUSED
,
793 tree proto ATTRIBUTE_UNUSED
)
795 tree
*chain
= &sel_ref_chain
;
800 if (TREE_VALUE (*chain
) == ident
)
801 return TREE_PURPOSE (*chain
);
803 chain
= &TREE_CHAIN (*chain
);
806 expr
= build_selector_reference_decl (ident
);
808 *chain
= tree_cons (expr
, ident
, NULL_TREE
);
813 /* Build a tree expression to send OBJECT the operation SELECTOR,
814 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
815 assuming the method has prototype METHOD_PROTOTYPE.
816 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
817 LOC is the location of the expression to build.
818 Use METHOD_PARAMS as list of args to pass to the method.
819 If SUPER_FLAG is nonzero, we look up the superclass's method. */
822 build_objc_method_call (location_t loc
, int super_flag
, tree method_prototype
,
823 tree lookup_object
, tree selector
,
826 tree sender
, sender_cast
, method
, t
;
827 tree rcv_p
= (super_flag
? objc_super_type
: objc_object_type
);
828 vec
<tree
, va_gc
> *parms
;
829 unsigned nparm
= (method_params
? list_length (method_params
) : 0);
831 /* If a prototype for the method to be called exists, then cast
832 the sender's return type and arguments to match that of the method.
833 Otherwise, leave sender as is. */
836 ? TREE_VALUE (TREE_TYPE (method_prototype
))
838 tree ftype
= build_function_type_for_method (ret_type
, method_prototype
,
839 METHOD_REF
, super_flag
);
841 if (method_prototype
&& METHOD_TYPE_ATTRIBUTES (method_prototype
))
842 ftype
= build_type_attribute_variant (ftype
,
843 METHOD_TYPE_ATTRIBUTES
846 sender_cast
= build_pointer_type (ftype
);
848 lookup_object
= build_c_cast (loc
, rcv_p
, lookup_object
);
850 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
851 lookup_object
= save_expr (lookup_object
);
853 /* Param list + 2 slots for object and selector. */
854 vec_alloc (parms
, nparm
+ 2);
856 /* If we are returning a struct in memory, and the address
857 of that memory location is passed as a hidden first
858 argument, then change which messenger entry point this
859 expr will call. NB: Note that sender_cast remains
860 unchanged (it already has a struct return type). */
861 if (!targetm
.calls
.struct_value_rtx (0, 0)
862 && (TREE_CODE (ret_type
) == RECORD_TYPE
863 || TREE_CODE (ret_type
) == UNION_TYPE
)
864 && targetm
.calls
.return_in_memory (ret_type
, 0))
865 sender
= (super_flag
? umsg_super_stret_decl
866 : flag_nil_receivers
? umsg_stret_decl
867 : umsg_nonnil_stret_decl
);
869 sender
= (super_flag
? umsg_super_decl
870 : (flag_nil_receivers
? (flag_objc_direct_dispatch
873 : umsg_nonnil_decl
));
874 method
= build_fold_addr_expr_loc (loc
, sender
);
876 /* Pass the object to the method. */
877 parms
->quick_push (lookup_object
);
878 /* Pass the selector to the method. */
879 parms
->quick_push (selector
);
880 /* Now append the remainder of the parms. */
882 for (; method_params
; method_params
= TREE_CHAIN (method_params
))
883 parms
->quick_push (TREE_VALUE (method_params
));
885 /* Build an obj_type_ref, with the correct cast for the method call. */
886 t
= build3 (OBJ_TYPE_REF
, sender_cast
, method
,
887 lookup_object
, size_zero_node
);
888 t
= build_function_call_vec (loc
, vNULL
, t
, parms
, NULL
);
894 next_runtime_abi_01_build_objc_method_call (location_t loc
,
895 tree method_prototype
,
897 tree rtype ATTRIBUTE_UNUSED
,
902 tree selector
= next_runtime_abi_01_build_selector_reference (loc
, sel_name
,
905 return build_objc_method_call (loc
, super
, method_prototype
,
906 receiver
, selector
, method_params
);
910 next_runtime_abi_01_get_protocol_reference (location_t loc
, tree p
)
914 if (!PROTOCOL_FORWARD_DECL (p
))
915 PROTOCOL_FORWARD_DECL (p
) = next_runtime_abi_01_protocol_decl (p
);
917 expr
= build_unary_op (loc
, ADDR_EXPR
, PROTOCOL_FORWARD_DECL (p
), 0);
918 return convert (objc_protocol_type
, expr
);
921 /* For ABI 0/1 and IVAR is just a fixed offset in the class struct. */
924 next_runtime_abi_01_build_ivar_ref (location_t loc ATTRIBUTE_UNUSED
,
927 return objc_build_component_ref (base
, id
);
930 /* We build super class references as we need them (but keep them once
931 built for the sake of efficiency). */
934 next_runtime_abi_01_get_class_super_ref (location_t loc ATTRIBUTE_UNUSED
,
935 struct imp_entry
*imp
, bool inst_meth
)
941 objc_build_component_ref (imp
->class_decl
,
942 get_identifier ("super_class"));
943 return ucls_super_ref
;
947 if (!uucls_super_ref
)
949 objc_build_component_ref (imp
->meta_decl
,
950 get_identifier ("super_class"));
951 return uucls_super_ref
;
956 next_runtime_abi_01_get_category_super_ref (location_t loc ATTRIBUTE_UNUSED
,
957 struct imp_entry
*imp
, bool inst_meth
)
959 tree super_name
= CLASS_SUPER_NAME (imp
->imp_template
);
964 super_class
= objc_get_class_reference (super_name
);
968 /* If we are in a class method, we must retrieve the
969 _metaclass_ for the current class, pointed at by
970 the class's "isa" pointer. The following assumes that
971 "isa" is the first ivar in a class (which it must be). */
973 build_indirect_ref (input_location
,
974 build_c_cast (input_location
,
975 build_pointer_type (objc_class_type
),
981 /* else do it the slow way. */
982 add_class_reference (super_name
);
983 super_class
= (inst_meth
? objc_get_class_decl
: objc_get_meta_class_decl
);
984 super_name
= my_build_string_pointer (IDENTIFIER_LENGTH (super_name
) + 1,
985 IDENTIFIER_POINTER (super_name
));
986 /* super_class = objc_get{Meta}Class("CLASS_SUPER_NAME"); */
987 return build_function_call (input_location
,
989 build_tree_list (NULL_TREE
, super_name
));
993 next_runtime_abi_01_setup_const_string_class_decl (void)
995 if (!constant_string_global_id
)
997 /* Hopefully, this should not represent a serious limitation. */
999 snprintf (buf
, BUFSIZE
, "_%sClassReference", constant_string_class_name
);
1000 constant_string_global_id
= get_identifier (buf
);
1003 string_class_decl
= lookup_name (constant_string_global_id
);
1005 return (string_class_decl
!= NULL_TREE
);
1009 next_runtime_abi_01_build_const_string_constructor (location_t loc
, tree string
,
1012 tree constructor
, fields
, var
;
1013 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1015 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
1016 fields
= TYPE_FIELDS (internal_const_str_type
);
1017 CONSTRUCTOR_APPEND_ELT (v
, fields
,
1018 build_unary_op (loc
, ADDR_EXPR
, string_class_decl
, 0));
1020 fields
= DECL_CHAIN (fields
);
1021 CONSTRUCTOR_APPEND_ELT (v
, fields
,
1022 build_unary_op (loc
, ADDR_EXPR
, string
, 1));
1024 /* ??? check if this should be long. */
1025 fields
= DECL_CHAIN (fields
);
1026 CONSTRUCTOR_APPEND_ELT (v
, fields
, build_int_cst (NULL_TREE
, length
));
1027 constructor
= objc_build_constructor (internal_const_str_type
, v
);
1029 var
= build_decl (input_location
, CONST_DECL
, NULL
, TREE_TYPE (constructor
));
1030 DECL_INITIAL (var
) = constructor
;
1031 TREE_STATIC (var
) = 1;
1032 DECL_CONTEXT (var
) = NULL
;
1033 OBJCMETA (var
, objc_meta
, meta_const_str
);
1037 /* --- metadata templates --- */
1039 /* This routine builds the following type:
1041 const char * const name; // property name
1042 const char * const attributes; // comma-delimited, encoded,
1043 // property attributes
1047 static GTY(()) tree objc_v1_property_template
;
1050 build_v1_property_template (void)
1053 tree decls
, *chain
= NULL
;
1055 prop_record
= objc_start_struct (get_identifier ("_prop_t"));
1056 /* const char * name */
1057 decls
= add_field_decl (string_type_node
, "name", &chain
);
1059 /* const char * attribute */
1060 add_field_decl (string_type_node
, "attribute", &chain
);
1062 objc_finish_struct (prop_record
, decls
);
1066 /* Build the following type:
1068 struct _objc_protocol_extension
1070 uint32_t size; // sizeof (struct _objc_protocol_extension)
1071 struct objc_method_list *optional_instance_methods;
1072 struct objc_method_list *optional_class_methods;
1073 struct objc_prop_list *instance_properties;
1077 static GTY(()) tree objc_protocol_extension_template
;
1080 build_v1_objc_protocol_extension_template (void)
1082 tree decls
, *chain
= NULL
;
1084 objc_protocol_extension_template
=
1085 objc_start_struct (get_identifier (UTAG_PROTOCOL_EXT
));
1087 /* uint32_t size; */
1088 decls
= add_field_decl (integer_type_node
, "size", &chain
);
1090 /* struct objc_method_list *optional_instance_methods; */
1091 add_field_decl (objc_method_list_ptr
, "optional_instance_methods", &chain
);
1093 /* struct objc_method_list *optional_class_methods; */
1094 add_field_decl (objc_method_list_ptr
, "optional_class_methods", &chain
);
1096 /* struct objc_prop_list *instance_properties; */
1097 add_field_decl (objc_prop_list_ptr
, "instance_properties", &chain
);
1099 objc_finish_struct (objc_protocol_extension_template
, decls
);
1102 /* This routine build following struct type:
1103 struct _objc_class_ext
1105 uint32_t size; // sizeof(struct _objc_class_ext)
1106 const char *weak_ivar_layout;
1107 struct _prop_list_t *properties;
1111 static GTY(()) tree objc_class_ext_template
;
1114 build_objc_class_ext_template (void)
1116 tree ptrt
, decls
, *chain
= NULL
;
1118 objc_class_ext_template
= objc_start_struct (get_identifier (UTAG_CLASS_EXT
));
1120 /* uint32_t size; */
1121 decls
= add_field_decl (integer_type_node
, "size", &chain
);
1123 /* const char *weak_ivar_layout; */
1124 add_field_decl (const_string_type_node
, "weak_ivar_layout", &chain
);
1126 /* struct _prop_list_t *properties; */
1127 ptrt
= build_pointer_type (xref_tag (RECORD_TYPE
,
1128 get_identifier(UTAG_PROPERTY_LIST
)));
1129 add_field_decl (ptrt
, "properties", &chain
);
1131 objc_finish_struct (objc_class_ext_template
, decls
);
1135 build_metadata_templates (void)
1138 if (!objc_method_template
)
1139 objc_method_template
= build_method_template ();
1145 /* --- emit metadata --- */
1148 generate_v1_meth_descriptor_table (tree chain
, tree protocol
,
1149 const char *prefix
, tree attr
)
1151 tree method_list_template
, initlist
, decl
;
1153 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1156 if (!chain
|| !prefix
)
1159 if (!objc_method_prototype_template
)
1160 objc_method_prototype_template
= build_method_prototype_template ();
1162 size
= list_length (chain
);
1163 method_list_template
=
1164 build_method_prototype_list_template (objc_method_prototype_template
,
1166 snprintf (buf
, BUFSIZE
, "%s_%s", prefix
,
1167 IDENTIFIER_POINTER (PROTOCOL_NAME (protocol
)));
1169 decl
= start_var_decl (method_list_template
, buf
);
1171 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
1173 build_descriptor_table_initializer (objc_method_prototype_template
,
1175 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, initlist
);
1176 /* Get into the right section. */
1177 OBJCMETA (decl
, objc_meta
, attr
);
1178 finish_var_decl (decl
, objc_build_constructor (method_list_template
, v
));
1182 /* Build protocol ext =
1183 {size, opt_instance_meth, opt_class_meth, instance_props};
1184 or NULL_TREE if none are present. */
1187 generate_v1_objc_protocol_extension (tree proto_interface
,
1188 tree opt_instance_meth
,
1189 tree opt_class_meth
,
1190 tree instance_props
)
1194 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1198 /* If there are no extensions, then don't bother... */
1199 if (!opt_instance_meth
&& !opt_class_meth
&& !instance_props
)
1202 if (!objc_protocol_extension_template
)
1203 build_v1_objc_protocol_extension_template ();
1206 size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (objc_protocol_extension_template
));
1207 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
1209 /* Try for meaningful diagnostics. */
1210 loc
= DECL_SOURCE_LOCATION (PROTOCOL_FORWARD_DECL (proto_interface
));
1212 /* struct objc_method_list *optional_instance_methods; */
1213 if (opt_instance_meth
)
1214 expr
= convert (objc_method_list_ptr
,
1215 build_unary_op (loc
, ADDR_EXPR
, opt_instance_meth
, 0));
1217 expr
= convert (objc_method_list_ptr
, null_pointer_node
);
1219 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1221 /* struct objc_method_list *optional_class_methods; */
1223 expr
= convert (objc_method_list_ptr
,
1224 build_unary_op (loc
, ADDR_EXPR
, opt_class_meth
, 0));
1226 expr
= convert (objc_method_list_ptr
, null_pointer_node
);
1228 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1229 /* struct objc_prop_list *instance_properties; */
1231 expr
= convert (objc_prop_list_ptr
,
1232 build_unary_op (loc
, ADDR_EXPR
, instance_props
, 0));
1234 expr
= convert (objc_prop_list_ptr
, null_pointer_node
);
1236 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1237 snprintf (buf
, BUFSIZE
, "_OBJC_ProtocolExt_%s",
1238 IDENTIFIER_POINTER (PROTOCOL_NAME (proto_interface
)));
1240 decl
= start_var_decl (objc_protocol_extension_template
, buf
);
1241 expr
= objc_build_constructor (TREE_TYPE (decl
), v
);
1242 OBJCMETA (decl
, objc_meta
, meta_protocol_extension
);
1243 finish_var_decl (decl
, expr
);
1247 /* This routine builds the following type:
1248 struct _prop_list_t {
1249 uint32_t entsize; // sizeof (struct _prop_t)
1250 uint32_t prop_count;
1251 struct _prop_t prop_list [prop_count];
1256 build_v1_property_list_template (tree list_type
, int size
)
1258 tree property_list_t_record
;
1259 tree array_type
, decls
, *chain
= NULL
;
1262 property_list_t_record
= objc_start_struct (NULL_TREE
);
1264 /* uint32_t const entsize */
1265 decls
= add_field_decl (integer_type_node
, "entsize", &chain
);
1267 /* int prop_count */
1268 add_field_decl (integer_type_node
, "prop_count", &chain
);
1270 /* struct _prop_t prop_list[]; */
1271 array_type
= build_sized_array_type (list_type
, size
);
1272 add_field_decl (array_type
, "prop_list", &chain
);
1274 objc_finish_struct (property_list_t_record
, decls
);
1275 return property_list_t_record
;
1278 /* This routine builds the initializer list to initialize the
1279 'struct _prop_t prop_list[]' field of 'struct _prop_list_t' meta-data. */
1282 build_v1_property_table_initializer (tree type
, tree context
)
1285 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
1287 if (TREE_CODE (context
) == PROTOCOL_INTERFACE_TYPE
)
1288 x
= CLASS_PROPERTY_DECL (context
);
1290 x
= IMPL_PROPERTY_DECL (context
);
1292 for (; x
; x
= TREE_CHAIN (x
))
1294 vec
<constructor_elt
, va_gc
> *elemlist
= NULL
;
1295 tree attribute
, name_ident
= PROPERTY_NAME (x
);
1297 CONSTRUCTOR_APPEND_ELT (elemlist
, NULL_TREE
,
1298 add_objc_string (name_ident
, prop_names_attr
));
1300 attribute
= objc_v2_encode_prop_attr (x
);
1301 CONSTRUCTOR_APPEND_ELT (elemlist
, NULL_TREE
,
1302 add_objc_string (attribute
, prop_names_attr
));
1304 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
,
1305 objc_build_constructor (type
, elemlist
));
1308 return objc_build_constructor (build_array_type (type
, 0),inits
);
1311 /* This routine builds the 'struct _prop_list_t' variable declaration and
1312 initializes it with its initializer list. TYPE is 'struct _prop_list_t',
1313 NAME is the internal name of this variable, SIZE is number of properties
1314 for this class and LIST is the initializer list for its 'prop_list' field. */
1317 generate_v1_property_table (tree context
, tree klass_ctxt
)
1319 tree x
, decl
, initlist
, property_list_template
;
1320 bool is_proto
= false;
1321 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
1322 int init_val
, size
= 0;
1327 gcc_assert (TREE_CODE (context
) == PROTOCOL_INTERFACE_TYPE
);
1328 x
= CLASS_PROPERTY_DECL (context
);
1332 x
= IMPL_PROPERTY_DECL (klass_ctxt
);
1334 for (; x
; x
= TREE_CHAIN (x
))
1340 if (!objc_v1_property_template
)
1341 objc_v1_property_template
= build_v1_property_template ();
1343 property_list_template
=
1344 build_v1_property_list_template (objc_v1_property_template
,
1346 initlist
= build_v1_property_table_initializer (objc_v1_property_template
,
1350 init_val
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (objc_v1_property_template
));
1352 snprintf (buf
, BUFSIZE
, "_OBJC_ProtocolPropList_%s",
1353 IDENTIFIER_POINTER (PROTOCOL_NAME (context
)));
1355 snprintf (buf
, BUFSIZE
, "_OBJC_ClassPropList_%s",
1356 IDENTIFIER_POINTER (CLASS_NAME (klass_ctxt
)));
1358 decl
= start_var_decl (property_list_template
, buf
);
1359 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, init_val
));
1360 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
1361 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, initlist
);
1362 x
= objc_build_constructor (TREE_TYPE (decl
), inits
);
1363 OBJCMETA (decl
, objc_meta
, meta_proplist
);
1364 finish_var_decl (decl
, x
);
1369 generate_v1_protocol_list (tree i_or_p
, tree klass_ctxt
)
1371 tree array_type
, ptype
, refs_decl
, lproto
, e
, plist
, attr
;
1373 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1376 switch (TREE_CODE (i_or_p
))
1378 case CLASS_INTERFACE_TYPE
:
1379 case CATEGORY_INTERFACE_TYPE
:
1380 plist
= CLASS_PROTOCOL_LIST (i_or_p
);
1382 case PROTOCOL_INTERFACE_TYPE
:
1383 plist
= PROTOCOL_LIST (i_or_p
);
1390 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
1391 if (TREE_CODE (TREE_VALUE (lproto
)) == PROTOCOL_INTERFACE_TYPE
1392 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto
)))
1395 /* Build initializer. */
1396 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, 0));
1397 e
= build_int_cst (build_pointer_type (objc_protocol_template
), size
);
1398 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
1400 for (lproto
= plist
; lproto
; lproto
= TREE_CHAIN (lproto
))
1402 tree pval
= TREE_VALUE (lproto
);
1404 if (TREE_CODE (pval
) == PROTOCOL_INTERFACE_TYPE
1405 && PROTOCOL_FORWARD_DECL (pval
))
1407 tree fwref
= PROTOCOL_FORWARD_DECL (pval
);
1408 location_t loc
= DECL_SOURCE_LOCATION (fwref
) ;
1409 e
= build_unary_op (loc
, ADDR_EXPR
, fwref
, 0);
1410 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, e
);
1414 /* static struct objc_protocol *refs[n]; */
1415 switch (TREE_CODE (i_or_p
))
1417 case PROTOCOL_INTERFACE_TYPE
:
1418 snprintf (buf
, BUFSIZE
, "_OBJC_ProtocolRefs_%s",
1419 IDENTIFIER_POINTER (PROTOCOL_NAME (i_or_p
)));
1420 attr
= meta_proto_ref
;
1422 case CLASS_INTERFACE_TYPE
:
1423 snprintf (buf
, BUFSIZE
, "_OBJC_ClassProtocols_%s",
1424 IDENTIFIER_POINTER (CLASS_NAME (i_or_p
)));
1425 attr
= meta_clas_prot
;
1427 case CATEGORY_INTERFACE_TYPE
:
1428 snprintf (buf
, BUFSIZE
, "_OBJC_CategoryProtocols_%s_%s",
1429 IDENTIFIER_POINTER (CLASS_NAME (klass_ctxt
)),
1430 IDENTIFIER_POINTER (CLASS_SUPER_NAME (klass_ctxt
)));
1431 attr
= meta_catg_prot
;
1437 ptype
= build_pointer_type (objc_protocol_template
);
1438 array_type
= build_sized_array_type (ptype
, size
+ 3);
1439 refs_decl
= start_var_decl (array_type
, buf
);
1441 OBJCMETA (refs_decl
, objc_meta
, attr
);
1442 finish_var_decl (refs_decl
,
1443 objc_build_constructor (TREE_TYPE (refs_decl
), v
));
1449 build_v1_protocol_initializer (tree type
, tree protocol_name
, tree protocol_list
,
1450 tree inst_methods
, tree class_methods
,
1455 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
1457 if (!objc_protocol_extension_template
)
1458 build_v1_objc_protocol_extension_template ();
1460 /* TODO: find a better representation of location from the inputs. */
1461 loc
= UNKNOWN_LOCATION
;
1462 ttyp
= build_pointer_type (objc_protocol_extension_template
);
1463 /* Instead of jamming the protocol version number into the isa, we pass
1464 either a pointer to the protocol extension - or NULL. */
1466 expr
= convert (ttyp
, build_unary_op (loc
, ADDR_EXPR
, protocol_ext
, 0));
1468 expr
= convert (ttyp
, null_pointer_node
);
1470 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
1471 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_name
);
1472 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, protocol_list
);
1474 ttyp
= objc_method_proto_list_ptr
;
1476 expr
= convert (ttyp
, build_unary_op (loc
, ADDR_EXPR
, inst_methods
, 0));
1478 expr
= convert (ttyp
, null_pointer_node
);
1479 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
1482 expr
= convert (ttyp
, build_unary_op (loc
, ADDR_EXPR
, class_methods
, 0));
1484 expr
= convert (ttyp
, null_pointer_node
);
1485 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, expr
);
1487 return objc_build_constructor (type
, inits
);
1490 /* An updated version of generate_protocols () that emit the protocol
1491 extension for ABI=1. */
1493 /* For each protocol which was referenced either from a @protocol()
1494 expression, or because a class/category implements it (then a
1495 pointer to the protocol is stored in the struct describing the
1496 class/category), we create a statically allocated instance of the
1497 Protocol class. The code is written in such a way as to generate
1498 as few Protocol objects as possible; we generate a unique Protocol
1499 instance for each protocol, and we don't generate a Protocol
1500 instance if the protocol is never referenced (either from a
1501 @protocol() or from a class/category implementation). These
1502 statically allocated objects can be referred to via the static
1503 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
1505 The statically allocated Protocol objects that we generate here
1506 need to be fixed up at runtime in order to be used: the 'isa'
1507 pointer of the objects need to be set up to point to the 'Protocol'
1508 class, as known at runtime.
1510 The NeXT runtime fixes up all protocols at program startup time,
1511 before main() is entered. It uses a low-level trick to look up all
1512 those symbols, then loops on them and fixes them up. */
1514 /* TODO: finish getting rid of passing stuff around in globals. */
1516 static GTY(()) tree V1_Protocol_OPT_NST_METHODS_decl
;
1517 static GTY(()) tree V1_Protocol_OPT_CLS_METHODS_decl
;
1518 static GTY(()) tree V1_ProtocolExt_decl
;
1519 static GTY(()) tree V1_Property_decl
;
1522 generate_v1_protocols (void)
1526 /* If a protocol was directly referenced, pull in indirect references. */
1527 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
1528 if (PROTOCOL_FORWARD_DECL (p
) && PROTOCOL_LIST (p
))
1529 generate_protocol_references (PROTOCOL_LIST (p
));
1531 for (p
= protocol_chain
; p
; p
= TREE_CHAIN (p
))
1533 tree decl
, encoding
, initlist
, protocol_name_expr
;
1534 tree refs_type
, refs_decl
, refs_expr
;
1536 tree nst_methods
= PROTOCOL_NST_METHODS (p
);
1537 tree cls_methods
= PROTOCOL_CLS_METHODS (p
);
1539 /* If protocol wasn't referenced, don't generate any code. */
1540 decl
= PROTOCOL_FORWARD_DECL (p
);
1545 /* Make sure we link in the Protocol class. */
1546 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME
));
1550 if (! METHOD_ENCODING (nst_methods
))
1552 encoding
= encode_method_prototype (nst_methods
);
1553 METHOD_ENCODING (nst_methods
) = encoding
;
1555 nst_methods
= TREE_CHAIN (nst_methods
);
1558 UOBJC_INSTANCE_METHODS_decl
=
1559 generate_v1_meth_descriptor_table (PROTOCOL_NST_METHODS (p
), p
,
1560 "_OBJC_ProtocolInstanceMethods",
1561 meta_proto_nst_meth
);
1565 if (! METHOD_ENCODING (cls_methods
))
1567 encoding
= encode_method_prototype (cls_methods
);
1568 METHOD_ENCODING (cls_methods
) = encoding
;
1571 cls_methods
= TREE_CHAIN (cls_methods
);
1574 UOBJC_CLASS_METHODS_decl
=
1575 generate_v1_meth_descriptor_table (PROTOCOL_CLS_METHODS (p
), p
,
1576 "_OBJC_ProtocolClassMethods",
1577 meta_proto_cls_meth
);
1579 /* There should be no optional methods for ABI-0 - but we need to
1580 check all this here before the lists are made. */
1581 nst_methods
= PROTOCOL_OPTIONAL_NST_METHODS (p
);
1584 if (! METHOD_ENCODING (nst_methods
))
1586 encoding
= encode_method_prototype (nst_methods
);
1587 METHOD_ENCODING (nst_methods
) = encoding
;
1589 nst_methods
= TREE_CHAIN (nst_methods
);
1592 V1_Protocol_OPT_NST_METHODS_decl
=
1593 generate_v1_meth_descriptor_table (PROTOCOL_OPTIONAL_NST_METHODS (p
), p
,
1594 "_OBJC_OptionalProtocolInstanceMethods",
1595 meta_proto_nst_meth
);
1597 cls_methods
= PROTOCOL_OPTIONAL_CLS_METHODS (p
);
1600 if (! METHOD_ENCODING (cls_methods
))
1602 encoding
= encode_method_prototype (cls_methods
);
1603 METHOD_ENCODING (cls_methods
) = encoding
;
1606 cls_methods
= TREE_CHAIN (cls_methods
);
1609 V1_Protocol_OPT_CLS_METHODS_decl
=
1610 generate_v1_meth_descriptor_table (PROTOCOL_OPTIONAL_CLS_METHODS (p
), p
,
1611 "_OBJC_OptionalProtocolClassMethods",
1612 meta_proto_cls_meth
);
1614 if (PROTOCOL_LIST (p
))
1615 refs_decl
= generate_v1_protocol_list (p
, objc_implementation_context
);
1619 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
1620 protocol_name_expr
= add_objc_string (PROTOCOL_NAME (p
), class_names
);
1621 /* TODO: more locations to be fixed up... */
1622 loc
= UNKNOWN_LOCATION
;
1624 build_pointer_type (build_pointer_type (objc_protocol_template
));
1626 refs_expr
= convert (refs_type
,
1627 build_unary_op (loc
, ADDR_EXPR
, refs_decl
, 0));
1629 refs_expr
= convert (refs_type
, null_pointer_node
);
1631 if (flag_objc_abi
< 1)
1635 build_protocol_initializer (TREE_TYPE (decl
),
1636 protocol_name_expr
, refs_expr
,
1637 UOBJC_INSTANCE_METHODS_decl
,
1638 UOBJC_CLASS_METHODS_decl
);
1639 finish_var_decl (decl
, initlist
);
1643 /* else - V1 extensions. */
1646 generate_v1_property_table (p
, NULL_TREE
);
1648 V1_ProtocolExt_decl
=
1649 generate_v1_objc_protocol_extension (p
,
1650 V1_Protocol_OPT_NST_METHODS_decl
,
1651 V1_Protocol_OPT_CLS_METHODS_decl
,
1654 initlist
= build_v1_protocol_initializer (TREE_TYPE (decl
),
1655 protocol_name_expr
, refs_expr
,
1656 UOBJC_INSTANCE_METHODS_decl
,
1657 UOBJC_CLASS_METHODS_decl
,
1658 V1_ProtocolExt_decl
);
1659 finish_var_decl (decl
, initlist
);
1664 generate_dispatch_table (tree chain
, const char *name
, tree attr
)
1666 tree decl
, method_list_template
, initlist
;
1667 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1670 if (!chain
|| !name
|| !(size
= list_length (chain
)))
1673 if (!objc_method_template
)
1674 objc_method_template
= build_method_template ();
1676 method_list_template
= build_method_list_template (objc_method_template
,
1678 initlist
= build_dispatch_table_initializer (objc_method_template
, chain
);
1680 decl
= start_var_decl (method_list_template
, name
);
1682 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, integer_zero_node
);
1683 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1684 build_int_cst (integer_type_node
, size
));
1685 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, initlist
);
1687 OBJCMETA (decl
, objc_meta
, attr
);
1688 finish_var_decl (decl
,
1689 objc_build_constructor (TREE_TYPE (decl
), v
));
1694 /* Init a category. */
1696 build_v1_category_initializer (tree type
, tree cat_name
, tree class_name
,
1697 tree inst_methods
, tree class_methods
,
1698 tree protocol_list
, tree property_list
,
1702 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1704 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, cat_name
);
1705 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, class_name
);
1707 ltyp
= objc_method_list_ptr
;
1709 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, inst_methods
, 0));
1711 expr
= convert (ltyp
, null_pointer_node
);
1712 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1715 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, class_methods
, 0));
1717 expr
= convert (ltyp
, null_pointer_node
);
1718 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1720 /* protocol_list = */
1721 ltyp
= build_pointer_type (build_pointer_type (objc_protocol_template
));
1723 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, protocol_list
, 0));
1725 expr
= convert (ltyp
, null_pointer_node
);
1726 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1728 if (flag_objc_abi
>= 1)
1730 int val
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (objc_category_template
));
1731 expr
= build_int_cst (NULL_TREE
, val
);
1732 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1733 ltyp
= objc_prop_list_ptr
;
1735 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, property_list
, 0));
1737 expr
= convert (ltyp
, null_pointer_node
);
1738 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1741 return objc_build_constructor (type
, v
);
1744 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
1745 /* TODO: get rid of passing stuff around in globals. */
1747 generate_v1_category (struct imp_entry
*impent
)
1749 tree initlist
, cat_name_expr
, class_name_expr
;
1750 tree protocol_decl
, category
, cat_decl
;
1751 tree inst_methods
= NULL_TREE
, class_methods
= NULL_TREE
;
1752 tree cat
= impent
->imp_context
;
1756 cat_decl
= impent
->class_decl
;
1757 loc
= DECL_SOURCE_LOCATION (cat_decl
);
1759 add_class_reference (CLASS_NAME (cat
));
1760 cat_name_expr
= add_objc_string (CLASS_SUPER_NAME (cat
), class_names
);
1761 class_name_expr
= add_objc_string (CLASS_NAME (cat
), class_names
);
1763 category
= lookup_category (impent
->imp_template
, CLASS_SUPER_NAME (cat
));
1765 if (category
&& CLASS_PROTOCOL_LIST (category
))
1767 generate_protocol_references (CLASS_PROTOCOL_LIST (category
));
1768 protocol_decl
= generate_v1_protocol_list (category
, cat
);
1773 if (flag_objc_abi
>= 1)
1774 V1_Property_decl
= generate_v1_property_table (NULL_TREE
, cat
);
1776 V1_Property_decl
= NULL_TREE
;
1778 if (CLASS_NST_METHODS (cat
))
1780 snprintf (buf
, BUFSIZE
, "_OBJC_CategoryInstanceMethods_%s_%s",
1781 IDENTIFIER_POINTER (CLASS_NAME (cat
)),
1782 IDENTIFIER_POINTER (CLASS_SUPER_NAME (cat
)));
1783 inst_methods
= generate_dispatch_table (CLASS_NST_METHODS (cat
), buf
,
1787 if (CLASS_CLS_METHODS (cat
))
1789 snprintf (buf
, BUFSIZE
, "_OBJC_CategoryClassMethods_%s_%s",
1790 IDENTIFIER_POINTER (CLASS_NAME (cat
)),
1791 IDENTIFIER_POINTER (CLASS_SUPER_NAME (cat
)));
1792 class_methods
= generate_dispatch_table (CLASS_CLS_METHODS (cat
), buf
,
1796 initlist
= build_v1_category_initializer (TREE_TYPE (cat_decl
),
1797 cat_name_expr
, class_name_expr
,
1798 inst_methods
, class_methods
,
1799 protocol_decl
, V1_Property_decl
,
1802 finish_var_decl (cat_decl
, initlist
);
1803 impent
->class_decl
= cat_decl
;
1806 /* This routine builds the class extension used by v1 NeXT. */
1809 generate_objc_class_ext (tree property_list
, tree context
)
1811 tree decl
, expr
, ltyp
;
1812 tree weak_ivar_layout_tree
;
1815 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1818 /* TODO: pass the loc in or find it from args. */
1819 loc
= UNKNOWN_LOCATION
;
1821 /* const char *weak_ivar_layout
1822 TODO: Figure the ivar layouts out... */
1823 weak_ivar_layout_tree
= NULL_TREE
;
1825 if (!property_list
&& !weak_ivar_layout_tree
)
1828 if (!objc_class_ext_template
)
1829 build_objc_class_ext_template ();
1832 size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (objc_class_ext_template
));
1833 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
1835 ltyp
= const_string_type_node
;
1836 if (weak_ivar_layout_tree
)
1837 expr
= convert (ltyp
, weak_ivar_layout_tree
);
1839 expr
= convert (ltyp
, null_pointer_node
);
1840 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1842 /* struct _prop_list_t *properties; */
1843 ltyp
= objc_prop_list_ptr
;
1845 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, property_list
, 0));
1847 expr
= convert (ltyp
, null_pointer_node
);
1848 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1850 snprintf (buf
, BUFSIZE
, "_OBJC_ClassExt_%s",
1851 IDENTIFIER_POINTER (CLASS_NAME (context
)));
1852 decl
= start_var_decl (objc_class_ext_template
, buf
);
1853 expr
= objc_build_constructor (TREE_TYPE (decl
), v
);
1854 OBJCMETA (decl
, objc_meta
, meta_class_extension
);
1855 finish_var_decl (decl
, expr
);
1859 /* struct _objc_class {
1860 struct objc_class *isa;
1861 struct objc_class *super_class;
1866 struct objc_ivar_list *ivars;
1867 struct objc_method_list *methods;
1868 struct objc_cache *cache;
1869 struct objc_protocol_list *protocols;
1871 const char *ivar_layout;
1872 struct _objc_class_ext *ext;
1875 void *gc_object_type;
1880 build_v1_shared_structure_initializer (tree type
, tree isa
, tree super
,
1881 tree name
, tree size
, int status
,
1882 tree dispatch_table
, tree ivar_list
,
1883 tree protocol_list
, tree class_ext
)
1887 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1889 /* TODO: fish the location out of the input data. */
1890 loc
= UNKNOWN_LOCATION
;
1893 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, isa
);
1896 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, super
);
1899 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, default_conversion (name
));
1902 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1903 build_int_cst (long_integer_type_node
, 0));
1906 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1907 build_int_cst (long_integer_type_node
, status
));
1909 /* instance_size = */
1910 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1911 convert (long_integer_type_node
, size
));
1913 /* objc_ivar_list = */
1914 ltyp
= objc_ivar_list_ptr
;
1916 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, ivar_list
, 0));
1918 expr
= convert (ltyp
, null_pointer_node
);
1919 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1921 /* objc_method_list = */
1922 ltyp
= objc_method_list_ptr
;
1924 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, dispatch_table
, 0));
1926 expr
= convert (ltyp
, null_pointer_node
);
1927 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1929 ltyp
= build_pointer_type (xref_tag (RECORD_TYPE
,
1930 get_identifier ("objc_cache")));
1931 /* method_cache = */
1932 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, convert (ltyp
, null_pointer_node
));
1934 /* protocol_list = */
1935 ltyp
= build_pointer_type (build_pointer_type (objc_protocol_template
));
1937 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, protocol_list
, 0));
1939 expr
= convert (ltyp
, null_pointer_node
);
1940 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1942 if (flag_objc_abi
>= 1)
1944 /* TODO: figure out the ivar_layout stuff. */
1945 expr
= convert (const_string_type_node
, null_pointer_node
);
1946 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1947 if (!objc_class_ext_template
)
1948 build_objc_class_ext_template ();
1949 ltyp
= build_pointer_type (objc_class_ext_template
);
1951 expr
= convert (ltyp
, build_unary_op (loc
, ADDR_EXPR
, class_ext
, 0));
1953 expr
= convert (ltyp
, null_pointer_node
);
1954 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
1959 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, null_pointer_node
);
1961 /* gc_object_type = NULL */
1962 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, null_pointer_node
);
1964 return objc_build_constructor (type
, v
);
1968 generate_ivars_list (tree chain
, const char *name
, tree attr
)
1970 tree initlist
, ivar_list_template
, decl
;
1972 vec
<constructor_elt
, va_gc
> *inits
= NULL
;
1977 if (!objc_ivar_template
)
1978 objc_ivar_template
= build_ivar_template ();
1980 size
= ivar_list_length (chain
);
1982 generating_instance_variables
= 1;
1983 ivar_list_template
= build_ivar_list_template (objc_ivar_template
, size
);
1984 initlist
= build_ivar_list_initializer (objc_ivar_template
, chain
);
1985 generating_instance_variables
= 0;
1987 decl
= start_var_decl (ivar_list_template
, name
);
1989 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, build_int_cst (NULL_TREE
, size
));
1990 CONSTRUCTOR_APPEND_ELT (inits
, NULL_TREE
, initlist
);
1992 OBJCMETA (decl
, objc_meta
, attr
);
1993 finish_var_decl (decl
,
1994 objc_build_constructor (TREE_TYPE (decl
), inits
));
1999 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
2000 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
2003 generate_v1_class_structs (struct imp_entry
*impent
)
2005 tree name_expr
, super_expr
, root_expr
, class_decl
, meta_decl
;
2006 tree my_root_id
, my_super_id
;
2007 tree cast_type
, initlist
, protocol_decl
;
2008 tree class_ext_decl
= NULL_TREE
, props
= NULL_TREE
;
2009 tree inst_methods
= NULL_TREE
, class_methods
= NULL_TREE
;
2010 tree chain
, inst_ivars
= NULL_TREE
, class_ivars
= NULL_TREE
;
2015 /* objc_implementation_context = impent->imp_context;
2016 implementation_template = impent->imp_template;*/
2017 class_decl
= impent
->class_decl
;
2018 meta_decl
= impent
->meta_decl
;
2019 cls_flags
= impent
->has_cxx_cdtors
? CLS_HAS_CXX_STRUCTORS
: 0 ;
2021 loc
= DECL_SOURCE_LOCATION (impent
->class_decl
);
2023 if (flag_objc_abi
>= 1)
2025 /* ABI=1 additions. */
2026 props
= generate_v1_property_table (NULL_TREE
, impent
->imp_context
);
2027 class_ext_decl
= generate_objc_class_ext (props
, impent
->imp_context
);
2030 my_super_id
= CLASS_SUPER_NAME (impent
->imp_template
);
2033 add_class_reference (my_super_id
);
2035 /* Compute "my_root_id" - this is required for code generation.
2036 the "isa" for all meta class structures points to the root of
2037 the inheritance hierarchy (e.g. "__Object")... */
2038 my_root_id
= my_super_id
;
2041 tree my_root_int
= lookup_interface (my_root_id
);
2043 if (my_root_int
&& CLASS_SUPER_NAME (my_root_int
))
2044 my_root_id
= CLASS_SUPER_NAME (my_root_int
);
2049 super_expr
= add_objc_string (my_super_id
, class_names
);
2053 /* No super class. */
2054 my_root_id
= CLASS_NAME (impent
->imp_template
);
2055 super_expr
= null_pointer_node
;
2058 /* Install class `isa' and `super' pointers at runtime. */
2059 cast_type
= build_pointer_type (objc_class_template
);
2060 super_expr
= build_c_cast (loc
, cast_type
, super_expr
);
2062 root_expr
= add_objc_string (my_root_id
, class_names
);
2063 root_expr
= build_c_cast (loc
, cast_type
, root_expr
);
2065 if (CLASS_PROTOCOL_LIST (impent
->imp_template
))
2067 generate_protocol_references (CLASS_PROTOCOL_LIST (impent
->imp_template
));
2068 protocol_decl
= generate_v1_protocol_list (impent
->imp_template
,
2069 impent
->imp_context
);
2072 protocol_decl
= NULL_TREE
;
2074 if (CLASS_CLS_METHODS (impent
->imp_context
))
2076 snprintf (buf
, BUFSIZE
, "_OBJC_ClassMethods_%s",
2077 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
)));
2078 class_methods
= generate_dispatch_table (CLASS_CLS_METHODS (impent
->imp_context
),
2079 buf
, meta_clac_meth
);
2082 if (CLASS_SUPER_NAME (impent
->imp_template
) == NULL_TREE
2083 && (chain
= TYPE_FIELDS (objc_class_template
)))
2085 snprintf (buf
, BUFSIZE
, "_OBJC_ClassIvars_%s",
2086 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
)));
2087 class_ivars
= generate_ivars_list (chain
, buf
, meta_clac_vars
);
2089 /* TODO: get rid of hidden passing of stuff in globals. */
2090 /* UOBJC_INSTANCE/CLASS_Variables_decl made in generate_ivarlists(). */
2092 name_expr
= add_objc_string (CLASS_NAME (impent
->imp_template
), class_names
);
2094 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
2096 initlist
= build_v1_shared_structure_initializer
2097 (TREE_TYPE (meta_decl
),
2098 root_expr
, super_expr
, name_expr
,
2099 convert (integer_type_node
, TYPE_SIZE_UNIT (objc_class_template
)),
2100 CLS_META
, class_methods
, class_ivars
,
2101 protocol_decl
, NULL_TREE
);
2103 finish_var_decl (meta_decl
, initlist
);
2104 impent
->meta_decl
= meta_decl
;
2106 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
2107 if (CLASS_NST_METHODS (impent
->imp_context
))
2109 snprintf (buf
, BUFSIZE
, "_OBJC_InstanceMethods_%s",
2110 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
)));
2111 inst_methods
= generate_dispatch_table (CLASS_NST_METHODS (impent
->imp_context
),
2112 buf
, meta_clai_meth
);
2115 if ((chain
= CLASS_IVARS (impent
->imp_template
)))
2117 snprintf (buf
, BUFSIZE
, "_OBJC_InstanceIvars_%s",
2118 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
)));
2119 inst_ivars
= generate_ivars_list (chain
, buf
, meta_clai_vars
);
2122 initlist
= build_v1_shared_structure_initializer
2123 (TREE_TYPE (class_decl
),
2124 build_unary_op (loc
, ADDR_EXPR
, meta_decl
, 0),
2125 super_expr
, name_expr
,
2126 convert (integer_type_node
,
2127 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE (impent
->imp_template
))),
2128 CLS_FACTORY
| cls_flags
, inst_methods
, inst_ivars
,
2129 protocol_decl
, class_ext_decl
);
2131 finish_var_decl (class_decl
, initlist
);
2132 impent
->class_decl
= class_decl
;
2135 /* --- Output NeXT V1 Metadata --- */
2137 /* Create the initial value for the `defs' field of _objc_symtab.
2138 This is a CONSTRUCTOR. */
2141 init_def_list (tree type
)
2145 struct imp_entry
*impent
;
2146 vec
<constructor_elt
, va_gc
> *v
= NULL
;
2149 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2151 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
2153 loc
= DECL_SOURCE_LOCATION (impent
->class_decl
);
2154 expr
= build_unary_op (loc
,
2155 ADDR_EXPR
, impent
->class_decl
, 0);
2156 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2161 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2163 if (TREE_CODE (impent
->imp_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
2165 loc
= DECL_SOURCE_LOCATION (impent
->class_decl
);
2166 expr
= build_unary_op (loc
,
2167 ADDR_EXPR
, impent
->class_decl
, 0);
2168 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, expr
);
2172 return objc_build_constructor (type
, v
);
2175 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2177 /* Predefine the following data type:
2185 void *defs[cls_def_cnt + cat_def_cnt];
2189 build_objc_symtab_template (void)
2191 tree fields
, *chain
= NULL
;
2193 objc_symtab_template
= objc_start_struct (get_identifier (UTAG_SYMTAB
));
2195 /* long sel_ref_cnt; */
2196 fields
= add_field_decl (long_integer_type_node
, "sel_ref_cnt", &chain
);
2199 add_field_decl (build_pointer_type (objc_selector_type
), "refs", &chain
);
2201 /* short cls_def_cnt; */
2202 add_field_decl (short_integer_type_node
, "cls_def_cnt", &chain
);
2204 /* short cat_def_cnt; */
2205 add_field_decl (short_integer_type_node
, "cat_def_cnt", &chain
);
2207 if (imp_count
|| cat_count
)
2209 /* void *defs[imp_count + cat_count (+ 1)]; */
2210 /* NB: The index is one less than the size of the array. */
2211 int index
= imp_count
+ cat_count
;
2212 tree array_type
= build_sized_array_type (ptr_type_node
, index
);
2213 add_field_decl (array_type
, "defs", &chain
);
2216 objc_finish_struct (objc_symtab_template
, fields
);
2218 /* Construct the initial value for all of _objc_symtab. */
2221 init_objc_symtab (tree type
)
2223 vec
<constructor_elt
, va_gc
> *v
= NULL
;
2225 /* sel_ref_cnt = { ..., 5, ... } */
2227 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2228 build_int_cst (long_integer_type_node
, 0));
2230 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2232 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2233 convert (build_pointer_type (objc_selector_type
),
2234 integer_zero_node
));
2236 /* cls_def_cnt = { ..., 5, ... } */
2238 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2239 build_int_cst (short_integer_type_node
, imp_count
));
2241 /* cat_def_cnt = { ..., 5, ... } */
2243 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
2244 build_int_cst (short_integer_type_node
, cat_count
));
2246 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2248 if (imp_count
|| cat_count
)
2250 tree field
= TYPE_FIELDS (type
);
2251 field
= DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field
))));
2253 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init_def_list (TREE_TYPE (field
)));
2256 return objc_build_constructor (type
, v
);
2259 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2260 and initialized appropriately. */
2263 generate_objc_symtab_decl (void)
2265 build_objc_symtab_template ();
2266 UOBJC_SYMBOLS_decl
= start_var_decl (objc_symtab_template
, "_OBJC_Symbols");
2267 /* Allow the runtime to mark meta-data such that it can be assigned to target
2268 specific sections by the back-end. */
2269 OBJCMETA (UOBJC_SYMBOLS_decl
, objc_meta
, meta_symtab
);
2270 finish_var_decl (UOBJC_SYMBOLS_decl
,
2271 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl
)));
2274 /* Any target implementing NeXT ObjC m32 ABI has to ensure that objects
2275 refer to, and define, symbols that enforce linkage of classes into the
2276 executable image, preserving unix archive semantics.
2278 At present (4.8), the only targets implementing this are Darwin; these
2279 use top level asms to implement a scheme (see config/darwin-c.c). The
2280 latter method is a hack, but compatible with LTO see also PR48109 for
2281 further discussion and other possible methods. */
2284 handle_next_class_ref (tree chain ATTRIBUTE_UNUSED
)
2286 if (targetcm
.objc_declare_unresolved_class_reference
)
2288 const char *name
= IDENTIFIER_POINTER (TREE_VALUE (chain
));
2289 char *string
= (char *) alloca (strlen (name
) + 30);
2290 sprintf (string
, ".objc_class_name_%s", name
);
2291 targetcm
.objc_declare_unresolved_class_reference (string
);
2296 handle_next_impent (struct imp_entry
*impent ATTRIBUTE_UNUSED
)
2298 if (targetcm
.objc_declare_class_definition
)
2302 switch (TREE_CODE (impent
->imp_context
))
2304 case CLASS_IMPLEMENTATION_TYPE
:
2305 snprintf (buf
, BUFSIZE
, ".objc_class_name_%s",
2306 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
)));
2308 case CATEGORY_IMPLEMENTATION_TYPE
:
2309 snprintf (buf
, BUFSIZE
, "*.objc_category_name_%s_%s",
2310 IDENTIFIER_POINTER (CLASS_NAME (impent
->imp_context
)),
2311 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent
->imp_context
)));
2316 targetcm
.objc_declare_class_definition (buf
);
2321 generate_classref_translation_entry (tree chain
)
2323 tree expr
, decl
, type
;
2325 decl
= TREE_PURPOSE (chain
);
2326 type
= TREE_TYPE (decl
);
2328 expr
= add_objc_string (TREE_VALUE (chain
), class_names
);
2329 expr
= convert (type
, expr
); /* cast! */
2331 /* This is a class reference. It is re-written by the runtime,
2332 but will be optimized away unless we force it. */
2333 DECL_PRESERVE_P (decl
) = 1;
2334 OBJCMETA (decl
, objc_meta
, meta_class_reference
);
2335 finish_var_decl (decl
, expr
);
2340 objc_generate_v1_next_metadata (void)
2342 struct imp_entry
*impent
;
2346 /* FIXME: Make sure that we generate no metadata if there is nothing
2349 if (objc_static_instances
)
2350 gcc_unreachable (); /* Not for NeXT */
2352 build_metadata_templates ();
2353 objc_implementation_context
=
2354 implementation_template
=
2356 UOBJC_METACLASS_decl
= NULL_TREE
;
2358 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2361 /* If -gen-decls is present, Dump the @interface of each class.
2362 TODO: Dump the classes in the order they were found, rather than in
2363 reverse order as we are doing now. */
2364 if (flag_gen_declaration
)
2365 dump_interface (gen_declaration_file
, impent
->imp_context
);
2367 /* all of the following reference the string pool... */
2368 if (TREE_CODE (impent
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
)
2369 generate_v1_class_structs (impent
);
2371 generate_v1_category (impent
);
2374 /* If we are using an array of selectors, we must always
2375 finish up the array decl even if no selectors were used. */
2376 build_next_selector_translation_table ();
2379 generate_v1_protocols ();
2381 /* Pass summary information to the runtime. */
2382 if (imp_count
|| cat_count
)
2383 generate_objc_symtab_decl ();
2385 vers
= OBJC_VERSION
;
2386 attr
= build_tree_list (objc_meta
, meta_modules
);
2387 build_module_descriptor (vers
, attr
);
2389 /* Dump the class references. This forces the appropriate classes
2390 to be linked into the executable image, preserving unix archive
2392 for (chain
= cls_ref_chain
; chain
; chain
= TREE_CHAIN (chain
))
2394 handle_next_class_ref (chain
);
2395 if (TREE_PURPOSE (chain
))
2396 generate_classref_translation_entry (chain
);
2399 for (impent
= imp_list
; impent
; impent
= impent
->next
)
2400 handle_next_impent (impent
);
2402 /* Emit the strings tables. */
2403 generate_strings ();
2406 /* --- exceptions stuff --- */
2408 /* Predefine the following data type:
2410 struct _objc_exception_data
2412 int buf[OBJC_JBLEN];
2416 /* The following yuckiness should prevent users from having to #include
2417 <setjmp.h> in their code... */
2419 /* Define to a harmless positive value so the below code doesn't die. */
2421 #define OBJC_JBLEN 18
2425 build_next_objc_exception_stuff (void)
2427 tree decls
, temp_type
, *chain
= NULL
;
2429 objc_exception_data_template
2430 = objc_start_struct (get_identifier (UTAG_EXCDATA
));
2432 /* int buf[OBJC_JBLEN]; */
2434 temp_type
= build_sized_array_type (integer_type_node
, OBJC_JBLEN
);
2435 decls
= add_field_decl (temp_type
, "buf", &chain
);
2437 /* void *pointers[4]; */
2439 temp_type
= build_sized_array_type (ptr_type_node
, 4);
2440 add_field_decl (temp_type
, "pointers", &chain
);
2442 objc_finish_struct (objc_exception_data_template
, decls
);
2444 /* int _setjmp(...); */
2445 /* If the user includes <setjmp.h>, this shall be superseded by
2446 'int _setjmp(jmp_buf);' */
2447 temp_type
= build_varargs_function_type_list (integer_type_node
, NULL_TREE
);
2449 = add_builtin_function (TAG_SETJMP
, temp_type
, 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
2451 /* id objc_exception_extract(struct _objc_exception_data *); */
2453 = build_function_type_list (objc_object_type
,
2454 build_pointer_type (objc_exception_data_template
),
2456 objc_exception_extract_decl
2457 = add_builtin_function (TAG_EXCEPTIONEXTRACT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
2459 /* void objc_exception_try_enter(struct _objc_exception_data *); */
2460 /* void objc_exception_try_exit(struct _objc_exception_data *); */
2462 = build_function_type_list (void_type_node
,
2463 build_pointer_type (objc_exception_data_template
),
2465 objc_exception_try_enter_decl
2466 = add_builtin_function (TAG_EXCEPTIONTRYENTER
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
2468 objc_exception_try_exit_decl
2469 = add_builtin_function (TAG_EXCEPTIONTRYEXIT
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
2472 /* int objc_exception_match(id, id); */
2474 = build_function_type_list (integer_type_node
,
2475 objc_object_type
, objc_object_type
, NULL_TREE
);
2476 objc_exception_match_decl
2477 = add_builtin_function (TAG_EXCEPTIONMATCH
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
2480 /* id objc_assign_ivar (id, id, unsigned int); */
2481 /* id objc_assign_ivar_Fast (id, id, unsigned int)
2482 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
2484 = build_function_type_list (objc_object_type
,
2489 objc_assign_ivar_decl
2490 = add_builtin_function (TAG_ASSIGNIVAR
, temp_type
, 0, NOT_BUILT_IN
,
2492 #ifdef OFFS_ASSIGNIVAR_FAST
2493 objc_assign_ivar_fast_decl
2494 = add_builtin_function (TAG_ASSIGNIVAR_FAST
, temp_type
, 0,
2495 NOT_BUILT_IN
, NULL
, NULL_TREE
);
2496 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl
)
2497 = tree_cons (get_identifier ("hard_coded_address"),
2498 build_int_cst (NULL_TREE
, OFFS_ASSIGNIVAR_FAST
),
2501 /* Default to slower ivar method. */
2502 objc_assign_ivar_fast_decl
= objc_assign_ivar_decl
;
2505 /* id objc_assign_global (id, id *); */
2506 /* id objc_assign_strongCast (id, id *); */
2507 temp_type
= build_function_type_list (objc_object_type
,
2509 build_pointer_type (objc_object_type
),
2511 objc_assign_global_decl
2512 = add_builtin_function (TAG_ASSIGNGLOBAL
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
2514 objc_assign_strong_cast_decl
2515 = add_builtin_function (TAG_ASSIGNSTRONGCAST
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
2519 /* --- NeXT V1 SJLJ Exceptions --- */
2521 /* Build "objc_exception_try_exit(&_stack)". */
2524 next_sjlj_build_try_exit (struct objc_try_context
**ctcp
)
2527 t
= build_fold_addr_expr_loc (input_location
, (*ctcp
)->stack_decl
);
2528 t
= tree_cons (NULL
, t
, NULL
);
2529 t
= build_function_call (input_location
,
2530 objc_exception_try_exit_decl
, t
);
2535 objc_exception_try_enter (&_stack);
2536 if (_setjmp(&_stack.buf))
2540 Return the COND_EXPR. Note that the THEN and ELSE fields are left
2541 empty, ready for the caller to fill them in. */
2544 next_sjlj_build_enter_and_setjmp (struct objc_try_context
**ctcp
)
2546 tree t
, enter
, sj
, cond
;
2548 t
= build_fold_addr_expr_loc (input_location
, (*ctcp
)->stack_decl
);
2549 t
= tree_cons (NULL
, t
, NULL
);
2550 enter
= build_function_call (input_location
,
2551 objc_exception_try_enter_decl
, t
);
2553 t
= objc_build_component_ref ((*ctcp
)->stack_decl
,
2554 get_identifier ("buf"));
2555 t
= build_fold_addr_expr_loc (input_location
, t
);
2557 /* Convert _setjmp argument to type that is expected. */
2558 if (prototype_p (TREE_TYPE (objc_setjmp_decl
)))
2559 t
= convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl
))), t
);
2561 t
= convert (ptr_type_node
, t
);
2563 t
= convert (ptr_type_node
, t
);
2565 t
= tree_cons (NULL
, t
, NULL
);
2566 sj
= build_function_call (input_location
,
2567 objc_setjmp_decl
, t
);
2569 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (sj
), enter
, sj
);
2570 cond
= c_common_truthvalue_conversion (input_location
, cond
);
2572 return build3 (COND_EXPR
, void_type_node
, cond
, NULL
, NULL
);
2577 DECL = objc_exception_extract(&_stack); */
2580 next_sjlj_build_exc_extract (struct objc_try_context
**ctcp
, tree decl
)
2584 t
= build_fold_addr_expr_loc (input_location
, (*ctcp
)->stack_decl
);
2585 t
= tree_cons (NULL
, t
, NULL
);
2586 t
= build_function_call (input_location
,
2587 objc_exception_extract_decl
, t
);
2588 t
= convert (TREE_TYPE (decl
), t
);
2589 t
= build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
2595 if (objc_exception_match(obj_get_class(TYPE), _caught)
2602 objc_exception_try_exit(&_stack);
2604 from the sequence of CATCH_EXPRs in the current try context. */
2607 next_sjlj_build_catch_list (struct objc_try_context
**ctcp
)
2609 tree_stmt_iterator i
= tsi_start ((*ctcp
)->catch_list
);
2611 tree
*last
= &catch_seq
;
2612 bool saw_id
= false;
2614 for (; !tsi_end_p (i
); tsi_next (&i
))
2616 tree stmt
= tsi_stmt (i
);
2617 tree type
= CATCH_TYPES (stmt
);
2618 tree body
= CATCH_BODY (stmt
);
2620 if (type
!= error_mark_node
2621 && objc_is_object_id (TREE_TYPE (type
)))
2631 if (type
== error_mark_node
)
2632 cond
= error_mark_node
;
2635 args
= tree_cons (NULL
, (*ctcp
)->caught_decl
, NULL
);
2636 t
= objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type
)));
2637 args
= tree_cons (NULL
, t
, args
);
2638 t
= build_function_call (input_location
,
2639 objc_exception_match_decl
, args
);
2640 cond
= c_common_truthvalue_conversion (input_location
, t
);
2642 t
= build3 (COND_EXPR
, void_type_node
, cond
, body
, NULL
);
2643 SET_EXPR_LOCATION (t
, EXPR_LOCATION (stmt
));
2646 last
= &COND_EXPR_ELSE (t
);
2652 t
= build2 (MODIFY_EXPR
, void_type_node
, (*ctcp
)->rethrow_decl
,
2653 (*ctcp
)->caught_decl
);
2654 SET_EXPR_LOCATION (t
, (*ctcp
)->end_catch_locus
);
2655 append_to_statement_list (t
, last
);
2657 t
= next_sjlj_build_try_exit (ctcp
);
2658 SET_EXPR_LOCATION (t
, (*ctcp
)->end_catch_locus
);
2659 append_to_statement_list (t
, last
);
2665 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
2666 exception handling. We aim to build:
2669 struct _objc_exception_data _stack;
2673 objc_exception_try_enter (&_stack);
2674 if (_setjmp(&_stack.buf))
2676 id _caught = objc_exception_extract(&_stack);
2677 objc_exception_try_enter (&_stack);
2678 if (_setjmp(&_stack.buf))
2679 _rethrow = objc_exception_extract(&_stack);
2689 objc_exception_try_exit(&_stack);
2692 objc_exception_throw(_rethrow);
2696 If CATCH-LIST is empty, we can omit all of the block containing
2697 "_caught" except for the setting of _rethrow. Note the use of
2698 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
2699 but handles goto and other exits from the block. */
2702 next_sjlj_build_try_catch_finally (struct objc_try_context
**ctcp
)
2704 tree rethrow_decl
, stack_decl
, t
;
2705 tree catch_seq
, try_fin
, bind
;
2706 struct objc_try_context
*cur_try_context
= *ctcp
;
2708 /* Create the declarations involved. */
2709 t
= xref_tag (RECORD_TYPE
, get_identifier (UTAG_EXCDATA
));
2710 stack_decl
= objc_create_temporary_var (t
, NULL
);
2711 cur_try_context
->stack_decl
= stack_decl
;
2713 rethrow_decl
= objc_create_temporary_var (objc_object_type
, NULL
);
2714 cur_try_context
->rethrow_decl
= rethrow_decl
;
2715 TREE_CHAIN (rethrow_decl
) = stack_decl
;
2717 /* Build the outermost variable binding level. */
2718 bind
= build3 (BIND_EXPR
, void_type_node
, rethrow_decl
, NULL
, NULL
);
2719 SET_EXPR_LOCATION (bind
, cur_try_context
->try_locus
);
2720 TREE_SIDE_EFFECTS (bind
) = 1;
2722 /* Initialize rethrow_decl. */
2723 t
= build2 (MODIFY_EXPR
, void_type_node
, rethrow_decl
,
2724 convert (objc_object_type
, null_pointer_node
));
2725 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
2726 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
2728 /* Build the outermost TRY_FINALLY_EXPR. */
2729 try_fin
= build2 (TRY_FINALLY_EXPR
, void_type_node
, NULL
, NULL
);
2730 SET_EXPR_LOCATION (try_fin
, cur_try_context
->try_locus
);
2731 TREE_SIDE_EFFECTS (try_fin
) = 1;
2732 append_to_statement_list (try_fin
, &BIND_EXPR_BODY (bind
));
2734 /* Create the complete catch sequence. */
2735 if (cur_try_context
->catch_list
)
2737 tree caught_decl
= objc_build_exc_ptr (ctcp
);
2738 catch_seq
= build_stmt (input_location
, BIND_EXPR
, caught_decl
, NULL
, NULL
);
2739 TREE_SIDE_EFFECTS (catch_seq
) = 1;
2741 t
= next_sjlj_build_exc_extract (ctcp
, caught_decl
);
2742 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
2744 t
= next_sjlj_build_enter_and_setjmp (ctcp
);
2745 COND_EXPR_THEN (t
) = next_sjlj_build_exc_extract (ctcp
, rethrow_decl
);
2746 COND_EXPR_ELSE (t
) = next_sjlj_build_catch_list (ctcp
);
2747 append_to_statement_list (t
, &BIND_EXPR_BODY (catch_seq
));
2750 catch_seq
= next_sjlj_build_exc_extract (ctcp
, rethrow_decl
);
2751 SET_EXPR_LOCATION (catch_seq
, cur_try_context
->end_try_locus
);
2753 /* Build the main register-and-try if statement. */
2754 t
= next_sjlj_build_enter_and_setjmp (ctcp
);
2755 SET_EXPR_LOCATION (t
, cur_try_context
->try_locus
);
2756 COND_EXPR_THEN (t
) = catch_seq
;
2757 COND_EXPR_ELSE (t
) = cur_try_context
->try_body
;
2758 TREE_OPERAND (try_fin
, 0) = t
;
2760 /* Build the complete FINALLY statement list. */
2761 t
= next_sjlj_build_try_exit (ctcp
);
2762 t
= build_stmt (input_location
, COND_EXPR
,
2763 c_common_truthvalue_conversion
2764 (input_location
, rethrow_decl
),
2766 SET_EXPR_LOCATION (t
, cur_try_context
->finally_locus
);
2767 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
2769 append_to_statement_list (cur_try_context
->finally_body
,
2770 &TREE_OPERAND (try_fin
, 1));
2772 t
= tree_cons (NULL
, rethrow_decl
, NULL
);
2773 t
= build_function_call (input_location
,
2774 objc_exception_throw_decl
, t
);
2775 t
= build_stmt (input_location
, COND_EXPR
,
2776 c_common_truthvalue_conversion (input_location
,
2779 SET_EXPR_LOCATION (t
, cur_try_context
->end_finally_locus
);
2780 append_to_statement_list (t
, &TREE_OPERAND (try_fin
, 1));
2785 /* We do not expect this to be used at the moment.
2786 If (a) it is possible to implement unwinder exceptions.
2787 (b) we do it... then it might be possibly useful.
2789 static GTY(()) tree objc_eh_personality_decl
;
2792 objc_eh_runtime_type (tree type
)
2794 tree ident
, eh_id
, decl
, str
;
2797 if (type
== error_mark_node
)
2799 /* Use 'ErrorMarkNode' as class name when error_mark_node is found
2800 to prevent an ICE. Note that we know that the compiler will
2801 terminate with an error and this 'ErrorMarkNode' class name will
2802 never be actually used. */
2803 ident
= get_identifier ("ErrorMarkNode");
2804 goto make_err_class
;
2807 if (POINTER_TYPE_P (type
) && objc_is_object_id (TREE_TYPE (type
)))
2809 ident
= get_identifier ("id");
2810 goto make_err_class
;
2813 if (!POINTER_TYPE_P (type
) || !TYPED_OBJECT (TREE_TYPE (type
)))
2816 /* This routine is also called for c++'s catch clause; in which case,
2817 we use c++'s typeinfo decl. */
2818 return build_eh_type_type (type
);
2820 error ("non-objective-c type '%T' cannot be caught", type
);
2821 ident
= get_identifier ("ErrorMarkNode");
2822 goto make_err_class
;
2826 ident
= OBJC_TYPE_NAME (TREE_TYPE (type
));
2829 /* If this class was already referenced, then it will be output during
2830 meta-data emission, so we don't need to do it here. */
2831 decl
= get_objc_string_decl (ident
, class_names
);
2832 eh_id
= add_objc_string (ident
, class_names
);
2835 /* Not found ... so we need to build it - from the freshly-entered id. */
2836 decl
= get_objc_string_decl (ident
, class_names
);
2837 str
= my_build_string (IDENTIFIER_LENGTH (ident
) + 1,
2838 IDENTIFIER_POINTER (ident
));
2839 /* We have to finalize this var here, because this might be called after
2840 all the other metadata strings have been emitted. */
2841 finish_var_decl (decl
, str
);
2846 /* For NeXT ABI 0 and 1, the personality routines are just those of the
2847 underlying language. */
2850 objc_eh_personality (void)
2852 if (!objc_eh_personality_decl
)
2854 objc_eh_personality_decl
= build_personality_function ("gcc");
2856 objc_eh_personality_decl
= build_personality_function ("gxx");
2858 return objc_eh_personality_decl
;
2861 /* --- interfaces --- */
2864 build_throw_stmt (location_t loc
, tree throw_expr
, bool rethrown ATTRIBUTE_UNUSED
)
2867 vec
<tree
, va_gc
> *parms
;
2868 vec_alloc (parms
, 1);
2869 /* A throw is just a call to the runtime throw function with the
2870 object as a parameter. */
2871 parms
->quick_push (throw_expr
);
2872 t
= build_function_call_vec (loc
, vNULL
, objc_exception_throw_decl
, parms
,
2875 return add_stmt (t
);
2878 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
2879 of Darwin, we'll arrange for it to be initialized (and associated
2880 with a binding) later. */
2883 objc_build_exc_ptr (struct objc_try_context
**cur_try_context
)
2885 if (flag_objc_sjlj_exceptions
)
2887 tree var
= (*cur_try_context
)->caught_decl
;
2890 var
= objc_create_temporary_var (objc_object_type
, NULL
);
2891 (*cur_try_context
)->caught_decl
= var
;
2898 t
= builtin_decl_explicit (BUILT_IN_EH_POINTER
);
2899 t
= build_call_expr (t
, 1, integer_zero_node
);
2900 return fold_convert (objc_object_type
, t
);
2905 begin_catch (struct objc_try_context
**cur_try_context
, tree type
,
2906 tree decl
, tree compound
, bool ellipsis ATTRIBUTE_UNUSED
)
2909 /* Record the data for the catch in the try context so that we can
2910 finalize it later. We treat ellipsis the same way as catching
2912 t
= build_stmt (input_location
, CATCH_EXPR
, type
, compound
);
2913 (*cur_try_context
)->current_catch
= t
;
2915 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
2916 t
= objc_build_exc_ptr (cur_try_context
);
2917 t
= convert (TREE_TYPE (decl
), t
);
2918 return build2 (MODIFY_EXPR
, void_type_node
, decl
, t
);
2922 finish_catch (struct objc_try_context
**cur_try_context
, tree current_catch
)
2924 append_to_statement_list (current_catch
, &((*cur_try_context
)->catch_list
));
2928 finish_try_stmt (struct objc_try_context
**cur_try_context
)
2931 struct objc_try_context
*c
= *cur_try_context
;
2932 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
2933 if (flag_objc_sjlj_exceptions
)
2935 bool save
= in_late_binary_op
;
2936 in_late_binary_op
= true;
2937 if (!c
->finally_body
)
2939 c
->finally_locus
= input_location
;
2940 c
->end_finally_locus
= input_location
;
2942 stmt
= next_sjlj_build_try_catch_finally (cur_try_context
);
2943 in_late_binary_op
= save
;
2946 /* This doesn't happen at the moment... but maybe one day... */
2948 /* Otherwise, nest the CATCH inside a FINALLY. */
2951 stmt
= build_stmt (c
->try_locus
, TRY_CATCH_EXPR
, stmt
, c
->catch_list
);
2952 if (c
->finally_body
)
2953 stmt
= build_stmt (c
->try_locus
, TRY_FINALLY_EXPR
, stmt
, c
->finally_body
);
2958 #include "gt-objc-objc-next-runtime-abi-01.h"