In gcc/objc/: 2010-09-28 Nicola Pero <nicola.pero@meta-innovation.com>
[gcc.git] / gcc / objc / objc-act.c
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Steve Naroff.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28
29 #ifdef OBJCPLUS
30 #include "cp-tree.h"
31 #else
32 #include "c-tree.h"
33 #include "c-lang.h"
34 #endif
35
36 #include "c-family/c-common.h"
37 #include "c-family/c-pragma.h"
38 #include "flags.h"
39 #include "langhooks.h"
40 #include "objc-act.h"
41 #include "input.h"
42 #include "function.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "ggc.h"
46 #include "debug.h"
47 #include "target.h"
48 #include "diagnostic-core.h"
49 #include "intl.h"
50 #include "cgraph.h"
51 #include "tree-iterator.h"
52 #include "hashtab.h"
53 #include "langhooks-def.h"
54
55 /* For enum gimplify_status */
56 #include "gimple.h"
57
58 #define OBJC_VOID_AT_END void_list_node
59
60 static unsigned int should_call_super_dealloc = 0;
61
62 /* When building Objective-C++, we need in_late_binary_op. */
63 #ifdef OBJCPLUS
64 bool in_late_binary_op = false;
65 #endif /* OBJCPLUS */
66
67 /* When building Objective-C++, we are not linking against the C front-end
68 and so need to replicate the C tree-construction functions in some way. */
69 #ifdef OBJCPLUS
70 #define OBJCP_REMAP_FUNCTIONS
71 #include "objcp-decl.h"
72 #endif /* OBJCPLUS */
73
74 /* This is the default way of generating a method name. */
75 /* I am not sure it is really correct.
76 Perhaps there's a danger that it will make name conflicts
77 if method names contain underscores. -- rms. */
78 #ifndef OBJC_GEN_METHOD_LABEL
79 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
80 do { \
81 char *temp; \
82 sprintf ((BUF), "_%s_%s_%s_%s", \
83 ((IS_INST) ? "i" : "c"), \
84 (CLASS_NAME), \
85 ((CAT_NAME)? (CAT_NAME) : ""), \
86 (SEL_NAME)); \
87 for (temp = (BUF); *temp; temp++) \
88 if (*temp == ':') *temp = '_'; \
89 } while (0)
90 #endif
91
92 /* These need specifying. */
93 #ifndef OBJC_FORWARDING_STACK_OFFSET
94 #define OBJC_FORWARDING_STACK_OFFSET 0
95 #endif
96
97 #ifndef OBJC_FORWARDING_MIN_OFFSET
98 #define OBJC_FORWARDING_MIN_OFFSET 0
99 #endif
100 \f
101 /* Set up for use of obstacks. */
102
103 #include "obstack.h"
104
105 /* This obstack is used to accumulate the encoding of a data type. */
106 static struct obstack util_obstack;
107
108 /* This points to the beginning of obstack contents, so we can free
109 the whole contents. */
110 char *util_firstobj;
111
112 /* The version identifies which language generation and runtime
113 the module (file) was compiled for, and is recorded in the
114 module descriptor. */
115
116 #define OBJC_VERSION (flag_next_runtime ? 6 : 8)
117 #define PROTOCOL_VERSION 2
118
119 /* (Decide if these can ever be validly changed.) */
120 #define OBJC_ENCODE_INLINE_DEFS 0
121 #define OBJC_ENCODE_DONT_INLINE_DEFS 1
122
123 /*** Private Interface (procedures) ***/
124
125 /* Used by compile_file. */
126
127 static void init_objc (void);
128 static void finish_objc (void);
129
130 /* Code generation. */
131
132 static tree objc_build_constructor (tree, VEC(constructor_elt,gc) *);
133 static tree build_objc_method_call (location_t, int, tree, tree, tree, tree);
134 static tree get_proto_encoding (tree);
135 static tree lookup_interface (tree);
136 static tree objc_add_static_instance (tree, tree);
137
138 static tree start_class (enum tree_code, tree, tree, tree);
139 static tree continue_class (tree);
140 static void finish_class (tree);
141 static void start_method_def (tree);
142 #ifdef OBJCPLUS
143 static void objc_start_function (tree, tree, tree, tree);
144 #else
145 static void objc_start_function (tree, tree, tree, struct c_arg_info *);
146 #endif
147 static tree start_protocol (enum tree_code, tree, tree);
148 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
149 static tree objc_add_method (tree, tree, int);
150 static tree add_instance_variable (tree, int, tree);
151 static tree build_ivar_reference (tree);
152 static tree is_ivar (tree, tree);
153
154 static void build_objc_exception_stuff (void);
155 static void build_next_objc_exception_stuff (void);
156
157 /* We only need the following for ObjC; ObjC++ will use C++'s definition
158 of DERIVED_FROM_P. */
159 #ifndef OBJCPLUS
160 static bool objc_derived_from_p (tree, tree);
161 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
162 #endif
163 static void objc_xref_basetypes (tree, tree);
164
165 static void build_class_template (void);
166 static void build_selector_template (void);
167 static void build_category_template (void);
168 static void build_super_template (void);
169 static tree build_protocol_initializer (tree, tree, tree, tree, tree);
170 static tree get_class_ivars (tree, bool);
171 static tree generate_protocol_list (tree);
172 static void build_protocol_reference (tree);
173
174 #ifdef OBJCPLUS
175 static void objc_generate_cxx_cdtors (void);
176 #endif
177
178 static const char *synth_id_with_class_suffix (const char *, tree);
179
180 /* Hash tables to manage the global pool of method prototypes. */
181
182 hash *nst_method_hash_list = 0;
183 hash *cls_method_hash_list = 0;
184
185 static hash hash_lookup (hash *, tree);
186 static tree lookup_method (tree, tree);
187 static tree lookup_method_static (tree, tree, int);
188
189 enum string_section
190 {
191 class_names, /* class, category, protocol, module names */
192 meth_var_names, /* method and variable names */
193 meth_var_types /* method and variable type descriptors */
194 };
195
196 static tree add_objc_string (tree, enum string_section);
197 static void build_selector_table_decl (void);
198
199 /* Protocol additions. */
200
201 static tree lookup_protocol (tree);
202 static tree lookup_and_install_protocols (tree);
203
204 /* Type encoding. */
205
206 static void encode_type_qualifiers (tree);
207 static void encode_type (tree, int, int);
208 static void encode_field_decl (tree, int, int);
209
210 #ifdef OBJCPLUS
211 static void really_start_method (tree, tree);
212 #else
213 static void really_start_method (tree, struct c_arg_info *);
214 #endif
215 static int comp_proto_with_proto (tree, tree, int);
216 static tree objc_decay_parm_type (tree);
217 static void objc_push_parm (tree);
218 #ifdef OBJCPLUS
219 static tree objc_get_parm_info (int);
220 #else
221 static struct c_arg_info *objc_get_parm_info (int);
222 #endif
223
224 /* Utilities for debugging and error diagnostics. */
225
226 static char *gen_type_name (tree);
227 static char *gen_type_name_0 (tree);
228 static char *gen_method_decl (tree);
229 static char *gen_declaration (tree);
230
231 /* Everything else. */
232
233 static tree create_field_decl (tree, const char *);
234 static void add_class_reference (tree);
235 static void build_protocol_template (void);
236 static tree encode_method_prototype (tree);
237 static void generate_classref_translation_entry (tree);
238 static void handle_class_ref (tree);
239 static void generate_struct_by_value_array (void)
240 ATTRIBUTE_NORETURN;
241 static void mark_referenced_methods (void);
242 static void generate_objc_image_info (void);
243
244 /*** Private Interface (data) ***/
245
246 /* Reserved tag definitions. */
247
248 #define OBJECT_TYPEDEF_NAME "id"
249 #define CLASS_TYPEDEF_NAME "Class"
250
251 #define TAG_OBJECT "objc_object"
252 #define TAG_CLASS "objc_class"
253 #define TAG_SUPER "objc_super"
254 #define TAG_SELECTOR "objc_selector"
255
256 #define UTAG_CLASS "_objc_class"
257 #define UTAG_IVAR "_objc_ivar"
258 #define UTAG_IVAR_LIST "_objc_ivar_list"
259 #define UTAG_METHOD "_objc_method"
260 #define UTAG_METHOD_LIST "_objc_method_list"
261 #define UTAG_CATEGORY "_objc_category"
262 #define UTAG_MODULE "_objc_module"
263 #define UTAG_SYMTAB "_objc_symtab"
264 #define UTAG_SUPER "_objc_super"
265 #define UTAG_SELECTOR "_objc_selector"
266
267 #define UTAG_PROTOCOL "_objc_protocol"
268 #define UTAG_METHOD_PROTOTYPE "_objc_method_prototype"
269 #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
270
271 /* Note that the string object global name is only needed for the
272 NeXT runtime. */
273 #define STRING_OBJECT_GLOBAL_FORMAT "_%sClassReference"
274
275 #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
276
277 static const char *TAG_GETCLASS;
278 static const char *TAG_GETMETACLASS;
279 static const char *TAG_MSGSEND;
280 static const char *TAG_MSGSENDSUPER;
281 /* The NeXT Objective-C messenger may have two extra entry points, for use
282 when returning a structure. */
283 static const char *TAG_MSGSEND_STRET;
284 static const char *TAG_MSGSENDSUPER_STRET;
285 static const char *default_constant_string_class_name;
286
287 /* Runtime metadata flags. */
288 #define CLS_FACTORY 0x0001L
289 #define CLS_META 0x0002L
290 #define CLS_HAS_CXX_STRUCTORS 0x2000L
291
292 #define OBJC_MODIFIER_STATIC 0x00000001
293 #define OBJC_MODIFIER_FINAL 0x00000002
294 #define OBJC_MODIFIER_PUBLIC 0x00000004
295 #define OBJC_MODIFIER_PRIVATE 0x00000008
296 #define OBJC_MODIFIER_PROTECTED 0x00000010
297 #define OBJC_MODIFIER_NATIVE 0x00000020
298 #define OBJC_MODIFIER_SYNCHRONIZED 0x00000040
299 #define OBJC_MODIFIER_ABSTRACT 0x00000080
300 #define OBJC_MODIFIER_VOLATILE 0x00000100
301 #define OBJC_MODIFIER_TRANSIENT 0x00000200
302 #define OBJC_MODIFIER_NONE_SPECIFIED 0x80000000
303
304 /* NeXT-specific tags. */
305
306 #define TAG_MSGSEND_NONNIL "objc_msgSendNonNil"
307 #define TAG_MSGSEND_NONNIL_STRET "objc_msgSendNonNil_stret"
308 #define TAG_EXCEPTIONEXTRACT "objc_exception_extract"
309 #define TAG_EXCEPTIONTRYENTER "objc_exception_try_enter"
310 #define TAG_EXCEPTIONTRYEXIT "objc_exception_try_exit"
311 #define TAG_EXCEPTIONMATCH "objc_exception_match"
312 #define TAG_EXCEPTIONTHROW "objc_exception_throw"
313 #define TAG_SYNCENTER "objc_sync_enter"
314 #define TAG_SYNCEXIT "objc_sync_exit"
315 #define TAG_SETJMP "_setjmp"
316 #define UTAG_EXCDATA "_objc_exception_data"
317
318 #define TAG_ASSIGNIVAR "objc_assign_ivar"
319 #define TAG_ASSIGNGLOBAL "objc_assign_global"
320 #define TAG_ASSIGNSTRONGCAST "objc_assign_strongCast"
321
322 /* Branch entry points. All that matters here are the addresses;
323 functions with these names do not really exist in libobjc. */
324
325 #define TAG_MSGSEND_FAST "objc_msgSend_Fast"
326 #define TAG_ASSIGNIVAR_FAST "objc_assign_ivar_Fast"
327
328 #define TAG_CXX_CONSTRUCT ".cxx_construct"
329 #define TAG_CXX_DESTRUCT ".cxx_destruct"
330
331 /* GNU-specific tags. */
332
333 #define TAG_EXECCLASS "__objc_exec_class"
334 #define TAG_GNUINIT "__objc_gnu_init"
335
336 /* Flags for lookup_method_static(). */
337 #define OBJC_LOOKUP_CLASS 1 /* Look for class methods. */
338 #define OBJC_LOOKUP_NO_SUPER 2 /* Do not examine superclasses. */
339
340 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
341 tree objc_global_trees[OCTI_MAX];
342
343 static void handle_impent (struct imp_entry *);
344
345 struct imp_entry *imp_list = 0;
346 int imp_count = 0; /* `@implementation' */
347 int cat_count = 0; /* `@category' */
348
349 enum tree_code objc_inherit_code;
350 int objc_public_flag;
351
352 /* Use to generate method labels. */
353 static int method_slot = 0;
354
355 static int objc_collecting_ivars = 0;
356
357 #define BUFSIZE 1024
358
359 static char *errbuf; /* Buffer for error diagnostics */
360
361 /* Data imported from tree.c. */
362
363 extern enum debug_info_type write_symbols;
364
365 /* Data imported from toplev.c. */
366
367 extern const char *dump_base_name;
368 \f
369 static int flag_typed_selectors;
370
371 /* Store all constructed constant strings in a hash table so that
372 they get uniqued properly. */
373
374 struct GTY(()) string_descriptor {
375 /* The literal argument . */
376 tree literal;
377
378 /* The resulting constant string. */
379 tree constructor;
380 };
381
382 static GTY((param_is (struct string_descriptor))) htab_t string_htab;
383
384 /* Store the EH-volatilized types in a hash table, for easy retrieval. */
385 struct GTY(()) volatilized_type {
386 tree type;
387 };
388
389 static GTY((param_is (struct volatilized_type))) htab_t volatilized_htab;
390
391 FILE *gen_declaration_file;
392
393 /* Tells "encode_pointer/encode_aggregate" whether we are generating
394 type descriptors for instance variables (as opposed to methods).
395 Type descriptors for instance variables contain more information
396 than methods (for static typing and embedded structures). */
397
398 static int generating_instance_variables = 0;
399
400 /* For building an objc struct. These may not be used when this file
401 is compiled as part of obj-c++. */
402
403 static bool objc_building_struct;
404 static struct c_struct_parse_info *objc_struct_info ATTRIBUTE_UNUSED;
405
406 /* Start building a struct for objc. */
407
408 static tree
409 objc_start_struct (tree name)
410 {
411 gcc_assert (!objc_building_struct);
412 objc_building_struct = true;
413 return start_struct (input_location, RECORD_TYPE, name, &objc_struct_info);
414 }
415
416 /* Finish building a struct for objc. */
417
418 static tree
419 objc_finish_struct (tree type, tree fieldlist)
420 {
421 gcc_assert (objc_building_struct);
422 objc_building_struct = false;
423 return finish_struct (input_location, type, fieldlist, NULL_TREE,
424 objc_struct_info);
425 }
426
427 static tree
428 build_sized_array_type (tree base_type, int size)
429 {
430 tree index_type = build_index_type (build_int_cst (NULL_TREE, size - 1));
431 return build_array_type (base_type, index_type);
432 }
433
434 static tree
435 add_field_decl (tree type, const char *name, tree **chain)
436 {
437 tree field = create_field_decl (type, name);
438
439 if (*chain != NULL)
440 **chain = field;
441 *chain = &DECL_CHAIN (field);
442
443 return field;
444 }
445
446 /* Some platforms pass small structures through registers versus
447 through an invisible pointer. Determine at what size structure is
448 the transition point between the two possibilities. */
449
450 static void
451 generate_struct_by_value_array (void)
452 {
453 tree type;
454 tree decls;
455 int i, j;
456 int aggregate_in_mem[32];
457 int found = 0;
458
459 /* Presumably no platform passes 32 byte structures in a register. */
460 for (i = 1; i < 32; i++)
461 {
462 char buffer[5];
463 tree *chain = NULL;
464
465 /* Create an unnamed struct that has `i' character components */
466 type = objc_start_struct (NULL_TREE);
467
468 strcpy (buffer, "c1");
469 decls = add_field_decl (char_type_node, buffer, &chain);
470
471 for (j = 1; j < i; j++)
472 {
473 sprintf (buffer, "c%d", j + 1);
474 add_field_decl (char_type_node, buffer, &chain);
475 }
476 objc_finish_struct (type, decls);
477
478 aggregate_in_mem[i] = aggregate_value_p (type, 0);
479 if (!aggregate_in_mem[i])
480 found = 1;
481 }
482
483 /* We found some structures that are returned in registers instead of memory
484 so output the necessary data. */
485 if (found)
486 {
487 for (i = 31; i >= 0; i--)
488 if (!aggregate_in_mem[i])
489 break;
490 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n\n", i);
491
492 /* The first member of the structure is always 0 because we don't handle
493 structures with 0 members */
494 printf ("static int struct_forward_array[] = {\n 0");
495
496 for (j = 1; j <= i; j++)
497 printf (", %d", aggregate_in_mem[j]);
498 printf ("\n};\n");
499 }
500
501 exit (0);
502 }
503
504 bool
505 objc_init (void)
506 {
507 #ifdef OBJCPLUS
508 if (cxx_init () == false)
509 #else
510 if (c_objc_common_init () == false)
511 #endif
512 return false;
513
514 /* If gen_declaration desired, open the output file. */
515 if (flag_gen_declaration)
516 {
517 register char * const dumpname = concat (dump_base_name, ".decl", NULL);
518 gen_declaration_file = fopen (dumpname, "w");
519 if (gen_declaration_file == 0)
520 fatal_error ("can't open %s: %m", dumpname);
521 free (dumpname);
522 }
523
524 if (flag_next_runtime)
525 {
526 TAG_GETCLASS = "objc_getClass";
527 TAG_GETMETACLASS = "objc_getMetaClass";
528 TAG_MSGSEND = "objc_msgSend";
529 TAG_MSGSENDSUPER = "objc_msgSendSuper";
530 TAG_MSGSEND_STRET = "objc_msgSend_stret";
531 TAG_MSGSENDSUPER_STRET = "objc_msgSendSuper_stret";
532 default_constant_string_class_name = "NSConstantString";
533 }
534 else
535 {
536 TAG_GETCLASS = "objc_get_class";
537 TAG_GETMETACLASS = "objc_get_meta_class";
538 TAG_MSGSEND = "objc_msg_lookup";
539 TAG_MSGSENDSUPER = "objc_msg_lookup_super";
540 /* GNU runtime does not provide special functions to support
541 structure-returning methods. */
542 default_constant_string_class_name = "NXConstantString";
543 flag_typed_selectors = 1;
544 /* GNU runtime does not need the compiler to change code
545 in order to do GC. */
546 if (flag_objc_gc)
547 {
548 warning_at (0, 0, "%<-fobjc-gc%> is ignored for %<-fgnu-runtime%>");
549 flag_objc_gc=0;
550 }
551 }
552
553 init_objc ();
554
555 if (print_struct_values && !flag_compare_debug)
556 generate_struct_by_value_array ();
557
558 return true;
559 }
560
561 void
562 objc_finish_file (void)
563 {
564 mark_referenced_methods ();
565
566 #ifdef OBJCPLUS
567 /* We need to instantiate templates _before_ we emit ObjC metadata;
568 if we do not, some metadata (such as selectors) may go missing. */
569 at_eof = 1;
570 instantiate_pending_templates (0);
571 #endif
572
573 /* Finalize Objective-C runtime data. No need to generate tables
574 and code if only checking syntax, or if generating a PCH file. */
575 if (!flag_syntax_only && !pch_file)
576 finish_objc ();
577
578 if (gen_declaration_file)
579 fclose (gen_declaration_file);
580 }
581 \f
582 /* Return the first occurrence of a method declaration corresponding
583 to sel_name in rproto_list. Search rproto_list recursively.
584 If is_class is 0, search for instance methods, otherwise for class
585 methods. */
586 static tree
587 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
588 int is_class)
589 {
590 tree rproto, p;
591 tree fnd = 0;
592
593 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
594 {
595 p = TREE_VALUE (rproto);
596
597 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
598 {
599 if ((fnd = lookup_method (is_class
600 ? PROTOCOL_CLS_METHODS (p)
601 : PROTOCOL_NST_METHODS (p), sel_name)))
602 ;
603 else if (PROTOCOL_LIST (p))
604 fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
605 sel_name, is_class);
606 }
607 else
608 {
609 ; /* An identifier...if we could not find a protocol. */
610 }
611
612 if (fnd)
613 return fnd;
614 }
615
616 return 0;
617 }
618
619 static tree
620 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
621 {
622 tree rproto, p;
623
624 /* Make sure the protocol is supported by the object on the rhs. */
625 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
626 {
627 tree fnd = 0;
628 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
629 {
630 p = TREE_VALUE (rproto);
631
632 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
633 {
634 if (lproto == p)
635 fnd = lproto;
636
637 else if (PROTOCOL_LIST (p))
638 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
639 }
640
641 if (fnd)
642 return fnd;
643 }
644 }
645 else
646 {
647 ; /* An identifier...if we could not find a protocol. */
648 }
649
650 return 0;
651 }
652
653 void
654 objc_start_class_interface (tree klass, tree super_class, tree protos)
655 {
656 objc_interface_context
657 = objc_ivar_context
658 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos);
659 objc_public_flag = 0;
660 }
661
662 void
663 objc_start_category_interface (tree klass, tree categ, tree protos)
664 {
665 objc_interface_context
666 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos);
667 objc_ivar_chain
668 = continue_class (objc_interface_context);
669 }
670
671 void
672 objc_start_protocol (tree name, tree protos)
673 {
674 objc_interface_context
675 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos);
676 }
677
678 void
679 objc_continue_interface (void)
680 {
681 objc_ivar_chain
682 = continue_class (objc_interface_context);
683 }
684
685 void
686 objc_finish_interface (void)
687 {
688 finish_class (objc_interface_context);
689 objc_interface_context = NULL_TREE;
690 }
691
692 void
693 objc_start_class_implementation (tree klass, tree super_class)
694 {
695 objc_implementation_context
696 = objc_ivar_context
697 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE);
698 objc_public_flag = 0;
699 }
700
701 void
702 objc_start_category_implementation (tree klass, tree categ)
703 {
704 objc_implementation_context
705 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE);
706 objc_ivar_chain
707 = continue_class (objc_implementation_context);
708 }
709
710 void
711 objc_continue_implementation (void)
712 {
713 objc_ivar_chain
714 = continue_class (objc_implementation_context);
715 }
716
717 void
718 objc_finish_implementation (void)
719 {
720 #ifdef OBJCPLUS
721 if (flag_objc_call_cxx_cdtors)
722 objc_generate_cxx_cdtors ();
723 #endif
724
725 if (objc_implementation_context)
726 {
727 finish_class (objc_implementation_context);
728 objc_ivar_chain = NULL_TREE;
729 objc_implementation_context = NULL_TREE;
730 }
731 else
732 warning (0, "%<@end%> must appear in an @implementation context");
733 }
734
735 void
736 objc_set_visibility (int visibility)
737 {
738 objc_public_flag = visibility;
739 }
740
741 void
742 objc_set_method_type (enum tree_code type)
743 {
744 objc_inherit_code = (type == PLUS_EXPR
745 ? CLASS_METHOD_DECL
746 : INSTANCE_METHOD_DECL);
747 }
748
749 tree
750 objc_build_method_signature (tree rettype, tree selector,
751 tree optparms, bool ellipsis)
752 {
753 return build_method_decl (objc_inherit_code, rettype, selector,
754 optparms, ellipsis);
755 }
756
757 void
758 objc_add_method_declaration (tree decl)
759 {
760 if (!objc_interface_context)
761 {
762 /* PS: At the moment, due to how the parser works, it should be
763 impossible to get here. But it's good to have the check in
764 case the parser changes.
765 */
766 fatal_error ("method declaration not in @interface context");
767 }
768
769 objc_add_method (objc_interface_context,
770 decl,
771 objc_inherit_code == CLASS_METHOD_DECL);
772 }
773
774 /* Return 'true' if the method definition could be started, and
775 'false' if not (because we are outside an @implementation context).
776 */
777 bool
778 objc_start_method_definition (tree decl)
779 {
780 if (!objc_implementation_context)
781 {
782 error ("method definition not in @implementation context");
783 return false;
784 }
785
786 #ifndef OBJCPLUS
787 /* Indicate no valid break/continue context by setting these variables
788 to some non-null, non-label value. We'll notice and emit the proper
789 error message in c_finish_bc_stmt. */
790 c_break_label = c_cont_label = size_zero_node;
791 #endif
792
793 objc_add_method (objc_implementation_context,
794 decl,
795 objc_inherit_code == CLASS_METHOD_DECL);
796 start_method_def (decl);
797 return true;
798 }
799
800 void
801 objc_add_instance_variable (tree decl)
802 {
803 (void) add_instance_variable (objc_ivar_context,
804 objc_public_flag,
805 decl);
806 }
807
808 /* Return 1 if IDENT is an ObjC/ObjC++ reserved keyword in the context of
809 an '@'. */
810
811 int
812 objc_is_reserved_word (tree ident)
813 {
814 unsigned char code = C_RID_CODE (ident);
815
816 return (OBJC_IS_AT_KEYWORD (code)
817 || code == RID_CLASS || code == RID_PUBLIC
818 || code == RID_PROTECTED || code == RID_PRIVATE
819 || code == RID_TRY || code == RID_THROW || code == RID_CATCH);
820 }
821
822 /* Return true if TYPE is 'id'. */
823
824 static bool
825 objc_is_object_id (tree type)
826 {
827 return OBJC_TYPE_NAME (type) == objc_object_id;
828 }
829
830 static bool
831 objc_is_class_id (tree type)
832 {
833 return OBJC_TYPE_NAME (type) == objc_class_id;
834 }
835
836 /* Construct a C struct with same name as KLASS, a base struct with tag
837 SUPER_NAME (if any), and FIELDS indicated. */
838
839 static tree
840 objc_build_struct (tree klass, tree fields, tree super_name)
841 {
842 tree name = CLASS_NAME (klass);
843 tree s = objc_start_struct (name);
844 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
845 tree t;
846 VEC(tree,heap) *objc_info = NULL;
847 int i;
848
849 if (super)
850 {
851 /* Prepend a packed variant of the base class into the layout. This
852 is necessary to preserve ObjC ABI compatibility. */
853 tree base = build_decl (input_location,
854 FIELD_DECL, NULL_TREE, super);
855 tree field = TYPE_FIELDS (super);
856
857 while (field && DECL_CHAIN (field)
858 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
859 field = DECL_CHAIN (field);
860
861 /* For ObjC ABI purposes, the "packed" size of a base class is
862 the sum of the offset and the size (in bits) of the last field
863 in the class. */
864 DECL_SIZE (base)
865 = (field && TREE_CODE (field) == FIELD_DECL
866 ? size_binop (PLUS_EXPR,
867 size_binop (PLUS_EXPR,
868 size_binop
869 (MULT_EXPR,
870 convert (bitsizetype,
871 DECL_FIELD_OFFSET (field)),
872 bitsize_int (BITS_PER_UNIT)),
873 DECL_FIELD_BIT_OFFSET (field)),
874 DECL_SIZE (field))
875 : bitsize_zero_node);
876 DECL_SIZE_UNIT (base)
877 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
878 size_int (BITS_PER_UNIT));
879 DECL_ARTIFICIAL (base) = 1;
880 DECL_ALIGN (base) = 1;
881 DECL_FIELD_CONTEXT (base) = s;
882 #ifdef OBJCPLUS
883 DECL_FIELD_IS_BASE (base) = 1;
884
885 if (fields)
886 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
887 #endif /* are following the ObjC ABI here. */
888 DECL_CHAIN (base) = fields;
889 fields = base;
890 }
891
892 /* NB: Calling finish_struct() may cause type TYPE_LANG_SPECIFIC fields
893 in all variants of this RECORD_TYPE to be clobbered, but it is therein
894 that we store protocol conformance info (e.g., 'NSObject <MyProtocol>').
895 Hence, we must squirrel away the ObjC-specific information before calling
896 finish_struct(), and then reinstate it afterwards. */
897
898 for (t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
899 {
900 if (!TYPE_HAS_OBJC_INFO (t))
901 {
902 INIT_TYPE_OBJC_INFO (t);
903 TYPE_OBJC_INTERFACE (t) = klass;
904 }
905 VEC_safe_push (tree, heap, objc_info, TYPE_OBJC_INFO (t));
906 }
907
908 /* Point the struct at its related Objective-C class. */
909 INIT_TYPE_OBJC_INFO (s);
910 TYPE_OBJC_INTERFACE (s) = klass;
911
912 s = objc_finish_struct (s, fields);
913
914 for (i = 0, t = TYPE_NEXT_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
915 {
916 TYPE_OBJC_INFO (t) = VEC_index (tree, objc_info, i);
917 /* Replace the IDENTIFIER_NODE with an actual @interface. */
918 TYPE_OBJC_INTERFACE (t) = klass;
919 }
920 VEC_free (tree, heap, objc_info);
921
922 /* Use TYPE_BINFO structures to point at the super class, if any. */
923 objc_xref_basetypes (s, super);
924
925 /* Mark this struct as a class template. */
926 CLASS_STATIC_TEMPLATE (klass) = s;
927
928 return s;
929 }
930
931 /* Build a type differing from TYPE only in that TYPE_VOLATILE is set.
932 Unlike tree.c:build_qualified_type(), preserve TYPE_LANG_SPECIFIC in the
933 process. */
934 static tree
935 objc_build_volatilized_type (tree type)
936 {
937 tree t;
938
939 /* Check if we have not constructed the desired variant already. */
940 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
941 {
942 /* The type qualifiers must (obviously) match up. */
943 if (!TYPE_VOLATILE (t)
944 || (TYPE_READONLY (t) != TYPE_READONLY (type))
945 || (TYPE_RESTRICT (t) != TYPE_RESTRICT (type)))
946 continue;
947
948 /* For pointer types, the pointees (and hence their TYPE_LANG_SPECIFIC
949 info, if any) must match up. */
950 if (POINTER_TYPE_P (t)
951 && (TREE_TYPE (t) != TREE_TYPE (type)))
952 continue;
953
954 /* Everything matches up! */
955 return t;
956 }
957
958 /* Ok, we could not re-use any of the pre-existing variants. Create
959 a new one. */
960 t = build_variant_type_copy (type);
961 TYPE_VOLATILE (t) = 1;
962
963 /* Set up the canonical type information. */
964 if (TYPE_STRUCTURAL_EQUALITY_P (type))
965 SET_TYPE_STRUCTURAL_EQUALITY (t);
966 else if (TYPE_CANONICAL (type) != type)
967 TYPE_CANONICAL (t) = objc_build_volatilized_type (TYPE_CANONICAL (type));
968 else
969 TYPE_CANONICAL (t) = t;
970
971 return t;
972 }
973
974 /* Mark DECL as being 'volatile' for purposes of Darwin
975 _setjmp()/_longjmp() exception handling. Called from
976 objc_mark_locals_volatile(). */
977 void
978 objc_volatilize_decl (tree decl)
979 {
980 /* Do not mess with variables that are 'static' or (already)
981 'volatile'. */
982 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
983 && (TREE_CODE (decl) == VAR_DECL
984 || TREE_CODE (decl) == PARM_DECL))
985 {
986 tree t = TREE_TYPE (decl);
987 struct volatilized_type key;
988 void **loc;
989
990 t = objc_build_volatilized_type (t);
991 key.type = t;
992 loc = htab_find_slot (volatilized_htab, &key, INSERT);
993
994 if (!*loc)
995 {
996 *loc = ggc_alloc_volatilized_type ();
997 ((struct volatilized_type *) *loc)->type = t;
998 }
999
1000 TREE_TYPE (decl) = t;
1001 TREE_THIS_VOLATILE (decl) = 1;
1002 TREE_SIDE_EFFECTS (decl) = 1;
1003 DECL_REGISTER (decl) = 0;
1004 #ifndef OBJCPLUS
1005 C_DECL_REGISTER (decl) = 0;
1006 #endif
1007 }
1008 }
1009
1010 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
1011 (including its categories and superclasses) or by object type TYP.
1012 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
1013
1014 static bool
1015 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
1016 {
1017 bool class_type = (cls != NULL_TREE);
1018
1019 while (cls)
1020 {
1021 tree c;
1022
1023 /* Check protocols adopted by the class and its categories. */
1024 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
1025 {
1026 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
1027 return true;
1028 }
1029
1030 /* Repeat for superclasses. */
1031 cls = lookup_interface (CLASS_SUPER_NAME (cls));
1032 }
1033
1034 /* Check for any protocols attached directly to the object type. */
1035 if (TYPE_HAS_OBJC_INFO (typ))
1036 {
1037 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
1038 return true;
1039 }
1040
1041 if (warn)
1042 {
1043 *errbuf = 0;
1044 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
1045 /* NB: Types 'id' and 'Class' cannot reasonably be described as
1046 "implementing" a given protocol, since they do not have an
1047 implementation. */
1048 if (class_type)
1049 warning (0, "class %qs does not implement the %qE protocol",
1050 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1051 else
1052 warning (0, "type %qs does not conform to the %qE protocol",
1053 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
1054 }
1055
1056 return false;
1057 }
1058
1059 /* Check if class RCLS and instance struct type RTYP conform to at least the
1060 same protocols that LCLS and LTYP conform to. */
1061
1062 static bool
1063 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
1064 {
1065 tree p;
1066 bool have_lproto = false;
1067
1068 while (lcls)
1069 {
1070 /* NB: We do _not_ look at categories defined for LCLS; these may or
1071 may not get loaded in, and therefore it is unreasonable to require
1072 that RCLS/RTYP must implement any of their protocols. */
1073 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
1074 {
1075 have_lproto = true;
1076
1077 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1078 return warn;
1079 }
1080
1081 /* Repeat for superclasses. */
1082 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
1083 }
1084
1085 /* Check for any protocols attached directly to the object type. */
1086 if (TYPE_HAS_OBJC_INFO (ltyp))
1087 {
1088 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
1089 {
1090 have_lproto = true;
1091
1092 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
1093 return warn;
1094 }
1095 }
1096
1097 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
1098 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
1099 away with simply checking for 'id' or 'Class' (!RCLS), since this
1100 routine will not get called in other cases. */
1101 return have_lproto || (rcls != NULL_TREE);
1102 }
1103
1104 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
1105 Both TYPE1 and TYPE2 must be pointers, and already determined to be
1106 compatible by objc_compare_types() below. */
1107
1108 tree
1109 objc_common_type (tree type1, tree type2)
1110 {
1111 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
1112
1113 while (POINTER_TYPE_P (inner1))
1114 {
1115 inner1 = TREE_TYPE (inner1);
1116 inner2 = TREE_TYPE (inner2);
1117 }
1118
1119 /* If one type is derived from another, return the base type. */
1120 if (DERIVED_FROM_P (inner1, inner2))
1121 return type1;
1122 else if (DERIVED_FROM_P (inner2, inner1))
1123 return type2;
1124
1125 /* If both types are 'Class', return 'Class'. */
1126 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
1127 return objc_class_type;
1128
1129 /* Otherwise, return 'id'. */
1130 return objc_object_type;
1131 }
1132
1133 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
1134 an instance of RTYP to an instance of LTYP or to compare the two
1135 (if ARGNO is equal to -3), per ObjC type system rules. Before
1136 returning 'true', this routine may issue warnings related to, e.g.,
1137 protocol conformance. When returning 'false', the routine must
1138 produce absolutely no warnings; the C or C++ front-end will do so
1139 instead, if needed. If either LTYP or RTYP is not an Objective-C type,
1140 the routine must return 'false'.
1141
1142 The ARGNO parameter is encoded as follows:
1143 >= 1 Parameter number (CALLEE contains function being called);
1144 0 Return value;
1145 -1 Assignment;
1146 -2 Initialization;
1147 -3 Comparison (LTYP and RTYP may match in either direction);
1148 -4 Silent comparison (for C++ overload resolution).
1149 */
1150
1151 bool
1152 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
1153 {
1154 tree lcls, rcls, lproto, rproto;
1155 bool pointers_compatible;
1156
1157 /* We must be dealing with pointer types */
1158 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
1159 return false;
1160
1161 do
1162 {
1163 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1164 rtyp = TREE_TYPE (rtyp);
1165 }
1166 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1167
1168 /* We must also handle function pointers, since ObjC is a bit more
1169 lenient than C or C++ on this. */
1170 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
1171 {
1172 /* Return types must be covariant. */
1173 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
1174 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
1175 argno, callee))
1176 return false;
1177
1178 /* Argument types must be contravariant. */
1179 for (ltyp = TYPE_ARG_TYPES (ltyp), rtyp = TYPE_ARG_TYPES (rtyp);
1180 ltyp && rtyp; ltyp = TREE_CHAIN (ltyp), rtyp = TREE_CHAIN (rtyp))
1181 {
1182 if (!comptypes (TREE_VALUE (rtyp), TREE_VALUE (ltyp))
1183 && !objc_compare_types (TREE_VALUE (rtyp), TREE_VALUE (ltyp),
1184 argno, callee))
1185 return false;
1186 }
1187
1188 return (ltyp == rtyp);
1189 }
1190
1191 /* Past this point, we are only interested in ObjC class instances,
1192 or 'id' or 'Class'. */
1193 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
1194 return false;
1195
1196 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
1197 && !TYPE_HAS_OBJC_INFO (ltyp))
1198 return false;
1199
1200 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
1201 && !TYPE_HAS_OBJC_INFO (rtyp))
1202 return false;
1203
1204 /* Past this point, we are committed to returning 'true' to the caller
1205 (unless performing a silent comparison; see below). However, we can
1206 still warn about type and/or protocol mismatches. */
1207
1208 if (TYPE_HAS_OBJC_INFO (ltyp))
1209 {
1210 lcls = TYPE_OBJC_INTERFACE (ltyp);
1211 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
1212 }
1213 else
1214 lcls = lproto = NULL_TREE;
1215
1216 if (TYPE_HAS_OBJC_INFO (rtyp))
1217 {
1218 rcls = TYPE_OBJC_INTERFACE (rtyp);
1219 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
1220 }
1221 else
1222 rcls = rproto = NULL_TREE;
1223
1224 /* If we could not find an @interface declaration, we must have
1225 only seen a @class declaration; for purposes of type comparison,
1226 treat it as a stand-alone (root) class. */
1227
1228 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
1229 lcls = NULL_TREE;
1230
1231 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
1232 rcls = NULL_TREE;
1233
1234 /* If either type is an unqualified 'id', we're done. */
1235 if ((!lproto && objc_is_object_id (ltyp))
1236 || (!rproto && objc_is_object_id (rtyp)))
1237 return true;
1238
1239 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
1240
1241 /* If the underlying types are the same, and at most one of them has
1242 a protocol list, we do not need to issue any diagnostics. */
1243 if (pointers_compatible && (!lproto || !rproto))
1244 return true;
1245
1246 /* If exactly one of the types is 'Class', issue a diagnostic; any
1247 exceptions of this rule have already been handled. */
1248 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
1249 pointers_compatible = false;
1250 /* Otherwise, check for inheritance relations. */
1251 else
1252 {
1253 if (!pointers_compatible)
1254 pointers_compatible
1255 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
1256
1257 if (!pointers_compatible)
1258 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
1259
1260 if (!pointers_compatible && argno <= -3)
1261 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
1262 }
1263
1264 /* If the pointers match modulo protocols, check for protocol conformance
1265 mismatches. */
1266 if (pointers_compatible)
1267 {
1268 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
1269 argno != -3);
1270
1271 if (!pointers_compatible && argno == -3)
1272 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
1273 argno != -3);
1274 }
1275
1276 if (!pointers_compatible)
1277 {
1278 /* The two pointers are not exactly compatible. Issue a warning, unless
1279 we are performing a silent comparison, in which case return 'false'
1280 instead. */
1281 /* NB: For the time being, we shall make our warnings look like their
1282 C counterparts. In the future, we may wish to make them more
1283 ObjC-specific. */
1284 switch (argno)
1285 {
1286 case -4:
1287 return false;
1288
1289 case -3:
1290 warning (0, "comparison of distinct Objective-C types lacks a cast");
1291 break;
1292
1293 case -2:
1294 warning (0, "initialization from distinct Objective-C type");
1295 break;
1296
1297 case -1:
1298 warning (0, "assignment from distinct Objective-C type");
1299 break;
1300
1301 case 0:
1302 warning (0, "distinct Objective-C type in return");
1303 break;
1304
1305 default:
1306 warning (0, "passing argument %d of %qE from distinct "
1307 "Objective-C type", argno, callee);
1308 break;
1309 }
1310 }
1311
1312 return true;
1313 }
1314
1315 /* This routine is similar to objc_compare_types except that function-pointers are
1316 excluded. This is because, caller assumes that common types are of (id, Object*)
1317 variety and calls objc_common_type to obtain a common type. There is no commonolty
1318 between two function-pointers in this regard. */
1319
1320 bool
1321 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
1322 {
1323 if (objc_compare_types (ltyp, rtyp, argno, callee))
1324 {
1325 /* exclude function-pointer types. */
1326 do
1327 {
1328 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
1329 rtyp = TREE_TYPE (rtyp);
1330 }
1331 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
1332 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
1333 }
1334 return false;
1335 }
1336
1337 /* Check if LTYP and RTYP have the same type qualifiers. If either type
1338 lives in the volatilized hash table, ignore the 'volatile' bit when
1339 making the comparison. */
1340
1341 bool
1342 objc_type_quals_match (tree ltyp, tree rtyp)
1343 {
1344 int lquals = TYPE_QUALS (ltyp), rquals = TYPE_QUALS (rtyp);
1345 struct volatilized_type key;
1346
1347 key.type = ltyp;
1348
1349 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1350 lquals &= ~TYPE_QUAL_VOLATILE;
1351
1352 key.type = rtyp;
1353
1354 if (htab_find_slot (volatilized_htab, &key, NO_INSERT))
1355 rquals &= ~TYPE_QUAL_VOLATILE;
1356
1357 return (lquals == rquals);
1358 }
1359
1360 #ifndef OBJCPLUS
1361 /* Determine if CHILD is derived from PARENT. The routine assumes that
1362 both parameters are RECORD_TYPEs, and is non-reflexive. */
1363
1364 static bool
1365 objc_derived_from_p (tree parent, tree child)
1366 {
1367 parent = TYPE_MAIN_VARIANT (parent);
1368
1369 for (child = TYPE_MAIN_VARIANT (child);
1370 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
1371 {
1372 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
1373 (TYPE_BINFO (child),
1374 0)));
1375
1376 if (child == parent)
1377 return true;
1378 }
1379
1380 return false;
1381 }
1382 #endif
1383
1384 static tree
1385 objc_build_component_ref (tree datum, tree component)
1386 {
1387 /* If COMPONENT is NULL, the caller is referring to the anonymous
1388 base class field. */
1389 if (!component)
1390 {
1391 tree base = TYPE_FIELDS (TREE_TYPE (datum));
1392
1393 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
1394 }
1395
1396 /* The 'build_component_ref' routine has been removed from the C++
1397 front-end, but 'finish_class_member_access_expr' seems to be
1398 a worthy substitute. */
1399 #ifdef OBJCPLUS
1400 return finish_class_member_access_expr (datum, component, false,
1401 tf_warning_or_error);
1402 #else
1403 return build_component_ref (input_location, datum, component);
1404 #endif
1405 }
1406
1407 /* Recursively copy inheritance information rooted at BINFO. To do this,
1408 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
1409
1410 static tree
1411 objc_copy_binfo (tree binfo)
1412 {
1413 tree btype = BINFO_TYPE (binfo);
1414 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
1415 tree base_binfo;
1416 int ix;
1417
1418 BINFO_TYPE (binfo2) = btype;
1419 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
1420 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
1421
1422 /* Recursively copy base binfos of BINFO. */
1423 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1424 {
1425 tree base_binfo2 = objc_copy_binfo (base_binfo);
1426
1427 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
1428 BINFO_BASE_APPEND (binfo2, base_binfo2);
1429 }
1430
1431 return binfo2;
1432 }
1433
1434 /* Record superclass information provided in BASETYPE for ObjC class REF.
1435 This is loosely based on cp/decl.c:xref_basetypes(). */
1436
1437 static void
1438 objc_xref_basetypes (tree ref, tree basetype)
1439 {
1440 tree binfo = make_tree_binfo (basetype ? 1 : 0);
1441
1442 TYPE_BINFO (ref) = binfo;
1443 BINFO_OFFSET (binfo) = size_zero_node;
1444 BINFO_TYPE (binfo) = ref;
1445
1446 if (basetype)
1447 {
1448 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
1449
1450 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
1451 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, 1);
1452 BINFO_BASE_APPEND (binfo, base_binfo);
1453 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
1454 }
1455 }
1456
1457 static hashval_t
1458 volatilized_hash (const void *ptr)
1459 {
1460 const_tree const typ = ((const struct volatilized_type *)ptr)->type;
1461
1462 return htab_hash_pointer(typ);
1463 }
1464
1465 static int
1466 volatilized_eq (const void *ptr1, const void *ptr2)
1467 {
1468 const_tree const typ1 = ((const struct volatilized_type *)ptr1)->type;
1469 const_tree const typ2 = ((const struct volatilized_type *)ptr2)->type;
1470
1471 return typ1 == typ2;
1472 }
1473
1474 /* Called from finish_decl. */
1475
1476 void
1477 objc_check_decl (tree decl)
1478 {
1479 tree type = TREE_TYPE (decl);
1480
1481 if (TREE_CODE (type) != RECORD_TYPE)
1482 return;
1483 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
1484 error ("statically allocated instance of Objective-C class %qE",
1485 type);
1486 }
1487
1488 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where INTERFACE may
1489 either name an Objective-C class, or refer to the special 'id' or 'Class'
1490 types. If INTERFACE is not a valid ObjC type, just return it unchanged. */
1491
1492 tree
1493 objc_get_protocol_qualified_type (tree interface, tree protocols)
1494 {
1495 /* If INTERFACE is not provided, default to 'id'. */
1496 tree type = (interface ? objc_is_id (interface) : objc_object_type);
1497 bool is_ptr = (type != NULL_TREE);
1498
1499 if (!is_ptr)
1500 {
1501 type = objc_is_class_name (interface);
1502
1503 if (type)
1504 {
1505 /* If looking at a typedef, retrieve the precise type it
1506 describes. */
1507 if (TREE_CODE (interface) == IDENTIFIER_NODE)
1508 interface = identifier_global_value (interface);
1509
1510 type = ((interface && TREE_CODE (interface) == TYPE_DECL
1511 && DECL_ORIGINAL_TYPE (interface))
1512 ? DECL_ORIGINAL_TYPE (interface)
1513 : xref_tag (RECORD_TYPE, type));
1514 }
1515 else
1516 return interface;
1517 }
1518
1519 if (protocols)
1520 {
1521 type = build_variant_type_copy (type);
1522
1523 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
1524 to the pointee. */
1525 if (is_ptr)
1526 {
1527 tree orig_pointee_type = TREE_TYPE (type);
1528 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
1529
1530 /* Set up the canonical type information. */
1531 TYPE_CANONICAL (type)
1532 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
1533
1534 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
1535 type = TREE_TYPE (type);
1536 }
1537
1538 /* Look up protocols and install in lang specific list. */
1539 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
1540 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols (protocols);
1541
1542 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
1543 return the pointer to the new pointee variant. */
1544 if (is_ptr)
1545 type = TYPE_POINTER_TO (type);
1546 else
1547 TYPE_OBJC_INTERFACE (type)
1548 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
1549 }
1550
1551 return type;
1552 }
1553
1554 /* Check for circular dependencies in protocols. The arguments are
1555 PROTO, the protocol to check, and LIST, a list of protocol it
1556 conforms to. */
1557
1558 static void
1559 check_protocol_recursively (tree proto, tree list)
1560 {
1561 tree p;
1562
1563 for (p = list; p; p = TREE_CHAIN (p))
1564 {
1565 tree pp = TREE_VALUE (p);
1566
1567 if (TREE_CODE (pp) == IDENTIFIER_NODE)
1568 pp = lookup_protocol (pp);
1569
1570 if (pp == proto)
1571 fatal_error ("protocol %qE has circular dependency",
1572 PROTOCOL_NAME (pp));
1573 if (pp)
1574 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
1575 }
1576 }
1577
1578 /* Look up PROTOCOLS, and return a list of those that are found.
1579 If none are found, return NULL. */
1580
1581 static tree
1582 lookup_and_install_protocols (tree protocols)
1583 {
1584 tree proto;
1585 tree return_value = NULL_TREE;
1586
1587 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
1588 {
1589 tree ident = TREE_VALUE (proto);
1590 tree p = lookup_protocol (ident);
1591
1592 if (p)
1593 return_value = chainon (return_value,
1594 build_tree_list (NULL_TREE, p));
1595 else if (ident != error_mark_node)
1596 error ("cannot find protocol declaration for %qE",
1597 ident);
1598 }
1599
1600 return return_value;
1601 }
1602
1603 /* Create a declaration for field NAME of a given TYPE. */
1604
1605 static tree
1606 create_field_decl (tree type, const char *name)
1607 {
1608 return build_decl (input_location,
1609 FIELD_DECL, get_identifier (name), type);
1610 }
1611
1612 /* Create a global, static declaration for variable NAME of a given TYPE. The
1613 finish_var_decl() routine will need to be called on it afterwards. */
1614
1615 static tree
1616 start_var_decl (tree type, const char *name)
1617 {
1618 tree var = build_decl (input_location,
1619 VAR_DECL, get_identifier (name), type);
1620
1621 TREE_STATIC (var) = 1;
1622 DECL_INITIAL (var) = error_mark_node; /* A real initializer is coming... */
1623 DECL_IGNORED_P (var) = 1;
1624 DECL_ARTIFICIAL (var) = 1;
1625 DECL_CONTEXT (var) = NULL_TREE;
1626 #ifdef OBJCPLUS
1627 DECL_THIS_STATIC (var) = 1; /* squash redeclaration errors */
1628 #endif
1629
1630 return var;
1631 }
1632
1633 /* Finish off the variable declaration created by start_var_decl(). */
1634
1635 static void
1636 finish_var_decl (tree var, tree initializer)
1637 {
1638 finish_decl (var, input_location, initializer, NULL_TREE, NULL_TREE);
1639 }
1640
1641 /* Find the decl for the constant string class reference. This is only
1642 used for the NeXT runtime. */
1643
1644 static tree
1645 setup_string_decl (void)
1646 {
1647 char *name;
1648 size_t length;
1649
1650 /* %s in format will provide room for terminating null */
1651 length = strlen (STRING_OBJECT_GLOBAL_FORMAT)
1652 + strlen (constant_string_class_name);
1653 name = XNEWVEC (char, length);
1654 sprintf (name, STRING_OBJECT_GLOBAL_FORMAT,
1655 constant_string_class_name);
1656 constant_string_global_id = get_identifier (name);
1657 string_class_decl = lookup_name (constant_string_global_id);
1658
1659 return string_class_decl;
1660 }
1661
1662 /* Purpose: "play" parser, creating/installing representations
1663 of the declarations that are required by Objective-C.
1664
1665 Model:
1666
1667 type_spec--------->sc_spec
1668 (tree_list) (tree_list)
1669 | |
1670 | |
1671 identifier_node identifier_node */
1672
1673 static void
1674 synth_module_prologue (void)
1675 {
1676 tree type;
1677 enum debug_info_type save_write_symbols = write_symbols;
1678 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
1679
1680 /* Suppress outputting debug symbols, because
1681 dbxout_init hasn't been called yet. */
1682 write_symbols = NO_DEBUG;
1683 debug_hooks = &do_nothing_debug_hooks;
1684
1685 #ifdef OBJCPLUS
1686 push_lang_context (lang_name_c); /* extern "C" */
1687 #endif
1688
1689 /* The following are also defined in <objc/objc.h> and friends. */
1690
1691 objc_object_id = get_identifier (TAG_OBJECT);
1692 objc_class_id = get_identifier (TAG_CLASS);
1693
1694 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
1695 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
1696
1697 objc_object_type = build_pointer_type (objc_object_reference);
1698 objc_class_type = build_pointer_type (objc_class_reference);
1699
1700 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
1701 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
1702
1703 /* Declare the 'id' and 'Class' typedefs. */
1704
1705 type = lang_hooks.decls.pushdecl (build_decl (input_location,
1706 TYPE_DECL,
1707 objc_object_name,
1708 objc_object_type));
1709 TREE_NO_WARNING (type) = 1;
1710 type = lang_hooks.decls.pushdecl (build_decl (input_location,
1711 TYPE_DECL,
1712 objc_class_name,
1713 objc_class_type));
1714 TREE_NO_WARNING (type) = 1;
1715
1716 /* Forward-declare '@interface Protocol'. */
1717
1718 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
1719 objc_declare_class (tree_cons (NULL_TREE, type, NULL_TREE));
1720 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
1721 type));
1722
1723 /* Declare type of selector-objects that represent an operation name. */
1724
1725 if (flag_next_runtime)
1726 /* `struct objc_selector *' */
1727 objc_selector_type
1728 = build_pointer_type (xref_tag (RECORD_TYPE,
1729 get_identifier (TAG_SELECTOR)));
1730 else
1731 /* `const struct objc_selector *' */
1732 objc_selector_type
1733 = build_pointer_type
1734 (build_qualified_type (xref_tag (RECORD_TYPE,
1735 get_identifier (TAG_SELECTOR)),
1736 TYPE_QUAL_CONST));
1737
1738 /* Declare receiver type used for dispatching messages to 'super'. */
1739
1740 /* `struct objc_super *' */
1741 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
1742 get_identifier (TAG_SUPER)));
1743
1744 /* Declare pointers to method and ivar lists. */
1745 objc_method_list_ptr = build_pointer_type
1746 (xref_tag (RECORD_TYPE,
1747 get_identifier (UTAG_METHOD_LIST)));
1748 objc_method_proto_list_ptr
1749 = build_pointer_type (xref_tag (RECORD_TYPE,
1750 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
1751 objc_ivar_list_ptr = build_pointer_type
1752 (xref_tag (RECORD_TYPE,
1753 get_identifier (UTAG_IVAR_LIST)));
1754
1755 /* TREE_NOTHROW is cleared for the message-sending functions,
1756 because the function that gets called can throw in Obj-C++, or
1757 could itself call something that can throw even in Obj-C. */
1758
1759 if (flag_next_runtime)
1760 {
1761 /* NB: In order to call one of the ..._stret (struct-returning)
1762 functions, the function *MUST* first be cast to a signature that
1763 corresponds to the actual ObjC method being invoked. This is
1764 what is done by the build_objc_method_call() routine below. */
1765
1766 /* id objc_msgSend (id, SEL, ...); */
1767 /* id objc_msgSendNonNil (id, SEL, ...); */
1768 /* id objc_msgSend_stret (id, SEL, ...); */
1769 /* id objc_msgSendNonNil_stret (id, SEL, ...); */
1770 type
1771 = build_varargs_function_type_list (objc_object_type,
1772 objc_object_type,
1773 objc_selector_type,
1774 NULL_TREE);
1775 umsg_decl = add_builtin_function (TAG_MSGSEND,
1776 type, 0, NOT_BUILT_IN,
1777 NULL, NULL_TREE);
1778 umsg_nonnil_decl = add_builtin_function (TAG_MSGSEND_NONNIL,
1779 type, 0, NOT_BUILT_IN,
1780 NULL, NULL_TREE);
1781 umsg_stret_decl = add_builtin_function (TAG_MSGSEND_STRET,
1782 type, 0, NOT_BUILT_IN,
1783 NULL, NULL_TREE);
1784 umsg_nonnil_stret_decl = add_builtin_function (TAG_MSGSEND_NONNIL_STRET,
1785 type, 0, NOT_BUILT_IN,
1786 NULL, NULL_TREE);
1787
1788 /* These can throw, because the function that gets called can throw
1789 in Obj-C++, or could itself call something that can throw even
1790 in Obj-C. */
1791 TREE_NOTHROW (umsg_decl) = 0;
1792 TREE_NOTHROW (umsg_nonnil_decl) = 0;
1793 TREE_NOTHROW (umsg_stret_decl) = 0;
1794 TREE_NOTHROW (umsg_nonnil_stret_decl) = 0;
1795
1796 /* id objc_msgSend_Fast (id, SEL, ...)
1797 __attribute__ ((hard_coded_address (OFFS_MSGSEND_FAST))); */
1798 #ifdef OFFS_MSGSEND_FAST
1799 umsg_fast_decl = add_builtin_function (TAG_MSGSEND_FAST,
1800 type, 0, NOT_BUILT_IN,
1801 NULL, NULL_TREE);
1802 TREE_NOTHROW (umsg_fast_decl) = 0;
1803 DECL_ATTRIBUTES (umsg_fast_decl)
1804 = tree_cons (get_identifier ("hard_coded_address"),
1805 build_int_cst (NULL_TREE, OFFS_MSGSEND_FAST),
1806 NULL_TREE);
1807 #else
1808 /* No direct dispatch available. */
1809 umsg_fast_decl = umsg_decl;
1810 #endif
1811
1812 /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
1813 /* id objc_msgSendSuper_stret (struct objc_super *, SEL, ...); */
1814 type
1815 = build_varargs_function_type_list (objc_object_type,
1816 objc_super_type,
1817 objc_selector_type,
1818 NULL_TREE);
1819 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
1820 type, 0, NOT_BUILT_IN,
1821 NULL, NULL_TREE);
1822 umsg_super_stret_decl = add_builtin_function (TAG_MSGSENDSUPER_STRET,
1823 type, 0, NOT_BUILT_IN, 0,
1824 NULL_TREE);
1825 TREE_NOTHROW (umsg_super_decl) = 0;
1826 TREE_NOTHROW (umsg_super_stret_decl) = 0;
1827 }
1828 else
1829 {
1830 /* GNU runtime messenger entry points. */
1831
1832 /* typedef id (*IMP)(id, SEL, ...); */
1833 tree ftype =
1834 build_varargs_function_type_list (objc_object_type,
1835 objc_object_type,
1836 objc_selector_type,
1837 NULL_TREE);
1838 tree IMP_type = build_pointer_type (ftype);
1839
1840 /* IMP objc_msg_lookup (id, SEL); */
1841 type = build_function_type_list (IMP_type,
1842 objc_object_type,
1843 objc_selector_type,
1844 NULL_TREE);
1845 umsg_decl = add_builtin_function (TAG_MSGSEND,
1846 type, 0, NOT_BUILT_IN,
1847 NULL, NULL_TREE);
1848 TREE_NOTHROW (umsg_decl) = 0;
1849
1850 /* IMP objc_msg_lookup_super (struct objc_super *, SEL); */
1851 type
1852 = build_function_type_list (IMP_type,
1853 objc_super_type,
1854 objc_selector_type,
1855 NULL_TREE);
1856 umsg_super_decl = add_builtin_function (TAG_MSGSENDSUPER,
1857 type, 0, NOT_BUILT_IN,
1858 NULL, NULL_TREE);
1859 TREE_NOTHROW (umsg_super_decl) = 0;
1860
1861 /* The following GNU runtime entry point is called to initialize
1862 each module:
1863
1864 __objc_exec_class (void *); */
1865 type
1866 = build_function_type_list (void_type_node,
1867 ptr_type_node,
1868 NULL_TREE);
1869 execclass_decl = add_builtin_function (TAG_EXECCLASS,
1870 type, 0, NOT_BUILT_IN,
1871 NULL, NULL_TREE);
1872 }
1873
1874 /* id objc_getClass (const char *); */
1875
1876 type = build_function_type_list (objc_object_type,
1877 const_string_type_node,
1878 NULL_TREE);
1879
1880 objc_get_class_decl
1881 = add_builtin_function (TAG_GETCLASS, type, 0, NOT_BUILT_IN,
1882 NULL, NULL_TREE);
1883
1884 /* id objc_getMetaClass (const char *); */
1885
1886 objc_get_meta_class_decl
1887 = add_builtin_function (TAG_GETMETACLASS, type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
1888
1889 build_class_template ();
1890 build_super_template ();
1891 build_protocol_template ();
1892 build_category_template ();
1893 build_objc_exception_stuff ();
1894
1895 if (flag_next_runtime)
1896 build_next_objc_exception_stuff ();
1897
1898 /* static SEL _OBJC_SELECTOR_TABLE[]; */
1899
1900 if (! flag_next_runtime)
1901 build_selector_table_decl ();
1902
1903 /* Forward declare constant_string_id and constant_string_type. */
1904 if (!constant_string_class_name)
1905 constant_string_class_name = default_constant_string_class_name;
1906
1907 constant_string_id = get_identifier (constant_string_class_name);
1908 objc_declare_class (tree_cons (NULL_TREE, constant_string_id, NULL_TREE));
1909
1910 /* Pre-build the following entities - for speed/convenience. */
1911 self_id = get_identifier ("self");
1912 ucmd_id = get_identifier ("_cmd");
1913
1914 #ifdef OBJCPLUS
1915 pop_lang_context ();
1916 #endif
1917
1918 write_symbols = save_write_symbols;
1919 debug_hooks = save_hooks;
1920 }
1921
1922 /* Ensure that the ivar list for NSConstantString/NXConstantString
1923 (or whatever was specified via `-fconstant-string-class')
1924 contains fields at least as large as the following three, so that
1925 the runtime can stomp on them with confidence:
1926
1927 struct STRING_OBJECT_CLASS_NAME
1928 {
1929 Object isa;
1930 char *cString;
1931 unsigned int length;
1932 }; */
1933
1934 static int
1935 check_string_class_template (void)
1936 {
1937 tree field_decl = objc_get_class_ivars (constant_string_id);
1938
1939 #define AT_LEAST_AS_LARGE_AS(F, T) \
1940 (F && TREE_CODE (F) == FIELD_DECL \
1941 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
1942 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
1943
1944 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1945 return 0;
1946
1947 field_decl = DECL_CHAIN (field_decl);
1948 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
1949 return 0;
1950
1951 field_decl = DECL_CHAIN (field_decl);
1952 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
1953
1954 #undef AT_LEAST_AS_LARGE_AS
1955 }
1956
1957 /* Avoid calling `check_string_class_template ()' more than once. */
1958 static GTY(()) int string_layout_checked;
1959
1960 /* Construct an internal string layout to be used as a template for
1961 creating NSConstantString/NXConstantString instances. */
1962
1963 static tree
1964 objc_build_internal_const_str_type (void)
1965 {
1966 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
1967 tree fields = build_decl (input_location,
1968 FIELD_DECL, NULL_TREE, ptr_type_node);
1969 tree field = build_decl (input_location,
1970 FIELD_DECL, NULL_TREE, ptr_type_node);
1971
1972 DECL_CHAIN (field) = fields; fields = field;
1973 field = build_decl (input_location,
1974 FIELD_DECL, NULL_TREE, unsigned_type_node);
1975 DECL_CHAIN (field) = fields; fields = field;
1976 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
1977 reverse order! */
1978 finish_builtin_struct (type, "__builtin_ObjCString",
1979 fields, NULL_TREE);
1980
1981 return type;
1982 }
1983
1984 /* Custom build_string which sets TREE_TYPE! */
1985
1986 static tree
1987 my_build_string (int len, const char *str)
1988 {
1989 return fix_string_type (build_string (len, str));
1990 }
1991
1992 /* Build a string with contents STR and length LEN and convert it to a
1993 pointer. */
1994
1995 static tree
1996 my_build_string_pointer (int len, const char *str)
1997 {
1998 tree string = my_build_string (len, str);
1999 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
2000 return build1 (ADDR_EXPR, ptrtype, string);
2001 }
2002
2003 static hashval_t
2004 string_hash (const void *ptr)
2005 {
2006 const_tree const str = ((const struct string_descriptor *)ptr)->literal;
2007 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
2008 int i, len = TREE_STRING_LENGTH (str);
2009 hashval_t h = len;
2010
2011 for (i = 0; i < len; i++)
2012 h = ((h * 613) + p[i]);
2013
2014 return h;
2015 }
2016
2017 static int
2018 string_eq (const void *ptr1, const void *ptr2)
2019 {
2020 const_tree const str1 = ((const struct string_descriptor *)ptr1)->literal;
2021 const_tree const str2 = ((const struct string_descriptor *)ptr2)->literal;
2022 int len1 = TREE_STRING_LENGTH (str1);
2023
2024 return (len1 == TREE_STRING_LENGTH (str2)
2025 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
2026 len1));
2027 }
2028
2029 /* Given a chain of STRING_CST's, build a static instance of
2030 NXConstantString which points at the concatenation of those
2031 strings. We place the string object in the __string_objects
2032 section of the __OBJC segment. The Objective-C runtime will
2033 initialize the isa pointers of the string objects to point at the
2034 NXConstantString class object. */
2035
2036 tree
2037 objc_build_string_object (tree string)
2038 {
2039 tree constructor, constant_string_class;
2040 int length;
2041 tree fields, addr;
2042 struct string_descriptor *desc, key;
2043 void **loc;
2044
2045 /* Prep the string argument. */
2046 string = fix_string_type (string);
2047 TREE_SET_CODE (string, STRING_CST);
2048 length = TREE_STRING_LENGTH (string) - 1;
2049
2050 /* Check whether the string class being used actually exists and has the
2051 correct ivar layout. */
2052 if (!string_layout_checked)
2053 {
2054 string_layout_checked = -1;
2055 constant_string_class = lookup_interface (constant_string_id);
2056 internal_const_str_type = objc_build_internal_const_str_type ();
2057
2058 if (!constant_string_class
2059 || !(constant_string_type
2060 = CLASS_STATIC_TEMPLATE (constant_string_class)))
2061 error ("cannot find interface declaration for %qE",
2062 constant_string_id);
2063 /* The NSConstantString/NXConstantString ivar layout is now known. */
2064 else if (!check_string_class_template ())
2065 error ("interface %qE does not have valid constant string layout",
2066 constant_string_id);
2067 /* For the NeXT runtime, we can generate a literal reference
2068 to the string class, don't need to run a constructor. */
2069 else if (flag_next_runtime && !setup_string_decl ())
2070 error ("cannot find reference tag for class %qE",
2071 constant_string_id);
2072 else
2073 {
2074 string_layout_checked = 1; /* Success! */
2075 add_class_reference (constant_string_id);
2076 }
2077 }
2078
2079 if (string_layout_checked == -1)
2080 return error_mark_node;
2081
2082 /* Perhaps we already constructed a constant string just like this one? */
2083 key.literal = string;
2084 loc = htab_find_slot (string_htab, &key, INSERT);
2085 desc = (struct string_descriptor *) *loc;
2086
2087 if (!desc)
2088 {
2089 tree var;
2090 VEC(constructor_elt,gc) *v = NULL;
2091 *loc = desc = ggc_alloc_string_descriptor ();
2092 desc->literal = string;
2093
2094 /* GNU: (NXConstantString *) & ((__builtin_ObjCString) { NULL, string, length }) */
2095 /* NeXT: (NSConstantString *) & ((__builtin_ObjCString) { isa, string, length }) */
2096 fields = TYPE_FIELDS (internal_const_str_type);
2097 CONSTRUCTOR_APPEND_ELT (v, fields,
2098 flag_next_runtime
2099 ? build_unary_op (input_location,
2100 ADDR_EXPR, string_class_decl, 0)
2101 : build_int_cst (NULL_TREE, 0));
2102 fields = DECL_CHAIN (fields);
2103 CONSTRUCTOR_APPEND_ELT (v, fields,
2104 build_unary_op (input_location,
2105 ADDR_EXPR, string, 1));
2106 fields = DECL_CHAIN (fields);
2107 CONSTRUCTOR_APPEND_ELT (v, fields, build_int_cst (NULL_TREE, length));
2108 constructor = objc_build_constructor (internal_const_str_type, v);
2109
2110 if (!flag_next_runtime)
2111 constructor
2112 = objc_add_static_instance (constructor, constant_string_type);
2113 else
2114 {
2115 var = build_decl (input_location,
2116 CONST_DECL, NULL, TREE_TYPE (constructor));
2117 DECL_INITIAL (var) = constructor;
2118 TREE_STATIC (var) = 1;
2119 pushdecl_top_level (var);
2120 constructor = var;
2121 }
2122 desc->constructor = constructor;
2123 }
2124
2125 addr = convert (build_pointer_type (constant_string_type),
2126 build_unary_op (input_location,
2127 ADDR_EXPR, desc->constructor, 1));
2128
2129 return addr;
2130 }
2131
2132 /* Declare a static instance of CLASS_DECL initialized by CONSTRUCTOR. */
2133
2134 static GTY(()) int num_static_inst;
2135
2136 static tree
2137 objc_add_static_instance (tree constructor, tree class_decl)
2138 {
2139 tree *chain, decl;
2140 char buf[256];
2141
2142 /* Find the list of static instances for the CLASS_DECL. Create one if
2143 not found. */
2144 for (chain = &objc_static_instances;
2145 *chain && TREE_VALUE (*chain) != class_decl;
2146 chain = &TREE_CHAIN (*chain));
2147 if (!*chain)
2148 {
2149 *chain = tree_cons (NULL_TREE, class_decl, NULL_TREE);
2150 add_objc_string (OBJC_TYPE_NAME (class_decl), class_names);
2151 }
2152
2153 sprintf (buf, "_OBJC_INSTANCE_%d", num_static_inst++);
2154 decl = build_decl (input_location,
2155 VAR_DECL, get_identifier (buf), class_decl);
2156 TREE_STATIC (decl) = 1;
2157 DECL_ARTIFICIAL (decl) = 1;
2158 TREE_USED (decl) = 1;
2159 DECL_INITIAL (decl) = constructor;
2160
2161 /* We may be writing something else just now.
2162 Postpone till end of input. */
2163 DECL_DEFER_OUTPUT (decl) = 1;
2164 pushdecl_top_level (decl);
2165 rest_of_decl_compilation (decl, 1, 0);
2166
2167 /* Add the DECL to the head of this CLASS' list. */
2168 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE, decl, TREE_PURPOSE (*chain));
2169
2170 return decl;
2171 }
2172
2173 /* Build a static constant CONSTRUCTOR
2174 with type TYPE and elements ELTS. */
2175
2176 static tree
2177 objc_build_constructor (tree type, VEC(constructor_elt,gc) *elts)
2178 {
2179 tree constructor = build_constructor (type, elts);
2180
2181 TREE_CONSTANT (constructor) = 1;
2182 TREE_STATIC (constructor) = 1;
2183 TREE_READONLY (constructor) = 1;
2184
2185 #ifdef OBJCPLUS
2186 /* Adjust for impedance mismatch. We should figure out how to build
2187 CONSTRUCTORs that consistently please both the C and C++ gods. */
2188 if (!VEC_index (constructor_elt, elts, 0)->index)
2189 TREE_TYPE (constructor) = init_list_type_node;
2190 #endif
2191
2192 return constructor;
2193 }
2194 \f
2195 /* Take care of defining and initializing _OBJC_SYMBOLS. */
2196
2197 /* Predefine the following data type:
2198
2199 struct _objc_symtab
2200 {
2201 long sel_ref_cnt;
2202 SEL *refs;
2203 short cls_def_cnt;
2204 short cat_def_cnt;
2205 void *defs[cls_def_cnt + cat_def_cnt];
2206 }; */
2207
2208 static void
2209 build_objc_symtab_template (void)
2210 {
2211 tree fields, *chain = NULL;
2212
2213 objc_symtab_template = objc_start_struct (get_identifier (UTAG_SYMTAB));
2214
2215 /* long sel_ref_cnt; */
2216 fields = add_field_decl (long_integer_type_node, "sel_ref_cnt", &chain);
2217
2218 /* SEL *refs; */
2219 add_field_decl (build_pointer_type (objc_selector_type), "refs", &chain);
2220
2221 /* short cls_def_cnt; */
2222 add_field_decl (short_integer_type_node, "cls_def_cnt", &chain);
2223
2224 /* short cat_def_cnt; */
2225 add_field_decl (short_integer_type_node, "cat_def_cnt", &chain);
2226
2227 if (imp_count || cat_count || !flag_next_runtime)
2228 {
2229 /* void *defs[imp_count + cat_count (+ 1)]; */
2230 /* NB: The index is one less than the size of the array. */
2231 int index = imp_count + cat_count + (flag_next_runtime ? -1: 0);
2232 tree array_type = build_sized_array_type (ptr_type_node, index + 1);
2233 add_field_decl (array_type, "defs", &chain);
2234 }
2235
2236 objc_finish_struct (objc_symtab_template, fields);
2237 }
2238
2239 /* Create the initial value for the `defs' field of _objc_symtab.
2240 This is a CONSTRUCTOR. */
2241
2242 static tree
2243 init_def_list (tree type)
2244 {
2245 tree expr;
2246 struct imp_entry *impent;
2247 VEC(constructor_elt,gc) *v = NULL;
2248
2249 if (imp_count)
2250 for (impent = imp_list; impent; impent = impent->next)
2251 {
2252 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
2253 {
2254 expr = build_unary_op (input_location,
2255 ADDR_EXPR, impent->class_decl, 0);
2256 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2257 }
2258 }
2259
2260 if (cat_count)
2261 for (impent = imp_list; impent; impent = impent->next)
2262 {
2263 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2264 {
2265 expr = build_unary_op (input_location,
2266 ADDR_EXPR, impent->class_decl, 0);
2267 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2268 }
2269 }
2270
2271 if (!flag_next_runtime)
2272 {
2273 /* statics = { ..., _OBJC_STATIC_INSTANCES, ... } */
2274 if (static_instances_decl)
2275 expr = build_unary_op (input_location,
2276 ADDR_EXPR, static_instances_decl, 0);
2277 else
2278 expr = integer_zero_node;
2279
2280 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2281 }
2282
2283 return objc_build_constructor (type, v);
2284 }
2285
2286 /* Construct the initial value for all of _objc_symtab. */
2287
2288 static tree
2289 init_objc_symtab (tree type)
2290 {
2291 VEC(constructor_elt,gc) *v = NULL;
2292
2293 /* sel_ref_cnt = { ..., 5, ... } */
2294
2295 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2296 build_int_cst (long_integer_type_node, 0));
2297
2298 /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
2299
2300 if (flag_next_runtime || ! sel_ref_chain)
2301 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, convert (
2302 build_pointer_type (objc_selector_type),
2303 integer_zero_node));
2304 else
2305 {
2306 tree expr = build_unary_op (input_location, ADDR_EXPR,
2307 UOBJC_SELECTOR_TABLE_decl, 1);
2308
2309 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2310 convert (build_pointer_type (objc_selector_type),
2311 expr));
2312 }
2313
2314 /* cls_def_cnt = { ..., 5, ... } */
2315
2316 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2317 build_int_cst (short_integer_type_node, imp_count));
2318
2319 /* cat_def_cnt = { ..., 5, ... } */
2320
2321 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2322 build_int_cst (short_integer_type_node, cat_count));
2323
2324 /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
2325
2326 if (imp_count || cat_count || !flag_next_runtime)
2327 {
2328
2329 tree field = TYPE_FIELDS (type);
2330 field = DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (DECL_CHAIN (field))));
2331
2332 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_def_list (TREE_TYPE (field)));
2333 }
2334
2335 return objc_build_constructor (type, v);
2336 }
2337
2338 /* Generate forward declarations for metadata such as
2339 'OBJC_CLASS_...'. */
2340
2341 static tree
2342 build_metadata_decl (const char *name, tree type)
2343 {
2344 tree decl;
2345
2346 /* struct TYPE NAME_<name>; */
2347 decl = start_var_decl (type, synth_id_with_class_suffix
2348 (name,
2349 objc_implementation_context));
2350
2351 return decl;
2352 }
2353
2354 /* Push forward-declarations of all the categories so that
2355 init_def_list can use them in a CONSTRUCTOR. */
2356
2357 static void
2358 forward_declare_categories (void)
2359 {
2360 struct imp_entry *impent;
2361 tree sav = objc_implementation_context;
2362
2363 for (impent = imp_list; impent; impent = impent->next)
2364 {
2365 if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
2366 {
2367 /* Set an invisible arg to synth_id_with_class_suffix. */
2368 objc_implementation_context = impent->imp_context;
2369 /* extern struct objc_category _OBJC_CATEGORY_<name>; */
2370 impent->class_decl = build_metadata_decl ("_OBJC_CATEGORY",
2371 objc_category_template);
2372 }
2373 }
2374 objc_implementation_context = sav;
2375 }
2376
2377 /* Create the declaration of _OBJC_SYMBOLS, with type `struct _objc_symtab'
2378 and initialized appropriately. */
2379
2380 static void
2381 generate_objc_symtab_decl (void)
2382 {
2383
2384 build_objc_symtab_template ();
2385 UOBJC_SYMBOLS_decl = start_var_decl (objc_symtab_template, "_OBJC_SYMBOLS");
2386 finish_var_decl (UOBJC_SYMBOLS_decl,
2387 init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)));
2388 }
2389 \f
2390 static tree
2391 init_module_descriptor (tree type)
2392 {
2393 tree expr;
2394 VEC(constructor_elt,gc) *v = NULL;
2395
2396 /* version = { 1, ... } */
2397
2398 expr = build_int_cst (long_integer_type_node, OBJC_VERSION);
2399 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2400
2401 /* size = { ..., sizeof (struct _objc_module), ... } */
2402
2403 expr = convert (long_integer_type_node,
2404 size_in_bytes (objc_module_template));
2405 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2406
2407 /* Don't provide any file name for security reasons. */
2408 /* name = { ..., "", ... } */
2409
2410 expr = add_objc_string (get_identifier (""), class_names);
2411 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2412
2413 /* symtab = { ..., _OBJC_SYMBOLS, ... } */
2414
2415 if (UOBJC_SYMBOLS_decl)
2416 expr = build_unary_op (input_location,
2417 ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
2418 else
2419 expr = null_pointer_node;
2420 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2421
2422 return objc_build_constructor (type, v);
2423 }
2424
2425 /* Write out the data structures to describe Objective C classes defined.
2426
2427 struct _objc_module { ... } _OBJC_MODULE = { ... }; */
2428
2429 static void
2430 build_module_descriptor (void)
2431 {
2432 tree decls, *chain = NULL;
2433
2434 #ifdef OBJCPLUS
2435 push_lang_context (lang_name_c); /* extern "C" */
2436 #endif
2437
2438 objc_module_template = objc_start_struct (get_identifier (UTAG_MODULE));
2439
2440 /* long version; */
2441 decls = add_field_decl (long_integer_type_node, "version", &chain);
2442
2443 /* long size; */
2444 add_field_decl (long_integer_type_node, "size", &chain);
2445
2446 /* char *name; */
2447 add_field_decl (string_type_node, "name", &chain);
2448
2449 /* struct _objc_symtab *symtab; */
2450 add_field_decl (build_pointer_type (xref_tag (RECORD_TYPE,
2451 get_identifier (UTAG_SYMTAB))),
2452 "symtab", &chain);
2453
2454 objc_finish_struct (objc_module_template, decls);
2455
2456 /* Create an instance of "_objc_module". */
2457 UOBJC_MODULES_decl = start_var_decl (objc_module_template, "_OBJC_MODULES");
2458 /* This is the root of the metadata for defined classes and categories, it
2459 is referenced by the runtime and, therefore, needed. */
2460 DECL_PRESERVE_P (UOBJC_MODULES_decl) = 1;
2461 finish_var_decl (UOBJC_MODULES_decl,
2462 init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)));
2463
2464 #ifdef OBJCPLUS
2465 pop_lang_context ();
2466 #endif
2467 }
2468
2469 /* The GNU runtime requires us to provide a static initializer function
2470 for each module:
2471
2472 static void __objc_gnu_init (void) {
2473 __objc_exec_class (&L_OBJC_MODULES);
2474 } */
2475
2476 static void
2477 build_module_initializer_routine (void)
2478 {
2479 tree body;
2480
2481 #ifdef OBJCPLUS
2482 push_lang_context (lang_name_c); /* extern "C" */
2483 #endif
2484
2485 objc_push_parm (build_decl (input_location,
2486 PARM_DECL, NULL_TREE, void_type_node));
2487 #ifdef OBJCPLUS
2488 objc_start_function (get_identifier (TAG_GNUINIT),
2489 build_function_type_list (void_type_node, NULL_TREE),
2490 NULL_TREE, NULL_TREE);
2491 #else
2492 objc_start_function (get_identifier (TAG_GNUINIT),
2493 build_function_type_list (void_type_node, NULL_TREE),
2494 NULL_TREE, objc_get_parm_info (0));
2495 #endif
2496 body = c_begin_compound_stmt (true);
2497 add_stmt (build_function_call
2498 (input_location,
2499 execclass_decl,
2500 build_tree_list
2501 (NULL_TREE,
2502 build_unary_op (input_location, ADDR_EXPR,
2503 UOBJC_MODULES_decl, 0))));
2504 add_stmt (c_end_compound_stmt (input_location, body, true));
2505
2506 TREE_PUBLIC (current_function_decl) = 0;
2507
2508 #ifndef OBJCPLUS
2509 /* For Objective-C++, we will need to call __objc_gnu_init
2510 from objc_generate_static_init_call() below. */
2511 DECL_STATIC_CONSTRUCTOR (current_function_decl) = 1;
2512 #endif
2513
2514 GNU_INIT_decl = current_function_decl;
2515 finish_function ();
2516
2517 #ifdef OBJCPLUS
2518 pop_lang_context ();
2519 #endif
2520 }
2521
2522 #ifdef OBJCPLUS
2523 /* Return 1 if the __objc_gnu_init function has been synthesized and needs
2524 to be called by the module initializer routine. */
2525
2526 int
2527 objc_static_init_needed_p (void)
2528 {
2529 return (GNU_INIT_decl != NULL_TREE);
2530 }
2531
2532 /* Generate a call to the __objc_gnu_init initializer function. */
2533
2534 tree
2535 objc_generate_static_init_call (tree ctors ATTRIBUTE_UNUSED)
2536 {
2537 add_stmt (build_stmt (input_location, EXPR_STMT,
2538 build_function_call (input_location,
2539 GNU_INIT_decl, NULL_TREE)));
2540
2541 return ctors;
2542 }
2543 #endif /* OBJCPLUS */
2544
2545 /* Return the DECL of the string IDENT in the SECTION. */
2546
2547 static tree
2548 get_objc_string_decl (tree ident, enum string_section section)
2549 {
2550 tree chain;
2551
2552 if (section == class_names)
2553 chain = class_names_chain;
2554 else if (section == meth_var_names)
2555 chain = meth_var_names_chain;
2556 else if (section == meth_var_types)
2557 chain = meth_var_types_chain;
2558 else
2559 abort ();
2560
2561 for (; chain != 0; chain = TREE_CHAIN (chain))
2562 if (TREE_VALUE (chain) == ident)
2563 return (TREE_PURPOSE (chain));
2564
2565 abort ();
2566 return NULL_TREE;
2567 }
2568
2569 /* Output references to all statically allocated objects. Return the DECL
2570 for the array built. */
2571
2572 static void
2573 generate_static_references (void)
2574 {
2575 tree expr = NULL_TREE;
2576 tree class_name, klass, decl;
2577 tree cl_chain, in_chain, type
2578 = build_array_type (build_pointer_type (void_type_node), NULL_TREE);
2579 int num_inst, num_class;
2580 char buf[256];
2581 VEC(constructor_elt,gc) *decls = NULL;
2582
2583 if (flag_next_runtime)
2584 gcc_unreachable ();
2585
2586 for (cl_chain = objc_static_instances, num_class = 0;
2587 cl_chain; cl_chain = TREE_CHAIN (cl_chain), num_class++)
2588 {
2589 VEC(constructor_elt,gc) *v = NULL;
2590
2591 for (num_inst = 0, in_chain = TREE_PURPOSE (cl_chain);
2592 in_chain; num_inst++, in_chain = TREE_CHAIN (in_chain));
2593
2594 sprintf (buf, "_OBJC_STATIC_INSTANCES_%d", num_class);
2595 decl = start_var_decl (type, buf);
2596
2597 /* Output {class_name, ...}. */
2598 klass = TREE_VALUE (cl_chain);
2599 class_name = get_objc_string_decl (OBJC_TYPE_NAME (klass), class_names);
2600 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2601 build_unary_op (input_location,
2602 ADDR_EXPR, class_name, 1));
2603
2604 /* Output {..., instance, ...}. */
2605 for (in_chain = TREE_PURPOSE (cl_chain);
2606 in_chain; in_chain = TREE_CHAIN (in_chain))
2607 {
2608 expr = build_unary_op (input_location,
2609 ADDR_EXPR, TREE_VALUE (in_chain), 1);
2610 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2611 }
2612
2613 /* Output {..., NULL}. */
2614 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
2615
2616 expr = objc_build_constructor (TREE_TYPE (decl), v);
2617 finish_var_decl (decl, expr);
2618 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE,
2619 build_unary_op (input_location,
2620 ADDR_EXPR, decl, 1));
2621 }
2622
2623 CONSTRUCTOR_APPEND_ELT (decls, NULL_TREE, build_int_cst (NULL_TREE, 0));
2624 expr = objc_build_constructor (type, decls);
2625 static_instances_decl = start_var_decl (type, "_OBJC_STATIC_INSTANCES");
2626 finish_var_decl (static_instances_decl, expr);
2627 }
2628
2629 static GTY(()) int selector_reference_idx;
2630
2631 static tree
2632 build_selector_reference_decl (void)
2633 {
2634 tree decl;
2635 char buf[256];
2636
2637 sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", selector_reference_idx++);
2638 decl = start_var_decl (objc_selector_type, buf);
2639
2640 return decl;
2641 }
2642
2643 static void
2644 build_selector_table_decl (void)
2645 {
2646 tree temp;
2647
2648 if (flag_typed_selectors)
2649 {
2650 build_selector_template ();
2651 temp = build_array_type (objc_selector_template, NULL_TREE);
2652 }
2653 else
2654 temp = build_array_type (objc_selector_type, NULL_TREE);
2655
2656 UOBJC_SELECTOR_TABLE_decl = start_var_decl (temp, "_OBJC_SELECTOR_TABLE");
2657 }
2658
2659 /* Just a handy wrapper for add_objc_string. */
2660
2661 static tree
2662 build_selector (tree ident)
2663 {
2664 return convert (objc_selector_type,
2665 add_objc_string (ident, meth_var_names));
2666 }
2667
2668 /* Used only by build_*_selector_translation_table (). */
2669 static void
2670 diagnose_missing_method (tree meth, location_t here)
2671 {
2672 tree method_chain;
2673 bool found = false;
2674 for (method_chain = meth_var_names_chain;
2675 method_chain;
2676 method_chain = TREE_CHAIN (method_chain))
2677 {
2678 if (TREE_VALUE (method_chain) == meth)
2679 {
2680 found = true;
2681 break;
2682 }
2683 }
2684
2685 if (!found)
2686 warning_at (here, 0, "creating selector for nonexistent method %qE",
2687 meth);
2688 }
2689
2690 static void
2691 build_next_selector_translation_table (void)
2692 {
2693 tree chain;
2694 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
2695 {
2696 tree expr;
2697 tree decl = TREE_PURPOSE (chain);
2698 if (warn_selector && objc_implementation_context)
2699 {
2700 location_t loc;
2701 if (decl)
2702 loc = DECL_SOURCE_LOCATION (decl);
2703 else
2704 loc = input_location;
2705 diagnose_missing_method (TREE_VALUE (chain), loc);
2706 }
2707
2708 expr = build_selector (TREE_VALUE (chain));
2709
2710 if (decl)
2711 {
2712 /* Entries of this form are used for references to methods.
2713 The runtime re-writes these on start-up, but the compiler can't see
2714 that and optimizes it away unless we force it. */
2715 DECL_PRESERVE_P (decl) = 1;
2716 finish_var_decl (decl, expr);
2717 }
2718 }
2719 }
2720
2721 static void
2722 build_gnu_selector_translation_table (void)
2723 {
2724 tree chain;
2725 /* int offset = 0;
2726 tree decl = NULL_TREE;*/
2727 VEC(constructor_elt,gc) *inits = NULL;
2728
2729 for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
2730 {
2731 tree expr;
2732
2733 if (warn_selector && objc_implementation_context)
2734 diagnose_missing_method (TREE_VALUE (chain), input_location);
2735
2736 expr = build_selector (TREE_VALUE (chain));
2737 /* add one for the '\0' character
2738 offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;*/
2739
2740 {
2741 if (flag_typed_selectors)
2742 {
2743 VEC(constructor_elt,gc) *v = NULL;
2744 tree encoding = get_proto_encoding (TREE_PURPOSE (chain));
2745 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
2746 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, encoding);
2747 expr = objc_build_constructor (objc_selector_template, v);
2748 }
2749
2750 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
2751 }
2752 } /* each element in the chain */
2753
2754 {
2755 /* Cause the selector table (previously forward-declared)
2756 to be actually output. */
2757 tree expr;
2758
2759 if (flag_typed_selectors)
2760 {
2761 VEC(constructor_elt,gc) *v = NULL;
2762 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
2763 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
2764 expr = objc_build_constructor (objc_selector_template, v);
2765 }
2766 else
2767 expr = integer_zero_node;
2768
2769 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
2770 expr = objc_build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
2771 inits);
2772 finish_var_decl (UOBJC_SELECTOR_TABLE_decl, expr);
2773 }
2774 }
2775
2776 static tree
2777 get_proto_encoding (tree proto)
2778 {
2779 tree encoding;
2780 if (proto)
2781 {
2782 if (! METHOD_ENCODING (proto))
2783 {
2784 encoding = encode_method_prototype (proto);
2785 METHOD_ENCODING (proto) = encoding;
2786 }
2787 else
2788 encoding = METHOD_ENCODING (proto);
2789
2790 return add_objc_string (encoding, meth_var_types);
2791 }
2792 else
2793 return build_int_cst (NULL_TREE, 0);
2794 }
2795
2796 /* sel_ref_chain is a list whose "value" fields will be instances of
2797 identifier_node that represent the selector. LOC is the location of
2798 the @selector. */
2799
2800 static tree
2801 build_typed_selector_reference (location_t loc, tree ident, tree prototype)
2802 {
2803 tree *chain = &sel_ref_chain;
2804 tree expr;
2805 int index = 0;
2806
2807 while (*chain)
2808 {
2809 if (TREE_PURPOSE (*chain) == prototype && TREE_VALUE (*chain) == ident)
2810 goto return_at_index;
2811
2812 index++;
2813 chain = &TREE_CHAIN (*chain);
2814 }
2815
2816 *chain = tree_cons (prototype, ident, NULL_TREE);
2817
2818 return_at_index:
2819 expr = build_unary_op (loc, ADDR_EXPR,
2820 build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
2821 build_int_cst (NULL_TREE, index)),
2822 1);
2823 return convert (objc_selector_type, expr);
2824 }
2825
2826 static tree
2827 build_selector_reference (location_t loc, tree ident)
2828 {
2829 tree *chain = &sel_ref_chain;
2830 tree expr;
2831 int index = 0;
2832
2833 while (*chain)
2834 {
2835 if (TREE_VALUE (*chain) == ident)
2836 return (flag_next_runtime
2837 ? TREE_PURPOSE (*chain)
2838 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
2839 build_int_cst (NULL_TREE, index)));
2840
2841 index++;
2842 chain = &TREE_CHAIN (*chain);
2843 }
2844
2845 expr = (flag_next_runtime ? build_selector_reference_decl (): NULL_TREE);
2846
2847 *chain = tree_cons (expr, ident, NULL_TREE);
2848
2849 return (flag_next_runtime
2850 ? expr
2851 : build_array_ref (loc, UOBJC_SELECTOR_TABLE_decl,
2852 build_int_cst (NULL_TREE, index)));
2853 }
2854
2855 static GTY(()) int class_reference_idx;
2856
2857 static tree
2858 build_class_reference_decl (void)
2859 {
2860 tree decl;
2861 char buf[256];
2862
2863 sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", class_reference_idx++);
2864 decl = start_var_decl (objc_class_type, buf);
2865
2866 return decl;
2867 }
2868
2869 /* Create a class reference, but don't create a variable to reference
2870 it. */
2871
2872 static void
2873 add_class_reference (tree ident)
2874 {
2875 tree chain;
2876
2877 if ((chain = cls_ref_chain))
2878 {
2879 tree tail;
2880 do
2881 {
2882 if (ident == TREE_VALUE (chain))
2883 return;
2884
2885 tail = chain;
2886 chain = TREE_CHAIN (chain);
2887 }
2888 while (chain);
2889
2890 /* Append to the end of the list */
2891 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
2892 }
2893 else
2894 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
2895 }
2896
2897 /* Get a class reference, creating it if necessary. Also create the
2898 reference variable. */
2899
2900 tree
2901 objc_get_class_reference (tree ident)
2902 {
2903 tree orig_ident = (DECL_P (ident)
2904 ? DECL_NAME (ident)
2905 : TYPE_P (ident)
2906 ? OBJC_TYPE_NAME (ident)
2907 : ident);
2908 bool local_scope = false;
2909
2910 #ifdef OBJCPLUS
2911 if (processing_template_decl)
2912 /* Must wait until template instantiation time. */
2913 return build_min_nt (CLASS_REFERENCE_EXPR, ident);
2914 #endif
2915
2916 if (TREE_CODE (ident) == TYPE_DECL)
2917 ident = (DECL_ORIGINAL_TYPE (ident)
2918 ? DECL_ORIGINAL_TYPE (ident)
2919 : TREE_TYPE (ident));
2920
2921 #ifdef OBJCPLUS
2922 if (TYPE_P (ident)
2923 && CP_TYPE_CONTEXT (ident) != global_namespace)
2924 local_scope = true;
2925 #endif
2926
2927 if (local_scope || !(ident = objc_is_class_name (ident)))
2928 {
2929 error ("%qE is not an Objective-C class name or alias",
2930 orig_ident);
2931 return error_mark_node;
2932 }
2933
2934 if (flag_next_runtime && !flag_zero_link)
2935 {
2936 tree *chain;
2937 tree decl;
2938
2939 for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
2940 if (TREE_VALUE (*chain) == ident)
2941 {
2942 if (! TREE_PURPOSE (*chain))
2943 TREE_PURPOSE (*chain) = build_class_reference_decl ();
2944
2945 return TREE_PURPOSE (*chain);
2946 }
2947
2948 decl = build_class_reference_decl ();
2949 *chain = tree_cons (decl, ident, NULL_TREE);
2950 return decl;
2951 }
2952 else
2953 {
2954 tree params;
2955
2956 add_class_reference (ident);
2957
2958 params = build_tree_list (NULL_TREE,
2959 my_build_string_pointer
2960 (IDENTIFIER_LENGTH (ident) + 1,
2961 IDENTIFIER_POINTER (ident)));
2962
2963 assemble_external (objc_get_class_decl);
2964 return build_function_call (input_location, objc_get_class_decl, params);
2965 }
2966 }
2967
2968 /* For each string section we have a chain which maps identifier nodes
2969 to decls for the strings. */
2970
2971 static GTY(()) int class_names_idx;
2972 static GTY(()) int meth_var_names_idx;
2973 static GTY(()) int meth_var_types_idx;
2974
2975 static tree
2976 add_objc_string (tree ident, enum string_section section)
2977 {
2978 tree *chain, decl, type, string_expr;
2979 char buf[256];
2980
2981 buf[0] = 0;
2982 if (section == class_names)
2983 {
2984 chain = &class_names_chain;
2985 sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
2986 }
2987 else if (section == meth_var_names)
2988 {
2989 chain = &meth_var_names_chain;
2990 sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
2991 }
2992 else if (section == meth_var_types)
2993 {
2994 chain = &meth_var_types_chain;
2995 sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
2996 }
2997 else
2998 gcc_unreachable ();
2999
3000 while (*chain)
3001 {
3002 if (TREE_VALUE (*chain) == ident)
3003 return convert (string_type_node,
3004 build_unary_op (input_location,
3005 ADDR_EXPR, TREE_PURPOSE (*chain), 1));
3006
3007 chain = &TREE_CHAIN (*chain);
3008 }
3009
3010 type = build_sized_array_type (char_type_node, IDENTIFIER_LENGTH (ident) + 1);
3011 decl = start_var_decl (type, buf);
3012 string_expr = my_build_string (IDENTIFIER_LENGTH (ident) + 1,
3013 IDENTIFIER_POINTER (ident));
3014 TREE_CONSTANT (decl) = 1;
3015 finish_var_decl (decl, string_expr);
3016
3017 *chain = tree_cons (decl, ident, NULL_TREE);
3018
3019 return convert (string_type_node, build_unary_op (input_location,
3020 ADDR_EXPR, decl, 1));
3021 }
3022
3023 void
3024 objc_declare_alias (tree alias_ident, tree class_ident)
3025 {
3026 tree underlying_class;
3027
3028 #ifdef OBJCPLUS
3029 if (current_namespace != global_namespace) {
3030 error ("Objective-C declarations may only appear in global scope");
3031 }
3032 #endif /* OBJCPLUS */
3033
3034 if (!(underlying_class = objc_is_class_name (class_ident)))
3035 warning (0, "cannot find class %qE", class_ident);
3036 else if (objc_is_class_name (alias_ident))
3037 warning (0, "class %qE already exists", alias_ident);
3038 else
3039 {
3040 /* Implement @compatibility_alias as a typedef. */
3041 #ifdef OBJCPLUS
3042 push_lang_context (lang_name_c); /* extern "C" */
3043 #endif
3044 lang_hooks.decls.pushdecl (build_decl
3045 (input_location,
3046 TYPE_DECL,
3047 alias_ident,
3048 xref_tag (RECORD_TYPE, underlying_class)));
3049 #ifdef OBJCPLUS
3050 pop_lang_context ();
3051 #endif
3052 alias_chain = tree_cons (underlying_class, alias_ident, alias_chain);
3053 }
3054 }
3055
3056 void
3057 objc_declare_class (tree ident_list)
3058 {
3059 tree list;
3060 #ifdef OBJCPLUS
3061 if (current_namespace != global_namespace) {
3062 error ("Objective-C declarations may only appear in global scope");
3063 }
3064 #endif /* OBJCPLUS */
3065
3066 for (list = ident_list; list; list = TREE_CHAIN (list))
3067 {
3068 tree ident = TREE_VALUE (list);
3069
3070 if (! objc_is_class_name (ident))
3071 {
3072 tree record = lookup_name (ident), type = record;
3073
3074 if (record)
3075 {
3076 if (TREE_CODE (record) == TYPE_DECL)
3077 type = DECL_ORIGINAL_TYPE (record);
3078
3079 if (!TYPE_HAS_OBJC_INFO (type)
3080 || !TYPE_OBJC_INTERFACE (type))
3081 {
3082 error ("%qE redeclared as different kind of symbol",
3083 ident);
3084 error ("previous declaration of %q+D",
3085 record);
3086 }
3087 }
3088
3089 record = xref_tag (RECORD_TYPE, ident);
3090 INIT_TYPE_OBJC_INFO (record);
3091 TYPE_OBJC_INTERFACE (record) = ident;
3092 class_chain = tree_cons (NULL_TREE, ident, class_chain);
3093 }
3094 }
3095 }
3096
3097 tree
3098 objc_is_class_name (tree ident)
3099 {
3100 tree chain;
3101
3102 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE
3103 && identifier_global_value (ident))
3104 ident = identifier_global_value (ident);
3105 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3106 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3107
3108 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3109 ident = OBJC_TYPE_NAME (ident);
3110 #ifdef OBJCPLUS
3111 if (ident && TREE_CODE (ident) == TYPE_DECL)
3112 ident = DECL_NAME (ident);
3113 #endif
3114 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3115 return NULL_TREE;
3116
3117 if (lookup_interface (ident))
3118 return ident;
3119
3120 for (chain = class_chain; chain; chain = TREE_CHAIN (chain))
3121 {
3122 if (ident == TREE_VALUE (chain))
3123 return ident;
3124 }
3125
3126 for (chain = alias_chain; chain; chain = TREE_CHAIN (chain))
3127 {
3128 if (ident == TREE_VALUE (chain))
3129 return TREE_PURPOSE (chain);
3130 }
3131
3132 return 0;
3133 }
3134
3135 /* Check whether TYPE is either 'id' or 'Class'. */
3136
3137 tree
3138 objc_is_id (tree type)
3139 {
3140 if (type && TREE_CODE (type) == IDENTIFIER_NODE
3141 && identifier_global_value (type))
3142 type = identifier_global_value (type);
3143
3144 if (type && TREE_CODE (type) == TYPE_DECL)
3145 type = TREE_TYPE (type);
3146
3147 /* NB: This function may be called before the ObjC front-end has
3148 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3149 return (objc_object_type && type
3150 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3151 ? type
3152 : NULL_TREE);
3153 }
3154
3155 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3156 class instance. This is needed by other parts of the compiler to
3157 handle ObjC types gracefully. */
3158
3159 tree
3160 objc_is_object_ptr (tree type)
3161 {
3162 tree ret;
3163
3164 type = TYPE_MAIN_VARIANT (type);
3165 if (!POINTER_TYPE_P (type))
3166 return 0;
3167
3168 ret = objc_is_id (type);
3169 if (!ret)
3170 ret = objc_is_class_name (TREE_TYPE (type));
3171
3172 return ret;
3173 }
3174
3175 static int
3176 objc_is_gcable_type (tree type, int or_strong_p)
3177 {
3178 tree name;
3179
3180 if (!TYPE_P (type))
3181 return 0;
3182 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3183 return 1;
3184 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3185 return 1;
3186 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3187 return 0;
3188 type = TREE_TYPE (type);
3189 if (TREE_CODE (type) != RECORD_TYPE)
3190 return 0;
3191 name = TYPE_NAME (type);
3192 return (objc_is_class_name (name) != NULL_TREE);
3193 }
3194
3195 static tree
3196 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3197 {
3198 if (expr == oldexpr)
3199 return newexpr;
3200
3201 switch (TREE_CODE (expr))
3202 {
3203 case COMPONENT_REF:
3204 return objc_build_component_ref
3205 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3206 oldexpr,
3207 newexpr),
3208 DECL_NAME (TREE_OPERAND (expr, 1)));
3209 case ARRAY_REF:
3210 return build_array_ref (input_location,
3211 objc_substitute_decl (TREE_OPERAND (expr, 0),
3212 oldexpr,
3213 newexpr),
3214 TREE_OPERAND (expr, 1));
3215 case INDIRECT_REF:
3216 return build_indirect_ref (input_location,
3217 objc_substitute_decl (TREE_OPERAND (expr, 0),
3218 oldexpr,
3219 newexpr), RO_ARROW);
3220 default:
3221 return expr;
3222 }
3223 }
3224
3225 static tree
3226 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3227 {
3228 tree func_params;
3229 /* The LHS parameter contains the expression 'outervar->memberspec';
3230 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3231 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3232 */
3233 tree offs
3234 = objc_substitute_decl
3235 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3236 tree func
3237 = (flag_objc_direct_dispatch
3238 ? objc_assign_ivar_fast_decl
3239 : objc_assign_ivar_decl);
3240
3241 offs = convert (integer_type_node, build_unary_op (input_location,
3242 ADDR_EXPR, offs, 0));
3243 offs = fold (offs);
3244 func_params = tree_cons (NULL_TREE,
3245 convert (objc_object_type, rhs),
3246 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3247 tree_cons (NULL_TREE, offs,
3248 NULL_TREE)));
3249
3250 assemble_external (func);
3251 return build_function_call (input_location, func, func_params);
3252 }
3253
3254 static tree
3255 objc_build_global_assignment (tree lhs, tree rhs)
3256 {
3257 tree func_params = tree_cons (NULL_TREE,
3258 convert (objc_object_type, rhs),
3259 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3260 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3261 NULL_TREE));
3262
3263 assemble_external (objc_assign_global_decl);
3264 return build_function_call (input_location,
3265 objc_assign_global_decl, func_params);
3266 }
3267
3268 static tree
3269 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3270 {
3271 tree func_params = tree_cons (NULL_TREE,
3272 convert (objc_object_type, rhs),
3273 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3274 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3275 NULL_TREE));
3276
3277 assemble_external (objc_assign_strong_cast_decl);
3278 return build_function_call (input_location,
3279 objc_assign_strong_cast_decl, func_params);
3280 }
3281
3282 static int
3283 objc_is_gcable_p (tree expr)
3284 {
3285 return (TREE_CODE (expr) == COMPONENT_REF
3286 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3287 : TREE_CODE (expr) == ARRAY_REF
3288 ? (objc_is_gcable_p (TREE_TYPE (expr))
3289 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3290 : TREE_CODE (expr) == ARRAY_TYPE
3291 ? objc_is_gcable_p (TREE_TYPE (expr))
3292 : TYPE_P (expr)
3293 ? objc_is_gcable_type (expr, 1)
3294 : (objc_is_gcable_p (TREE_TYPE (expr))
3295 || (DECL_P (expr)
3296 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3297 }
3298
3299 static int
3300 objc_is_ivar_reference_p (tree expr)
3301 {
3302 return (TREE_CODE (expr) == ARRAY_REF
3303 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3304 : TREE_CODE (expr) == COMPONENT_REF
3305 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3306 : 0);
3307 }
3308
3309 static int
3310 objc_is_global_reference_p (tree expr)
3311 {
3312 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3313 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3314 : DECL_P (expr)
3315 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3316 : 0);
3317 }
3318
3319 tree
3320 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3321 {
3322 tree result = NULL_TREE, outer;
3323 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3324
3325 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3326 will have been transformed to the form '*(type *)&expr'. */
3327 if (TREE_CODE (lhs) == INDIRECT_REF)
3328 {
3329 outer = TREE_OPERAND (lhs, 0);
3330
3331 while (!strong_cast_p
3332 && (CONVERT_EXPR_P (outer)
3333 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3334 {
3335 tree lhstype = TREE_TYPE (outer);
3336
3337 /* Descend down the cast chain, and record the first objc_gc
3338 attribute found. */
3339 if (POINTER_TYPE_P (lhstype))
3340 {
3341 tree attr
3342 = lookup_attribute ("objc_gc",
3343 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3344
3345 if (attr)
3346 strong_cast_p = 1;
3347 }
3348
3349 outer = TREE_OPERAND (outer, 0);
3350 }
3351 }
3352
3353 /* If we have a __strong cast, it trumps all else. */
3354 if (strong_cast_p)
3355 {
3356 if (modifycode != NOP_EXPR)
3357 goto invalid_pointer_arithmetic;
3358
3359 if (warn_assign_intercept)
3360 warning (0, "strong-cast assignment has been intercepted");
3361
3362 result = objc_build_strong_cast_assignment (lhs, rhs);
3363
3364 goto exit_point;
3365 }
3366
3367 /* the lhs must be of a suitable type, regardless of its underlying
3368 structure. */
3369 if (!objc_is_gcable_p (lhs))
3370 goto exit_point;
3371
3372 outer = lhs;
3373
3374 while (outer
3375 && (TREE_CODE (outer) == COMPONENT_REF
3376 || TREE_CODE (outer) == ARRAY_REF))
3377 outer = TREE_OPERAND (outer, 0);
3378
3379 if (TREE_CODE (outer) == INDIRECT_REF)
3380 {
3381 outer = TREE_OPERAND (outer, 0);
3382 indirect_p = 1;
3383 }
3384
3385 outer_gc_p = objc_is_gcable_p (outer);
3386
3387 /* Handle ivar assignments. */
3388 if (objc_is_ivar_reference_p (lhs))
3389 {
3390 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3391 doesn't cut it here), the best we can do here is suggest a cast. */
3392 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3393 {
3394 /* We may still be able to use the global write barrier... */
3395 if (!indirect_p && objc_is_global_reference_p (outer))
3396 goto global_reference;
3397
3398 suggest_cast:
3399 if (modifycode == NOP_EXPR)
3400 {
3401 if (warn_assign_intercept)
3402 warning (0, "strong-cast may possibly be needed");
3403 }
3404
3405 goto exit_point;
3406 }
3407
3408 if (modifycode != NOP_EXPR)
3409 goto invalid_pointer_arithmetic;
3410
3411 if (warn_assign_intercept)
3412 warning (0, "instance variable assignment has been intercepted");
3413
3414 result = objc_build_ivar_assignment (outer, lhs, rhs);
3415
3416 goto exit_point;
3417 }
3418
3419 /* Likewise, intercept assignment to global/static variables if their type is
3420 GC-marked. */
3421 if (objc_is_global_reference_p (outer))
3422 {
3423 if (indirect_p)
3424 goto suggest_cast;
3425
3426 global_reference:
3427 if (modifycode != NOP_EXPR)
3428 {
3429 invalid_pointer_arithmetic:
3430 if (outer_gc_p)
3431 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3432
3433 goto exit_point;
3434 }
3435
3436 if (warn_assign_intercept)
3437 warning (0, "global/static variable assignment has been intercepted");
3438
3439 result = objc_build_global_assignment (lhs, rhs);
3440 }
3441
3442 /* In all other cases, fall back to the normal mechanism. */
3443 exit_point:
3444 return result;
3445 }
3446
3447 struct GTY(()) interface_tuple {
3448 tree id;
3449 tree class_name;
3450 };
3451
3452 static GTY ((param_is (struct interface_tuple))) htab_t interface_htab;
3453
3454 static hashval_t
3455 hash_interface (const void *p)
3456 {
3457 const struct interface_tuple *d = (const struct interface_tuple *) p;
3458 return IDENTIFIER_HASH_VALUE (d->id);
3459 }
3460
3461 static int
3462 eq_interface (const void *p1, const void *p2)
3463 {
3464 const struct interface_tuple *d = (const struct interface_tuple *) p1;
3465 return d->id == p2;
3466 }
3467
3468 static tree
3469 lookup_interface (tree ident)
3470 {
3471 #ifdef OBJCPLUS
3472 if (ident && TREE_CODE (ident) == TYPE_DECL)
3473 ident = DECL_NAME (ident);
3474 #endif
3475
3476 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3477 return NULL_TREE;
3478
3479 {
3480 struct interface_tuple **slot;
3481 tree i = NULL_TREE;
3482
3483 if (interface_htab)
3484 {
3485 slot = (struct interface_tuple **)
3486 htab_find_slot_with_hash (interface_htab, ident,
3487 IDENTIFIER_HASH_VALUE (ident),
3488 NO_INSERT);
3489 if (slot && *slot)
3490 i = (*slot)->class_name;
3491 }
3492 return i;
3493 }
3494 }
3495
3496 /* Implement @defs (<classname>) within struct bodies. */
3497
3498 tree
3499 objc_get_class_ivars (tree class_name)
3500 {
3501 tree interface = lookup_interface (class_name);
3502
3503 if (interface)
3504 return get_class_ivars (interface, true);
3505
3506 error ("cannot find interface declaration for %qE",
3507 class_name);
3508
3509 return error_mark_node;
3510 }
3511
3512 /* Called when checking the variables in a struct. If we are not
3513 doing the ivars list inside an @interface context, then returns
3514 fieldlist unchanged. Else, returns the list of class ivars.
3515 */
3516 tree
3517 objc_get_interface_ivars (tree fieldlist)
3518 {
3519 if (!objc_collecting_ivars || !objc_interface_context
3520 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE
3521 || CLASS_SUPER_NAME (objc_interface_context) == NULL_TREE)
3522 return fieldlist;
3523
3524 return get_class_ivars (objc_interface_context, true);
3525 }
3526
3527 /* Used by: build_private_template, continue_class,
3528 and for @defs constructs. */
3529
3530 static tree
3531 get_class_ivars (tree interface, bool inherited)
3532 {
3533 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
3534
3535 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
3536 by the current class (i.e., they do not include super-class ivars).
3537 However, the CLASS_IVARS list will be side-effected by a call to
3538 finish_struct(), which will fill in field offsets. */
3539 if (!CLASS_IVARS (interface))
3540 CLASS_IVARS (interface) = ivar_chain;
3541
3542 if (!inherited)
3543 return ivar_chain;
3544
3545 while (CLASS_SUPER_NAME (interface))
3546 {
3547 /* Prepend super-class ivars. */
3548 interface = lookup_interface (CLASS_SUPER_NAME (interface));
3549 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
3550 ivar_chain);
3551 }
3552
3553 return ivar_chain;
3554 }
3555
3556 static tree
3557 objc_create_temporary_var (tree type)
3558 {
3559 tree decl;
3560
3561 decl = build_decl (input_location,
3562 VAR_DECL, NULL_TREE, type);
3563 TREE_USED (decl) = 1;
3564 DECL_ARTIFICIAL (decl) = 1;
3565 DECL_IGNORED_P (decl) = 1;
3566 DECL_CONTEXT (decl) = current_function_decl;
3567
3568 return decl;
3569 }
3570 \f
3571 /* Exception handling constructs. We begin by having the parser do most
3572 of the work and passing us blocks. What we do next depends on whether
3573 we're doing "native" exception handling or legacy Darwin setjmp exceptions.
3574 We abstract all of this in a handful of appropriately named routines. */
3575
3576 /* Stack of open try blocks. */
3577
3578 struct objc_try_context
3579 {
3580 struct objc_try_context *outer;
3581
3582 /* Statements (or statement lists) as processed by the parser. */
3583 tree try_body;
3584 tree finally_body;
3585
3586 /* Some file position locations. */
3587 location_t try_locus;
3588 location_t end_try_locus;
3589 location_t end_catch_locus;
3590 location_t finally_locus;
3591 location_t end_finally_locus;
3592
3593 /* A STATEMENT_LIST of CATCH_EXPRs, appropriate for sticking into op1
3594 of a TRY_CATCH_EXPR. Even when doing Darwin setjmp. */
3595 tree catch_list;
3596
3597 /* The CATCH_EXPR of an open @catch clause. */
3598 tree current_catch;
3599
3600 /* The VAR_DECL holding the Darwin equivalent of __builtin_eh_pointer. */
3601 tree caught_decl;
3602 tree stack_decl;
3603 tree rethrow_decl;
3604 };
3605
3606 static struct objc_try_context *cur_try_context;
3607
3608 static GTY(()) tree objc_eh_personality_decl;
3609
3610 /* This hook, called via lang_eh_runtime_type, generates a runtime object
3611 that represents TYPE. For Objective-C, this is just the class name. */
3612 /* ??? Isn't there a class object or some such? Is it easy to get? */
3613
3614 #ifndef OBJCPLUS
3615 tree
3616 objc_eh_runtime_type (tree type)
3617 {
3618 return add_objc_string (OBJC_TYPE_NAME (TREE_TYPE (type)), class_names);
3619 }
3620
3621 tree
3622 objc_eh_personality (void)
3623 {
3624 if (!flag_objc_sjlj_exceptions
3625 && !objc_eh_personality_decl)
3626 objc_eh_personality_decl
3627 = build_personality_function (USING_SJLJ_EXCEPTIONS
3628 ? "__gnu_objc_personality_sj0"
3629 : "__gnu_objc_personality_v0");
3630
3631 return objc_eh_personality_decl;
3632 }
3633 #endif
3634
3635 /* Build __builtin_eh_pointer, or the moral equivalent. In the case
3636 of Darwin, we'll arrange for it to be initialized (and associated
3637 with a binding) later. */
3638
3639 static tree
3640 objc_build_exc_ptr (void)
3641 {
3642 if (flag_objc_sjlj_exceptions)
3643 {
3644 tree var = cur_try_context->caught_decl;
3645 if (!var)
3646 {
3647 var = objc_create_temporary_var (objc_object_type);
3648 cur_try_context->caught_decl = var;
3649 }
3650 return var;
3651 }
3652 else
3653 {
3654 tree t;
3655 t = built_in_decls[BUILT_IN_EH_POINTER];
3656 t = build_call_expr (t, 1, integer_zero_node);
3657 return fold_convert (objc_object_type, t);
3658 }
3659 }
3660
3661 /* Build "objc_exception_try_exit(&_stack)". */
3662
3663 static tree
3664 next_sjlj_build_try_exit (void)
3665 {
3666 tree t;
3667 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
3668 t = tree_cons (NULL, t, NULL);
3669 t = build_function_call (input_location,
3670 objc_exception_try_exit_decl, t);
3671 return t;
3672 }
3673
3674 /* Build
3675 objc_exception_try_enter (&_stack);
3676 if (_setjmp(&_stack.buf))
3677 ;
3678 else
3679 ;
3680 Return the COND_EXPR. Note that the THEN and ELSE fields are left
3681 empty, ready for the caller to fill them in. */
3682
3683 static tree
3684 next_sjlj_build_enter_and_setjmp (void)
3685 {
3686 tree t, enter, sj, cond;
3687
3688 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
3689 t = tree_cons (NULL, t, NULL);
3690 enter = build_function_call (input_location,
3691 objc_exception_try_enter_decl, t);
3692
3693 t = objc_build_component_ref (cur_try_context->stack_decl,
3694 get_identifier ("buf"));
3695 t = build_fold_addr_expr_loc (input_location, t);
3696 #ifdef OBJCPLUS
3697 /* Convert _setjmp argument to type that is expected. */
3698 if (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl)))
3699 t = convert (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (objc_setjmp_decl))), t);
3700 else
3701 t = convert (ptr_type_node, t);
3702 #else
3703 t = convert (ptr_type_node, t);
3704 #endif
3705 t = tree_cons (NULL, t, NULL);
3706 sj = build_function_call (input_location,
3707 objc_setjmp_decl, t);
3708
3709 cond = build2 (COMPOUND_EXPR, TREE_TYPE (sj), enter, sj);
3710 cond = c_common_truthvalue_conversion (input_location, cond);
3711
3712 return build3 (COND_EXPR, void_type_node, cond, NULL, NULL);
3713 }
3714
3715 /* Build:
3716
3717 DECL = objc_exception_extract(&_stack); */
3718
3719 static tree
3720 next_sjlj_build_exc_extract (tree decl)
3721 {
3722 tree t;
3723
3724 t = build_fold_addr_expr_loc (input_location, cur_try_context->stack_decl);
3725 t = tree_cons (NULL, t, NULL);
3726 t = build_function_call (input_location,
3727 objc_exception_extract_decl, t);
3728 t = convert (TREE_TYPE (decl), t);
3729 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
3730
3731 return t;
3732 }
3733
3734 /* Build
3735 if (objc_exception_match(obj_get_class(TYPE), _caught)
3736 BODY
3737 else if (...)
3738 ...
3739 else
3740 {
3741 _rethrow = _caught;
3742 objc_exception_try_exit(&_stack);
3743 }
3744 from the sequence of CATCH_EXPRs in the current try context. */
3745
3746 static tree
3747 next_sjlj_build_catch_list (void)
3748 {
3749 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
3750 tree catch_seq, t;
3751 tree *last = &catch_seq;
3752 bool saw_id = false;
3753
3754 for (; !tsi_end_p (i); tsi_next (&i))
3755 {
3756 tree stmt = tsi_stmt (i);
3757 tree type = CATCH_TYPES (stmt);
3758 tree body = CATCH_BODY (stmt);
3759
3760 if (type == NULL)
3761 {
3762 *last = body;
3763 saw_id = true;
3764 break;
3765 }
3766 else
3767 {
3768 tree args, cond;
3769
3770 if (type == error_mark_node)
3771 cond = error_mark_node;
3772 else
3773 {
3774 args = tree_cons (NULL, cur_try_context->caught_decl, NULL);
3775 t = objc_get_class_reference (OBJC_TYPE_NAME (TREE_TYPE (type)));
3776 args = tree_cons (NULL, t, args);
3777 t = build_function_call (input_location,
3778 objc_exception_match_decl, args);
3779 cond = c_common_truthvalue_conversion (input_location, t);
3780 }
3781 t = build3 (COND_EXPR, void_type_node, cond, body, NULL);
3782 SET_EXPR_LOCATION (t, EXPR_LOCATION (stmt));
3783
3784 *last = t;
3785 last = &COND_EXPR_ELSE (t);
3786 }
3787 }
3788
3789 if (!saw_id)
3790 {
3791 t = build2 (MODIFY_EXPR, void_type_node, cur_try_context->rethrow_decl,
3792 cur_try_context->caught_decl);
3793 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
3794 append_to_statement_list (t, last);
3795
3796 t = next_sjlj_build_try_exit ();
3797 SET_EXPR_LOCATION (t, cur_try_context->end_catch_locus);
3798 append_to_statement_list (t, last);
3799 }
3800
3801 return catch_seq;
3802 }
3803
3804 /* Build a complete @try-@catch-@finally block for legacy Darwin setjmp
3805 exception handling. We aim to build:
3806
3807 {
3808 struct _objc_exception_data _stack;
3809 id _rethrow = 0;
3810 try
3811 {
3812 objc_exception_try_enter (&_stack);
3813 if (_setjmp(&_stack.buf))
3814 {
3815 id _caught = objc_exception_extract(&_stack);
3816 objc_exception_try_enter (&_stack);
3817 if (_setjmp(&_stack.buf))
3818 _rethrow = objc_exception_extract(&_stack);
3819 else
3820 CATCH-LIST
3821 }
3822 else
3823 TRY-BLOCK
3824 }
3825 finally
3826 {
3827 if (!_rethrow)
3828 objc_exception_try_exit(&_stack);
3829 FINALLY-BLOCK
3830 if (_rethrow)
3831 objc_exception_throw(_rethrow);
3832 }
3833 }
3834
3835 If CATCH-LIST is empty, we can omit all of the block containing
3836 "_caught" except for the setting of _rethrow. Note the use of
3837 a real TRY_FINALLY_EXPR here, which is not involved in EH per-se,
3838 but handles goto and other exits from the block. */
3839
3840 static tree
3841 next_sjlj_build_try_catch_finally (void)
3842 {
3843 tree rethrow_decl, stack_decl, t;
3844 tree catch_seq, try_fin, bind;
3845
3846 /* Create the declarations involved. */
3847 t = xref_tag (RECORD_TYPE, get_identifier (UTAG_EXCDATA));
3848 stack_decl = objc_create_temporary_var (t);
3849 cur_try_context->stack_decl = stack_decl;
3850
3851 rethrow_decl = objc_create_temporary_var (objc_object_type);
3852 cur_try_context->rethrow_decl = rethrow_decl;
3853 TREE_CHAIN (rethrow_decl) = stack_decl;
3854
3855 /* Build the outermost variable binding level. */
3856 bind = build3 (BIND_EXPR, void_type_node, rethrow_decl, NULL, NULL);
3857 SET_EXPR_LOCATION (bind, cur_try_context->try_locus);
3858 TREE_SIDE_EFFECTS (bind) = 1;
3859
3860 /* Initialize rethrow_decl. */
3861 t = build2 (MODIFY_EXPR, void_type_node, rethrow_decl,
3862 convert (objc_object_type, null_pointer_node));
3863 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
3864 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
3865
3866 /* Build the outermost TRY_FINALLY_EXPR. */
3867 try_fin = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3868 SET_EXPR_LOCATION (try_fin, cur_try_context->try_locus);
3869 TREE_SIDE_EFFECTS (try_fin) = 1;
3870 append_to_statement_list (try_fin, &BIND_EXPR_BODY (bind));
3871
3872 /* Create the complete catch sequence. */
3873 if (cur_try_context->catch_list)
3874 {
3875 tree caught_decl = objc_build_exc_ptr ();
3876 catch_seq = build_stmt (input_location, BIND_EXPR, caught_decl, NULL, NULL);
3877 TREE_SIDE_EFFECTS (catch_seq) = 1;
3878
3879 t = next_sjlj_build_exc_extract (caught_decl);
3880 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
3881
3882 t = next_sjlj_build_enter_and_setjmp ();
3883 COND_EXPR_THEN (t) = next_sjlj_build_exc_extract (rethrow_decl);
3884 COND_EXPR_ELSE (t) = next_sjlj_build_catch_list ();
3885 append_to_statement_list (t, &BIND_EXPR_BODY (catch_seq));
3886 }
3887 else
3888 catch_seq = next_sjlj_build_exc_extract (rethrow_decl);
3889 SET_EXPR_LOCATION (catch_seq, cur_try_context->end_try_locus);
3890
3891 /* Build the main register-and-try if statement. */
3892 t = next_sjlj_build_enter_and_setjmp ();
3893 SET_EXPR_LOCATION (t, cur_try_context->try_locus);
3894 COND_EXPR_THEN (t) = catch_seq;
3895 COND_EXPR_ELSE (t) = cur_try_context->try_body;
3896 TREE_OPERAND (try_fin, 0) = t;
3897
3898 /* Build the complete FINALLY statement list. */
3899 t = next_sjlj_build_try_exit ();
3900 t = build_stmt (input_location, COND_EXPR,
3901 c_common_truthvalue_conversion
3902 (input_location, rethrow_decl),
3903 NULL, t);
3904 SET_EXPR_LOCATION (t, cur_try_context->finally_locus);
3905 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
3906
3907 append_to_statement_list (cur_try_context->finally_body,
3908 &TREE_OPERAND (try_fin, 1));
3909
3910 t = tree_cons (NULL, rethrow_decl, NULL);
3911 t = build_function_call (input_location,
3912 objc_exception_throw_decl, t);
3913 t = build_stmt (input_location, COND_EXPR,
3914 c_common_truthvalue_conversion (input_location,
3915 rethrow_decl),
3916 t, NULL);
3917 SET_EXPR_LOCATION (t, cur_try_context->end_finally_locus);
3918 append_to_statement_list (t, &TREE_OPERAND (try_fin, 1));
3919
3920 return bind;
3921 }
3922
3923 /* Called just after parsing the @try and its associated BODY. We now
3924 must prepare for the tricky bits -- handling the catches and finally. */
3925
3926 void
3927 objc_begin_try_stmt (location_t try_locus, tree body)
3928 {
3929 struct objc_try_context *c = XCNEW (struct objc_try_context);
3930 c->outer = cur_try_context;
3931 c->try_body = body;
3932 c->try_locus = try_locus;
3933 c->end_try_locus = input_location;
3934 cur_try_context = c;
3935
3936 /* -fobjc-exceptions is required to enable Objective-C exceptions.
3937 For example, on Darwin, ObjC exceptions require a sufficiently
3938 recent version of the runtime, so the user must ask for them
3939 explicitly. On other platforms, at the moment -fobjc-exceptions
3940 triggers -fexceptions which again is required for exceptions to
3941 work.
3942 */
3943 if (!flag_objc_exceptions)
3944 {
3945 error_at (try_locus, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
3946 }
3947
3948 if (flag_objc_sjlj_exceptions)
3949 objc_mark_locals_volatile (NULL);
3950 }
3951
3952 /* Called just after parsing "@catch (parm)". Open a binding level,
3953 enter DECL into the binding level, and initialize it. Leave the
3954 binding level open while the body of the compound statement is parsed. */
3955
3956 void
3957 objc_begin_catch_clause (tree decl)
3958 {
3959 tree compound, type, t;
3960
3961 /* Begin a new scope that the entire catch clause will live in. */
3962 compound = c_begin_compound_stmt (true);
3963
3964 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
3965 decl = build_decl (input_location,
3966 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
3967 lang_hooks.decls.pushdecl (decl);
3968
3969 /* Since a decl is required here by syntax, don't warn if its unused. */
3970 /* ??? As opposed to __attribute__((unused))? Anyway, this appears to
3971 be what the previous objc implementation did. */
3972 TREE_USED (decl) = 1;
3973 DECL_READ_P (decl) = 1;
3974
3975 /* Verify that the type of the catch is valid. It must be a pointer
3976 to an Objective-C class, or "id" (which is catch-all). */
3977 type = TREE_TYPE (decl);
3978
3979 if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
3980 type = NULL;
3981 else if (!POINTER_TYPE_P (type) || !TYPED_OBJECT (TREE_TYPE (type)))
3982 {
3983 error ("@catch parameter is not a known Objective-C class type");
3984 type = error_mark_node;
3985 }
3986 else if (cur_try_context->catch_list)
3987 {
3988 /* Examine previous @catch clauses and see if we've already
3989 caught the type in question. */
3990 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
3991 for (; !tsi_end_p (i); tsi_next (&i))
3992 {
3993 tree stmt = tsi_stmt (i);
3994 t = CATCH_TYPES (stmt);
3995 if (t == error_mark_node)
3996 continue;
3997 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
3998 {
3999 warning (0, "exception of type %<%T%> will be caught",
4000 TREE_TYPE (type));
4001 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4002 TREE_TYPE (t ? t : objc_object_type));
4003 break;
4004 }
4005 }
4006 }
4007
4008 /* Record the data for the catch in the try context so that we can
4009 finalize it later. */
4010 t = build_stmt (input_location, CATCH_EXPR, type, compound);
4011 cur_try_context->current_catch = t;
4012
4013 /* Initialize the decl from the EXC_PTR_EXPR we get from the runtime. */
4014 t = objc_build_exc_ptr ();
4015 t = convert (TREE_TYPE (decl), t);
4016 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4017 add_stmt (t);
4018 }
4019
4020 /* Called just after parsing the closing brace of a @catch clause. Close
4021 the open binding level, and record a CATCH_EXPR for it. */
4022
4023 void
4024 objc_finish_catch_clause (void)
4025 {
4026 tree c = cur_try_context->current_catch;
4027 cur_try_context->current_catch = NULL;
4028 cur_try_context->end_catch_locus = input_location;
4029
4030 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4031 append_to_statement_list (c, &cur_try_context->catch_list);
4032 }
4033
4034 /* Called after parsing a @finally clause and its associated BODY.
4035 Record the body for later placement. */
4036
4037 void
4038 objc_build_finally_clause (location_t finally_locus, tree body)
4039 {
4040 cur_try_context->finally_body = body;
4041 cur_try_context->finally_locus = finally_locus;
4042 cur_try_context->end_finally_locus = input_location;
4043 }
4044
4045 /* Called to finalize a @try construct. */
4046
4047 tree
4048 objc_finish_try_stmt (void)
4049 {
4050 struct objc_try_context *c = cur_try_context;
4051 tree stmt;
4052
4053 if (c->catch_list == NULL && c->finally_body == NULL)
4054 error ("%<@try%> without %<@catch%> or %<@finally%>");
4055
4056 /* If we're doing Darwin setjmp exceptions, build the big nasty. */
4057 if (flag_objc_sjlj_exceptions)
4058 {
4059 bool save = in_late_binary_op;
4060 in_late_binary_op = true;
4061 if (!cur_try_context->finally_body)
4062 {
4063 cur_try_context->finally_locus = input_location;
4064 cur_try_context->end_finally_locus = input_location;
4065 }
4066 stmt = next_sjlj_build_try_catch_finally ();
4067 in_late_binary_op = save;
4068 }
4069 else
4070 {
4071 /* Otherwise, nest the CATCH inside a FINALLY. */
4072 stmt = c->try_body;
4073 if (c->catch_list)
4074 {
4075 stmt = build_stmt (input_location, TRY_CATCH_EXPR, stmt, c->catch_list);
4076 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4077 }
4078 if (c->finally_body)
4079 {
4080 stmt = build_stmt (input_location, TRY_FINALLY_EXPR, stmt, c->finally_body);
4081 SET_EXPR_LOCATION (stmt, cur_try_context->try_locus);
4082 }
4083 }
4084 add_stmt (stmt);
4085
4086 cur_try_context = c->outer;
4087 free (c);
4088 return stmt;
4089 }
4090
4091 tree
4092 objc_build_throw_stmt (location_t loc, tree throw_expr)
4093 {
4094 tree args;
4095
4096 if (!flag_objc_exceptions)
4097 {
4098 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4099 }
4100
4101 if (throw_expr == NULL)
4102 {
4103 /* If we're not inside a @catch block, there is no "current
4104 exception" to be rethrown. */
4105 if (cur_try_context == NULL
4106 || cur_try_context->current_catch == NULL)
4107 {
4108 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4109 return NULL_TREE;
4110 }
4111
4112 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4113 value that we get from the runtime. */
4114 throw_expr = objc_build_exc_ptr ();
4115 }
4116
4117 /* A throw is just a call to the runtime throw function with the
4118 object as a parameter. */
4119 args = tree_cons (NULL, throw_expr, NULL);
4120 return add_stmt (build_function_call (loc,
4121 objc_exception_throw_decl, args));
4122 }
4123
4124 tree
4125 objc_build_synchronized (location_t start_locus, tree mutex, tree body)
4126 {
4127 tree args, call;
4128
4129 /* First lock the mutex. */
4130 mutex = save_expr (mutex);
4131 args = tree_cons (NULL, mutex, NULL);
4132 call = build_function_call (input_location,
4133 objc_sync_enter_decl, args);
4134 SET_EXPR_LOCATION (call, start_locus);
4135 add_stmt (call);
4136
4137 /* Build the mutex unlock. */
4138 args = tree_cons (NULL, mutex, NULL);
4139 call = build_function_call (input_location,
4140 objc_sync_exit_decl, args);
4141 SET_EXPR_LOCATION (call, input_location);
4142
4143 /* Put the that and the body in a TRY_FINALLY. */
4144 objc_begin_try_stmt (start_locus, body);
4145 objc_build_finally_clause (input_location, call);
4146 return objc_finish_try_stmt ();
4147 }
4148
4149 \f
4150 /* Predefine the following data type:
4151
4152 struct _objc_exception_data
4153 {
4154 int buf[OBJC_JBLEN];
4155 void *pointers[4];
4156 }; */
4157
4158 /* The following yuckiness should prevent users from having to #include
4159 <setjmp.h> in their code... */
4160
4161 /* Define to a harmless positive value so the below code doesn't die. */
4162 #ifndef OBJC_JBLEN
4163 #define OBJC_JBLEN 18
4164 #endif
4165
4166 static void
4167 build_next_objc_exception_stuff (void)
4168 {
4169 tree decls, temp_type, *chain = NULL;
4170
4171 objc_exception_data_template
4172 = objc_start_struct (get_identifier (UTAG_EXCDATA));
4173
4174 /* int buf[OBJC_JBLEN]; */
4175
4176 temp_type = build_sized_array_type (integer_type_node, OBJC_JBLEN);
4177 decls = add_field_decl (temp_type, "buf", &chain);
4178
4179 /* void *pointers[4]; */
4180
4181 temp_type = build_sized_array_type (ptr_type_node, 4);
4182 add_field_decl (temp_type, "pointers", &chain);
4183
4184 objc_finish_struct (objc_exception_data_template, decls);
4185
4186 /* int _setjmp(...); */
4187 /* If the user includes <setjmp.h>, this shall be superseded by
4188 'int _setjmp(jmp_buf);' */
4189 temp_type = build_varargs_function_type_list (integer_type_node, NULL_TREE);
4190 objc_setjmp_decl
4191 = add_builtin_function (TAG_SETJMP, temp_type, 0, NOT_BUILT_IN, NULL, NULL_TREE);
4192
4193 /* id objc_exception_extract(struct _objc_exception_data *); */
4194 temp_type
4195 = build_function_type_list (objc_object_type,
4196 build_pointer_type (objc_exception_data_template),
4197 NULL_TREE);
4198 objc_exception_extract_decl
4199 = add_builtin_function (TAG_EXCEPTIONEXTRACT, temp_type, 0, NOT_BUILT_IN, NULL,
4200 NULL_TREE);
4201 /* void objc_exception_try_enter(struct _objc_exception_data *); */
4202 /* void objc_exception_try_exit(struct _objc_exception_data *); */
4203 temp_type
4204 = build_function_type_list (void_type_node,
4205 build_pointer_type (objc_exception_data_template),
4206 NULL_TREE);
4207 objc_exception_try_enter_decl
4208 = add_builtin_function (TAG_EXCEPTIONTRYENTER, temp_type, 0, NOT_BUILT_IN, NULL,
4209 NULL_TREE);
4210 objc_exception_try_exit_decl
4211 = add_builtin_function (TAG_EXCEPTIONTRYEXIT, temp_type, 0, NOT_BUILT_IN, NULL,
4212 NULL_TREE);
4213
4214 /* int objc_exception_match(id, id); */
4215 temp_type
4216 = build_function_type_list (integer_type_node,
4217 objc_object_type, objc_object_type, NULL_TREE);
4218 objc_exception_match_decl
4219 = add_builtin_function (TAG_EXCEPTIONMATCH, temp_type, 0, NOT_BUILT_IN, NULL,
4220 NULL_TREE);
4221
4222 /* id objc_assign_ivar (id, id, unsigned int); */
4223 /* id objc_assign_ivar_Fast (id, id, unsigned int)
4224 __attribute__ ((hard_coded_address (OFFS_ASSIGNIVAR_FAST))); */
4225 temp_type
4226 = build_function_type_list (objc_object_type,
4227 objc_object_type,
4228 objc_object_type,
4229 unsigned_type_node,
4230 NULL_TREE);
4231 objc_assign_ivar_decl
4232 = add_builtin_function (TAG_ASSIGNIVAR, temp_type, 0, NOT_BUILT_IN,
4233 NULL, NULL_TREE);
4234 #ifdef OFFS_ASSIGNIVAR_FAST
4235 objc_assign_ivar_fast_decl
4236 = add_builtin_function (TAG_ASSIGNIVAR_FAST, temp_type, 0,
4237 NOT_BUILT_IN, NULL, NULL_TREE);
4238 DECL_ATTRIBUTES (objc_assign_ivar_fast_decl)
4239 = tree_cons (get_identifier ("hard_coded_address"),
4240 build_int_cst (NULL_TREE, OFFS_ASSIGNIVAR_FAST),
4241 NULL_TREE);
4242 #else
4243 /* Default to slower ivar method. */
4244 objc_assign_ivar_fast_decl = objc_assign_ivar_decl;
4245 #endif
4246
4247 /* id objc_assign_global (id, id *); */
4248 /* id objc_assign_strongCast (id, id *); */
4249 temp_type = build_function_type_list (objc_object_type,
4250 objc_object_type,
4251 build_pointer_type (objc_object_type),
4252 NULL_TREE);
4253 objc_assign_global_decl
4254 = add_builtin_function (TAG_ASSIGNGLOBAL, temp_type, 0, NOT_BUILT_IN, NULL,
4255 NULL_TREE);
4256 objc_assign_strong_cast_decl
4257 = add_builtin_function (TAG_ASSIGNSTRONGCAST, temp_type, 0, NOT_BUILT_IN, NULL,
4258 NULL_TREE);
4259 }
4260
4261 static void
4262 build_objc_exception_stuff (void)
4263 {
4264 tree noreturn_list, nothrow_list, temp_type;
4265
4266 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
4267 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
4268
4269 /* void objc_exception_throw(id) __attribute__((noreturn)); */
4270 /* void objc_sync_enter(id); */
4271 /* void objc_sync_exit(id); */
4272 temp_type = build_function_type_list (void_type_node,
4273 objc_object_type,
4274 NULL_TREE);
4275 objc_exception_throw_decl
4276 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
4277 noreturn_list);
4278 objc_sync_enter_decl
4279 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
4280 NULL, nothrow_list);
4281 objc_sync_exit_decl
4282 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
4283 NULL, nothrow_list);
4284 }
4285
4286 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4287 name as the class:
4288
4289 struct <classname> {
4290 struct _objc_class *isa;
4291 ...
4292 }; */
4293
4294 static void
4295 build_private_template (tree klass)
4296 {
4297 if (!CLASS_STATIC_TEMPLATE (klass))
4298 {
4299 tree record = objc_build_struct (klass,
4300 get_class_ivars (klass, false),
4301 CLASS_SUPER_NAME (klass));
4302
4303 /* Set the TREE_USED bit for this struct, so that stab generator
4304 can emit stabs for this struct type. */
4305 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4306 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4307 }
4308 }
4309 \f
4310 /* Begin code generation for protocols... */
4311
4312 /* struct _objc_protocol {
4313 struct _objc_class *isa;
4314 char *protocol_name;
4315 struct _objc_protocol **protocol_list;
4316 struct _objc__method_prototype_list *instance_methods;
4317 struct _objc__method_prototype_list *class_methods;
4318 }; */
4319
4320 static void
4321 build_protocol_template (void)
4322 {
4323 tree ptype, decls, *chain = NULL;
4324
4325 objc_protocol_template = objc_start_struct (get_identifier (UTAG_PROTOCOL));
4326
4327 /* struct _objc_class *isa; */
4328 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
4329 get_identifier (UTAG_CLASS)));
4330 decls = add_field_decl (ptype, "isa", &chain);
4331
4332 /* char *protocol_name; */
4333 add_field_decl (string_type_node, "protocol_name", &chain);
4334
4335 /* struct _objc_protocol **protocol_list; */
4336 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
4337 add_field_decl (ptype, "protocol_list", &chain);
4338
4339 /* struct _objc__method_prototype_list *instance_methods; */
4340 add_field_decl (objc_method_proto_list_ptr, "instance_methods", &chain);
4341
4342 /* struct _objc__method_prototype_list *class_methods; */
4343 add_field_decl (objc_method_proto_list_ptr, "class_methods", &chain);
4344
4345 objc_finish_struct (objc_protocol_template, decls);
4346 }
4347
4348 static tree
4349 build_descriptor_table_initializer (tree type, tree entries)
4350 {
4351 VEC(constructor_elt,gc) *inits = NULL;
4352
4353 do
4354 {
4355 VEC(constructor_elt,gc) *elts = NULL;
4356
4357 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
4358 build_selector (METHOD_SEL_NAME (entries)));
4359 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
4360 add_objc_string (METHOD_ENCODING (entries),
4361 meth_var_types));
4362
4363 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
4364 objc_build_constructor (type, elts));
4365
4366 entries = DECL_CHAIN (entries);
4367 }
4368 while (entries);
4369
4370 return objc_build_constructor (build_array_type (type, 0), inits);
4371 }
4372
4373 /* struct objc_method_prototype_list {
4374 int count;
4375 struct objc_method_prototype {
4376 SEL name;
4377 char *types;
4378 } list[1];
4379 }; */
4380
4381 static tree
4382 build_method_prototype_list_template (tree list_type, int size)
4383 {
4384 tree objc_ivar_list_record;
4385 tree array_type, decls, *chain = NULL;
4386
4387 /* Generate an unnamed struct definition. */
4388
4389 objc_ivar_list_record = objc_start_struct (NULL_TREE);
4390
4391 /* int method_count; */
4392 decls = add_field_decl (integer_type_node, "method_count", &chain);
4393
4394 /* struct objc_method method_list[]; */
4395 array_type = build_sized_array_type (list_type, size);
4396 add_field_decl (array_type, "method_list", &chain);
4397
4398 objc_finish_struct (objc_ivar_list_record, decls);
4399
4400 return objc_ivar_list_record;
4401 }
4402
4403 static tree
4404 build_method_prototype_template (void)
4405 {
4406 tree proto_record;
4407 tree decls, *chain = NULL;
4408
4409 proto_record = objc_start_struct (get_identifier (UTAG_METHOD_PROTOTYPE));
4410
4411 /* SEL _cmd; */
4412 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
4413
4414 /* char *method_types; */
4415 add_field_decl (string_type_node, "method_types", &chain);
4416
4417 objc_finish_struct (proto_record, decls);
4418
4419 return proto_record;
4420 }
4421
4422 static tree
4423 objc_method_parm_type (tree type)
4424 {
4425 type = TREE_VALUE (TREE_TYPE (type));
4426 if (TREE_CODE (type) == TYPE_DECL)
4427 type = TREE_TYPE (type);
4428 return type;
4429 }
4430
4431 static int
4432 objc_encoded_type_size (tree type)
4433 {
4434 int sz = int_size_in_bytes (type);
4435
4436 /* Make all integer and enum types at least as large
4437 as an int. */
4438 if (sz > 0 && INTEGRAL_TYPE_P (type))
4439 sz = MAX (sz, int_size_in_bytes (integer_type_node));
4440 /* Treat arrays as pointers, since that's how they're
4441 passed in. */
4442 else if (TREE_CODE (type) == ARRAY_TYPE)
4443 sz = int_size_in_bytes (ptr_type_node);
4444 return sz;
4445 }
4446
4447 /* Encode a method prototype.
4448
4449 The format is described in gcc/doc/objc.texi, section 'Method
4450 signatures'.
4451 */
4452 static tree
4453 encode_method_prototype (tree method_decl)
4454 {
4455 tree parms;
4456 int parm_offset, i;
4457 char buf[40];
4458 tree result;
4459
4460 /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
4461 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
4462
4463 /* Encode return type. */
4464 encode_type (objc_method_parm_type (method_decl),
4465 obstack_object_size (&util_obstack),
4466 OBJC_ENCODE_INLINE_DEFS);
4467
4468 /* Stack size. */
4469 /* The first two arguments (self and _cmd) are pointers; account for
4470 their size. */
4471 i = int_size_in_bytes (ptr_type_node);
4472 parm_offset = 2 * i;
4473 for (parms = METHOD_SEL_ARGS (method_decl); parms;
4474 parms = DECL_CHAIN (parms))
4475 {
4476 tree type = objc_method_parm_type (parms);
4477 int sz = objc_encoded_type_size (type);
4478
4479 /* If a type size is not known, bail out. */
4480 if (sz < 0)
4481 {
4482 error ("type %q+D does not have a known size",
4483 type);
4484 /* Pretend that the encoding succeeded; the compilation will
4485 fail nevertheless. */
4486 goto finish_encoding;
4487 }
4488 parm_offset += sz;
4489 }
4490
4491 sprintf (buf, "%d@0:%d", parm_offset, i);
4492 obstack_grow (&util_obstack, buf, strlen (buf));
4493
4494 /* Argument types. */
4495 parm_offset = 2 * i;
4496 for (parms = METHOD_SEL_ARGS (method_decl); parms;
4497 parms = DECL_CHAIN (parms))
4498 {
4499 tree type = objc_method_parm_type (parms);
4500
4501 /* Process argument qualifiers for user supplied arguments. */
4502 encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (parms)));
4503
4504 /* Type. */
4505 encode_type (type, obstack_object_size (&util_obstack),
4506 OBJC_ENCODE_INLINE_DEFS);
4507
4508 /* Compute offset. */
4509 sprintf (buf, "%d", parm_offset);
4510 parm_offset += objc_encoded_type_size (type);
4511
4512 obstack_grow (&util_obstack, buf, strlen (buf));
4513 }
4514
4515 finish_encoding:
4516 obstack_1grow (&util_obstack, '\0');
4517 result = get_identifier (XOBFINISH (&util_obstack, char *));
4518 obstack_free (&util_obstack, util_firstobj);
4519 return result;
4520 }
4521
4522 static tree
4523 generate_descriptor_table (tree type, const char *name, int size, tree list,
4524 tree proto)
4525 {
4526 tree decl;
4527 VEC(constructor_elt,gc) *v = NULL;
4528
4529 decl = start_var_decl (type, synth_id_with_class_suffix (name, proto));
4530
4531 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, size));
4532 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
4533
4534 finish_var_decl (decl, objc_build_constructor (type, v));
4535
4536 return decl;
4537 }
4538
4539 static void
4540 generate_method_descriptors (tree protocol)
4541 {
4542 tree initlist, chain, method_list_template;
4543 int size;
4544
4545 if (!objc_method_prototype_template)
4546 objc_method_prototype_template = build_method_prototype_template ();
4547
4548 chain = PROTOCOL_CLS_METHODS (protocol);
4549 if (chain)
4550 {
4551 size = list_length (chain);
4552
4553 method_list_template
4554 = build_method_prototype_list_template (objc_method_prototype_template,
4555 size);
4556
4557 initlist
4558 = build_descriptor_table_initializer (objc_method_prototype_template,
4559 chain);
4560
4561 UOBJC_CLASS_METHODS_decl
4562 = generate_descriptor_table (method_list_template,
4563 "_OBJC_PROTOCOL_CLASS_METHODS",
4564 size, initlist, protocol);
4565 }
4566 else
4567 UOBJC_CLASS_METHODS_decl = 0;
4568
4569 chain = PROTOCOL_NST_METHODS (protocol);
4570 if (chain)
4571 {
4572 size = list_length (chain);
4573
4574 method_list_template
4575 = build_method_prototype_list_template (objc_method_prototype_template,
4576 size);
4577 initlist
4578 = build_descriptor_table_initializer (objc_method_prototype_template,
4579 chain);
4580
4581 UOBJC_INSTANCE_METHODS_decl
4582 = generate_descriptor_table (method_list_template,
4583 "_OBJC_PROTOCOL_INSTANCE_METHODS",
4584 size, initlist, protocol);
4585 }
4586 else
4587 UOBJC_INSTANCE_METHODS_decl = 0;
4588 }
4589
4590 static void
4591 generate_protocol_references (tree plist)
4592 {
4593 tree lproto;
4594
4595 /* Forward declare protocols referenced. */
4596 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
4597 {
4598 tree proto = TREE_VALUE (lproto);
4599
4600 if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
4601 && PROTOCOL_NAME (proto))
4602 {
4603 if (! PROTOCOL_FORWARD_DECL (proto))
4604 build_protocol_reference (proto);
4605
4606 if (PROTOCOL_LIST (proto))
4607 generate_protocol_references (PROTOCOL_LIST (proto));
4608 }
4609 }
4610 }
4611
4612 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4613 current class. */
4614 #ifdef OBJCPLUS
4615 static void
4616 objc_generate_cxx_ctor_or_dtor (bool dtor)
4617 {
4618 tree fn, body, compound_stmt, ivar;
4619
4620 /* - (id) .cxx_construct { ... return self; } */
4621 /* - (void) .cxx_construct { ... } */
4622
4623 objc_set_method_type (MINUS_EXPR);
4624 objc_start_method_definition
4625 (objc_build_method_signature (build_tree_list (NULL_TREE,
4626 dtor
4627 ? void_type_node
4628 : objc_object_type),
4629 get_identifier (dtor
4630 ? TAG_CXX_DESTRUCT
4631 : TAG_CXX_CONSTRUCT),
4632 make_node (TREE_LIST),
4633 false));
4634 body = begin_function_body ();
4635 compound_stmt = begin_compound_stmt (0);
4636
4637 ivar = CLASS_IVARS (implementation_template);
4638 /* Destroy ivars in reverse order. */
4639 if (dtor)
4640 ivar = nreverse (copy_list (ivar));
4641
4642 for (; ivar; ivar = TREE_CHAIN (ivar))
4643 {
4644 if (TREE_CODE (ivar) == FIELD_DECL)
4645 {
4646 tree type = TREE_TYPE (ivar);
4647
4648 /* Call the ivar's default constructor or destructor. Do not
4649 call the destructor unless a corresponding constructor call
4650 has also been made (or is not needed). */
4651 if (MAYBE_CLASS_TYPE_P (type)
4652 && (dtor
4653 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4654 && (!TYPE_NEEDS_CONSTRUCTING (type)
4655 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4656 : (TYPE_NEEDS_CONSTRUCTING (type)
4657 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4658 finish_expr_stmt
4659 (build_special_member_call
4660 (build_ivar_reference (DECL_NAME (ivar)),
4661 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4662 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
4663 }
4664 }
4665
4666 /* The constructor returns 'self'. */
4667 if (!dtor)
4668 finish_return_stmt (self_decl);
4669
4670 finish_compound_stmt (compound_stmt);
4671 finish_function_body (body);
4672 fn = current_function_decl;
4673 finish_function ();
4674 objc_finish_method_definition (fn);
4675 }
4676
4677 /* The following routine will examine the current @interface for any
4678 non-POD C++ ivars requiring non-trivial construction and/or
4679 destruction, and then synthesize special '- .cxx_construct' and/or
4680 '- .cxx_destruct' methods which will run the appropriate
4681 construction or destruction code. Note that ivars inherited from
4682 super-classes are _not_ considered. */
4683 static void
4684 objc_generate_cxx_cdtors (void)
4685 {
4686 bool need_ctor = false, need_dtor = false;
4687 tree ivar;
4688
4689 /* We do not want to do this for categories, since they do not have
4690 their own ivars. */
4691
4692 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4693 return;
4694
4695 /* First, determine if we even need a constructor and/or destructor. */
4696
4697 for (ivar = CLASS_IVARS (implementation_template); ivar;
4698 ivar = TREE_CHAIN (ivar))
4699 {
4700 if (TREE_CODE (ivar) == FIELD_DECL)
4701 {
4702 tree type = TREE_TYPE (ivar);
4703
4704 if (MAYBE_CLASS_TYPE_P (type))
4705 {
4706 if (TYPE_NEEDS_CONSTRUCTING (type)
4707 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4708 /* NB: If a default constructor is not available, we will not
4709 be able to initialize this ivar; the add_instance_variable()
4710 routine will already have warned about this. */
4711 need_ctor = true;
4712
4713 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4714 && (!TYPE_NEEDS_CONSTRUCTING (type)
4715 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4716 /* NB: If a default constructor is not available, we will not
4717 call the destructor either, for symmetry. */
4718 need_dtor = true;
4719 }
4720 }
4721 }
4722
4723 /* Generate '- .cxx_construct' if needed. */
4724
4725 if (need_ctor)
4726 objc_generate_cxx_ctor_or_dtor (false);
4727
4728 /* Generate '- .cxx_destruct' if needed. */
4729
4730 if (need_dtor)
4731 objc_generate_cxx_ctor_or_dtor (true);
4732
4733 /* The 'imp_list' variable points at an imp_entry record for the current
4734 @implementation. Record the existence of '- .cxx_construct' and/or
4735 '- .cxx_destruct' methods therein; it will be included in the
4736 metadata for the class. */
4737 if (flag_next_runtime)
4738 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
4739 }
4740 #endif
4741
4742 /* For each protocol which was referenced either from a @protocol()
4743 expression, or because a class/category implements it (then a
4744 pointer to the protocol is stored in the struct describing the
4745 class/category), we create a statically allocated instance of the
4746 Protocol class. The code is written in such a way as to generate
4747 as few Protocol objects as possible; we generate a unique Protocol
4748 instance for each protocol, and we don't generate a Protocol
4749 instance if the protocol is never referenced (either from a
4750 @protocol() or from a class/category implementation). These
4751 statically allocated objects can be referred to via the static
4752 (that is, private to this module) symbols _OBJC_PROTOCOL_n.
4753
4754 The statically allocated Protocol objects that we generate here
4755 need to be fixed up at runtime in order to be used: the 'isa'
4756 pointer of the objects need to be set up to point to the 'Protocol'
4757 class, as known at runtime.
4758
4759 The NeXT runtime fixes up all protocols at program startup time,
4760 before main() is entered. It uses a low-level trick to look up all
4761 those symbols, then loops on them and fixes them up.
4762
4763 The GNU runtime as well fixes up all protocols before user code
4764 from the module is executed; it requires pointers to those symbols
4765 to be put in the objc_symtab (which is then passed as argument to
4766 the function __objc_exec_class() which the compiler sets up to be
4767 executed automatically when the module is loaded); setup of those
4768 Protocol objects happen in two ways in the GNU runtime: all
4769 Protocol objects referred to by a class or category implementation
4770 are fixed up when the class/category is loaded; all Protocol
4771 objects referred to by a @protocol() expression are added by the
4772 compiler to the list of statically allocated instances to fixup
4773 (the same list holding the statically allocated constant string
4774 objects). Because, as explained above, the compiler generates as
4775 few Protocol objects as possible, some Protocol object might end up
4776 being referenced multiple times when compiled with the GNU runtime,
4777 and end up being fixed up multiple times at runtime initialization.
4778 But that doesn't hurt, it's just a little inefficient. */
4779
4780 static void
4781 generate_protocols (void)
4782 {
4783 tree p, encoding;
4784 tree decl;
4785 tree initlist, protocol_name_expr, refs_decl, refs_expr;
4786
4787 /* If a protocol was directly referenced, pull in indirect references. */
4788 for (p = protocol_chain; p; p = TREE_CHAIN (p))
4789 if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
4790 generate_protocol_references (PROTOCOL_LIST (p));
4791
4792 for (p = protocol_chain; p; p = TREE_CHAIN (p))
4793 {
4794 tree nst_methods = PROTOCOL_NST_METHODS (p);
4795 tree cls_methods = PROTOCOL_CLS_METHODS (p);
4796
4797 /* If protocol wasn't referenced, don't generate any code. */
4798 decl = PROTOCOL_FORWARD_DECL (p);
4799
4800 if (!decl)
4801 continue;
4802
4803 /* Make sure we link in the Protocol class. */
4804 add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
4805
4806 while (nst_methods)
4807 {
4808 if (! METHOD_ENCODING (nst_methods))
4809 {
4810 encoding = encode_method_prototype (nst_methods);
4811 METHOD_ENCODING (nst_methods) = encoding;
4812 }
4813 nst_methods = DECL_CHAIN (nst_methods);
4814 }
4815
4816 while (cls_methods)
4817 {
4818 if (! METHOD_ENCODING (cls_methods))
4819 {
4820 encoding = encode_method_prototype (cls_methods);
4821 METHOD_ENCODING (cls_methods) = encoding;
4822 }
4823
4824 cls_methods = DECL_CHAIN (cls_methods);
4825 }
4826 generate_method_descriptors (p);
4827
4828 if (PROTOCOL_LIST (p))
4829 refs_decl = generate_protocol_list (p);
4830 else
4831 refs_decl = 0;
4832
4833 /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
4834 protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
4835
4836 if (refs_decl)
4837 refs_expr = convert (build_pointer_type (build_pointer_type
4838 (objc_protocol_template)),
4839 build_unary_op (input_location,
4840 ADDR_EXPR, refs_decl, 0));
4841 else
4842 refs_expr = build_int_cst (NULL_TREE, 0);
4843
4844 /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
4845 by generate_method_descriptors, which is called above. */
4846 initlist = build_protocol_initializer (TREE_TYPE (decl),
4847 protocol_name_expr, refs_expr,
4848 UOBJC_INSTANCE_METHODS_decl,
4849 UOBJC_CLASS_METHODS_decl);
4850 finish_var_decl (decl, initlist);
4851 }
4852 }
4853
4854 static tree
4855 build_protocol_initializer (tree type, tree protocol_name,
4856 tree protocol_list, tree instance_methods,
4857 tree class_methods)
4858 {
4859 tree expr;
4860 tree cast_type = build_pointer_type
4861 (xref_tag (RECORD_TYPE,
4862 get_identifier (UTAG_CLASS)));
4863 VEC(constructor_elt,gc) *inits = NULL;
4864
4865 /* Filling the "isa" in with one allows the runtime system to
4866 detect that the version change...should remove before final release. */
4867
4868 expr = build_int_cst (cast_type, PROTOCOL_VERSION);
4869 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
4870 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_name);
4871 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, protocol_list);
4872
4873 if (!instance_methods)
4874 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
4875 else
4876 {
4877 expr = convert (objc_method_proto_list_ptr,
4878 build_unary_op (input_location,
4879 ADDR_EXPR, instance_methods, 0));
4880 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
4881 }
4882
4883 if (!class_methods)
4884 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, 0));
4885 else
4886 {
4887 expr = convert (objc_method_proto_list_ptr,
4888 build_unary_op (input_location,
4889 ADDR_EXPR, class_methods, 0));
4890 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, expr);
4891 }
4892
4893 return objc_build_constructor (type, inits);
4894 }
4895 \f
4896 /* struct _objc_category {
4897 char *category_name;
4898 char *class_name;
4899 struct _objc_method_list *instance_methods;
4900 struct _objc_method_list *class_methods;
4901 struct _objc_protocol_list *protocols;
4902 }; */
4903
4904 static void
4905 build_category_template (void)
4906 {
4907 tree ptype, decls, *chain = NULL;
4908
4909 objc_category_template = objc_start_struct (get_identifier (UTAG_CATEGORY));
4910
4911 /* char *category_name; */
4912 decls = add_field_decl (string_type_node, "category_name", &chain);
4913
4914 /* char *class_name; */
4915 add_field_decl (string_type_node, "class_name", &chain);
4916
4917 /* struct _objc_method_list *instance_methods; */
4918 add_field_decl (objc_method_list_ptr, "instance_methods", &chain);
4919
4920 /* struct _objc_method_list *class_methods; */
4921 add_field_decl (objc_method_list_ptr, "class_methods", &chain);
4922
4923 /* struct _objc_protocol **protocol_list; */
4924 ptype = build_pointer_type (build_pointer_type (objc_protocol_template));
4925 add_field_decl (ptype, "protocol_list", &chain);
4926
4927 objc_finish_struct (objc_category_template, decls);
4928 }
4929
4930 /* struct _objc_selector {
4931 SEL sel_id;
4932 char *sel_type;
4933 }; */
4934
4935 static void
4936 build_selector_template (void)
4937 {
4938 tree decls, *chain = NULL;
4939
4940 objc_selector_template = objc_start_struct (get_identifier (UTAG_SELECTOR));
4941
4942 /* SEL sel_id; */
4943 decls = add_field_decl (objc_selector_type, "sel_id", &chain);
4944
4945 /* char *sel_type; */
4946 add_field_decl (string_type_node, "sel_type", &chain);
4947
4948 objc_finish_struct (objc_selector_template, decls);
4949 }
4950
4951 /* struct _objc_class {
4952 struct _objc_class *isa;
4953 struct _objc_class *super_class;
4954 char *name;
4955 long version;
4956 long info;
4957 long instance_size;
4958 struct _objc_ivar_list *ivars;
4959 struct _objc_method_list *methods;
4960 #ifdef __NEXT_RUNTIME__
4961 struct objc_cache *cache;
4962 #else
4963 struct sarray *dtable;
4964 struct _objc_class *subclass_list;
4965 struct _objc_class *sibling_class;
4966 #endif
4967 struct _objc_protocol_list *protocols;
4968 #ifdef __NEXT_RUNTIME__
4969 void *sel_id;
4970 #endif
4971 void *gc_object_type;
4972 }; */
4973
4974 /* NB: The 'sel_id' and 'gc_object_type' fields are not being used by
4975 the NeXT/Apple runtime; still, the compiler must generate them to
4976 maintain backward binary compatibility (and to allow for future
4977 expansion). */
4978
4979 static void
4980 build_class_template (void)
4981 {
4982 tree ptype, decls, *chain = NULL;
4983
4984 objc_class_template = objc_start_struct (get_identifier (UTAG_CLASS));
4985
4986 /* struct _objc_class *isa; */
4987 decls = add_field_decl (build_pointer_type (objc_class_template),
4988 "isa", &chain);
4989
4990 /* struct _objc_class *super_class; */
4991 add_field_decl (build_pointer_type (objc_class_template),
4992 "super_class", &chain);
4993
4994 /* char *name; */
4995 add_field_decl (string_type_node, "name", &chain);
4996
4997 /* long version; */
4998 add_field_decl (long_integer_type_node, "version", &chain);
4999
5000 /* long info; */
5001 add_field_decl (long_integer_type_node, "info", &chain);
5002
5003 /* long instance_size; */
5004 add_field_decl (long_integer_type_node, "instance_size", &chain);
5005
5006 /* struct _objc_ivar_list *ivars; */
5007 add_field_decl (objc_ivar_list_ptr,"ivars", &chain);
5008
5009 /* struct _objc_method_list *methods; */
5010 add_field_decl (objc_method_list_ptr, "methods", &chain);
5011
5012 if (flag_next_runtime)
5013 {
5014 /* struct objc_cache *cache; */
5015 ptype = build_pointer_type (xref_tag (RECORD_TYPE,
5016 get_identifier ("objc_cache")));
5017 add_field_decl (ptype, "cache", &chain);
5018 }
5019 else
5020 {
5021 /* struct sarray *dtable; */
5022 ptype = build_pointer_type(xref_tag (RECORD_TYPE,
5023 get_identifier ("sarray")));
5024 add_field_decl (ptype, "dtable", &chain);
5025
5026 /* struct objc_class *subclass_list; */
5027 ptype = build_pointer_type (objc_class_template);
5028 add_field_decl (ptype, "subclass_list", &chain);
5029
5030 /* struct objc_class *sibling_class; */
5031 ptype = build_pointer_type (objc_class_template);
5032 add_field_decl (ptype, "sibling_class", &chain);
5033 }
5034
5035 /* struct _objc_protocol **protocol_list; */
5036 ptype = build_pointer_type (build_pointer_type
5037 (xref_tag (RECORD_TYPE,
5038 get_identifier (UTAG_PROTOCOL))));
5039 add_field_decl (ptype, "protocol_list", &chain);
5040
5041 if (flag_next_runtime)
5042 {
5043 /* void *sel_id; */
5044 add_field_decl (build_pointer_type (void_type_node), "sel_id", &chain);
5045 }
5046
5047 /* void *gc_object_type; */
5048 add_field_decl (build_pointer_type (void_type_node),
5049 "gc_object_type", &chain);
5050
5051 objc_finish_struct (objc_class_template, decls);
5052 }
5053
5054 /* Generate appropriate forward declarations for an implementation. */
5055
5056 static void
5057 synth_forward_declarations (void)
5058 {
5059 tree an_id;
5060
5061 /* static struct objc_class _OBJC_CLASS_<my_name>; */
5062 UOBJC_CLASS_decl = build_metadata_decl ("_OBJC_CLASS",
5063 objc_class_template);
5064
5065 /* static struct objc_class _OBJC_METACLASS_<my_name>; */
5066 UOBJC_METACLASS_decl = build_metadata_decl ("_OBJC_METACLASS",
5067 objc_class_template);
5068
5069 /* Pre-build the following entities - for speed/convenience. */
5070
5071 an_id = get_identifier ("super_class");
5072 ucls_super_ref = objc_build_component_ref (UOBJC_CLASS_decl, an_id);
5073 uucls_super_ref = objc_build_component_ref (UOBJC_METACLASS_decl, an_id);
5074 }
5075
5076 static void
5077 error_with_ivar (const char *message, tree decl)
5078 {
5079 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
5080 message, identifier_to_locale (gen_declaration (decl)));
5081
5082 }
5083
5084 static void
5085 check_ivars (tree inter, tree imp)
5086 {
5087 tree intdecls = CLASS_RAW_IVARS (inter);
5088 tree impdecls = CLASS_RAW_IVARS (imp);
5089
5090 while (1)
5091 {
5092 tree t1, t2;
5093
5094 #ifdef OBJCPLUS
5095 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
5096 intdecls = TREE_CHAIN (intdecls);
5097 #endif
5098 if (intdecls == 0 && impdecls == 0)
5099 break;
5100 if (intdecls == 0 || impdecls == 0)
5101 {
5102 error ("inconsistent instance variable specification");
5103 break;
5104 }
5105
5106 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
5107
5108 if (!comptypes (t1, t2)
5109 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
5110 DECL_INITIAL (impdecls)))
5111 {
5112 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
5113 {
5114 error_with_ivar ("conflicting instance variable type",
5115 impdecls);
5116 error_with_ivar ("previous declaration of",
5117 intdecls);
5118 }
5119 else /* both the type and the name don't match */
5120 {
5121 error ("inconsistent instance variable specification");
5122 break;
5123 }
5124 }
5125
5126 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
5127 {
5128 error_with_ivar ("conflicting instance variable name",
5129 impdecls);
5130 error_with_ivar ("previous declaration of",
5131 intdecls);
5132 }
5133
5134 intdecls = DECL_CHAIN (intdecls);
5135 impdecls = DECL_CHAIN (impdecls);
5136 }
5137 }
5138
5139 /* Set 'objc_super_template' to the data type node for 'struct _objc_super'.
5140 This needs to be done just once per compilation. */
5141
5142 /* struct _objc_super {
5143 struct _objc_object *self;
5144 struct _objc_class *super_class;
5145 }; */
5146
5147 static void
5148 build_super_template (void)
5149 {
5150 tree decls, *chain = NULL;
5151
5152 objc_super_template = objc_start_struct (get_identifier (UTAG_SUPER));
5153
5154 /* struct _objc_object *self; */
5155 decls = add_field_decl (objc_object_type, "self", &chain);
5156
5157 /* struct _objc_class *super_class; */
5158 add_field_decl (build_pointer_type (objc_class_template),
5159 "super_class", &chain);
5160
5161 objc_finish_struct (objc_super_template, decls);
5162 }
5163
5164 /* struct _objc_ivar {
5165 char *ivar_name;
5166 char *ivar_type;
5167 int ivar_offset;
5168 }; */
5169
5170 static tree
5171 build_ivar_template (void)
5172 {
5173 tree objc_ivar_id, objc_ivar_record;
5174 tree decls, *chain = NULL;
5175
5176 objc_ivar_id = get_identifier (UTAG_IVAR);
5177 objc_ivar_record = objc_start_struct (objc_ivar_id);
5178
5179 /* char *ivar_name; */
5180 decls = add_field_decl (string_type_node, "ivar_name", &chain);
5181
5182 /* char *ivar_type; */
5183 add_field_decl (string_type_node, "ivar_type", &chain);
5184
5185 /* int ivar_offset; */
5186 add_field_decl (integer_type_node, "ivar_offset", &chain);
5187
5188 objc_finish_struct (objc_ivar_record, decls);
5189
5190 return objc_ivar_record;
5191 }
5192
5193 /* struct {
5194 int ivar_count;
5195 struct objc_ivar ivar_list[ivar_count];
5196 }; */
5197
5198 static tree
5199 build_ivar_list_template (tree list_type, int size)
5200 {
5201 tree objc_ivar_list_record;
5202 tree array_type, decls, *chain = NULL;
5203
5204 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5205
5206 /* int ivar_count; */
5207 decls = add_field_decl (integer_type_node, "ivar_count", &chain);
5208
5209 /* struct objc_ivar ivar_list[]; */
5210 array_type = build_sized_array_type (list_type, size);
5211 add_field_decl (array_type, "ivar_list", &chain);
5212
5213 objc_finish_struct (objc_ivar_list_record, decls);
5214
5215 return objc_ivar_list_record;
5216 }
5217
5218 /* struct {
5219 struct _objc__method_prototype_list *method_next;
5220 int method_count;
5221 struct objc_method method_list[method_count];
5222 }; */
5223
5224 static tree
5225 build_method_list_template (tree list_type, int size)
5226 {
5227 tree objc_ivar_list_record;
5228 tree array_type, decls, *chain = NULL;
5229
5230 objc_ivar_list_record = objc_start_struct (NULL_TREE);
5231
5232 /* struct _objc__method_prototype_list *method_next; */
5233 decls = add_field_decl (objc_method_proto_list_ptr, "method_next", &chain);
5234
5235 /* int method_count; */
5236 add_field_decl (integer_type_node, "method_count", &chain);
5237
5238 /* struct objc_method method_list[]; */
5239 array_type = build_sized_array_type (list_type, size);
5240 add_field_decl (array_type, "method_list", &chain);
5241
5242 objc_finish_struct (objc_ivar_list_record, decls);
5243
5244 return objc_ivar_list_record;
5245 }
5246
5247 static tree
5248 build_ivar_list_initializer (tree type, tree field_decl)
5249 {
5250 VEC(constructor_elt,gc) *inits = NULL;
5251
5252 do
5253 {
5254 VEC(constructor_elt,gc) *ivar = NULL;
5255 tree id;
5256
5257 /* Set name. */
5258 if (DECL_NAME (field_decl))
5259 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE,
5260 add_objc_string (DECL_NAME (field_decl),
5261 meth_var_names));
5262 else
5263 /* Unnamed bit-field ivar (yuck). */
5264 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, build_int_cst (NULL_TREE, 0));
5265
5266 /* Set type. */
5267 encode_field_decl (field_decl,
5268 obstack_object_size (&util_obstack),
5269 OBJC_ENCODE_DONT_INLINE_DEFS);
5270
5271 /* Null terminate string. */
5272 obstack_1grow (&util_obstack, 0);
5273 id = add_objc_string (get_identifier (XOBFINISH (&util_obstack, char *)),
5274 meth_var_types);
5275 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, id);
5276 obstack_free (&util_obstack, util_firstobj);
5277
5278 /* Set offset. */
5279 CONSTRUCTOR_APPEND_ELT (ivar, NULL_TREE, byte_position (field_decl));
5280 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5281 objc_build_constructor (type, ivar));
5282 do
5283 field_decl = DECL_CHAIN (field_decl);
5284 while (field_decl && TREE_CODE (field_decl) != FIELD_DECL);
5285 }
5286 while (field_decl);
5287
5288 return objc_build_constructor (build_array_type (type, 0), inits);
5289 }
5290
5291 static tree
5292 generate_ivars_list (tree type, const char *name, int size, tree list)
5293 {
5294 tree decl;
5295 VEC(constructor_elt,gc) *inits = NULL;
5296
5297 decl = start_var_decl (type, synth_id_with_class_suffix
5298 (name, objc_implementation_context));
5299
5300 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, build_int_cst (NULL_TREE, size));
5301 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, list);
5302
5303 finish_var_decl (decl,
5304 objc_build_constructor (TREE_TYPE (decl), inits));
5305
5306 return decl;
5307 }
5308
5309 /* Count only the fields occurring in T. */
5310
5311 static int
5312 ivar_list_length (tree t)
5313 {
5314 int count = 0;
5315
5316 for (; t; t = DECL_CHAIN (t))
5317 if (TREE_CODE (t) == FIELD_DECL)
5318 ++count;
5319
5320 return count;
5321 }
5322
5323 static void
5324 generate_ivar_lists (void)
5325 {
5326 tree initlist, ivar_list_template, chain;
5327 int size;
5328
5329 generating_instance_variables = 1;
5330
5331 if (!objc_ivar_template)
5332 objc_ivar_template = build_ivar_template ();
5333
5334 /* Only generate class variables for the root of the inheritance
5335 hierarchy since these will be the same for every class. */
5336
5337 if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
5338 && (chain = TYPE_FIELDS (objc_class_template)))
5339 {
5340 size = ivar_list_length (chain);
5341
5342 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
5343 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
5344
5345 UOBJC_CLASS_VARIABLES_decl
5346 = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
5347 size, initlist);
5348 }
5349 else
5350 UOBJC_CLASS_VARIABLES_decl = 0;
5351
5352 chain = CLASS_IVARS (implementation_template);
5353 if (chain)
5354 {
5355 size = ivar_list_length (chain);
5356 ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
5357 initlist = build_ivar_list_initializer (objc_ivar_template, chain);
5358
5359 UOBJC_INSTANCE_VARIABLES_decl
5360 = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
5361 size, initlist);
5362 }
5363 else
5364 UOBJC_INSTANCE_VARIABLES_decl = 0;
5365
5366 generating_instance_variables = 0;
5367 }
5368
5369 static tree
5370 build_dispatch_table_initializer (tree type, tree entries)
5371 {
5372 VEC(constructor_elt,gc) *inits = NULL;
5373
5374 do
5375 {
5376 VEC(constructor_elt,gc) *elems = NULL;
5377 tree expr;
5378
5379 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
5380 build_selector (METHOD_SEL_NAME (entries)));
5381
5382 /* Generate the method encoding if we don't have one already. */
5383 if (! METHOD_ENCODING (entries))
5384 METHOD_ENCODING (entries) =
5385 encode_method_prototype (entries);
5386
5387 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE,
5388 add_objc_string (METHOD_ENCODING (entries),
5389 meth_var_types));
5390
5391 expr = convert (ptr_type_node,
5392 build_unary_op (input_location, ADDR_EXPR,
5393 METHOD_DEFINITION (entries), 1));
5394 CONSTRUCTOR_APPEND_ELT (elems, NULL_TREE, expr);
5395
5396 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE,
5397 objc_build_constructor (type, elems));
5398
5399 entries = DECL_CHAIN (entries);
5400 }
5401 while (entries);
5402
5403 return objc_build_constructor (build_array_type (type, 0), inits);
5404 }
5405
5406 /* To accomplish method prototyping without generating all kinds of
5407 inane warnings, the definition of the dispatch table entries were
5408 changed from:
5409
5410 struct objc_method { SEL _cmd; ...; id (*_imp)(); };
5411 to:
5412 struct objc_method { SEL _cmd; ...; void *_imp; }; */
5413
5414 static tree
5415 build_method_template (void)
5416 {
5417 tree _SLT_record;
5418 tree decls, *chain = NULL;
5419
5420 _SLT_record = objc_start_struct (get_identifier (UTAG_METHOD));
5421
5422 /* SEL _cmd; */
5423 decls = add_field_decl (objc_selector_type, "_cmd", &chain);
5424
5425 /* char *method_types; */
5426 add_field_decl (string_type_node, "method_types", &chain);
5427
5428 /* void *_imp; */
5429 add_field_decl (build_pointer_type (void_type_node), "_imp", &chain);
5430
5431 objc_finish_struct (_SLT_record, decls);
5432
5433 return _SLT_record;
5434 }
5435
5436
5437 static tree
5438 generate_dispatch_table (tree type, const char *name, int size, tree list)
5439 {
5440 tree decl;
5441 VEC(constructor_elt,gc) *v = NULL;
5442
5443 decl = start_var_decl (type, synth_id_with_class_suffix
5444 (name, objc_implementation_context));
5445
5446 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
5447 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, size));
5448 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, list);
5449
5450 finish_var_decl (decl,
5451 objc_build_constructor (TREE_TYPE (decl), v));
5452
5453 return decl;
5454 }
5455
5456 static void
5457 mark_referenced_methods (void)
5458 {
5459 struct imp_entry *impent;
5460 tree chain;
5461
5462 for (impent = imp_list; impent; impent = impent->next)
5463 {
5464 chain = CLASS_CLS_METHODS (impent->imp_context);
5465 while (chain)
5466 {
5467 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
5468 chain = DECL_CHAIN (chain);
5469 }
5470
5471 chain = CLASS_NST_METHODS (impent->imp_context);
5472 while (chain)
5473 {
5474 cgraph_mark_needed_node (cgraph_node (METHOD_DEFINITION (chain)));
5475 chain = DECL_CHAIN (chain);
5476 }
5477 }
5478 }
5479
5480 static void
5481 generate_dispatch_tables (void)
5482 {
5483 tree initlist, chain, method_list_template;
5484 int size;
5485
5486 if (!objc_method_template)
5487 objc_method_template = build_method_template ();
5488
5489 chain = CLASS_CLS_METHODS (objc_implementation_context);
5490 if (chain)
5491 {
5492 size = list_length (chain);
5493
5494 method_list_template
5495 = build_method_list_template (objc_method_template, size);
5496 initlist
5497 = build_dispatch_table_initializer (objc_method_template, chain);
5498
5499 UOBJC_CLASS_METHODS_decl
5500 = generate_dispatch_table (method_list_template,
5501 ((TREE_CODE (objc_implementation_context)
5502 == CLASS_IMPLEMENTATION_TYPE)
5503 ? "_OBJC_CLASS_METHODS"
5504 : "_OBJC_CATEGORY_CLASS_METHODS"),
5505 size, initlist);
5506 }
5507 else
5508 UOBJC_CLASS_METHODS_decl = 0;
5509
5510 chain = CLASS_NST_METHODS (objc_implementation_context);
5511 if (chain)
5512 {
5513 size = list_length (chain);
5514
5515 method_list_template
5516 = build_method_list_template (objc_method_template, size);
5517 initlist
5518 = build_dispatch_table_initializer (objc_method_template, chain);
5519
5520 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
5521 UOBJC_INSTANCE_METHODS_decl
5522 = generate_dispatch_table (method_list_template,
5523 "_OBJC_INSTANCE_METHODS",
5524 size, initlist);
5525 else
5526 /* We have a category. */
5527 UOBJC_INSTANCE_METHODS_decl
5528 = generate_dispatch_table (method_list_template,
5529 "_OBJC_CATEGORY_INSTANCE_METHODS",
5530 size, initlist);
5531 }
5532 else
5533 UOBJC_INSTANCE_METHODS_decl = 0;
5534 }
5535
5536 static tree
5537 generate_protocol_list (tree i_or_p)
5538 {
5539 tree array_type, ptype, refs_decl, lproto, e, plist;
5540 int size = 0;
5541 const char *ref_name;
5542 VEC(constructor_elt,gc) *v = NULL;
5543
5544 if (TREE_CODE (i_or_p) == CLASS_INTERFACE_TYPE
5545 || TREE_CODE (i_or_p) == CATEGORY_INTERFACE_TYPE)
5546 plist = CLASS_PROTOCOL_LIST (i_or_p);
5547 else if (TREE_CODE (i_or_p) == PROTOCOL_INTERFACE_TYPE)
5548 plist = PROTOCOL_LIST (i_or_p);
5549 else
5550 abort ();
5551
5552 /* Compute size. */
5553 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5554 if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
5555 && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
5556 size++;
5557
5558 /* Build initializer. */
5559 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5560 e = build_int_cst (build_pointer_type (objc_protocol_template), size);
5561 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
5562
5563 for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
5564 {
5565 tree pval = TREE_VALUE (lproto);
5566
5567 if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
5568 && PROTOCOL_FORWARD_DECL (pval))
5569 {
5570 e = build_unary_op (input_location, ADDR_EXPR,
5571 PROTOCOL_FORWARD_DECL (pval), 0);
5572 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, e);
5573 }
5574 }
5575
5576 /* static struct objc_protocol *refs[n]; */
5577
5578 if (TREE_CODE (i_or_p) == PROTOCOL_INTERFACE_TYPE)
5579 ref_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p);
5580 else if (TREE_CODE (i_or_p) == CLASS_INTERFACE_TYPE)
5581 ref_name = synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p);
5582 else if (TREE_CODE (i_or_p) == CATEGORY_INTERFACE_TYPE)
5583 ref_name = synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p);
5584 else
5585 abort ();
5586
5587 ptype = build_pointer_type (objc_protocol_template);
5588 array_type = build_sized_array_type (ptype, size + 3);
5589 refs_decl = start_var_decl (array_type, ref_name);
5590
5591 finish_var_decl (refs_decl,
5592 objc_build_constructor (TREE_TYPE (refs_decl), v));
5593
5594 return refs_decl;
5595 }
5596
5597 static tree
5598 build_category_initializer (tree type, tree cat_name, tree class_name,
5599 tree instance_methods, tree class_methods,
5600 tree protocol_list)
5601 {
5602 tree expr;
5603 VEC(constructor_elt,gc) *v = NULL;
5604
5605 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, cat_name);
5606 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, class_name);
5607
5608 if (!instance_methods)
5609 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5610 else
5611 {
5612 expr = convert (objc_method_list_ptr,
5613 build_unary_op (input_location, ADDR_EXPR,
5614 instance_methods, 0));
5615 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
5616 }
5617 if (!class_methods)
5618 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5619 else
5620 {
5621 expr = convert (objc_method_list_ptr,
5622 build_unary_op (input_location, ADDR_EXPR,
5623 class_methods, 0));
5624 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
5625 }
5626
5627 /* protocol_list = */
5628 if (!protocol_list)
5629 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5630 else
5631 {
5632 expr = convert (build_pointer_type
5633 (build_pointer_type
5634 (objc_protocol_template)),
5635 build_unary_op (input_location, ADDR_EXPR,
5636 protocol_list, 0));
5637 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
5638 }
5639
5640 return objc_build_constructor (type, v);
5641 }
5642
5643 /* struct _objc_class {
5644 struct objc_class *isa;
5645 struct objc_class *super_class;
5646 char *name;
5647 long version;
5648 long info;
5649 long instance_size;
5650 struct objc_ivar_list *ivars;
5651 struct objc_method_list *methods;
5652 if (flag_next_runtime)
5653 struct objc_cache *cache;
5654 else {
5655 struct sarray *dtable;
5656 struct objc_class *subclass_list;
5657 struct objc_class *sibling_class;
5658 }
5659 struct objc_protocol_list *protocols;
5660 if (flag_next_runtime)
5661 void *sel_id;
5662 void *gc_object_type;
5663 }; */
5664
5665 static tree
5666 build_shared_structure_initializer (tree type, tree isa, tree super,
5667 tree name, tree size, int status,
5668 tree dispatch_table, tree ivar_list,
5669 tree protocol_list)
5670 {
5671 tree expr;
5672 VEC(constructor_elt,gc) *v = NULL;
5673
5674 /* isa = */
5675 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, isa);
5676
5677 /* super_class = */
5678 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, super);
5679
5680 /* name = */
5681 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, default_conversion (name));
5682
5683 /* version = */
5684 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5685 build_int_cst (long_integer_type_node, 0));
5686
5687 /* info = */
5688 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5689 build_int_cst (long_integer_type_node, status));
5690
5691 /* instance_size = */
5692 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
5693 convert (long_integer_type_node, size));
5694
5695 /* objc_ivar_list = */
5696 if (!ivar_list)
5697 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5698 else
5699 {
5700 expr = convert (objc_ivar_list_ptr,
5701 build_unary_op (input_location, ADDR_EXPR,
5702 ivar_list, 0));
5703 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
5704 }
5705
5706 /* objc_method_list = */
5707 if (!dispatch_table)
5708 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5709 else
5710 {
5711 expr = convert (objc_method_list_ptr,
5712 build_unary_op (input_location, ADDR_EXPR,
5713 dispatch_table, 0));
5714 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
5715 }
5716
5717 if (flag_next_runtime)
5718 /* method_cache = */
5719 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5720 else
5721 {
5722 /* dtable = */
5723 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5724
5725 /* subclass_list = */
5726 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5727
5728 /* sibling_class = */
5729 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5730 }
5731
5732 /* protocol_list = */
5733 if (! protocol_list)
5734 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5735 else
5736 {
5737 expr = convert (build_pointer_type
5738 (build_pointer_type
5739 (objc_protocol_template)),
5740 build_unary_op (input_location, ADDR_EXPR,
5741 protocol_list, 0));
5742 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, expr);
5743 }
5744
5745 if (flag_next_runtime)
5746 /* sel_id = NULL */
5747 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5748
5749 /* gc_object_type = NULL */
5750 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, 0));
5751
5752 return objc_build_constructor (type, v);
5753 }
5754
5755 /* Retrieve category interface CAT_NAME (if any) associated with CLASS. */
5756
5757 static inline tree
5758 lookup_category (tree klass, tree cat_name)
5759 {
5760 tree category = CLASS_CATEGORY_LIST (klass);
5761
5762 while (category && CLASS_SUPER_NAME (category) != cat_name)
5763 category = CLASS_CATEGORY_LIST (category);
5764 return category;
5765 }
5766
5767 /* static struct objc_category _OBJC_CATEGORY_<name> = { ... }; */
5768
5769 static void
5770 generate_category (struct imp_entry *impent)
5771 {
5772 tree initlist, cat_name_expr, class_name_expr;
5773 tree protocol_decl, category;
5774 tree cat = impent->imp_context;
5775
5776 implementation_template = impent->imp_template;
5777 UOBJC_CLASS_decl = impent->class_decl;
5778 UOBJC_METACLASS_decl = impent->meta_decl;
5779
5780 add_class_reference (CLASS_NAME (cat));
5781 cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
5782
5783 class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
5784
5785 category = lookup_category (implementation_template,
5786 CLASS_SUPER_NAME (cat));
5787
5788 if (category && CLASS_PROTOCOL_LIST (category))
5789 {
5790 generate_protocol_references (CLASS_PROTOCOL_LIST (category));
5791 protocol_decl = generate_protocol_list (category);
5792 }
5793 else
5794 protocol_decl = 0;
5795
5796 initlist = build_category_initializer (TREE_TYPE (UOBJC_CLASS_decl),
5797 cat_name_expr, class_name_expr,
5798 UOBJC_INSTANCE_METHODS_decl,
5799 UOBJC_CLASS_METHODS_decl,
5800 protocol_decl);
5801 /* Finish and initialize the forward decl. */
5802 finish_var_decl (UOBJC_CLASS_decl, initlist);
5803 }
5804
5805 /* static struct objc_class _OBJC_METACLASS_Foo={ ... };
5806 static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5807
5808 static void
5809 generate_shared_structures (struct imp_entry *impent)
5810 {
5811 tree name_expr, super_expr, root_expr;
5812 tree my_root_id, my_super_id;
5813 tree cast_type, initlist, protocol_decl;
5814 int cls_flags;
5815
5816 objc_implementation_context = impent->imp_context;
5817 implementation_template = impent->imp_template;
5818 UOBJC_CLASS_decl = impent->class_decl;
5819 UOBJC_METACLASS_decl = impent->meta_decl;
5820 cls_flags = impent->has_cxx_cdtors ? CLS_HAS_CXX_STRUCTORS : 0 ;
5821
5822 my_super_id = CLASS_SUPER_NAME (implementation_template);
5823 if (my_super_id)
5824 {
5825 add_class_reference (my_super_id);
5826
5827 /* Compute "my_root_id" - this is required for code generation.
5828 the "isa" for all meta class structures points to the root of
5829 the inheritance hierarchy (e.g. "__Object")... */
5830 my_root_id = my_super_id;
5831 do
5832 {
5833 tree my_root_int = lookup_interface (my_root_id);
5834
5835 if (my_root_int && CLASS_SUPER_NAME (my_root_int))
5836 my_root_id = CLASS_SUPER_NAME (my_root_int);
5837 else
5838 break;
5839 }
5840 while (1);
5841 }
5842 else
5843 /* No super class. */
5844 my_root_id = CLASS_NAME (implementation_template);
5845
5846 cast_type = build_pointer_type (objc_class_template);
5847 name_expr = add_objc_string (CLASS_NAME (implementation_template),
5848 class_names);
5849
5850 /* Install class `isa' and `super' pointers at runtime. */
5851 if (my_super_id)
5852 super_expr = add_objc_string (my_super_id, class_names);
5853 else
5854 super_expr = integer_zero_node;
5855
5856 super_expr = build_c_cast (input_location,
5857 cast_type, super_expr); /* cast! */
5858
5859 root_expr = add_objc_string (my_root_id, class_names);
5860 root_expr = build_c_cast (input_location, cast_type, root_expr); /* cast! */
5861
5862 if (CLASS_PROTOCOL_LIST (implementation_template))
5863 {
5864 generate_protocol_references
5865 (CLASS_PROTOCOL_LIST (implementation_template));
5866 protocol_decl = generate_protocol_list (implementation_template);
5867 }
5868 else
5869 protocol_decl = 0;
5870
5871 /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
5872
5873 initlist
5874 = build_shared_structure_initializer
5875 (TREE_TYPE (UOBJC_METACLASS_decl),
5876 root_expr, super_expr, name_expr,
5877 convert (integer_type_node, TYPE_SIZE_UNIT (objc_class_template)),
5878 2 /*CLS_META*/,
5879 UOBJC_CLASS_METHODS_decl,
5880 UOBJC_CLASS_VARIABLES_decl,
5881 protocol_decl);
5882
5883 finish_var_decl (UOBJC_METACLASS_decl, initlist);
5884
5885 /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
5886
5887 initlist
5888 = build_shared_structure_initializer
5889 (TREE_TYPE (UOBJC_CLASS_decl),
5890 build_unary_op (input_location, ADDR_EXPR, UOBJC_METACLASS_decl, 0),
5891 super_expr, name_expr,
5892 convert (integer_type_node,
5893 TYPE_SIZE_UNIT (CLASS_STATIC_TEMPLATE
5894 (implementation_template))),
5895 1 /*CLS_FACTORY*/ | cls_flags,
5896 UOBJC_INSTANCE_METHODS_decl,
5897 UOBJC_INSTANCE_VARIABLES_decl,
5898 protocol_decl);
5899
5900 finish_var_decl (UOBJC_CLASS_decl, initlist);
5901 }
5902
5903
5904 static const char *
5905 synth_id_with_class_suffix (const char *preamble, tree ctxt)
5906 {
5907 static char string[BUFSIZE];
5908
5909 if (TREE_CODE (ctxt) == CLASS_IMPLEMENTATION_TYPE
5910 || TREE_CODE (ctxt) == CLASS_INTERFACE_TYPE)
5911 {
5912 sprintf (string, "%s_%s", preamble,
5913 IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
5914 }
5915 else if (TREE_CODE (ctxt) == CATEGORY_IMPLEMENTATION_TYPE
5916 || TREE_CODE (ctxt) == CATEGORY_INTERFACE_TYPE)
5917 {
5918 /* We have a category. */
5919 const char *const class_name
5920 = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
5921 const char *const class_super_name
5922 = IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context));
5923 sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
5924 }
5925 else if (TREE_CODE (ctxt) == PROTOCOL_INTERFACE_TYPE)
5926 {
5927 const char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
5928 sprintf (string, "%s_%s", preamble, protocol_name);
5929 }
5930 else
5931 abort ();
5932
5933 return string;
5934 }
5935
5936 /* If type is empty or only type qualifiers are present, add default
5937 type of id (otherwise grokdeclarator will default to int). */
5938
5939 static tree
5940 adjust_type_for_id_default (tree type)
5941 {
5942 if (!type)
5943 type = make_node (TREE_LIST);
5944
5945 if (!TREE_VALUE (type))
5946 TREE_VALUE (type) = objc_object_type;
5947 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
5948 && TYPED_OBJECT (TREE_VALUE (type)))
5949 error ("can not use an object as parameter to a method");
5950
5951 return type;
5952 }
5953
5954 /* Usage:
5955 keyworddecl:
5956 selector ':' '(' typename ')' identifier
5957
5958 Purpose:
5959 Transform an Objective-C keyword argument into
5960 the C equivalent parameter declarator.
5961
5962 In: key_name, an "identifier_node" (optional).
5963 arg_type, a "tree_list" (optional).
5964 arg_name, an "identifier_node".
5965
5966 Note: It would be really nice to strongly type the preceding
5967 arguments in the function prototype; however, then I
5968 could not use the "accessor" macros defined in "tree.h".
5969
5970 Out: an instance of "keyword_decl". */
5971
5972 tree
5973 objc_build_keyword_decl (tree key_name, tree arg_type, tree arg_name)
5974 {
5975 tree keyword_decl;
5976
5977 /* If no type is specified, default to "id". */
5978 arg_type = adjust_type_for_id_default (arg_type);
5979
5980 keyword_decl = make_node (KEYWORD_DECL);
5981
5982 TREE_TYPE (keyword_decl) = arg_type;
5983 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
5984 KEYWORD_KEY_NAME (keyword_decl) = key_name;
5985
5986 return keyword_decl;
5987 }
5988
5989 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
5990
5991 static tree
5992 build_keyword_selector (tree selector)
5993 {
5994 int len = 0;
5995 tree key_chain, key_name;
5996 char *buf;
5997
5998 /* Scan the selector to see how much space we'll need. */
5999 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6000 {
6001 if (TREE_CODE (selector) == KEYWORD_DECL)
6002 key_name = KEYWORD_KEY_NAME (key_chain);
6003 else if (TREE_CODE (selector) == TREE_LIST)
6004 key_name = TREE_PURPOSE (key_chain);
6005 else
6006 abort ();
6007
6008 if (key_name)
6009 len += IDENTIFIER_LENGTH (key_name) + 1;
6010 else
6011 /* Just a ':' arg. */
6012 len++;
6013 }
6014
6015 buf = (char *) alloca (len + 1);
6016 /* Start the buffer out as an empty string. */
6017 buf[0] = '\0';
6018
6019 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
6020 {
6021 if (TREE_CODE (selector) == KEYWORD_DECL)
6022 key_name = KEYWORD_KEY_NAME (key_chain);
6023 else if (TREE_CODE (selector) == TREE_LIST)
6024 {
6025 key_name = TREE_PURPOSE (key_chain);
6026 /* The keyword decl chain will later be used as a function argument
6027 chain. Unhook the selector itself so as to not confuse other
6028 parts of the compiler. */
6029 TREE_PURPOSE (key_chain) = NULL_TREE;
6030 }
6031 else
6032 abort ();
6033
6034 if (key_name)
6035 strcat (buf, IDENTIFIER_POINTER (key_name));
6036 strcat (buf, ":");
6037 }
6038
6039 return get_identifier (buf);
6040 }
6041
6042 /* Used for declarations and definitions. */
6043
6044 static tree
6045 build_method_decl (enum tree_code code, tree ret_type, tree selector,
6046 tree add_args, bool ellipsis)
6047 {
6048 tree method_decl;
6049
6050 /* If no type is specified, default to "id". */
6051 ret_type = adjust_type_for_id_default (ret_type);
6052
6053 method_decl = make_node (code);
6054 TREE_TYPE (method_decl) = ret_type;
6055
6056 /* If we have a keyword selector, create an identifier_node that
6057 represents the full selector name (`:' included)... */
6058 if (TREE_CODE (selector) == KEYWORD_DECL)
6059 {
6060 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
6061 METHOD_SEL_ARGS (method_decl) = selector;
6062 METHOD_ADD_ARGS (method_decl) = add_args;
6063 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
6064 }
6065 else
6066 {
6067 METHOD_SEL_NAME (method_decl) = selector;
6068 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
6069 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
6070 }
6071
6072 return method_decl;
6073 }
6074
6075 #define METHOD_DEF 0
6076 #define METHOD_REF 1
6077
6078 /* Used by `build_objc_method_call' and `comp_proto_with_proto'. Return
6079 an argument list for method METH. CONTEXT is either METHOD_DEF or
6080 METHOD_REF, saying whether we are trying to define a method or call
6081 one. SUPERFLAG says this is for a send to super; this makes a
6082 difference for the NeXT calling sequence in which the lookup and
6083 the method call are done together. If METH is null, user-defined
6084 arguments (i.e., beyond self and _cmd) shall be represented by `...'. */
6085
6086 static tree
6087 get_arg_type_list (tree meth, int context, int superflag)
6088 {
6089 tree arglist, akey;
6090
6091 /* Receiver type. */
6092 if (flag_next_runtime && superflag)
6093 arglist = build_tree_list (NULL_TREE, objc_super_type);
6094 else if (context == METHOD_DEF && TREE_CODE (meth) == INSTANCE_METHOD_DECL)
6095 arglist = build_tree_list (NULL_TREE, objc_instance_type);
6096 else
6097 arglist = build_tree_list (NULL_TREE, objc_object_type);
6098
6099 /* Selector type - will eventually change to `int'. */
6100 chainon (arglist, build_tree_list (NULL_TREE, objc_selector_type));
6101
6102 /* No actual method prototype given -- assume that remaining arguments
6103 are `...'. */
6104 if (!meth)
6105 return arglist;
6106
6107 /* Build a list of argument types. */
6108 for (akey = METHOD_SEL_ARGS (meth); akey; akey = DECL_CHAIN (akey))
6109 {
6110 tree arg_type = TREE_VALUE (TREE_TYPE (akey));
6111
6112 /* Decay argument types for the underlying C function as appropriate. */
6113 arg_type = objc_decay_parm_type (arg_type);
6114
6115 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
6116 }
6117
6118 if (METHOD_ADD_ARGS (meth))
6119 {
6120 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (meth));
6121 akey; akey = TREE_CHAIN (akey))
6122 {
6123 tree arg_type = TREE_TYPE (TREE_VALUE (akey));
6124
6125 arg_type = objc_decay_parm_type (arg_type);
6126
6127 chainon (arglist, build_tree_list (NULL_TREE, arg_type));
6128 }
6129
6130 if (!METHOD_ADD_ARGS_ELLIPSIS_P (meth))
6131 goto lack_of_ellipsis;
6132 }
6133 else
6134 {
6135 lack_of_ellipsis:
6136 chainon (arglist, OBJC_VOID_AT_END);
6137 }
6138
6139 return arglist;
6140 }
6141
6142 static tree
6143 check_duplicates (hash hsh, int methods, int is_class)
6144 {
6145 tree meth = NULL_TREE;
6146
6147 if (hsh)
6148 {
6149 meth = hsh->key;
6150
6151 if (hsh->list)
6152 {
6153 /* We have two or more methods with the same name but
6154 different types. */
6155 attr loop;
6156
6157 /* But just how different are those types? If
6158 -Wno-strict-selector-match is specified, we shall not
6159 complain if the differences are solely among types with
6160 identical size and alignment. */
6161 if (!warn_strict_selector_match)
6162 {
6163 for (loop = hsh->list; loop; loop = loop->next)
6164 if (!comp_proto_with_proto (meth, loop->value, 0))
6165 goto issue_warning;
6166
6167 return meth;
6168 }
6169
6170 issue_warning:
6171 if (methods)
6172 {
6173 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
6174
6175 warning_at (input_location, 0,
6176 "multiple methods named %<%c%E%> found",
6177 (is_class ? '+' : '-'),
6178 METHOD_SEL_NAME (meth));
6179 inform (DECL_SOURCE_LOCATION (meth), "using %<%c%s%>",
6180 (type ? '-' : '+'),
6181 identifier_to_locale (gen_method_decl (meth)));
6182 }
6183 else
6184 {
6185 bool type = TREE_CODE (meth) == INSTANCE_METHOD_DECL;
6186
6187 warning_at (input_location, 0,
6188 "multiple selectors named %<%c%E%> found",
6189 (is_class ? '+' : '-'),
6190 METHOD_SEL_NAME (meth));
6191 inform (DECL_SOURCE_LOCATION (meth), "found %<%c%s%>",
6192 (type ? '-' : '+'),
6193 identifier_to_locale (gen_method_decl (meth)));
6194 }
6195
6196 for (loop = hsh->list; loop; loop = loop->next)
6197 {
6198 bool type = TREE_CODE (loop->value) == INSTANCE_METHOD_DECL;
6199
6200 inform (DECL_SOURCE_LOCATION (loop->value), "also found %<%c%s%>",
6201 (type ? '-' : '+'),
6202 identifier_to_locale (gen_method_decl (loop->value)));
6203 }
6204 }
6205 }
6206 return meth;
6207 }
6208
6209 /* If RECEIVER is a class reference, return the identifier node for
6210 the referenced class. RECEIVER is created by objc_get_class_reference,
6211 so we check the exact form created depending on which runtimes are
6212 used. */
6213
6214 static tree
6215 receiver_is_class_object (tree receiver, int self, int super)
6216 {
6217 tree chain, exp, arg;
6218
6219 /* The receiver is 'self' or 'super' in the context of a class method. */
6220 if (objc_method_context
6221 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
6222 && (self || super))
6223 return (super
6224 ? CLASS_SUPER_NAME (implementation_template)
6225 : CLASS_NAME (implementation_template));
6226
6227 if (flag_next_runtime)
6228 {
6229 /* The receiver is a variable created by
6230 build_class_reference_decl. */
6231 if (TREE_CODE (receiver) == VAR_DECL && IS_CLASS (TREE_TYPE (receiver)))
6232 /* Look up the identifier. */
6233 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
6234 if (TREE_PURPOSE (chain) == receiver)
6235 return TREE_VALUE (chain);
6236 }
6237
6238 /* The receiver is a function call that returns an id. Check if
6239 it is a call to objc_getClass, if so, pick up the class name. */
6240 if (TREE_CODE (receiver) == CALL_EXPR
6241 && (exp = CALL_EXPR_FN (receiver))
6242 && TREE_CODE (exp) == ADDR_EXPR
6243 && (exp = TREE_OPERAND (exp, 0))
6244 && TREE_CODE (exp) == FUNCTION_DECL
6245 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
6246 prototypes for objc_get_class(). Thankfully, they seem to share the
6247 same function type. */
6248 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
6249 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), TAG_GETCLASS)
6250 /* We have a call to objc_get_class/objc_getClass! */
6251 && (arg = CALL_EXPR_ARG (receiver, 0)))
6252 {
6253 STRIP_NOPS (arg);
6254 if (TREE_CODE (arg) == ADDR_EXPR
6255 && (arg = TREE_OPERAND (arg, 0))
6256 && TREE_CODE (arg) == STRING_CST)
6257 /* Finally, we have the class name. */
6258 return get_identifier (TREE_STRING_POINTER (arg));
6259 }
6260 return 0;
6261 }
6262 \f
6263 /* If we are currently building a message expr, this holds
6264 the identifier of the selector of the message. This is
6265 used when printing warnings about argument mismatches. */
6266
6267 static tree current_objc_message_selector = 0;
6268
6269 tree
6270 objc_message_selector (void)
6271 {
6272 return current_objc_message_selector;
6273 }
6274
6275 /* Construct an expression for sending a message.
6276 MESS has the object to send to in TREE_PURPOSE
6277 and the argument list (including selector) in TREE_VALUE.
6278
6279 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
6280 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
6281
6282 tree
6283 objc_build_message_expr (tree mess)
6284 {
6285 tree receiver = TREE_PURPOSE (mess);
6286 tree sel_name;
6287 #ifdef OBJCPLUS
6288 tree args = TREE_PURPOSE (TREE_VALUE (mess));
6289 #else
6290 tree args = TREE_VALUE (mess);
6291 #endif
6292 tree method_params = NULL_TREE;
6293
6294 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
6295 return error_mark_node;
6296
6297 /* Obtain the full selector name. */
6298 if (TREE_CODE (args) == IDENTIFIER_NODE)
6299 /* A unary selector. */
6300 sel_name = args;
6301 else if (TREE_CODE (args) == TREE_LIST)
6302 sel_name = build_keyword_selector (args);
6303 else
6304 abort ();
6305
6306 /* Build the parameter list to give to the method. */
6307 if (TREE_CODE (args) == TREE_LIST)
6308 #ifdef OBJCPLUS
6309 method_params = chainon (args, TREE_VALUE (TREE_VALUE (mess)));
6310 #else
6311 {
6312 tree chain = args, prev = NULL_TREE;
6313
6314 /* We have a keyword selector--check for comma expressions. */
6315 while (chain)
6316 {
6317 tree element = TREE_VALUE (chain);
6318
6319 /* We have a comma expression, must collapse... */
6320 if (TREE_CODE (element) == TREE_LIST)
6321 {
6322 if (prev)
6323 TREE_CHAIN (prev) = element;
6324 else
6325 args = element;
6326 }
6327 prev = chain;
6328 chain = TREE_CHAIN (chain);
6329 }
6330 method_params = args;
6331 }
6332 #endif
6333
6334 #ifdef OBJCPLUS
6335 if (processing_template_decl)
6336 /* Must wait until template instantiation time. */
6337 return build_min_nt (MESSAGE_SEND_EXPR, receiver, sel_name,
6338 method_params);
6339 #endif
6340
6341 return objc_finish_message_expr (receiver, sel_name, method_params);
6342 }
6343
6344 /* Look up method SEL_NAME that would be suitable for receiver
6345 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
6346 nonzero), and report on any duplicates. */
6347
6348 static tree
6349 lookup_method_in_hash_lists (tree sel_name, int is_class)
6350 {
6351 hash method_prototype = NULL;
6352
6353 if (!is_class)
6354 method_prototype = hash_lookup (nst_method_hash_list,
6355 sel_name);
6356
6357 if (!method_prototype)
6358 {
6359 method_prototype = hash_lookup (cls_method_hash_list,
6360 sel_name);
6361 is_class = 1;
6362 }
6363
6364 return check_duplicates (method_prototype, 1, is_class);
6365 }
6366
6367 /* The 'objc_finish_message_expr' routine is called from within
6368 'objc_build_message_expr' for non-template functions. In the case of
6369 C++ template functions, it is called from 'build_expr_from_tree'
6370 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded. */
6371
6372 tree
6373 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params)
6374 {
6375 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
6376 tree selector, retval, class_tree;
6377 int self, super, have_cast;
6378
6379 /* Extract the receiver of the message, as well as its type
6380 (where the latter may take the form of a cast or be inferred
6381 from the implementation context). */
6382 rtype = receiver;
6383 while (TREE_CODE (rtype) == COMPOUND_EXPR
6384 || TREE_CODE (rtype) == MODIFY_EXPR
6385 || CONVERT_EXPR_P (rtype)
6386 || TREE_CODE (rtype) == COMPONENT_REF)
6387 rtype = TREE_OPERAND (rtype, 0);
6388 self = (rtype == self_decl);
6389 super = (rtype == UOBJC_SUPER_decl);
6390 rtype = TREE_TYPE (receiver);
6391 have_cast = (TREE_CODE (receiver) == NOP_EXPR
6392 || (TREE_CODE (receiver) == COMPOUND_EXPR
6393 && !IS_SUPER (rtype)));
6394
6395 /* If we are calling [super dealloc], reset our warning flag. */
6396 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
6397 should_call_super_dealloc = 0;
6398
6399 /* If the receiver is a class object, retrieve the corresponding
6400 @interface, if one exists. */
6401 class_tree = receiver_is_class_object (receiver, self, super);
6402
6403 /* Now determine the receiver type (if an explicit cast has not been
6404 provided). */
6405 if (!have_cast)
6406 {
6407 if (class_tree)
6408 rtype = lookup_interface (class_tree);
6409 /* Handle `self' and `super'. */
6410 else if (super)
6411 {
6412 if (!CLASS_SUPER_NAME (implementation_template))
6413 {
6414 error ("no super class declared in @interface for %qE",
6415 CLASS_NAME (implementation_template));
6416 return error_mark_node;
6417 }
6418 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
6419 }
6420 else if (self)
6421 rtype = lookup_interface (CLASS_NAME (implementation_template));
6422 }
6423
6424 /* If receiver is of type `id' or `Class' (or if the @interface for a
6425 class is not visible), we shall be satisfied with the existence of
6426 any instance or class method. */
6427 if (objc_is_id (rtype))
6428 {
6429 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
6430 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
6431 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
6432 : NULL_TREE);
6433 rtype = NULL_TREE;
6434
6435 if (rprotos)
6436 {
6437 /* If messaging 'id <Protos>' or 'Class <Proto>', first search
6438 in protocols themselves for the method prototype. */
6439 method_prototype
6440 = lookup_method_in_protocol_list (rprotos, sel_name,
6441 class_tree != NULL_TREE);
6442
6443 /* If messaging 'Class <Proto>' but did not find a class method
6444 prototype, search for an instance method instead, and warn
6445 about having done so. */
6446 if (!method_prototype && !rtype && class_tree != NULL_TREE)
6447 {
6448 method_prototype
6449 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
6450
6451 if (method_prototype)
6452 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
6453 sel_name, sel_name);
6454 }
6455 }
6456 }
6457 else if (rtype)
6458 {
6459 tree orig_rtype = rtype;
6460
6461 if (TREE_CODE (rtype) == POINTER_TYPE)
6462 rtype = TREE_TYPE (rtype);
6463 /* Traverse typedef aliases */
6464 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
6465 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
6466 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
6467 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
6468 if (TYPED_OBJECT (rtype))
6469 {
6470 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
6471 rtype = TYPE_OBJC_INTERFACE (rtype);
6472 }
6473 /* If we could not find an @interface declaration, we must have
6474 only seen a @class declaration; so, we cannot say anything
6475 more intelligent about which methods the receiver will
6476 understand. */
6477 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
6478 {
6479 rtype = NULL_TREE;
6480 /* We could not find an @interface declaration, yet Message maybe in a
6481 @class's protocol. */
6482 if (!method_prototype && rprotos)
6483 method_prototype
6484 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
6485 }
6486 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
6487 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
6488 {
6489 /* We have a valid ObjC class name. Look up the method name
6490 in the published @interface for the class (and its
6491 superclasses). */
6492 method_prototype
6493 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
6494
6495 /* If the method was not found in the @interface, it may still
6496 exist locally as part of the @implementation. */
6497 if (!method_prototype && objc_implementation_context
6498 && CLASS_NAME (objc_implementation_context)
6499 == OBJC_TYPE_NAME (rtype))
6500 method_prototype
6501 = lookup_method
6502 ((class_tree
6503 ? CLASS_CLS_METHODS (objc_implementation_context)
6504 : CLASS_NST_METHODS (objc_implementation_context)),
6505 sel_name);
6506
6507 /* If we haven't found a candidate method by now, try looking for
6508 it in the protocol list. */
6509 if (!method_prototype && rprotos)
6510 method_prototype
6511 = lookup_method_in_protocol_list (rprotos, sel_name,
6512 class_tree != NULL_TREE);
6513 }
6514 else
6515 {
6516 warning (0, "invalid receiver type %qs",
6517 identifier_to_locale (gen_type_name (orig_rtype)));
6518 /* After issuing the "invalid receiver" warning, perform method
6519 lookup as if we were messaging 'id'. */
6520 rtype = rprotos = NULL_TREE;
6521 }
6522 }
6523
6524
6525 /* For 'id' or 'Class' receivers, search in the global hash table
6526 as a last resort. For all receivers, warn if protocol searches
6527 have failed. */
6528 if (!method_prototype)
6529 {
6530 if (rprotos)
6531 warning (0, "%<%c%E%> not found in protocol(s)",
6532 (class_tree ? '+' : '-'),
6533 sel_name);
6534
6535 if (!rtype)
6536 method_prototype
6537 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
6538 }
6539
6540 if (!method_prototype)
6541 {
6542 static bool warn_missing_methods = false;
6543
6544 if (rtype)
6545 warning (0, "%qE may not respond to %<%c%E%>",
6546 OBJC_TYPE_NAME (rtype),
6547 (class_tree ? '+' : '-'),
6548 sel_name);
6549 /* If we are messaging an 'id' or 'Class' object and made it here,
6550 then we have failed to find _any_ instance or class method,
6551 respectively. */
6552 else
6553 warning (0, "no %<%c%E%> method found",
6554 (class_tree ? '+' : '-'),
6555 sel_name);
6556
6557 if (!warn_missing_methods)
6558 {
6559 warning_at (input_location,
6560 0, "(Messages without a matching method signature");
6561 warning_at (input_location,
6562 0, "will be assumed to return %<id%> and accept");
6563 warning_at (input_location,
6564 0, "%<...%> as arguments.)");
6565 warn_missing_methods = true;
6566 }
6567 }
6568
6569 /* Save the selector name for printing error messages. */
6570 current_objc_message_selector = sel_name;
6571
6572 /* Build the parameters list for looking up the method.
6573 These are the object itself and the selector. */
6574
6575 if (flag_typed_selectors)
6576 selector = build_typed_selector_reference (input_location,
6577 sel_name, method_prototype);
6578 else
6579 selector = build_selector_reference (input_location, sel_name);
6580
6581 retval = build_objc_method_call (input_location, super, method_prototype,
6582 receiver,
6583 selector, method_params);
6584
6585 current_objc_message_selector = 0;
6586
6587 return retval;
6588 }
6589 \f
6590 /* Build a tree expression to send OBJECT the operation SELECTOR,
6591 looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
6592 assuming the method has prototype METHOD_PROTOTYPE.
6593 (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
6594 LOC is the location of the expression to build.
6595 Use METHOD_PARAMS as list of args to pass to the method.
6596 If SUPER_FLAG is nonzero, we look up the superclass's method. */
6597
6598 static tree
6599 build_objc_method_call (location_t loc, int super_flag, tree method_prototype,
6600 tree lookup_object, tree selector,
6601 tree method_params)
6602 {
6603 tree sender = (super_flag ? umsg_super_decl :
6604 (!flag_next_runtime || flag_nil_receivers
6605 ? (flag_objc_direct_dispatch
6606 ? umsg_fast_decl
6607 : umsg_decl)
6608 : umsg_nonnil_decl));
6609 tree rcv_p = (super_flag ? objc_super_type : objc_object_type);
6610
6611 /* If a prototype for the method to be called exists, then cast
6612 the sender's return type and arguments to match that of the method.
6613 Otherwise, leave sender as is. */
6614 tree ret_type
6615 = (method_prototype
6616 ? TREE_VALUE (TREE_TYPE (method_prototype))
6617 : objc_object_type);
6618 tree sender_cast
6619 = build_pointer_type
6620 (build_function_type
6621 (ret_type,
6622 get_arg_type_list
6623 (method_prototype, METHOD_REF, super_flag)));
6624 tree method, t;
6625
6626 lookup_object = build_c_cast (loc, rcv_p, lookup_object);
6627
6628 /* Use SAVE_EXPR to avoid evaluating the receiver twice. */
6629 lookup_object = save_expr (lookup_object);
6630
6631 if (flag_next_runtime)
6632 {
6633 /* If we are returning a struct in memory, and the address
6634 of that memory location is passed as a hidden first
6635 argument, then change which messenger entry point this
6636 expr will call. NB: Note that sender_cast remains
6637 unchanged (it already has a struct return type). */
6638 if (!targetm.calls.struct_value_rtx (0, 0)
6639 && (TREE_CODE (ret_type) == RECORD_TYPE
6640 || TREE_CODE (ret_type) == UNION_TYPE)
6641 && targetm.calls.return_in_memory (ret_type, 0))
6642 sender = (super_flag ? umsg_super_stret_decl :
6643 flag_nil_receivers ? umsg_stret_decl : umsg_nonnil_stret_decl);
6644
6645 method_params = tree_cons (NULL_TREE, lookup_object,
6646 tree_cons (NULL_TREE, selector,
6647 method_params));
6648 method = build_fold_addr_expr_loc (input_location, sender);
6649 }
6650 else
6651 {
6652 /* This is the portable (GNU) way. */
6653 tree object;
6654
6655 /* First, call the lookup function to get a pointer to the method,
6656 then cast the pointer, then call it with the method arguments. */
6657
6658 object = (super_flag ? self_decl : lookup_object);
6659
6660 t = tree_cons (NULL_TREE, selector, NULL_TREE);
6661 t = tree_cons (NULL_TREE, lookup_object, t);
6662 method = build_function_call (loc, sender, t);
6663
6664 /* Pass the object to the method. */
6665 method_params = tree_cons (NULL_TREE, object,
6666 tree_cons (NULL_TREE, selector,
6667 method_params));
6668 }
6669
6670 /* ??? Selector is not at this point something we can use inside
6671 the compiler itself. Set it to garbage for the nonce. */
6672 t = build3 (OBJ_TYPE_REF, sender_cast, method, lookup_object, size_zero_node);
6673 return build_function_call (loc,
6674 t, method_params);
6675 }
6676 \f
6677 static void
6678 build_protocol_reference (tree p)
6679 {
6680 tree decl;
6681 const char *proto_name;
6682
6683 /* static struct _objc_protocol _OBJC_PROTOCOL_<mumble>; */
6684
6685 proto_name = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
6686 decl = start_var_decl (objc_protocol_template, proto_name);
6687
6688 PROTOCOL_FORWARD_DECL (p) = decl;
6689 }
6690
6691 /* This function is called by the parser when (and only when) a
6692 @protocol() expression is found, in order to compile it. */
6693 tree
6694 objc_build_protocol_expr (tree protoname)
6695 {
6696 tree expr;
6697 tree p = lookup_protocol (protoname);
6698
6699 if (!p)
6700 {
6701 error ("cannot find protocol declaration for %qE",
6702 protoname);
6703 return error_mark_node;
6704 }
6705
6706 if (!PROTOCOL_FORWARD_DECL (p))
6707 build_protocol_reference (p);
6708
6709 expr = build_unary_op (input_location,
6710 ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
6711
6712 /* ??? Ideally we'd build the reference with objc_protocol_type directly,
6713 if we have it, rather than converting it here. */
6714 expr = convert (objc_protocol_type, expr);
6715
6716 /* The @protocol() expression is being compiled into a pointer to a
6717 statically allocated instance of the Protocol class. To become
6718 usable at runtime, the 'isa' pointer of the instance need to be
6719 fixed up at runtime by the runtime library, to point to the
6720 actual 'Protocol' class. */
6721
6722 /* For the GNU runtime, put the static Protocol instance in the list
6723 of statically allocated instances, so that we make sure that its
6724 'isa' pointer is fixed up at runtime by the GNU runtime library
6725 to point to the Protocol class (at runtime, when loading the
6726 module, the GNU runtime library loops on the statically allocated
6727 instances (as found in the defs field in objc_symtab) and fixups
6728 all the 'isa' pointers of those objects). */
6729 if (! flag_next_runtime)
6730 {
6731 /* This type is a struct containing the fields of a Protocol
6732 object. (Cfr. objc_protocol_type instead is the type of a pointer
6733 to such a struct). */
6734 tree protocol_struct_type = xref_tag
6735 (RECORD_TYPE, get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
6736 tree *chain;
6737
6738 /* Look for the list of Protocol statically allocated instances
6739 to fixup at runtime. Create a new list to hold Protocol
6740 statically allocated instances, if the list is not found. At
6741 present there is only another list, holding NSConstantString
6742 static instances to be fixed up at runtime. */
6743 for (chain = &objc_static_instances;
6744 *chain && TREE_VALUE (*chain) != protocol_struct_type;
6745 chain = &TREE_CHAIN (*chain));
6746 if (!*chain)
6747 {
6748 *chain = tree_cons (NULL_TREE, protocol_struct_type, NULL_TREE);
6749 add_objc_string (OBJC_TYPE_NAME (protocol_struct_type),
6750 class_names);
6751 }
6752
6753 /* Add this statically allocated instance to the Protocol list. */
6754 TREE_PURPOSE (*chain) = tree_cons (NULL_TREE,
6755 PROTOCOL_FORWARD_DECL (p),
6756 TREE_PURPOSE (*chain));
6757 }
6758
6759
6760 return expr;
6761 }
6762
6763 /* This function is called by the parser when a @selector() expression
6764 is found, in order to compile it. It is only called by the parser
6765 and only to compile a @selector(). LOC is the location of the
6766 @selector. */
6767 tree
6768 objc_build_selector_expr (location_t loc, tree selnamelist)
6769 {
6770 tree selname;
6771
6772 /* Obtain the full selector name. */
6773 if (TREE_CODE (selnamelist) == IDENTIFIER_NODE)
6774 /* A unary selector. */
6775 selname = selnamelist;
6776 else if (TREE_CODE (selnamelist) == TREE_LIST)
6777 selname = build_keyword_selector (selnamelist);
6778 else
6779 abort ();
6780
6781 /* If we are required to check @selector() expressions as they
6782 are found, check that the selector has been declared. */
6783 if (warn_undeclared_selector)
6784 {
6785 /* Look the selector up in the list of all known class and
6786 instance methods (up to this line) to check that the selector
6787 exists. */
6788 hash hsh;
6789
6790 /* First try with instance methods. */
6791 hsh = hash_lookup (nst_method_hash_list, selname);
6792
6793 /* If not found, try with class methods. */
6794 if (!hsh)
6795 {
6796 hsh = hash_lookup (cls_method_hash_list, selname);
6797 }
6798
6799 /* If still not found, print out a warning. */
6800 if (!hsh)
6801 {
6802 warning (0, "undeclared selector %qE", selname);
6803 }
6804 }
6805
6806
6807 if (flag_typed_selectors)
6808 return build_typed_selector_reference (loc, selname, 0);
6809 else
6810 return build_selector_reference (loc, selname);
6811 }
6812
6813 /* This is used to implement @encode(). See gcc/doc/objc.texi,
6814 section '@encode'. */
6815 tree
6816 objc_build_encode_expr (tree type)
6817 {
6818 tree result;
6819 const char *string;
6820
6821 encode_type (type, obstack_object_size (&util_obstack),
6822 OBJC_ENCODE_INLINE_DEFS);
6823 obstack_1grow (&util_obstack, 0); /* null terminate string */
6824 string = XOBFINISH (&util_obstack, const char *);
6825
6826 /* Synthesize a string that represents the encoded struct/union. */
6827 result = my_build_string (strlen (string) + 1, string);
6828 obstack_free (&util_obstack, util_firstobj);
6829 return result;
6830 }
6831
6832 static tree
6833 build_ivar_reference (tree id)
6834 {
6835 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
6836 {
6837 /* Historically, a class method that produced objects (factory
6838 method) would assign `self' to the instance that it
6839 allocated. This would effectively turn the class method into
6840 an instance method. Following this assignment, the instance
6841 variables could be accessed. That practice, while safe,
6842 violates the simple rule that a class method should not refer
6843 to an instance variable. It's better to catch the cases
6844 where this is done unknowingly than to support the above
6845 paradigm. */
6846 warning (0, "instance variable %qE accessed in class method",
6847 id);
6848 self_decl = convert (objc_instance_type, self_decl); /* cast */
6849 }
6850
6851 return objc_build_component_ref (build_indirect_ref (input_location,
6852 self_decl, RO_ARROW),
6853 id);
6854 }
6855 \f
6856 /* Compute a hash value for a given method SEL_NAME. */
6857
6858 static size_t
6859 hash_func (tree sel_name)
6860 {
6861 const unsigned char *s
6862 = (const unsigned char *)IDENTIFIER_POINTER (sel_name);
6863 size_t h = 0;
6864
6865 while (*s)
6866 h = h * 67 + *s++ - 113;
6867 return h;
6868 }
6869
6870 static void
6871 hash_init (void)
6872 {
6873 nst_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
6874 cls_method_hash_list = ggc_alloc_cleared_vec_hash (SIZEHASHTABLE);
6875
6876 /* Initialize the hash table used to hold the constant string objects. */
6877 string_htab = htab_create_ggc (31, string_hash,
6878 string_eq, NULL);
6879
6880 /* Initialize the hash table used to hold EH-volatilized types. */
6881 volatilized_htab = htab_create_ggc (31, volatilized_hash,
6882 volatilized_eq, NULL);
6883 }
6884
6885 /* WARNING!!!! hash_enter is called with a method, and will peek
6886 inside to find its selector! But hash_lookup is given a selector
6887 directly, and looks for the selector that's inside the found
6888 entry's key (method) for comparison. */
6889
6890 static void
6891 hash_enter (hash *hashlist, tree method)
6892 {
6893 hash obj;
6894 int slot = hash_func (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
6895
6896 obj = ggc_alloc_hashed_entry ();
6897 obj->list = 0;
6898 obj->next = hashlist[slot];
6899 obj->key = method;
6900
6901 hashlist[slot] = obj; /* append to front */
6902 }
6903
6904 static hash
6905 hash_lookup (hash *hashlist, tree sel_name)
6906 {
6907 hash target;
6908
6909 target = hashlist[hash_func (sel_name) % SIZEHASHTABLE];
6910
6911 while (target)
6912 {
6913 if (sel_name == METHOD_SEL_NAME (target->key))
6914 return target;
6915
6916 target = target->next;
6917 }
6918 return 0;
6919 }
6920
6921 static void
6922 hash_add_attr (hash entry, tree value)
6923 {
6924 attr obj;
6925
6926 obj = ggc_alloc_hashed_attribute ();
6927 obj->next = entry->list;
6928 obj->value = value;
6929
6930 entry->list = obj; /* append to front */
6931 }
6932 \f
6933 static tree
6934 lookup_method (tree mchain, tree method)
6935 {
6936 tree key;
6937
6938 if (TREE_CODE (method) == IDENTIFIER_NODE)
6939 key = method;
6940 else
6941 key = METHOD_SEL_NAME (method);
6942
6943 while (mchain)
6944 {
6945 if (METHOD_SEL_NAME (mchain) == key)
6946 return mchain;
6947
6948 mchain = DECL_CHAIN (mchain);
6949 }
6950 return NULL_TREE;
6951 }
6952
6953 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance method
6954 in INTERFACE, along with any categories and protocols attached thereto.
6955 If method is not found, and the OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS,
6956 recursively examine the INTERFACE's superclass. If OBJC_LOOKUP_CLASS is
6957 set, OBJC_LOOKUP_NO_SUPER is cleared, and no suitable class method could
6958 be found in INTERFACE or any of its superclasses, look for an _instance_
6959 method of the same name in the root class as a last resort.
6960
6961 If a suitable method cannot be found, return NULL_TREE. */
6962
6963 static tree
6964 lookup_method_static (tree interface, tree ident, int flags)
6965 {
6966 tree meth = NULL_TREE, root_inter = NULL_TREE;
6967 tree inter = interface;
6968 int is_class = (flags & OBJC_LOOKUP_CLASS);
6969 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
6970
6971 while (inter)
6972 {
6973 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
6974 tree category = inter;
6975
6976 /* First, look up the method in the class itself. */
6977 if ((meth = lookup_method (chain, ident)))
6978 return meth;
6979
6980 /* Failing that, look for the method in each category of the class. */
6981 while ((category = CLASS_CATEGORY_LIST (category)))
6982 {
6983 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
6984
6985 /* Check directly in each category. */
6986 if ((meth = lookup_method (chain, ident)))
6987 return meth;
6988
6989 /* Failing that, check in each category's protocols. */
6990 if (CLASS_PROTOCOL_LIST (category))
6991 {
6992 if ((meth = (lookup_method_in_protocol_list
6993 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
6994 return meth;
6995 }
6996 }
6997
6998 /* If not found in categories, check in protocols of the main class. */
6999 if (CLASS_PROTOCOL_LIST (inter))
7000 {
7001 if ((meth = (lookup_method_in_protocol_list
7002 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
7003 return meth;
7004 }
7005
7006 /* If we were instructed not to look in superclasses, don't. */
7007 if (no_superclasses)
7008 return NULL_TREE;
7009
7010 /* Failing that, climb up the inheritance hierarchy. */
7011 root_inter = inter;
7012 inter = lookup_interface (CLASS_SUPER_NAME (inter));
7013 }
7014 while (inter);
7015
7016 /* If no class (factory) method was found, check if an _instance_
7017 method of the same name exists in the root class. This is what
7018 the Objective-C runtime will do. If an instance method was not
7019 found, return 0. */
7020 return is_class ? lookup_method_static (root_inter, ident, 0): NULL_TREE;
7021 }
7022
7023 /* Add the method to the hash list if it doesn't contain an identical
7024 method already. */
7025
7026 static void
7027 add_method_to_hash_list (hash *hash_list, tree method)
7028 {
7029 hash hsh;
7030
7031 if (!(hsh = hash_lookup (hash_list, METHOD_SEL_NAME (method))))
7032 {
7033 /* Install on a global chain. */
7034 hash_enter (hash_list, method);
7035 }
7036 else
7037 {
7038 /* Check types against those; if different, add to a list. */
7039 attr loop;
7040 int already_there = comp_proto_with_proto (method, hsh->key, 1);
7041 for (loop = hsh->list; !already_there && loop; loop = loop->next)
7042 already_there |= comp_proto_with_proto (method, loop->value, 1);
7043 if (!already_there)
7044 hash_add_attr (hsh, method);
7045 }
7046 }
7047
7048 static tree
7049 objc_add_method (tree klass, tree method, int is_class)
7050 {
7051 tree mth;
7052
7053 if (!(mth = lookup_method (is_class
7054 ? CLASS_CLS_METHODS (klass)
7055 : CLASS_NST_METHODS (klass), method)))
7056 {
7057 /* put method on list in reverse order */
7058 if (is_class)
7059 {
7060 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
7061 CLASS_CLS_METHODS (klass) = method;
7062 }
7063 else
7064 {
7065 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
7066 CLASS_NST_METHODS (klass) = method;
7067 }
7068 }
7069 else
7070 {
7071 /* When processing an @interface for a class or category, give hard
7072 errors on methods with identical selectors but differing argument
7073 and/or return types. We do not do this for @implementations, because
7074 C/C++ will do it for us (i.e., there will be duplicate function
7075 definition errors). */
7076 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
7077 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE)
7078 && !comp_proto_with_proto (method, mth, 1))
7079 error ("duplicate declaration of method %<%c%E%>",
7080 is_class ? '+' : '-',
7081 METHOD_SEL_NAME (mth));
7082 }
7083
7084 if (is_class)
7085 add_method_to_hash_list (cls_method_hash_list, method);
7086 else
7087 {
7088 add_method_to_hash_list (nst_method_hash_list, method);
7089
7090 /* Instance methods in root classes (and categories thereof)
7091 may act as class methods as a last resort. We also add
7092 instance methods listed in @protocol declarations to
7093 the class hash table, on the assumption that @protocols
7094 may be adopted by root classes or categories. */
7095 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
7096 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
7097 klass = lookup_interface (CLASS_NAME (klass));
7098
7099 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
7100 || !CLASS_SUPER_NAME (klass))
7101 add_method_to_hash_list (cls_method_hash_list, method);
7102 }
7103
7104 return method;
7105 }
7106
7107 static tree
7108 add_class (tree class_name, tree name)
7109 {
7110 struct interface_tuple **slot;
7111
7112 /* Put interfaces on list in reverse order. */
7113 TREE_CHAIN (class_name) = interface_chain;
7114 interface_chain = class_name;
7115
7116 if (interface_htab == NULL)
7117 interface_htab = htab_create_ggc (31, hash_interface, eq_interface, NULL);
7118 slot = (struct interface_tuple **)
7119 htab_find_slot_with_hash (interface_htab, name,
7120 IDENTIFIER_HASH_VALUE (name),
7121 INSERT);
7122 if (!*slot)
7123 {
7124 *slot = ggc_alloc_cleared_interface_tuple ();
7125 (*slot)->id = name;
7126 }
7127 (*slot)->class_name = class_name;
7128
7129 return interface_chain;
7130 }
7131
7132 static void
7133 add_category (tree klass, tree category)
7134 {
7135 /* Put categories on list in reverse order. */
7136 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
7137
7138 if (cat)
7139 {
7140 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
7141 CLASS_NAME (klass),
7142 CLASS_SUPER_NAME (category));
7143 }
7144 else
7145 {
7146 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
7147 CLASS_CATEGORY_LIST (klass) = category;
7148 }
7149 }
7150
7151 /* Called after parsing each instance variable declaration. Necessary to
7152 preserve typedefs and implement public/private...
7153
7154 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
7155
7156 static tree
7157 add_instance_variable (tree klass, int visibility, tree field_decl)
7158 {
7159 tree field_type = TREE_TYPE (field_decl);
7160 const char *ivar_name = DECL_NAME (field_decl)
7161 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)))
7162 : _("<unnamed>");
7163
7164 #ifdef OBJCPLUS
7165 if (TREE_CODE (field_type) == REFERENCE_TYPE)
7166 {
7167 error ("illegal reference type specified for instance variable %qs",
7168 ivar_name);
7169 /* Return class as is without adding this ivar. */
7170 return klass;
7171 }
7172 #endif
7173
7174 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
7175 || TYPE_SIZE (field_type) == error_mark_node)
7176 /* 'type[0]' is allowed, but 'type[]' is not! */
7177 {
7178 error ("instance variable %qs has unknown size", ivar_name);
7179 /* Return class as is without adding this ivar. */
7180 return klass;
7181 }
7182
7183 #ifdef OBJCPLUS
7184 /* Check if the ivar being added has a non-POD C++ type. If so, we will
7185 need to either (1) warn the user about it or (2) generate suitable
7186 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
7187 methods (if '-fobjc-call-cxx-cdtors' was specified). */
7188 if (MAYBE_CLASS_TYPE_P (field_type)
7189 && (TYPE_NEEDS_CONSTRUCTING (field_type)
7190 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
7191 || TYPE_POLYMORPHIC_P (field_type)))
7192 {
7193 tree type_name = OBJC_TYPE_NAME (field_type);
7194
7195 if (flag_objc_call_cxx_cdtors)
7196 {
7197 /* Since the ObjC runtime will be calling the constructors and
7198 destructors for us, the only thing we can't handle is the lack
7199 of a default constructor. */
7200 if (TYPE_NEEDS_CONSTRUCTING (field_type)
7201 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
7202 {
7203 warning (0, "type %qE has no default constructor to call",
7204 type_name);
7205
7206 /* If we cannot call a constructor, we should also avoid
7207 calling the destructor, for symmetry. */
7208 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
7209 warning (0, "destructor for %qE shall not be run either",
7210 type_name);
7211 }
7212 }
7213 else
7214 {
7215 static bool warn_cxx_ivars = false;
7216
7217 if (TYPE_POLYMORPHIC_P (field_type))
7218 {
7219 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
7220 initialize them. */
7221 error ("type %qE has virtual member functions", type_name);
7222 error ("illegal aggregate type %qE specified "
7223 "for instance variable %qs",
7224 type_name, ivar_name);
7225 /* Return class as is without adding this ivar. */
7226 return klass;
7227 }
7228
7229 /* User-defined constructors and destructors are not known to Obj-C
7230 and hence will not be called. This may or may not be a problem. */
7231 if (TYPE_NEEDS_CONSTRUCTING (field_type))
7232 warning (0, "type %qE has a user-defined constructor", type_name);
7233 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
7234 warning (0, "type %qE has a user-defined destructor", type_name);
7235
7236 if (!warn_cxx_ivars)
7237 {
7238 warning (0, "C++ constructors and destructors will not "
7239 "be invoked for Objective-C fields");
7240 warn_cxx_ivars = true;
7241 }
7242 }
7243 }
7244 #endif
7245
7246 /* Overload the public attribute, it is not used for FIELD_DECLs. */
7247 switch (visibility)
7248 {
7249 case 0:
7250 TREE_PUBLIC (field_decl) = 0;
7251 TREE_PRIVATE (field_decl) = 0;
7252 TREE_PROTECTED (field_decl) = 1;
7253 break;
7254
7255 case 1:
7256 TREE_PUBLIC (field_decl) = 1;
7257 TREE_PRIVATE (field_decl) = 0;
7258 TREE_PROTECTED (field_decl) = 0;
7259 break;
7260
7261 case 2:
7262 TREE_PUBLIC (field_decl) = 0;
7263 TREE_PRIVATE (field_decl) = 1;
7264 TREE_PROTECTED (field_decl) = 0;
7265 break;
7266
7267 }
7268
7269 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
7270
7271 return klass;
7272 }
7273 \f
7274 static tree
7275 is_ivar (tree decl_chain, tree ident)
7276 {
7277 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7278 if (DECL_NAME (decl_chain) == ident)
7279 return decl_chain;
7280 return NULL_TREE;
7281 }
7282
7283 /* True if the ivar is private and we are not in its implementation. */
7284
7285 static int
7286 is_private (tree decl)
7287 {
7288 return (TREE_PRIVATE (decl)
7289 && ! is_ivar (CLASS_IVARS (implementation_template),
7290 DECL_NAME (decl)));
7291 }
7292
7293 /* We have an instance variable reference;, check to see if it is public. */
7294
7295 int
7296 objc_is_public (tree expr, tree identifier)
7297 {
7298 tree basetype, decl;
7299
7300 #ifdef OBJCPLUS
7301 if (processing_template_decl)
7302 return 1;
7303 #endif
7304
7305 if (TREE_TYPE (expr) == error_mark_node)
7306 return 1;
7307
7308 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7309
7310 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
7311 {
7312 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
7313 {
7314 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
7315
7316 if (!klass)
7317 {
7318 error ("cannot find interface declaration for %qE",
7319 OBJC_TYPE_NAME (basetype));
7320 return 0;
7321 }
7322
7323 if ((decl = is_ivar (get_class_ivars (klass, true), identifier)))
7324 {
7325 if (TREE_PUBLIC (decl))
7326 return 1;
7327
7328 /* Important difference between the Stepstone translator:
7329 all instance variables should be public within the context
7330 of the implementation. */
7331 if (objc_implementation_context
7332 && ((TREE_CODE (objc_implementation_context)
7333 == CLASS_IMPLEMENTATION_TYPE)
7334 || (TREE_CODE (objc_implementation_context)
7335 == CATEGORY_IMPLEMENTATION_TYPE)))
7336 {
7337 tree curtype = TYPE_MAIN_VARIANT
7338 (CLASS_STATIC_TEMPLATE
7339 (implementation_template));
7340
7341 if (basetype == curtype
7342 || DERIVED_FROM_P (basetype, curtype))
7343 {
7344 int priv = is_private (decl);
7345
7346 if (priv)
7347 error ("instance variable %qE is declared private",
7348 DECL_NAME (decl));
7349
7350 return !priv;
7351 }
7352 }
7353
7354 /* The 2.95.2 compiler sometimes allowed C functions to access
7355 non-@public ivars. We will let this slide for now... */
7356 if (!objc_method_context)
7357 {
7358 warning (0, "instance variable %qE is %s; "
7359 "this will be a hard error in the future",
7360 identifier,
7361 TREE_PRIVATE (decl) ? "@private" : "@protected");
7362 return 1;
7363 }
7364
7365 error ("instance variable %qE is declared %s",
7366 identifier,
7367 TREE_PRIVATE (decl) ? "private" : "protected");
7368 return 0;
7369 }
7370 }
7371 }
7372
7373 return 1;
7374 }
7375 \f
7376 /* Make sure all entries in CHAIN are also in LIST. */
7377
7378 static int
7379 check_methods (tree chain, tree list, int mtype)
7380 {
7381 int first = 1;
7382
7383 while (chain)
7384 {
7385 if (!lookup_method (list, chain))
7386 {
7387 if (first)
7388 {
7389 if (TREE_CODE (objc_implementation_context)
7390 == CLASS_IMPLEMENTATION_TYPE)
7391 warning (0, "incomplete implementation of class %qE",
7392 CLASS_NAME (objc_implementation_context));
7393 else if (TREE_CODE (objc_implementation_context)
7394 == CATEGORY_IMPLEMENTATION_TYPE)
7395 warning (0, "incomplete implementation of category %qE",
7396 CLASS_SUPER_NAME (objc_implementation_context));
7397 first = 0;
7398 }
7399
7400 warning (0, "method definition for %<%c%E%> not found",
7401 mtype, METHOD_SEL_NAME (chain));
7402 }
7403
7404 chain = DECL_CHAIN (chain);
7405 }
7406
7407 return first;
7408 }
7409
7410 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
7411
7412 static int
7413 conforms_to_protocol (tree klass, tree protocol)
7414 {
7415 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
7416 {
7417 tree p = CLASS_PROTOCOL_LIST (klass);
7418 while (p && TREE_VALUE (p) != protocol)
7419 p = TREE_CHAIN (p);
7420
7421 if (!p)
7422 {
7423 tree super = (CLASS_SUPER_NAME (klass)
7424 ? lookup_interface (CLASS_SUPER_NAME (klass))
7425 : NULL_TREE);
7426 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
7427 if (!tmp)
7428 return 0;
7429 }
7430 }
7431
7432 return 1;
7433 }
7434
7435 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
7436 CONTEXT. This is one of two mechanisms to check protocol integrity. */
7437
7438 static int
7439 check_methods_accessible (tree chain, tree context, int mtype)
7440 {
7441 int first = 1;
7442 tree list;
7443 tree base_context = context;
7444
7445 while (chain)
7446 {
7447 context = base_context;
7448 while (context)
7449 {
7450 if (mtype == '+')
7451 list = CLASS_CLS_METHODS (context);
7452 else
7453 list = CLASS_NST_METHODS (context);
7454
7455 if (lookup_method (list, chain))
7456 break;
7457
7458 else if (TREE_CODE (context) == CLASS_IMPLEMENTATION_TYPE
7459 || TREE_CODE (context) == CLASS_INTERFACE_TYPE)
7460 context = (CLASS_SUPER_NAME (context)
7461 ? lookup_interface (CLASS_SUPER_NAME (context))
7462 : NULL_TREE);
7463
7464 else if (TREE_CODE (context) == CATEGORY_IMPLEMENTATION_TYPE
7465 || TREE_CODE (context) == CATEGORY_INTERFACE_TYPE)
7466 context = (CLASS_NAME (context)
7467 ? lookup_interface (CLASS_NAME (context))
7468 : NULL_TREE);
7469 else
7470 abort ();
7471 }
7472
7473 if (context == NULL_TREE)
7474 {
7475 if (first)
7476 {
7477 if (TREE_CODE (objc_implementation_context)
7478 == CLASS_IMPLEMENTATION_TYPE)
7479 warning (0, "incomplete implementation of class %qE",
7480 CLASS_NAME (objc_implementation_context));
7481 else if (TREE_CODE (objc_implementation_context)
7482 == CATEGORY_IMPLEMENTATION_TYPE)
7483 warning (0, "incomplete implementation of category %qE",
7484 CLASS_SUPER_NAME (objc_implementation_context));
7485 first = 0;
7486 }
7487 warning (0, "method definition for %<%c%E%> not found",
7488 mtype, METHOD_SEL_NAME (chain));
7489 }
7490
7491 chain = TREE_CHAIN (chain); /* next method... */
7492 }
7493 return first;
7494 }
7495
7496 /* Check whether the current interface (accessible via
7497 'objc_implementation_context') actually implements protocol P, along
7498 with any protocols that P inherits. */
7499
7500 static void
7501 check_protocol (tree p, const char *type, tree name)
7502 {
7503 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
7504 {
7505 int f1, f2;
7506
7507 /* Ensure that all protocols have bodies! */
7508 if (warn_protocol)
7509 {
7510 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
7511 CLASS_CLS_METHODS (objc_implementation_context),
7512 '+');
7513 f2 = check_methods (PROTOCOL_NST_METHODS (p),
7514 CLASS_NST_METHODS (objc_implementation_context),
7515 '-');
7516 }
7517 else
7518 {
7519 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
7520 objc_implementation_context,
7521 '+');
7522 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
7523 objc_implementation_context,
7524 '-');
7525 }
7526
7527 if (!f1 || !f2)
7528 warning (0, "%s %qE does not fully implement the %qE protocol",
7529 type, name, PROTOCOL_NAME (p));
7530 }
7531
7532 /* Check protocols recursively. */
7533 if (PROTOCOL_LIST (p))
7534 {
7535 tree subs = PROTOCOL_LIST (p);
7536 tree super_class =
7537 lookup_interface (CLASS_SUPER_NAME (implementation_template));
7538
7539 while (subs)
7540 {
7541 tree sub = TREE_VALUE (subs);
7542
7543 /* If the superclass does not conform to the protocols
7544 inherited by P, then we must! */
7545 if (!super_class || !conforms_to_protocol (super_class, sub))
7546 check_protocol (sub, type, name);
7547 subs = TREE_CHAIN (subs);
7548 }
7549 }
7550 }
7551
7552 /* Check whether the current interface (accessible via
7553 'objc_implementation_context') actually implements the protocols listed
7554 in PROTO_LIST. */
7555
7556 static void
7557 check_protocols (tree proto_list, const char *type, tree name)
7558 {
7559 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
7560 {
7561 tree p = TREE_VALUE (proto_list);
7562
7563 check_protocol (p, type, name);
7564 }
7565 }
7566 \f
7567 /* Make sure that the class CLASS_NAME is defined
7568 CODE says which kind of thing CLASS_NAME ought to be.
7569 It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
7570 CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE. */
7571
7572 static tree
7573 start_class (enum tree_code code, tree class_name, tree super_name,
7574 tree protocol_list)
7575 {
7576 tree klass, decl;
7577
7578 #ifdef OBJCPLUS
7579 if (current_namespace != global_namespace) {
7580 error ("Objective-C declarations may only appear in global scope");
7581 }
7582 #endif /* OBJCPLUS */
7583
7584 if (objc_implementation_context)
7585 {
7586 warning (0, "%<@end%> missing in implementation context");
7587 finish_class (objc_implementation_context);
7588 objc_ivar_chain = NULL_TREE;
7589 objc_implementation_context = NULL_TREE;
7590 }
7591
7592 klass = make_node (code);
7593 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
7594
7595 /* Check for existence of the super class, if one was specified. Note
7596 that we must have seen an @interface, not just a @class. If we
7597 are looking at a @compatibility_alias, traverse it first. */
7598 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
7599 && super_name)
7600 {
7601 tree super = objc_is_class_name (super_name);
7602
7603 if (!super || !lookup_interface (super))
7604 {
7605 error ("cannot find interface declaration for %qE, superclass of %qE",
7606 super ? super : super_name,
7607 class_name);
7608 super_name = NULL_TREE;
7609 }
7610 else
7611 super_name = super;
7612 }
7613
7614 CLASS_NAME (klass) = class_name;
7615 CLASS_SUPER_NAME (klass) = super_name;
7616 CLASS_CLS_METHODS (klass) = NULL_TREE;
7617
7618 if (! objc_is_class_name (class_name)
7619 && (decl = lookup_name (class_name)))
7620 {
7621 error ("%qE redeclared as different kind of symbol",
7622 class_name);
7623 error ("previous declaration of %q+D",
7624 decl);
7625 }
7626
7627 if (code == CLASS_IMPLEMENTATION_TYPE)
7628 {
7629 {
7630 tree chain;
7631
7632 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
7633 if (TREE_VALUE (chain) == class_name)
7634 {
7635 error ("reimplementation of class %qE",
7636 class_name);
7637 return error_mark_node;
7638 }
7639 implemented_classes = tree_cons (NULL_TREE, class_name,
7640 implemented_classes);
7641 }
7642
7643 /* Reset for multiple classes per file. */
7644 method_slot = 0;
7645
7646 objc_implementation_context = klass;
7647
7648 /* Lookup the interface for this implementation. */
7649
7650 if (!(implementation_template = lookup_interface (class_name)))
7651 {
7652 warning (0, "cannot find interface declaration for %qE",
7653 class_name);
7654 add_class (implementation_template = objc_implementation_context,
7655 class_name);
7656 }
7657
7658 /* If a super class has been specified in the implementation,
7659 insure it conforms to the one specified in the interface. */
7660
7661 if (super_name
7662 && (super_name != CLASS_SUPER_NAME (implementation_template)))
7663 {
7664 tree previous_name = CLASS_SUPER_NAME (implementation_template);
7665 error ("conflicting super class name %qE",
7666 super_name);
7667 if (previous_name)
7668 error ("previous declaration of %qE", previous_name);
7669 else
7670 error ("previous declaration");
7671 }
7672
7673 else if (! super_name)
7674 {
7675 CLASS_SUPER_NAME (objc_implementation_context)
7676 = CLASS_SUPER_NAME (implementation_template);
7677 }
7678 }
7679
7680 else if (code == CLASS_INTERFACE_TYPE)
7681 {
7682 if (lookup_interface (class_name))
7683 #ifdef OBJCPLUS
7684 error ("duplicate interface declaration for class %qE",
7685 #else
7686 warning (0, "duplicate interface declaration for class %qE",
7687 #endif
7688 class_name);
7689 else
7690 add_class (klass, class_name);
7691
7692 if (protocol_list)
7693 CLASS_PROTOCOL_LIST (klass)
7694 = lookup_and_install_protocols (protocol_list);
7695 }
7696
7697 else if (code == CATEGORY_INTERFACE_TYPE)
7698 {
7699 tree class_category_is_assoc_with;
7700
7701 /* For a category, class_name is really the name of the class that
7702 the following set of methods will be associated with. We must
7703 find the interface so that can derive the objects template. */
7704
7705 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
7706 {
7707 error ("cannot find interface declaration for %qE",
7708 class_name);
7709 exit (FATAL_EXIT_CODE);
7710 }
7711 else
7712 add_category (class_category_is_assoc_with, klass);
7713
7714 if (protocol_list)
7715 CLASS_PROTOCOL_LIST (klass)
7716 = lookup_and_install_protocols (protocol_list);
7717 }
7718
7719 else if (code == CATEGORY_IMPLEMENTATION_TYPE)
7720 {
7721 /* Reset for multiple classes per file. */
7722 method_slot = 0;
7723
7724 objc_implementation_context = klass;
7725
7726 /* For a category, class_name is really the name of the class that
7727 the following set of methods will be associated with. We must
7728 find the interface so that can derive the objects template. */
7729
7730 if (!(implementation_template = lookup_interface (class_name)))
7731 {
7732 error ("cannot find interface declaration for %qE",
7733 class_name);
7734 exit (FATAL_EXIT_CODE);
7735 }
7736 }
7737 return klass;
7738 }
7739
7740 static tree
7741 continue_class (tree klass)
7742 {
7743 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE
7744 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
7745 {
7746 struct imp_entry *imp_entry;
7747
7748 /* Check consistency of the instance variables. */
7749
7750 if (CLASS_RAW_IVARS (klass))
7751 check_ivars (implementation_template, klass);
7752
7753 /* code generation */
7754
7755 #ifdef OBJCPLUS
7756 push_lang_context (lang_name_c);
7757 #endif
7758
7759 build_private_template (implementation_template);
7760 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7761 objc_instance_type = build_pointer_type (uprivate_record);
7762
7763 imp_entry = ggc_alloc_imp_entry ();
7764
7765 imp_entry->next = imp_list;
7766 imp_entry->imp_context = klass;
7767 imp_entry->imp_template = implementation_template;
7768
7769 synth_forward_declarations ();
7770 imp_entry->class_decl = UOBJC_CLASS_decl;
7771 imp_entry->meta_decl = UOBJC_METACLASS_decl;
7772 imp_entry->has_cxx_cdtors = 0;
7773
7774 /* Append to front and increment count. */
7775 imp_list = imp_entry;
7776 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7777 imp_count++;
7778 else
7779 cat_count++;
7780
7781 #ifdef OBJCPLUS
7782 pop_lang_context ();
7783 #endif /* OBJCPLUS */
7784
7785 return get_class_ivars (implementation_template, true);
7786 }
7787
7788 else if (TREE_CODE (klass) == CLASS_INTERFACE_TYPE)
7789 {
7790 #ifdef OBJCPLUS
7791 push_lang_context (lang_name_c);
7792 #endif /* OBJCPLUS */
7793
7794 objc_collecting_ivars = 1;
7795 build_private_template (klass);
7796 objc_collecting_ivars = 0;
7797
7798 #ifdef OBJCPLUS
7799 pop_lang_context ();
7800 #endif /* OBJCPLUS */
7801
7802 return NULL_TREE;
7803 }
7804
7805 else
7806 return error_mark_node;
7807 }
7808
7809 /* This is called once we see the "@end" in an interface/implementation. */
7810
7811 static void
7812 finish_class (tree klass)
7813 {
7814 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7815 {
7816 /* All code generation is done in finish_objc. */
7817
7818 if (implementation_template != objc_implementation_context)
7819 {
7820 /* Ensure that all method listed in the interface contain bodies. */
7821 check_methods (CLASS_CLS_METHODS (implementation_template),
7822 CLASS_CLS_METHODS (objc_implementation_context), '+');
7823 check_methods (CLASS_NST_METHODS (implementation_template),
7824 CLASS_NST_METHODS (objc_implementation_context), '-');
7825
7826 if (CLASS_PROTOCOL_LIST (implementation_template))
7827 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7828 "class",
7829 CLASS_NAME (objc_implementation_context));
7830 }
7831 }
7832
7833 else if (TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
7834 {
7835 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7836
7837 if (category)
7838 {
7839 /* Ensure all method listed in the interface contain bodies. */
7840 check_methods (CLASS_CLS_METHODS (category),
7841 CLASS_CLS_METHODS (objc_implementation_context), '+');
7842 check_methods (CLASS_NST_METHODS (category),
7843 CLASS_NST_METHODS (objc_implementation_context), '-');
7844
7845 if (CLASS_PROTOCOL_LIST (category))
7846 check_protocols (CLASS_PROTOCOL_LIST (category),
7847 "category",
7848 CLASS_SUPER_NAME (objc_implementation_context));
7849 }
7850 }
7851 }
7852
7853 static tree
7854 add_protocol (tree protocol)
7855 {
7856 /* Put protocol on list in reverse order. */
7857 TREE_CHAIN (protocol) = protocol_chain;
7858 protocol_chain = protocol;
7859 return protocol_chain;
7860 }
7861
7862 static tree
7863 lookup_protocol (tree ident)
7864 {
7865 tree chain;
7866
7867 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
7868 if (ident == PROTOCOL_NAME (chain))
7869 return chain;
7870
7871 return NULL_TREE;
7872 }
7873
7874 /* This function forward declares the protocols named by NAMES. If
7875 they are already declared or defined, the function has no effect. */
7876
7877 void
7878 objc_declare_protocols (tree names)
7879 {
7880 tree list;
7881
7882 #ifdef OBJCPLUS
7883 if (current_namespace != global_namespace) {
7884 error ("Objective-C declarations may only appear in global scope");
7885 }
7886 #endif /* OBJCPLUS */
7887
7888 for (list = names; list; list = TREE_CHAIN (list))
7889 {
7890 tree name = TREE_VALUE (list);
7891
7892 if (lookup_protocol (name) == NULL_TREE)
7893 {
7894 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
7895
7896 TYPE_LANG_SLOT_1 (protocol)
7897 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
7898 PROTOCOL_NAME (protocol) = name;
7899 PROTOCOL_LIST (protocol) = NULL_TREE;
7900 add_protocol (protocol);
7901 PROTOCOL_DEFINED (protocol) = 0;
7902 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
7903 }
7904 }
7905 }
7906
7907 static tree
7908 start_protocol (enum tree_code code, tree name, tree list)
7909 {
7910 tree protocol;
7911
7912 #ifdef OBJCPLUS
7913 if (current_namespace != global_namespace) {
7914 error ("Objective-C declarations may only appear in global scope");
7915 }
7916 #endif /* OBJCPLUS */
7917
7918 protocol = lookup_protocol (name);
7919
7920 if (!protocol)
7921 {
7922 protocol = make_node (code);
7923 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
7924
7925 PROTOCOL_NAME (protocol) = name;
7926 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
7927 add_protocol (protocol);
7928 PROTOCOL_DEFINED (protocol) = 1;
7929 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
7930
7931 check_protocol_recursively (protocol, list);
7932 }
7933 else if (! PROTOCOL_DEFINED (protocol))
7934 {
7935 PROTOCOL_DEFINED (protocol) = 1;
7936 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list);
7937
7938 check_protocol_recursively (protocol, list);
7939 }
7940 else
7941 {
7942 warning (0, "duplicate declaration for protocol %qE",
7943 name);
7944 }
7945 return protocol;
7946 }
7947
7948 \f
7949 /* "Encode" a data type into a string, which grows in util_obstack.
7950
7951 The format is described in gcc/doc/objc.texi, section 'Type
7952 encoding'.
7953
7954 Most of the encode_xxx functions have a 'type' argument, which is
7955 the type to encode, and an integer 'curtype' argument, which is the
7956 index in the encoding string of the beginning of the encoding of
7957 the current type, and allows you to find what characters have
7958 already been written for the current type (they are the ones in the
7959 current encoding string starting from 'curtype').
7960
7961 For example, if we are encoding a method which returns 'int' and
7962 takes a 'char **' argument, then when we get to the point of
7963 encoding the 'char **' argument, the encoded string already
7964 contains 'i12@0:4' (assuming a pointer size of 4 bytes). So,
7965 'curtype' will be set to 7 when starting to encode 'char **'.
7966 During the whole of the encoding of 'char **', 'curtype' will be
7967 fixed at 7, so the routine encoding the second pointer can find out
7968 that it's actually encoding a pointer to a pointer by looking
7969 backwards at what has already been encoded for the current type,
7970 and seeing there is a "^" (meaning a pointer) in there.
7971 */
7972
7973
7974 /* Encode type qualifiers encodes one of the "PQ" Objective-C
7975 keywords, ie 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway'.
7976 'const', instead, is encoded directly as part of the type.
7977 */
7978
7979 static void
7980 encode_type_qualifiers (tree declspecs)
7981 {
7982 tree spec;
7983
7984 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
7985 {
7986 /* FIXME: Shouldn't we use token->keyword here ? */
7987 if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
7988 obstack_1grow (&util_obstack, 'n');
7989 else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
7990 obstack_1grow (&util_obstack, 'N');
7991 else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
7992 obstack_1grow (&util_obstack, 'o');
7993 else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
7994 obstack_1grow (&util_obstack, 'O');
7995 else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
7996 obstack_1grow (&util_obstack, 'R');
7997 else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
7998 obstack_1grow (&util_obstack, 'V');
7999 }
8000 }
8001
8002 /* Determine if a pointee is marked read-only. Only used by the NeXT
8003 runtime to be compatible with gcc-3.3. */
8004
8005 static bool
8006 pointee_is_readonly (tree pointee)
8007 {
8008 while (POINTER_TYPE_P (pointee))
8009 pointee = TREE_TYPE (pointee);
8010
8011 return TYPE_READONLY (pointee);
8012 }
8013
8014 /* Encode a pointer type. */
8015
8016 static void
8017 encode_pointer (tree type, int curtype, int format)
8018 {
8019 tree pointer_to = TREE_TYPE (type);
8020
8021 if (flag_next_runtime)
8022 {
8023 /* This code is used to be compatible with gcc-3.3. */
8024 /* For historical/compatibility reasons, the read-only qualifier
8025 of the pointee gets emitted _before_ the '^'. The read-only
8026 qualifier of the pointer itself gets ignored, _unless_ we are
8027 looking at a typedef! Also, do not emit the 'r' for anything
8028 but the outermost type! */
8029 if (!generating_instance_variables
8030 && (obstack_object_size (&util_obstack) - curtype <= 1)
8031 && (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8032 ? TYPE_READONLY (type)
8033 : pointee_is_readonly (pointer_to)))
8034 obstack_1grow (&util_obstack, 'r');
8035 }
8036
8037 if (TREE_CODE (pointer_to) == RECORD_TYPE)
8038 {
8039 if (OBJC_TYPE_NAME (pointer_to)
8040 && TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
8041 {
8042 const char *name = IDENTIFIER_POINTER (OBJC_TYPE_NAME (pointer_to));
8043
8044 if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
8045 {
8046 obstack_1grow (&util_obstack, '@');
8047 return;
8048 }
8049 else if (TYPE_HAS_OBJC_INFO (pointer_to)
8050 && TYPE_OBJC_INTERFACE (pointer_to))
8051 {
8052 if (generating_instance_variables)
8053 {
8054 obstack_1grow (&util_obstack, '@');
8055 obstack_1grow (&util_obstack, '"');
8056 obstack_grow (&util_obstack, name, strlen (name));
8057 obstack_1grow (&util_obstack, '"');
8058 return;
8059 }
8060 else
8061 {
8062 obstack_1grow (&util_obstack, '@');
8063 return;
8064 }
8065 }
8066 else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
8067 {
8068 obstack_1grow (&util_obstack, '#');
8069 return;
8070 }
8071 else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
8072 {
8073 obstack_1grow (&util_obstack, ':');
8074 return;
8075 }
8076 }
8077 }
8078 else if (TREE_CODE (pointer_to) == INTEGER_TYPE
8079 && TYPE_MODE (pointer_to) == QImode)
8080 {
8081 tree pname = TREE_CODE (OBJC_TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
8082 ? OBJC_TYPE_NAME (pointer_to)
8083 : DECL_NAME (OBJC_TYPE_NAME (pointer_to));
8084
8085 /* (BOOL *) are an exception and are encoded as ^c, while all
8086 other pointers to char are encoded as *. */
8087 if (strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
8088 {
8089 if (!flag_next_runtime)
8090 {
8091 /* The NeXT runtime adds the 'r' before getting here. */
8092
8093 /* It appears that "r*" means "const char *" rather than
8094 "char *const". "char *const" is encoded as "*",
8095 which is identical to "char *", so the "const" is
8096 unfortunately lost. */
8097 if (TYPE_READONLY (pointer_to))
8098 obstack_1grow (&util_obstack, 'r');
8099 }
8100
8101 obstack_1grow (&util_obstack, '*');
8102 return;
8103 }
8104 }
8105
8106 /* We have a normal pointer type that does not get special treatment. */
8107 obstack_1grow (&util_obstack, '^');
8108 encode_type (pointer_to, curtype, format);
8109 }
8110
8111 static void
8112 encode_array (tree type, int curtype, int format)
8113 {
8114 tree an_int_cst = TYPE_SIZE (type);
8115 tree array_of = TREE_TYPE (type);
8116 char buffer[40];
8117
8118 if (an_int_cst == NULL)
8119 {
8120 /* We are trying to encode an incomplete array. An incomplete
8121 array is forbidden as part of an instance variable. */
8122 if (generating_instance_variables)
8123 {
8124 /* TODO: Detect this error earlier. */
8125 error ("instance variable has unknown size");
8126 return;
8127 }
8128
8129 /* So the only case in which an incomplete array could occur is
8130 if we are encoding the arguments or return value of a method.
8131 In that case, an incomplete array argument or return value
8132 (eg, -(void)display: (char[])string) is treated like a
8133 pointer because that is how the compiler does the function
8134 call. A special, more complicated case, is when the
8135 incomplete array is the last member of a struct (eg, if we
8136 are encoding "struct { unsigned long int a;double b[];}"),
8137 which is again part of a method argument/return value. In
8138 that case, we really need to communicate to the runtime that
8139 there is an incomplete array (not a pointer!) there. So, we
8140 detect that special case and encode it as a zero-length
8141 array.
8142
8143 Try to detect that we are part of a struct. We do this by
8144 searching for '=' in the type encoding for the current type.
8145 NB: This hack assumes that you can't use '=' as part of a C
8146 identifier.
8147 */
8148 {
8149 char *enc = obstack_base (&util_obstack) + curtype;
8150 if (memchr (enc, '=',
8151 obstack_object_size (&util_obstack) - curtype) == NULL)
8152 {
8153 /* We are not inside a struct. Encode the array as a
8154 pointer. */
8155 encode_pointer (type, curtype, format);
8156 return;
8157 }
8158 }
8159
8160 /* Else, we are in a struct, and we encode it as a zero-length
8161 array. */
8162 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
8163 }
8164 else if (TREE_INT_CST_LOW (TYPE_SIZE (array_of)) == 0)
8165 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT)0);
8166 else
8167 sprintf (buffer, "[" HOST_WIDE_INT_PRINT_DEC,
8168 TREE_INT_CST_LOW (an_int_cst)
8169 / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
8170
8171 obstack_grow (&util_obstack, buffer, strlen (buffer));
8172 encode_type (array_of, curtype, format);
8173 obstack_1grow (&util_obstack, ']');
8174 return;
8175 }
8176
8177 /* Encode a vector. The vector type is a GCC extension to C. */
8178 static void
8179 encode_vector (tree type, int curtype, int format)
8180 {
8181 tree vector_of = TREE_TYPE (type);
8182 char buffer[40];
8183
8184 /* Vectors are like simple fixed-size arrays. */
8185
8186 /* Output ![xx,yy,<code>] where xx is the vector_size, yy is the
8187 alignment of the vector, and <code> is the base type. Eg, int
8188 __attribute__ ((vector_size (16))) gets encoded as ![16,32,i]
8189 assuming that the alignment is 32 bytes. We include size and
8190 alignment in bytes so that the runtime does not have to have any
8191 knowledge of the actual types.
8192 */
8193 sprintf (buffer, "![" HOST_WIDE_INT_PRINT_DEC ",%d",
8194 /* We want to compute the equivalent of sizeof (<vector>).
8195 Code inspired by c_sizeof_or_alignof_type. */
8196 ((TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type))
8197 / (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT))),
8198 /* We want to compute the equivalent of __alignof__
8199 (<vector>). Code inspired by
8200 c_sizeof_or_alignof_type. */
8201 TYPE_ALIGN_UNIT (type));
8202 obstack_grow (&util_obstack, buffer, strlen (buffer));
8203 encode_type (vector_of, curtype, format);
8204 obstack_1grow (&util_obstack, ']');
8205 return;
8206 }
8207 \f
8208 static void
8209 encode_aggregate_fields (tree type, bool pointed_to, int curtype, int format)
8210 {
8211 tree field = TYPE_FIELDS (type);
8212
8213 for (; field; field = DECL_CHAIN (field))
8214 {
8215 #ifdef OBJCPLUS
8216 /* C++ static members, and things that are not field at all,
8217 should not appear in the encoding. */
8218 if (TREE_CODE (field) != FIELD_DECL || TREE_STATIC (field))
8219 continue;
8220 #endif
8221
8222 /* Recursively encode fields of embedded base classes. */
8223 if (DECL_ARTIFICIAL (field) && !DECL_NAME (field)
8224 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8225 {
8226 encode_aggregate_fields (TREE_TYPE (field),
8227 pointed_to, curtype, format);
8228 continue;
8229 }
8230
8231 if (generating_instance_variables && !pointed_to)
8232 {
8233 tree fname = DECL_NAME (field);
8234
8235 obstack_1grow (&util_obstack, '"');
8236
8237 if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
8238 obstack_grow (&util_obstack,
8239 IDENTIFIER_POINTER (fname),
8240 strlen (IDENTIFIER_POINTER (fname)));
8241
8242 obstack_1grow (&util_obstack, '"');
8243 }
8244
8245 encode_field_decl (field, curtype, format);
8246 }
8247 }
8248
8249 static void
8250 encode_aggregate_within (tree type, int curtype, int format, int left,
8251 int right)
8252 {
8253 tree name;
8254 /* NB: aggregates that are pointed to have slightly different encoding
8255 rules in that you never encode the names of instance variables. */
8256 int ob_size = obstack_object_size (&util_obstack);
8257 bool inline_contents = false;
8258 bool pointed_to = false;
8259
8260 if (flag_next_runtime)
8261 {
8262 if (ob_size > 0 && *(obstack_next_free (&util_obstack) - 1) == '^')
8263 pointed_to = true;
8264
8265 if ((format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
8266 && (!pointed_to || ob_size - curtype == 1
8267 || (ob_size - curtype == 2
8268 && *(obstack_next_free (&util_obstack) - 2) == 'r')))
8269 inline_contents = true;
8270 }
8271 else
8272 {
8273 /* c0 and c1 are the last two characters in the encoding of the
8274 current type; if the last two characters were '^' or '^r',
8275 then we are encoding an aggregate that is "pointed to". The
8276 comment above applies: in that case we should avoid encoding
8277 the names of instance variables.
8278 */
8279 char c1 = ob_size > 1 ? *(obstack_next_free (&util_obstack) - 2) : 0;
8280 char c0 = ob_size > 0 ? *(obstack_next_free (&util_obstack) - 1) : 0;
8281
8282 if (c0 == '^' || (c1 == '^' && c0 == 'r'))
8283 pointed_to = true;
8284
8285 if (format == OBJC_ENCODE_INLINE_DEFS || generating_instance_variables)
8286 {
8287 if (!pointed_to)
8288 inline_contents = true;
8289 else
8290 {
8291 /* Note that the check (ob_size - curtype < 2) prevents
8292 infinite recursion when encoding a structure which is
8293 a linked list (eg, struct node { struct node *next;
8294 }). Each time we follow a pointer, we add one
8295 character to ob_size, and curtype is fixed, so after
8296 at most two pointers we stop inlining contents and
8297 break the loop.
8298
8299 The other case where we don't inline is "^r", which
8300 is a pointer to a constant struct.
8301 */
8302 if ((ob_size - curtype <= 2) && !(c0 == 'r'))
8303 inline_contents = true;
8304 }
8305 }
8306 }
8307
8308 /* Traverse struct aliases; it is important to get the
8309 original struct and its tag name (if any). */
8310 type = TYPE_MAIN_VARIANT (type);
8311 name = OBJC_TYPE_NAME (type);
8312 /* Open parenth/bracket. */
8313 obstack_1grow (&util_obstack, left);
8314
8315 /* Encode the struct/union tag name, or '?' if a tag was
8316 not provided. Typedef aliases do not qualify. */
8317 #ifdef OBJCPLUS
8318 /* For compatibility with the NeXT runtime, ObjC++ encodes template
8319 args as a composite struct tag name. */
8320 if (name && TREE_CODE (name) == IDENTIFIER_NODE
8321 /* Did this struct have a tag? */
8322 && !TYPE_WAS_ANONYMOUS (type))
8323 obstack_grow (&util_obstack,
8324 decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME),
8325 strlen (decl_as_string (type, TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME)));
8326 #else
8327 if (name && TREE_CODE (name) == IDENTIFIER_NODE)
8328 obstack_grow (&util_obstack,
8329 IDENTIFIER_POINTER (name),
8330 strlen (IDENTIFIER_POINTER (name)));
8331 #endif
8332 else
8333 obstack_1grow (&util_obstack, '?');
8334
8335 /* Encode the types (and possibly names) of the inner fields,
8336 if required. */
8337 if (inline_contents)
8338 {
8339 obstack_1grow (&util_obstack, '=');
8340 encode_aggregate_fields (type, pointed_to, curtype, format);
8341 }
8342 /* Close parenth/bracket. */
8343 obstack_1grow (&util_obstack, right);
8344 }
8345
8346 /* Encode a bitfield NeXT-style (i.e., without a bit offset or the underlying
8347 field type. */
8348
8349 static void
8350 encode_next_bitfield (int width)
8351 {
8352 char buffer[40];
8353 sprintf (buffer, "b%d", width);
8354 obstack_grow (&util_obstack, buffer, strlen (buffer));
8355 }
8356 \f
8357
8358 /* Encodes 'type', ignoring type qualifiers (which you should encode
8359 beforehand if needed) with the exception of 'const', which is
8360 encoded by encode_type. See above for the explanation of
8361 'curtype'. 'format' can be OBJC_ENCODE_INLINE_DEFS or
8362 OBJC_ENCODE_DONT_INLINE_DEFS.
8363 */
8364 static void
8365 encode_type (tree type, int curtype, int format)
8366 {
8367 enum tree_code code = TREE_CODE (type);
8368
8369 /* Ignore type qualifiers other than 'const' when encoding a
8370 type. */
8371
8372 if (type == error_mark_node)
8373 return;
8374
8375 if (!flag_next_runtime)
8376 {
8377 if (TYPE_READONLY (type))
8378 obstack_1grow (&util_obstack, 'r');
8379 }
8380
8381 switch (code)
8382 {
8383 case ENUMERAL_TYPE:
8384 if (flag_next_runtime)
8385 {
8386 /* Kludge for backwards-compatibility with gcc-3.3: enums
8387 are always encoded as 'i' no matter what type they
8388 actually are (!). */
8389 obstack_1grow (&util_obstack, 'i');
8390 break;
8391 }
8392 /* Else, they are encoded exactly like the integer type that is
8393 used by the compiler to store them. */
8394 case INTEGER_TYPE:
8395 {
8396 char c;
8397 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
8398 {
8399 case 8: c = TYPE_UNSIGNED (type) ? 'C' : 'c'; break;
8400 case 16: c = TYPE_UNSIGNED (type) ? 'S' : 's'; break;
8401 case 32:
8402 if (flag_next_runtime)
8403 {
8404 tree int_type;
8405 /* Another legacy kludge for compatiblity with
8406 gcc-3.3: 32-bit longs are encoded as 'l' or 'L',
8407 but not always. For typedefs, we need to use 'i'
8408 or 'I' instead if encoding a struct field, or a
8409 pointer! */
8410 int_type = ((!generating_instance_variables
8411 && (obstack_object_size (&util_obstack)
8412 == (unsigned) curtype))
8413 ? TYPE_MAIN_VARIANT (type)
8414 : type);
8415
8416 if (int_type == long_unsigned_type_node
8417 || int_type == long_integer_type_node)
8418 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
8419 else
8420 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
8421 }
8422 else
8423 {
8424 if (type == long_unsigned_type_node
8425 || type == long_integer_type_node)
8426 c = TYPE_UNSIGNED (type) ? 'L' : 'l';
8427 else
8428 c = TYPE_UNSIGNED (type) ? 'I' : 'i';
8429 }
8430 break;
8431 case 64: c = TYPE_UNSIGNED (type) ? 'Q' : 'q'; break;
8432 case 128: c = TYPE_UNSIGNED (type) ? 'T' : 't'; break;
8433 default: abort ();
8434 }
8435 obstack_1grow (&util_obstack, c);
8436 break;
8437 }
8438 case REAL_TYPE:
8439 {
8440 char c;
8441 /* Floating point types. */
8442 switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
8443 {
8444 case 32: c = 'f'; break;
8445 case 64: c = 'd'; break;
8446 case 96:
8447 case 128: c = 'D'; break;
8448 default: abort ();
8449 }
8450 obstack_1grow (&util_obstack, c);
8451 break;
8452 }
8453 case VOID_TYPE:
8454 obstack_1grow (&util_obstack, 'v');
8455 break;
8456
8457 case BOOLEAN_TYPE:
8458 obstack_1grow (&util_obstack, 'B');
8459 break;
8460
8461 case ARRAY_TYPE:
8462 encode_array (type, curtype, format);
8463 break;
8464
8465 case POINTER_TYPE:
8466 #ifdef OBJCPLUS
8467 case REFERENCE_TYPE:
8468 #endif
8469 encode_pointer (type, curtype, format);
8470 break;
8471
8472 case RECORD_TYPE:
8473 encode_aggregate_within (type, curtype, format, '{', '}');
8474 break;
8475
8476 case UNION_TYPE:
8477 encode_aggregate_within (type, curtype, format, '(', ')');
8478 break;
8479
8480 case FUNCTION_TYPE: /* '?' means an unknown type. */
8481 obstack_1grow (&util_obstack, '?');
8482 break;
8483
8484 case COMPLEX_TYPE:
8485 /* A complex is encoded as 'j' followed by the inner type (eg,
8486 "_Complex int" is encoded as 'ji'). */
8487 obstack_1grow (&util_obstack, 'j');
8488 encode_type (TREE_TYPE (type), curtype, format);
8489 break;
8490
8491 case VECTOR_TYPE:
8492 encode_vector (type, curtype, format);
8493 break;
8494
8495 default:
8496 warning (0, "unknown type %s found during Objective-C encoding",
8497 gen_type_name (type));
8498 obstack_1grow (&util_obstack, '?');
8499 break;
8500 }
8501
8502 if (flag_next_runtime)
8503 {
8504 /* Super-kludge. Some ObjC qualifier and type combinations need
8505 to be rearranged for compatibility with gcc-3.3. */
8506 if (code == POINTER_TYPE && obstack_object_size (&util_obstack) >= 3)
8507 {
8508 char *enc = obstack_base (&util_obstack) + curtype;
8509
8510 /* Rewrite "in const" from "nr" to "rn". */
8511 if (curtype >= 1 && !strncmp (enc - 1, "nr", 2))
8512 strncpy (enc - 1, "rn", 2);
8513 }
8514 }
8515 }
8516
8517 static void
8518 encode_gnu_bitfield (int position, tree type, int size)
8519 {
8520 enum tree_code code = TREE_CODE (type);
8521 char buffer[40];
8522 char charType = '?';
8523
8524 /* This code is only executed for the GNU runtime, so we can ignore
8525 the NeXT runtime kludge of always encoding enums as 'i' no matter
8526 what integers they actually are. */
8527 if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
8528 {
8529 if (integer_zerop (TYPE_MIN_VALUE (type)))
8530 /* Unsigned integer types. */
8531 {
8532 if (TYPE_MODE (type) == QImode)
8533 charType = 'C';
8534 else if (TYPE_MODE (type) == HImode)
8535 charType = 'S';
8536 else if (TYPE_MODE (type) == SImode)
8537 {
8538 if (type == long_unsigned_type_node)
8539 charType = 'L';
8540 else
8541 charType = 'I';
8542 }
8543 else if (TYPE_MODE (type) == DImode)
8544 charType = 'Q';
8545 }
8546 else
8547 /* Signed integer types. */
8548 {
8549 if (TYPE_MODE (type) == QImode)
8550 charType = 'c';
8551 else if (TYPE_MODE (type) == HImode)
8552 charType = 's';
8553 else if (TYPE_MODE (type) == SImode)
8554 {
8555 if (type == long_integer_type_node)
8556 charType = 'l';
8557 else
8558 charType = 'i';
8559 }
8560
8561 else if (TYPE_MODE (type) == DImode)
8562 charType = 'q';
8563 }
8564 }
8565 else
8566 {
8567 /* Do not do any encoding, produce an error and keep going. */
8568 error ("trying to encode non-integer type as a bitfield");
8569 return;
8570 }
8571
8572 sprintf (buffer, "b%d%c%d", position, charType, size);
8573 obstack_grow (&util_obstack, buffer, strlen (buffer));
8574 }
8575
8576 static void
8577 encode_field_decl (tree field_decl, int curtype, int format)
8578 {
8579 #ifdef OBJCPLUS
8580 /* C++ static members, and things that are not fields at all,
8581 should not appear in the encoding. */
8582 if (TREE_CODE (field_decl) != FIELD_DECL || TREE_STATIC (field_decl))
8583 return;
8584 #endif
8585
8586 /* Generate the bitfield typing information, if needed. Note the difference
8587 between GNU and NeXT runtimes. */
8588 if (DECL_BIT_FIELD_TYPE (field_decl))
8589 {
8590 int size = tree_low_cst (DECL_SIZE (field_decl), 1);
8591
8592 if (flag_next_runtime)
8593 encode_next_bitfield (size);
8594 else
8595 encode_gnu_bitfield (int_bit_position (field_decl),
8596 DECL_BIT_FIELD_TYPE (field_decl), size);
8597 }
8598 else
8599 encode_type (TREE_TYPE (field_decl), curtype, format);
8600 }
8601
8602 /* Decay array and function parameters into pointers. */
8603
8604 static tree
8605 objc_decay_parm_type (tree type)
8606 {
8607 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8608 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8609 ? TREE_TYPE (type)
8610 : type);
8611
8612 return type;
8613 }
8614
8615 static GTY(()) tree objc_parmlist = NULL_TREE;
8616
8617 /* Append PARM to a list of formal parameters of a method, making a necessary
8618 array-to-pointer adjustment along the way. */
8619
8620 static void
8621 objc_push_parm (tree parm)
8622 {
8623 tree type;
8624
8625 if (TREE_TYPE (parm) == error_mark_node)
8626 {
8627 objc_parmlist = chainon (objc_parmlist, parm);
8628 return;
8629 }
8630
8631 /* Decay arrays and functions into pointers. */
8632 type = objc_decay_parm_type (TREE_TYPE (parm));
8633
8634 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8635 built as well. */
8636 if (type != TREE_TYPE (parm))
8637 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8638
8639 DECL_ARG_TYPE (parm)
8640 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8641
8642 /* Record constancy and volatility. */
8643 c_apply_type_quals_to_decl
8644 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8645 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8646 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8647
8648 objc_parmlist = chainon (objc_parmlist, parm);
8649 }
8650
8651 /* Retrieve the formal parameter list constructed via preceding calls to
8652 objc_push_parm(). */
8653
8654 #ifdef OBJCPLUS
8655 static tree
8656 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED)
8657 #else
8658 static struct c_arg_info *
8659 objc_get_parm_info (int have_ellipsis)
8660 #endif
8661 {
8662 #ifdef OBJCPLUS
8663 tree parm_info = objc_parmlist;
8664 objc_parmlist = NULL_TREE;
8665
8666 return parm_info;
8667 #else
8668 tree parm_info = objc_parmlist;
8669 struct c_arg_info *arg_info;
8670 /* The C front-end requires an elaborate song and dance at
8671 this point. */
8672 push_scope ();
8673 declare_parm_level ();
8674 while (parm_info)
8675 {
8676 tree next = DECL_CHAIN (parm_info);
8677
8678 DECL_CHAIN (parm_info) = NULL_TREE;
8679 parm_info = pushdecl (parm_info);
8680 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8681 parm_info = next;
8682 }
8683 arg_info = get_parm_info (have_ellipsis);
8684 pop_scope ();
8685 objc_parmlist = NULL_TREE;
8686 return arg_info;
8687 #endif
8688 }
8689
8690 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8691 method definitions. In the case of instance methods, we can be more
8692 specific as to the type of 'self'. */
8693
8694 static void
8695 synth_self_and_ucmd_args (void)
8696 {
8697 tree self_type;
8698
8699 if (objc_method_context
8700 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8701 self_type = objc_instance_type;
8702 else
8703 /* Really a `struct objc_class *'. However, we allow people to
8704 assign to self, which changes its type midstream. */
8705 self_type = objc_object_type;
8706
8707 /* id self; */
8708 objc_push_parm (build_decl (input_location,
8709 PARM_DECL, self_id, self_type));
8710
8711 /* SEL _cmd; */
8712 objc_push_parm (build_decl (input_location,
8713 PARM_DECL, ucmd_id, objc_selector_type));
8714 }
8715
8716 /* Transform an Objective-C method definition into a static C function
8717 definition, synthesizing the first two arguments, "self" and "_cmd",
8718 in the process. */
8719
8720 static void
8721 start_method_def (tree method)
8722 {
8723 tree parmlist;
8724 #ifdef OBJCPLUS
8725 tree parm_info;
8726 #else
8727 struct c_arg_info *parm_info;
8728 #endif
8729 int have_ellipsis = 0;
8730
8731 /* If we are defining a "dealloc" method in a non-root class, we
8732 will need to check if a [super dealloc] is missing, and warn if
8733 it is. */
8734 if(CLASS_SUPER_NAME (objc_implementation_context)
8735 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8736 should_call_super_dealloc = 1;
8737 else
8738 should_call_super_dealloc = 0;
8739
8740 /* Required to implement _msgSuper. */
8741 objc_method_context = method;
8742 UOBJC_SUPER_decl = NULL_TREE;
8743
8744 /* Generate prototype declarations for arguments..."new-style". */
8745 synth_self_and_ucmd_args ();
8746
8747 /* Generate argument declarations if a keyword_decl. */
8748 parmlist = METHOD_SEL_ARGS (method);
8749 while (parmlist)
8750 {
8751 tree type = TREE_VALUE (TREE_TYPE (parmlist)), parm;
8752
8753 parm = build_decl (input_location,
8754 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8755 objc_push_parm (parm);
8756 parmlist = DECL_CHAIN (parmlist);
8757 }
8758
8759 if (METHOD_ADD_ARGS (method))
8760 {
8761 tree akey;
8762
8763 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8764 akey; akey = TREE_CHAIN (akey))
8765 {
8766 objc_push_parm (TREE_VALUE (akey));
8767 }
8768
8769 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8770 have_ellipsis = 1;
8771 }
8772
8773 parm_info = objc_get_parm_info (have_ellipsis);
8774
8775 really_start_method (objc_method_context, parm_info);
8776 }
8777
8778 /* Return 1 if TYPE1 is equivalent to TYPE2
8779 for purposes of method overloading. */
8780
8781 static int
8782 objc_types_are_equivalent (tree type1, tree type2)
8783 {
8784 if (type1 == type2)
8785 return 1;
8786
8787 /* Strip away indirections. */
8788 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8789 && (TREE_CODE (type1) == TREE_CODE (type2)))
8790 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8791 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8792 return 0;
8793
8794 type1 = (TYPE_HAS_OBJC_INFO (type1)
8795 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8796 : NULL_TREE);
8797 type2 = (TYPE_HAS_OBJC_INFO (type2)
8798 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8799 : NULL_TREE);
8800
8801 if (list_length (type1) == list_length (type2))
8802 {
8803 for (; type2; type2 = TREE_CHAIN (type2))
8804 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (type2)))
8805 return 0;
8806 return 1;
8807 }
8808 return 0;
8809 }
8810
8811 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8812
8813 static int
8814 objc_types_share_size_and_alignment (tree type1, tree type2)
8815 {
8816 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8817 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8818 }
8819
8820 /* Return 1 if PROTO1 is equivalent to PROTO2
8821 for purposes of method overloading. Ordinarily, the type signatures
8822 should match up exactly, unless STRICT is zero, in which case we
8823 shall allow differences in which the size and alignment of a type
8824 is the same. */
8825
8826 static int
8827 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8828 {
8829 tree type1, type2;
8830
8831 /* The following test is needed in case there are hashing
8832 collisions. */
8833 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8834 return 0;
8835
8836 /* Compare return types. */
8837 type1 = TREE_VALUE (TREE_TYPE (proto1));
8838 type2 = TREE_VALUE (TREE_TYPE (proto2));
8839
8840 if (!objc_types_are_equivalent (type1, type2)
8841 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8842 return 0;
8843
8844 /* Compare argument types. */
8845 for (type1 = get_arg_type_list (proto1, METHOD_REF, 0),
8846 type2 = get_arg_type_list (proto2, METHOD_REF, 0);
8847 type1 && type2;
8848 type1 = TREE_CHAIN (type1), type2 = TREE_CHAIN (type2))
8849 {
8850 if (!objc_types_are_equivalent (TREE_VALUE (type1), TREE_VALUE (type2))
8851 && (strict
8852 || !objc_types_share_size_and_alignment (TREE_VALUE (type1),
8853 TREE_VALUE (type2))))
8854 return 0;
8855 }
8856
8857 return (!type1 && !type2);
8858 }
8859
8860 /* Fold an OBJ_TYPE_REF expression for ObjC method dispatches, where
8861 this occurs. ObjC method dispatches are _not_ like C++ virtual
8862 member function dispatches, and we account for the difference here. */
8863 tree
8864 #ifdef OBJCPLUS
8865 objc_fold_obj_type_ref (tree ref, tree known_type)
8866 #else
8867 objc_fold_obj_type_ref (tree ref ATTRIBUTE_UNUSED,
8868 tree known_type ATTRIBUTE_UNUSED)
8869 #endif
8870 {
8871 #ifdef OBJCPLUS
8872 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
8873
8874 /* If the receiver does not have virtual member functions, there
8875 is nothing we can (or need to) do here. */
8876 if (!v)
8877 return NULL_TREE;
8878
8879 /* Let C++ handle C++ virtual functions. */
8880 return cp_fold_obj_type_ref (ref, known_type);
8881 #else
8882 /* For plain ObjC, we currently do not need to do anything. */
8883 return NULL_TREE;
8884 #endif
8885 }
8886
8887 static void
8888 objc_start_function (tree name, tree type, tree attrs,
8889 #ifdef OBJCPLUS
8890 tree params
8891 #else
8892 struct c_arg_info *params
8893 #endif
8894 )
8895 {
8896 tree fndecl = build_decl (input_location,
8897 FUNCTION_DECL, name, type);
8898
8899 #ifdef OBJCPLUS
8900 DECL_ARGUMENTS (fndecl) = params;
8901 DECL_INITIAL (fndecl) = error_mark_node;
8902 DECL_EXTERNAL (fndecl) = 0;
8903 TREE_STATIC (fndecl) = 1;
8904 retrofit_lang_decl (fndecl);
8905 cplus_decl_attributes (&fndecl, attrs, 0);
8906 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8907 #else
8908 current_function_returns_value = 0; /* Assume, until we see it does. */
8909 current_function_returns_null = 0;
8910
8911 decl_attributes (&fndecl, attrs, 0);
8912 announce_function (fndecl);
8913 DECL_INITIAL (fndecl) = error_mark_node;
8914 DECL_EXTERNAL (fndecl) = 0;
8915 TREE_STATIC (fndecl) = 1;
8916 current_function_decl = pushdecl (fndecl);
8917 push_scope ();
8918 declare_parm_level ();
8919 DECL_RESULT (current_function_decl)
8920 = build_decl (input_location,
8921 RESULT_DECL, NULL_TREE,
8922 TREE_TYPE (TREE_TYPE (current_function_decl)));
8923 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8924 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8925 start_fname_decls ();
8926 store_parm_decls_from (params);
8927 #endif
8928
8929 TREE_USED (current_function_decl) = 1;
8930 }
8931
8932 /* - Generate an identifier for the function. the format is "_n_cls",
8933 where 1 <= n <= nMethods, and cls is the name the implementation we
8934 are processing.
8935 - Install the return type from the method declaration.
8936 - If we have a prototype, check for type consistency. */
8937
8938 static void
8939 really_start_method (tree method,
8940 #ifdef OBJCPLUS
8941 tree parmlist
8942 #else
8943 struct c_arg_info *parmlist
8944 #endif
8945 )
8946 {
8947 tree ret_type, meth_type;
8948 tree method_id;
8949 const char *sel_name, *class_name, *cat_name;
8950 char *buf;
8951
8952 /* Synth the storage class & assemble the return type. */
8953 ret_type = TREE_VALUE (TREE_TYPE (method));
8954
8955 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8956 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8957 cat_name = ((TREE_CODE (objc_implementation_context)
8958 == CLASS_IMPLEMENTATION_TYPE)
8959 ? NULL
8960 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8961 method_slot++;
8962
8963 /* Make sure this is big enough for any plausible method label. */
8964 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8965 + (cat_name ? strlen (cat_name) : 0));
8966
8967 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8968 class_name, cat_name, sel_name, method_slot);
8969
8970 method_id = get_identifier (buf);
8971
8972 #ifdef OBJCPLUS
8973 /* Objective-C methods cannot be overloaded, so we don't need
8974 the type encoding appended. It looks bad anyway... */
8975 push_lang_context (lang_name_c);
8976 #endif
8977
8978 meth_type
8979 = build_function_type (ret_type,
8980 get_arg_type_list (method, METHOD_DEF, 0));
8981 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8982
8983 /* Set self_decl from the first argument. */
8984 self_decl = DECL_ARGUMENTS (current_function_decl);
8985
8986 /* Suppress unused warnings. */
8987 TREE_USED (self_decl) = 1;
8988 DECL_READ_P (self_decl) = 1;
8989 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8990 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8991 #ifdef OBJCPLUS
8992 pop_lang_context ();
8993 #endif
8994
8995 METHOD_DEFINITION (method) = current_function_decl;
8996
8997 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8998
8999 if (implementation_template != objc_implementation_context)
9000 {
9001 tree proto
9002 = lookup_method_static (implementation_template,
9003 METHOD_SEL_NAME (method),
9004 ((TREE_CODE (method) == CLASS_METHOD_DECL)
9005 | OBJC_LOOKUP_NO_SUPER));
9006
9007 if (proto)
9008 {
9009 if (!comp_proto_with_proto (method, proto, 1))
9010 {
9011 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
9012
9013 warning_at (DECL_SOURCE_LOCATION (method), 0,
9014 "conflicting types for %<%c%s%>",
9015 (type ? '-' : '+'),
9016 identifier_to_locale (gen_method_decl (method)));
9017 inform (DECL_SOURCE_LOCATION (proto),
9018 "previous declaration of %<%c%s%>",
9019 (type ? '-' : '+'),
9020 identifier_to_locale (gen_method_decl (proto)));
9021 }
9022 }
9023 else
9024 {
9025 /* We have a method @implementation even though we did not
9026 see a corresponding @interface declaration (which is allowed
9027 by Objective-C rules). Go ahead and place the method in
9028 the @interface anyway, so that message dispatch lookups
9029 will see it. */
9030 tree interface = implementation_template;
9031
9032 if (TREE_CODE (objc_implementation_context)
9033 == CATEGORY_IMPLEMENTATION_TYPE)
9034 interface = lookup_category
9035 (interface,
9036 CLASS_SUPER_NAME (objc_implementation_context));
9037
9038 if (interface)
9039 objc_add_method (interface, copy_node (method),
9040 TREE_CODE (method) == CLASS_METHOD_DECL);
9041 }
9042 }
9043 }
9044
9045 static void *UOBJC_SUPER_scope = 0;
9046
9047 /* _n_Method (id self, SEL sel, ...)
9048 {
9049 struct objc_super _S;
9050 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
9051 } */
9052
9053 static tree
9054 get_super_receiver (void)
9055 {
9056 if (objc_method_context)
9057 {
9058 tree super_expr, super_expr_list;
9059
9060 if (!UOBJC_SUPER_decl)
9061 {
9062 UOBJC_SUPER_decl = build_decl (input_location,
9063 VAR_DECL, get_identifier (TAG_SUPER),
9064 objc_super_template);
9065 /* This prevents `unused variable' warnings when compiling with -Wall. */
9066 TREE_USED (UOBJC_SUPER_decl) = 1;
9067 DECL_READ_P (UOBJC_SUPER_decl) = 1;
9068 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
9069 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
9070 NULL_TREE);
9071 UOBJC_SUPER_scope = objc_get_current_scope ();
9072 }
9073
9074 /* Set receiver to self. */
9075 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
9076 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
9077 NOP_EXPR, input_location, self_decl,
9078 NULL_TREE);
9079 super_expr_list = super_expr;
9080
9081 /* Set class to begin searching. */
9082 super_expr = objc_build_component_ref (UOBJC_SUPER_decl,
9083 get_identifier ("super_class"));
9084
9085 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
9086 {
9087 /* [_cls, __cls]Super are "pre-built" in
9088 synth_forward_declarations. */
9089
9090 super_expr = build_modify_expr (input_location, super_expr,
9091 NULL_TREE, NOP_EXPR,
9092 input_location,
9093 ((TREE_CODE (objc_method_context)
9094 == INSTANCE_METHOD_DECL)
9095 ? ucls_super_ref
9096 : uucls_super_ref),
9097 NULL_TREE);
9098 }
9099
9100 else
9101 /* We have a category. */
9102 {
9103 tree super_name = CLASS_SUPER_NAME (implementation_template);
9104 tree super_class;
9105
9106 /* Barf if super used in a category of Object. */
9107 if (!super_name)
9108 {
9109 error ("no super class declared in interface for %qE",
9110 CLASS_NAME (implementation_template));
9111 return error_mark_node;
9112 }
9113
9114 if (flag_next_runtime && !flag_zero_link)
9115 {
9116 super_class = objc_get_class_reference (super_name);
9117 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
9118 /* If we are in a class method, we must retrieve the
9119 _metaclass_ for the current class, pointed at by
9120 the class's "isa" pointer. The following assumes that
9121 "isa" is the first ivar in a class (which it must be). */
9122 super_class
9123 = build_indirect_ref
9124 (input_location,
9125 build_c_cast (input_location,
9126 build_pointer_type (objc_class_type),
9127 super_class), RO_UNARY_STAR);
9128 }
9129 else
9130 {
9131 add_class_reference (super_name);
9132 super_class = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9133 ? objc_get_class_decl : objc_get_meta_class_decl);
9134 assemble_external (super_class);
9135 super_class
9136 = build_function_call
9137 (input_location,
9138 super_class,
9139 build_tree_list
9140 (NULL_TREE,
9141 my_build_string_pointer
9142 (IDENTIFIER_LENGTH (super_name) + 1,
9143 IDENTIFIER_POINTER (super_name))));
9144 }
9145
9146 super_expr
9147 = build_modify_expr (input_location, super_expr, NULL_TREE,
9148 NOP_EXPR,
9149 input_location,
9150 build_c_cast (input_location,
9151 TREE_TYPE (super_expr),
9152 super_class),
9153 NULL_TREE);
9154 }
9155
9156 super_expr_list = build_compound_expr (input_location,
9157 super_expr_list, super_expr);
9158
9159 super_expr = build_unary_op (input_location,
9160 ADDR_EXPR, UOBJC_SUPER_decl, 0);
9161 super_expr_list = build_compound_expr (input_location,
9162 super_expr_list, super_expr);
9163
9164 return super_expr_list;
9165 }
9166 else
9167 {
9168 error ("[super ...] must appear in a method context");
9169 return error_mark_node;
9170 }
9171 }
9172
9173 /* When exiting a scope, sever links to a 'super' declaration (if any)
9174 therein contained. */
9175
9176 void
9177 objc_clear_super_receiver (void)
9178 {
9179 if (objc_method_context
9180 && UOBJC_SUPER_scope == objc_get_current_scope ()) {
9181 UOBJC_SUPER_decl = 0;
9182 UOBJC_SUPER_scope = 0;
9183 }
9184 }
9185
9186 void
9187 objc_finish_method_definition (tree fndecl)
9188 {
9189 /* We cannot validly inline ObjC methods, at least not without a language
9190 extension to declare that a method need not be dynamically
9191 dispatched, so suppress all thoughts of doing so. */
9192 DECL_UNINLINABLE (fndecl) = 1;
9193
9194 #ifndef OBJCPLUS
9195 /* The C++ front-end will have called finish_function() for us. */
9196 finish_function ();
9197 #endif
9198
9199 METHOD_ENCODING (objc_method_context)
9200 = encode_method_prototype (objc_method_context);
9201
9202 /* Required to implement _msgSuper. This must be done AFTER finish_function,
9203 since the optimizer may find "may be used before set" errors. */
9204 objc_method_context = NULL_TREE;
9205
9206 if (should_call_super_dealloc)
9207 warning (0, "method possibly missing a [super dealloc] call");
9208 }
9209
9210 /* Given a tree DECL node, produce a printable description of it in the given
9211 buffer, overwriting the buffer. */
9212
9213 static char *
9214 gen_declaration (tree decl)
9215 {
9216 errbuf[0] = '\0';
9217
9218 if (DECL_P (decl))
9219 {
9220 gen_type_name_0 (TREE_TYPE (decl));
9221
9222 if (DECL_NAME (decl))
9223 {
9224 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
9225 strcat (errbuf, " ");
9226
9227 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
9228 }
9229
9230 if (DECL_INITIAL (decl)
9231 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
9232 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
9233 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
9234 }
9235
9236 return errbuf;
9237 }
9238
9239 /* Given a tree TYPE node, produce a printable description of it in the given
9240 buffer, overwriting the buffer. */
9241
9242 static char *
9243 gen_type_name_0 (tree type)
9244 {
9245 tree orig = type, proto;
9246
9247 if (TYPE_P (type) && TYPE_NAME (type))
9248 type = TYPE_NAME (type);
9249 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
9250 {
9251 tree inner = TREE_TYPE (type);
9252
9253 while (TREE_CODE (inner) == ARRAY_TYPE)
9254 inner = TREE_TYPE (inner);
9255
9256 gen_type_name_0 (inner);
9257
9258 if (!POINTER_TYPE_P (inner))
9259 strcat (errbuf, " ");
9260
9261 if (POINTER_TYPE_P (type))
9262 strcat (errbuf, "*");
9263 else
9264 while (type != inner)
9265 {
9266 strcat (errbuf, "[");
9267
9268 if (TYPE_DOMAIN (type))
9269 {
9270 char sz[20];
9271
9272 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
9273 (TREE_INT_CST_LOW
9274 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
9275 strcat (errbuf, sz);
9276 }
9277
9278 strcat (errbuf, "]");
9279 type = TREE_TYPE (type);
9280 }
9281
9282 goto exit_function;
9283 }
9284
9285 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
9286 type = DECL_NAME (type);
9287
9288 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
9289 ? IDENTIFIER_POINTER (type)
9290 : "");
9291
9292 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
9293 if (objc_is_id (orig))
9294 orig = TREE_TYPE (orig);
9295
9296 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
9297
9298 if (proto)
9299 {
9300 strcat (errbuf, " <");
9301
9302 while (proto) {
9303 strcat (errbuf,
9304 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
9305 proto = TREE_CHAIN (proto);
9306 strcat (errbuf, proto ? ", " : ">");
9307 }
9308 }
9309
9310 exit_function:
9311 return errbuf;
9312 }
9313
9314 static char *
9315 gen_type_name (tree type)
9316 {
9317 errbuf[0] = '\0';
9318
9319 return gen_type_name_0 (type);
9320 }
9321
9322 /* Given a method tree, put a printable description into the given
9323 buffer (overwriting) and return a pointer to the buffer. */
9324
9325 static char *
9326 gen_method_decl (tree method)
9327 {
9328 tree chain;
9329
9330 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9331 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9332 strcat (errbuf, ")");
9333 chain = METHOD_SEL_ARGS (method);
9334
9335 if (chain)
9336 {
9337 /* We have a chain of keyword_decls. */
9338 do
9339 {
9340 if (KEYWORD_KEY_NAME (chain))
9341 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9342
9343 strcat (errbuf, ":(");
9344 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9345 strcat (errbuf, ")");
9346
9347 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9348 if ((chain = DECL_CHAIN (chain)))
9349 strcat (errbuf, " ");
9350 }
9351 while (chain);
9352
9353 if (METHOD_ADD_ARGS (method))
9354 {
9355 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9356
9357 /* Know we have a chain of parm_decls. */
9358 while (chain)
9359 {
9360 strcat (errbuf, ", ");
9361 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9362 chain = TREE_CHAIN (chain);
9363 }
9364
9365 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9366 strcat (errbuf, ", ...");
9367 }
9368 }
9369
9370 else
9371 /* We have a unary selector. */
9372 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9373
9374 return errbuf;
9375 }
9376 \f
9377 /* Debug info. */
9378
9379
9380 /* Dump an @interface declaration of the supplied class CHAIN to the
9381 supplied file FP. Used to implement the -gen-decls option (which
9382 prints out an @interface declaration of all classes compiled in
9383 this run); potentially useful for debugging the compiler too. */
9384 static void
9385 dump_interface (FILE *fp, tree chain)
9386 {
9387 /* FIXME: A heap overflow here whenever a method (or ivar)
9388 declaration is so long that it doesn't fit in the buffer. The
9389 code and all the related functions should be rewritten to avoid
9390 using fixed size buffers. */
9391 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9392 tree ivar_decls = CLASS_RAW_IVARS (chain);
9393 tree nst_methods = CLASS_NST_METHODS (chain);
9394 tree cls_methods = CLASS_CLS_METHODS (chain);
9395
9396 fprintf (fp, "\n@interface %s", my_name);
9397
9398 /* CLASS_SUPER_NAME is used to store the superclass name for
9399 classes, and the category name for categories. */
9400 if (CLASS_SUPER_NAME (chain))
9401 {
9402 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9403
9404 if (TREE_CODE (chain) == CATEGORY_IMPLEMENTATION_TYPE
9405 || TREE_CODE (chain) == CATEGORY_INTERFACE_TYPE)
9406 {
9407 fprintf (fp, " (%s)\n", name);
9408 }
9409 else
9410 {
9411 fprintf (fp, " : %s\n", name);
9412 }
9413 }
9414 else
9415 fprintf (fp, "\n");
9416
9417 /* FIXME - the following doesn't seem to work at the moment. */
9418 if (ivar_decls)
9419 {
9420 fprintf (fp, "{\n");
9421 do
9422 {
9423 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9424 ivar_decls = TREE_CHAIN (ivar_decls);
9425 }
9426 while (ivar_decls);
9427 fprintf (fp, "}\n");
9428 }
9429
9430 while (nst_methods)
9431 {
9432 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9433 nst_methods = TREE_CHAIN (nst_methods);
9434 }
9435
9436 while (cls_methods)
9437 {
9438 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9439 cls_methods = TREE_CHAIN (cls_methods);
9440 }
9441
9442 fprintf (fp, "@end\n");
9443 }
9444
9445 /* Demangle function for Objective-C */
9446 static const char *
9447 objc_demangle (const char *mangled)
9448 {
9449 char *demangled, *cp;
9450
9451 if (mangled[0] == '_' &&
9452 (mangled[1] == 'i' || mangled[1] == 'c') &&
9453 mangled[2] == '_')
9454 {
9455 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9456 if (mangled[1] == 'i')
9457 *cp++ = '-'; /* for instance method */
9458 else
9459 *cp++ = '+'; /* for class method */
9460 *cp++ = '['; /* opening left brace */
9461 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9462 while (*cp && *cp == '_')
9463 cp++; /* skip any initial underbars in class name */
9464 cp = strchr(cp, '_'); /* find first non-initial underbar */
9465 if (cp == NULL)
9466 {
9467 free(demangled); /* not mangled name */
9468 return mangled;
9469 }
9470 if (cp[1] == '_') /* easy case: no category name */
9471 {
9472 *cp++ = ' '; /* replace two '_' with one ' ' */
9473 strcpy(cp, mangled + (cp - demangled) + 2);
9474 }
9475 else
9476 {
9477 *cp++ = '('; /* less easy case: category name */
9478 cp = strchr(cp, '_');
9479 if (cp == 0)
9480 {
9481 free(demangled); /* not mangled name */
9482 return mangled;
9483 }
9484 *cp++ = ')';
9485 *cp++ = ' '; /* overwriting 1st char of method name... */
9486 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9487 }
9488 while (*cp && *cp == '_')
9489 cp++; /* skip any initial underbars in method name */
9490 for (; *cp; cp++)
9491 if (*cp == '_')
9492 *cp = ':'; /* replace remaining '_' with ':' */
9493 *cp++ = ']'; /* closing right brace */
9494 *cp++ = 0; /* string terminator */
9495 return demangled;
9496 }
9497 else
9498 return mangled; /* not an objc mangled name */
9499 }
9500
9501 const char *
9502 objc_printable_name (tree decl, int kind ATTRIBUTE_UNUSED)
9503 {
9504 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9505 }
9506
9507 static void
9508 init_objc (void)
9509 {
9510 gcc_obstack_init (&util_obstack);
9511 util_firstobj = (char *) obstack_finish (&util_obstack);
9512
9513 errbuf = XNEWVEC (char, 1024 * 10);
9514 hash_init ();
9515 synth_module_prologue ();
9516 }
9517 \f
9518 static void
9519 finish_objc (void)
9520 {
9521 struct imp_entry *impent;
9522 tree chain;
9523 /* The internally generated initializers appear to have missing braces.
9524 Don't warn about this. */
9525 int save_warn_missing_braces = warn_missing_braces;
9526 warn_missing_braces = 0;
9527
9528 /* A missing @end may not be detected by the parser. */
9529 if (objc_implementation_context)
9530 {
9531 warning (0, "%<@end%> missing in implementation context");
9532 finish_class (objc_implementation_context);
9533 objc_ivar_chain = NULL_TREE;
9534 objc_implementation_context = NULL_TREE;
9535 }
9536
9537 /* Process the static instances here because initialization of objc_symtab
9538 depends on them. */
9539 if (objc_static_instances)
9540 generate_static_references ();
9541
9542 /* forward declare categories */
9543 if (cat_count)
9544 forward_declare_categories ();
9545
9546 for (impent = imp_list; impent; impent = impent->next)
9547 {
9548 objc_implementation_context = impent->imp_context;
9549 implementation_template = impent->imp_template;
9550
9551 /* FIXME: This needs reworking to be more obvious. */
9552
9553 UOBJC_CLASS_decl = impent->class_decl;
9554 UOBJC_METACLASS_decl = impent->meta_decl;
9555
9556 /* Dump the @interface of each class as we compile it, if the
9557 -gen-decls option is in use. TODO: Dump the classes in the
9558 order they were found, rather than in reverse order as we
9559 are doing now. */
9560 if (flag_gen_declaration)
9561 {
9562 dump_interface (gen_declaration_file, objc_implementation_context);
9563 }
9564
9565 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
9566 {
9567 /* all of the following reference the string pool... */
9568 generate_ivar_lists ();
9569 generate_dispatch_tables ();
9570 generate_shared_structures (impent);
9571 }
9572 else
9573 {
9574 generate_dispatch_tables ();
9575 generate_category (impent);
9576 }
9577
9578 impent->class_decl = UOBJC_CLASS_decl;
9579 impent->meta_decl = UOBJC_METACLASS_decl;
9580 }
9581
9582 /* If we are using an array of selectors, we must always
9583 finish up the array decl even if no selectors were used. */
9584 if (flag_next_runtime)
9585 build_next_selector_translation_table ();
9586 else
9587 build_gnu_selector_translation_table ();
9588
9589 if (protocol_chain)
9590 generate_protocols ();
9591
9592 if (flag_next_runtime)
9593 generate_objc_image_info ();
9594
9595 if (imp_list || class_names_chain
9596 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
9597 generate_objc_symtab_decl ();
9598
9599 /* Arrange for ObjC data structures to be initialized at run time. */
9600 if (objc_implementation_context || class_names_chain || objc_static_instances
9601 || meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
9602 {
9603 build_module_descriptor ();
9604
9605 if (!flag_next_runtime)
9606 build_module_initializer_routine ();
9607 }
9608
9609 /* Dump the class references. This forces the appropriate classes
9610 to be linked into the executable image, preserving unix archive
9611 semantics. This can be removed when we move to a more dynamically
9612 linked environment. */
9613
9614 for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
9615 {
9616 handle_class_ref (chain);
9617 if (TREE_PURPOSE (chain))
9618 generate_classref_translation_entry (chain);
9619 }
9620
9621 for (impent = imp_list; impent; impent = impent->next)
9622 handle_impent (impent);
9623
9624 if (warn_selector)
9625 {
9626 int slot;
9627 hash hsh;
9628
9629 /* Run through the selector hash tables and print a warning for any
9630 selector which has multiple methods. */
9631
9632 for (slot = 0; slot < SIZEHASHTABLE; slot++)
9633 {
9634 for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
9635 check_duplicates (hsh, 0, 1);
9636 for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
9637 check_duplicates (hsh, 0, 1);
9638 }
9639 }
9640
9641 warn_missing_braces = save_warn_missing_braces;
9642 }
9643 \f
9644 /* Subroutines of finish_objc. */
9645
9646 static void
9647 generate_classref_translation_entry (tree chain)
9648 {
9649 tree expr, decl, type;
9650
9651 decl = TREE_PURPOSE (chain);
9652 type = TREE_TYPE (decl);
9653
9654 expr = add_objc_string (TREE_VALUE (chain), class_names);
9655 expr = convert (type, expr); /* cast! */
9656
9657 /* This is a class reference. It is re-written by the runtime,
9658 but will be optimized away unless we force it. */
9659 DECL_PRESERVE_P (decl) = 1;
9660 finish_var_decl (decl, expr);
9661 return;
9662 }
9663
9664 static void
9665 handle_class_ref (tree chain)
9666 {
9667 const char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
9668 char *string = (char *) alloca (strlen (name) + 30);
9669 tree decl;
9670 tree exp;
9671
9672 sprintf (string, "%sobjc_class_name_%s",
9673 (flag_next_runtime ? "." : "__"), name);
9674
9675 #ifdef ASM_DECLARE_UNRESOLVED_REFERENCE
9676 if (flag_next_runtime)
9677 {
9678 ASM_DECLARE_UNRESOLVED_REFERENCE (asm_out_file, string);
9679 return;
9680 }
9681 #endif
9682
9683 /* Make a decl for this name, so we can use its address in a tree. */
9684 decl = build_decl (input_location,
9685 VAR_DECL, get_identifier (string), TREE_TYPE (integer_zero_node));
9686 DECL_EXTERNAL (decl) = 1;
9687 TREE_PUBLIC (decl) = 1;
9688 pushdecl (decl);
9689 finish_var_decl (decl, 0);
9690
9691 /* Make a decl for the address. */
9692 sprintf (string, "%sobjc_class_ref_%s",
9693 (flag_next_runtime ? "." : "__"), name);
9694 exp = build1 (ADDR_EXPR, string_type_node, decl);
9695 decl = build_decl (input_location,
9696 VAR_DECL, get_identifier (string), string_type_node);
9697 TREE_STATIC (decl) = 1;
9698 TREE_USED (decl) = 1;
9699 DECL_READ_P (decl) = 1;
9700 DECL_ARTIFICIAL (decl) = 1;
9701 DECL_INITIAL (decl) = error_mark_node;
9702
9703 /* We must force the reference. */
9704 DECL_PRESERVE_P (decl) = 1;
9705
9706 pushdecl (decl);
9707 finish_var_decl (decl, exp);
9708 }
9709
9710 static void
9711 handle_impent (struct imp_entry *impent)
9712 {
9713 char *string;
9714
9715 objc_implementation_context = impent->imp_context;
9716 implementation_template = impent->imp_template;
9717
9718 if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
9719 {
9720 const char *const class_name =
9721 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
9722
9723 string = (char *) alloca (strlen (class_name) + 30);
9724
9725 sprintf (string, "%sobjc_class_name_%s",
9726 (flag_next_runtime ? "." : "__"), class_name);
9727 }
9728 else if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
9729 {
9730 const char *const class_name =
9731 IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
9732 const char *const class_super_name =
9733 IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
9734
9735 string = (char *) alloca (strlen (class_name)
9736 + strlen (class_super_name) + 30);
9737
9738 /* Do the same for categories. Even though no references to
9739 these symbols are generated automatically by the compiler, it
9740 gives you a handle to pull them into an archive by hand. */
9741 sprintf (string, "*%sobjc_category_name_%s_%s",
9742 (flag_next_runtime ? "." : "__"), class_name, class_super_name);
9743 }
9744 else
9745 return;
9746
9747 #ifdef ASM_DECLARE_CLASS_REFERENCE
9748 if (flag_next_runtime)
9749 {
9750 ASM_DECLARE_CLASS_REFERENCE (asm_out_file, string);
9751 return;
9752 }
9753 else
9754 #endif
9755 {
9756 tree decl, init;
9757
9758 init = integer_zero_node;
9759 decl = build_decl (input_location,
9760 VAR_DECL, get_identifier (string), TREE_TYPE (init));
9761 TREE_PUBLIC (decl) = 1;
9762 TREE_READONLY (decl) = 1;
9763 TREE_USED (decl) = 1;
9764 TREE_CONSTANT (decl) = 1;
9765 DECL_CONTEXT (decl) = NULL_TREE;
9766 DECL_ARTIFICIAL (decl) = 1;
9767 TREE_STATIC (decl) = 1;
9768 DECL_INITIAL (decl) = error_mark_node; /* A real initializer is coming... */
9769 /* We must force the reference. */
9770 DECL_PRESERVE_P (decl) = 1;
9771
9772 finish_var_decl(decl, init) ;
9773 }
9774 }
9775 \f
9776 /* The Fix-and-Continue functionality available in Mac OS X 10.3 and
9777 later requires that ObjC translation units participating in F&C be
9778 specially marked. The following routine accomplishes this. */
9779
9780 /* static int _OBJC_IMAGE_INFO[2] = { 0, 1 }; */
9781
9782 static void
9783 generate_objc_image_info (void)
9784 {
9785 tree decl;
9786 int flags
9787 = ((flag_replace_objc_classes && imp_count ? 1 : 0)
9788 | (flag_objc_gc ? 2 : 0));
9789 VEC(constructor_elt,gc) *v = NULL;
9790 tree array_type;
9791
9792 if (!flags)
9793 return; /* No need for an image_info entry. */
9794
9795 array_type = build_sized_array_type (integer_type_node, 2);
9796
9797 decl = start_var_decl (array_type, "_OBJC_IMAGE_INFO");
9798
9799 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, integer_zero_node);
9800 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (integer_type_node, flags));
9801 /* If we need this (determined above) it is because the runtime wants to
9802 refer to it in a manner hidden from the compiler. So we must force the
9803 output. */
9804 DECL_PRESERVE_P (decl) = 1;
9805 finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v));
9806 }
9807
9808 /* Look up ID as an instance variable. OTHER contains the result of
9809 the C or C++ lookup, which we may want to use instead. */
9810
9811 tree
9812 objc_lookup_ivar (tree other, tree id)
9813 {
9814 tree ivar;
9815
9816 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9817 if (!objc_method_context)
9818 return other;
9819
9820 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9821 /* We have a message to super. */
9822 return get_super_receiver ();
9823
9824 /* In a class method, look up an instance variable only as a last
9825 resort. */
9826 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9827 && other && other != error_mark_node)
9828 return other;
9829
9830 /* Look up the ivar, but do not use it if it is not accessible. */
9831 ivar = is_ivar (objc_ivar_chain, id);
9832
9833 if (!ivar || is_private (ivar))
9834 return other;
9835
9836 /* In an instance method, a local variable (or parameter) may hide the
9837 instance variable. */
9838 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9839 && other && other != error_mark_node
9840 #ifdef OBJCPLUS
9841 && CP_DECL_CONTEXT (other) != global_namespace)
9842 #else
9843 && !DECL_FILE_SCOPE_P (other))
9844 #endif
9845 {
9846 warning (0, "local declaration of %qE hides instance variable",
9847 id);
9848
9849 return other;
9850 }
9851
9852 /* At this point, we are either in an instance method with no obscuring
9853 local definitions, or in a class method with no alternate definitions
9854 at all. */
9855 return build_ivar_reference (id);
9856 }
9857
9858 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9859 needs to be done if we are calling a function through a cast. */
9860
9861 tree
9862 objc_rewrite_function_call (tree function, tree first_param)
9863 {
9864 if (TREE_CODE (function) == NOP_EXPR
9865 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9866 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9867 == FUNCTION_DECL)
9868 {
9869 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9870 TREE_OPERAND (function, 0),
9871 first_param, size_zero_node);
9872 }
9873
9874 return function;
9875 }
9876
9877 /* Look for the special case of OBJC_TYPE_REF with the address of
9878 a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend or one
9879 of its cousins). */
9880
9881 int
9882 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9883 {
9884 enum gimplify_status r0, r1;
9885 if (TREE_CODE (*expr_p) == OBJ_TYPE_REF
9886 && TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9887 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9888 == FUNCTION_DECL)
9889 {
9890 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9891 value of the OBJ_TYPE_REF, so force them to be emitted
9892 during subexpression evaluation rather than after the
9893 OBJ_TYPE_REF. This permits objc_msgSend calls in Objective
9894 C to use direct rather than indirect calls when the
9895 object expression has a postincrement. */
9896 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9897 is_gimple_val, fb_rvalue);
9898 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9899 is_gimple_val, fb_rvalue);
9900
9901 return MIN (r0, r1);
9902 }
9903
9904 #ifdef OBJCPLUS
9905 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9906 #else
9907 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9908 #endif
9909 }
9910
9911 #include "gt-objc-objc-act.h"