1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Steve Naroff.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Purpose: This module implements the Objective-C 4.0 language.
25 compatibility issues (with the Stepstone translator):
27 - does not recognize the following 3.3 constructs.
28 @requires, @classes, @messages, = (...)
29 - methods with variable arguments must conform to ANSI standard.
30 - tagged structure definitions that appear in BOTH the interface
31 and implementation are not allowed.
32 - public/private: all instance variables are public within the
33 context of the implementation...I consider this to be a bug in
35 - statically allocated objects are not supported. the user will
36 receive an error if this service is requested.
38 code generation `options':
44 #include "coretypes.h"
53 #include "langhooks.h"
64 #include "diagnostic.h"
67 /* This is the default way of generating a method name. */
68 /* I am not sure it is really correct.
69 Perhaps there's a danger that it will make name conflicts
70 if method names contain underscores. -- rms. */
71 #ifndef OBJC_GEN_METHOD_LABEL
72 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
75 sprintf ((BUF), "_%s_%s_%s_%s", \
76 ((IS_INST) ? "i" : "c"), \
78 ((CAT_NAME)? (CAT_NAME) : ""), \
80 for (temp = (BUF); *temp; temp++) \
81 if (*temp == ':') *temp = '_'; \
85 /* These need specifying. */
86 #ifndef OBJC_FORWARDING_STACK_OFFSET
87 #define OBJC_FORWARDING_STACK_OFFSET 0
90 #ifndef OBJC_FORWARDING_MIN_OFFSET
91 #define OBJC_FORWARDING_MIN_OFFSET 0
94 /* Set up for use of obstacks. */
98 /* This obstack is used to accumulate the encoding of a data type. */
99 static struct obstack util_obstack;
101 /* This points to the beginning of obstack contents, so we can free
102 the whole contents. */
105 /* The version identifies which language generation and runtime
106 the module (file) was compiled for, and is recorded in the
107 module descriptor. */
109 #define OBJC_VERSION (flag_next_runtime ? 5 : 8)
110 #define PROTOCOL_VERSION 2
112 /* (Decide if these can ever be validly changed.) */
113 #define OBJC_ENCODE_INLINE_DEFS 0
114 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
116 /*** Private Interface (procedures) ***/
118 /* Used by compile_file. */
120 static void init_objc (void);
121 static void finish_objc (void);
123 /* Code generation. */
125 static void synth_module_prologue (void);
126 static tree objc_build_constructor (tree, tree);
127 static rtx build_module_descriptor (void);
128 static tree init_module_descriptor (tree);
129 static tree build_objc_method_call (int, tree, tree, tree, tree);
130 static void generate_strings (void);
131 static tree get_proto_encoding (tree);
132 static void build_selector_translation_table (void);
134 static tree objc_add_static_instance (tree, tree);
136 static void build_objc_exception_stuff (void);
137 static tree objc_declare_variable (enum rid, tree, tree, tree);
138 static tree objc_enter_block (void);
139 static tree objc_exit_block (void);
140 static void objc_build_try_enter_fragment (void);
141 static void objc_build_try_exit_fragment (void);
142 static void objc_build_extract_fragment (void);
143 static tree objc_build_extract_expr (void);
145 static tree build_ivar_template (void);
146 static tree build_method_template (void);
147 static tree build_private_template (tree);
148 static void build_class_template (void);
149 static void build_selector_template (void);
150 static void build_category_template (void);
151 static tree lookup_method_in_hash_lists (tree, int);
152 static void build_super_template (void);
153 static tree build_category_initializer (tree, tree, tree, tree, tree, tree);
154 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
155 static void synth_forward_declarations (void);
156 static int ivar_list_length (tree);
157 static tree get_class_ivars (tree, int);
158 static void generate_ivar_lists (void);
159 static void generate_dispatch_tables (void);
160 static void generate_shared_structures (void);
161 static tree generate_protocol_list (tree);
162 static void build_protocol_reference (tree);
164 static tree build_keyword_selector (tree);
165 static tree synth_id_with_class_suffix (const char *, tree);
167 static void generate_static_references (void);
168 static int check_methods_accessible (tree, tree, int);
169 static void encode_aggregate_within (tree, int, int, int, int);
170 static const char *objc_demangle (const char *);
171 static void objc_expand_function_end (void);
173 /* Hash tables to manage the global pool of method prototypes. */
175 hash *nst_method_hash_list = 0;
176 hash *cls_method_hash_list = 0;
178 static size_t hash_func (tree);
179 static void hash_init (void);
180 static void hash_enter (hash *, tree);
181 static hash hash_lookup (hash *, tree);
182 static void hash_add_attr (hash, tree);
183 static tree lookup_method (tree, tree);
184 static tree lookup_method_static (tree, tree, int);
185 static void add_method_to_hash_list (hash *, tree);
186 static tree add_class (tree);
187 static void add_category (tree, tree);
188 static inline tree lookup_category (tree, tree);
192 class_names, /* class, category, protocol, module names */
193 meth_var_names, /* method and variable names */
194 meth_var_types /* method and variable type descriptors */
197 static tree add_objc_string (tree, enum string_section);
198 static tree get_objc_string_decl (tree, enum string_section);
199 static tree build_objc_string_decl (enum string_section);
200 static tree build_selector_reference_decl (void);
202 /* Protocol additions. */
204 static tree add_protocol (tree);
205 static tree lookup_protocol (tree);
206 static void check_protocol_recursively (tree, tree);
207 static tree lookup_and_install_protocols (tree);
211 static void encode_type_qualifiers (tree);
212 static void encode_pointer (tree, int, int);
213 static void encode_array (tree, int, int);
214 static void encode_aggregate (tree, int, int);
215 static void encode_next_bitfield (int);
216 static void encode_gnu_bitfield (int, tree, int);
217 static void encode_type (tree, int, int);
218 static void encode_field_decl (tree, int, int);
220 static void really_start_method (tree, tree);
221 static int comp_method_with_proto (tree, tree);
222 static int objc_types_are_equivalent (tree, tree);
223 static int comp_proto_with_proto (tree, tree);
224 static tree get_arg_type_list (tree, int, int);
225 static tree objc_expr_last (tree);
226 static void synth_self_and_ucmd_args (void);
228 /* Utilities for debugging and error diagnostics. */
230 static void warn_with_method (const char *, int, tree);
231 static void error_with_ivar (const char *, tree, tree);
232 static char *gen_method_decl (tree, char *);
233 static char *gen_declaration (tree, char *);
234 static void gen_declaration_1 (tree, char *);
235 static char *gen_declarator (tree, char *, const char *);
236 static int is_complex_decl (tree);
237 static void adorn_decl (tree, char *);
238 static void dump_interface (FILE *, tree);
240 /* Everything else. */
242 static tree define_decl (tree, tree);
243 static tree lookup_method_in_protocol_list (tree, tree, int);
244 static tree lookup_protocol_in_reflist (tree, tree);
245 static tree create_builtin_decl (enum tree_code, tree, const char *);
246 static void setup_string_decl (void);
247 static int check_string_class_template (void);
248 static tree my_build_string (int, const char *);
249 static void build_objc_symtab_template (void);
250 static tree init_def_list (tree);
251 static tree init_objc_symtab (tree);
252 static tree build_metadata_decl (const char *, tree);
253 static void forward_declare_categories (void);
254 static void generate_objc_symtab_decl (void);
255 static tree build_selector (tree);
256 static tree build_typed_selector_reference (tree, tree);
257 static tree build_selector_reference (tree);
258 static tree build_class_reference_decl (void);
259 static void add_class_reference (tree);
260 static tree build_protocol_template (void);
261 static tree build_descriptor_table_initializer (tree, tree);
262 static tree build_method_prototype_list_template (tree, int);
263 static tree build_method_prototype_template (void);
264 static tree objc_method_parm_type (tree);
265 static int objc_encoded_type_size (tree);
266 static tree encode_method_prototype (tree);
267 static tree generate_descriptor_table (tree, const char *, int, tree, tree);
268 static void generate_method_descriptors (tree);
269 static void generate_protocol_references (tree);
270 static void generate_protocols (void);
271 static void check_ivars (tree, tree);
272 static tree build_ivar_list_template (tree, int);
273 static tree build_method_list_template (tree, int);
274 static tree build_ivar_list_initializer (tree, tree);
275 static tree generate_ivars_list (tree, const char *, int, tree);
276 static tree build_dispatch_table_initializer (tree, tree);
277 static tree generate_dispatch_table (tree, const char *, int, tree);
278 static tree build_shared_structure_initializer (tree, tree, tree, tree,
279 tree, int, tree, tree, tree);
280 static void generate_category (tree);
281 static int is_objc_type_qualifier (tree);
282 static tree adjust_type_for_id_default (tree);
283 static tree check_duplicates (hash, int, int);
284 static tree receiver_is_class_object (tree, int, int);
285 static int check_methods (tree, tree, int);
286 static int conforms_to_protocol (tree, tree);
287 static void check_protocol (tree, const char *, const char *);
288 static void check_protocols (tree, const char *, const char *);
289 static void gen_declspecs (tree, char *, int);
290 static void generate_classref_translation_entry (tree);
291 static void handle_class_ref (tree);
292 static void generate_struct_by_value_array (void)
294 static void mark_referenced_methods (void);
295 static void generate_objc_image_info (void);
297 /*** Private Interface (data) ***/
299 /* Reserved tag definitions. */
302 #define TAG_OBJECT "objc_object"
303 #define TAG_CLASS "objc_class"
304 #define TAG_SUPER "objc_super"
305 #define TAG_SELECTOR "objc_selector"
307 #define UTAG_CLASS "_objc_class"
308 #define UTAG_IVAR "_objc_ivar"
309 #define UTAG_IVAR_LIST "_objc_ivar_list"
310 #define UTAG_METHOD "_objc_method"
311 #define UTAG_METHOD_LIST "_objc_method_list"
312 #define UTAG_CATEGORY "_objc_category"
313 #define UTAG_MODULE "_objc_module"
314 #define UTAG_SYMTAB "_objc_symtab"
315 #define UTAG_SUPER "_objc_super"
316 #define UTAG_SELECTOR "_objc_selector"
318 #define UTAG_PROTOCOL "_objc_protocol"
319 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
320 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
322 /* Note that the string object global name is only needed for the
324 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
326 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
328 static const char *TAG_GETCLASS;
329 static const char *TAG_GETMETACLASS;
330 static const char *TAG_MSGSEND;
331 static const char *TAG_MSGSENDSUPER;
332 /* The NeXT Objective-C messenger may have two extra entry points, for use
333 when returning a structure. */
334 static const char *TAG_MSGSEND_STRET;
335 static const char *TAG_MSGSENDSUPER_STRET;
336 static const char *TAG_EXECCLASS;
337 static const char *default_constant_string_class_name;
339 /* Runtime metadata flags. */
340 #define CLS_FACTORY 0x0001L
341 #define CLS_META 0x0002L
343 #define OBJC_MODIFIER_STATIC 0x00000001
344 #define OBJC_MODIFIER_FINAL 0x00000002
345 #define OBJC_MODIFIER_PUBLIC 0x00000004
346 #define OBJC_MODIFIER_PRIVATE 0x00000008
347 #define OBJC_MODIFIER_PROTECTED 0x00000010
348 #define OBJC_MODIFIER_NATIVE 0x00000020
349 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
350 #define OBJC_MODIFIER_ABSTRACT 0x00000080
351 #define OBJC_MODIFIER_VOLATILE 0x00000100
352 #define OBJC_MODIFIER_TRANSIENT 0x00000200
353 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
355 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
356 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
357 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
358 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
359 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
360 #define TAG_EXCEPTIONMATCH "objc_exception_match"
361 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
362 #define TAG_SYNCENTER "objc_sync_enter"
363 #define TAG_SYNCEXIT "objc_sync_exit"
364 #define TAG_SETJMP "_setjmp"
365 #define TAG_RETURN_STRUCT "objc_return_struct"
367 #define UTAG_EXCDATA "_objc_exception_data"
368 #define UTAG_EXCDATA_VAR "_stackExceptionData"
369 #define UTAG_CAUGHTEXC_VAR "_caughtException"
370 #define UTAG_RETHROWEXC_VAR "_rethrowException"
371 #define UTAG_EVALONCE_VAR "_eval_once"
375 struct val_stack *next;
377 static struct val_stack *catch_count_stack, *exc_binding_stack;
379 /* useful for debugging */
380 static int if_nesting_count;
381 static int blk_nesting_count;
383 static void val_stack_push (struct val_stack **, long);
384 static void val_stack_pop (struct val_stack **);
386 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
387 tree objc_global_trees[OCTI_MAX];
389 static void handle_impent (struct imp_entry *);
391 struct imp_entry *imp_list = 0;
392 int imp_count = 0; /* `@implementation' */
393 int cat_count = 0; /* `@category' */
395 /* Use to generate method labels. */
396 static int method_slot = 0;
400 static char *errbuf; /* Buffer for error diagnostics */
402 /* Data imported from tree.c. */
404 extern enum debug_info_type write_symbols;
406 /* Data imported from toplev.c. */
408 extern const char *dump_base_name;
410 static int flag_typed_selectors;
412 FILE *gen_declaration_file;
414 /* Tells "encode_pointer/encode_aggregate" whether we are generating
415 type descriptors for instance variables (as opposed to methods).
416 Type descriptors for instance variables contain more information
417 than methods (for static typing and embedded structures). */
419 static int generating_instance_variables = 0;
421 /* Some platforms pass small structures through registers versus
422 through an invisible pointer. Determine at what size structure is
423 the transition point between the two possibilities. */
426 generate_struct_by_value_array (void)
429 tree field_decl, field_decl_chain;
431 int aggregate_in_mem[32];
434 /* Presumably no platform passes 32 byte structures in a register. */
435 for (i = 1; i < 32; i++)
439 /* Create an unnamed struct that has `i' character components */
440 type = start_struct (RECORD_TYPE, NULL_TREE);
442 strcpy (buffer, "c1");
443 field_decl = create_builtin_decl (FIELD_DECL,
446 field_decl_chain = field_decl;
448 for (j = 1; j < i; j++)
450 sprintf (buffer, "c%d", j + 1);
451 field_decl = create_builtin_decl (FIELD_DECL,
454 chainon (field_decl_chain, field_decl);
456 finish_struct (type, field_decl_chain, NULL_TREE);
458 aggregate_in_mem[i] = aggregate_value_p (type, 0);
459 if (!aggregate_in_mem[i])
463 /* We found some structures that are returned in registers instead of memory
464 so output the necessary data. */
467 for (i = 31; i >= 0; i--)
468 if (!aggregate_in_mem[i])
470 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
472 /* The first member of the structure is always 0 because we don't handle
473 structures with 0 members */
474 printf ("static int struct_forward_array[] = {\n 0");
476 for (j = 1; j <= i; j++)
477 printf (", %d", aggregate_in_mem[j]);
487 if (c_objc_common_init () == false)
490 /* Force the line number back to 0; check_newline will have
491 raised it to 1, which will make the builtin functions appear
492 not to be built in. */
495 /* If gen_declaration desired, open the output file. */
496 if (flag_gen_declaration)
498 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
499 gen_declaration_file = fopen (dumpname, "w");
500 if (gen_declaration_file == 0)
501 fatal_error ("can't open %s: %m", dumpname);
505 if (flag_next_runtime)
507 TAG_GETCLASS = "objc_getClass";
508 TAG_GETMETACLASS = "objc_getMetaClass";
509 TAG_MSGSEND = "objc_msgSend";
510 TAG_MSGSENDSUPER = "objc_msgSendSuper";
511 TAG_MSGSEND_STRET = "objc_msgSend_stret";
512 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
513 TAG_EXECCLASS = "__objc_execClass";
514 default_constant_string_class_name = "NSConstantString";
518 TAG_GETCLASS = "objc_get_class";
519 TAG_GETMETACLASS = "objc_get_meta_class";
520 TAG_MSGSEND = "objc_msg_lookup";
521 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
522 /* GNU runtime does not provide special functions to support
523 structure-returning methods. */
524 TAG_EXECCLASS = "__objc_exec_class";
525 default_constant_string_class_name = "NXConstantString";
526 flag_typed_selectors = 1;
529 objc_ellipsis_node = make_node (ERROR_MARK);
533 if (print_struct_values)
534 generate_struct_by_value_array ();
542 mark_referenced_methods ();
543 c_objc_common_finish_file ();
545 /* Finalize Objective-C runtime data. No need to generate tables
546 and code if only checking syntax. */
547 if (!flag_syntax_only)
550 if (gen_declaration_file)
551 fclose (gen_declaration_file);
555 define_decl (tree declarator, tree declspecs)
557 tree decl = start_decl (declarator, declspecs, 0, NULL_TREE);
558 finish_decl (decl, NULL_TREE, NULL_TREE);
563 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
569 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
571 p = TREE_VALUE (rproto);
573 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
575 if ((fnd = lookup_method (class_meth
576 ? PROTOCOL_CLS_METHODS (p)
577 : PROTOCOL_NST_METHODS (p), sel_name)))
579 else if (PROTOCOL_LIST (p))
580 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
581 sel_name, class_meth);
585 ; /* An identifier...if we could not find a protocol. */
596 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
600 /* Make sure the protocol is supported by the object on the rhs. */
601 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
604 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
606 p = TREE_VALUE (rproto);
608 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
613 else if (PROTOCOL_LIST (p))
614 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
623 ; /* An identifier...if we could not find a protocol. */
629 /* Return 1 if LHS and RHS are compatible types for assignment or
630 various other operations. Return 0 if they are incompatible, and
631 return -1 if we choose to not decide (because the types are really
632 just C types, not ObjC specific ones). When the operation is
633 REFLEXIVE (typically comparisons), check for compatibility in
634 either direction; when it's not (typically assignments), don't.
636 This function is called in two cases: when both lhs and rhs are
637 pointers to records (in which case we check protocols too), and
638 when both lhs and rhs are records (in which case we check class
641 Warnings about classes/protocols not implementing a protocol are
642 emitted here (multiple of those warnings might be emitted for a
643 single line!); generic warnings about incompatible assignments and
644 lacks of casts in comparisons are/must be emitted by the caller if
649 objc_comptypes (tree lhs, tree rhs, int reflexive)
651 /* New clause for protocols. */
653 /* Here we manage the case of a POINTER_TYPE = POINTER_TYPE. We only
654 manage the ObjC ones, and leave the rest to the C code. */
655 if (TREE_CODE (lhs) == POINTER_TYPE
656 && TREE_CODE (TREE_TYPE (lhs)) == RECORD_TYPE
657 && TREE_CODE (rhs) == POINTER_TYPE
658 && TREE_CODE (TREE_TYPE (rhs)) == RECORD_TYPE)
660 int lhs_is_proto = IS_PROTOCOL_QUALIFIED_ID (lhs);
661 int rhs_is_proto = IS_PROTOCOL_QUALIFIED_ID (rhs);
665 tree lproto, lproto_list = TYPE_PROTOCOL_LIST (lhs);
666 tree rproto, rproto_list;
669 /* <Protocol> = <Protocol> */
672 rproto_list = TYPE_PROTOCOL_LIST (rhs);
676 /* An assignment between objects of type 'id
677 <Protocol>'; make sure the protocol on the lhs is
678 supported by the object on the rhs. */
679 for (lproto = lproto_list; lproto;
680 lproto = TREE_CHAIN (lproto))
682 p = TREE_VALUE (lproto);
683 rproto = lookup_protocol_in_reflist (rproto_list, p);
687 ("object does not conform to the `%s' protocol",
688 IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
694 /* Obscure case - a comparison between two objects
695 of type 'id <Protocol>'. Check that either the
696 protocol on the lhs is supported by the object on
697 the rhs, or viceversa. */
699 /* Check if the protocol on the lhs is supported by the
700 object on the rhs. */
701 for (lproto = lproto_list; lproto;
702 lproto = TREE_CHAIN (lproto))
704 p = TREE_VALUE (lproto);
705 rproto = lookup_protocol_in_reflist (rproto_list, p);
709 /* Check failed - check if the protocol on the rhs
710 is supported by the object on the lhs. */
711 for (rproto = rproto_list; rproto;
712 rproto = TREE_CHAIN (rproto))
714 p = TREE_VALUE (rproto);
715 lproto = lookup_protocol_in_reflist (lproto_list,
720 /* This check failed too: incompatible */
730 /* <Protocol> = <class> * */
731 else if (TYPED_OBJECT (TREE_TYPE (rhs)))
733 tree rname = OBJC_TYPE_NAME (TREE_TYPE (rhs));
736 /* Make sure the protocol is supported by the object on
738 for (lproto = lproto_list; lproto; lproto = TREE_CHAIN (lproto))
740 p = TREE_VALUE (lproto);
742 rinter = lookup_interface (rname);
744 while (rinter && !rproto)
748 rproto_list = CLASS_PROTOCOL_LIST (rinter);
749 rproto = lookup_protocol_in_reflist (rproto_list, p);
750 /* If the underlying ObjC class does not have
751 the protocol we're looking for, check for "one-off"
752 protocols (e.g., `NSObject<MyProt> *foo;') attached
756 rproto_list = TYPE_PROTOCOL_LIST (TREE_TYPE (rhs));
757 rproto = lookup_protocol_in_reflist (rproto_list, p);
760 /* Check for protocols adopted by categories. */
761 cat = CLASS_CATEGORY_LIST (rinter);
762 while (cat && !rproto)
764 rproto_list = CLASS_PROTOCOL_LIST (cat);
765 rproto = lookup_protocol_in_reflist (rproto_list, p);
766 cat = CLASS_CATEGORY_LIST (cat);
769 rinter = lookup_interface (CLASS_SUPER_NAME (rinter));
773 warning ("class `%s' does not implement the `%s' protocol",
774 IDENTIFIER_POINTER (OBJC_TYPE_NAME (TREE_TYPE (rhs))),
775 IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
779 /* <Protocol> = id */
780 else if (OBJC_TYPE_NAME (TREE_TYPE (rhs)) == objc_object_id)
784 /* <Protocol> = Class */
785 else if (OBJC_TYPE_NAME (TREE_TYPE (rhs)) == objc_class_id)
789 /* <Protocol> = ?? : let comptypes decide. */
792 else if (rhs_is_proto)
794 /* <class> * = <Protocol> */
795 if (TYPED_OBJECT (TREE_TYPE (lhs)))
799 tree rname = OBJC_TYPE_NAME (TREE_TYPE (lhs));
801 tree rproto, rproto_list = TYPE_PROTOCOL_LIST (rhs);
803 /* Make sure the protocol is supported by the object on
805 for (rproto = rproto_list; rproto;
806 rproto = TREE_CHAIN (rproto))
808 tree p = TREE_VALUE (rproto);
810 rinter = lookup_interface (rname);
812 while (rinter && !lproto)
816 tree lproto_list = CLASS_PROTOCOL_LIST (rinter);
817 lproto = lookup_protocol_in_reflist (lproto_list, p);
818 /* If the underlying ObjC class does not
819 have the protocol we're looking for,
820 check for "one-off" protocols (e.g.,
821 `NSObject<MyProt> *foo;') attached to the
825 lproto_list = TYPE_PROTOCOL_LIST
827 lproto = lookup_protocol_in_reflist
831 /* Check for protocols adopted by categories. */
832 cat = CLASS_CATEGORY_LIST (rinter);
833 while (cat && !lproto)
835 lproto_list = CLASS_PROTOCOL_LIST (cat);
836 lproto = lookup_protocol_in_reflist (lproto_list,
838 cat = CLASS_CATEGORY_LIST (cat);
841 rinter = lookup_interface (CLASS_SUPER_NAME
846 warning ("class `%s' does not implement the `%s' protocol",
847 IDENTIFIER_POINTER (OBJC_TYPE_NAME
849 IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
856 /* id = <Protocol> */
857 else if (OBJC_TYPE_NAME (TREE_TYPE (lhs)) == objc_object_id)
861 /* Class = <Protocol> */
862 else if (OBJC_TYPE_NAME (TREE_TYPE (lhs)) == objc_class_id)
866 /* ??? = <Protocol> : let comptypes decide */
874 /* Attention: we shouldn't defer to comptypes here. One bad
875 side effect would be that we might loose the REFLEXIVE
878 lhs = TREE_TYPE (lhs);
879 rhs = TREE_TYPE (rhs);
883 if (TREE_CODE (lhs) != RECORD_TYPE || TREE_CODE (rhs) != RECORD_TYPE)
885 /* Nothing to do with ObjC - let immediately comptypes take
886 responsibility for checking. */
890 /* `id' = `<class> *' `<class> *' = `id': always allow it.
892 'Object *o = [[Object alloc] init]; falls
893 in the case <class> * = `id'.
895 if ((OBJC_TYPE_NAME (lhs) == objc_object_id && TYPED_OBJECT (rhs))
896 || (OBJC_TYPE_NAME (rhs) == objc_object_id && TYPED_OBJECT (lhs)))
899 /* `id' = `Class', `Class' = `id' */
901 else if ((OBJC_TYPE_NAME (lhs) == objc_object_id
902 && OBJC_TYPE_NAME (rhs) == objc_class_id)
903 || (OBJC_TYPE_NAME (lhs) == objc_class_id
904 && OBJC_TYPE_NAME (rhs) == objc_object_id))
907 /* `Class' != `<class> *' && `<class> *' != `Class'! */
908 else if ((OBJC_TYPE_NAME (lhs) == objc_class_id && TYPED_OBJECT (rhs))
909 || (OBJC_TYPE_NAME (rhs) == objc_class_id && TYPED_OBJECT (lhs)))
912 /* `<class> *' = `<class> *' */
914 else if (TYPED_OBJECT (lhs) && TYPED_OBJECT (rhs))
916 tree lname = OBJC_TYPE_NAME (lhs);
917 tree rname = OBJC_TYPE_NAME (rhs);
923 /* If the left hand side is a super class of the right hand side,
925 for (inter = lookup_interface (rname); inter;
926 inter = lookup_interface (CLASS_SUPER_NAME (inter)))
927 if (lname == CLASS_SUPER_NAME (inter))
930 /* Allow the reverse when reflexive. */
932 for (inter = lookup_interface (lname); inter;
933 inter = lookup_interface (CLASS_SUPER_NAME (inter)))
934 if (rname == CLASS_SUPER_NAME (inter))
940 /* Not an ObjC type - let comptypes do the check. */
944 /* Called from finish_decl. */
947 objc_check_decl (tree decl)
949 tree type = TREE_TYPE (decl);
951 if (TREE_CODE (type) != RECORD_TYPE)
953 if (TYPE_NAME (type) && (type = is_class_name (TYPE_NAME (type))))
954 error ("statically allocated instance of Objective-C class `%s'",
955 IDENTIFIER_POINTER (type));
958 /* Implement static typing. At this point, we know we have an interface. */
961 get_static_reference (tree interface, tree protocols)
963 tree type = xref_tag (RECORD_TYPE, interface);
967 tree t, m = TYPE_MAIN_VARIANT (type);
969 t = copy_node (type);
971 /* Add this type to the chain of variants of TYPE. */
972 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
973 TYPE_NEXT_VARIANT (m) = t;
975 /* Look up protocols and install in lang specific list. Note
976 that the protocol list can have a different lifetime than T! */
977 SET_TYPE_PROTOCOL_LIST (t, lookup_and_install_protocols (protocols));
979 /* This forces a new pointer type to be created later
980 (in build_pointer_type)...so that the new template
981 we just created will actually be used...what a hack! */
982 if (TYPE_POINTER_TO (t))
983 TYPE_POINTER_TO (t) = NULL_TREE;
992 get_object_reference (tree protocols)
994 tree type_decl = lookup_name (objc_id_id);
997 if (type_decl && TREE_CODE (type_decl) == TYPE_DECL)
999 type = TREE_TYPE (type_decl);
1000 if (TYPE_MAIN_VARIANT (type) != id_type)
1001 warning ("unexpected type for `id' (%s)",
1002 gen_declaration (type, errbuf));
1006 error ("undefined type `id', please import <objc/objc.h>");
1007 return error_mark_node;
1010 /* This clause creates a new pointer type that is qualified with
1011 the protocol specification...this info is used later to do more
1012 elaborate type checking. */
1016 tree t, m = TYPE_MAIN_VARIANT (type);
1018 t = copy_node (type);
1020 /* Add this type to the chain of variants of TYPE. */
1021 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1022 TYPE_NEXT_VARIANT (m) = t;
1024 /* Look up protocols...and install in lang specific list */
1025 SET_TYPE_PROTOCOL_LIST (t, lookup_and_install_protocols (protocols));
1027 /* This forces a new pointer type to be created later
1028 (in build_pointer_type)...so that the new template
1029 we just created will actually be used...what a hack! */
1030 if (TYPE_POINTER_TO (t))
1031 TYPE_POINTER_TO (t) = NULL_TREE;
1038 /* Check for circular dependencies in protocols. The arguments are
1039 PROTO, the protocol to check, and LIST, a list of protocol it
1043 check_protocol_recursively (tree proto, tree list)
1047 for (p = list; p; p = TREE_CHAIN (p))
1049 tree pp = TREE_VALUE (p);
1051 if (TREE_CODE (pp) == IDENTIFIER_NODE)
1052 pp = lookup_protocol (pp);
1055 fatal_error ("protocol `%s' has circular dependency",
1056 IDENTIFIER_POINTER (PROTOCOL_NAME (pp)));
1058 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
1062 /* Look up PROTOCOLS, and return a list of those that are found.
1063 If none are found, return NULL. */
1066 lookup_and_install_protocols (tree protocols)
1069 tree return_value = NULL_TREE;
1071 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
1073 tree ident = TREE_VALUE (proto);
1074 tree p = lookup_protocol (ident);
1077 error ("cannot find protocol declaration for `%s'",
1078 IDENTIFIER_POINTER (ident));
1080 return_value = chainon (return_value,
1081 build_tree_list (NULL_TREE, p));
1084 return return_value;
1087 /* Create and push a decl for a built-in external variable or field NAME.
1089 TYPE is its data type. */
1092 create_builtin_decl (enum tree_code code, tree type, const char *name)
1094 tree decl = build_decl (code, get_identifier (name), type);
1096 if (code == VAR_DECL)
1098 TREE_STATIC (decl) = 1;
1099 make_decl_rtl (decl, 0);
1101 DECL_ARTIFICIAL (decl) = 1;
1107 /* Find the decl for the constant string class. */
1110 setup_string_decl (void)
1112 if (!string_class_decl)
1114 if (!constant_string_global_id)
1118 /* %s in format will provide room for terminating null */
1119 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
1120 + strlen (constant_string_class_name);
1121 name = xmalloc (length);
1122 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
1123 constant_string_class_name);
1124 constant_string_global_id = get_identifier (name);
1126 string_class_decl = lookup_name (constant_string_global_id);
1130 /* Purpose: "play" parser, creating/installing representations
1131 of the declarations that are required by Objective-C.
1135 type_spec--------->sc_spec
1136 (tree_list) (tree_list)
1139 identifier_node identifier_node */
1142 synth_module_prologue (void)
1146 /* Defined in `objc.h' */
1147 objc_object_id = get_identifier (TAG_OBJECT);
1149 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
1151 id_type = build_pointer_type (objc_object_reference);
1153 objc_id_id = get_identifier (TYPE_ID);
1154 objc_class_id = get_identifier (TAG_CLASS);
1156 objc_class_type = build_pointer_type (xref_tag (RECORD_TYPE, objc_class_id));
1157 temp_type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
1158 objc_declare_class (tree_cons (NULL_TREE, temp_type, NULL_TREE));
1159 protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
1162 /* Declare type of selector-objects that represent an operation name. */
1164 if (flag_next_runtime)
1165 /* `struct objc_selector *' */
1167 = build_pointer_type (xref_tag (RECORD_TYPE,
1168 get_identifier (TAG_SELECTOR)));
1170 /* `const struct objc_selector *' */
1172 = build_pointer_type
1173 (build_qualified_type (xref_tag (RECORD_TYPE,
1174 get_identifier (TAG_SELECTOR)),
1177 /* Declare receiver type used for dispatching messages to 'super'. */
1179 /* `struct objc_super *' */
1180 super_type = build_pointer_type (xref_tag (RECORD_TYPE,
1181 get_identifier (TAG_SUPER)));
1183 if (flag_next_runtime)
1185 /* NB: In order to call one of the ..._stret (struct-returning)
1186 functions, the function *MUST* first be cast to a signature that
1187 corresponds to the actual ObjC method being invoked. This is
1188 what is done by the build_objc_method_call() routine below. */
1190 /* id objc_msgSend (id, SEL, ...); */
1191 /* id objc_msgSendNonNil (id, SEL, ...); */
1192 /* id objc_msgSend_stret (id, SEL, ...); */
1193 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
1195 = build_function_type (id_type,
1196 tree_cons (NULL_TREE, id_type,
1197 tree_cons (NULL_TREE, selector_type,
1199 umsg_decl = builtin_function (TAG_MSGSEND,
1200 temp_type, 0, NOT_BUILT_IN,
1202 umsg_nonnil_decl = builtin_function (TAG_MSGSEND_NONNIL,
1203 temp_type, 0, NOT_BUILT_IN,
1205 umsg_stret_decl = builtin_function (TAG_MSGSEND_STRET,
1206 temp_type, 0, NOT_BUILT_IN,
1208 umsg_nonnil_stret_decl = builtin_function (TAG_MSGSEND_NONNIL_STRET,
1209 temp_type, 0, NOT_BUILT_IN,
1212 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
1213 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
1215 = build_function_type (id_type,
1216 tree_cons (NULL_TREE, super_type,
1217 tree_cons (NULL_TREE, selector_type,
1219 umsg_super_decl = builtin_function (TAG_MSGSENDSUPER,
1220 temp_type, 0, NOT_BUILT_IN,
1222 umsg_super_stret_decl = builtin_function (TAG_MSGSENDSUPER_STRET,
1223 temp_type, 0, NOT_BUILT_IN, 0,
1228 /* GNU runtime messenger entry points. */
1230 /* typedef id (*IMP)(id, SEL, ...); */
1232 = build_pointer_type
1233 (build_function_type (id_type,
1234 tree_cons (NULL_TREE, id_type,
1235 tree_cons (NULL_TREE, selector_type,
1238 /* IMP objc_msg_lookup (id, SEL); */
1240 = build_function_type (IMP_type,
1241 tree_cons (NULL_TREE, id_type,
1242 tree_cons (NULL_TREE, selector_type,
1244 umsg_decl = builtin_function (TAG_MSGSEND,
1245 temp_type, 0, NOT_BUILT_IN,
1248 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
1250 = build_function_type (IMP_type,
1251 tree_cons (NULL_TREE, super_type,
1252 tree_cons (NULL_TREE, selector_type,
1254 umsg_super_decl = builtin_function (TAG_MSGSENDSUPER,
1255 temp_type, 0, NOT_BUILT_IN,
1259 /* id objc_getClass (const char *); */
1261 temp_type = build_function_type (id_type,
1262 tree_cons (NULL_TREE,
1263 const_string_type_node,
1267 = builtin_function (TAG_GETCLASS, temp_type, 0, NOT_BUILT_IN,
1270 /* id objc_getMetaClass (const char *); */
1272 objc_get_meta_class_decl
1273 = builtin_function (TAG_GETMETACLASS, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
1275 build_super_template ();
1276 if (flag_next_runtime)
1277 build_objc_exception_stuff ();
1279 /* static SEL _OBJC_SELECTOR_TABLE[]; */
1281 if (! flag_next_runtime)
1283 if (flag_typed_selectors)
1285 /* Suppress outputting debug symbols, because
1286 dbxout_init hasn'r been called yet. */
1287 enum debug_info_type save_write_symbols = write_symbols;
1288 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
1289 write_symbols = NO_DEBUG;
1290 debug_hooks = &do_nothing_debug_hooks;
1292 build_selector_template ();
1293 temp_type = build_array_type (objc_selector_template, NULL_TREE);
1295 write_symbols = save_write_symbols;
1296 debug_hooks = save_hooks;
1299 temp_type = build_array_type (selector_type, NULL_TREE);
1301 layout_type (temp_type);
1302 UOBJC_SELECTOR_TABLE_decl
1303 = create_builtin_decl (VAR_DECL, temp_type,
1304 "_OBJC_SELECTOR_TABLE");
1306 /* Avoid warning when not sending messages. */
1307 TREE_USED (UOBJC_SELECTOR_TABLE_decl) = 1;
1310 /* Forward declare constant_string_id and constant_string_type. */
1311 if (!constant_string_class_name)
1312 constant_string_class_name = default_constant_string_class_name;
1314 constant_string_id = get_identifier (constant_string_class_name);
1315 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
1317 /* Pre-build the following entities - for speed/convenience. */
1318 self_id = get_identifier ("self");
1319 ucmd_id = get_identifier ("_cmd");
1321 /* The C++ front-end does not appear to grok __attribute__((__unused__)). */
1322 unused_list = build_tree_list (get_identifier ("__unused__"), NULL_TREE);
1326 /* Ensure that the ivar list for NSConstantString/NXConstantString
1327 (or whatever was specified via `-fconstant-string-class')
1328 contains fields at least as large as the following three, so that
1329 the runtime can stomp on them with confidence:
1331 struct STRING_OBJECT_CLASS_NAME
1335 unsigned int length;
1339 check_string_class_template (void)
1341 tree field_decl = TYPE_FIELDS (constant_string_type);
1343 #define AT_LEAST_AS_LARGE_AS(F, T) \
1344 (F && TREE_CODE (F) == FIELD_DECL \
1345 && (TREE_INT_CST_LOW (DECL_SIZE (F)) \
1346 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
1348 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1351 field_decl = TREE_CHAIN (field_decl);
1352 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1355 field_decl = TREE_CHAIN (field_decl);
1356 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
1358 #undef AT_LEAST_AS_LARGE_AS
1361 /* Avoid calling `check_string_class_template ()' more than once. */
1362 static GTY(()) int string_layout_checked;
1364 /* Custom build_string which sets TREE_TYPE! */
1367 my_build_string (int len, const char *str)
1369 return fix_string_type (build_string (len, str));
1372 /* Given a chain of STRING_CST's, build a static instance of
1373 NXConstantString which points at the concatenation of those
1374 strings. We place the string object in the __string_objects
1375 section of the __OBJC segment. The Objective-C runtime will
1376 initialize the isa pointers of the string objects to point at the
1377 NXConstantString class object. */
1380 build_objc_string_object (tree string)
1382 tree initlist, constructor, constant_string_class;
1386 string = fix_string_type (string);
1388 constant_string_class = lookup_interface (constant_string_id);
1389 if (!constant_string_class
1390 || !(constant_string_type
1391 = CLASS_STATIC_TEMPLATE (constant_string_class)))
1393 error ("cannot find interface declaration for `%s'",
1394 IDENTIFIER_POINTER (constant_string_id));
1395 return error_mark_node;
1398 /* Call to 'combine_strings' has been moved above. */
1399 TREE_SET_CODE (string, STRING_CST);
1400 length = TREE_STRING_LENGTH (string) - 1;
1402 if (!string_layout_checked)
1404 /* The NSConstantString/NXConstantString ivar layout is now
1406 if (!check_string_class_template ())
1408 error ("interface `%s' does not have valid constant string layout",
1409 IDENTIFIER_POINTER (constant_string_id));
1410 return error_mark_node;
1412 add_class_reference (constant_string_id);
1414 fields = TYPE_FIELDS (constant_string_type);
1416 /* & ((NXConstantString) { NULL, string, length }) */
1418 if (flag_next_runtime)
1420 /* For the NeXT runtime, we can generate a literal reference
1421 to the string class, don't need to run a constructor. */
1422 setup_string_decl ();
1423 if (string_class_decl == NULL_TREE)
1425 error ("cannot find reference tag for class `%s'",
1426 IDENTIFIER_POINTER (constant_string_id));
1427 return error_mark_node;
1429 initlist = build_tree_list
1431 copy_node (build_unary_op (ADDR_EXPR, string_class_decl, 0)));
1435 initlist = build_tree_list (fields, build_int_2 (0, 0));
1438 fields = TREE_CHAIN (fields);
1441 = tree_cons (fields, copy_node (build_unary_op (ADDR_EXPR, string, 1)),
1444 fields = TREE_CHAIN (fields);
1446 initlist = tree_cons (fields, build_int_2 (length, 0), initlist);
1447 constructor = objc_build_constructor (constant_string_type,
1448 nreverse (initlist));
1450 if (!flag_next_runtime)
1453 = objc_add_static_instance (constructor, constant_string_type);
1456 return (build_unary_op (ADDR_EXPR, constructor, 1));
1459 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
1461 static GTY(()) int num_static_inst;
1464 objc_add_static_instance (tree constructor, tree class_decl)
1469 /* Find the list of static instances for the CLASS_DECL. Create one if
1471 for (chain = &objc_static_instances;
1472 *chain && TREE_VALUE (*chain) != class_decl;
1473 chain = &TREE_CHAIN (*chain));
1476 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
1477 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
1480 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
1481 decl = build_decl (VAR_DECL, get_identifier (buf), class_decl);
1482 DECL_COMMON (decl) = 1;
1483 TREE_STATIC (decl) = 1;
1484 DECL_ARTIFICIAL (decl) = 1;
1485 DECL_INITIAL (decl) = constructor;
1487 /* We may be writing something else just now.
1488 Postpone till end of input. */
1489 DECL_DEFER_OUTPUT (decl) = 1;
1490 pushdecl_top_level (decl);
1491 rest_of_decl_compilation (decl, 0, 1, 0);
1493 /* Add the DECL to the head of this CLASS' list. */
1494 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
1499 /* Build a static constant CONSTRUCTOR
1500 with type TYPE and elements ELTS. */
1503 objc_build_constructor (tree type, tree elts)
1505 tree constructor, f, e;
1507 /* ??? Most of the places that we build constructors, we don't fill in
1508 the type of integers properly. Convert them all en masse. */
1509 if (TREE_CODE (type) == ARRAY_TYPE)
1511 f = TREE_TYPE (type);
1512 if (TREE_CODE (f) == POINTER_TYPE || TREE_CODE (f) == INTEGER_TYPE)
1513 for (e = elts; e ; e = TREE_CHAIN (e))
1514 TREE_VALUE (e) = convert (f, TREE_VALUE (e));
1518 f = TYPE_FIELDS (type);
1519 for (e = elts; e && f; e = TREE_CHAIN (e), f = TREE_CHAIN (f))
1520 if (TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE
1521 || TREE_CODE (TREE_TYPE (f)) == INTEGER_TYPE)
1522 TREE_VALUE (e) = convert (TREE_TYPE (f), TREE_VALUE (e));
1525 constructor = build_constructor (type, elts);
1526 TREE_CONSTANT (constructor) = 1;
1527 TREE_STATIC (constructor) = 1;
1528 TREE_READONLY (constructor) = 1;
1531 /* zlaski 2001-Apr-02: mark this as a call to a constructor, as required by
1532 build_unary_op (wasn't true in 2.7.2.1 days) */
1533 TREE_HAS_CONSTRUCTOR (constructor) = 1;
1538 /* Take care of defining and initializing _OBJC_SYMBOLS. */
1540 /* Predefine the following data type:
1548 void *defs[cls_def_cnt + cat_def_cnt];
1552 build_objc_symtab_template (void)
1554 tree field_decl, field_decl_chain;
1556 objc_symtab_template
1557 = start_struct (RECORD_TYPE, get_identifier (UTAG_SYMTAB));
1559 /* long sel_ref_cnt; */
1561 field_decl = create_builtin_decl (FIELD_DECL,
1562 long_integer_type_node,
1564 field_decl_chain = field_decl;
1568 field_decl = create_builtin_decl (FIELD_DECL,
1569 build_pointer_type (selector_type),
1571 chainon (field_decl_chain, field_decl);
1573 /* short cls_def_cnt; */
1575 field_decl = create_builtin_decl (FIELD_DECL,
1576 short_integer_type_node,
1578 chainon (field_decl_chain, field_decl);
1580 /* short cat_def_cnt; */
1582 field_decl = create_builtin_decl (FIELD_DECL,
1583 short_integer_type_node,
1585 chainon (field_decl_chain, field_decl);
1587 if (imp_count || cat_count || !flag_next_runtime)
1589 /* void *defs[imp_count + cat_count (+ 1)]; */
1590 /* NB: The index is one less than the size of the array. */
1591 int index = imp_count + cat_count
1592 + (flag_next_runtime? -1: 0);
1593 field_decl = create_builtin_decl
1597 build_index_type (build_int_2 (index, 0))),
1599 chainon (field_decl_chain, field_decl);
1602 finish_struct (objc_symtab_template, field_decl_chain, NULL_TREE);
1605 /* Create the initial value for the `defs' field of _objc_symtab.
1606 This is a CONSTRUCTOR. */
1609 init_def_list (tree type)
1611 tree expr, initlist = NULL_TREE;
1612 struct imp_entry *impent;
1615 for (impent = imp_list; impent; impent = impent->next)
1617 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
1619 expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
1620 initlist = tree_cons (NULL_TREE, expr, initlist);
1625 for (impent = imp_list; impent; impent = impent->next)
1627 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
1629 expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
1630 initlist = tree_cons (NULL_TREE, expr, initlist);
1634 if (!flag_next_runtime)
1636 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
1639 if (static_instances_decl)
1640 expr = build_unary_op (ADDR_EXPR, static_instances_decl, 0);
1642 expr = build_int_2 (0, 0);
1644 initlist = tree_cons (NULL_TREE, expr, initlist);
1647 return objc_build_constructor (type, nreverse (initlist));
1650 /* Construct the initial value for all of _objc_symtab. */
1653 init_objc_symtab (tree type)
1657 /* sel_ref_cnt = { ..., 5, ... } */
1659 initlist = build_tree_list (NULL_TREE, build_int_2 (0, 0));
1661 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
1663 if (flag_next_runtime || ! sel_ref_chain)
1664 initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
1666 initlist = tree_cons (NULL_TREE,
1667 build_unary_op (ADDR_EXPR,
1668 UOBJC_SELECTOR_TABLE_decl, 1),
1671 /* cls_def_cnt = { ..., 5, ... } */
1673 initlist = tree_cons (NULL_TREE, build_int_2 (imp_count, 0), initlist);
1675 /* cat_def_cnt = { ..., 5, ... } */
1677 initlist = tree_cons (NULL_TREE, build_int_2 (cat_count, 0), initlist);
1679 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
1681 if (imp_count || cat_count || !flag_next_runtime)
1684 tree field = TYPE_FIELDS (type);
1685 field = TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (field))));
1687 initlist = tree_cons (NULL_TREE, init_def_list (TREE_TYPE (field)),
1691 return objc_build_constructor (type, nreverse (initlist));
1694 /* Generate forward declarations for metadata such as
1695 'OBJC_CLASS_...'. */
1698 build_metadata_decl (const char *name, tree type)
1700 tree decl, decl_specs;
1701 /* extern struct TYPE NAME_<name>; */
1702 decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_EXTERN]);
1703 decl_specs = tree_cons (NULL_TREE, type, decl_specs);
1704 decl = define_decl (synth_id_with_class_suffix
1706 objc_implementation_context),
1708 TREE_USED (decl) = 1;
1709 DECL_ARTIFICIAL (decl) = 1;
1710 TREE_PUBLIC (decl) = 0;
1714 /* Push forward-declarations of all the categories so that
1715 init_def_list can use them in a CONSTRUCTOR. */
1718 forward_declare_categories (void)
1720 struct imp_entry *impent;
1721 tree sav = objc_implementation_context;
1723 for (impent = imp_list; impent; impent = impent->next)
1725 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
1727 /* Set an invisible arg to synth_id_with_class_suffix. */
1728 objc_implementation_context = impent->imp_context;
1729 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
1730 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
1731 objc_category_template);
1734 objc_implementation_context = sav;
1737 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
1738 and initialized appropriately. */
1741 generate_objc_symtab_decl (void)
1745 if (!objc_category_template)
1746 build_category_template ();
1748 /* forward declare categories */
1750 forward_declare_categories ();
1752 if (!objc_symtab_template)
1753 build_objc_symtab_template ();
1755 sc_spec = build_tree_list (NULL_TREE, ridpointers[(int) RID_STATIC]);
1757 UOBJC_SYMBOLS_decl = start_decl (get_identifier ("_OBJC_SYMBOLS"),
1758 tree_cons (NULL_TREE,
1759 objc_symtab_template, sc_spec),
1763 TREE_USED (UOBJC_SYMBOLS_decl) = 1;
1764 DECL_IGNORED_P (UOBJC_SYMBOLS_decl) = 1;
1765 DECL_ARTIFICIAL (UOBJC_SYMBOLS_decl) = 1;
1766 finish_decl (UOBJC_SYMBOLS_decl,
1767 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)),
1772 init_module_descriptor (tree type)
1774 tree initlist, expr;
1776 /* version = { 1, ... } */
1778 expr = build_int_2 (OBJC_VERSION, 0);
1779 initlist = build_tree_list (NULL_TREE, expr);
1781 /* size = { ..., sizeof (struct objc_module), ... } */
1783 expr = size_in_bytes (objc_module_template);
1784 initlist = tree_cons (NULL_TREE, expr, initlist);
1786 /* name = { ..., "foo.m", ... } */
1788 expr = add_objc_string (get_identifier (input_filename), class_names);
1789 initlist = tree_cons (NULL_TREE, expr, initlist);
1791 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
1793 if (UOBJC_SYMBOLS_decl)
1794 expr = build_unary_op (ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
1796 expr = build_int_2 (0, 0);
1797 initlist = tree_cons (NULL_TREE, expr, initlist);
1799 return objc_build_constructor (type, nreverse (initlist));
1802 /* Write out the data structures to describe Objective C classes defined.
1803 If appropriate, compile and output a setup function to initialize them.
1804 Return a symbol_ref to the function to call to initialize the Objective C
1805 data structures for this file (and perhaps for other files also).
1807 struct objc_module { ... } _OBJC_MODULE = { ... }; */
1810 build_module_descriptor (void)
1812 tree decl_specs, field_decl, field_decl_chain;
1814 objc_module_template
1815 = start_struct (RECORD_TYPE, get_identifier (UTAG_MODULE));
1819 decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_LONG]);
1820 field_decl = get_identifier ("version");
1821 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
1822 field_decl_chain = field_decl;
1826 decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_LONG]);
1827 field_decl = get_identifier ("size");
1828 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
1829 chainon (field_decl_chain, field_decl);
1833 decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
1834 field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("name"));
1835 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
1836 chainon (field_decl_chain, field_decl);
1838 /* struct objc_symtab *symtab; */
1840 decl_specs = get_identifier (UTAG_SYMTAB);
1841 decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE, decl_specs));
1842 field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("symtab"));
1843 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
1844 chainon (field_decl_chain, field_decl);
1846 finish_struct (objc_module_template, field_decl_chain, NULL_TREE);
1848 /* Create an instance of "objc_module". */
1850 decl_specs = tree_cons (NULL_TREE, objc_module_template,
1851 build_tree_list (NULL_TREE,
1852 ridpointers[(int) RID_STATIC]));
1854 UOBJC_MODULES_decl = start_decl (get_identifier ("_OBJC_MODULES"),
1855 decl_specs, 1, NULL_TREE);
1857 DECL_ARTIFICIAL (UOBJC_MODULES_decl) = 1;
1858 DECL_IGNORED_P (UOBJC_MODULES_decl) = 1;
1859 DECL_CONTEXT (UOBJC_MODULES_decl) = NULL_TREE;
1861 finish_decl (UOBJC_MODULES_decl,
1862 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)),
1865 /* Mark the decl to avoid "defined but not used" warning. */
1866 DECL_IN_SYSTEM_HEADER (UOBJC_MODULES_decl) = 1;
1868 /* Generate a constructor call for the module descriptor.
1869 This code was generated by reading the grammar rules
1870 of c-parse.in; Therefore, it may not be the most efficient
1871 way of generating the requisite code. */
1873 if (flag_next_runtime)
1877 tree parms, execclass_decl, decelerator, void_list_node_1;
1878 tree init_function_name, init_function_decl;
1880 /* Declare void __objc_execClass (void *); */
1882 void_list_node_1 = build_tree_list (NULL_TREE, void_type_node);
1883 execclass_decl = build_decl (FUNCTION_DECL,
1884 get_identifier (TAG_EXECCLASS),
1885 build_function_type (void_type_node,
1886 tree_cons (NULL_TREE, ptr_type_node,
1889 DECL_EXTERNAL (execclass_decl) = 1;
1890 DECL_ARTIFICIAL (execclass_decl) = 1;
1891 TREE_PUBLIC (execclass_decl) = 1;
1892 pushdecl (execclass_decl);
1893 rest_of_decl_compilation (execclass_decl, 0, 0, 0);
1894 assemble_external (execclass_decl);
1896 /* void _GLOBAL_$I$<gnyf> () {objc_execClass (&L_OBJC_MODULES);} */
1898 init_function_name = get_file_function_name ('I');
1899 start_function (void_list_node_1,
1900 build_nt (CALL_EXPR, init_function_name,
1901 tree_cons (NULL_TREE, NULL_TREE,
1905 store_parm_decls ();
1907 init_function_decl = current_function_decl;
1908 TREE_PUBLIC (init_function_decl) = ! targetm.have_ctors_dtors;
1909 TREE_USED (init_function_decl) = 1;
1910 /* Don't let this one be deferred. */
1911 DECL_INLINE (init_function_decl) = 0;
1912 DECL_UNINLINABLE (init_function_decl) = 1;
1913 current_function_cannot_inline
1914 = "static constructors and destructors cannot be inlined";
1917 = build_tree_list (NULL_TREE,
1918 build_unary_op (ADDR_EXPR, UOBJC_MODULES_decl, 0));
1919 decelerator = build_function_call (execclass_decl, parms);
1921 c_expand_expr_stmt (decelerator);
1925 return XEXP (DECL_RTL (init_function_decl), 0);
1929 /* Return the DECL of the string IDENT in the SECTION. */
1932 get_objc_string_decl (tree ident, enum string_section section)
1936 if (section == class_names)
1937 chain = class_names_chain;
1938 else if (section == meth_var_names)
1939 chain = meth_var_names_chain;
1940 else if (section == meth_var_types)
1941 chain = meth_var_types_chain;
1945 for (; chain != 0; chain = TREE_CHAIN (chain))
1946 if (TREE_VALUE (chain) == ident)
1947 return (TREE_PURPOSE (chain));
1953 /* Output references to all statically allocated objects. Return the DECL
1954 for the array built. */
1957 generate_static_references (void)
1959 tree decls = NULL_TREE, ident, decl_spec, expr_decl, expr = NULL_TREE;
1960 tree class_name, class, decl, initlist;
1961 tree cl_chain, in_chain, type;
1962 int num_inst, num_class;
1965 if (flag_next_runtime)
1968 for (cl_chain = objc_static_instances, num_class = 0;
1969 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
1971 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
1972 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
1974 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
1975 ident = get_identifier (buf);
1977 expr_decl = build_nt (ARRAY_REF, ident, NULL_TREE);
1978 decl_spec = tree_cons (NULL_TREE, build_pointer_type (void_type_node),
1979 build_tree_list (NULL_TREE,
1980 ridpointers[(int) RID_STATIC]));
1981 decl = start_decl (expr_decl, decl_spec, 1, NULL_TREE);
1982 DECL_CONTEXT (decl) = 0;
1983 DECL_ARTIFICIAL (decl) = 1;
1985 /* Output {class_name, ...}. */
1986 class = TREE_VALUE (cl_chain);
1987 class_name = get_objc_string_decl (OBJC_TYPE_NAME (class), class_names);
1988 initlist = build_tree_list (NULL_TREE,
1989 build_unary_op (ADDR_EXPR, class_name, 1));
1991 /* Output {..., instance, ...}. */
1992 for (in_chain = TREE_PURPOSE (cl_chain);
1993 in_chain; in_chain = TREE_CHAIN (in_chain))
1995 expr = build_unary_op (ADDR_EXPR, TREE_VALUE (in_chain), 1);
1996 initlist = tree_cons (NULL_TREE, expr, initlist);
1999 /* Output {..., NULL}. */
2000 initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
2002 expr = objc_build_constructor (TREE_TYPE (decl), nreverse (initlist));
2003 finish_decl (decl, expr, NULL_TREE);
2004 TREE_USED (decl) = 1;
2006 type = build_array_type (build_pointer_type (void_type_node), 0);
2007 decl = build_decl (VAR_DECL, ident, type);
2008 TREE_USED (decl) = 1;
2009 TREE_STATIC (decl) = 1;
2011 = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, decl, 1), decls);
2014 decls = tree_cons (NULL_TREE, build_int_2 (0, 0), decls);
2015 ident = get_identifier ("_OBJC_STATIC_INSTANCES");
2016 expr_decl = build_nt (ARRAY_REF, ident, NULL_TREE);
2017 decl_spec = tree_cons (NULL_TREE, build_pointer_type (void_type_node),
2018 build_tree_list (NULL_TREE,
2019 ridpointers[(int) RID_STATIC]));
2020 static_instances_decl
2021 = start_decl (expr_decl, decl_spec, 1, NULL_TREE);
2022 TREE_USED (static_instances_decl) = 1;
2023 DECL_CONTEXT (static_instances_decl) = 0;
2024 DECL_ARTIFICIAL (static_instances_decl) = 1;
2025 expr = objc_build_constructor (TREE_TYPE (static_instances_decl),
2027 finish_decl (static_instances_decl, expr, NULL_TREE);
2030 /* Output all strings. */
2033 generate_strings (void)
2035 tree sc_spec, decl_specs, expr_decl;
2036 tree chain, string_expr;
2039 for (chain = class_names_chain; chain; chain = TREE_CHAIN (chain))
2041 string = TREE_VALUE (chain);
2042 decl = TREE_PURPOSE (chain);
2044 = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
2045 decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], sc_spec);
2046 expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULL_TREE);
2047 decl = start_decl (expr_decl, decl_specs, 1, NULL_TREE);
2048 DECL_CONTEXT (decl) = NULL_TREE;
2049 string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
2050 IDENTIFIER_POINTER (string));
2051 finish_decl (decl, string_expr, NULL_TREE);
2054 for (chain = meth_var_names_chain; chain; chain = TREE_CHAIN (chain))
2056 string = TREE_VALUE (chain);
2057 decl = TREE_PURPOSE (chain);
2059 = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
2060 decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], sc_spec);
2061 expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULL_TREE);
2062 decl = start_decl (expr_decl, decl_specs, 1, NULL_TREE);
2063 DECL_CONTEXT (decl) = NULL_TREE;
2064 string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
2065 IDENTIFIER_POINTER (string));
2066 finish_decl (decl, string_expr, NULL_TREE);
2069 for (chain = meth_var_types_chain; chain; chain = TREE_CHAIN (chain))
2071 string = TREE_VALUE (chain);
2072 decl = TREE_PURPOSE (chain);
2074 = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
2075 decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], sc_spec);
2076 expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULL_TREE);
2077 decl = start_decl (expr_decl, decl_specs, 1, NULL_TREE);
2078 DECL_CONTEXT (decl) = NULL_TREE;
2079 string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
2080 IDENTIFIER_POINTER (string));
2081 finish_decl (decl, string_expr, NULL_TREE);
2085 static GTY(()) int selector_reference_idx;
2088 build_selector_reference_decl (void)
2093 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
2095 ident = get_identifier (buf);
2097 decl = build_decl (VAR_DECL, ident, selector_type);
2098 DECL_EXTERNAL (decl) = 1;
2099 TREE_PUBLIC (decl) = 0;
2100 TREE_USED (decl) = 1;
2101 DECL_ARTIFICIAL (decl) = 1;
2102 DECL_CONTEXT (decl) = 0;
2104 make_decl_rtl (decl, 0);
2105 pushdecl_top_level (decl);
2110 /* Just a handy wrapper for add_objc_string. */
2113 build_selector (tree ident)
2115 tree expr = add_objc_string (ident, meth_var_names);
2116 if (flag_typed_selectors)
2119 return build_c_cast (selector_type, expr); /* cast! */
2123 build_selector_translation_table (void)
2125 tree sc_spec, decl_specs;
2126 tree chain, initlist = NULL_TREE;
2128 tree decl = NULL_TREE, var_decl, name;
2130 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
2134 if (warn_selector && objc_implementation_context)
2138 for (method_chain = meth_var_names_chain;
2140 method_chain = TREE_CHAIN (method_chain))
2142 if (TREE_VALUE (method_chain) == TREE_VALUE (chain))
2150 /* Adjust line number for warning message. */
2151 int save_lineno = input_line;
2152 if (flag_next_runtime && TREE_PURPOSE (chain))
2153 input_line = DECL_SOURCE_LINE (TREE_PURPOSE (chain));
2154 warning ("creating selector for non existant method %s",
2155 IDENTIFIER_POINTER (TREE_VALUE (chain)));
2156 input_line = save_lineno;
2160 expr = build_selector (TREE_VALUE (chain));
2162 if (flag_next_runtime)
2164 name = DECL_NAME (TREE_PURPOSE (chain));
2166 sc_spec = build_tree_list (NULL_TREE, ridpointers[(int) RID_STATIC]);
2168 /* static SEL _OBJC_SELECTOR_REFERENCES_n = ...; */
2169 decl_specs = tree_cons (NULL_TREE, selector_type, sc_spec);
2173 /* The `decl' that is returned from start_decl is the one that we
2174 forward declared in `build_selector_reference' */
2175 decl = start_decl (var_decl, decl_specs, 1, NULL_TREE );
2178 /* add one for the '\0' character */
2179 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;
2181 if (flag_next_runtime)
2182 finish_decl (decl, expr, NULL_TREE);
2185 if (flag_typed_selectors)
2187 tree eltlist = NULL_TREE;
2188 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
2189 eltlist = tree_cons (NULL_TREE, expr, NULL_TREE);
2190 eltlist = tree_cons (NULL_TREE, encoding, eltlist);
2191 expr = objc_build_constructor (objc_selector_template,
2192 nreverse (eltlist));
2194 initlist = tree_cons (NULL_TREE, expr, initlist);
2199 if (! flag_next_runtime)
2201 /* Cause the variable and its initial value to be actually output. */
2202 DECL_EXTERNAL (UOBJC_SELECTOR_TABLE_decl) = 0;
2203 TREE_STATIC (UOBJC_SELECTOR_TABLE_decl) = 1;
2204 /* NULL terminate the list and fix the decl for output. */
2205 initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
2206 DECL_INITIAL (UOBJC_SELECTOR_TABLE_decl) = objc_ellipsis_node;
2207 initlist = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
2208 nreverse (initlist));
2209 finish_decl (UOBJC_SELECTOR_TABLE_decl, initlist, NULL_TREE);
2210 current_function_decl = NULL_TREE;
2215 get_proto_encoding (tree proto)
2220 if (! METHOD_ENCODING (proto))
2222 encoding = encode_method_prototype (proto);
2223 METHOD_ENCODING (proto) = encoding;
2226 encoding = METHOD_ENCODING (proto);
2228 return add_objc_string (encoding, meth_var_types);
2231 return build_int_2 (0, 0);
2234 /* sel_ref_chain is a list whose "value" fields will be instances of
2235 identifier_node that represent the selector. */
2238 build_typed_selector_reference (tree ident, tree prototype)
2240 tree *chain = &sel_ref_chain;
2246 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
2247 goto return_at_index;
2250 chain = &TREE_CHAIN (*chain);
2253 *chain = tree_cons (prototype, ident, NULL_TREE);
2256 expr = build_unary_op (ADDR_EXPR,
2257 build_array_ref (UOBJC_SELECTOR_TABLE_decl,
2258 build_int_2 (index, 0)),
2260 return build_c_cast (selector_type, expr);
2264 build_selector_reference (tree ident)
2266 tree *chain = &sel_ref_chain;
2272 if (TREE_VALUE (*chain) == ident)
2273 return (flag_next_runtime
2274 ? TREE_PURPOSE (*chain)
2275 : build_array_ref (UOBJC_SELECTOR_TABLE_decl,
2276 build_int_2 (index, 0)));
2279 chain = &TREE_CHAIN (*chain);
2282 expr = build_selector_reference_decl ();
2284 *chain = tree_cons (expr, ident, NULL_TREE);
2286 return (flag_next_runtime
2288 : build_array_ref (UOBJC_SELECTOR_TABLE_decl,
2289 build_int_2 (index, 0)));
2292 static GTY(()) int class_reference_idx;
2295 build_class_reference_decl (void)
2300 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
2302 ident = get_identifier (buf);
2304 decl = build_decl (VAR_DECL, ident, objc_class_type);
2305 DECL_EXTERNAL (decl) = 1;
2306 TREE_PUBLIC (decl) = 0;
2307 TREE_USED (decl) = 1;
2308 DECL_CONTEXT (decl) = 0;
2309 DECL_ARTIFICIAL (decl) = 1;
2311 make_decl_rtl (decl, 0);
2312 pushdecl_top_level (decl);
2317 /* Create a class reference, but don't create a variable to reference
2321 add_class_reference (tree ident)
2325 if ((chain = cls_ref_chain))
2330 if (ident == TREE_VALUE (chain))
2334 chain = TREE_CHAIN (chain);
2338 /* Append to the end of the list */
2339 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
2342 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
2345 /* Get a class reference, creating it if necessary. Also create the
2346 reference variable. */
2349 get_class_reference (tree ident)
2354 if (processing_template_decl)
2355 /* Must wait until template instantiation time. */
2356 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
2357 if (TREE_CODE (ident) == TYPE_DECL)
2358 ident = DECL_NAME (ident);
2362 if (!(ident = is_class_name (ident)))
2364 error ("`%s' is not an Objective-C class name or alias",
2365 IDENTIFIER_POINTER (orig_ident));
2366 return error_mark_node;
2369 if (flag_next_runtime && !flag_zero_link)
2374 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
2375 if (TREE_VALUE (*chain) == ident)
2377 if (! TREE_PURPOSE (*chain))
2378 TREE_PURPOSE (*chain) = build_class_reference_decl ();
2380 return TREE_PURPOSE (*chain);
2383 decl = build_class_reference_decl ();
2384 *chain = tree_cons (decl, ident, NULL_TREE);
2391 add_class_reference (ident);
2393 params = build_tree_list (NULL_TREE,
2394 my_build_string (IDENTIFIER_LENGTH (ident) + 1,
2395 IDENTIFIER_POINTER (ident)));
2397 assemble_external (objc_get_class_decl);
2398 return build_function_call (objc_get_class_decl, params);
2402 /* For each string section we have a chain which maps identifier nodes
2403 to decls for the strings. */
2406 add_objc_string (tree ident, enum string_section section)
2410 if (section == class_names)
2411 chain = &class_names_chain;
2412 else if (section == meth_var_names)
2413 chain = &meth_var_names_chain;
2414 else if (section == meth_var_types)
2415 chain = &meth_var_types_chain;
2421 if (TREE_VALUE (*chain) == ident)
2422 return build_unary_op (ADDR_EXPR, TREE_PURPOSE (*chain), 1);
2424 chain = &TREE_CHAIN (*chain);
2427 decl = build_objc_string_decl (section);
2429 *chain = tree_cons (decl, ident, NULL_TREE);
2431 return build_unary_op (ADDR_EXPR, decl, 1);
2434 static GTY(()) int class_names_idx;
2435 static GTY(()) int meth_var_names_idx;
2436 static GTY(()) int meth_var_types_idx;
2439 build_objc_string_decl (enum string_section section)
2444 if (section == class_names)
2445 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
2446 else if (section == meth_var_names)
2447 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
2448 else if (section == meth_var_types)
2449 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
2451 ident = get_identifier (buf);
2453 decl = build_decl (VAR_DECL, ident, build_array_type (char_type_node, 0));
2454 DECL_EXTERNAL (decl) = 1;
2455 TREE_PUBLIC (decl) = 0;
2456 TREE_USED (decl) = 1;
2457 TREE_CONSTANT (decl) = 1;
2458 DECL_CONTEXT (decl) = 0;
2459 DECL_ARTIFICIAL (decl) = 1;
2461 make_decl_rtl (decl, 0);
2462 pushdecl_top_level (decl);
2469 objc_declare_alias (tree alias_ident, tree class_ident)
2471 tree underlying_class;
2474 if (current_namespace != global_namespace) {
2475 error ("Objective-C declarations may only appear in global scope");
2477 #endif /* OBJCPLUS */
2479 if (!(underlying_class = is_class_name (class_ident)))
2480 warning ("cannot find class `%s'", IDENTIFIER_POINTER (class_ident));
2481 else if (is_class_name (alias_ident))
2482 warning ("class `%s' already exists", IDENTIFIER_POINTER (alias_ident));
2484 alias_chain = tree_cons (underlying_class, alias_ident, alias_chain);
2488 objc_declare_class (tree ident_list)
2492 if (current_namespace != global_namespace) {
2493 error ("Objective-C declarations may only appear in global scope");
2495 #endif /* OBJCPLUS */
2497 for (list = ident_list; list; list = TREE_CHAIN (list))
2499 tree ident = TREE_VALUE (list);
2501 if (! is_class_name (ident))
2503 tree record = lookup_name (ident);
2505 if (record && ! TREE_STATIC_TEMPLATE (record))
2507 error ("`%s' redeclared as different kind of symbol",
2508 IDENTIFIER_POINTER (ident));
2509 error ("%Jprevious declaration of '%D'",
2513 record = xref_tag (RECORD_TYPE, ident);
2514 TREE_STATIC_TEMPLATE (record) = 1;
2515 class_chain = tree_cons (NULL_TREE, ident, class_chain);
2521 is_class_name (tree ident)
2525 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
2526 && identifier_global_value (ident))
2527 ident = identifier_global_value (ident);
2528 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
2529 ident = TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
2532 if (ident && TREE_CODE (ident) == RECORD_TYPE)
2533 ident = TYPE_NAME (ident);
2534 if (ident && TREE_CODE (ident) == TYPE_DECL)
2535 ident = DECL_NAME (ident);
2537 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
2540 if (lookup_interface (ident))
2543 for (chain = class_chain; chain; chain = TREE_CHAIN (chain))
2545 if (ident == TREE_VALUE (chain))
2549 for (chain = alias_chain; chain; chain = TREE_CHAIN (chain))
2551 if (ident == TREE_VALUE (chain))
2552 return TREE_PURPOSE (chain);
2558 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
2559 class instance. This is needed by other parts of the compiler to
2560 handle ObjC types gracefully. */
2563 objc_is_object_ptr (tree type)
2565 type = TYPE_MAIN_VARIANT (type);
2566 if (!type || TREE_CODE (type) != POINTER_TYPE)
2568 /* NB: This function may be called before the ObjC front-end has
2569 been initialized, in which case ID_TYPE will be NULL. */
2570 if (id_type && type && TYPE_P (type)
2572 || TREE_TYPE (type) == TREE_TYPE (objc_class_type)))
2574 return is_class_name (OBJC_TYPE_NAME (TREE_TYPE (type)));
2578 lookup_interface (tree ident)
2583 if (ident && TREE_CODE (ident) == TYPE_DECL)
2584 ident = DECL_NAME (ident);
2586 for (chain = interface_chain; chain; chain = TREE_CHAIN (chain))
2588 if (ident == CLASS_NAME (chain))
2594 /* Implement @defs (<classname>) within struct bodies. */
2597 get_class_ivars_from_name (tree class_name)
2599 tree interface = lookup_interface (class_name);
2600 tree field, fields = NULL_TREE;
2604 tree raw_ivar = get_class_ivars (interface, 1);
2606 /* Regenerate the FIELD_DECLs for the enclosing struct. */
2607 for (; raw_ivar; raw_ivar = TREE_CHAIN (raw_ivar))
2609 field = grokfield (TREE_PURPOSE (TREE_VALUE (raw_ivar)),
2610 TREE_PURPOSE (raw_ivar),
2611 TREE_VALUE (TREE_VALUE (raw_ivar)));
2613 finish_member_declaration (field);
2615 fields = chainon (fields, field);
2620 error ("cannot find interface declaration for `%s'",
2621 IDENTIFIER_POINTER (class_name));
2626 /* Used by: build_private_template, continue_class,
2627 and for @defs constructs. */
2630 get_class_ivars (tree interface, int raw)
2632 tree my_name, super_name, ivar_chain;
2634 my_name = CLASS_NAME (interface);
2635 super_name = CLASS_SUPER_NAME (interface);
2637 ivar_chain = CLASS_RAW_IVARS (interface);
2640 ivar_chain = CLASS_IVARS (interface);
2641 /* Save off a pristine copy of the leaf ivars (i.e, those not
2642 inherited from a super class). */
2643 if (!CLASS_OWN_IVARS (interface))
2644 CLASS_OWN_IVARS (interface) = copy_list (ivar_chain);
2650 tree super_interface = lookup_interface (super_name);
2652 if (!super_interface)
2654 /* fatal did not work with 2 args...should fix */
2655 error ("cannot find interface declaration for `%s', superclass of `%s'",
2656 IDENTIFIER_POINTER (super_name),
2657 IDENTIFIER_POINTER (my_name));
2658 exit (FATAL_EXIT_CODE);
2661 if (super_interface == interface)
2662 fatal_error ("circular inheritance in interface declaration for `%s'",
2663 IDENTIFIER_POINTER (super_name));
2665 interface = super_interface;
2666 my_name = CLASS_NAME (interface);
2667 super_name = CLASS_SUPER_NAME (interface);
2669 op1 = (raw ? CLASS_RAW_IVARS (interface) : CLASS_OWN_IVARS (interface));
2672 tree head = copy_list (op1);
2674 /* Prepend super class ivars...make a copy of the list, we
2675 do not want to alter the original. */
2676 chainon (head, ivar_chain);
2685 objc_enter_block (void)
2690 block = begin_compound_stmt (0);
2692 block = c_begin_compound_stmt ();
2695 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
2698 objc_exception_block_stack = tree_cons (NULL_TREE, block,
2699 objc_exception_block_stack);
2701 blk_nesting_count++;
2706 objc_exit_block (void)
2708 tree block = TREE_VALUE (objc_exception_block_stack);
2710 tree scope_stmt, inner;
2713 objc_clear_super_receiver ();
2715 finish_compound_stmt (0, block);
2717 scope_stmt = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
2718 inner = pop_scope ();
2720 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmt))
2721 = SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmt))
2723 RECHAIN_STMTS (block, COMPOUND_BODY (block));
2725 last_expr_type = NULL_TREE;
2726 objc_exception_block_stack = TREE_CHAIN (objc_exception_block_stack);
2728 blk_nesting_count--;
2733 objc_declare_variable (enum rid scspec, tree name, tree type, tree init)
2737 type = tree_cons (NULL_TREE, type,
2738 tree_cons (NULL_TREE, ridpointers[(int) scspec],
2740 TREE_STATIC (type) = 1;
2741 decl = start_decl (name, type, (init != NULL_TREE), NULL_TREE);
2742 finish_decl (decl, init, NULL_TREE);
2743 /* This prevents `unused variable' warnings when compiling with -Wall. */
2744 TREE_USED (decl) = 1;
2745 DECL_ARTIFICIAL (decl) = 1;
2750 objc_build_throw_stmt (tree throw_expr)
2754 if (!flag_objc_exceptions)
2755 fatal_error ("Use `-fobjc-exceptions' to enable Objective-C exception syntax");
2757 if (!throw_expr && objc_caught_exception)
2758 throw_expr = TREE_VALUE (objc_caught_exception);
2762 error ("`@throw;' (rethrow) used outside of a `@catch' block");
2763 return error_mark_node;
2766 func_params = tree_cons (NULL_TREE, throw_expr, NULL_TREE);
2768 assemble_external (objc_exception_throw_decl);
2769 return c_expand_expr_stmt (build_function_call (objc_exception_throw_decl,
2774 val_stack_push (struct val_stack **nc, long val)
2776 struct val_stack *new_elem = xmalloc (sizeof (struct val_stack));
2777 new_elem->val = val;
2778 new_elem->next = *nc;
2783 val_stack_pop (struct val_stack **nc)
2785 struct val_stack *old_elem = *nc;
2786 *nc = old_elem->next;
2791 objc_build_try_enter_fragment (void)
2793 /* objc_exception_try_enter(&_stackExceptionData);
2794 if (!_setjmp(&_stackExceptionData.buf)) { */
2796 tree func_params, if_stmt, cond;
2799 = tree_cons (NULL_TREE,
2800 build_unary_op (ADDR_EXPR,
2801 TREE_VALUE (objc_stack_exception_data),
2805 assemble_external (objc_exception_try_enter_decl);
2806 c_expand_expr_stmt (build_function_call
2807 (objc_exception_try_enter_decl, func_params));
2809 if_stmt = c_begin_if_stmt ();
2811 /* If <setjmp.h> has been included, the _setjmp prototype has
2812 acquired a real, breathing type for its parameter. Cast our
2813 argument to that type. */
2815 = tree_cons (NULL_TREE,
2816 build_c_cast (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))
2817 ? TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
2821 build_component_ref (TREE_VALUE (objc_stack_exception_data),
2822 get_identifier ("buf")), 0)),
2824 assemble_external (objc_setjmp_decl);
2825 cond = build_unary_op (TRUTH_NOT_EXPR,
2826 build_function_call (objc_setjmp_decl, func_params),
2828 c_expand_start_cond (lang_hooks.truthvalue_conversion (cond), 0, if_stmt);
2829 objc_enter_block ();
2833 objc_build_extract_expr (void)
2835 /* ... = objc_exception_extract(&_stackExceptionData); */
2838 = tree_cons (NULL_TREE,
2839 build_unary_op (ADDR_EXPR,
2840 TREE_VALUE (objc_stack_exception_data), 0),
2843 assemble_external (objc_exception_extract_decl);
2844 return build_function_call (objc_exception_extract_decl, func_params);
2848 objc_build_try_exit_fragment (void)
2850 /* objc_exception_try_exit(&_stackExceptionData); */
2853 = tree_cons (NULL_TREE,
2854 build_unary_op (ADDR_EXPR,
2855 TREE_VALUE (objc_stack_exception_data), 0),
2858 assemble_external (objc_exception_try_exit_decl);
2859 c_expand_expr_stmt (build_function_call (objc_exception_try_exit_decl,
2864 objc_build_extract_fragment (void)
2867 _rethrowException = objc_exception_extract(&_stackExceptionData);
2873 c_expand_start_else ();
2874 objc_enter_block ();
2875 c_expand_expr_stmt (build_modify_expr
2876 (TREE_VALUE (objc_rethrow_exception),
2878 objc_build_extract_expr ()));
2881 c_expand_end_cond ();
2886 objc_build_try_prologue (void)
2889 struct _objc_exception_data _stackExceptionData;
2890 volatile id _rethrowException = nil;
2891 { // begin TRY-CATCH scope
2892 objc_exception_try_enter(&_stackExceptionData);
2893 if (!_setjmp(&_stackExceptionData.buf)) { */
2895 tree try_catch_block;
2897 if (!flag_objc_exceptions)
2898 fatal_error ("Use `-fobjc-exceptions' to enable Objective-C exception syntax");
2900 objc_mark_locals_volatile ((void *)(exc_binding_stack
2901 ? exc_binding_stack->val
2903 objc_enter_block ();
2904 objc_stack_exception_data
2905 = tree_cons (NULL_TREE,
2906 objc_declare_variable (RID_AUTO,
2907 get_identifier (UTAG_EXCDATA_VAR),
2908 xref_tag (RECORD_TYPE,
2909 get_identifier (UTAG_EXCDATA)),
2911 objc_stack_exception_data);
2912 objc_rethrow_exception = tree_cons (NULL_TREE,
2913 objc_declare_variable (RID_VOLATILE,
2914 get_identifier (UTAG_RETHROWEXC_VAR),
2916 build_int_2 (0, 0)),
2917 objc_rethrow_exception);
2919 try_catch_block = objc_enter_block ();
2920 val_stack_push (&exc_binding_stack, (long) get_current_scope ());
2921 objc_build_try_enter_fragment ();
2923 return try_catch_block;
2927 objc_build_try_epilogue (int also_catch_prologue)
2929 if (also_catch_prologue)
2932 register id _caughtException = objc_exception_extract( &_stackExceptionData);
2933 objc_exception_try_enter(&_stackExceptionData);
2934 if(!_setjmp(&_stackExceptionData.buf)) {
2942 c_expand_start_else ();
2943 objc_enter_block ();
2944 objc_caught_exception
2945 = tree_cons (NULL_TREE,
2946 objc_declare_variable (RID_REGISTER,
2947 get_identifier (UTAG_CAUGHTEXC_VAR),
2949 objc_build_extract_expr ()),
2950 objc_caught_exception);
2951 objc_build_try_enter_fragment ();
2952 val_stack_push (&catch_count_stack, 1);
2953 if_stmt = c_begin_if_stmt ();
2955 c_expand_start_cond (lang_hooks.truthvalue_conversion (boolean_false_node),
2957 objc_enter_block ();
2959 /* Start a new chain of @catch statements for this @try. */
2960 objc_catch_type = tree_cons (objc_catch_type, NULL_TREE, NULL_TREE);
2963 { /* !also_catch_prologue */
2966 _rethrowException = objc_exception_extract( &_stackExceptionData);
2969 objc_build_extract_fragment ();
2975 objc_build_catch_stmt (tree catch_expr)
2977 /* } else if (objc_exception_match(objc_get_class("SomeClass"), _caughtException)) {
2978 register SomeClass *e = _caughtException; */
2980 tree if_stmt, cond, func_params, prev_catch, var_name, var_type;
2984 /* Yet another C/C++ impedance mismatch. */
2985 catch_expr = TREE_PURPOSE (catch_expr);
2988 var_name = TREE_VALUE (catch_expr);
2989 var_type = TREE_VALUE (TREE_PURPOSE (catch_expr));
2990 if (TREE_CODE (var_name) == INDIRECT_REF)
2991 var_name = TREE_OPERAND (var_name, 0);
2992 if (TREE_CODE (var_type) == TYPE_DECL
2993 || TREE_CODE (var_type) == POINTER_TYPE)
2994 var_type = TREE_TYPE (var_type);
2995 catch_id = (var_type == TREE_TYPE (id_type));
2997 if (!flag_objc_exceptions)
2998 fatal_error ("Use `-fobjc-exceptions' to enable Objective-C exception syntax");
3000 if (!(catch_id || TYPED_OBJECT (var_type)))
3001 fatal_error ("`@catch' parameter is not a known Objective-C class type");
3003 /* Examine previous @catch clauses for the current @try block for
3004 superclasses of the 'var_type' class. */
3005 for (prev_catch = objc_catch_type; TREE_VALUE (prev_catch);
3006 prev_catch = TREE_CHAIN (prev_catch))
3008 if (TREE_VALUE (prev_catch) == TREE_TYPE (id_type))
3010 warning ("Exception already handled by preceding `@catch(id)'");
3014 && objc_comptypes (TREE_VALUE (prev_catch), var_type, 0) == 1)
3015 warning ("Exception of type `%s *' already handled by `@catch (%s *)'",
3016 IDENTIFIER_POINTER (OBJC_TYPE_NAME (var_type)),
3017 IDENTIFIER_POINTER (OBJC_TYPE_NAME (TREE_VALUE (prev_catch))));
3020 objc_catch_type = tree_cons (NULL_TREE, var_type, objc_catch_type);
3025 c_expand_start_else ();
3026 catch_count_stack->val++;
3027 if_stmt = c_begin_if_stmt ();
3031 cond = integer_one_node;
3034 cond = get_class_reference (OBJC_TYPE_NAME (var_type));
3037 = tree_cons (NULL_TREE, cond,
3038 tree_cons (NULL_TREE,
3039 TREE_VALUE (objc_caught_exception),
3041 assemble_external (objc_exception_match_decl);
3042 cond = build_function_call (objc_exception_match_decl, func_params);
3045 c_expand_start_cond (lang_hooks.truthvalue_conversion (cond), 0, if_stmt);
3046 objc_enter_block ();
3047 objc_declare_variable (RID_REGISTER, var_name,
3048 build_pointer_type (var_type),
3049 TREE_VALUE (objc_caught_exception));
3053 objc_build_catch_epilogue (void)
3056 _rethrowException = _caughtException;
3057 objc_exception_try_exit(&_stackExceptionData);
3060 _rethrowException = objc_exception_extract(&_stackExceptionData);
3063 } // end TRY-CATCH scope
3069 c_expand_start_else ();
3070 objc_enter_block ();
3073 (TREE_VALUE (objc_rethrow_exception),
3075 TREE_VALUE (objc_caught_exception)));
3076 objc_build_try_exit_fragment ();
3078 while (catch_count_stack->val--)
3080 c_finish_else (); /* close off all the nested ifs ! */
3081 c_expand_end_cond ();
3084 val_stack_pop (&catch_count_stack);
3085 objc_caught_exception = TREE_CHAIN (objc_caught_exception);
3087 objc_build_extract_fragment ();
3091 c_expand_end_cond ();
3095 /* Return to enclosing chain of @catch statements (if any). */
3096 while (TREE_VALUE (objc_catch_type))
3097 objc_catch_type = TREE_CHAIN (objc_catch_type);
3098 objc_catch_type = TREE_PURPOSE (objc_catch_type);
3102 objc_build_finally_prologue (void)
3104 /* { // begin FINALLY scope
3105 if (!_rethrowException) {
3106 objc_exception_try_exit(&_stackExceptionData);
3109 tree blk = objc_enter_block ();
3111 tree if_stmt = c_begin_if_stmt ();
3114 c_expand_start_cond (lang_hooks.truthvalue_conversion
3115 (build_unary_op (TRUTH_NOT_EXPR,
3116 TREE_VALUE (objc_rethrow_exception),
3119 objc_enter_block ();
3120 objc_build_try_exit_fragment ();
3123 c_expand_end_cond ();
3130 objc_build_finally_epilogue (void)
3132 /* if (_rethrowException) {
3133 objc_exception_throw(_rethrowException);
3135 } // end FINALLY scope
3138 tree if_stmt = c_begin_if_stmt ();
3142 (lang_hooks.truthvalue_conversion (TREE_VALUE (objc_rethrow_exception)),
3144 objc_enter_block ();
3145 objc_build_throw_stmt (TREE_VALUE (objc_rethrow_exception));
3148 c_expand_end_cond ();
3152 objc_rethrow_exception = TREE_CHAIN (objc_rethrow_exception);
3153 objc_stack_exception_data = TREE_CHAIN (objc_stack_exception_data);
3155 val_stack_pop (&exc_binding_stack);
3156 return objc_exit_block ();
3160 objc_build_try_catch_finally_stmt (int has_catch, int has_finally)
3162 /* NB: The operative assumption here is that TRY_FINALLY_EXPR will
3163 deal with all exits from 'try_catch_blk' and route them through
3165 tree outer_blk = objc_build_finally_epilogue ();
3166 tree prec_stmt = TREE_CHAIN (TREE_CHAIN (COMPOUND_BODY (outer_blk)));
3167 tree try_catch_blk = TREE_CHAIN (prec_stmt), try_catch_expr;
3168 tree finally_blk = TREE_CHAIN (try_catch_blk), finally_expr;
3169 tree succ_stmt = TREE_CHAIN (finally_blk);
3170 tree try_finally_stmt, try_finally_expr;
3172 if (!flag_objc_exceptions)
3173 fatal_error ("Use `-fobjc-exceptions' to enable Objective-C exception syntax");
3175 /* It is an error to have a @try block without a @catch and/or @finally
3176 (even though sensible code can be generated nonetheless). */
3178 if (!has_catch && !has_finally)
3179 error ("`@try' without `@catch' or `@finally'");
3181 /* We shall now do something truly disgusting. We shall remove the
3182 'try_catch_blk' and 'finally_blk' from the 'outer_blk' statement
3183 chain, and replace them with a TRY_FINALLY_EXPR statement! If
3184 this doesn't work, we will have to learn (from Per/gcj) how to
3185 construct the 'outer_blk' lazily. */
3187 TREE_CHAIN (try_catch_blk) = TREE_CHAIN (finally_blk) = NULL_TREE;
3188 try_catch_expr = build1 (STMT_EXPR, void_type_node, try_catch_blk);
3189 TREE_SIDE_EFFECTS (try_catch_expr) = 1;
3190 finally_expr = build1 (STMT_EXPR, void_type_node, finally_blk);
3191 TREE_SIDE_EFFECTS (finally_expr) = 1;
3192 try_finally_expr = build (TRY_FINALLY_EXPR, void_type_node, try_catch_expr,
3194 TREE_SIDE_EFFECTS (try_finally_expr) = 1;
3195 try_finally_stmt = build_stmt (EXPR_STMT, try_finally_expr);
3196 TREE_CHAIN (prec_stmt) = try_finally_stmt;
3197 TREE_CHAIN (try_finally_stmt) = succ_stmt;
3199 return outer_blk; /* the whole enchilada */
3203 objc_build_synchronized_prologue (tree sync_expr)
3206 id _eval_once = <sync_expr>;
3208 objc_sync_enter( _eval_once ); */
3212 if (!flag_objc_exceptions)
3213 fatal_error ("Use `-fobjc-exceptions' to enable Objective-C exception syntax");
3215 objc_enter_block ();
3217 = tree_cons (NULL_TREE,
3218 objc_declare_variable (RID_AUTO,
3219 get_identifier (UTAG_EVALONCE_VAR),
3223 objc_build_try_prologue ();
3224 objc_enter_block ();
3225 func_params = tree_cons (NULL_TREE,
3226 TREE_VALUE (objc_eval_once),
3229 assemble_external (objc_sync_enter_decl);
3230 c_expand_expr_stmt (build_function_call
3231 (objc_sync_enter_decl, func_params));
3235 objc_build_synchronized_epilogue (void)
3239 objc_sync_exit( _eval_once );
3246 objc_build_try_epilogue (0);
3247 objc_build_finally_prologue ();
3248 func_params = tree_cons (NULL_TREE, TREE_VALUE (objc_eval_once),
3251 assemble_external (objc_sync_exit_decl);
3252 c_expand_expr_stmt (build_function_call (objc_sync_exit_decl,
3254 objc_build_try_catch_finally_stmt (0, 1);
3256 return objc_exit_block ();
3259 /* Predefine the following data type:
3261 struct _objc_exception_data
3267 /* The following yuckiness should prevent users from having to #include
3268 <setjmp.h> in their code... */
3270 #ifdef TARGET_POWERPC
3271 /* snarfed from /usr/include/ppc/setjmp.h */
3272 #define _JBLEN (26 + 36 + 129 + 1)
3274 /* snarfed from /usr/include/i386/{setjmp,signal}.h */
3279 build_objc_exception_stuff (void)
3281 tree field_decl, field_decl_chain, index, temp_type;
3283 /* Suppress outputting debug symbols, because
3284 dbxout_init hasn't been called yet. */
3285 enum debug_info_type save_write_symbols = write_symbols;
3286 const struct gcc_debug_hooks *save_hooks = debug_hooks;
3288 write_symbols = NO_DEBUG;
3289 debug_hooks = &do_nothing_debug_hooks;
3290 objc_exception_data_template
3291 = start_struct (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
3293 /* int buf[_JBLEN]; */
3295 index = build_index_type (build_int_2 (_JBLEN - 1, 0));
3296 field_decl = create_builtin_decl (FIELD_DECL,
3297 build_array_type (integer_type_node, index),
3299 field_decl_chain = field_decl;
3301 /* void *pointers[4]; */
3303 index = build_index_type (build_int_2 (4 - 1, 0));
3304 field_decl = create_builtin_decl (FIELD_DECL,
3305 build_array_type (ptr_type_node, index),
3307 chainon (field_decl_chain, field_decl);
3309 finish_struct (objc_exception_data_template, field_decl_chain, NULL_TREE);
3311 /* int _setjmp(...); */
3312 /* If the user includes <setjmp.h>, this shall be superseded by
3313 'int _setjmp(jmp_buf);' */
3314 temp_type = build_function_type (integer_type_node, NULL_TREE);
3316 = builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3318 /* id objc_exception_extract(struct _objc_exception_data *); */
3320 = build_function_type (id_type,
3321 tree_cons (NULL_TREE,
3322 build_pointer_type (objc_exception_data_template),
3324 objc_exception_extract_decl
3325 = builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3326 /* void objc_exception_try_enter(struct _objc_exception_data *); */
3327 /* void objc_exception_try_exit(struct _objc_exception_data *); */
3329 = build_function_type (void_type_node,
3330 tree_cons (NULL_TREE,
3331 build_pointer_type (objc_exception_data_template),
3333 objc_exception_try_enter_decl
3334 = builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3335 objc_exception_try_exit_decl
3336 = builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3337 /* void objc_exception_throw(id) __attribute__((noreturn)); */
3338 /* void objc_sync_enter(id); */
3339 /* void objc_sync_exit(id); */
3340 temp_type = build_function_type (void_type_node,
3341 tree_cons (NULL_TREE, id_type,
3343 objc_exception_throw_decl
3344 = builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3345 DECL_ATTRIBUTES (objc_exception_throw_decl)
3346 = tree_cons (get_identifier ("noreturn"), NULL_TREE, NULL_TREE);
3347 objc_sync_enter_decl
3348 = builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3350 = builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3351 /* int objc_exception_match(id, id); */
3352 temp_type = build_function_type (integer_type_node,
3353 tree_cons (NULL_TREE, id_type,
3354 tree_cons (NULL_TREE, id_type,
3356 objc_exception_match_decl
3357 = builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
3359 write_symbols = save_write_symbols;
3360 debug_hooks = save_hooks;
3363 /* struct <classname> {
3364 struct objc_class *isa;
3369 build_private_template (tree class)
3373 if (CLASS_STATIC_TEMPLATE (class))
3375 uprivate_record = CLASS_STATIC_TEMPLATE (class);
3376 ivar_context = TYPE_FIELDS (CLASS_STATIC_TEMPLATE (class));
3380 uprivate_record = start_struct (RECORD_TYPE, CLASS_NAME (class));
3381 ivar_context = get_class_ivars (class, 0);
3383 finish_struct (uprivate_record, ivar_context, NULL_TREE);
3385 CLASS_STATIC_TEMPLATE (class) = uprivate_record;
3387 /* mark this record as class template - for class type checking */
3388 TREE_STATIC_TEMPLATE (uprivate_record) = 1;
3392 = groktypename (build_tree_list (build_tree_list (NULL_TREE,
3394 build1 (INDIRECT_REF, NULL_TREE,
3397 return ivar_context;
3400 /* Begin code generation for protocols... */
3402 /* struct objc_protocol {
3403 char *protocol_name;
3404 struct objc_protocol **protocol_list;
3405 struct objc_method_desc *instance_methods;
3406 struct objc_method_desc *class_methods;
3410 build_protocol_template (void)
3412 tree decl_specs, field_decl, field_decl_chain;
3415 template = start_struct (RECORD_TYPE, get_identifier (UTAG_PROTOCOL));
3417 /* struct objc_class *isa; */
3419 decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE,
3420 get_identifier (UTAG_CLASS)));
3421 field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("isa"));
3422 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
3423 field_decl_chain = field_decl;
3425 /* char *protocol_name; */
3427 decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
3429 = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("protocol_name"));
3430 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
3431 chainon (field_decl_chain, field_decl);
3433 /* struct objc_protocol **protocol_list; */
3435 decl_specs = build_tree_list (NULL_TREE, template);
3437 = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("protocol_list"));
3438 field_decl = build1 (INDIRECT_REF, NULL_TREE, field_decl);
3439 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
3440 chainon (field_decl_chain, field_decl);
3442 /* struct objc_method_list *instance_methods; */
3445 = build_tree_list (NULL_TREE,
3446 xref_tag (RECORD_TYPE,
3447 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3449 = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("instance_methods"));
3450 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
3451 chainon (field_decl_chain, field_decl);
3453 /* struct objc_method_list *class_methods; */
3456 = build_tree_list (NULL_TREE,
3457 xref_tag (RECORD_TYPE,
3458 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3460 = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("class_methods"));
3461 field_decl = grokfield (field_decl, decl_specs, NULL_TREE);
3462 chainon (field_decl_chain, field_decl);
3464 return finish_struct (template, field_decl_chain, NULL_TREE);
3468 build_descriptor_table_initializer (tree type, tree entries)
3470 tree initlist = NULL_TREE;
3474 tree eltlist = NULL_TREE;
3477 = tree_cons (NULL_TREE,
3478 build_selector (METHOD_SEL_NAME (entries)), NULL_TREE);
3480 = tree_cons (NULL_TREE,
3481 add_objc_string (METHOD_ENCODING (entries),
3486 = tree_cons (NULL_TREE,
3487 objc_build_constructor (type, nreverse (eltlist)),
3490 entries = TREE_CHAIN (entries);
3494 return objc_build_constructor (build_array_type (type, 0),
3495 nreverse (initlist));
3498 /* struct objc_method_prototype_list {
3500 struct objc_method_prototype {