gcc/cp:
[official-gcc.git] / gcc / objc / objc-act.c
blob12614feb0474c5c866db4e5227e120f76f0dc130
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Steve Naroff.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "stringpool.h"
27 #include "stor-layout.h"
28 #include "attribs.h"
30 #ifdef OBJCPLUS
31 #include "cp/cp-tree.h"
32 #else
33 #include "c/c-tree.h"
34 #include "c/c-lang.h"
35 #endif
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "c-family/c-pragma.h"
40 #include "c-family/c-format.h"
41 #include "flags.h"
42 #include "langhooks.h"
43 #include "objc-act.h"
44 #include "objc-map.h"
45 #include "input.h"
46 #include "hashtab.h"
47 #include "hash-set.h"
48 #include "vec.h"
49 #include "machmode.h"
50 #include "hard-reg-set.h"
51 #include "function.h"
52 #include "toplev.h"
53 #include "debug.h"
54 #include "c-family/c-target.h"
55 #include "diagnostic-core.h"
56 #include "intl.h"
57 #include "hash-map.h"
58 #include "is-a.h"
59 #include "plugin-api.h"
60 #include "ipa-ref.h"
61 #include "cgraph.h"
62 #include "tree-iterator.h"
63 #include "hash-table.h"
64 #include "wide-int.h"
65 #include "langhooks-def.h"
66 /* Different initialization, code gen and meta data generation for each
67 runtime. */
68 #include "objc-runtime-hooks.h"
69 /* Routines used mainly by the runtimes. */
70 #include "objc-runtime-shared-support.h"
71 /* For default_tree_printer (). */
72 #include "tree-pretty-print.h"
74 /* For enum gimplify_status */
75 #include "gimple-expr.h"
76 #include "gimplify.h"
78 /* For encode_method_prototype(). */
79 #include "objc-encoding.h"
81 static unsigned int should_call_super_dealloc = 0;
83 /* When building Objective-C++, we are not linking against the C front-end
84 and so need to replicate the C tree-construction functions in some way. */
85 #ifdef OBJCPLUS
86 #define OBJCP_REMAP_FUNCTIONS
87 #include "objcp-decl.h"
88 #endif /* OBJCPLUS */
90 /* This is the default way of generating a method name. */
91 /* This has the problem that "test_method:argument:" and
92 "test:method_argument:" will generate the same name
93 ("_i_Test__test_method_argument_" for an instance method of the
94 class "Test"), so you can't have them both in the same class!
95 Moreover, the demangling (going from
96 "_i_Test__test_method_argument" back to the original name) is
97 undefined because there are two correct ways of demangling the
98 name. */
99 #ifndef OBJC_GEN_METHOD_LABEL
100 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
101 do { \
102 char *temp; \
103 sprintf ((BUF), "_%s_%s_%s_%s", \
104 ((IS_INST) ? "i" : "c"), \
105 (CLASS_NAME), \
106 ((CAT_NAME)? (CAT_NAME) : ""), \
107 (SEL_NAME)); \
108 for (temp = (BUF); *temp; temp++) \
109 if (*temp == ':') *temp = '_'; \
110 } while (0)
111 #endif
113 /* These need specifying. */
114 #ifndef OBJC_FORWARDING_STACK_OFFSET
115 #define OBJC_FORWARDING_STACK_OFFSET 0
116 #endif
118 #ifndef OBJC_FORWARDING_MIN_OFFSET
119 #define OBJC_FORWARDING_MIN_OFFSET 0
120 #endif
122 /*** Private Interface (procedures) ***/
124 /* Init stuff. */
125 static void synth_module_prologue (void);
127 /* Code generation. */
129 static tree start_class (enum tree_code, tree, tree, tree, tree);
130 static tree continue_class (tree);
131 static void finish_class (tree);
132 static void start_method_def (tree, tree);
134 static tree start_protocol (enum tree_code, tree, tree, tree);
135 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
136 static tree objc_add_method (tree, tree, int, bool);
137 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
138 static tree build_ivar_reference (tree);
139 static tree is_ivar (tree, tree);
141 /* We only need the following for ObjC; ObjC++ will use C++'s definition
142 of DERIVED_FROM_P. */
143 #ifndef OBJCPLUS
144 static bool objc_derived_from_p (tree, tree);
145 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
146 #endif
148 /* Property. */
149 static void objc_gen_property_data (tree, tree);
150 static void objc_synthesize_getter (tree, tree, tree);
151 static void objc_synthesize_setter (tree, tree, tree);
152 static tree lookup_property (tree, tree);
153 static tree lookup_property_in_list (tree, tree);
154 static tree lookup_property_in_protocol_list (tree, tree);
155 static void build_common_objc_property_accessor_helpers (void);
157 static void objc_xref_basetypes (tree, tree);
159 static tree get_class_ivars (tree, bool);
161 static void build_fast_enumeration_state_template (void);
163 #ifdef OBJCPLUS
164 static void objc_generate_cxx_cdtors (void);
165 #endif
167 /* objc attribute */
168 static void objc_decl_method_attributes (tree*, tree, int);
169 static tree build_keyword_selector (tree);
171 static void hash_init (void);
173 /* Hash tables to manage the global pool of method prototypes. Each
174 of these maps map a method name (selector) identifier to either a
175 single tree (for methods with a single method prototype) or a
176 TREE_VEC (for methods with multiple method prototypes). */
177 static GTY(()) objc_map_t instance_method_map = 0;
178 static GTY(()) objc_map_t class_method_map = 0;
180 /* Hash tables to manage the global pool of class names. */
182 static GTY(()) objc_map_t class_name_map = 0;
183 static GTY(()) objc_map_t alias_name_map = 0;
185 static tree lookup_method (tree, tree);
186 static tree lookup_method_static (tree, tree, int);
188 static void interface_hash_init (void);
189 static tree add_interface (tree, tree);
190 static void add_category (tree, tree);
191 static inline tree lookup_category (tree, tree);
193 /* Protocols. */
195 static tree lookup_protocol (tree, bool, bool);
196 static tree lookup_and_install_protocols (tree, bool);
198 #ifdef OBJCPLUS
199 static void really_start_method (tree, tree);
200 #else
201 static void really_start_method (tree, struct c_arg_info *);
202 #endif
203 static int comp_proto_with_proto (tree, tree, int);
204 static tree objc_decay_parm_type (tree);
206 /* Utilities for debugging and error diagnostics. */
208 static char *gen_type_name (tree);
209 static char *gen_type_name_0 (tree);
210 static char *gen_method_decl (tree);
211 static char *gen_declaration (tree);
213 /* Everything else. */
215 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
217 static void mark_referenced_methods (void);
218 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
219 static tree check_duplicates (tree, int, int);
221 /*** Private Interface (data) ***/
222 /* Flags for lookup_method_static(). */
224 /* Look for class methods. */
225 #define OBJC_LOOKUP_CLASS 1
226 /* Do not examine superclasses. */
227 #define OBJC_LOOKUP_NO_SUPER 2
228 /* Disable returning an instance method of a root class when a class
229 method can't be found. */
230 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
232 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
233 tree objc_global_trees[OCTI_MAX];
235 struct imp_entry *imp_list = 0;
236 int imp_count = 0; /* `@implementation' */
237 int cat_count = 0; /* `@category' */
239 objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
241 /* Use to generate method labels. */
242 static int method_slot = 0;
244 /* Flag to say whether methods in a protocol are optional or
245 required. */
246 static bool objc_method_optional_flag = false;
248 static int objc_collecting_ivars = 0;
250 /* Flag that is set to 'true' while we are processing a class
251 extension. Since a class extension just "reopens" the main
252 @interface, this can be used to determine if we are in the main
253 @interface, or in a class extension. */
254 static bool objc_in_class_extension = false;
256 static char *errbuf; /* Buffer for error diagnostics */
258 /* An array of all the local variables in the current function that
259 need to be marked as volatile. */
260 vec<tree, va_gc> *local_variables_to_volatilize = NULL;
262 /* Store all constructed constant strings in a hash table so that
263 they get uniqued properly. */
265 struct GTY((for_user)) string_descriptor {
266 /* The literal argument . */
267 tree literal;
269 /* The resulting constant string. */
270 tree constructor;
273 struct objc_string_hasher : ggc_hasher<string_descriptor *>
275 static hashval_t hash (string_descriptor *);
276 static bool equal (string_descriptor *, string_descriptor *);
279 static GTY(()) hash_table<objc_string_hasher> *string_htab;
281 FILE *gen_declaration_file;
283 /* Hooks for stuff that differs between runtimes. */
284 objc_runtime_hooks runtime;
286 /* Create a temporary variable of type 'type'. If 'name' is set, uses
287 the specified name, else use no name. Returns the declaration of
288 the type. The 'name' is mostly useful for debugging.
290 tree
291 objc_create_temporary_var (tree type, const char *name)
293 tree decl;
295 if (name != NULL)
297 decl = build_decl (input_location,
298 VAR_DECL, get_identifier (name), type);
300 else
302 decl = build_decl (input_location,
303 VAR_DECL, NULL_TREE, type);
305 TREE_USED (decl) = 1;
306 DECL_ARTIFICIAL (decl) = 1;
307 DECL_IGNORED_P (decl) = 1;
308 DECL_CONTEXT (decl) = current_function_decl;
310 return decl;
313 /* Some platforms pass small structures through registers versus
314 through an invisible pointer. Determine at what size structure is
315 the transition point between the two possibilities. */
317 static void
318 generate_struct_by_value_array (void)
320 tree type;
321 tree decls;
322 int i, j;
323 int aggregate_in_mem[32];
324 int found = 0;
326 /* Presumably no platform passes 32 byte structures in a register. */
327 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
328 in registers. */
329 for (i = 1; i < 32; i++)
331 char buffer[5];
332 tree *chain = NULL;
334 /* Create an unnamed struct that has `i' character components */
335 type = objc_start_struct (NULL_TREE);
337 strcpy (buffer, "c1");
338 decls = add_field_decl (char_type_node, buffer, &chain);
340 for (j = 1; j < i; j++)
342 sprintf (buffer, "c%d", j + 1);
343 add_field_decl (char_type_node, buffer, &chain);
345 objc_finish_struct (type, decls);
347 aggregate_in_mem[i] = aggregate_value_p (type, 0);
348 if (!aggregate_in_mem[i])
349 found = 1;
352 /* We found some structures that are returned in registers instead of memory
353 so output the necessary data. */
354 if (found)
356 for (i = 31; i >= 0; i--)
357 if (!aggregate_in_mem[i])
358 break;
359 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
362 exit (0);
365 bool
366 objc_init (void)
368 bool ok;
369 #ifdef OBJCPLUS
370 if (cxx_init () == false)
371 #else
372 if (c_objc_common_init () == false)
373 #endif
374 return false;
376 /* print_struct_values is triggered by -print-runtime-info (used
377 when building libobjc, with an empty file as input). It does not
378 require any ObjC setup, and it never returns.
380 -fcompare-debug is used to check the compiler output; we are
381 executed twice, once with flag_compare_debug set, and once with
382 it not set. If the flag is used together with
383 -print-runtime-info, we want to print the runtime info only once,
384 else it would be output in duplicate. So we check
385 flag_compare_debug to output it in only one of the invocations.
387 As a side effect, this also that means -fcompare-debug
388 -print-runtime-info will run the compiler twice, and compare the
389 generated assembler file; the first time the compiler exits
390 immediately (producing no file), and the second time it compiles
391 an empty file. This checks, as a side effect, that compiling an
392 empty file produces no assembler output. */
393 if (print_struct_values && !flag_compare_debug)
394 generate_struct_by_value_array ();
396 /* Set up stuff used by FE parser and all runtimes. */
397 errbuf = XNEWVEC (char, 1024 * 10);
398 interface_hash_init ();
399 hash_init ();
400 objc_encoding_init ();
401 /* ... and then check flags and set-up for the selected runtime ... */
402 if (flag_next_runtime && flag_objc_abi >= 2)
403 ok = objc_next_runtime_abi_02_init (&runtime);
404 else if (flag_next_runtime)
405 ok = objc_next_runtime_abi_01_init (&runtime);
406 else
407 ok = objc_gnu_runtime_abi_01_init (&runtime);
409 /* If that part of the setup failed - bail out immediately. */
410 if (!ok)
411 return false;
413 /* Determine the default visibility for instance variables. */
414 switch (default_ivar_visibility)
416 case IVAR_VISIBILITY_PRIVATE:
417 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
418 break;
419 case IVAR_VISIBILITY_PUBLIC:
420 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
421 break;
422 case IVAR_VISIBILITY_PACKAGE:
423 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
424 break;
425 default:
426 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
429 /* Generate general types and push runtime-specific decls to file scope. */
430 synth_module_prologue ();
432 return true;
435 /* This is called automatically (at the very end of compilation) by
436 c_write_global_declarations and cp_write_global_declarations. */
437 void
438 objc_write_global_declarations (void)
440 mark_referenced_methods ();
442 /* A missing @end might not be detected by the parser. */
443 if (objc_implementation_context)
445 warning (0, "%<@end%> missing in implementation context");
446 finish_class (objc_implementation_context);
447 objc_ivar_chain = NULL_TREE;
448 objc_implementation_context = NULL_TREE;
451 if (warn_selector)
453 objc_map_iterator_t i;
455 objc_map_iterator_initialize (class_method_map, &i);
456 while (objc_map_iterator_move_to_next (class_method_map, &i))
457 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
459 objc_map_iterator_initialize (instance_method_map, &i);
460 while (objc_map_iterator_move_to_next (instance_method_map, &i))
461 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
464 /* TODO: consider an early exit here if either errorcount or sorrycount
465 is non-zero. Not only is it wasting time to generate the metadata,
466 it needlessly imposes need to re-check for things that are already
467 determined to be errors. */
469 /* Finalize Objective-C runtime data. No need to generate tables
470 and code if only checking syntax, or if generating a PCH file. */
471 if (!flag_syntax_only && !pch_file)
473 location_t saved_location;
475 /* If gen_declaration desired, open the output file. */
476 if (flag_gen_declaration)
478 char * const dumpname = concat (dump_base_name, ".decl", NULL);
479 gen_declaration_file = fopen (dumpname, "w");
480 if (gen_declaration_file == 0)
481 fatal_error ("can%'t open %s: %m", dumpname);
482 free (dumpname);
485 /* Set the input location to BUILTINS_LOCATION. This is good
486 for error messages, in case any is generated while producing
487 the metadata, but it also silences warnings that would be
488 produced when compiling with -Wpadded in case when padding is
489 automatically added to the built-in runtime data structure
490 declarations. We know about this padding, and it is fine; we
491 don't want users to see any warnings about it if they use
492 -Wpadded. */
493 saved_location = input_location;
494 input_location = BUILTINS_LOCATION;
496 /* Compute and emit the meta-data tables for this runtime. */
497 (*runtime.generate_metadata) ();
499 /* Restore the original location, just in case it mattered. */
500 input_location = saved_location;
502 /* ... and then close any declaration file we opened. */
503 if (gen_declaration_file)
504 fclose (gen_declaration_file);
508 /* Return the first occurrence of a method declaration corresponding
509 to sel_name in rproto_list. Search rproto_list recursively.
510 If is_class is 0, search for instance methods, otherwise for class
511 methods. */
512 static tree
513 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
514 int is_class)
516 tree rproto, p, m;
518 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
520 p = TREE_VALUE (rproto);
521 m = NULL_TREE;
523 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
525 /* First, search the @required protocol methods. */
526 if (is_class)
527 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
528 else
529 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
531 if (m)
532 return m;
534 /* If still not found, search the @optional protocol methods. */
535 if (is_class)
536 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
537 else
538 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
540 if (m)
541 return m;
543 /* If still not found, search the attached protocols. */
544 if (PROTOCOL_LIST (p))
545 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
546 sel_name, is_class);
547 if (m)
548 return m;
550 else
552 ; /* An identifier...if we could not find a protocol. */
556 return 0;
559 static tree
560 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
562 tree rproto, p;
564 /* Make sure the protocol is supported by the object on the rhs. */
565 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
567 tree fnd = 0;
568 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
570 p = TREE_VALUE (rproto);
572 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
574 if (lproto == p)
575 fnd = lproto;
577 else if (PROTOCOL_LIST (p))
578 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
581 if (fnd)
582 return fnd;
585 else
587 ; /* An identifier...if we could not find a protocol. */
590 return 0;
593 void
594 objc_start_class_interface (tree klass, tree super_class,
595 tree protos, tree attributes)
597 if (flag_objc1_only && attributes)
598 error_at (input_location, "class attributes are not available in Objective-C 1.0");
600 objc_interface_context
601 = objc_ivar_context
602 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
603 objc_ivar_visibility = objc_default_ivar_visibility;
606 void
607 objc_start_category_interface (tree klass, tree categ,
608 tree protos, tree attributes)
610 if (attributes)
612 if (flag_objc1_only)
613 error_at (input_location, "category attributes are not available in Objective-C 1.0");
614 else
615 warning_at (input_location, OPT_Wattributes,
616 "category attributes are not available in this version"
617 " of the compiler, (ignored)");
619 if (categ == NULL_TREE)
621 if (flag_objc1_only)
622 error_at (input_location, "class extensions are not available in Objective-C 1.0");
623 else
625 /* Iterate over all the classes and categories implemented
626 up to now in this compilation unit. */
627 struct imp_entry *t;
629 for (t = imp_list; t; t = t->next)
631 /* If we find a class @implementation with the same name
632 as the one we are extending, produce an error. */
633 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
634 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
635 error_at (input_location,
636 "class extension for class %qE declared after its %<@implementation%>",
637 klass);
641 objc_interface_context
642 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
643 objc_ivar_chain
644 = continue_class (objc_interface_context);
647 void
648 objc_start_protocol (tree name, tree protos, tree attributes)
650 if (flag_objc1_only && attributes)
651 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
653 objc_interface_context
654 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
655 objc_method_optional_flag = false;
658 void
659 objc_continue_interface (void)
661 objc_ivar_chain
662 = continue_class (objc_interface_context);
665 void
666 objc_finish_interface (void)
668 finish_class (objc_interface_context);
669 objc_interface_context = NULL_TREE;
670 objc_method_optional_flag = false;
671 objc_in_class_extension = false;
674 void
675 objc_start_class_implementation (tree klass, tree super_class)
677 objc_implementation_context
678 = objc_ivar_context
679 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
680 NULL_TREE);
681 objc_ivar_visibility = objc_default_ivar_visibility;
684 void
685 objc_start_category_implementation (tree klass, tree categ)
687 objc_implementation_context
688 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
689 NULL_TREE);
690 objc_ivar_chain
691 = continue_class (objc_implementation_context);
694 void
695 objc_continue_implementation (void)
697 objc_ivar_chain
698 = continue_class (objc_implementation_context);
701 void
702 objc_finish_implementation (void)
704 #ifdef OBJCPLUS
705 if (flag_objc_call_cxx_cdtors)
706 objc_generate_cxx_cdtors ();
707 #endif
709 if (objc_implementation_context)
711 finish_class (objc_implementation_context);
712 objc_ivar_chain = NULL_TREE;
713 objc_implementation_context = NULL_TREE;
715 else
716 warning (0, "%<@end%> must appear in an @implementation context");
719 void
720 objc_set_visibility (objc_ivar_visibility_kind visibility)
722 if (visibility == OBJC_IVAR_VIS_PACKAGE)
724 if (flag_objc1_only)
725 error ("%<@package%> is not available in Objective-C 1.0");
726 else
727 warning (0, "%<@package%> presently has the same effect as %<@public%>");
729 objc_ivar_visibility = visibility;
732 void
733 objc_set_method_opt (bool optional)
735 if (flag_objc1_only)
737 if (optional)
738 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
739 else
740 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
743 objc_method_optional_flag = optional;
744 if (!objc_interface_context
745 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
747 if (optional)
748 error ("%<@optional%> is allowed in @protocol context only");
749 else
750 error ("%<@required%> is allowed in @protocol context only");
751 objc_method_optional_flag = false;
755 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
756 PROTOCOL. */
757 static tree
758 lookup_property_in_list (tree chain, tree property)
760 tree x;
761 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
762 if (PROPERTY_NAME (x) == property)
763 return x;
764 return NULL_TREE;
767 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
768 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
770 tree rproto, x;
771 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
773 tree p = TREE_VALUE (rproto);
774 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
776 if ((x = lookup_property_in_list (p, property)))
777 return x;
778 if (PROTOCOL_LIST (p))
779 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
781 else
783 ; /* An identifier...if we could not find a protocol. */
786 return NULL_TREE;
789 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
790 chain of interface hierarchy. */
791 static tree
792 lookup_property (tree interface_type, tree property)
794 tree inter = interface_type;
795 while (inter)
797 tree x, category;
798 if ((x = lookup_property_in_list (inter, property)))
799 return x;
800 /* Failing that, look for the property in each category of the class. */
801 category = inter;
802 while ((category = CLASS_CATEGORY_LIST (category)))
804 if ((x = lookup_property_in_list (category, property)))
805 return x;
807 /* When checking a category, also check the protocols
808 attached with the category itself. */
809 if (CLASS_PROTOCOL_LIST (category)
810 && (x = lookup_property_in_protocol_list
811 (CLASS_PROTOCOL_LIST (category), property)))
812 return x;
815 /* Failing to find in categories, look for property in protocol list. */
816 if (CLASS_PROTOCOL_LIST (inter)
817 && (x = lookup_property_in_protocol_list
818 (CLASS_PROTOCOL_LIST (inter), property)))
819 return x;
821 /* Failing that, climb up the inheritance hierarchy. */
822 inter = lookup_interface (CLASS_SUPER_NAME (inter));
824 return inter;
827 /* This routine is called by the parser when a
828 @property... declaration is found. 'decl' is the declaration of
829 the property (type/identifier), and the other arguments represent
830 property attributes that may have been specified in the Objective-C
831 declaration. 'parsed_property_readonly' is 'true' if the attribute
832 'readonly' was specified, and 'false' if not; similarly for the
833 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
834 if the attribute 'getter' was not specified, and is the identifier
835 corresponding to the specified getter if it was; similarly for
836 'parsed_property_setter_ident'. */
837 void
838 objc_add_property_declaration (location_t location, tree decl,
839 bool parsed_property_readonly, bool parsed_property_readwrite,
840 bool parsed_property_assign, bool parsed_property_retain,
841 bool parsed_property_copy, bool parsed_property_nonatomic,
842 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
844 tree property_decl;
845 tree x;
846 /* 'property_readonly' and 'property_assign_semantics' are the final
847 attributes of the property after all parsed attributes have been
848 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
849 parsed_property_readonly = false and parsed_property_readwrite =
850 false, then property_readonly will be false because the default
851 is readwrite). */
852 bool property_readonly = false;
853 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
854 bool property_extension_in_class_extension = false;
856 if (flag_objc1_only)
857 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
859 if (parsed_property_readonly && parsed_property_readwrite)
861 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
862 /* In case of conflicting attributes (here and below), after
863 producing an error, we pick one of the attributes and keep
864 going. */
865 property_readonly = false;
867 else
869 if (parsed_property_readonly)
870 property_readonly = true;
872 if (parsed_property_readwrite)
873 property_readonly = false;
876 if (parsed_property_readonly && parsed_property_setter_ident)
878 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
879 property_readonly = false;
882 if (parsed_property_assign && parsed_property_retain)
884 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
885 property_assign_semantics = OBJC_PROPERTY_RETAIN;
887 else if (parsed_property_assign && parsed_property_copy)
889 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
890 property_assign_semantics = OBJC_PROPERTY_COPY;
892 else if (parsed_property_retain && parsed_property_copy)
894 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
895 property_assign_semantics = OBJC_PROPERTY_COPY;
897 else
899 if (parsed_property_assign)
900 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
902 if (parsed_property_retain)
903 property_assign_semantics = OBJC_PROPERTY_RETAIN;
905 if (parsed_property_copy)
906 property_assign_semantics = OBJC_PROPERTY_COPY;
909 if (!objc_interface_context)
911 error_at (location, "property declaration not in @interface or @protocol context");
912 return;
915 /* At this point we know that we are either in an interface, a
916 category, or a protocol. */
918 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
919 something else, as that would confuse the checks below. */
920 if (TREE_CODE (decl) != FIELD_DECL)
922 error_at (location, "invalid property declaration");
923 return;
926 /* Do some spot-checks for the most obvious invalid types. */
928 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
930 error_at (location, "property can not be an array");
931 return;
934 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
935 parsing, while the C/ObjC parser accepts it and gives us a
936 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
937 to check for a bitfield when doing ObjC. */
938 #ifndef OBJCPLUS
939 if (DECL_INITIAL (decl))
941 /* A @property is not an actual variable, but it is a way to
942 describe a pair of accessor methods, so its type (which is
943 the type of the return value of the getter and the first
944 argument of the setter) can't be a bitfield (as return values
945 and arguments of functions can not be bitfields). The
946 underlying instance variable could be a bitfield, but that is
947 a different matter. */
948 error_at (location, "property can not be a bit-field");
949 return;
951 #endif
953 /* TODO: Check that the property type is an Objective-C object or a
954 "POD". */
956 /* Implement -Wproperty-assign-default (which is enabled by default). */
957 if (warn_property_assign_default
958 /* If garbage collection is not being used, then 'assign' is
959 valid for objects (and typically used for delegates) but it
960 is wrong in most cases (since most objects need to be
961 retained or copied in setters). Warn users when 'assign' is
962 used implicitly. */
963 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
964 /* Read-only properties are never assigned, so the assignment
965 semantics do not matter in that case. */
966 && !property_readonly
967 && !flag_objc_gc)
969 /* Please note that it would make sense to default to 'assign'
970 for non-{Objective-C objects}, and to 'retain' for
971 Objective-C objects. But that would break compatibility with
972 other compilers. */
973 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
975 /* Use 'false' so we do not warn for Class objects. */
976 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
978 warning_at (location,
980 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
981 decl);
982 inform (location,
983 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
988 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
989 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
990 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
992 if (property_assign_semantics == OBJC_PROPERTY_COPY
993 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
994 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
996 /* Now determine the final property getter and setter names. They
997 will be stored in the PROPERTY_DECL, from which they'll always be
998 extracted and used. */
1000 /* Adjust, or fill in, setter and getter names. We overwrite the
1001 parsed_property_setter_ident and parsed_property_getter_ident
1002 with the final setter and getter identifiers that will be
1003 used. */
1004 if (parsed_property_setter_ident)
1006 /* The setter should be terminated by ':', but the parser only
1007 gives us an identifier without ':'. So, we need to add ':'
1008 at the end. */
1009 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1010 size_t length = strlen (parsed_setter);
1011 char *final_setter = (char *)alloca (length + 2);
1013 sprintf (final_setter, "%s:", parsed_setter);
1014 parsed_property_setter_ident = get_identifier (final_setter);
1016 else
1018 if (!property_readonly)
1019 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1020 (DECL_NAME (decl)));
1023 if (!parsed_property_getter_ident)
1024 parsed_property_getter_ident = DECL_NAME (decl);
1026 /* Check for duplicate property declarations. We first check the
1027 immediate context for a property with the same name. Any such
1028 declarations are an error, unless this is a class extension and
1029 we are extending a property from readonly to readwrite. */
1030 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1032 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1034 if (objc_in_class_extension
1035 && property_readonly == 0
1036 && PROPERTY_READONLY (x) == 1)
1038 /* This is a class extension, and we are extending an
1039 existing readonly property to a readwrite one.
1040 That's fine. :-) */
1041 property_extension_in_class_extension = true;
1042 break;
1044 else
1046 location_t original_location = DECL_SOURCE_LOCATION (x);
1048 error_at (location, "redeclaration of property %qD", decl);
1050 if (original_location != UNKNOWN_LOCATION)
1051 inform (original_location, "originally specified here");
1052 return;
1057 /* If x is not NULL_TREE, we must be in a class extension and we're
1058 extending a readonly property. In that case, no point in
1059 searching for another declaration. */
1060 if (x == NULL_TREE)
1062 /* We now need to check for existing property declarations (in
1063 the superclass, other categories or protocols) and check that
1064 the new declaration is not in conflict with existing
1065 ones. */
1067 /* Search for a previous, existing declaration of a property
1068 with the same name in superclasses, protocols etc. If one is
1069 found, it will be in the 'x' variable. */
1071 /* Note that, for simplicity, the following may search again the
1072 local context. That's Ok as nothing will be found (else we'd
1073 have thrown an error above); it's only a little inefficient,
1074 but the code is simpler. */
1075 switch (TREE_CODE (objc_interface_context))
1077 case CLASS_INTERFACE_TYPE:
1078 /* Look up the property in the current @interface (which
1079 will find nothing), then its protocols and categories and
1080 superclasses. */
1081 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1082 break;
1083 case CATEGORY_INTERFACE_TYPE:
1084 /* Look up the property in the main @interface, then
1085 protocols and categories (one of them is ours, and will
1086 find nothing) and superclasses. */
1087 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1088 DECL_NAME (decl));
1089 break;
1090 case PROTOCOL_INTERFACE_TYPE:
1091 /* Looks up the property in any protocols attached to the
1092 current protocol. */
1093 if (PROTOCOL_LIST (objc_interface_context))
1095 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1096 DECL_NAME (decl));
1098 break;
1099 default:
1100 gcc_unreachable ();
1104 if (x != NULL_TREE)
1106 /* An existing property was found; check that it has the same
1107 types, or it is compatible. */
1108 location_t original_location = DECL_SOURCE_LOCATION (x);
1110 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1112 warning_at (location, 0,
1113 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1115 if (original_location != UNKNOWN_LOCATION)
1116 inform (original_location, "originally specified here");
1117 return;
1120 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1122 warning_at (location, 0,
1123 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1125 if (original_location != UNKNOWN_LOCATION)
1126 inform (original_location, "originally specified here");
1127 return;
1130 /* We can only compare the setter names if both the old and new property have a setter. */
1131 if (!property_readonly && !PROPERTY_READONLY(x))
1133 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1135 warning_at (location, 0,
1136 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1138 if (original_location != UNKNOWN_LOCATION)
1139 inform (original_location, "originally specified here");
1140 return;
1144 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1146 warning_at (location, 0,
1147 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1149 if (original_location != UNKNOWN_LOCATION)
1150 inform (original_location, "originally specified here");
1151 return;
1154 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1155 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1157 warning_at (location, 0,
1158 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1160 if (original_location != UNKNOWN_LOCATION)
1161 inform (original_location, "originally specified here");
1162 return;
1165 /* We now check that the new and old property declarations have
1166 the same types (or compatible one). In the Objective-C
1167 tradition of loose type checking, we do type-checking but
1168 only generate warnings (not errors) if they do not match.
1169 For non-readonly properties, the types must match exactly;
1170 for readonly properties, it is allowed to use a "more
1171 specialized" type in the new property declaration. Eg, the
1172 superclass has a getter returning (NSArray *) and the
1173 subclass a getter returning (NSMutableArray *). The object's
1174 getter returns an (NSMutableArray *); but if you cast the
1175 object to the superclass, which is allowed, you'd still
1176 expect the getter to return an (NSArray *), which works since
1177 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1178 objects belonging to the type of the new @property should be
1179 a subset of the set of objects belonging to the type of the
1180 old @property. This is what "specialization" means. And the
1181 reason it only applies to readonly properties is that for a
1182 readwrite property the setter would have the opposite
1183 requirement - ie that the superclass type is more specialized
1184 then the subclass one; hence the only way to satisfy both
1185 constraints is that the types match. */
1187 /* If the types are not the same in the C sense, we warn ... */
1188 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1189 /* ... unless the property is readonly, in which case we
1190 allow a new, more specialized, declaration. */
1191 && (!property_readonly
1192 || !objc_compare_types (TREE_TYPE (x),
1193 TREE_TYPE (decl), -5, NULL_TREE)))
1195 warning_at (location, 0,
1196 "type of property %qD conflicts with previous declaration", decl);
1197 if (original_location != UNKNOWN_LOCATION)
1198 inform (original_location, "originally specified here");
1199 return;
1202 /* If we are in a class extension and we're extending a readonly
1203 property in the main @interface, we'll just update the
1204 existing property with the readwrite flag and potentially the
1205 new setter name. */
1206 if (property_extension_in_class_extension)
1208 PROPERTY_READONLY (x) = 0;
1209 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1210 return;
1214 /* Create a PROPERTY_DECL node. */
1215 property_decl = make_node (PROPERTY_DECL);
1217 /* Copy the basic information from the original decl. */
1218 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1219 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1220 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1222 /* Add property-specific information. */
1223 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1224 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1225 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1226 PROPERTY_READONLY (property_decl) = property_readonly;
1227 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1228 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1229 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1230 PROPERTY_DYNAMIC (property_decl) = 0;
1232 /* Remember the fact that the property was found in the @optional
1233 section in a @protocol, or not. */
1234 if (objc_method_optional_flag)
1235 PROPERTY_OPTIONAL (property_decl) = 1;
1236 else
1237 PROPERTY_OPTIONAL (property_decl) = 0;
1239 /* Note that PROPERTY_GETTER_NAME is always set for all
1240 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1241 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1242 with a getter or setter, we should get the PROPERTY_DECL and use
1243 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1244 names. */
1246 /* Add the PROPERTY_DECL to the list of properties for the class. */
1247 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1248 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1251 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1252 list of methods in the interface (and, failing that, the local list
1253 in the implementation, and failing that, the protocol list)
1254 provided for a 'setter' or 'getter' for 'component' with default
1255 names (ie, if 'component' is "name", then search for "name" and
1256 "setName:"). It is also possible to specify a different
1257 'getter_name' (this is used for @optional readonly properties). If
1258 any is found, then create an artificial property that uses them.
1259 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1260 static tree
1261 maybe_make_artificial_property_decl (tree interface, tree implementation,
1262 tree protocol_list, tree component, bool is_class,
1263 tree getter_name)
1265 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1266 tree getter = NULL_TREE;
1267 tree setter = NULL_TREE;
1269 if (getter_name == NULL_TREE)
1270 getter_name = component;
1272 /* First, check the @interface and all superclasses. */
1273 if (interface)
1275 int flags = 0;
1277 /* Using instance methods of the root class as accessors is most
1278 likely unwanted and can be extremely confusing (and, most
1279 importantly, other Objective-C 2.0 compilers do not do it).
1280 Turn it off. */
1281 if (is_class)
1282 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1284 getter = lookup_method_static (interface, getter_name, flags);
1285 setter = lookup_method_static (interface, setter_name, flags);
1288 /* Second, check the local @implementation context. */
1289 if (!getter && !setter)
1291 if (implementation)
1293 if (is_class)
1295 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1296 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1298 else
1300 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1301 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1306 /* Try the protocol_list if we didn't find anything in the
1307 @interface and in the @implementation. */
1308 if (!getter && !setter)
1310 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1311 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1314 /* There needs to be at least a getter or setter for this to be a
1315 valid 'object.component' syntax. */
1316 if (getter || setter)
1318 /* Yes ... determine the type of the expression. */
1319 tree property_decl;
1320 tree type;
1322 if (getter)
1323 type = TREE_VALUE (TREE_TYPE (getter));
1324 else
1325 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1327 /* Create an artificial property declaration with the
1328 information we collected on the type and getter/setter
1329 names. */
1330 property_decl = make_node (PROPERTY_DECL);
1332 TREE_TYPE (property_decl) = type;
1333 DECL_SOURCE_LOCATION (property_decl) = input_location;
1334 TREE_DEPRECATED (property_decl) = 0;
1335 DECL_ARTIFICIAL (property_decl) = 1;
1337 /* Add property-specific information. Note that one of
1338 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1339 non-existing method; this will generate an error when the
1340 expression is later compiled. At this stage we don't know if
1341 the getter or setter will be used, so we can't generate an
1342 error. */
1343 PROPERTY_NAME (property_decl) = component;
1344 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1345 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1346 PROPERTY_READONLY (property_decl) = 0;
1347 PROPERTY_NONATOMIC (property_decl) = 0;
1348 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1349 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1350 PROPERTY_DYNAMIC (property_decl) = 0;
1351 PROPERTY_OPTIONAL (property_decl) = 0;
1353 if (!getter)
1354 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1356 /* The following is currently unused, but it's nice to have
1357 there. We may use it if we need in the future. */
1358 if (!setter)
1359 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1361 return property_decl;
1364 return NULL_TREE;
1367 /* This hook routine is invoked by the parser when an expression such
1368 as 'xxx.yyy' is parsed. We get a chance to process these
1369 expressions in a way that is specified to Objective-C (to implement
1370 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1371 If the expression is not an Objective-C specified expression, we
1372 should return NULL_TREE; else we return the expression.
1374 At the moment this only implements dot-syntax and properties (not
1375 non-fragile ivars yet), ie 'object.property' or 'object.component'
1376 where 'component' is not a declared property, but a valid getter or
1377 setter for it could be found. */
1378 tree
1379 objc_maybe_build_component_ref (tree object, tree property_ident)
1381 tree x = NULL_TREE;
1382 tree rtype;
1384 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1385 not available. */
1386 if (flag_objc1_only)
1387 return NULL_TREE;
1389 /* Try to determine if 'object' is an Objective-C object or not. If
1390 not, return. */
1391 if (object == NULL_TREE || object == error_mark_node
1392 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1393 return NULL_TREE;
1395 if (property_ident == NULL_TREE || property_ident == error_mark_node
1396 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1397 return NULL_TREE;
1399 /* The following analysis of 'object' is similar to the one used for
1400 the 'receiver' of a method invocation. We need to determine what
1401 'object' is and find the appropriate property (either declared,
1402 or artificial) for it (in the same way as we need to find the
1403 appropriate method prototype for a method invocation). There are
1404 some simplifications here though: "object.property" is invalid if
1405 "object" has a type of "id" or "Class"; it must at least have a
1406 protocol attached to it, and "object" is never a class name as
1407 that is done by objc_build_class_component_ref. Finally, we
1408 don't know if this really is a dot-syntax expression, so we want
1409 to make a quick exit if it is not; for this reason, we try to
1410 postpone checks after determining that 'object' looks like an
1411 Objective-C object. */
1413 if (objc_is_id (rtype))
1415 /* This is the case that the 'object' is of type 'id' or
1416 'Class'. */
1418 /* Check if at least it is of type 'id <Protocol>' or 'Class
1419 <Protocol>'; if so, look the property up in the
1420 protocols. */
1421 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1423 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1425 if (rprotos)
1427 /* No point looking up declared @properties if we are
1428 dealing with a class. Classes have no declared
1429 properties. */
1430 if (!IS_CLASS (rtype))
1431 x = lookup_property_in_protocol_list (rprotos, property_ident);
1433 if (x == NULL_TREE)
1435 /* Ok, no property. Maybe it was an
1436 object.component dot-syntax without a declared
1437 property (this is valid for classes too). Look
1438 for getter/setter methods and internally declare
1439 an artificial property based on them if found. */
1440 x = maybe_make_artificial_property_decl (NULL_TREE,
1441 NULL_TREE,
1442 rprotos,
1443 property_ident,
1444 IS_CLASS (rtype),
1445 NULL_TREE);
1447 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1449 /* This is a special, complicated case. If the
1450 property is optional, and is read-only, then the
1451 property is always used for reading, but an
1452 eventual existing non-property setter can be used
1453 for writing. We create an artificial property
1454 decl copying the getter from the optional
1455 property, and looking up the setter in the
1456 interface. */
1457 x = maybe_make_artificial_property_decl (NULL_TREE,
1458 NULL_TREE,
1459 rprotos,
1460 property_ident,
1461 false,
1462 PROPERTY_GETTER_NAME (x));
1466 else if (objc_method_context)
1468 /* Else, if we are inside a method it could be the case of
1469 'super' or 'self'. */
1470 tree interface_type = NULL_TREE;
1471 tree t = object;
1472 while (TREE_CODE (t) == COMPOUND_EXPR
1473 || TREE_CODE (t) == MODIFY_EXPR
1474 || CONVERT_EXPR_P (t)
1475 || TREE_CODE (t) == COMPONENT_REF)
1476 t = TREE_OPERAND (t, 0);
1478 if (t == UOBJC_SUPER_decl)
1479 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1480 else if (t == self_decl)
1481 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1483 if (interface_type)
1485 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1486 x = lookup_property (interface_type, property_ident);
1488 if (x == NULL_TREE)
1490 /* Try the dot-syntax without a declared property.
1491 If this is an access to 'self', it is possible
1492 that they may refer to a setter/getter that is
1493 not declared in the interface, but exists locally
1494 in the implementation. In that case, get the
1495 implementation context and use it. */
1496 tree implementation = NULL_TREE;
1498 if (t == self_decl)
1499 implementation = objc_implementation_context;
1501 x = maybe_make_artificial_property_decl
1502 (interface_type, implementation, NULL_TREE,
1503 property_ident,
1504 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1505 NULL_TREE);
1507 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1509 tree implementation = NULL_TREE;
1511 if (t == self_decl)
1512 implementation = objc_implementation_context;
1514 x = maybe_make_artificial_property_decl (interface_type,
1515 implementation,
1516 NULL_TREE,
1517 property_ident,
1518 false,
1519 PROPERTY_GETTER_NAME (x));
1524 else
1526 /* This is the case where we have more information on 'rtype'. */
1527 tree basetype = TYPE_MAIN_VARIANT (rtype);
1529 /* Skip the pointer - if none, it's not an Objective-C object or
1530 class. */
1531 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1532 basetype = TREE_TYPE (basetype);
1533 else
1534 return NULL_TREE;
1536 /* Traverse typedefs. */
1537 while (basetype != NULL_TREE
1538 && TREE_CODE (basetype) == RECORD_TYPE
1539 && OBJC_TYPE_NAME (basetype)
1540 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1541 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1542 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1544 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1546 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1547 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1549 if (interface_type
1550 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1551 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1552 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1554 /* Not sure 'rtype' could ever be a class here! Just
1555 for safety we keep the checks. */
1556 if (!IS_CLASS (rtype))
1558 x = lookup_property (interface_type, property_ident);
1560 if (x == NULL_TREE)
1561 x = lookup_property_in_protocol_list (protocol_list,
1562 property_ident);
1565 if (x == NULL_TREE)
1567 /* Try the dot-syntax without a declared property.
1568 If we are inside a method implementation, it is
1569 possible that they may refer to a setter/getter
1570 that is not declared in the interface, but exists
1571 locally in the implementation. In that case, get
1572 the implementation context and use it. */
1573 tree implementation = NULL_TREE;
1575 if (objc_implementation_context
1576 && CLASS_NAME (objc_implementation_context)
1577 == OBJC_TYPE_NAME (interface_type))
1578 implementation = objc_implementation_context;
1580 x = maybe_make_artificial_property_decl (interface_type,
1581 implementation,
1582 protocol_list,
1583 property_ident,
1584 IS_CLASS (rtype),
1585 NULL_TREE);
1587 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1589 tree implementation = NULL_TREE;
1591 if (objc_implementation_context
1592 && CLASS_NAME (objc_implementation_context)
1593 == OBJC_TYPE_NAME (interface_type))
1594 implementation = objc_implementation_context;
1596 x = maybe_make_artificial_property_decl (interface_type,
1597 implementation,
1598 protocol_list,
1599 property_ident,
1600 false,
1601 PROPERTY_GETTER_NAME (x));
1607 if (x)
1609 tree expression;
1610 tree getter_call;
1611 tree deprecated_method_prototype = NULL_TREE;
1613 /* We have an additional nasty problem here; if this
1614 PROPERTY_REF needs to become a 'getter', then the conversion
1615 from PROPERTY_REF into a getter call happens in gimplify,
1616 after the selector table has already been generated and when
1617 it is too late to add another selector to it. To work around
1618 the problem, we always create the getter call at this stage,
1619 which puts the selector in the table. Note that if the
1620 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1621 we have added a selector too many to the selector table.
1622 This is a little inefficient.
1624 Also note that method calls to 'self' and 'super' require the
1625 context (self_decl, UOBJS_SUPER_decl,
1626 objc_implementation_context etc) to be built correctly; this
1627 is yet another reason why building the call at the gimplify
1628 stage (when this context has been lost) is not very
1629 practical. If we build it at this stage, we know it will
1630 always be built correctly.
1632 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1633 property decl created to deal with a dotsyntax not really
1634 referring to an existing property) then do not try to build a
1635 call to the getter as there is no getter. */
1636 if (PROPERTY_HAS_NO_GETTER (x))
1637 getter_call = NULL_TREE;
1638 else
1639 getter_call = objc_finish_message_expr
1640 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1641 /* Disable the immediate deprecation warning if the getter
1642 is deprecated, but record the fact that the getter is
1643 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1644 the method prototype. */
1645 &deprecated_method_prototype);
1647 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1648 deprecated_method_prototype);
1649 SET_EXPR_LOCATION (expression, input_location);
1650 TREE_SIDE_EFFECTS (expression) = 1;
1652 return expression;
1655 return NULL_TREE;
1658 /* This hook routine is invoked by the parser when an expression such
1659 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1660 Objective-C 2.0 dot-syntax applied to classes, so we need to
1661 convert it into a setter/getter call on the class. */
1662 tree
1663 objc_build_class_component_ref (tree class_name, tree property_ident)
1665 tree x = NULL_TREE;
1666 tree object, rtype;
1668 if (flag_objc1_only)
1669 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1671 if (class_name == NULL_TREE || class_name == error_mark_node
1672 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1673 return error_mark_node;
1675 if (property_ident == NULL_TREE || property_ident == error_mark_node
1676 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1677 return NULL_TREE;
1679 object = objc_get_class_reference (class_name);
1680 if (!object)
1682 /* We know that 'class_name' is an Objective-C class name as the
1683 parser won't call this function if it is not. This is only a
1684 double-check for safety. */
1685 error_at (input_location, "could not find class %qE", class_name);
1686 return error_mark_node;
1689 rtype = lookup_interface (class_name);
1690 if (!rtype)
1692 /* Again, this should never happen, but we do check. */
1693 error_at (input_location, "could not find interface for class %qE", class_name);
1694 return error_mark_node;
1696 else
1698 if (TREE_DEPRECATED (rtype))
1699 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1702 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1703 property_ident,
1704 true, NULL_TREE);
1706 if (x)
1708 tree expression;
1709 tree getter_call;
1710 tree deprecated_method_prototype = NULL_TREE;
1712 if (PROPERTY_HAS_NO_GETTER (x))
1713 getter_call = NULL_TREE;
1714 else
1715 getter_call = objc_finish_message_expr
1716 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1717 &deprecated_method_prototype);
1719 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1720 deprecated_method_prototype);
1721 SET_EXPR_LOCATION (expression, input_location);
1722 TREE_SIDE_EFFECTS (expression) = 1;
1724 return expression;
1726 else
1728 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1729 property_ident, class_name);
1730 return error_mark_node;
1733 return NULL_TREE;
1738 /* This is used because we don't want to expose PROPERTY_REF to the
1739 C/C++ frontends. Maybe we should! */
1740 bool
1741 objc_is_property_ref (tree node)
1743 if (node && TREE_CODE (node) == PROPERTY_REF)
1744 return true;
1745 else
1746 return false;
1749 /* This function builds a setter call for a PROPERTY_REF (real, for a
1750 declared property, or artificial, for a dot-syntax accessor which
1751 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1752 (the caller must check this beforehand). 'rhs' is the value to
1753 assign to the property. A plain setter call is returned, or
1754 error_mark_node if the property is readonly. */
1756 static tree
1757 objc_build_setter_call (tree lhs, tree rhs)
1759 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1760 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1762 if (PROPERTY_READONLY (property_decl))
1764 error ("readonly property can not be set");
1765 return error_mark_node;
1767 else
1769 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1770 tree setter;
1772 /* TODO: Check that the setter return type is 'void'. */
1774 /* TODO: Decay arguments in C. */
1775 setter = objc_finish_message_expr (object_expr,
1776 PROPERTY_SETTER_NAME (property_decl),
1777 setter_argument, NULL);
1778 return setter;
1781 /* Unreachable, but the compiler may not realize. */
1782 return error_mark_node;
1785 /* This hook routine is called when a MODIFY_EXPR is being built. We
1786 check what is being modified; if it is a PROPERTY_REF, we need to
1787 generate a 'setter' function call for the property. If this is not
1788 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1789 on creating their MODIFY_EXPR.
1791 This is used for example if you write
1793 object.count = 1;
1795 where 'count' is a property. The left-hand side creates a
1796 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1797 to assign something to it. We intercept that here, and generate a
1798 call to the 'setter' method instead. */
1799 tree
1800 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1802 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1804 /* Building a simple call to the setter method would work for cases such as
1806 object.count = 1;
1808 but wouldn't work for cases such as
1810 count = object2.count = 1;
1812 to get these to work with very little effort, we build a
1813 compound statement which does the setter call (to set the
1814 property to 'rhs'), but which can also be evaluated returning
1815 the 'rhs'. If the 'rhs' has no side effects, we can simply
1816 evaluate it twice, building
1818 ([object setProperty: rhs]; rhs)
1820 If it has side effects, we put it in a temporary variable first,
1821 so we create the following:
1823 (temp = rhs; [object setProperty: temp]; temp)
1825 setter_argument is rhs in the first case, and temp in the second
1826 case.
1828 tree setter_argument;
1830 /* s1, s2 and s3 are the tree statements that we need in the
1831 compound expression. */
1832 tree s1, s2, s3, compound_expr;
1834 if (TREE_SIDE_EFFECTS (rhs))
1836 tree bind;
1838 /* Declare __objc_property_temp in a local bind. */
1839 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1840 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1841 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1842 SET_EXPR_LOCATION (bind, input_location);
1843 TREE_SIDE_EFFECTS (bind) = 1;
1844 add_stmt (bind);
1846 /* s1: x = rhs */
1847 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1848 NOP_EXPR,
1849 input_location, rhs, NULL_TREE);
1850 SET_EXPR_LOCATION (s1, input_location);
1852 else
1854 /* No s1. */
1855 setter_argument = rhs;
1856 s1 = NULL_TREE;
1859 /* Now build the compound statement. */
1861 /* s2: [object setProperty: x] */
1862 s2 = objc_build_setter_call (lhs, setter_argument);
1864 /* This happens if building the setter failed because the
1865 property is readonly. */
1866 if (s2 == error_mark_node)
1867 return error_mark_node;
1869 SET_EXPR_LOCATION (s2, input_location);
1871 /* s3: x */
1872 s3 = convert (TREE_TYPE (lhs), setter_argument);
1874 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1875 appropriate. */
1876 if (s1)
1877 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1878 else
1879 compound_expr = build_compound_expr (input_location, s2, s3);
1881 /* Without this, with -Wall you get a 'valued computed is not
1882 used' every time there is a "object.property = x" where the
1883 value of the resulting MODIFY_EXPR is not used. That is
1884 correct (maybe a more sophisticated implementation could
1885 avoid generating the compound expression if not needed), but
1886 we need to turn it off. */
1887 TREE_NO_WARNING (compound_expr) = 1;
1888 return compound_expr;
1890 else
1891 return NULL_TREE;
1894 /* This hook is called by the frontend when one of the four unary
1895 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1896 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1897 argument which is a PROPERTY_REF. For example, this happens if you have
1899 object.count++;
1901 where 'count' is a property. We need to use the 'getter' and
1902 'setter' for the property in an appropriate way to build the
1903 appropriate expression. 'code' is the code for the expression (one
1904 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1905 'increment' is how much we need to add or subtract. */
1906 tree
1907 objc_build_incr_expr_for_property_ref (location_t location,
1908 enum tree_code code,
1909 tree argument, tree increment)
1911 /* Here are the expressions that we want to build:
1913 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1914 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1916 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1917 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1919 tree temp_variable_decl, bind;
1920 /* s1, s2 and s3 are the tree statements that we need in the
1921 compound expression. */
1922 tree s1, s2, s3, compound_expr;
1924 /* Safety check. */
1925 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1926 return error_mark_node;
1928 /* Declare __objc_property_temp in a local bind. */
1929 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1930 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1931 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1932 SET_EXPR_LOCATION (bind, location);
1933 TREE_SIDE_EFFECTS (bind) = 1;
1934 add_stmt (bind);
1936 /* Now build the compound statement. */
1938 /* Note that the 'getter' is generated at gimplify time; at this
1939 time, we can simply put the property_ref (ie, argument) wherever
1940 we want the getter ultimately to be. */
1942 /* s1: __objc_property_temp = [object property] <+/- increment> */
1943 switch (code)
1945 case PREINCREMENT_EXPR:
1946 /* __objc_property_temp = [object property] + increment */
1947 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1948 NOP_EXPR,
1949 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1950 argument, increment), NULL_TREE);
1951 break;
1952 case PREDECREMENT_EXPR:
1953 /* __objc_property_temp = [object property] - increment */
1954 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1955 NOP_EXPR,
1956 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1957 argument, increment), NULL_TREE);
1958 break;
1959 case POSTINCREMENT_EXPR:
1960 case POSTDECREMENT_EXPR:
1961 /* __objc_property_temp = [object property] */
1962 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1963 NOP_EXPR,
1964 location, argument, NULL_TREE);
1965 break;
1966 default:
1967 gcc_unreachable ();
1970 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1971 switch (code)
1973 case PREINCREMENT_EXPR:
1974 case PREDECREMENT_EXPR:
1975 /* [object setProperty: __objc_property_temp] */
1976 s2 = objc_build_setter_call (argument, temp_variable_decl);
1977 break;
1978 case POSTINCREMENT_EXPR:
1979 /* [object setProperty: __objc_property_temp + increment] */
1980 s2 = objc_build_setter_call (argument,
1981 build2 (PLUS_EXPR, TREE_TYPE (argument),
1982 temp_variable_decl, increment));
1983 break;
1984 case POSTDECREMENT_EXPR:
1985 /* [object setProperty: __objc_property_temp - increment] */
1986 s2 = objc_build_setter_call (argument,
1987 build2 (MINUS_EXPR, TREE_TYPE (argument),
1988 temp_variable_decl, increment));
1989 break;
1990 default:
1991 gcc_unreachable ();
1994 /* This happens if building the setter failed because the property
1995 is readonly. */
1996 if (s2 == error_mark_node)
1997 return error_mark_node;
1999 SET_EXPR_LOCATION (s2, location);
2001 /* s3: __objc_property_temp */
2002 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
2004 /* Now build the compound statement (s1, s2, s3) */
2005 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
2007 /* Prevent C++ from warning with -Wall that "right operand of comma
2008 operator has no effect". */
2009 TREE_NO_WARNING (compound_expr) = 1;
2010 return compound_expr;
2013 tree
2014 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
2015 tree optparms, bool ellipsis)
2017 if (is_class_method)
2018 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2019 optparms, ellipsis);
2020 else
2021 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2022 optparms, ellipsis);
2025 void
2026 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2028 if (!objc_interface_context)
2030 /* PS: At the moment, due to how the parser works, it should be
2031 impossible to get here. But it's good to have the check in
2032 case the parser changes.
2034 fatal_error ("method declaration not in @interface context");
2037 if (flag_objc1_only && attributes)
2038 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2040 objc_decl_method_attributes (&decl, attributes, 0);
2041 objc_add_method (objc_interface_context,
2042 decl,
2043 is_class_method,
2044 objc_method_optional_flag);
2047 /* Return 'true' if the method definition could be started, and
2048 'false' if not (because we are outside an @implementation context).
2049 EXPR is NULL or an expression that needs to be evaluated for the
2050 side effects of array size expressions in the parameters.
2052 bool
2053 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2054 tree expr)
2056 if (!objc_implementation_context)
2058 error ("method definition not in @implementation context");
2059 return false;
2062 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2063 return false;
2065 #ifndef OBJCPLUS
2066 /* Indicate no valid break/continue context by setting these variables
2067 to some non-null, non-label value. We'll notice and emit the proper
2068 error message in c_finish_bc_stmt. */
2069 c_break_label = c_cont_label = size_zero_node;
2070 #endif
2072 if (attributes)
2073 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2074 else
2075 objc_decl_method_attributes (&decl, attributes, 0);
2077 objc_add_method (objc_implementation_context,
2078 decl,
2079 is_class_method,
2080 /* is optional */ false);
2081 start_method_def (decl, expr);
2082 return true;
2085 void
2086 objc_add_instance_variable (tree decl)
2088 (void) add_instance_variable (objc_ivar_context,
2089 objc_ivar_visibility,
2090 decl);
2093 /* Construct a C struct with same name as KLASS, a base struct with tag
2094 SUPER_NAME (if any), and FIELDS indicated. */
2096 static tree
2097 objc_build_struct (tree klass, tree fields, tree super_name)
2099 tree name = CLASS_NAME (klass);
2100 tree s = objc_start_struct (name);
2101 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2102 tree t;
2103 vec<tree> objc_info = vNULL;
2104 int i;
2106 if (super)
2108 /* Prepend a packed variant of the base class into the layout. This
2109 is necessary to preserve ObjC ABI compatibility. */
2110 tree base = build_decl (input_location,
2111 FIELD_DECL, NULL_TREE, super);
2112 tree field = TYPE_FIELDS (super);
2114 while (field && DECL_CHAIN (field)
2115 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2116 field = DECL_CHAIN (field);
2118 /* For ObjC ABI purposes, the "packed" size of a base class is
2119 the sum of the offset and the size (in bits) of the last field
2120 in the class. */
2121 DECL_SIZE (base)
2122 = (field && TREE_CODE (field) == FIELD_DECL
2123 ? size_binop (PLUS_EXPR,
2124 size_binop (PLUS_EXPR,
2125 size_binop
2126 (MULT_EXPR,
2127 convert (bitsizetype,
2128 DECL_FIELD_OFFSET (field)),
2129 bitsize_int (BITS_PER_UNIT)),
2130 DECL_FIELD_BIT_OFFSET (field)),
2131 DECL_SIZE (field))
2132 : bitsize_zero_node);
2133 DECL_SIZE_UNIT (base)
2134 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2135 size_int (BITS_PER_UNIT));
2136 DECL_ARTIFICIAL (base) = 1;
2137 DECL_ALIGN (base) = 1;
2138 DECL_FIELD_CONTEXT (base) = s;
2139 #ifdef OBJCPLUS
2140 DECL_FIELD_IS_BASE (base) = 1;
2142 if (fields)
2143 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2144 #endif /* are following the ObjC ABI here. */
2145 DECL_CHAIN (base) = fields;
2146 fields = base;
2149 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2150 information in all variants of this RECORD_TYPE to be destroyed
2151 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2152 for something else and then will change all variants to use the
2153 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2154 it for ObjC protocols and that such propagation will make all
2155 variants use the same objc_info), but it is therein that we store
2156 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2157 Hence, we must save the ObjC-specific information before calling
2158 finish_struct(), and then reinstate it afterwards. */
2160 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2162 INIT_TYPE_OBJC_INFO (t);
2163 objc_info.safe_push (TYPE_OBJC_INFO (t));
2166 s = objc_finish_struct (s, fields);
2168 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2170 /* We now want to restore the different TYPE_OBJC_INFO, but we
2171 have the additional problem that the C frontend doesn't just
2172 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2173 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2174 we need a different TYPE_OBJC_INFO for each (and
2175 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2176 make a copy of each TYPE_LANG_SPECIFIC before we modify
2177 TYPE_OBJC_INFO. */
2178 if (TYPE_LANG_SPECIFIC (t))
2180 /* Create a copy of TYPE_LANG_SPECIFIC. */
2181 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2182 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2183 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2184 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2186 else
2188 /* Just create a new one. */
2189 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2191 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2192 protocol information that may have been associated with the
2193 type. */
2194 TYPE_OBJC_INFO (t) = objc_info[i];
2195 /* Replace the IDENTIFIER_NODE with an actual @interface now
2196 that we have it. */
2197 TYPE_OBJC_INTERFACE (t) = klass;
2199 objc_info.release ();
2201 /* Use TYPE_BINFO structures to point at the super class, if any. */
2202 objc_xref_basetypes (s, super);
2204 /* Mark this struct as a class template. */
2205 CLASS_STATIC_TEMPLATE (klass) = s;
2207 return s;
2210 /* Mark DECL as being 'volatile' for purposes of Darwin
2211 _setjmp()/_longjmp() exception handling. Called from
2212 objc_mark_locals_volatile(). */
2213 void
2214 objc_volatilize_decl (tree decl)
2216 /* Do not mess with variables that are 'static' or (already)
2217 'volatile'. */
2218 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2219 && (TREE_CODE (decl) == VAR_DECL
2220 || TREE_CODE (decl) == PARM_DECL))
2222 if (local_variables_to_volatilize == NULL)
2223 vec_alloc (local_variables_to_volatilize, 8);
2225 vec_safe_push (local_variables_to_volatilize, decl);
2229 /* Called when parsing of a function completes; if any local variables
2230 in the function were marked as variables to volatilize, change them
2231 to volatile. We do this at the end of the function when the
2232 warnings about discarding 'volatile' have already been produced.
2233 We are making the variables as volatile just to force the compiler
2234 to preserve them between setjmp/longjmp, but we don't want warnings
2235 for them as they aren't really volatile. */
2236 void
2237 objc_finish_function (void)
2239 /* If there are any local variables to volatilize, volatilize them. */
2240 if (local_variables_to_volatilize)
2242 int i;
2243 tree decl;
2244 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
2246 tree t = TREE_TYPE (decl);
2248 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2249 TREE_TYPE (decl) = t;
2250 TREE_THIS_VOLATILE (decl) = 1;
2251 TREE_SIDE_EFFECTS (decl) = 1;
2252 DECL_REGISTER (decl) = 0;
2253 #ifndef OBJCPLUS
2254 C_DECL_REGISTER (decl) = 0;
2255 #endif
2258 /* Now we delete the vector. This sets it to NULL as well. */
2259 vec_free (local_variables_to_volatilize);
2263 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2264 (including its categories and superclasses) or by object type TYP.
2265 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2267 static bool
2268 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2270 bool class_type = (cls != NULL_TREE);
2272 while (cls)
2274 tree c;
2276 /* Check protocols adopted by the class and its categories. */
2277 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2279 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2280 return true;
2283 /* Repeat for superclasses. */
2284 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2287 /* Check for any protocols attached directly to the object type. */
2288 if (TYPE_HAS_OBJC_INFO (typ))
2290 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2291 return true;
2294 if (warn)
2296 *errbuf = 0;
2297 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2298 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2299 "implementing" a given protocol, since they do not have an
2300 implementation. */
2301 if (class_type)
2302 warning (0, "class %qs does not implement the %qE protocol",
2303 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2304 else
2305 warning (0, "type %qs does not conform to the %qE protocol",
2306 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2309 return false;
2312 /* Check if class RCLS and instance struct type RTYP conform to at least the
2313 same protocols that LCLS and LTYP conform to. */
2315 static bool
2316 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2318 tree p;
2319 bool have_lproto = false;
2321 while (lcls)
2323 /* NB: We do _not_ look at categories defined for LCLS; these may or
2324 may not get loaded in, and therefore it is unreasonable to require
2325 that RCLS/RTYP must implement any of their protocols. */
2326 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2328 have_lproto = true;
2330 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2331 return warn;
2334 /* Repeat for superclasses. */
2335 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2338 /* Check for any protocols attached directly to the object type. */
2339 if (TYPE_HAS_OBJC_INFO (ltyp))
2341 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2343 have_lproto = true;
2345 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2346 return warn;
2350 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2351 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2352 away with simply checking for 'id' or 'Class' (!RCLS), since this
2353 routine will not get called in other cases. */
2354 return have_lproto || (rcls != NULL_TREE);
2357 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2358 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2359 compatible by objc_compare_types() below. */
2361 tree
2362 objc_common_type (tree type1, tree type2)
2364 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2366 while (POINTER_TYPE_P (inner1))
2368 inner1 = TREE_TYPE (inner1);
2369 inner2 = TREE_TYPE (inner2);
2372 /* If one type is derived from another, return the base type. */
2373 if (DERIVED_FROM_P (inner1, inner2))
2374 return type1;
2375 else if (DERIVED_FROM_P (inner2, inner1))
2376 return type2;
2378 /* If both types are 'Class', return 'Class'. */
2379 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2380 return objc_class_type;
2382 /* Otherwise, return 'id'. */
2383 return objc_object_type;
2386 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2387 an instance of RTYP to an instance of LTYP or to compare the two
2388 (if ARGNO is equal to -3), per ObjC type system rules. Before
2389 returning 'true', this routine may issue warnings related to, e.g.,
2390 protocol conformance. When returning 'false', the routine must
2391 produce absolutely no warnings; the C or C++ front-end will do so
2392 instead, if needed. If either LTYP or RTYP is not an Objective-C
2393 type, the routine must return 'false'.
2395 The ARGNO parameter is encoded as follows:
2396 >= 1 Parameter number (CALLEE contains function being called);
2397 0 Return value;
2398 -1 Assignment;
2399 -2 Initialization;
2400 -3 Comparison (LTYP and RTYP may match in either direction);
2401 -4 Silent comparison (for C++ overload resolution);
2402 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2403 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2404 so that each object of type RTYP is also of type LTYP). This is used
2405 when comparing property types. */
2407 bool
2408 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2410 tree lcls, rcls, lproto, rproto;
2411 bool pointers_compatible;
2413 /* We must be dealing with pointer types */
2414 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2415 return false;
2419 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2420 rtyp = TREE_TYPE (rtyp);
2422 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2424 /* We must also handle function pointers, since ObjC is a bit more
2425 lenient than C or C++ on this. */
2426 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2428 function_args_iterator liter, riter;
2430 /* Return types must be covariant. */
2431 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2432 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2433 argno, callee))
2434 return false;
2436 /* Argument types must be contravariant. */
2437 function_args_iter_init (&liter, ltyp);
2438 function_args_iter_init (&riter, rtyp);
2440 while (1)
2442 ltyp = function_args_iter_cond (&liter);
2443 rtyp = function_args_iter_cond (&riter);
2445 /* If we've exhaused both lists simulateously, we're done. */
2446 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2447 break;
2449 /* If one list is shorter than the other, they fail to match. */
2450 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2451 return false;
2453 if (!comptypes (rtyp, ltyp)
2454 && !objc_compare_types (rtyp, ltyp, argno, callee))
2455 return false;
2457 function_args_iter_next (&liter);
2458 function_args_iter_next (&riter);
2461 return true;
2464 /* Past this point, we are only interested in ObjC class instances,
2465 or 'id' or 'Class'. */
2466 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2467 return false;
2469 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2470 && !TYPE_HAS_OBJC_INFO (ltyp))
2471 return false;
2473 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2474 && !TYPE_HAS_OBJC_INFO (rtyp))
2475 return false;
2477 /* Past this point, we are committed to returning 'true' to the caller
2478 (unless performing a silent comparison; see below). However, we can
2479 still warn about type and/or protocol mismatches. */
2481 if (TYPE_HAS_OBJC_INFO (ltyp))
2483 lcls = TYPE_OBJC_INTERFACE (ltyp);
2484 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2486 else
2487 lcls = lproto = NULL_TREE;
2489 if (TYPE_HAS_OBJC_INFO (rtyp))
2491 rcls = TYPE_OBJC_INTERFACE (rtyp);
2492 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2494 else
2495 rcls = rproto = NULL_TREE;
2497 /* If we could not find an @interface declaration, we must have
2498 only seen a @class declaration; for purposes of type comparison,
2499 treat it as a stand-alone (root) class. */
2501 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2502 lcls = NULL_TREE;
2504 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2505 rcls = NULL_TREE;
2507 /* If either type is an unqualified 'id', we're done. This is because
2508 an 'id' can be assigned to or from any type with no warnings. */
2509 if (argno != -5)
2511 if ((!lproto && objc_is_object_id (ltyp))
2512 || (!rproto && objc_is_object_id (rtyp)))
2513 return true;
2515 else
2517 /* For property checks, though, an 'id' is considered the most
2518 general type of object, hence if you try to specialize an
2519 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2520 to warn. */
2521 if (!lproto && objc_is_object_id (ltyp))
2522 return true;
2525 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2527 /* If the underlying types are the same, and at most one of them has
2528 a protocol list, we do not need to issue any diagnostics. */
2529 if (pointers_compatible && (!lproto || !rproto))
2530 return true;
2532 /* If exactly one of the types is 'Class', issue a diagnostic; any
2533 exceptions of this rule have already been handled. */
2534 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2535 pointers_compatible = false;
2536 /* Otherwise, check for inheritance relations. */
2537 else
2539 if (!pointers_compatible)
2541 /* Again, if any of the two is an 'id', we're satisfied,
2542 unless we're comparing properties, in which case only an
2543 'id' on the left-hand side (old property) is good
2544 enough. */
2545 if (argno != -5)
2546 pointers_compatible
2547 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2548 else
2549 pointers_compatible = objc_is_object_id (ltyp);
2552 if (!pointers_compatible)
2553 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2555 if (!pointers_compatible && (argno == -3 || argno == -4))
2556 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2559 /* If the pointers match modulo protocols, check for protocol conformance
2560 mismatches. */
2561 if (pointers_compatible)
2563 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2564 argno != -3);
2566 if (!pointers_compatible && argno == -3)
2567 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2568 argno != -3);
2571 if (!pointers_compatible)
2573 /* The two pointers are not exactly compatible. Issue a warning, unless
2574 we are performing a silent comparison, in which case return 'false'
2575 instead. */
2576 /* NB: For the time being, we shall make our warnings look like their
2577 C counterparts. In the future, we may wish to make them more
2578 ObjC-specific. */
2579 switch (argno)
2581 case -5:
2582 case -4:
2583 return false;
2585 case -3:
2586 warning (0, "comparison of distinct Objective-C types lacks a cast");
2587 break;
2589 case -2:
2590 warning (0, "initialization from distinct Objective-C type");
2591 break;
2593 case -1:
2594 warning (0, "assignment from distinct Objective-C type");
2595 break;
2597 case 0:
2598 warning (0, "distinct Objective-C type in return");
2599 break;
2601 default:
2602 warning (0, "passing argument %d of %qE from distinct "
2603 "Objective-C type", argno, callee);
2604 break;
2608 return true;
2611 /* This routine is similar to objc_compare_types except that function-pointers are
2612 excluded. This is because, caller assumes that common types are of (id, Object*)
2613 variety and calls objc_common_type to obtain a common type. There is no commonolty
2614 between two function-pointers in this regard. */
2616 bool
2617 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2619 if (objc_compare_types (ltyp, rtyp, argno, callee))
2621 /* exclude function-pointer types. */
2624 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2625 rtyp = TREE_TYPE (rtyp);
2627 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2628 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2630 return false;
2633 #ifndef OBJCPLUS
2634 /* Determine if CHILD is derived from PARENT. The routine assumes that
2635 both parameters are RECORD_TYPEs, and is non-reflexive. */
2637 static bool
2638 objc_derived_from_p (tree parent, tree child)
2640 parent = TYPE_MAIN_VARIANT (parent);
2642 for (child = TYPE_MAIN_VARIANT (child);
2643 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2645 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2646 (TYPE_BINFO (child),
2647 0)));
2649 if (child == parent)
2650 return true;
2653 return false;
2655 #endif
2657 tree
2658 objc_build_component_ref (tree datum, tree component)
2660 /* If COMPONENT is NULL, the caller is referring to the anonymous
2661 base class field. */
2662 if (!component)
2664 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2666 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2669 /* The 'build_component_ref' routine has been removed from the C++
2670 front-end, but 'finish_class_member_access_expr' seems to be
2671 a worthy substitute. */
2672 #ifdef OBJCPLUS
2673 return finish_class_member_access_expr (datum, component, false,
2674 tf_warning_or_error);
2675 #else
2676 return build_component_ref (input_location, datum, component);
2677 #endif
2680 /* Recursively copy inheritance information rooted at BINFO. To do this,
2681 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2683 static tree
2684 objc_copy_binfo (tree binfo)
2686 tree btype = BINFO_TYPE (binfo);
2687 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2688 tree base_binfo;
2689 int ix;
2691 BINFO_TYPE (binfo2) = btype;
2692 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2693 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2695 /* Recursively copy base binfos of BINFO. */
2696 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2698 tree base_binfo2 = objc_copy_binfo (base_binfo);
2700 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2701 BINFO_BASE_APPEND (binfo2, base_binfo2);
2704 return binfo2;
2707 /* Record superclass information provided in BASETYPE for ObjC class REF.
2708 This is loosely based on cp/decl.c:xref_basetypes(). */
2710 static void
2711 objc_xref_basetypes (tree ref, tree basetype)
2713 tree variant;
2714 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2715 TYPE_BINFO (ref) = binfo;
2716 BINFO_OFFSET (binfo) = size_zero_node;
2717 BINFO_TYPE (binfo) = ref;
2719 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2720 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2721 TYPE_BINFO (variant) = binfo;
2723 if (basetype)
2725 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2727 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2728 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
2729 BINFO_BASE_APPEND (binfo, base_binfo);
2730 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2734 /* Called from finish_decl. */
2736 void
2737 objc_check_decl (tree decl)
2739 tree type = TREE_TYPE (decl);
2741 if (TREE_CODE (type) != RECORD_TYPE)
2742 return;
2743 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2744 error ("statically allocated instance of Objective-C class %qE",
2745 type);
2748 void
2749 objc_check_global_decl (tree decl)
2751 tree id = DECL_NAME (decl);
2752 if (objc_is_class_name (id) && global_bindings_p())
2753 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2756 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2757 INTERFACE may either name an Objective-C class, or refer to the
2758 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2759 type, just return it unchanged. This function is often called when
2760 PROTOCOLS is NULL_TREE, in which case we simply look up the
2761 appropriate INTERFACE. */
2763 tree
2764 objc_get_protocol_qualified_type (tree interface, tree protocols)
2766 /* If INTERFACE is not provided, default to 'id'. */
2767 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2768 bool is_ptr = (type != NULL_TREE);
2770 if (!is_ptr)
2772 type = objc_is_class_name (interface);
2774 if (type)
2776 /* If looking at a typedef, retrieve the precise type it
2777 describes. */
2778 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2779 interface = identifier_global_value (interface);
2781 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2782 && DECL_ORIGINAL_TYPE (interface))
2783 ? DECL_ORIGINAL_TYPE (interface)
2784 : xref_tag (RECORD_TYPE, type));
2786 else
2788 /* This case happens when we are given an 'interface' which
2789 is not a valid class name. For example if a typedef was
2790 used, and 'interface' really is the identifier of the
2791 typedef, but when you resolve it you don't get an
2792 Objective-C class, but something else, such as 'int'.
2793 This is an error; protocols make no sense unless you use
2794 them with Objective-C objects. */
2795 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2797 /* Try to recover. Ignore the invalid class name, and treat
2798 the object as an 'id' to silence further warnings about
2799 the class. */
2800 type = objc_object_type;
2801 is_ptr = true;
2805 if (protocols)
2807 type = build_variant_type_copy (type);
2809 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2810 to the pointee. */
2811 if (is_ptr)
2813 tree orig_pointee_type = TREE_TYPE (type);
2814 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2816 /* Set up the canonical type information. */
2817 TYPE_CANONICAL (type)
2818 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2820 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2821 type = TREE_TYPE (type);
2824 /* Look up protocols and install in lang specific list. */
2825 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2826 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2827 (protocols, /* definition_required */ false);
2829 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2830 return the pointer to the new pointee variant. */
2831 if (is_ptr)
2832 type = TYPE_POINTER_TO (type);
2833 else
2834 TYPE_OBJC_INTERFACE (type)
2835 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2838 return type;
2841 /* Check for circular dependencies in protocols. The arguments are
2842 PROTO, the protocol to check, and LIST, a list of protocol it
2843 conforms to. */
2845 static void
2846 check_protocol_recursively (tree proto, tree list)
2848 tree p;
2850 for (p = list; p; p = TREE_CHAIN (p))
2852 tree pp = TREE_VALUE (p);
2854 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2855 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2856 /* definition_required */ false);
2858 if (pp == proto)
2859 fatal_error ("protocol %qE has circular dependency",
2860 PROTOCOL_NAME (pp));
2861 if (pp)
2862 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2866 /* Look up PROTOCOLS, and return a list of those that are found. If
2867 none are found, return NULL. Note that this function will emit a
2868 warning if a protocol is found and is deprecated. If
2869 'definition_required', then warn if the protocol is found but is
2870 not defined (ie, if we only saw a forward-declaration of the
2871 protocol (as in "@protocol NSObject;") not a real definition with
2872 the list of methods). */
2873 static tree
2874 lookup_and_install_protocols (tree protocols, bool definition_required)
2876 tree proto;
2877 tree return_value = NULL_TREE;
2879 if (protocols == error_mark_node)
2880 return NULL;
2882 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2884 tree ident = TREE_VALUE (proto);
2885 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2886 definition_required);
2888 if (p)
2889 return_value = chainon (return_value,
2890 build_tree_list (NULL_TREE, p));
2891 else if (ident != error_mark_node)
2892 error ("cannot find protocol declaration for %qE",
2893 ident);
2896 return return_value;
2899 static void
2900 build_common_objc_exception_stuff (void)
2902 tree noreturn_list, nothrow_list, temp_type;
2904 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2905 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2907 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2908 /* void objc_sync_enter(id); */
2909 /* void objc_sync_exit(id); */
2910 temp_type = build_function_type_list (void_type_node,
2911 objc_object_type,
2912 NULL_TREE);
2913 objc_exception_throw_decl
2914 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2915 noreturn_list);
2916 /* Make sure that objc_exception_throw (id) claims that it may throw an
2917 exception. */
2918 TREE_NOTHROW (objc_exception_throw_decl) = 0;
2920 objc_sync_enter_decl
2921 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2922 NULL, nothrow_list);
2924 objc_sync_exit_decl
2925 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2926 NULL, nothrow_list);
2929 /* Purpose: "play" parser, creating/installing representations
2930 of the declarations that are required by Objective-C.
2932 Model:
2934 type_spec--------->sc_spec
2935 (tree_list) (tree_list)
2938 identifier_node identifier_node */
2940 static void
2941 synth_module_prologue (void)
2943 tree type;
2944 enum debug_info_type save_write_symbols = write_symbols;
2945 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2947 /* Suppress outputting debug symbols, because
2948 dbxout_init hasn't been called yet. */
2949 write_symbols = NO_DEBUG;
2950 debug_hooks = &do_nothing_debug_hooks;
2952 #ifdef OBJCPLUS
2953 push_lang_context (lang_name_c); /* extern "C" */
2954 #endif
2956 /* The following are also defined in <objc/objc.h> and friends. */
2958 objc_object_id = get_identifier (TAG_OBJECT);
2959 objc_class_id = get_identifier (TAG_CLASS);
2961 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2962 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2964 objc_object_type = build_pointer_type (objc_object_reference);
2965 objc_class_type = build_pointer_type (objc_class_reference);
2967 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2968 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2970 /* Declare the 'id' and 'Class' typedefs. */
2971 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2972 TYPE_DECL,
2973 objc_object_name,
2974 objc_object_type));
2975 TREE_NO_WARNING (type) = 1;
2977 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2978 TYPE_DECL,
2979 objc_class_name,
2980 objc_class_type));
2981 TREE_NO_WARNING (type) = 1;
2983 /* Forward-declare '@interface Protocol'. */
2984 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2985 objc_declare_class (type);
2986 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
2988 /* Declare receiver type used for dispatching messages to 'super'. */
2989 /* `struct objc_super *' */
2990 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2991 get_identifier (TAG_SUPER)));
2993 /* Declare pointers to method and ivar lists. */
2994 objc_method_list_ptr = build_pointer_type
2995 (xref_tag (RECORD_TYPE,
2996 get_identifier (UTAG_METHOD_LIST)));
2997 objc_method_proto_list_ptr
2998 = build_pointer_type (xref_tag (RECORD_TYPE,
2999 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3000 objc_ivar_list_ptr = build_pointer_type
3001 (xref_tag (RECORD_TYPE,
3002 get_identifier (UTAG_IVAR_LIST)));
3004 build_common_objc_exception_stuff ();
3006 /* Set-up runtime-specific templates, message and exception stuff. */
3007 (*runtime.initialize) ();
3009 /* Declare objc_getProperty, object_setProperty and other property
3010 accessor helpers. */
3011 build_common_objc_property_accessor_helpers ();
3013 /* Forward declare constant_string_id and constant_string_type. */
3014 if (!constant_string_class_name)
3015 constant_string_class_name = runtime.default_constant_string_class_name;
3016 constant_string_id = get_identifier (constant_string_class_name);
3017 objc_declare_class (constant_string_id);
3019 /* Pre-build the following entities - for speed/convenience. */
3020 self_id = get_identifier ("self");
3021 ucmd_id = get_identifier ("_cmd");
3023 /* Declare struct _objc_fast_enumeration_state { ... }; */
3024 build_fast_enumeration_state_template ();
3026 /* void objc_enumeration_mutation (id) */
3027 type = build_function_type_list (void_type_node,
3028 objc_object_type, NULL_TREE);
3029 objc_enumeration_mutation_decl
3030 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3031 NULL, NULL_TREE);
3032 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3034 #ifdef OBJCPLUS
3035 pop_lang_context ();
3036 #endif
3038 write_symbols = save_write_symbols;
3039 debug_hooks = save_hooks;
3042 /* --- const strings --- */
3044 /* Ensure that the ivar list for NSConstantString/NXConstantString
3045 (or whatever was specified via `-fconstant-string-class')
3046 contains fields at least as large as the following three, so that
3047 the runtime can stomp on them with confidence:
3049 struct STRING_OBJECT_CLASS_NAME
3051 Object isa;
3052 char *cString;
3053 unsigned int length;
3054 }; */
3056 static int
3057 check_string_class_template (void)
3059 tree field_decl = objc_get_class_ivars (constant_string_id);
3061 #define AT_LEAST_AS_LARGE_AS(F, T) \
3062 (F && TREE_CODE (F) == FIELD_DECL \
3063 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3064 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3066 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3067 return 0;
3069 field_decl = DECL_CHAIN (field_decl);
3070 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3071 return 0;
3073 field_decl = DECL_CHAIN (field_decl);
3074 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3076 #undef AT_LEAST_AS_LARGE_AS
3079 /* Avoid calling `check_string_class_template ()' more than once. */
3080 static GTY(()) int string_layout_checked;
3082 /* Construct an internal string layout to be used as a template for
3083 creating NSConstantString/NXConstantString instances. */
3085 static tree
3086 objc_build_internal_const_str_type (void)
3088 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3089 tree fields = build_decl (input_location,
3090 FIELD_DECL, NULL_TREE, ptr_type_node);
3091 tree field = build_decl (input_location,
3092 FIELD_DECL, NULL_TREE, ptr_type_node);
3094 DECL_CHAIN (field) = fields; fields = field;
3095 field = build_decl (input_location,
3096 FIELD_DECL, NULL_TREE, unsigned_type_node);
3097 DECL_CHAIN (field) = fields; fields = field;
3098 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3099 reverse order! */
3100 finish_builtin_struct (type, "__builtin_ObjCString",
3101 fields, NULL_TREE);
3103 return type;
3106 /* Custom build_string which sets TREE_TYPE! */
3108 tree
3109 my_build_string (int len, const char *str)
3111 return fix_string_type (build_string (len, str));
3114 /* Build a string with contents STR and length LEN and convert it to a
3115 pointer. */
3117 tree
3118 my_build_string_pointer (int len, const char *str)
3120 tree string = my_build_string (len, str);
3121 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3122 return build1 (ADDR_EXPR, ptrtype, string);
3125 hashval_t
3126 objc_string_hasher::hash (string_descriptor *ptr)
3128 const_tree const str = ptr->literal;
3129 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3130 int i, len = TREE_STRING_LENGTH (str);
3131 hashval_t h = len;
3133 for (i = 0; i < len; i++)
3134 h = ((h * 613) + p[i]);
3136 return h;
3139 bool
3140 objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2)
3142 const_tree const str1 = ptr1->literal;
3143 const_tree const str2 = ptr2->literal;
3144 int len1 = TREE_STRING_LENGTH (str1);
3146 return (len1 == TREE_STRING_LENGTH (str2)
3147 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3148 len1));
3151 /* Given a chain of STRING_CST's, build a static instance of
3152 NXConstantString which points at the concatenation of those
3153 strings. We place the string object in the __string_objects
3154 section of the __OBJC segment. The Objective-C runtime will
3155 initialize the isa pointers of the string objects to point at the
3156 NXConstantString class object. */
3158 tree
3159 objc_build_string_object (tree string)
3161 tree constant_string_class;
3162 int length;
3163 tree addr;
3164 struct string_descriptor *desc, key;
3166 /* We should be passed a STRING_CST. */
3167 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
3168 length = TREE_STRING_LENGTH (string) - 1;
3170 /* The target may have different ideas on how to construct an ObjC string
3171 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3172 constant CFString reference instead.
3173 At present, this is only supported for the NeXT runtime. */
3174 if (flag_next_runtime
3175 && targetcm.objc_construct_string_object)
3177 tree constructor = (*targetcm.objc_construct_string_object) (string);
3178 if (constructor)
3179 return build1 (NOP_EXPR, objc_object_type, constructor);
3182 /* Check whether the string class being used actually exists and has the
3183 correct ivar layout. */
3184 if (!string_layout_checked)
3186 string_layout_checked = -1;
3187 constant_string_class = lookup_interface (constant_string_id);
3188 internal_const_str_type = objc_build_internal_const_str_type ();
3190 if (!constant_string_class
3191 || !(constant_string_type
3192 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3193 error ("cannot find interface declaration for %qE",
3194 constant_string_id);
3195 /* The NSConstantString/NXConstantString ivar layout is now known. */
3196 else if (!check_string_class_template ())
3197 error ("interface %qE does not have valid constant string layout",
3198 constant_string_id);
3199 /* If the runtime can generate a literal reference to the string class,
3200 don't need to run a constructor. */
3201 else if (!(*runtime.setup_const_string_class_decl)())
3202 error ("cannot find reference tag for class %qE", constant_string_id);
3203 else
3205 string_layout_checked = 1; /* Success! */
3206 add_class_reference (constant_string_id);
3210 if (string_layout_checked == -1)
3211 return error_mark_node;
3213 /* Perhaps we already constructed a constant string just like this one? */
3214 key.literal = string;
3215 string_descriptor **loc = string_htab->find_slot (&key, INSERT);
3216 desc = *loc;
3218 if (!desc)
3220 *loc = desc = ggc_alloc<string_descriptor> ();
3221 desc->literal = string;
3222 desc->constructor =
3223 (*runtime.build_const_string_constructor) (input_location, string, length);
3226 addr = convert (build_pointer_type (constant_string_type),
3227 build_unary_op (input_location,
3228 ADDR_EXPR, desc->constructor, 1));
3230 return addr;
3233 /* Build a static constant CONSTRUCTOR
3234 with type TYPE and elements ELTS. */
3236 tree
3237 objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
3239 tree constructor = build_constructor (type, elts);
3241 TREE_CONSTANT (constructor) = 1;
3242 TREE_STATIC (constructor) = 1;
3243 TREE_READONLY (constructor) = 1;
3245 #ifdef OBJCPLUS
3246 /* Adjust for impedance mismatch. We should figure out how to build
3247 CONSTRUCTORs that consistently please both the C and C++ gods. */
3248 if (!(*elts)[0].index)
3249 TREE_TYPE (constructor) = init_list_type_node;
3250 #endif
3252 return constructor;
3255 /* Return the DECL of the string IDENT in the SECTION. */
3257 tree
3258 get_objc_string_decl (tree ident, enum string_section section)
3260 tree chain;
3262 switch (section)
3264 case class_names:
3265 chain = class_names_chain;
3266 break;
3267 case meth_var_names:
3268 chain = meth_var_names_chain;
3269 break;
3270 case meth_var_types:
3271 chain = meth_var_types_chain;
3272 break;
3273 case prop_names_attr:
3274 chain = prop_names_attr_chain;
3275 break;
3276 default:
3277 gcc_unreachable ();
3280 for (; chain != 0; chain = TREE_CHAIN (chain))
3281 if (TREE_VALUE (chain) == ident)
3282 return (TREE_PURPOSE (chain));
3284 /* We didn't find the entry. */
3285 return NULL_TREE;
3288 /* Create a class reference, but don't create a variable to reference
3289 it. */
3291 void
3292 add_class_reference (tree ident)
3294 tree chain;
3296 if ((chain = cls_ref_chain))
3298 tree tail;
3301 if (ident == TREE_VALUE (chain))
3302 return;
3304 tail = chain;
3305 chain = TREE_CHAIN (chain);
3307 while (chain);
3309 /* Append to the end of the list */
3310 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3312 else
3313 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3316 /* Get a class reference, creating it if necessary. Also create the
3317 reference variable. */
3318 tree
3319 objc_get_class_reference (tree ident)
3321 tree orig_ident = (DECL_P (ident)
3322 ? DECL_NAME (ident)
3323 : TYPE_P (ident)
3324 ? OBJC_TYPE_NAME (ident)
3325 : ident);
3326 bool local_scope = false;
3328 #ifdef OBJCPLUS
3329 if (processing_template_decl)
3330 /* Must wait until template instantiation time. */
3331 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
3332 #endif
3334 if (TREE_CODE (ident) == TYPE_DECL)
3335 ident = (DECL_ORIGINAL_TYPE (ident)
3336 ? DECL_ORIGINAL_TYPE (ident)
3337 : TREE_TYPE (ident));
3339 #ifdef OBJCPLUS
3340 if (TYPE_P (ident)
3341 && CP_TYPE_CONTEXT (ident) != global_namespace)
3342 local_scope = true;
3343 #endif
3345 if (local_scope || !(ident = objc_is_class_name (ident)))
3347 error ("%qE is not an Objective-C class name or alias",
3348 orig_ident);
3349 return error_mark_node;
3352 return (*runtime.get_class_reference) (ident);
3355 void
3356 objc_declare_alias (tree alias_ident, tree class_ident)
3358 tree underlying_class;
3360 #ifdef OBJCPLUS
3361 if (current_namespace != global_namespace) {
3362 error ("Objective-C declarations may only appear in global scope");
3364 #endif /* OBJCPLUS */
3366 if (!(underlying_class = objc_is_class_name (class_ident)))
3367 warning (0, "cannot find class %qE", class_ident);
3368 else if (objc_is_class_name (alias_ident))
3369 warning (0, "class %qE already exists", alias_ident);
3370 else
3372 /* Implement @compatibility_alias as a typedef. */
3373 #ifdef OBJCPLUS
3374 push_lang_context (lang_name_c); /* extern "C" */
3375 #endif
3376 lang_hooks.decls.pushdecl (build_decl
3377 (input_location,
3378 TYPE_DECL,
3379 alias_ident,
3380 xref_tag (RECORD_TYPE, underlying_class)));
3381 #ifdef OBJCPLUS
3382 pop_lang_context ();
3383 #endif
3384 objc_map_put (alias_name_map, alias_ident, underlying_class);
3388 void
3389 objc_declare_class (tree identifier)
3391 #ifdef OBJCPLUS
3392 if (current_namespace != global_namespace) {
3393 error ("Objective-C declarations may only appear in global scope");
3395 #endif /* OBJCPLUS */
3397 if (! objc_is_class_name (identifier))
3399 tree record = lookup_name (identifier), type = record;
3401 if (record)
3403 if (TREE_CODE (record) == TYPE_DECL)
3404 type = DECL_ORIGINAL_TYPE (record)
3405 ? DECL_ORIGINAL_TYPE (record)
3406 : TREE_TYPE (record);
3408 if (!TYPE_HAS_OBJC_INFO (type)
3409 || !TYPE_OBJC_INTERFACE (type))
3411 error ("%qE redeclared as different kind of symbol",
3412 identifier);
3413 error ("previous declaration of %q+D",
3414 record);
3418 record = xref_tag (RECORD_TYPE, identifier);
3419 INIT_TYPE_OBJC_INFO (record);
3420 /* In the case of a @class declaration, we store the ident in
3421 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3422 we'll replace the ident with the interface. */
3423 TYPE_OBJC_INTERFACE (record) = identifier;
3424 objc_map_put (class_name_map, identifier, NULL_TREE);
3428 tree
3429 objc_is_class_name (tree ident)
3431 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3433 tree t = identifier_global_value (ident);
3434 if (t)
3435 ident = t;
3438 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3439 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3441 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3442 ident = OBJC_TYPE_NAME (ident);
3443 #ifdef OBJCPLUS
3444 if (ident && TREE_CODE (ident) == TYPE_DECL)
3446 tree type = TREE_TYPE (ident);
3447 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3448 return NULL_TREE;
3449 ident = DECL_NAME (ident);
3451 #endif
3452 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3453 return NULL_TREE;
3455 if (lookup_interface (ident))
3456 return ident;
3459 tree target;
3461 target = objc_map_get (class_name_map, ident);
3462 if (target != OBJC_MAP_NOT_FOUND)
3463 return ident;
3465 target = objc_map_get (alias_name_map, ident);
3466 if (target != OBJC_MAP_NOT_FOUND)
3467 return target;
3470 return 0;
3473 /* Check whether TYPE is either 'id' or 'Class'. */
3475 tree
3476 objc_is_id (tree type)
3478 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3480 tree t = identifier_global_value (type);
3481 if (t)
3482 type = t;
3485 if (type && TREE_CODE (type) == TYPE_DECL)
3486 type = TREE_TYPE (type);
3488 /* NB: This function may be called before the ObjC front-end has
3489 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3490 return (objc_object_type && type
3491 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3492 ? type
3493 : NULL_TREE);
3496 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3497 class instance. This is needed by other parts of the compiler to
3498 handle ObjC types gracefully. */
3500 tree
3501 objc_is_object_ptr (tree type)
3503 tree ret;
3505 type = TYPE_MAIN_VARIANT (type);
3506 if (!POINTER_TYPE_P (type))
3507 return 0;
3509 ret = objc_is_id (type);
3510 if (!ret)
3511 ret = objc_is_class_name (TREE_TYPE (type));
3513 return ret;
3516 static int
3517 objc_is_gcable_type (tree type, int or_strong_p)
3519 tree name;
3521 if (!TYPE_P (type))
3522 return 0;
3523 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3524 return 1;
3525 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3526 return 1;
3527 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3528 return 0;
3529 type = TREE_TYPE (type);
3530 if (TREE_CODE (type) != RECORD_TYPE)
3531 return 0;
3532 name = TYPE_NAME (type);
3533 return (objc_is_class_name (name) != NULL_TREE);
3536 static tree
3537 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3539 if (expr == oldexpr)
3540 return newexpr;
3542 switch (TREE_CODE (expr))
3544 case COMPONENT_REF:
3545 return objc_build_component_ref
3546 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3547 oldexpr,
3548 newexpr),
3549 DECL_NAME (TREE_OPERAND (expr, 1)));
3550 case ARRAY_REF:
3551 return build_array_ref (input_location,
3552 objc_substitute_decl (TREE_OPERAND (expr, 0),
3553 oldexpr,
3554 newexpr),
3555 TREE_OPERAND (expr, 1));
3556 case INDIRECT_REF:
3557 return build_indirect_ref (input_location,
3558 objc_substitute_decl (TREE_OPERAND (expr, 0),
3559 oldexpr,
3560 newexpr), RO_ARROW);
3561 default:
3562 return expr;
3566 static tree
3567 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3569 tree func_params;
3570 /* The LHS parameter contains the expression 'outervar->memberspec';
3571 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3572 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3574 tree offs
3575 = objc_substitute_decl
3576 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3577 tree func
3578 = (flag_objc_direct_dispatch
3579 ? objc_assign_ivar_fast_decl
3580 : objc_assign_ivar_decl);
3582 offs = convert (integer_type_node, build_unary_op (input_location,
3583 ADDR_EXPR, offs, 0));
3584 offs = fold (offs);
3585 func_params = tree_cons (NULL_TREE,
3586 convert (objc_object_type, rhs),
3587 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3588 tree_cons (NULL_TREE, offs,
3589 NULL_TREE)));
3591 return build_function_call (input_location, func, func_params);
3594 static tree
3595 objc_build_global_assignment (tree lhs, tree rhs)
3597 tree func_params = tree_cons (NULL_TREE,
3598 convert (objc_object_type, rhs),
3599 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3600 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3601 NULL_TREE));
3603 return build_function_call (input_location,
3604 objc_assign_global_decl, func_params);
3607 static tree
3608 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3610 tree func_params = tree_cons (NULL_TREE,
3611 convert (objc_object_type, rhs),
3612 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3613 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3614 NULL_TREE));
3616 return build_function_call (input_location,
3617 objc_assign_strong_cast_decl, func_params);
3620 static int
3621 objc_is_gcable_p (tree expr)
3623 return (TREE_CODE (expr) == COMPONENT_REF
3624 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3625 : TREE_CODE (expr) == ARRAY_REF
3626 ? (objc_is_gcable_p (TREE_TYPE (expr))
3627 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3628 : TREE_CODE (expr) == ARRAY_TYPE
3629 ? objc_is_gcable_p (TREE_TYPE (expr))
3630 : TYPE_P (expr)
3631 ? objc_is_gcable_type (expr, 1)
3632 : (objc_is_gcable_p (TREE_TYPE (expr))
3633 || (DECL_P (expr)
3634 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3637 static int
3638 objc_is_ivar_reference_p (tree expr)
3640 return (TREE_CODE (expr) == ARRAY_REF
3641 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3642 : TREE_CODE (expr) == COMPONENT_REF
3643 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3644 : 0);
3647 static int
3648 objc_is_global_reference_p (tree expr)
3650 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3651 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3652 : DECL_P (expr)
3653 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3654 : 0);
3657 tree
3658 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3660 tree result = NULL_TREE, outer;
3661 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3663 /* This function is currently only used with the next runtime with
3664 garbage collection enabled (-fobjc-gc). */
3665 gcc_assert (flag_next_runtime);
3667 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3668 will have been transformed to the form '*(type *)&expr'. */
3669 if (TREE_CODE (lhs) == INDIRECT_REF)
3671 outer = TREE_OPERAND (lhs, 0);
3673 while (!strong_cast_p
3674 && (CONVERT_EXPR_P (outer)
3675 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3677 tree lhstype = TREE_TYPE (outer);
3679 /* Descend down the cast chain, and record the first objc_gc
3680 attribute found. */
3681 if (POINTER_TYPE_P (lhstype))
3683 tree attr
3684 = lookup_attribute ("objc_gc",
3685 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3687 if (attr)
3688 strong_cast_p = 1;
3691 outer = TREE_OPERAND (outer, 0);
3695 /* If we have a __strong cast, it trumps all else. */
3696 if (strong_cast_p)
3698 if (modifycode != NOP_EXPR)
3699 goto invalid_pointer_arithmetic;
3701 if (warn_assign_intercept)
3702 warning (0, "strong-cast assignment has been intercepted");
3704 result = objc_build_strong_cast_assignment (lhs, rhs);
3706 goto exit_point;
3709 /* the lhs must be of a suitable type, regardless of its underlying
3710 structure. */
3711 if (!objc_is_gcable_p (lhs))
3712 goto exit_point;
3714 outer = lhs;
3716 while (outer
3717 && (TREE_CODE (outer) == COMPONENT_REF
3718 || TREE_CODE (outer) == ARRAY_REF))
3719 outer = TREE_OPERAND (outer, 0);
3721 if (TREE_CODE (outer) == INDIRECT_REF)
3723 outer = TREE_OPERAND (outer, 0);
3724 indirect_p = 1;
3727 outer_gc_p = objc_is_gcable_p (outer);
3729 /* Handle ivar assignments. */
3730 if (objc_is_ivar_reference_p (lhs))
3732 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3733 doesn't cut it here), the best we can do here is suggest a cast. */
3734 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3736 /* We may still be able to use the global write barrier... */
3737 if (!indirect_p && objc_is_global_reference_p (outer))
3738 goto global_reference;
3740 suggest_cast:
3741 if (modifycode == NOP_EXPR)
3743 if (warn_assign_intercept)
3744 warning (0, "strong-cast may possibly be needed");
3747 goto exit_point;
3750 if (modifycode != NOP_EXPR)
3751 goto invalid_pointer_arithmetic;
3753 if (warn_assign_intercept)
3754 warning (0, "instance variable assignment has been intercepted");
3756 result = objc_build_ivar_assignment (outer, lhs, rhs);
3758 goto exit_point;
3761 /* Likewise, intercept assignment to global/static variables if their type is
3762 GC-marked. */
3763 if (objc_is_global_reference_p (outer))
3765 if (indirect_p)
3766 goto suggest_cast;
3768 global_reference:
3769 if (modifycode != NOP_EXPR)
3771 invalid_pointer_arithmetic:
3772 if (outer_gc_p)
3773 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3775 goto exit_point;
3778 if (warn_assign_intercept)
3779 warning (0, "global/static variable assignment has been intercepted");
3781 result = objc_build_global_assignment (lhs, rhs);
3784 /* In all other cases, fall back to the normal mechanism. */
3785 exit_point:
3786 return result;
3789 /* Implementation of the table mapping a class name (as an identifier)
3790 to a class node. The two public functions for it are
3791 lookup_interface() and add_interface(). add_interface() is only
3792 used in this file, so we can make it static. */
3794 static GTY(()) objc_map_t interface_map;
3796 static void
3797 interface_hash_init (void)
3799 interface_map = objc_map_alloc_ggc (200);
3802 static tree
3803 add_interface (tree class_name, tree name)
3805 /* Put interfaces on list in reverse order. */
3806 TREE_CHAIN (class_name) = interface_chain;
3807 interface_chain = class_name;
3809 /* Add it to the map. */
3810 objc_map_put (interface_map, name, class_name);
3812 return interface_chain;
3815 tree
3816 lookup_interface (tree ident)
3818 #ifdef OBJCPLUS
3819 if (ident && TREE_CODE (ident) == TYPE_DECL)
3820 ident = DECL_NAME (ident);
3821 #endif
3823 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3824 return NULL_TREE;
3827 tree interface = objc_map_get (interface_map, ident);
3829 if (interface == OBJC_MAP_NOT_FOUND)
3830 return NULL_TREE;
3831 else
3832 return interface;
3838 /* Implement @defs (<classname>) within struct bodies. */
3840 tree
3841 objc_get_class_ivars (tree class_name)
3843 tree interface = lookup_interface (class_name);
3845 if (interface)
3846 return get_class_ivars (interface, true);
3848 error ("cannot find interface declaration for %qE",
3849 class_name);
3851 return error_mark_node;
3855 /* Functions used by the hashtable for field duplicates in
3856 objc_detect_field_duplicates(). Ideally, we'd use a standard
3857 key-value dictionary hashtable , and store as keys the field names,
3858 and as values the actual declarations (used to print nice error
3859 messages with the locations). But, the hashtable we are using only
3860 allows us to store keys in the hashtable, without values (it looks
3861 more like a set). So, we store the DECLs, but define equality as
3862 DECLs having the same name, and hash as the hash of the name. */
3864 struct decl_name_hash : typed_noop_remove <tree_node>
3866 typedef tree_node value_type;
3867 typedef tree_node compare_type;
3868 static inline hashval_t hash (const value_type *);
3869 static inline bool equal (const value_type *, const compare_type *);
3872 inline hashval_t
3873 decl_name_hash::hash (const value_type *q)
3875 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3878 inline bool
3879 decl_name_hash::equal (const value_type *a, const compare_type *b)
3881 return DECL_NAME (a) == DECL_NAME (b);
3884 /* Called when checking the variables in a struct. If we are not
3885 doing the ivars list inside an @interface context, then return
3886 false. Else, perform the check for duplicate ivars, then return
3887 true. The check for duplicates checks if an instance variable with
3888 the same name exists in the class or in a superclass. If
3889 'check_superclasses_only' is set to true, then it is assumed that
3890 checks for instance variables in the same class has already been
3891 performed (this is the case for ObjC++) and only the instance
3892 variables of superclasses are checked. */
3893 bool
3894 objc_detect_field_duplicates (bool check_superclasses_only)
3896 if (!objc_collecting_ivars || !objc_interface_context
3897 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3898 return false;
3900 /* We have two ways of doing this check:
3902 "direct comparison": we iterate over the instance variables and
3903 compare them directly. This works great for small numbers of
3904 instance variables (such as 10 or 20), which are extremely common.
3905 But it will potentially take forever for the pathological case with
3906 a huge number (eg, 10k) of instance variables.
3908 "hashtable": we use a hashtable, which requires a single sweep
3909 through the list of instances variables. This is much slower for a
3910 small number of variables, and we only use it for large numbers.
3912 To decide which one to use, we need to get an idea of how many
3913 instance variables we have to compare. */
3915 unsigned int number_of_ivars_to_check = 0;
3917 tree ivar;
3918 for (ivar = CLASS_RAW_IVARS (objc_interface_context);
3919 ivar; ivar = DECL_CHAIN (ivar))
3921 /* Ignore anonymous ivars. */
3922 if (DECL_NAME (ivar))
3923 number_of_ivars_to_check++;
3927 /* Exit if there is nothing to do. */
3928 if (number_of_ivars_to_check == 0)
3929 return true;
3931 /* In case that there are only 1 or 2 instance variables to check,
3932 we always use direct comparison. If there are more, it is
3933 worth iterating over the instance variables in the superclass
3934 to count how many there are (note that this has the same cost
3935 as checking 1 instance variable by direct comparison, which is
3936 why we skip this check in the case of 1 or 2 ivars and just do
3937 the direct comparison) and then decide if it worth using a
3938 hashtable. */
3939 if (number_of_ivars_to_check > 2)
3941 unsigned int number_of_superclass_ivars = 0;
3943 tree interface;
3944 for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context));
3945 interface; interface = lookup_interface (CLASS_SUPER_NAME (interface)))
3947 tree ivar;
3948 for (ivar = CLASS_RAW_IVARS (interface);
3949 ivar; ivar = DECL_CHAIN (ivar))
3950 number_of_superclass_ivars++;
3954 /* We use a hashtable if we have over 10k comparisons. */
3955 if (number_of_ivars_to_check * (number_of_superclass_ivars
3956 + (number_of_ivars_to_check / 2))
3957 > 10000)
3959 /* First, build the hashtable by putting all the instance
3960 variables of superclasses in it. */
3961 hash_table<decl_name_hash> htab (37);
3962 tree interface;
3963 for (interface = lookup_interface (CLASS_SUPER_NAME
3964 (objc_interface_context));
3965 interface; interface = lookup_interface
3966 (CLASS_SUPER_NAME (interface)))
3968 tree ivar;
3969 for (ivar = CLASS_RAW_IVARS (interface); ivar;
3970 ivar = DECL_CHAIN (ivar))
3972 if (DECL_NAME (ivar) != NULL_TREE)
3974 tree_node **slot = htab.find_slot (ivar, INSERT);
3975 /* Do not check for duplicate instance
3976 variables in superclasses. Errors have
3977 already been generated. */
3978 *slot = ivar;
3983 /* Now, we go through all the instance variables in the
3984 class, and check that they are not in the
3985 hashtable. */
3986 if (check_superclasses_only)
3988 tree ivar;
3989 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
3990 ivar = DECL_CHAIN (ivar))
3992 if (DECL_NAME (ivar) != NULL_TREE)
3994 tree duplicate_ivar = htab.find (ivar);
3995 if (duplicate_ivar != HTAB_EMPTY_ENTRY)
3997 error_at (DECL_SOURCE_LOCATION (ivar),
3998 "duplicate instance variable %q+D",
3999 ivar);
4000 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4001 "previous declaration of %q+D",
4002 duplicate_ivar);
4003 /* FIXME: Do we need the following ? */
4004 /* DECL_NAME (ivar) = NULL_TREE; */
4009 else
4011 /* If we're checking for duplicates in the class as
4012 well, we insert variables in the hashtable as we
4013 check them, so if a duplicate follows, it will be
4014 caught. */
4015 tree ivar;
4016 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
4017 ivar = DECL_CHAIN (ivar))
4019 if (DECL_NAME (ivar) != NULL_TREE)
4021 tree_node **slot = htab.find_slot (ivar, INSERT);
4022 if (*slot)
4024 tree duplicate_ivar = (tree)(*slot);
4025 error_at (DECL_SOURCE_LOCATION (ivar),
4026 "duplicate instance variable %q+D",
4027 ivar);
4028 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4029 "previous declaration of %q+D",
4030 duplicate_ivar);
4031 /* FIXME: Do we need the following ? */
4032 /* DECL_NAME (ivar) = NULL_TREE; */
4034 *slot = ivar;
4038 return true;
4043 /* This is the "direct comparison" approach, which is used in most
4044 non-pathological cases. */
4046 /* Walk up to class hierarchy, starting with this class (this is
4047 the external loop, because lookup_interface() is expensive, and
4048 we want to do it few times). */
4049 tree interface = objc_interface_context;
4051 if (check_superclasses_only)
4052 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4054 for ( ; interface; interface = lookup_interface
4055 (CLASS_SUPER_NAME (interface)))
4057 tree ivar_being_checked;
4059 for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context);
4060 ivar_being_checked;
4061 ivar_being_checked = DECL_CHAIN (ivar_being_checked))
4063 tree decl;
4065 /* Ignore anonymous ivars. */
4066 if (DECL_NAME (ivar_being_checked) == NULL_TREE)
4067 continue;
4069 /* Note how we stop when we find the ivar we are checking
4070 (this can only happen in the main class, not
4071 superclasses), to avoid comparing things twice
4072 (otherwise, for each ivar, you'd compare A to B then B
4073 to A, and get duplicated error messages). */
4074 for (decl = CLASS_RAW_IVARS (interface);
4075 decl && decl != ivar_being_checked;
4076 decl = DECL_CHAIN (decl))
4078 if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl))
4080 error_at (DECL_SOURCE_LOCATION (ivar_being_checked),
4081 "duplicate instance variable %q+D",
4082 ivar_being_checked);
4083 inform (DECL_SOURCE_LOCATION (decl),
4084 "previous declaration of %q+D",
4085 decl);
4086 /* FIXME: Do we need the following ? */
4087 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4093 return true;
4096 /* Used by: build_private_template, continue_class,
4097 and for @defs constructs. */
4099 static tree
4100 get_class_ivars (tree interface, bool inherited)
4102 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4104 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4105 by the current class (i.e., they do not include super-class ivars).
4106 However, the CLASS_IVARS list will be side-effected by a call to
4107 finish_struct(), which will fill in field offsets. */
4108 if (!CLASS_IVARS (interface))
4109 CLASS_IVARS (interface) = ivar_chain;
4111 if (!inherited)
4112 return ivar_chain;
4114 while (CLASS_SUPER_NAME (interface))
4116 /* Prepend super-class ivars. */
4117 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4118 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4119 ivar_chain);
4122 return ivar_chain;
4125 void
4126 objc_maybe_warn_exceptions (location_t loc)
4128 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4129 For example, on Darwin, ObjC exceptions require a sufficiently
4130 recent version of the runtime, so the user must ask for them
4131 explicitly. On other platforms, at the moment -fobjc-exceptions
4132 triggers -fexceptions which again is required for exceptions to
4133 work. */
4134 if (!flag_objc_exceptions)
4136 /* Warn only once per compilation unit. */
4137 static bool warned = false;
4139 if (!warned)
4141 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4142 warned = true;
4147 static struct objc_try_context *cur_try_context;
4149 /* Called just after parsing the @try and its associated BODY. We now
4150 must prepare for the tricky bits -- handling the catches and finally. */
4152 void
4153 objc_begin_try_stmt (location_t try_locus, tree body)
4155 struct objc_try_context *c = XCNEW (struct objc_try_context);
4156 c->outer = cur_try_context;
4157 c->try_body = body;
4158 c->try_locus = try_locus;
4159 c->end_try_locus = input_location;
4160 cur_try_context = c;
4162 /* Collect the list of local variables. We'll mark them as volatile
4163 at the end of compilation of this function to prevent them being
4164 clobbered by setjmp/longjmp. */
4165 if (flag_objc_sjlj_exceptions)
4166 objc_mark_locals_volatile (NULL);
4169 /* Called just after parsing "@catch (parm)". Open a binding level,
4170 enter DECL into the binding level, and initialize it. Leave the
4171 binding level open while the body of the compound statement is
4172 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4173 which we compile as "@catch(id tmp_variable)". */
4175 void
4176 objc_begin_catch_clause (tree decl)
4178 tree compound, type, t;
4179 bool ellipsis = false;
4181 /* Begin a new scope that the entire catch clause will live in. */
4182 compound = c_begin_compound_stmt (true);
4184 /* Create the appropriate declaration for the argument. */
4185 if (decl == error_mark_node)
4186 type = error_mark_node;
4187 else
4189 if (decl == NULL_TREE)
4191 /* If @catch(...) was specified, create a temporary variable of
4192 type 'id' and use it. */
4193 decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var");
4194 DECL_SOURCE_LOCATION (decl) = input_location;
4195 /* ... but allow the runtime to differentiate between ellipsis and the
4196 case of @catch (id xyz). */
4197 ellipsis = true;
4199 else
4201 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4202 decl = build_decl (input_location,
4203 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4205 lang_hooks.decls.pushdecl (decl);
4207 /* Mark the declaration as used so you never any warnings whether
4208 you use the exception argument or not. TODO: Implement a
4209 -Wunused-exception-parameter flag, which would cause warnings
4210 if exception parameter is not used. */
4211 TREE_USED (decl) = 1;
4212 DECL_READ_P (decl) = 1;
4214 type = TREE_TYPE (decl);
4217 /* Verify that the type of the catch is valid. It must be a pointer
4218 to an Objective-C class, or "id" (which is catch-all). */
4219 if (type == error_mark_node)
4221 ;/* Just keep going. */
4223 else if (!objc_type_valid_for_messaging (type, false))
4225 error ("@catch parameter is not a known Objective-C class type");
4226 type = error_mark_node;
4228 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type))
4229 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type)))
4231 error ("@catch parameter can not be protocol-qualified");
4232 type = error_mark_node;
4234 else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4235 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4236 identify 'id'. */
4238 else
4240 /* If 'type' was built using typedefs, we need to get rid of
4241 them and get a simple pointer to the class. */
4242 bool is_typedef = false;
4243 tree x = TYPE_MAIN_VARIANT (type);
4245 /* Skip from the pointer to the pointee. */
4246 if (TREE_CODE (x) == POINTER_TYPE)
4247 x = TREE_TYPE (x);
4249 /* Traverse typedef aliases */
4250 while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x)
4251 && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL
4252 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)))
4254 is_typedef = true;
4255 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4258 /* If it was a typedef, build a pointer to the final, original
4259 class. */
4260 if (is_typedef)
4261 type = build_pointer_type (x);
4263 if (cur_try_context->catch_list)
4265 /* Examine previous @catch clauses and see if we've already
4266 caught the type in question. */
4267 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4268 for (; !tsi_end_p (i); tsi_next (&i))
4270 tree stmt = tsi_stmt (i);
4271 t = CATCH_TYPES (stmt);
4272 if (t == error_mark_node)
4273 continue;
4274 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4276 warning (0, "exception of type %<%T%> will be caught",
4277 TREE_TYPE (type));
4278 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4279 TREE_TYPE (t ? t : objc_object_type));
4280 break;
4286 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4287 add_stmt (t);
4290 /* Called just after parsing the closing brace of a @catch clause. Close
4291 the open binding level, and record a CATCH_EXPR for it. */
4293 void
4294 objc_finish_catch_clause (void)
4296 tree c = cur_try_context->current_catch;
4297 cur_try_context->current_catch = NULL;
4298 cur_try_context->end_catch_locus = input_location;
4300 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4302 (*runtime.finish_catch) (&cur_try_context, c);
4305 /* Called after parsing a @finally clause and its associated BODY.
4306 Record the body for later placement. */
4308 void
4309 objc_build_finally_clause (location_t finally_locus, tree body)
4311 cur_try_context->finally_body = body;
4312 cur_try_context->finally_locus = finally_locus;
4313 cur_try_context->end_finally_locus = input_location;
4316 /* Called to finalize a @try construct. */
4318 tree
4319 objc_finish_try_stmt (void)
4321 struct objc_try_context *c = cur_try_context;
4322 tree stmt;
4324 if (c->catch_list == NULL && c->finally_body == NULL)
4325 error ("%<@try%> without %<@catch%> or %<@finally%>");
4327 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4328 add_stmt (stmt);
4330 cur_try_context = c->outer;
4331 free (c);
4332 return stmt;
4335 tree
4336 objc_build_throw_stmt (location_t loc, tree throw_expr)
4338 bool rethrown = false;
4340 objc_maybe_warn_exceptions (loc);
4342 /* Don't waste time trying to build something if we're already dead. */
4343 if (throw_expr == error_mark_node)
4344 return error_mark_node;
4346 if (throw_expr == NULL)
4348 /* If we're not inside a @catch block, there is no "current
4349 exception" to be rethrown. */
4350 if (cur_try_context == NULL
4351 || cur_try_context->current_catch == NULL)
4353 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4354 return error_mark_node;
4357 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4358 value that we get from the runtime. */
4359 throw_expr = (*runtime.build_exc_ptr) (&cur_try_context);
4360 rethrown = true;
4362 else
4364 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
4366 error_at (loc, "%<@throw%> argument is not an object");
4367 return error_mark_node;
4371 return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown);
4374 tree
4375 objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
4377 /* object_expr should never be NULL; but in case it is, convert it to
4378 error_mark_node. */
4379 if (object_expr == NULL)
4380 object_expr = error_mark_node;
4382 /* Validate object_expr. If not valid, set it to error_mark_node. */
4383 if (object_expr != error_mark_node)
4385 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true))
4387 error_at (start_locus, "%<@synchronized%> argument is not an object");
4388 object_expr = error_mark_node;
4392 if (object_expr == error_mark_node)
4394 /* If we found an error, we simply ignore the '@synchronized'.
4395 Compile the body so we can keep going with minimal
4396 casualties. */
4397 return add_stmt (body);
4399 else
4401 tree call;
4402 tree args;
4404 /* objc_sync_enter (object_expr); */
4405 object_expr = save_expr (object_expr);
4406 args = tree_cons (NULL, object_expr, NULL);
4407 call = build_function_call (input_location,
4408 objc_sync_enter_decl, args);
4409 SET_EXPR_LOCATION (call, start_locus);
4410 add_stmt (call);
4412 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4413 it goes inside the @finalize() clause. */
4414 args = tree_cons (NULL, object_expr, NULL);
4415 call = build_function_call (input_location,
4416 objc_sync_exit_decl, args);
4417 SET_EXPR_LOCATION (call, input_location);
4419 /* @try { body; } */
4420 objc_begin_try_stmt (start_locus, body);
4422 /* @finally { objc_sync_exit (object_expr); } */
4423 objc_build_finally_clause (input_location, call);
4425 /* End of try statement. */
4426 return objc_finish_try_stmt ();
4430 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4431 name as the class:
4433 struct <classname> {
4434 struct _objc_class *isa;
4436 }; */
4438 static void
4439 build_private_template (tree klass)
4441 if (!CLASS_STATIC_TEMPLATE (klass))
4443 tree record = objc_build_struct (klass,
4444 get_class_ivars (klass, false),
4445 CLASS_SUPER_NAME (klass));
4447 /* Set the TREE_USED bit for this struct, so that stab generator
4448 can emit stabs for this struct type. */
4449 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4450 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4452 /* Copy the attributes from the class to the type. */
4453 if (TREE_DEPRECATED (klass))
4454 TREE_DEPRECATED (record) = 1;
4458 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4459 current class. */
4460 #ifdef OBJCPLUS
4461 static void
4462 objc_generate_cxx_ctor_or_dtor (bool dtor)
4464 tree fn, body, compound_stmt, ivar;
4466 /* - (id) .cxx_construct { ... return self; } */
4467 /* - (void) .cxx_construct { ... } */
4469 objc_start_method_definition
4470 (false /* is_class_method */,
4471 objc_build_method_signature (false /* is_class_method */,
4472 build_tree_list (NULL_TREE,
4473 dtor
4474 ? void_type_node
4475 : objc_object_type),
4476 get_identifier (dtor
4477 ? TAG_CXX_DESTRUCT
4478 : TAG_CXX_CONSTRUCT),
4479 make_node (TREE_LIST),
4480 false), NULL, NULL_TREE);
4481 body = begin_function_body ();
4482 compound_stmt = begin_compound_stmt (0);
4484 ivar = CLASS_IVARS (implementation_template);
4485 /* Destroy ivars in reverse order. */
4486 if (dtor)
4487 ivar = nreverse (copy_list (ivar));
4489 for (; ivar; ivar = TREE_CHAIN (ivar))
4491 if (TREE_CODE (ivar) == FIELD_DECL)
4493 tree type = TREE_TYPE (ivar);
4495 /* Call the ivar's default constructor or destructor. Do not
4496 call the destructor unless a corresponding constructor call
4497 has also been made (or is not needed). */
4498 if (MAYBE_CLASS_TYPE_P (type)
4499 && (dtor
4500 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4501 && (!TYPE_NEEDS_CONSTRUCTING (type)
4502 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4503 : (TYPE_NEEDS_CONSTRUCTING (type)
4504 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4505 finish_expr_stmt
4506 (build_special_member_call
4507 (build_ivar_reference (DECL_NAME (ivar)),
4508 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4509 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
4513 /* The constructor returns 'self'. */
4514 if (!dtor)
4515 finish_return_stmt (self_decl);
4517 finish_compound_stmt (compound_stmt);
4518 finish_function_body (body);
4519 fn = current_function_decl;
4520 finish_function ();
4521 objc_finish_method_definition (fn);
4524 /* The following routine will examine the current @interface for any
4525 non-POD C++ ivars requiring non-trivial construction and/or
4526 destruction, and then synthesize special '- .cxx_construct' and/or
4527 '- .cxx_destruct' methods which will run the appropriate
4528 construction or destruction code. Note that ivars inherited from
4529 super-classes are _not_ considered. */
4530 static void
4531 objc_generate_cxx_cdtors (void)
4533 bool need_ctor = false, need_dtor = false;
4534 tree ivar;
4536 /* Error case, due to possibly an extra @end. */
4537 if (!objc_implementation_context)
4538 return;
4540 /* We do not want to do this for categories, since they do not have
4541 their own ivars. */
4543 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4544 return;
4546 /* First, determine if we even need a constructor and/or destructor. */
4548 for (ivar = CLASS_IVARS (implementation_template); ivar;
4549 ivar = TREE_CHAIN (ivar))
4551 if (TREE_CODE (ivar) == FIELD_DECL)
4553 tree type = TREE_TYPE (ivar);
4555 if (MAYBE_CLASS_TYPE_P (type))
4557 if (TYPE_NEEDS_CONSTRUCTING (type)
4558 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4559 /* NB: If a default constructor is not available, we will not
4560 be able to initialize this ivar; the add_instance_variable()
4561 routine will already have warned about this. */
4562 need_ctor = true;
4564 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4565 && (!TYPE_NEEDS_CONSTRUCTING (type)
4566 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4567 /* NB: If a default constructor is not available, we will not
4568 call the destructor either, for symmetry. */
4569 need_dtor = true;
4574 /* Generate '- .cxx_construct' if needed. */
4576 if (need_ctor)
4577 objc_generate_cxx_ctor_or_dtor (false);
4579 /* Generate '- .cxx_destruct' if needed. */
4581 if (need_dtor)
4582 objc_generate_cxx_ctor_or_dtor (true);
4584 /* The 'imp_list' variable points at an imp_entry record for the current
4585 @implementation. Record the existence of '- .cxx_construct' and/or
4586 '- .cxx_destruct' methods therein; it will be included in the
4587 metadata for the class if the runtime needs it. */
4588 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
4590 #endif
4592 static void
4593 error_with_ivar (const char *message, tree decl)
4595 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4596 message, identifier_to_locale (gen_declaration (decl)));
4600 static void
4601 check_ivars (tree inter, tree imp)
4603 tree intdecls = CLASS_RAW_IVARS (inter);
4604 tree impdecls = CLASS_RAW_IVARS (imp);
4606 while (1)
4608 tree t1, t2;
4610 #ifdef OBJCPLUS
4611 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4612 intdecls = TREE_CHAIN (intdecls);
4613 #endif
4614 if (intdecls == 0 && impdecls == 0)
4615 break;
4616 if (intdecls == 0 || impdecls == 0)
4618 error ("inconsistent instance variable specification");
4619 break;
4622 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
4624 if (!comptypes (t1, t2)
4625 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4626 DECL_INITIAL (impdecls)))
4628 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4630 error_with_ivar ("conflicting instance variable type",
4631 impdecls);
4632 error_with_ivar ("previous declaration of",
4633 intdecls);
4635 else /* both the type and the name don't match */
4637 error ("inconsistent instance variable specification");
4638 break;
4642 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4644 error_with_ivar ("conflicting instance variable name",
4645 impdecls);
4646 error_with_ivar ("previous declaration of",
4647 intdecls);
4650 intdecls = DECL_CHAIN (intdecls);
4651 impdecls = DECL_CHAIN (impdecls);
4656 static void
4657 mark_referenced_methods (void)
4659 struct imp_entry *impent;
4660 tree chain;
4662 for (impent = imp_list; impent; impent = impent->next)
4664 chain = CLASS_CLS_METHODS (impent->imp_context);
4665 while (chain)
4667 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4668 chain = DECL_CHAIN (chain);
4671 chain = CLASS_NST_METHODS (impent->imp_context);
4672 while (chain)
4674 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4675 chain = DECL_CHAIN (chain);
4680 /* If type is empty or only type qualifiers are present, add default
4681 type of id (otherwise grokdeclarator will default to int). */
4682 static inline tree
4683 adjust_type_for_id_default (tree type)
4685 if (!type)
4686 type = make_node (TREE_LIST);
4688 if (!TREE_VALUE (type))
4689 TREE_VALUE (type) = objc_object_type;
4690 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4691 && TYPED_OBJECT (TREE_VALUE (type)))
4692 error ("can not use an object as parameter to a method");
4694 return type;
4697 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4698 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4699 OBJC_METHOD_PARM_DECL ?)
4701 A KEYWORD_DECL is a tree representing the declaration of a
4702 parameter of an Objective-C method. It is produced when parsing a
4703 fragment of Objective-C method declaration of the form
4705 keyworddecl:
4706 selector ':' '(' typename ')' identifier
4708 For example, take the Objective-C method
4710 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4712 the two fragments "pathForResource:(NSString *)resource" and
4713 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4714 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4715 "pathForResource"), the 'arg_type' (eg, tree representing a
4716 NSString *), the 'arg_name' (eg identifier for "resource") and
4717 potentially some attributes (for example, a tree representing
4718 __attribute__ ((unused)) if such an attribute was attached to a
4719 certain parameter). You can access this information using the
4720 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4721 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4723 'key_name' is an identifier node (and is optional as you can omit
4724 it in Objective-C methods).
4725 'arg_type' is a tree list (and is optional too if no parameter type
4726 was specified).
4727 'arg_name' is an identifier node and is required.
4728 'attributes' is an optional tree containing parameter attributes. */
4729 tree
4730 objc_build_keyword_decl (tree key_name, tree arg_type,
4731 tree arg_name, tree attributes)
4733 tree keyword_decl;
4735 if (flag_objc1_only && attributes)
4736 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4738 /* If no type is specified, default to "id". */
4739 arg_type = adjust_type_for_id_default (arg_type);
4741 keyword_decl = make_node (KEYWORD_DECL);
4743 TREE_TYPE (keyword_decl) = arg_type;
4744 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4745 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4746 DECL_ATTRIBUTES (keyword_decl) = attributes;
4748 return keyword_decl;
4751 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4752 static tree
4753 build_keyword_selector (tree selector)
4755 int len = 0;
4756 tree key_chain, key_name;
4757 char *buf;
4759 /* Scan the selector to see how much space we'll need. */
4760 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4762 switch (TREE_CODE (selector))
4764 case KEYWORD_DECL:
4765 key_name = KEYWORD_KEY_NAME (key_chain);
4766 break;
4767 case TREE_LIST:
4768 key_name = TREE_PURPOSE (key_chain);
4769 break;
4770 default:
4771 gcc_unreachable ();
4774 if (key_name)
4775 len += IDENTIFIER_LENGTH (key_name) + 1;
4776 else
4777 /* Just a ':' arg. */
4778 len++;
4781 buf = (char *) alloca (len + 1);
4782 /* Start the buffer out as an empty string. */
4783 buf[0] = '\0';
4785 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4787 switch (TREE_CODE (selector))
4789 case KEYWORD_DECL:
4790 key_name = KEYWORD_KEY_NAME (key_chain);
4791 break;
4792 case TREE_LIST:
4793 key_name = TREE_PURPOSE (key_chain);
4794 /* The keyword decl chain will later be used as a function
4795 argument chain. Unhook the selector itself so as to not
4796 confuse other parts of the compiler. */
4797 TREE_PURPOSE (key_chain) = NULL_TREE;
4798 break;
4799 default:
4800 gcc_unreachable ();
4803 if (key_name)
4804 strcat (buf, IDENTIFIER_POINTER (key_name));
4805 strcat (buf, ":");
4808 return get_identifier_with_length (buf, len);
4811 /* Used for declarations and definitions. */
4813 static tree
4814 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4815 tree add_args, bool ellipsis)
4817 tree method_decl;
4819 /* If no type is specified, default to "id". */
4820 ret_type = adjust_type_for_id_default (ret_type);
4822 /* Note how a method_decl has a TREE_TYPE which is not the function
4823 type of the function implementing the method, but only the return
4824 type of the method. We may want to change this, and store the
4825 entire function type in there (eg, it may be used to simplify
4826 dealing with attributes below). */
4827 method_decl = make_node (code);
4828 TREE_TYPE (method_decl) = ret_type;
4830 /* If we have a keyword selector, create an identifier_node that
4831 represents the full selector name (`:' included)... */
4832 if (TREE_CODE (selector) == KEYWORD_DECL)
4834 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4835 METHOD_SEL_ARGS (method_decl) = selector;
4836 METHOD_ADD_ARGS (method_decl) = add_args;
4837 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4839 else
4841 METHOD_SEL_NAME (method_decl) = selector;
4842 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4843 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4846 return method_decl;
4849 /* This routine processes objective-c method attributes. */
4851 static void
4852 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4854 /* TODO: Replace the hackery below. An idea would be to store the
4855 full function type in the method declaration (for example in
4856 TREE_TYPE) and then expose ObjC method declarations to c-family
4857 and they could deal with them by simply treating them as
4858 functions. */
4860 /* Because of the dangers in the hackery below, we filter out any
4861 attribute that we do not know about. For the ones we know about,
4862 we know that they work with the hackery. For the other ones,
4863 there is no guarantee, so we have to filter them out. */
4864 tree filtered_attributes = NULL_TREE;
4866 if (attributes)
4868 tree attribute;
4869 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4871 tree name = TREE_PURPOSE (attribute);
4873 if (is_attribute_p ("deprecated", name)
4874 || is_attribute_p ("sentinel", name)
4875 || is_attribute_p ("noreturn", name))
4877 /* An attribute that we support; add it to the filtered
4878 attributes. */
4879 filtered_attributes = chainon (filtered_attributes,
4880 copy_node (attribute));
4882 else if (is_attribute_p ("format", name))
4884 /* "format" is special because before adding it to the
4885 filtered attributes we need to adjust the specified
4886 format by adding the hidden function parameters for
4887 an Objective-C method (self, _cmd). */
4888 tree new_attribute = copy_node (attribute);
4890 /* Check the arguments specified with the attribute, and
4891 modify them adding 2 for the two hidden arguments.
4892 Note how this differs from C++; according to the
4893 specs, C++ does not do it so you have to add the +1
4894 yourself. For Objective-C, instead, the compiler
4895 adds the +2 for you. */
4897 /* The attribute arguments have not been checked yet, so
4898 we need to be careful as they could be missing or
4899 invalid. If anything looks wrong, we skip the
4900 process and the compiler will complain about it later
4901 when it validates the attribute. */
4902 /* Check that we have at least three arguments. */
4903 if (TREE_VALUE (new_attribute)
4904 && TREE_CHAIN (TREE_VALUE (new_attribute))
4905 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4907 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4908 tree third_argument = TREE_CHAIN (second_argument);
4909 tree number;
4911 /* This is the second argument, the "string-index",
4912 which specifies the index of the format string
4913 argument. Add 2. */
4914 number = TREE_VALUE (second_argument);
4915 if (number
4916 && TREE_CODE (number) == INTEGER_CST
4917 && !wi::eq_p (number, 0))
4918 TREE_VALUE (second_argument)
4919 = wide_int_to_tree (TREE_TYPE (number),
4920 wi::add (number, 2));
4922 /* This is the third argument, the "first-to-check",
4923 which specifies the index of the first argument to
4924 check. This could be 0, meaning it is not available,
4925 in which case we don't need to add 2. Add 2 if not
4926 0. */
4927 number = TREE_VALUE (third_argument);
4928 if (number
4929 && TREE_CODE (number) == INTEGER_CST
4930 && !wi::eq_p (number, 0))
4931 TREE_VALUE (third_argument)
4932 = wide_int_to_tree (TREE_TYPE (number),
4933 wi::add (number, 2));
4935 filtered_attributes = chainon (filtered_attributes,
4936 new_attribute);
4938 else if (is_attribute_p ("nonnull", name))
4940 /* We need to fixup all the argument indexes by adding 2
4941 for the two hidden arguments of an Objective-C method
4942 invocation, similat to what we do above for the
4943 "format" attribute. */
4944 /* FIXME: This works great in terms of implementing the
4945 functionality, but the warnings that are produced by
4946 nonnull do mention the argument index (while the
4947 format ones don't). For example, you could get
4948 "warning: null argument where non-null required
4949 (argument 3)". Now in that message, "argument 3"
4950 includes the 2 hidden arguments; it would be much
4951 more friendly to call it "argument 1", as that would
4952 be consistent with __attribute__ ((nonnnull (1))).
4953 To do this, we'd need to have the C family code that
4954 checks the arguments know about adding/removing 2 to
4955 the argument index ... or alternatively we could
4956 maybe store the "printable" argument index in
4957 addition to the actual argument index ? Some
4958 refactoring is needed to do this elegantly. */
4959 tree new_attribute = copy_node (attribute);
4960 tree argument = TREE_VALUE (attribute);
4961 while (argument != NULL_TREE)
4963 /* Get the value of the argument and add 2. */
4964 tree number = TREE_VALUE (argument);
4965 if (number && TREE_CODE (number) == INTEGER_CST
4966 && !wi::eq_p (number, 0))
4967 TREE_VALUE (argument)
4968 = wide_int_to_tree (TREE_TYPE (number),
4969 wi::add (number, 2));
4970 argument = TREE_CHAIN (argument);
4973 filtered_attributes = chainon (filtered_attributes,
4974 new_attribute);
4976 else
4977 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
4981 if (filtered_attributes)
4983 /* This hackery changes the TREE_TYPE of the ObjC method
4984 declaration to be a function type, so that decl_attributes
4985 will treat the ObjC method as if it was a function. Some
4986 attributes (sentinel, format) will be applied to the function
4987 type, changing it in place; so after calling decl_attributes,
4988 we extract the function type attributes and store them in
4989 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4990 deprecated) are applied directly to the method declaration
4991 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4992 is nothing to do. */
4993 tree saved_type = TREE_TYPE (*node);
4994 TREE_TYPE (*node)
4995 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
4996 METHOD_REF, 0);
4997 decl_attributes (node, filtered_attributes, flags);
4998 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
4999 TREE_TYPE (*node) = saved_type;
5003 bool
5004 objc_method_decl (enum tree_code opcode)
5006 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
5009 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
5010 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
5011 method or calling one. SUPER_FLAG indicates whether this is a send
5012 to super; this makes a difference for the NeXT calling sequence in
5013 which the lookup and the method call are done together. If METHOD is
5014 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5015 represented as varargs. */
5017 tree
5018 build_function_type_for_method (tree return_type, tree method,
5019 int context, bool super_flag)
5021 vec<tree, va_gc> *argtypes = make_tree_vector ();
5022 tree t, ftype;
5023 bool is_varargs = false;
5025 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5027 /* No actual method prototype given; remaining args passed as varargs. */
5028 if (method == NULL_TREE)
5030 is_varargs = true;
5031 goto build_ftype;
5034 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5036 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5038 /* Decay argument types for the underlying C function as
5039 appropriate. */
5040 arg_type = objc_decay_parm_type (arg_type);
5042 vec_safe_push (argtypes, arg_type);
5045 if (METHOD_ADD_ARGS (method))
5047 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5048 t; t = TREE_CHAIN (t))
5050 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5052 arg_type = objc_decay_parm_type (arg_type);
5054 vec_safe_push (argtypes, arg_type);
5057 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5058 is_varargs = true;
5061 build_ftype:
5062 if (is_varargs)
5063 ftype = build_varargs_function_type_vec (return_type, argtypes);
5064 else
5065 ftype = build_function_type_vec (return_type, argtypes);
5067 release_tree_vector (argtypes);
5068 return ftype;
5071 /* The 'method' argument is a tree; this tree could either be a single
5072 method, which is returned, or could be a TREE_VEC containing a list
5073 of methods. In that case, the first one is returned, and warnings
5074 are issued as appropriate. */
5075 static tree
5076 check_duplicates (tree method, int methods, int is_class)
5078 tree first_method;
5079 size_t i;
5081 if (method == NULL_TREE)
5082 return NULL_TREE;
5084 if (TREE_CODE (method) != TREE_VEC)
5085 return method;
5087 /* We have two or more methods with the same name but different
5088 types. */
5089 first_method = TREE_VEC_ELT (method, 0);
5091 /* But just how different are those types? If
5092 -Wno-strict-selector-match is specified, we shall not complain if
5093 the differences are solely among types with identical size and
5094 alignment. */
5095 if (!warn_strict_selector_match)
5097 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5098 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5099 goto issue_warning;
5101 return first_method;
5104 issue_warning:
5105 if (methods)
5107 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5109 warning_at (input_location, 0,
5110 "multiple methods named %<%c%E%> found",
5111 (is_class ? '+' : '-'),
5112 METHOD_SEL_NAME (first_method));
5113 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5114 (type ? '-' : '+'),
5115 identifier_to_locale (gen_method_decl (first_method)));
5117 else
5119 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5121 warning_at (input_location, 0,
5122 "multiple selectors named %<%c%E%> found",
5123 (is_class ? '+' : '-'),
5124 METHOD_SEL_NAME (first_method));
5125 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5126 (type ? '-' : '+'),
5127 identifier_to_locale (gen_method_decl (first_method)));
5130 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5132 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5134 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5135 (type ? '-' : '+'),
5136 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5139 return first_method;
5142 /* If RECEIVER is a class reference, return the identifier node for
5143 the referenced class. RECEIVER is created by objc_get_class_reference,
5144 so we check the exact form created depending on which runtimes are
5145 used. */
5147 static tree
5148 receiver_is_class_object (tree receiver, int self, int super)
5150 tree exp, arg;
5152 /* The receiver is 'self' or 'super' in the context of a class method. */
5153 if (objc_method_context
5154 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5155 && (self || super))
5156 return (super
5157 ? CLASS_SUPER_NAME (implementation_template)
5158 : CLASS_NAME (implementation_template));
5160 /* The runtime might encapsulate things its own way. */
5161 exp = (*runtime.receiver_is_class_object) (receiver);
5162 if (exp)
5163 return exp;
5165 /* The receiver is a function call that returns an id. Check if
5166 it is a call to objc_getClass, if so, pick up the class name.
5168 This is required by the GNU runtime, which compiles
5170 [NSObject alloc]
5172 into
5174 [objc_get_class ("NSObject") alloc];
5176 and then, to check that the receiver responds to the +alloc
5177 method, needs to be able to determine that the objc_get_class()
5178 call returns the NSObject class and not just a generic Class
5179 pointer.
5181 But, traditionally this is enabled for all runtimes, not just the
5182 GNU one, which means that the compiler is smarter than you'd
5183 expect when dealing with objc_getClass(). For example, with the
5184 Apple runtime, in the code
5186 [objc_getClass ("NSObject") alloc];
5188 the compiler will recognize the objc_getClass() call as special
5189 (due to the code below) and so will know that +alloc is called on
5190 the 'NSObject' class, and can perform the corresponding checks.
5192 Programmers can disable this behaviour by casting the results of
5193 objc_getClass() to 'Class' (this may seem weird because
5194 objc_getClass() is already declared to return 'Class', but the
5195 compiler treats it as a special function). This may be useful if
5196 the class is never declared, and the compiler would complain
5197 about a missing @interface for it. Then, you can do
5199 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5201 to silence the warnings. */
5202 if (TREE_CODE (receiver) == CALL_EXPR
5203 && (exp = CALL_EXPR_FN (receiver))
5204 && TREE_CODE (exp) == ADDR_EXPR
5205 && (exp = TREE_OPERAND (exp, 0))
5206 && TREE_CODE (exp) == FUNCTION_DECL
5207 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5208 prototypes for objc_get_class(). Thankfully, they seem to share the
5209 same function type. */
5210 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5211 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5212 /* We have a call to objc_get_class/objc_getClass! */
5213 && (arg = CALL_EXPR_ARG (receiver, 0)))
5215 STRIP_NOPS (arg);
5216 if (TREE_CODE (arg) == ADDR_EXPR
5217 && (arg = TREE_OPERAND (arg, 0))
5218 && TREE_CODE (arg) == STRING_CST)
5219 /* Finally, we have the class name. */
5220 return get_identifier (TREE_STRING_POINTER (arg));
5222 return 0;
5225 /* If we are currently building a message expr, this holds
5226 the identifier of the selector of the message. This is
5227 used when printing warnings about argument mismatches. */
5229 static tree current_objc_message_selector = 0;
5231 tree
5232 objc_message_selector (void)
5234 return current_objc_message_selector;
5237 /* Construct an expression for sending a message.
5238 MESS has the object to send to in TREE_PURPOSE
5239 and the argument list (including selector) in TREE_VALUE.
5241 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5242 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5244 tree
5245 objc_build_message_expr (tree receiver, tree message_args)
5247 tree sel_name;
5248 #ifdef OBJCPLUS
5249 tree args = TREE_PURPOSE (message_args);
5250 #else
5251 tree args = message_args;
5252 #endif
5253 tree method_params = NULL_TREE;
5255 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5256 return error_mark_node;
5258 /* Obtain the full selector name. */
5259 switch (TREE_CODE (args))
5261 case IDENTIFIER_NODE:
5262 /* A unary selector. */
5263 sel_name = args;
5264 break;
5265 case TREE_LIST:
5266 sel_name = build_keyword_selector (args);
5267 break;
5268 default:
5269 gcc_unreachable ();
5272 /* Build the parameter list to give to the method. */
5273 if (TREE_CODE (args) == TREE_LIST)
5274 #ifdef OBJCPLUS
5275 method_params = chainon (args, TREE_VALUE (message_args));
5276 #else
5278 tree chain = args, prev = NULL_TREE;
5280 /* We have a keyword selector--check for comma expressions. */
5281 while (chain)
5283 tree element = TREE_VALUE (chain);
5285 /* We have a comma expression, must collapse... */
5286 if (TREE_CODE (element) == TREE_LIST)
5288 if (prev)
5289 TREE_CHAIN (prev) = element;
5290 else
5291 args = element;
5293 prev = chain;
5294 chain = TREE_CHAIN (chain);
5296 method_params = args;
5298 #endif
5300 #ifdef OBJCPLUS
5301 if (processing_template_decl)
5302 /* Must wait until template instantiation time. */
5303 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5304 sel_name, method_params);
5305 #endif
5307 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5310 /* Look up method SEL_NAME that would be suitable for receiver
5311 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5312 nonzero), and report on any duplicates. */
5314 static tree
5315 lookup_method_in_hash_lists (tree sel_name, int is_class)
5317 tree method_prototype = OBJC_MAP_NOT_FOUND;
5319 if (!is_class)
5320 method_prototype = objc_map_get (instance_method_map, sel_name);
5322 if (method_prototype == OBJC_MAP_NOT_FOUND)
5324 method_prototype = objc_map_get (class_method_map, sel_name);
5325 is_class = 1;
5327 if (method_prototype == OBJC_MAP_NOT_FOUND)
5328 return NULL_TREE;
5331 return check_duplicates (method_prototype, 1, is_class);
5334 /* The 'objc_finish_message_expr' routine is called from within
5335 'objc_build_message_expr' for non-template functions. In the case of
5336 C++ template functions, it is called from 'build_expr_from_tree'
5337 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5339 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5340 if the method being used is deprecated. If it is not NULL, instead
5341 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5342 prototype that was used and is deprecated. This is useful for
5343 getter calls that are always generated when compiling dot-syntax
5344 expressions, even if they may not be used. In that case, we don't
5345 want the warning immediately; we produce it (if needed) at gimplify
5346 stage when we are sure that the deprecated getter is being
5347 used. */
5348 tree
5349 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5350 tree *deprecated_method_prototype)
5352 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5353 tree retval, class_tree;
5354 int self, super, have_cast;
5356 /* We have used the receiver, so mark it as read. */
5357 mark_exp_read (receiver);
5359 /* Extract the receiver of the message, as well as its type
5360 (where the latter may take the form of a cast or be inferred
5361 from the implementation context). */
5362 rtype = receiver;
5363 while (TREE_CODE (rtype) == COMPOUND_EXPR
5364 || TREE_CODE (rtype) == MODIFY_EXPR
5365 || CONVERT_EXPR_P (rtype)
5366 || TREE_CODE (rtype) == COMPONENT_REF)
5367 rtype = TREE_OPERAND (rtype, 0);
5369 /* self is 1 if this is a message to self, 0 otherwise */
5370 self = (rtype == self_decl);
5372 /* super is 1 if this is a message to super, 0 otherwise. */
5373 super = (rtype == UOBJC_SUPER_decl);
5375 /* rtype is the type of the receiver. */
5376 rtype = TREE_TYPE (receiver);
5378 /* have_cast is 1 if the receiver is casted. */
5379 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5380 || (TREE_CODE (receiver) == COMPOUND_EXPR
5381 && !IS_SUPER (rtype)));
5383 /* If we are calling [super dealloc], reset our warning flag. */
5384 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5385 should_call_super_dealloc = 0;
5387 /* If the receiver is a class object, retrieve the corresponding
5388 @interface, if one exists. class_tree is the class name
5389 identifier, or NULL_TREE if this is not a class method or the
5390 class name could not be determined (as in the case "Class c; [c
5391 method];"). */
5392 class_tree = receiver_is_class_object (receiver, self, super);
5394 /* Now determine the receiver type (if an explicit cast has not been
5395 provided). */
5396 if (!have_cast)
5398 if (class_tree)
5400 /* We are here when we have no cast, and we have a class
5401 name. So, this is a plain method to a class object, as
5402 in [NSObject alloc]. Find the interface corresponding to
5403 the class name. */
5404 rtype = lookup_interface (class_tree);
5406 if (rtype == NULL_TREE)
5408 /* If 'rtype' is NULL_TREE at this point it means that
5409 we have seen no @interface corresponding to that
5410 class name, only a @class declaration (alternatively,
5411 this was a call such as [objc_getClass("SomeClass")
5412 alloc], where we've never seen the @interface of
5413 SomeClass). So, we have a class name (class_tree)
5414 but no actual details of the class methods. We won't
5415 be able to check that the class responds to the
5416 method, and we will have to guess the method
5417 prototype. Emit a warning, then keep going (this
5418 will use any method with a matching name, as if the
5419 receiver was of type 'Class'). */
5420 warning (0, "@interface of class %qE not found", class_tree);
5423 /* Handle `self' and `super'. */
5424 else if (super)
5426 if (!CLASS_SUPER_NAME (implementation_template))
5428 error ("no super class declared in @interface for %qE",
5429 CLASS_NAME (implementation_template));
5430 return error_mark_node;
5432 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5434 else if (self)
5435 rtype = lookup_interface (CLASS_NAME (implementation_template));
5438 if (objc_is_id (rtype))
5440 /* The receiver is of type 'id' or 'Class' (with or without some
5441 protocols attached to it). */
5443 /* We set class_tree to the identifier for 'Class' if this is a
5444 class method, and to NULL_TREE if not. */
5445 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5447 /* 'rprotos' is the list of protocols that the receiver
5448 supports. */
5449 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5450 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5451 : NULL_TREE);
5453 /* We have no information on the type, and we set it to
5454 NULL_TREE. */
5455 rtype = NULL_TREE;
5457 /* If there are any protocols, check that the method we are
5458 calling appears in the protocol list. If there are no
5459 protocols, this is a message to 'id' or 'Class' and we accept
5460 any method that exists. */
5461 if (rprotos)
5463 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5464 search in protocols themselves for the method
5465 prototype. */
5466 method_prototype
5467 = lookup_method_in_protocol_list (rprotos, sel_name,
5468 class_tree != NULL_TREE);
5470 /* If messaging 'Class <Proto>' but did not find a class
5471 method prototype, search for an instance method instead,
5472 and warn about having done so. */
5473 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5475 method_prototype
5476 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5478 if (method_prototype)
5479 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5480 sel_name, sel_name);
5484 else if (rtype)
5486 /* We have a receiver type which is more specific than 'id' or
5487 'Class'. */
5488 tree orig_rtype = rtype;
5490 if (TREE_CODE (rtype) == POINTER_TYPE)
5491 rtype = TREE_TYPE (rtype);
5492 /* Traverse typedef aliases */
5493 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5494 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5495 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5496 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5497 if (TYPED_OBJECT (rtype))
5499 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5500 rtype = TYPE_OBJC_INTERFACE (rtype);
5502 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5504 /* If we could not find an @interface declaration, we must
5505 have only seen a @class declaration; so, we cannot say
5506 anything more intelligent about which methods the
5507 receiver will understand. Note that this only happens
5508 for instance methods; for class methods to a class where
5509 we have only seen a @class declaration,
5510 lookup_interface() above would have set rtype to
5511 NULL_TREE. */
5512 if (rprotos)
5514 /* We could not find an @interface declaration, yet, if
5515 there are protocols attached to the type, we can
5516 still look up the method in the protocols. Ie, we
5517 are in the following case:
5519 @class MyClass;
5520 MyClass<MyProtocol> *x;
5521 [x method];
5523 If 'MyProtocol' has the method 'method', we can check
5524 and retrieve the method prototype. */
5525 method_prototype
5526 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5528 /* At this point, if we have found the method_prototype,
5529 we are quite happy. The details of the class are
5530 irrelevant. If we haven't found it, a warning will
5531 have been produced that the method could not be found
5532 in the protocol, and we won't produce further
5533 warnings (please note that this means that "@class
5534 MyClass; MyClass <MyProtocol> *x;" is exactly
5535 equivalent to "id <MyProtocol> x", which isn't too
5536 satisfactory but it's not easy to see how to do
5537 better). */
5539 else
5541 if (rtype)
5543 /* We could not find an @interface declaration, and
5544 there are no protocols attached to the receiver,
5545 so we can't complete the check that the receiver
5546 responds to the method, and we can't retrieve the
5547 method prototype. But, because the receiver has
5548 a well-specified class, the programmer did want
5549 this check to be performed. Emit a warning, then
5550 keep going as if it was an 'id'. To remove the
5551 warning, either include an @interface for the
5552 class, or cast the receiver to 'id'. Note that
5553 rtype is an IDENTIFIER_NODE at this point. */
5554 warning (0, "@interface of class %qE not found", rtype);
5558 rtype = NULL_TREE;
5560 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5561 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5563 /* We have a valid ObjC class name with an associated
5564 @interface. Look up the method name in the published
5565 @interface for the class (and its superclasses). */
5566 method_prototype
5567 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5569 /* If the method was not found in the @interface, it may still
5570 exist locally as part of the @implementation. */
5571 if (!method_prototype && objc_implementation_context
5572 && CLASS_NAME (objc_implementation_context)
5573 == OBJC_TYPE_NAME (rtype))
5574 method_prototype
5575 = lookup_method
5576 ((class_tree
5577 ? CLASS_CLS_METHODS (objc_implementation_context)
5578 : CLASS_NST_METHODS (objc_implementation_context)),
5579 sel_name);
5581 /* If we haven't found a candidate method by now, try looking for
5582 it in the protocol list. */
5583 if (!method_prototype && rprotos)
5584 method_prototype
5585 = lookup_method_in_protocol_list (rprotos, sel_name,
5586 class_tree != NULL_TREE);
5588 else
5590 /* We have a type, but it's not an Objective-C type (!). */
5591 warning (0, "invalid receiver type %qs",
5592 identifier_to_locale (gen_type_name (orig_rtype)));
5593 /* After issuing the "invalid receiver" warning, perform method
5594 lookup as if we were messaging 'id'. */
5595 rtype = rprotos = NULL_TREE;
5598 /* Note that rtype could also be NULL_TREE. This happens if we are
5599 messaging a class by name, but the class was only
5600 forward-declared using @class. */
5602 /* For 'id' or 'Class' receivers, search in the global hash table as
5603 a last resort. For all receivers, warn if protocol searches have
5604 failed. */
5605 if (!method_prototype)
5607 if (rprotos)
5608 warning (0, "%<%c%E%> not found in protocol(s)",
5609 (class_tree ? '+' : '-'),
5610 sel_name);
5612 if (!rtype)
5613 method_prototype
5614 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5617 if (!method_prototype)
5619 static bool warn_missing_methods = false;
5621 if (rtype)
5622 warning (0, "%qE may not respond to %<%c%E%>",
5623 OBJC_TYPE_NAME (rtype),
5624 (class_tree ? '+' : '-'),
5625 sel_name);
5626 /* If we are messaging an 'id' or 'Class' object and made it here,
5627 then we have failed to find _any_ instance or class method,
5628 respectively. */
5629 else
5630 warning (0, "no %<%c%E%> method found",
5631 (class_tree ? '+' : '-'),
5632 sel_name);
5634 if (!warn_missing_methods)
5636 warning_at (input_location,
5637 0, "(Messages without a matching method signature");
5638 warning_at (input_location,
5639 0, "will be assumed to return %<id%> and accept");
5640 warning_at (input_location,
5641 0, "%<...%> as arguments.)");
5642 warn_missing_methods = true;
5645 else
5647 /* Warn if the method is deprecated, but not if the receiver is
5648 a generic 'id'. 'id' is used to cast an object to a generic
5649 object of an unspecified class; in that case, we'll use
5650 whatever method prototype we can find to get the method
5651 argument and return types, but it is not appropriate to
5652 produce deprecation warnings since we don't know the class
5653 that the object will be of at runtime. The @interface(s) for
5654 that class may not even be available to the compiler right
5655 now, and it is perfectly possible that the method is marked
5656 as non-deprecated in such @interface(s).
5658 In practice this makes sense since casting an object to 'id'
5659 is often used precisely to turn off warnings associated with
5660 the object being of a particular class. */
5661 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5663 if (deprecated_method_prototype)
5664 *deprecated_method_prototype = method_prototype;
5665 else
5666 warn_deprecated_use (method_prototype, NULL_TREE);
5670 /* Save the selector name for printing error messages. */
5671 current_objc_message_selector = sel_name;
5673 /* Build the method call.
5674 TODO: Get the location from somewhere that will work for delayed
5675 expansion. */
5677 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5678 receiver, rtype, sel_name,
5679 method_params, super);
5681 current_objc_message_selector = 0;
5683 return retval;
5687 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5688 expression. This variable will be initialized to global protocol_t meta-data
5689 pointer. */
5691 /* This function is called by the parser when (and only when) a
5692 @protocol() expression is found, in order to compile it. */
5693 tree
5694 objc_build_protocol_expr (tree protoname)
5696 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5697 /* definition_required */ false);
5699 if (!p)
5701 error ("cannot find protocol declaration for %qE", protoname);
5702 return error_mark_node;
5705 return (*runtime.get_protocol_reference) (input_location, p);
5708 /* This function is called by the parser when a @selector() expression
5709 is found, in order to compile it. It is only called by the parser
5710 and only to compile a @selector(). LOC is the location of the
5711 @selector. */
5712 tree
5713 objc_build_selector_expr (location_t loc, tree selnamelist)
5715 tree selname;
5717 /* Obtain the full selector name. */
5718 switch (TREE_CODE (selnamelist))
5720 case IDENTIFIER_NODE:
5721 /* A unary selector. */
5722 selname = selnamelist;
5723 break;
5724 case TREE_LIST:
5725 selname = build_keyword_selector (selnamelist);
5726 break;
5727 default:
5728 gcc_unreachable ();
5731 /* If we are required to check @selector() expressions as they
5732 are found, check that the selector has been declared. */
5733 if (warn_undeclared_selector)
5735 /* Look the selector up in the list of all known class and
5736 instance methods (up to this line) to check that the selector
5737 exists. */
5738 tree method;
5740 /* First try with instance methods. */
5741 method = objc_map_get (instance_method_map, selname);
5743 /* If not found, try with class methods. */
5744 if (method == OBJC_MAP_NOT_FOUND)
5746 method = objc_map_get (class_method_map, selname);
5748 /* If still not found, print out a warning. */
5749 if (method == OBJC_MAP_NOT_FOUND)
5750 warning (0, "undeclared selector %qE", selname);
5754 /* The runtimes do this differently, most particularly, GNU has typed
5755 selectors, whilst NeXT does not. */
5756 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5759 static tree
5760 build_ivar_reference (tree id)
5762 tree base;
5763 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5765 /* Historically, a class method that produced objects (factory
5766 method) would assign `self' to the instance that it
5767 allocated. This would effectively turn the class method into
5768 an instance method. Following this assignment, the instance
5769 variables could be accessed. That practice, while safe,
5770 violates the simple rule that a class method should not refer
5771 to an instance variable. It's better to catch the cases
5772 where this is done unknowingly than to support the above
5773 paradigm. */
5774 warning (0, "instance variable %qE accessed in class method",
5775 id);
5776 self_decl = convert (objc_instance_type, self_decl); /* cast */
5779 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5780 return (*runtime.build_ivar_reference) (input_location, base, id);
5783 static void
5784 hash_init (void)
5786 instance_method_map = objc_map_alloc_ggc (1000);
5787 class_method_map = objc_map_alloc_ggc (1000);
5789 class_name_map = objc_map_alloc_ggc (200);
5790 alias_name_map = objc_map_alloc_ggc (200);
5792 /* Initialize the hash table used to hold the constant string objects. */
5793 string_htab = hash_table<objc_string_hasher>::create_ggc (31);
5796 /* Use the following to add a method to class_method_map or
5797 instance_method_map. It will add the method, keyed by the
5798 METHOD_SEL_NAME. If the method already exists, but with one or
5799 more different prototypes, it will store a TREE_VEC in the map,
5800 with the method prototypes in the vector. */
5801 static void
5802 insert_method_into_method_map (bool class_method, tree method)
5804 tree method_name = METHOD_SEL_NAME (method);
5805 tree existing_entry;
5806 objc_map_t map;
5808 if (class_method)
5809 map = class_method_map;
5810 else
5811 map = instance_method_map;
5813 /* Check if the method already exists in the map. */
5814 existing_entry = objc_map_get (map, method_name);
5816 /* If not, we simply add it to the map. */
5817 if (existing_entry == OBJC_MAP_NOT_FOUND)
5818 objc_map_put (map, method_name, method);
5819 else
5821 tree new_entry;
5823 /* If an entry already exists, it's more complicated. We'll
5824 have to check whether the method prototype is the same or
5825 not. */
5826 if (TREE_CODE (existing_entry) != TREE_VEC)
5828 /* If the method prototypes are the same, there is nothing
5829 to do. */
5830 if (comp_proto_with_proto (method, existing_entry, 1))
5831 return;
5833 /* If not, create a vector to store both the method already
5834 in the map, and the new one that we are adding. */
5835 new_entry = make_tree_vec (2);
5837 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5838 TREE_VEC_ELT (new_entry, 1) = method;
5840 else
5842 /* An entry already exists, and it's already a vector. This
5843 means that at least 2 different method prototypes were
5844 already found, and we're considering registering yet
5845 another one. */
5846 size_t i;
5848 /* Check all the existing prototypes. If any matches the
5849 one we need to add, there is nothing to do because it's
5850 already there. */
5851 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5852 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5853 return;
5855 /* Else, create a new, bigger vector and add the new method
5856 at the end of it. This is inefficient but extremely
5857 rare; in any sane program most methods have a single
5858 prototype, and very few, if any, will have more than
5859 2! */
5860 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5862 /* Copy the methods from the existing vector. */
5863 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5864 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5866 /* Add the new method at the end. */
5867 TREE_VEC_ELT (new_entry, i) = method;
5870 /* Store the new vector in the map. */
5871 objc_map_put (map, method_name, new_entry);
5876 static tree
5877 lookup_method (tree mchain, tree method)
5879 tree key;
5881 if (TREE_CODE (method) == IDENTIFIER_NODE)
5882 key = method;
5883 else
5884 key = METHOD_SEL_NAME (method);
5886 while (mchain)
5888 if (METHOD_SEL_NAME (mchain) == key)
5889 return mchain;
5891 mchain = DECL_CHAIN (mchain);
5893 return NULL_TREE;
5896 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5897 method in INTERFACE, along with any categories and protocols
5898 attached thereto. If method is not found, and the
5899 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5900 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5901 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5902 be found in INTERFACE or any of its superclasses, look for an
5903 _instance_ method of the same name in the root class as a last
5904 resort. This behaviour can be turned off by using
5905 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5907 If a suitable method cannot be found, return NULL_TREE. */
5909 static tree
5910 lookup_method_static (tree interface, tree ident, int flags)
5912 tree meth = NULL_TREE, root_inter = NULL_TREE;
5913 tree inter = interface;
5914 int is_class = (flags & OBJC_LOOKUP_CLASS);
5915 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5916 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
5918 while (inter)
5920 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5921 tree category = inter;
5923 /* First, look up the method in the class itself. */
5924 if ((meth = lookup_method (chain, ident)))
5925 return meth;
5927 /* Failing that, look for the method in each category of the class. */
5928 while ((category = CLASS_CATEGORY_LIST (category)))
5930 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
5932 /* Check directly in each category. */
5933 if ((meth = lookup_method (chain, ident)))
5934 return meth;
5936 /* Failing that, check in each category's protocols. */
5937 if (CLASS_PROTOCOL_LIST (category))
5939 if ((meth = (lookup_method_in_protocol_list
5940 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5941 return meth;
5945 /* If not found in categories, check in protocols of the main class. */
5946 if (CLASS_PROTOCOL_LIST (inter))
5948 if ((meth = (lookup_method_in_protocol_list
5949 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5950 return meth;
5953 /* If we were instructed not to look in superclasses, don't. */
5954 if (no_superclasses)
5955 return NULL_TREE;
5957 /* Failing that, climb up the inheritance hierarchy. */
5958 root_inter = inter;
5959 inter = lookup_interface (CLASS_SUPER_NAME (inter));
5961 while (inter);
5963 if (is_class && !no_instance_methods_of_root_class)
5965 /* If no class (factory) method was found, check if an _instance_
5966 method of the same name exists in the root class. This is what
5967 the Objective-C runtime will do. */
5968 return lookup_method_static (root_inter, ident, 0);
5970 else
5972 /* If an instance method was not found, return 0. */
5973 return NULL_TREE;
5977 static tree
5978 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
5980 tree existing_method = NULL_TREE;
5982 /* The first thing we do is look up the method in the list of
5983 methods already defined in the interface (or implementation). */
5984 if (is_class)
5985 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5986 else
5987 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
5989 /* In the case of protocols, we have a second list of methods to
5990 consider, the list of optional ones. */
5991 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
5993 /* @required methods are added to the protocol's normal list.
5994 @optional methods are added to the protocol's OPTIONAL lists.
5995 Note that adding the methods to the optional lists disables
5996 checking that the methods are implemented by classes
5997 implementing the protocol, since these checks only use the
5998 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
6000 /* First of all, if the method to add is @optional, and we found
6001 it already existing as @required, emit an error. */
6002 if (is_optional && existing_method)
6004 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6005 (is_class ? '+' : '-'),
6006 METHOD_SEL_NAME (existing_method));
6007 inform (DECL_SOURCE_LOCATION (existing_method),
6008 "previous declaration of %<%c%E%> as %<@required%>",
6009 (is_class ? '+' : '-'),
6010 METHOD_SEL_NAME (existing_method));
6013 /* Now check the list of @optional methods if we didn't find the
6014 method in the @required list. */
6015 if (!existing_method)
6017 if (is_class)
6018 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6019 else
6020 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
6022 if (!is_optional && existing_method)
6024 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6025 (is_class ? '+' : '-'),
6026 METHOD_SEL_NAME (existing_method));
6027 inform (DECL_SOURCE_LOCATION (existing_method),
6028 "previous declaration of %<%c%E%> as %<@optional%>",
6029 (is_class ? '+' : '-'),
6030 METHOD_SEL_NAME (existing_method));
6035 /* If the method didn't exist already, add it. */
6036 if (!existing_method)
6038 if (is_optional)
6040 if (is_class)
6042 /* Put the method on the list in reverse order. */
6043 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6044 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6046 else
6048 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6049 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6052 else
6054 if (is_class)
6056 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6057 CLASS_CLS_METHODS (klass) = method;
6059 else
6061 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6062 CLASS_NST_METHODS (klass) = method;
6066 else
6068 /* The method was already defined. Check that the types match
6069 for an @interface for a class or category, or for a
6070 @protocol. Give hard errors on methods with identical
6071 selectors but differing argument and/or return types. We do
6072 not do this for @implementations, because C/C++ will do it
6073 for us (i.e., there will be duplicate function definition
6074 errors). */
6075 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6076 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6077 /* Starting with GCC 4.6, we emit the same error for
6078 protocols too. The situation is identical to
6079 @interfaces as there is no possible meaningful reason
6080 for defining the same method with different signatures
6081 in the very same @protocol. If that was allowed,
6082 whenever the protocol is used (both at compile and run
6083 time) there wouldn't be any meaningful way to decide
6084 which of the two method signatures should be used. */
6085 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6086 && !comp_proto_with_proto (method, existing_method, 1))
6088 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6089 (is_class ? '+' : '-'),
6090 METHOD_SEL_NAME (existing_method));
6091 inform (DECL_SOURCE_LOCATION (existing_method),
6092 "previous declaration of %<%c%E%>",
6093 (is_class ? '+' : '-'),
6094 METHOD_SEL_NAME (existing_method));
6098 if (is_class)
6099 insert_method_into_method_map (true, method);
6100 else
6102 insert_method_into_method_map (false, method);
6104 /* Instance methods in root classes (and categories thereof)
6105 may act as class methods as a last resort. We also add
6106 instance methods listed in @protocol declarations to
6107 the class hash table, on the assumption that @protocols
6108 may be adopted by root classes or categories. */
6109 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6110 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6111 klass = lookup_interface (CLASS_NAME (klass));
6113 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6114 || !CLASS_SUPER_NAME (klass))
6115 insert_method_into_method_map (true, method);
6118 return method;
6121 static void
6122 add_category (tree klass, tree category)
6124 /* Put categories on list in reverse order. */
6125 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6127 if (cat)
6129 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6130 CLASS_NAME (klass),
6131 CLASS_SUPER_NAME (category));
6133 else
6135 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6136 CLASS_CATEGORY_LIST (klass) = category;
6140 #ifndef OBJCPLUS
6141 /* A flexible array member is a C99 extension where you can use
6142 "type[]" at the end of a struct to mean a variable-length array.
6144 In Objective-C, instance variables are fundamentally members of a
6145 struct, but the struct can always be extended by subclassing; hence
6146 we need to detect and forbid all instance variables declared using
6147 flexible array members.
6149 No check for this is needed in Objective-C++, since C++ does not
6150 have flexible array members. */
6152 /* Determine whether TYPE is a structure with a flexible array member,
6153 a union containing such a structure (possibly recursively) or an
6154 array of such structures or unions. These are all invalid as
6155 instance variable. */
6156 static bool
6157 flexible_array_type_p (tree type)
6159 tree x;
6160 switch (TREE_CODE (type))
6162 case RECORD_TYPE:
6163 x = TYPE_FIELDS (type);
6164 if (x == NULL_TREE)
6165 return false;
6166 while (DECL_CHAIN (x) != NULL_TREE)
6167 x = DECL_CHAIN (x);
6168 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6169 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6170 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6171 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6172 return true;
6173 return false;
6174 case UNION_TYPE:
6175 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6177 if (flexible_array_type_p (TREE_TYPE (x)))
6178 return true;
6180 return false;
6181 /* Note that we also check for arrays of something that uses a flexible array member. */
6182 case ARRAY_TYPE:
6183 if (flexible_array_type_p (TREE_TYPE (type)))
6184 return true;
6185 return false;
6186 default:
6187 return false;
6190 #endif
6192 /* Produce a printable version of an ivar name. This is only used
6193 inside add_instance_variable. */
6194 static const char *
6195 printable_ivar_name (tree field_decl)
6197 if (DECL_NAME (field_decl))
6198 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6199 else
6200 return _("<unnamed>");
6203 /* Called after parsing each instance variable declaration. Necessary to
6204 preserve typedefs and implement public/private...
6206 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6208 static tree
6209 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6210 tree field_decl)
6212 tree field_type = TREE_TYPE (field_decl);
6214 #ifdef OBJCPLUS
6215 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6217 error ("illegal reference type specified for instance variable %qs",
6218 printable_ivar_name (field_decl));
6219 /* Return class as is without adding this ivar. */
6220 return klass;
6222 #endif
6224 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6225 || TYPE_SIZE (field_type) == error_mark_node)
6226 /* 'type[0]' is allowed, but 'type[]' is not! */
6228 error ("instance variable %qs has unknown size",
6229 printable_ivar_name (field_decl));
6230 /* Return class as is without adding this ivar. */
6231 return klass;
6234 #ifndef OBJCPLUS
6235 /* Also, in C reject a struct with a flexible array member. Ie,
6237 struct A { int x; int[] y; };
6239 @interface X
6241 struct A instance_variable;
6243 @end
6245 is not valid because if the class is subclassed, we wouldn't be able
6246 to calculate the offset of the next instance variable. */
6247 if (flexible_array_type_p (field_type))
6249 error ("instance variable %qs uses flexible array member",
6250 printable_ivar_name (field_decl));
6251 /* Return class as is without adding this ivar. */
6252 return klass;
6254 #endif
6256 #ifdef OBJCPLUS
6257 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6258 need to either (1) warn the user about it or (2) generate suitable
6259 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6260 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6261 if (MAYBE_CLASS_TYPE_P (field_type)
6262 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6263 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6264 || TYPE_POLYMORPHIC_P (field_type)))
6266 tree type_name = OBJC_TYPE_NAME (field_type);
6268 if (flag_objc_call_cxx_cdtors)
6270 /* Since the ObjC runtime will be calling the constructors and
6271 destructors for us, the only thing we can't handle is the lack
6272 of a default constructor. */
6273 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6274 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6276 warning (0, "type %qE has no default constructor to call",
6277 type_name);
6279 /* If we cannot call a constructor, we should also avoid
6280 calling the destructor, for symmetry. */
6281 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6282 warning (0, "destructor for %qE shall not be run either",
6283 type_name);
6286 else
6288 static bool warn_cxx_ivars = false;
6290 if (TYPE_POLYMORPHIC_P (field_type))
6292 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6293 initialize them. */
6294 error ("type %qE has virtual member functions", type_name);
6295 error ("illegal aggregate type %qE specified "
6296 "for instance variable %qs",
6297 type_name, printable_ivar_name (field_decl));
6298 /* Return class as is without adding this ivar. */
6299 return klass;
6302 /* User-defined constructors and destructors are not known to Obj-C
6303 and hence will not be called. This may or may not be a problem. */
6304 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6305 warning (0, "type %qE has a user-defined constructor", type_name);
6306 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6307 warning (0, "type %qE has a user-defined destructor", type_name);
6309 if (!warn_cxx_ivars)
6311 warning (0, "C++ constructors and destructors will not "
6312 "be invoked for Objective-C fields");
6313 warn_cxx_ivars = true;
6317 #endif
6319 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6320 switch (visibility)
6322 case OBJC_IVAR_VIS_PROTECTED:
6323 TREE_PUBLIC (field_decl) = 0;
6324 TREE_PRIVATE (field_decl) = 0;
6325 TREE_PROTECTED (field_decl) = 1;
6326 break;
6328 case OBJC_IVAR_VIS_PACKAGE:
6329 /* TODO: Implement the package variant. */
6330 case OBJC_IVAR_VIS_PUBLIC:
6331 TREE_PUBLIC (field_decl) = 1;
6332 TREE_PRIVATE (field_decl) = 0;
6333 TREE_PROTECTED (field_decl) = 0;
6334 break;
6336 case OBJC_IVAR_VIS_PRIVATE:
6337 TREE_PUBLIC (field_decl) = 0;
6338 TREE_PRIVATE (field_decl) = 1;
6339 TREE_PROTECTED (field_decl) = 0;
6340 break;
6344 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6346 return klass;
6349 /* True if the ivar is private and we are not in its implementation. */
6351 static int
6352 is_private (tree decl)
6354 return (TREE_PRIVATE (decl)
6355 && ! is_ivar (CLASS_IVARS (implementation_template),
6356 DECL_NAME (decl)));
6359 /* Searches all the instance variables of 'klass' and of its
6360 superclasses for an instance variable whose name (identifier) is
6361 'ivar_name_ident'. Return the declaration (DECL) of the instance
6362 variable, if found, or NULL_TREE, if not found. */
6363 static inline tree
6364 ivar_of_class (tree klass, tree ivar_name_ident)
6366 /* First, look up the ivar in CLASS_RAW_IVARS. */
6367 tree decl_chain = CLASS_RAW_IVARS (klass);
6369 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6370 if (DECL_NAME (decl_chain) == ivar_name_ident)
6371 return decl_chain;
6373 /* If not found, search up the class hierarchy. */
6374 while (CLASS_SUPER_NAME (klass))
6376 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6378 decl_chain = CLASS_RAW_IVARS (klass);
6380 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6381 if (DECL_NAME (decl_chain) == ivar_name_ident)
6382 return decl_chain;
6385 return NULL_TREE;
6388 /* We have an instance variable reference;, check to see if it is public. */
6391 objc_is_public (tree expr, tree identifier)
6393 tree basetype, decl;
6395 #ifdef OBJCPLUS
6396 if (processing_template_decl)
6397 return 1;
6398 #endif
6400 if (TREE_TYPE (expr) == error_mark_node)
6401 return 1;
6403 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6405 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6407 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6409 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6411 if (!klass)
6413 error ("cannot find interface declaration for %qE",
6414 OBJC_TYPE_NAME (basetype));
6415 return 0;
6418 if ((decl = ivar_of_class (klass, identifier)))
6420 if (TREE_PUBLIC (decl))
6421 return 1;
6423 /* Important difference between the Stepstone translator:
6424 all instance variables should be public within the context
6425 of the implementation. */
6426 if (objc_implementation_context
6427 && ((TREE_CODE (objc_implementation_context)
6428 == CLASS_IMPLEMENTATION_TYPE)
6429 || (TREE_CODE (objc_implementation_context)
6430 == CATEGORY_IMPLEMENTATION_TYPE)))
6432 tree curtype = TYPE_MAIN_VARIANT
6433 (CLASS_STATIC_TEMPLATE
6434 (implementation_template));
6436 if (basetype == curtype
6437 || DERIVED_FROM_P (basetype, curtype))
6439 int priv = is_private (decl);
6441 if (priv)
6442 error ("instance variable %qE is declared private",
6443 DECL_NAME (decl));
6445 return !priv;
6449 /* The 2.95.2 compiler sometimes allowed C functions to access
6450 non-@public ivars. We will let this slide for now... */
6451 if (!objc_method_context)
6453 warning (0, "instance variable %qE is %s; "
6454 "this will be a hard error in the future",
6455 identifier,
6456 TREE_PRIVATE (decl) ? "@private" : "@protected");
6457 return 1;
6460 error ("instance variable %qE is declared %s",
6461 identifier,
6462 TREE_PRIVATE (decl) ? "private" : "protected");
6463 return 0;
6468 return 1;
6471 /* Make sure all methods in CHAIN (a list of method declarations from
6472 an @interface or a @protocol) are in IMPLEMENTATION (the
6473 implementation context). This is used to check for example that
6474 all methods declared in an @interface were implemented in an
6475 @implementation.
6477 Some special methods (property setters/getters) are special and if
6478 they are not found in IMPLEMENTATION, we look them up in its
6479 superclasses. */
6481 static int
6482 check_methods (tree chain, tree implementation, int mtype)
6484 int first = 1;
6485 tree list;
6487 if (mtype == (int)'+')
6488 list = CLASS_CLS_METHODS (implementation);
6489 else
6490 list = CLASS_NST_METHODS (implementation);
6492 while (chain)
6494 /* If the method is associated with a dynamic property, then it
6495 is Ok not to have the method implementation, as it will be
6496 generated dynamically at runtime. To decide if the method is
6497 associated with a @dynamic property, we search the list of
6498 @synthesize and @dynamic for this implementation, and look
6499 for any @dynamic property with the same setter or getter name
6500 as this method. */
6501 tree x;
6502 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6503 if (PROPERTY_DYNAMIC (x)
6504 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6505 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6506 break;
6508 if (x != NULL_TREE)
6510 chain = TREE_CHAIN (chain); /* next method... */
6511 continue;
6514 if (!lookup_method (list, chain))
6516 /* If the method is a property setter/getter, we'll still
6517 allow it to be missing if it is implemented by
6518 'interface' or any of its superclasses. */
6519 tree property = METHOD_PROPERTY_CONTEXT (chain);
6520 if (property)
6522 /* Note that since this is a property getter/setter, it
6523 is obviously an instance method. */
6524 tree interface = NULL_TREE;
6526 /* For a category, first check the main class
6527 @interface. */
6528 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6530 interface = lookup_interface (CLASS_NAME (implementation));
6532 /* If the method is found in the main class, it's Ok. */
6533 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6535 chain = DECL_CHAIN (chain);
6536 continue;
6539 /* Else, get the superclass. */
6540 if (CLASS_SUPER_NAME (interface))
6541 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6542 else
6543 interface = NULL_TREE;
6546 /* Get the superclass for classes. */
6547 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6549 if (CLASS_SUPER_NAME (implementation))
6550 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6551 else
6552 interface = NULL_TREE;
6555 /* Now, interface is the superclass, if any; go check it. */
6556 if (interface)
6558 if (lookup_method_static (interface, chain, 0))
6560 chain = DECL_CHAIN (chain);
6561 continue;
6564 /* Else, fall through - warn. */
6566 if (first)
6568 switch (TREE_CODE (implementation))
6570 case CLASS_IMPLEMENTATION_TYPE:
6571 warning (0, "incomplete implementation of class %qE",
6572 CLASS_NAME (implementation));
6573 break;
6574 case CATEGORY_IMPLEMENTATION_TYPE:
6575 warning (0, "incomplete implementation of category %qE",
6576 CLASS_SUPER_NAME (implementation));
6577 break;
6578 default:
6579 gcc_unreachable ();
6581 first = 0;
6584 warning (0, "method definition for %<%c%E%> not found",
6585 mtype, METHOD_SEL_NAME (chain));
6588 chain = DECL_CHAIN (chain);
6591 return first;
6594 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6596 static int
6597 conforms_to_protocol (tree klass, tree protocol)
6599 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6601 tree p = CLASS_PROTOCOL_LIST (klass);
6602 while (p && TREE_VALUE (p) != protocol)
6603 p = TREE_CHAIN (p);
6605 if (!p)
6607 tree super = (CLASS_SUPER_NAME (klass)
6608 ? lookup_interface (CLASS_SUPER_NAME (klass))
6609 : NULL_TREE);
6610 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6611 if (!tmp)
6612 return 0;
6616 return 1;
6619 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6620 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6622 static int
6623 check_methods_accessible (tree chain, tree context, int mtype)
6625 int first = 1;
6626 tree list;
6627 tree base_context = context;
6629 while (chain)
6631 /* If the method is associated with a dynamic property, then it
6632 is Ok not to have the method implementation, as it will be
6633 generated dynamically at runtime. Search for any @dynamic
6634 property with the same setter or getter name as this
6635 method. TODO: Use a hashtable lookup. */
6636 tree x;
6637 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6638 if (PROPERTY_DYNAMIC (x)
6639 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6640 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6641 break;
6643 if (x != NULL_TREE)
6645 chain = TREE_CHAIN (chain); /* next method... */
6646 continue;
6649 context = base_context;
6650 while (context)
6652 if (mtype == '+')
6653 list = CLASS_CLS_METHODS (context);
6654 else
6655 list = CLASS_NST_METHODS (context);
6657 if (lookup_method (list, chain))
6658 break;
6660 switch (TREE_CODE (context))
6662 case CLASS_IMPLEMENTATION_TYPE:
6663 case CLASS_INTERFACE_TYPE:
6664 context = (CLASS_SUPER_NAME (context)
6665 ? lookup_interface (CLASS_SUPER_NAME (context))
6666 : NULL_TREE);
6667 break;
6668 case CATEGORY_IMPLEMENTATION_TYPE:
6669 case CATEGORY_INTERFACE_TYPE:
6670 context = (CLASS_NAME (context)
6671 ? lookup_interface (CLASS_NAME (context))
6672 : NULL_TREE);
6673 break;
6674 default:
6675 gcc_unreachable ();
6679 if (context == NULL_TREE)
6681 if (first)
6683 switch (TREE_CODE (objc_implementation_context))
6685 case CLASS_IMPLEMENTATION_TYPE:
6686 warning (0, "incomplete implementation of class %qE",
6687 CLASS_NAME (objc_implementation_context));
6688 break;
6689 case CATEGORY_IMPLEMENTATION_TYPE:
6690 warning (0, "incomplete implementation of category %qE",
6691 CLASS_SUPER_NAME (objc_implementation_context));
6692 break;
6693 default:
6694 gcc_unreachable ();
6696 first = 0;
6698 warning (0, "method definition for %<%c%E%> not found",
6699 mtype, METHOD_SEL_NAME (chain));
6702 chain = TREE_CHAIN (chain); /* next method... */
6704 return first;
6707 /* Check whether the current interface (accessible via
6708 'objc_implementation_context') actually implements protocol P, along
6709 with any protocols that P inherits. */
6711 static void
6712 check_protocol (tree p, const char *type, tree name)
6714 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6716 int f1, f2;
6718 /* Ensure that all protocols have bodies! */
6719 if (warn_protocol)
6721 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6722 objc_implementation_context,
6723 '+');
6724 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6725 objc_implementation_context,
6726 '-');
6728 else
6730 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6731 objc_implementation_context,
6732 '+');
6733 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6734 objc_implementation_context,
6735 '-');
6738 if (!f1 || !f2)
6739 warning (0, "%s %qE does not fully implement the %qE protocol",
6740 type, name, PROTOCOL_NAME (p));
6743 /* Check protocols recursively. */
6744 if (PROTOCOL_LIST (p))
6746 tree subs = PROTOCOL_LIST (p);
6747 tree super_class =
6748 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6750 while (subs)
6752 tree sub = TREE_VALUE (subs);
6754 /* If the superclass does not conform to the protocols
6755 inherited by P, then we must! */
6756 if (!super_class || !conforms_to_protocol (super_class, sub))
6757 check_protocol (sub, type, name);
6758 subs = TREE_CHAIN (subs);
6763 /* Check whether the current interface (accessible via
6764 'objc_implementation_context') actually implements the protocols listed
6765 in PROTO_LIST. */
6767 static void
6768 check_protocols (tree proto_list, const char *type, tree name)
6770 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6772 tree p = TREE_VALUE (proto_list);
6774 check_protocol (p, type, name);
6778 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6779 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6780 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6781 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6782 SUPER_NAME is the name of the category. For a class extension,
6783 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6784 static tree
6785 start_class (enum tree_code code, tree class_name, tree super_name,
6786 tree protocol_list, tree attributes)
6788 tree klass = NULL_TREE;
6789 tree decl;
6791 #ifdef OBJCPLUS
6792 if (current_namespace != global_namespace)
6794 error ("Objective-C declarations may only appear in global scope");
6796 #endif /* OBJCPLUS */
6798 if (objc_implementation_context)
6800 warning (0, "%<@end%> missing in implementation context");
6801 finish_class (objc_implementation_context);
6802 objc_ivar_chain = NULL_TREE;
6803 objc_implementation_context = NULL_TREE;
6806 /* If this is a class extension, we'll be "reopening" the existing
6807 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6808 a new node. */
6809 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6811 klass = make_node (code);
6812 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6815 /* Check for existence of the super class, if one was specified. Note
6816 that we must have seen an @interface, not just a @class. If we
6817 are looking at a @compatibility_alias, traverse it first. */
6818 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6819 && super_name)
6821 tree super = objc_is_class_name (super_name);
6822 tree super_interface = NULL_TREE;
6824 if (super)
6825 super_interface = lookup_interface (super);
6827 if (!super_interface)
6829 error ("cannot find interface declaration for %qE, superclass of %qE",
6830 super ? super : super_name,
6831 class_name);
6832 super_name = NULL_TREE;
6834 else
6836 if (TREE_DEPRECATED (super_interface))
6837 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6838 super);
6839 super_name = super;
6843 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6845 CLASS_NAME (klass) = class_name;
6846 CLASS_SUPER_NAME (klass) = super_name;
6847 CLASS_CLS_METHODS (klass) = NULL_TREE;
6850 if (! objc_is_class_name (class_name)
6851 && (decl = lookup_name (class_name)))
6853 error ("%qE redeclared as different kind of symbol",
6854 class_name);
6855 error ("previous declaration of %q+D",
6856 decl);
6859 switch (code)
6861 case CLASS_IMPLEMENTATION_TYPE:
6863 tree chain;
6865 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6866 if (TREE_VALUE (chain) == class_name)
6868 error ("reimplementation of class %qE",
6869 class_name);
6870 /* TODO: error message saying where it was previously
6871 implemented. */
6872 break;
6874 if (chain == NULL_TREE)
6875 implemented_classes = tree_cons (NULL_TREE, class_name,
6876 implemented_classes);
6879 /* Reset for multiple classes per file. */
6880 method_slot = 0;
6882 objc_implementation_context = klass;
6884 /* Lookup the interface for this implementation. */
6886 if (!(implementation_template = lookup_interface (class_name)))
6888 warning (0, "cannot find interface declaration for %qE",
6889 class_name);
6890 add_interface (implementation_template = objc_implementation_context,
6891 class_name);
6894 /* If a super class has been specified in the implementation,
6895 insure it conforms to the one specified in the interface. */
6897 if (super_name
6898 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6900 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6901 error ("conflicting super class name %qE",
6902 super_name);
6903 if (previous_name)
6904 error ("previous declaration of %qE", previous_name);
6905 else
6906 error ("previous declaration");
6909 else if (! super_name)
6911 CLASS_SUPER_NAME (objc_implementation_context)
6912 = CLASS_SUPER_NAME (implementation_template);
6914 break;
6916 case CLASS_INTERFACE_TYPE:
6917 if (lookup_interface (class_name))
6918 #ifdef OBJCPLUS
6919 error ("duplicate interface declaration for class %qE", class_name);
6920 #else
6921 warning (0, "duplicate interface declaration for class %qE", class_name);
6922 #endif
6923 else
6924 add_interface (klass, class_name);
6926 if (protocol_list)
6927 CLASS_PROTOCOL_LIST (klass)
6928 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
6930 if (attributes)
6932 tree attribute;
6933 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6935 tree name = TREE_PURPOSE (attribute);
6937 /* TODO: Document what the objc_exception attribute is/does. */
6938 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6939 attributes. */
6940 if (is_attribute_p ("deprecated", name))
6941 TREE_DEPRECATED (klass) = 1;
6942 else if (is_attribute_p ("objc_exception", name))
6943 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6944 else
6945 /* Warn about and ignore all others for now, but store them. */
6946 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6948 TYPE_ATTRIBUTES (klass) = attributes;
6950 break;
6952 case CATEGORY_INTERFACE_TYPE:
6954 tree class_category_is_assoc_with;
6956 /* For a category, class_name is really the name of the class that
6957 the following set of methods will be associated with. We must
6958 find the interface so that can derive the objects template. */
6959 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6961 error ("cannot find interface declaration for %qE",
6962 class_name);
6963 exit (FATAL_EXIT_CODE);
6965 else
6967 if (TREE_DEPRECATED (class_category_is_assoc_with))
6968 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6969 class_name);
6971 if (super_name == NULL_TREE)
6973 /* This is a class extension. Get the original
6974 interface, and continue working on it. */
6975 objc_in_class_extension = true;
6976 klass = class_category_is_assoc_with;
6978 if (protocol_list)
6980 /* Append protocols to the original protocol
6981 list. */
6982 CLASS_PROTOCOL_LIST (klass)
6983 = chainon (CLASS_PROTOCOL_LIST (klass),
6984 lookup_and_install_protocols
6985 (protocol_list,
6986 /* definition_required */ true));
6989 else
6991 add_category (class_category_is_assoc_with, klass);
6993 if (protocol_list)
6994 CLASS_PROTOCOL_LIST (klass)
6995 = lookup_and_install_protocols
6996 (protocol_list, /* definition_required */ true);
7000 break;
7002 case CATEGORY_IMPLEMENTATION_TYPE:
7003 /* Reset for multiple classes per file. */
7004 method_slot = 0;
7006 objc_implementation_context = klass;
7008 /* For a category, class_name is really the name of the class that
7009 the following set of methods will be associated with. We must
7010 find the interface so that can derive the objects template. */
7012 if (!(implementation_template = lookup_interface (class_name)))
7014 error ("cannot find interface declaration for %qE",
7015 class_name);
7016 exit (FATAL_EXIT_CODE);
7018 break;
7019 default:
7020 gcc_unreachable ();
7022 return klass;
7025 static tree
7026 continue_class (tree klass)
7028 switch (TREE_CODE (klass))
7030 case CLASS_IMPLEMENTATION_TYPE:
7031 case CATEGORY_IMPLEMENTATION_TYPE:
7033 struct imp_entry *imp_entry;
7035 /* Check consistency of the instance variables. */
7037 if (CLASS_RAW_IVARS (klass))
7038 check_ivars (implementation_template, klass);
7040 /* code generation */
7041 #ifdef OBJCPLUS
7042 push_lang_context (lang_name_c);
7043 #endif
7044 build_private_template (implementation_template);
7045 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7046 objc_instance_type = build_pointer_type (uprivate_record);
7048 imp_entry = ggc_alloc<struct imp_entry> ();
7050 imp_entry->next = imp_list;
7051 imp_entry->imp_context = klass;
7052 imp_entry->imp_template = implementation_template;
7053 ucls_super_ref = uucls_super_ref = NULL;
7054 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7056 imp_entry->class_decl = (*runtime.class_decl) (klass);
7057 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7059 else
7061 imp_entry->class_decl = (*runtime.category_decl) (klass);
7062 imp_entry->meta_decl = NULL;
7064 imp_entry->has_cxx_cdtors = 0;
7066 /* Append to front and increment count. */
7067 imp_list = imp_entry;
7068 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7069 imp_count++;
7070 else
7071 cat_count++;
7072 #ifdef OBJCPLUS
7073 pop_lang_context ();
7074 #endif /* OBJCPLUS */
7076 return get_class_ivars (implementation_template, true);
7077 break;
7079 case CLASS_INTERFACE_TYPE:
7081 if (objc_in_class_extension)
7082 return NULL_TREE;
7083 #ifdef OBJCPLUS
7084 push_lang_context (lang_name_c);
7085 #endif /* OBJCPLUS */
7086 objc_collecting_ivars = 1;
7087 build_private_template (klass);
7088 objc_collecting_ivars = 0;
7089 #ifdef OBJCPLUS
7090 pop_lang_context ();
7091 #endif /* OBJCPLUS */
7092 return NULL_TREE;
7093 break;
7095 default:
7096 return error_mark_node;
7100 /* This routine builds name of the setter synthesized function. */
7101 char *
7102 objc_build_property_setter_name (tree ident)
7104 /* TODO: Use alloca to allocate buffer of appropriate size. */
7105 static char string[BUFSIZE];
7106 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7107 string[3] = TOUPPER (string[3]);
7108 return string;
7111 /* This routine prepares the declarations of the property accessor
7112 helper functions (objc_getProperty(), etc) that are used when
7113 @synthesize is used.
7115 runtime-specific routines are built in the respective runtime
7116 initialize functions. */
7117 static void
7118 build_common_objc_property_accessor_helpers (void)
7120 tree type;
7122 /* Declare the following function:
7124 objc_getProperty (id self, SEL _cmd,
7125 ptrdiff_t offset, BOOL is_atomic); */
7126 type = build_function_type_list (objc_object_type,
7127 objc_object_type,
7128 objc_selector_type,
7129 ptrdiff_type_node,
7130 boolean_type_node,
7131 NULL_TREE);
7132 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7133 type, 0, NOT_BUILT_IN,
7134 NULL, NULL_TREE);
7135 TREE_NOTHROW (objc_getProperty_decl) = 0;
7137 /* Declare the following function:
7138 void
7139 objc_setProperty (id self, SEL _cmd,
7140 ptrdiff_t offset, id new_value,
7141 BOOL is_atomic, BOOL should_copy); */
7142 type = build_function_type_list (void_type_node,
7143 objc_object_type,
7144 objc_selector_type,
7145 ptrdiff_type_node,
7146 objc_object_type,
7147 boolean_type_node,
7148 boolean_type_node,
7149 NULL_TREE);
7150 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7151 type, 0, NOT_BUILT_IN,
7152 NULL, NULL_TREE);
7153 TREE_NOTHROW (objc_setProperty_decl) = 0;
7156 /* This looks up an ivar in a class (including superclasses). */
7157 static tree
7158 lookup_ivar (tree interface, tree instance_variable_name)
7160 while (interface)
7162 tree decl_chain;
7164 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7165 if (DECL_NAME (decl_chain) == instance_variable_name)
7166 return decl_chain;
7168 /* Not found. Search superclass if any. */
7169 if (CLASS_SUPER_NAME (interface))
7170 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7173 return NULL_TREE;
7176 /* This routine synthesizes a 'getter' method. This is only called
7177 for @synthesize properties. */
7178 static void
7179 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7181 location_t location = DECL_SOURCE_LOCATION (property);
7182 tree fn, decl;
7183 tree body;
7184 tree ret_val;
7186 /* If user has implemented a getter with same name then do nothing. */
7187 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7188 PROPERTY_GETTER_NAME (property)))
7189 return;
7191 /* Find declaration of the property getter in the interface (or
7192 superclass, or protocol). There must be one. */
7193 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7195 /* If one not declared in the interface, this condition has already
7196 been reported as user error (because property was not declared in
7197 the interface). */
7198 if (!decl)
7199 return;
7201 /* Adapt the 'decl'. Use the source location of the @synthesize
7202 statement for error messages. */
7203 decl = copy_node (decl);
7204 DECL_SOURCE_LOCATION (decl) = location;
7206 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7207 NULL_TREE);
7208 body = c_begin_compound_stmt (true);
7210 /* Now we need to decide how we build the getter. There are three
7211 cases:
7213 for 'copy' or 'retain' properties we need to use the
7214 objc_getProperty() accessor helper which knows about retain and
7215 copy. It supports both 'nonatomic' and 'atomic' access.
7217 for 'nonatomic, assign' properties we can access the instance
7218 variable directly. 'nonatomic' means we don't have to use locks,
7219 and 'assign' means we don't have to worry about retain or copy.
7220 If you combine the two, it means we can just access the instance
7221 variable directly.
7223 for 'atomic, assign' properties we use objc_copyStruct() (for the
7224 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7225 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7227 case OBJC_PROPERTY_RETAIN:
7228 case OBJC_PROPERTY_COPY:
7230 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7231 tree cmd, ivar, offset, is_atomic;
7232 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7234 /* Find the ivar to compute the offset. */
7235 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7236 if (!ivar || is_private (ivar))
7238 /* This should never happen. */
7239 error_at (location,
7240 "can not find instance variable associated with property");
7241 ret_val = error_mark_node;
7242 break;
7244 offset = byte_position (ivar);
7246 if (PROPERTY_NONATOMIC (property))
7247 is_atomic = boolean_false_node;
7248 else
7249 is_atomic = boolean_true_node;
7251 ret_val = build_function_call
7252 (location,
7253 /* Function prototype. */
7254 objc_getProperty_decl,
7255 /* Parameters. */
7256 tree_cons /* self */
7257 (NULL_TREE, self_decl,
7258 tree_cons /* _cmd */
7259 (NULL_TREE, cmd,
7260 tree_cons /* offset */
7261 (NULL_TREE, offset,
7262 tree_cons /* is_atomic */
7263 (NULL_TREE, is_atomic, NULL_TREE)))));
7265 break;
7266 case OBJC_PROPERTY_ASSIGN:
7267 if (PROPERTY_NONATOMIC (property))
7269 /* We build "return self->PROPERTY_IVAR_NAME;" */
7270 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7271 break;
7273 else
7275 /* We build
7276 <property type> __objc_property_temp;
7277 objc_getPropertyStruct (&__objc_property_temp,
7278 &(self->PROPERTY_IVAR_NAME),
7279 sizeof (type of self->PROPERTY_IVAR_NAME),
7280 is_atomic,
7281 false)
7282 return __objc_property_temp;
7284 For the NeXT runtime, we need to use objc_copyStruct
7285 instead of objc_getPropertyStruct. */
7286 tree objc_property_temp_decl, function_decl, function_call;
7287 tree size_of, is_atomic;
7289 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7290 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7291 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7293 /* sizeof (ivar type). Since the ivar and the property have
7294 the same type, there is no need to lookup the ivar. */
7295 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7296 true /* is_sizeof */,
7297 false /* min_alignof */,
7298 false /* complain */);
7300 if (PROPERTY_NONATOMIC (property))
7301 is_atomic = boolean_false_node;
7302 else
7303 is_atomic = boolean_true_node;
7305 if (objc_copyStruct_decl)
7306 function_decl = objc_copyStruct_decl;
7307 else
7308 function_decl = objc_getPropertyStruct_decl;
7310 function_call = build_function_call
7311 (location,
7312 /* Function prototype. */
7313 function_decl,
7314 /* Parameters. */
7315 tree_cons /* &__objc_property_temp_decl */
7316 /* Warning: note that using build_fold_addr_expr_loc()
7317 here causes invalid code to be generated. */
7318 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7319 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7320 (NULL_TREE, build_fold_addr_expr_loc (location,
7321 objc_lookup_ivar
7322 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7323 tree_cons /* sizeof (PROPERTY_IVAR) */
7324 (NULL_TREE, size_of,
7325 tree_cons /* is_atomic */
7326 (NULL_TREE, is_atomic,
7327 /* TODO: This is currently ignored by the GNU
7328 runtime, but what about the next one ? */
7329 tree_cons /* has_strong */
7330 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7332 add_stmt (function_call);
7334 ret_val = objc_property_temp_decl;
7336 break;
7337 default:
7338 gcc_unreachable ();
7341 gcc_assert (ret_val);
7343 #ifdef OBJCPLUS
7344 finish_return_stmt (ret_val);
7345 #else
7346 c_finish_return (location, ret_val, NULL_TREE);
7347 #endif
7349 add_stmt (c_end_compound_stmt (location, body, true));
7350 fn = current_function_decl;
7351 #ifdef OBJCPLUS
7352 finish_function ();
7353 #endif
7354 objc_finish_method_definition (fn);
7357 /* This routine synthesizes a 'setter' method. */
7359 static void
7360 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7362 location_t location = DECL_SOURCE_LOCATION (property);
7363 tree fn, decl;
7364 tree body;
7365 tree new_value, statement;
7367 /* If user has implemented a setter with same name then do nothing. */
7368 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7369 PROPERTY_SETTER_NAME (property)))
7370 return;
7372 /* Find declaration of the property setter in the interface (or
7373 superclass, or protocol). There must be one. */
7374 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7376 /* If one not declared in the interface, this condition has already
7377 been reported as user error (because property was not declared in
7378 the interface). */
7379 if (!decl)
7380 return;
7382 /* Adapt the 'decl'. Use the source location of the @synthesize
7383 statement for error messages. */
7384 decl = copy_node (decl);
7385 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7387 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7388 NULL_TREE);
7390 body = c_begin_compound_stmt (true);
7392 /* The 'new_value' is the only argument to the method, which is the
7393 3rd argument of the function, after self and _cmd. We use twice
7394 TREE_CHAIN to move forward two arguments. */
7395 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7397 /* This would presumably happen if the user has specified a
7398 prototype for the setter that does not have an argument! */
7399 if (new_value == NULL_TREE)
7401 /* TODO: This should be caught much earlier than this. */
7402 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7403 /* Try to recover somehow. */
7404 new_value = error_mark_node;
7407 /* Now we need to decide how we build the setter. There are three
7408 cases:
7410 for 'copy' or 'retain' properties we need to use the
7411 objc_setProperty() accessor helper which knows about retain and
7412 copy. It supports both 'nonatomic' and 'atomic' access.
7414 for 'nonatomic, assign' properties we can access the instance
7415 variable directly. 'nonatomic' means we don't have to use locks,
7416 and 'assign' means we don't have to worry about retain or copy.
7417 If you combine the two, it means we can just access the instance
7418 variable directly.
7420 for 'atomic, assign' properties we use objc_copyStruct() (for the
7421 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7422 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7424 case OBJC_PROPERTY_RETAIN:
7425 case OBJC_PROPERTY_COPY:
7427 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7428 tree cmd, ivar, offset, is_atomic, should_copy;
7429 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7431 /* Find the ivar to compute the offset. */
7432 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7433 if (!ivar || is_private (ivar))
7435 error_at (location,
7436 "can not find instance variable associated with property");
7437 statement = error_mark_node;
7438 break;
7440 offset = byte_position (ivar);
7442 if (PROPERTY_NONATOMIC (property))
7443 is_atomic = boolean_false_node;
7444 else
7445 is_atomic = boolean_true_node;
7447 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7448 should_copy = boolean_true_node;
7449 else
7450 should_copy = boolean_false_node;
7452 statement = build_function_call
7453 (location,
7454 /* Function prototype. */
7455 objc_setProperty_decl,
7456 /* Parameters. */
7457 tree_cons /* self */
7458 (NULL_TREE, self_decl,
7459 tree_cons /* _cmd */
7460 (NULL_TREE, cmd,
7461 tree_cons /* offset */
7462 (NULL_TREE, offset,
7463 tree_cons /* new_value */
7464 (NULL_TREE, new_value,
7465 tree_cons /* is_atomic */
7466 (NULL_TREE, is_atomic,
7467 tree_cons /* should_copy */
7468 (NULL_TREE, should_copy, NULL_TREE)))))));
7470 break;
7471 case OBJC_PROPERTY_ASSIGN:
7472 if (PROPERTY_NONATOMIC (property))
7474 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7475 statement = build_modify_expr
7476 (location,
7477 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7478 NULL_TREE, NOP_EXPR,
7479 location, new_value, NULL_TREE);
7480 break;
7482 else
7484 /* We build
7485 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7486 &new_value,
7487 sizeof (type of self->PROPERTY_IVAR_NAME),
7488 is_atomic,
7489 false)
7491 For the NeXT runtime, we need to use objc_copyStruct
7492 instead of objc_getPropertyStruct. */
7493 tree function_decl, size_of, is_atomic;
7495 /* sizeof (ivar type). Since the ivar and the property have
7496 the same type, there is no need to lookup the ivar. */
7497 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7498 true /* is_sizeof */,
7499 false /* min_alignof */,
7500 false /* complain */);
7502 if (PROPERTY_NONATOMIC (property))
7503 is_atomic = boolean_false_node;
7504 else
7505 is_atomic = boolean_true_node;
7507 if (objc_copyStruct_decl)
7508 function_decl = objc_copyStruct_decl;
7509 else
7510 function_decl = objc_setPropertyStruct_decl;
7512 statement = build_function_call
7513 (location,
7514 /* Function prototype. */
7515 function_decl,
7516 /* Parameters. */
7517 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7518 (NULL_TREE, build_fold_addr_expr_loc (location,
7519 objc_lookup_ivar
7520 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7521 tree_cons /* &new_value */
7522 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7523 tree_cons /* sizeof (PROPERTY_IVAR) */
7524 (NULL_TREE, size_of,
7525 tree_cons /* is_atomic */
7526 (NULL_TREE, is_atomic,
7527 /* TODO: This is currently ignored by the GNU
7528 runtime, but what about the next one ? */
7529 tree_cons /* has_strong */
7530 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7532 break;
7533 default:
7534 gcc_unreachable ();
7536 gcc_assert (statement);
7538 add_stmt (statement);
7539 add_stmt (c_end_compound_stmt (location, body, true));
7540 fn = current_function_decl;
7541 #ifdef OBJCPLUS
7542 finish_function ();
7543 #endif
7544 objc_finish_method_definition (fn);
7547 /* This function is a sub-routine of objc_add_synthesize_declaration.
7548 It is called for each property to synthesize once we have
7549 determined that the context is Ok. */
7550 static void
7551 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7552 tree property_name, tree ivar_name)
7554 /* Find the @property declaration. */
7555 tree property;
7556 tree x;
7558 /* Check that synthesize or dynamic has not already been used for
7559 the same property. */
7560 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7561 if (PROPERTY_NAME (property) == property_name)
7563 location_t original_location = DECL_SOURCE_LOCATION (property);
7565 if (PROPERTY_DYNAMIC (property))
7566 error_at (location, "property %qs already specified in %<@dynamic%>",
7567 IDENTIFIER_POINTER (property_name));
7568 else
7569 error_at (location, "property %qs already specified in %<@synthesize%>",
7570 IDENTIFIER_POINTER (property_name));
7572 if (original_location != UNKNOWN_LOCATION)
7573 inform (original_location, "originally specified here");
7574 return;
7577 /* Check that the property is declared in the interface. It could
7578 also be declared in a superclass or protocol. */
7579 property = lookup_property (interface, property_name);
7581 if (!property)
7583 error_at (location, "no declaration of property %qs found in the interface",
7584 IDENTIFIER_POINTER (property_name));
7585 return;
7587 else
7589 /* We have to copy the property, because we want to chain it to
7590 the implementation context, and we want to store the source
7591 location of the @synthesize, not of the original
7592 @property. */
7593 property = copy_node (property);
7594 DECL_SOURCE_LOCATION (property) = location;
7597 /* Determine PROPERTY_IVAR_NAME. */
7598 if (ivar_name == NULL_TREE)
7599 ivar_name = property_name;
7601 /* Check that the instance variable exists. You can only use an
7602 instance variable from the same class, not one from the
7603 superclass (this makes sense as it allows us to check that an
7604 instance variable is only used in one synthesized property). */
7606 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7607 tree type_of_ivar;
7608 if (!ivar)
7610 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7611 IDENTIFIER_POINTER (property_name));
7612 return;
7615 if (DECL_BIT_FIELD_TYPE (ivar))
7616 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7617 else
7618 type_of_ivar = TREE_TYPE (ivar);
7620 /* If the instance variable has a different C type, we throw an error ... */
7621 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7622 /* ... unless the property is readonly, in which case we allow
7623 the instance variable to be more specialized (this means we
7624 can generate the getter all right and it works). */
7625 && (!PROPERTY_READONLY (property)
7626 || !objc_compare_types (TREE_TYPE (property),
7627 type_of_ivar, -5, NULL_TREE)))
7629 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7631 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7632 IDENTIFIER_POINTER (property_name),
7633 IDENTIFIER_POINTER (ivar_name));
7635 if (original_location != UNKNOWN_LOCATION)
7636 inform (original_location, "originally specified here");
7639 /* If the instance variable is a bitfield, the property must be
7640 'assign', 'nonatomic' because the runtime getter/setter helper
7641 do not work with bitfield instance variables. */
7642 if (DECL_BIT_FIELD_TYPE (ivar))
7644 /* If there is an error, we return and not generate any
7645 getter/setter because trying to set up the runtime
7646 getter/setter helper calls with bitfields is at high risk
7647 of ICE. */
7649 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7651 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7653 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
7654 IDENTIFIER_POINTER (property_name),
7655 IDENTIFIER_POINTER (ivar_name));
7657 if (original_location != UNKNOWN_LOCATION)
7658 inform (original_location, "originally specified here");
7659 return;
7662 if (!PROPERTY_NONATOMIC (property))
7664 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7666 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
7667 IDENTIFIER_POINTER (property_name),
7668 IDENTIFIER_POINTER (ivar_name));
7670 if (original_location != UNKNOWN_LOCATION)
7671 inform (original_location, "originally specified here");
7672 return;
7677 /* Check that no other property is using the same instance
7678 variable. */
7679 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7680 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7682 location_t original_location = DECL_SOURCE_LOCATION (x);
7684 error_at (location, "property %qs is using the same instance variable as property %qs",
7685 IDENTIFIER_POINTER (property_name),
7686 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7688 if (original_location != UNKNOWN_LOCATION)
7689 inform (original_location, "originally specified here");
7691 /* We keep going on. This won't cause the compiler to fail;
7692 the failure would most likely be at runtime. */
7695 /* Note that a @synthesize (and only a @synthesize) always sets
7696 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7697 @synthesize by that. */
7698 PROPERTY_IVAR_NAME (property) = ivar_name;
7700 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7701 original declaration; they are always set (with the exception of
7702 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7704 /* Add the property to the list of properties for current implementation. */
7705 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7706 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7708 /* Note how we don't actually synthesize the getter/setter here; it
7709 would be very natural, but we may miss the fact that the user has
7710 implemented his own getter/setter later on in the @implementation
7711 (in which case we shouldn't generate getter/setter). We wait
7712 until we have parsed it all before generating the code. */
7715 /* This function is called by the parser after a @synthesize
7716 expression is parsed. 'location' is the location of the
7717 @synthesize expression, and 'property_and_ivar_list' is a chained
7718 list of the property and ivar names. */
7719 void
7720 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7722 tree interface, chain;
7724 if (flag_objc1_only)
7725 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7727 if (property_and_ivar_list == error_mark_node)
7728 return;
7730 if (!objc_implementation_context)
7732 /* We can get here only in Objective-C; the Objective-C++ parser
7733 detects the problem while parsing, outputs the error
7734 "misplaced '@synthesize' Objective-C++ construct" and skips
7735 the declaration. */
7736 error_at (location, "%<@synthesize%> not in @implementation context");
7737 return;
7740 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7742 error_at (location, "%<@synthesize%> can not be used in categories");
7743 return;
7746 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7747 if (!interface)
7749 /* I can't see how this could happen, but it is good as a safety check. */
7750 error_at (location,
7751 "%<@synthesize%> requires the @interface of the class to be available");
7752 return;
7755 /* Now, iterate over the properties and do each of them. */
7756 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7758 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7759 TREE_PURPOSE (chain));
7763 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7764 is called for each property to mark as dynamic once we have
7765 determined that the context is Ok. */
7766 static void
7767 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7768 tree property_name)
7770 /* Find the @property declaration. */
7771 tree property;
7773 /* Check that synthesize or dynamic has not already been used for
7774 the same property. */
7775 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7776 if (PROPERTY_NAME (property) == property_name)
7778 location_t original_location = DECL_SOURCE_LOCATION (property);
7780 if (PROPERTY_DYNAMIC (property))
7781 error_at (location, "property %qs already specified in %<@dynamic%>",
7782 IDENTIFIER_POINTER (property_name));
7783 else
7784 error_at (location, "property %qs already specified in %<@synthesize%>",
7785 IDENTIFIER_POINTER (property_name));
7787 if (original_location != UNKNOWN_LOCATION)
7788 inform (original_location, "originally specified here");
7789 return;
7792 /* Check that the property is declared in the interface. It could
7793 also be declared in a superclass or protocol. */
7794 property = lookup_property (interface, property_name);
7796 if (!property)
7798 error_at (location, "no declaration of property %qs found in the interface",
7799 IDENTIFIER_POINTER (property_name));
7800 return;
7802 else
7804 /* We have to copy the property, because we want to chain it to
7805 the implementation context, and we want to store the source
7806 location of the @synthesize, not of the original
7807 @property. */
7808 property = copy_node (property);
7809 DECL_SOURCE_LOCATION (property) = location;
7812 /* Note that a @dynamic (and only a @dynamic) always sets
7813 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7814 (actually, as explained above, PROPERTY_DECL generated by
7815 @property and associated with a @dynamic property are also marked
7816 as PROPERTY_DYNAMIC). */
7817 PROPERTY_DYNAMIC (property) = 1;
7819 /* Add the property to the list of properties for current implementation. */
7820 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7821 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7824 /* This function is called by the parser after a @dynamic expression
7825 is parsed. 'location' is the location of the @dynamic expression,
7826 and 'property_list' is a chained list of all the property
7827 names. */
7828 void
7829 objc_add_dynamic_declaration (location_t location, tree property_list)
7831 tree interface, chain;
7833 if (flag_objc1_only)
7834 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7836 if (property_list == error_mark_node)
7837 return;
7839 if (!objc_implementation_context)
7841 /* We can get here only in Objective-C; the Objective-C++ parser
7842 detects the problem while parsing, outputs the error
7843 "misplaced '@dynamic' Objective-C++ construct" and skips the
7844 declaration. */
7845 error_at (location, "%<@dynamic%> not in @implementation context");
7846 return;
7849 /* @dynamic is allowed in categories. */
7850 switch (TREE_CODE (objc_implementation_context))
7852 case CLASS_IMPLEMENTATION_TYPE:
7853 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7854 break;
7855 case CATEGORY_IMPLEMENTATION_TYPE:
7856 interface = lookup_category (implementation_template,
7857 CLASS_SUPER_NAME (objc_implementation_context));
7858 break;
7859 default:
7860 gcc_unreachable ();
7863 if (!interface)
7865 /* I can't see how this could happen, but it is good as a safety check. */
7866 error_at (location,
7867 "%<@dynamic%> requires the @interface of the class to be available");
7868 return;
7871 /* Now, iterate over the properties and do each of them. */
7872 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7874 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7878 /* Main routine to generate code/data for all the property information for
7879 current implementation (class or category). CLASS is the interface where
7880 ivars are declared. CLASS_METHODS is where methods are found which
7881 could be a class or a category depending on whether we are implementing
7882 property of a class or a category. */
7884 static void
7885 objc_gen_property_data (tree klass, tree class_methods)
7887 tree x;
7889 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7891 /* @dynamic property - nothing to check or synthesize. */
7892 if (PROPERTY_DYNAMIC (x))
7893 continue;
7895 /* @synthesize property - need to synthesize the accessors. */
7896 if (PROPERTY_IVAR_NAME (x))
7898 objc_synthesize_getter (klass, class_methods, x);
7900 if (PROPERTY_READONLY (x) == 0)
7901 objc_synthesize_setter (klass, class_methods, x);
7903 continue;
7906 gcc_unreachable ();
7910 /* This is called once we see the "@end" in an interface/implementation. */
7912 static void
7913 finish_class (tree klass)
7915 switch (TREE_CODE (klass))
7917 case CLASS_IMPLEMENTATION_TYPE:
7919 /* All metadata generation is done in runtime.generate_metadata(). */
7921 /* Generate what needed for property; setters, getters, etc. */
7922 objc_gen_property_data (implementation_template, implementation_template);
7924 if (implementation_template != objc_implementation_context)
7926 /* Ensure that all method listed in the interface contain bodies. */
7927 check_methods (CLASS_CLS_METHODS (implementation_template),
7928 objc_implementation_context, '+');
7929 check_methods (CLASS_NST_METHODS (implementation_template),
7930 objc_implementation_context, '-');
7932 if (CLASS_PROTOCOL_LIST (implementation_template))
7933 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7934 "class",
7935 CLASS_NAME (objc_implementation_context));
7937 break;
7939 case CATEGORY_IMPLEMENTATION_TYPE:
7941 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7943 if (category)
7945 /* Generate what needed for property; setters, getters, etc. */
7946 objc_gen_property_data (implementation_template, category);
7948 /* Ensure all method listed in the interface contain bodies. */
7949 check_methods (CLASS_CLS_METHODS (category),
7950 objc_implementation_context, '+');
7951 check_methods (CLASS_NST_METHODS (category),
7952 objc_implementation_context, '-');
7954 if (CLASS_PROTOCOL_LIST (category))
7955 check_protocols (CLASS_PROTOCOL_LIST (category),
7956 "category",
7957 CLASS_SUPER_NAME (objc_implementation_context));
7959 break;
7961 case CLASS_INTERFACE_TYPE:
7962 case CATEGORY_INTERFACE_TYPE:
7963 case PROTOCOL_INTERFACE_TYPE:
7965 /* Process properties of the class. */
7966 tree x;
7967 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7969 /* Now we check that the appropriate getter is declared,
7970 and if not, we declare one ourselves. */
7971 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7972 PROPERTY_GETTER_NAME (x));
7974 if (getter_decl)
7976 /* TODO: Check that the declaration is consistent with the property. */
7979 else
7981 /* Generate an instance method declaration for the
7982 getter; for example "- (id) name;". In general it
7983 will be of the form
7984 -(type)property_getter_name; */
7985 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
7986 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7987 rettype, PROPERTY_GETTER_NAME (x),
7988 NULL_TREE, false);
7989 if (PROPERTY_OPTIONAL (x))
7990 objc_add_method (objc_interface_context, getter_decl, false, true);
7991 else
7992 objc_add_method (objc_interface_context, getter_decl, false, false);
7993 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
7994 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
7997 if (PROPERTY_READONLY (x) == 0)
7999 /* Now we check that the appropriate setter is declared,
8000 and if not, we declare on ourselves. */
8001 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
8002 PROPERTY_SETTER_NAME (x));
8004 if (setter_decl)
8006 /* TODO: Check that the declaration is consistent with the property. */
8009 else
8011 /* The setter name is something like 'setName:'.
8012 We need the substring 'setName' to build the
8013 method declaration due to how the declaration
8014 works. TODO: build_method_decl() will then
8015 generate back 'setName:' from 'setName'; it
8016 would be more efficient to hook into there. */
8017 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8018 size_t length = strlen (full_setter_name);
8019 char *setter_name = (char *) alloca (length);
8020 tree ret_type, selector, arg_type, arg_name;
8022 strcpy (setter_name, full_setter_name);
8023 setter_name[length - 1] = '\0';
8024 ret_type = build_tree_list (NULL_TREE, void_type_node);
8025 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8026 arg_name = get_identifier ("_value");
8027 selector = objc_build_keyword_decl (get_identifier (setter_name),
8028 arg_type, arg_name, NULL);
8029 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8030 ret_type, selector,
8031 build_tree_list (NULL_TREE, NULL_TREE),
8032 false);
8033 if (PROPERTY_OPTIONAL (x))
8034 objc_add_method (objc_interface_context, setter_decl, false, true);
8035 else
8036 objc_add_method (objc_interface_context, setter_decl, false, false);
8037 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8038 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8042 break;
8044 default:
8045 gcc_unreachable ();
8046 break;
8050 static tree
8051 add_protocol (tree protocol)
8053 /* Put protocol on list in reverse order. */
8054 TREE_CHAIN (protocol) = protocol_chain;
8055 protocol_chain = protocol;
8056 return protocol_chain;
8059 /* Check that a protocol is defined, and, recursively, that all
8060 protocols that this protocol conforms to are defined too. */
8061 static void
8062 check_that_protocol_is_defined (tree protocol)
8064 if (!PROTOCOL_DEFINED (protocol))
8065 warning (0, "definition of protocol %qE not found",
8066 PROTOCOL_NAME (protocol));
8068 /* If the protocol itself conforms to other protocols, check them
8069 too, recursively. */
8070 if (PROTOCOL_LIST (protocol))
8072 tree p;
8074 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8075 check_that_protocol_is_defined (TREE_VALUE (p));
8079 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8080 emitted if the protocol is deprecated. If 'definition_required' is
8081 true, a warning is emitted if a full @protocol definition has not
8082 been seen. */
8083 static tree
8084 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8086 tree chain;
8088 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8089 if (ident == PROTOCOL_NAME (chain))
8091 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8093 /* It would be nice to use warn_deprecated_use() here, but
8094 we are using TREE_CHAIN (which is supposed to be the
8095 TYPE_STUB_DECL for a TYPE) for something different. */
8096 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8097 PROTOCOL_NAME (chain));
8100 if (definition_required)
8101 check_that_protocol_is_defined (chain);
8103 return chain;
8106 return NULL_TREE;
8109 /* This function forward declares the protocols named by NAMES. If
8110 they are already declared or defined, the function has no effect. */
8112 void
8113 objc_declare_protocol (tree name, tree attributes)
8115 bool deprecated = false;
8117 #ifdef OBJCPLUS
8118 if (current_namespace != global_namespace) {
8119 error ("Objective-C declarations may only appear in global scope");
8121 #endif /* OBJCPLUS */
8123 /* Determine if 'deprecated', the only attribute we recognize for
8124 protocols, was used. Ignore all other attributes. */
8125 if (attributes)
8127 tree attribute;
8128 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8130 tree name = TREE_PURPOSE (attribute);
8132 if (is_attribute_p ("deprecated", name))
8133 deprecated = true;
8134 else
8135 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8139 if (lookup_protocol (name, /* warn if deprecated */ false,
8140 /* definition_required */ false) == NULL_TREE)
8142 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8144 TYPE_LANG_SLOT_1 (protocol)
8145 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8146 PROTOCOL_NAME (protocol) = name;
8147 PROTOCOL_LIST (protocol) = NULL_TREE;
8148 add_protocol (protocol);
8149 PROTOCOL_DEFINED (protocol) = 0;
8150 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8152 if (attributes)
8154 /* TODO: Do we need to store the attributes here ? */
8155 TYPE_ATTRIBUTES (protocol) = attributes;
8156 if (deprecated)
8157 TREE_DEPRECATED (protocol) = 1;
8162 static tree
8163 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8165 tree protocol;
8166 bool deprecated = false;
8168 #ifdef OBJCPLUS
8169 if (current_namespace != global_namespace) {
8170 error ("Objective-C declarations may only appear in global scope");
8172 #endif /* OBJCPLUS */
8174 /* Determine if 'deprecated', the only attribute we recognize for
8175 protocols, was used. Ignore all other attributes. */
8176 if (attributes)
8178 tree attribute;
8179 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8181 tree name = TREE_PURPOSE (attribute);
8183 if (is_attribute_p ("deprecated", name))
8184 deprecated = true;
8185 else
8186 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8190 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8191 /* definition_required */ false);
8193 if (!protocol)
8195 protocol = make_node (code);
8196 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8198 PROTOCOL_NAME (protocol) = name;
8199 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8200 add_protocol (protocol);
8201 PROTOCOL_DEFINED (protocol) = 1;
8202 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8204 check_protocol_recursively (protocol, list);
8206 else if (! PROTOCOL_DEFINED (protocol))
8208 PROTOCOL_DEFINED (protocol) = 1;
8209 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8211 check_protocol_recursively (protocol, list);
8213 else
8215 warning (0, "duplicate declaration for protocol %qE",
8216 name);
8219 if (attributes)
8221 TYPE_ATTRIBUTES (protocol) = attributes;
8222 if (deprecated)
8223 TREE_DEPRECATED (protocol) = 1;
8226 return protocol;
8229 /* Decay array and function parameters into pointers. */
8231 static tree
8232 objc_decay_parm_type (tree type)
8234 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8235 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8236 ? TREE_TYPE (type)
8237 : type);
8239 return type;
8242 static GTY(()) tree objc_parmlist = NULL_TREE;
8244 /* Append PARM to a list of formal parameters of a method, making a necessary
8245 array-to-pointer adjustment along the way. */
8247 void
8248 objc_push_parm (tree parm)
8250 tree type;
8252 if (TREE_TYPE (parm) == error_mark_node)
8254 objc_parmlist = chainon (objc_parmlist, parm);
8255 return;
8258 /* Decay arrays and functions into pointers. */
8259 type = objc_decay_parm_type (TREE_TYPE (parm));
8261 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8262 built as well. */
8263 if (type != TREE_TYPE (parm))
8264 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8266 DECL_ARG_TYPE (parm)
8267 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8269 /* Record constancy and volatility. */
8270 c_apply_type_quals_to_decl
8271 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8272 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8273 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
8274 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8276 objc_parmlist = chainon (objc_parmlist, parm);
8279 /* Retrieve the formal parameter list constructed via preceding calls to
8280 objc_push_parm(). */
8282 #ifdef OBJCPLUS
8283 tree
8284 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8285 tree expr ATTRIBUTE_UNUSED)
8287 tree parm_info = objc_parmlist;
8288 objc_parmlist = NULL_TREE;
8290 return parm_info;
8292 #else
8293 struct c_arg_info *
8294 objc_get_parm_info (int have_ellipsis, tree expr)
8296 tree parm_info = objc_parmlist;
8297 struct c_arg_info *arg_info;
8298 /* The C front-end requires an elaborate song and dance at
8299 this point. */
8300 push_scope ();
8301 declare_parm_level ();
8302 while (parm_info)
8304 tree next = DECL_CHAIN (parm_info);
8306 DECL_CHAIN (parm_info) = NULL_TREE;
8307 parm_info = pushdecl (parm_info);
8308 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8309 parm_info = next;
8311 arg_info = get_parm_info (have_ellipsis, expr);
8312 pop_scope ();
8313 objc_parmlist = NULL_TREE;
8314 return arg_info;
8316 #endif
8318 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8319 method definitions. In the case of instance methods, we can be more
8320 specific as to the type of 'self'. */
8322 static void
8323 synth_self_and_ucmd_args (void)
8325 tree self_type;
8327 if (objc_method_context
8328 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8329 self_type = objc_instance_type;
8330 else
8331 /* Really a `struct objc_class *'. However, we allow people to
8332 assign to self, which changes its type midstream. */
8333 self_type = objc_object_type;
8335 /* id self; */
8336 objc_push_parm (build_decl (input_location,
8337 PARM_DECL, self_id, self_type));
8339 /* SEL _cmd; */
8340 objc_push_parm (build_decl (input_location,
8341 PARM_DECL, ucmd_id, objc_selector_type));
8344 /* Transform an Objective-C method definition into a static C function
8345 definition, synthesizing the first two arguments, "self" and "_cmd",
8346 in the process. EXPR is NULL or an expression that needs to be
8347 evaluated for the side effects of array size expressions in the
8348 parameters. */
8350 static void
8351 start_method_def (tree method, tree expr)
8353 tree parmlist;
8354 #ifdef OBJCPLUS
8355 tree parm_info;
8356 #else
8357 struct c_arg_info *parm_info;
8358 #endif
8359 int have_ellipsis = 0;
8361 /* If we are defining a "dealloc" method in a non-root class, we
8362 will need to check if a [super dealloc] is missing, and warn if
8363 it is. */
8364 if(CLASS_SUPER_NAME (objc_implementation_context)
8365 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8366 should_call_super_dealloc = 1;
8367 else
8368 should_call_super_dealloc = 0;
8370 /* Required to implement _msgSuper. */
8371 objc_method_context = method;
8372 UOBJC_SUPER_decl = NULL_TREE;
8374 /* Generate prototype declarations for arguments..."new-style". */
8375 synth_self_and_ucmd_args ();
8377 /* Generate argument declarations if a keyword_decl. */
8378 parmlist = METHOD_SEL_ARGS (method);
8379 while (parmlist)
8381 /* parmlist is a KEYWORD_DECL. */
8382 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8383 tree parm;
8385 parm = build_decl (input_location,
8386 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8387 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8388 objc_push_parm (parm);
8389 parmlist = DECL_CHAIN (parmlist);
8392 if (METHOD_ADD_ARGS (method))
8394 tree akey;
8396 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8397 akey; akey = TREE_CHAIN (akey))
8399 objc_push_parm (TREE_VALUE (akey));
8402 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8403 have_ellipsis = 1;
8406 parm_info = objc_get_parm_info (have_ellipsis, expr);
8408 really_start_method (objc_method_context, parm_info);
8411 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8412 overloading. */
8413 static int
8414 objc_types_are_equivalent (tree type1, tree type2)
8416 if (type1 == type2)
8417 return 1;
8419 /* Strip away indirections. */
8420 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8421 && (TREE_CODE (type1) == TREE_CODE (type2)))
8422 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8423 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8424 return 0;
8426 /* Compare the protocol lists. */
8427 type1 = (TYPE_HAS_OBJC_INFO (type1)
8428 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8429 : NULL_TREE);
8430 type2 = (TYPE_HAS_OBJC_INFO (type2)
8431 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8432 : NULL_TREE);
8434 /* If there are no protocols (most common case), the types are
8435 identical. */
8436 if (type1 == NULL_TREE && type2 == NULL_TREE)
8437 return 1;
8439 /* If one has protocols, and the other one hasn't, they are not
8440 identical. */
8441 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8442 || (type1 != NULL_TREE && type2 == NULL_TREE))
8443 return 0;
8444 else
8446 /* Else, both have protocols, and we need to do the full
8447 comparison. It is possible that either type1 or type2
8448 contain some duplicate protocols in the list, so we can't
8449 even just compare list_length as a first check. */
8450 tree t;
8452 for (t = type2; t; t = TREE_CHAIN (t))
8453 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8454 return 0;
8456 for (t = type1; t; t = TREE_CHAIN (t))
8457 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8458 return 0;
8460 return 1;
8464 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8466 static int
8467 objc_types_share_size_and_alignment (tree type1, tree type2)
8469 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8470 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8473 /* Return 1 if PROTO1 is equivalent to PROTO2
8474 for purposes of method overloading. Ordinarily, the type signatures
8475 should match up exactly, unless STRICT is zero, in which case we
8476 shall allow differences in which the size and alignment of a type
8477 is the same. */
8479 static int
8480 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8482 tree type1, type2;
8484 /* The following test is needed in case there are hashing
8485 collisions. */
8486 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8487 return 0;
8489 /* Compare return types. */
8490 type1 = TREE_VALUE (TREE_TYPE (proto1));
8491 type2 = TREE_VALUE (TREE_TYPE (proto2));
8493 if (!objc_types_are_equivalent (type1, type2)
8494 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8495 return 0;
8497 /* Compare argument types. */
8499 /* The first argument (objc_object_type) is always the same, no need
8500 to compare. */
8502 /* The second argument (objc_selector_type) is always the same, no
8503 need to compare. */
8505 /* Compare the other arguments. */
8507 tree arg1, arg2;
8509 /* Compare METHOD_SEL_ARGS. */
8510 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8511 arg1 && arg2;
8512 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8514 type1 = TREE_VALUE (TREE_TYPE (arg1));
8515 type2 = TREE_VALUE (TREE_TYPE (arg2));
8517 /* FIXME: Do we need to decay argument types to compare them ? */
8518 type1 = objc_decay_parm_type (type1);
8519 type2 = objc_decay_parm_type (type2);
8521 if (!objc_types_are_equivalent (type1, type2)
8522 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8523 return 0;
8526 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8527 both NULL. */
8528 if (arg1 != arg2)
8529 return 0;
8531 /* Compare METHOD_ADD_ARGS. */
8532 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8533 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8534 return 0;
8536 if (METHOD_ADD_ARGS (proto1))
8538 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8539 arg1 && arg2;
8540 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8542 type1 = TREE_TYPE (TREE_VALUE (arg1));
8543 type2 = TREE_TYPE (TREE_VALUE (arg2));
8545 /* FIXME: Do we need to decay argument types to compare them ? */
8546 type1 = objc_decay_parm_type (type1);
8547 type2 = objc_decay_parm_type (type2);
8549 if (!objc_types_are_equivalent (type1, type2)
8550 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8551 return 0;
8555 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8556 both NULL. */
8557 if (arg1 != arg2)
8558 return 0;
8560 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8561 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8562 return 0;
8565 /* Success. */
8566 return 1;
8569 /* This routine returns true if TYPE is a valid objc object type,
8570 suitable for messaging; false otherwise. If 'accept_class' is
8571 'true', then a Class object is considered valid for messaging and
8572 'true' is returned if 'type' refers to a Class. If 'accept_class'
8573 is 'false', then a Class object is not considered valid for
8574 messaging and 'false' is returned in that case. */
8576 static bool
8577 objc_type_valid_for_messaging (tree type, bool accept_classes)
8579 if (!POINTER_TYPE_P (type))
8580 return false;
8582 /* Remove the pointer indirection; don't remove more than one
8583 otherwise we'd consider "NSObject **" a valid type for messaging,
8584 which it isn't. */
8585 type = TREE_TYPE (type);
8587 if (TREE_CODE (type) != RECORD_TYPE)
8588 return false;
8590 if (objc_is_object_id (type))
8591 return true;
8593 if (objc_is_class_id (type))
8594 return accept_classes;
8596 if (TYPE_HAS_OBJC_INFO (type))
8597 return true;
8599 return false;
8602 void
8603 objc_start_function (tree name, tree type, tree attrs,
8604 #ifdef OBJCPLUS
8605 tree params
8606 #else
8607 struct c_arg_info *params
8608 #endif
8611 tree fndecl = build_decl (input_location,
8612 FUNCTION_DECL, name, type);
8614 #ifdef OBJCPLUS
8615 DECL_ARGUMENTS (fndecl) = params;
8616 DECL_INITIAL (fndecl) = error_mark_node;
8617 DECL_EXTERNAL (fndecl) = 0;
8618 TREE_STATIC (fndecl) = 1;
8619 retrofit_lang_decl (fndecl);
8620 cplus_decl_attributes (&fndecl, attrs, 0);
8621 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8622 #else
8623 current_function_returns_value = 0; /* Assume, until we see it does. */
8624 current_function_returns_null = 0;
8625 decl_attributes (&fndecl, attrs, 0);
8626 announce_function (fndecl);
8627 DECL_INITIAL (fndecl) = error_mark_node;
8628 DECL_EXTERNAL (fndecl) = 0;
8629 TREE_STATIC (fndecl) = 1;
8630 current_function_decl = pushdecl (fndecl);
8631 push_scope ();
8632 declare_parm_level ();
8633 DECL_RESULT (current_function_decl)
8634 = build_decl (input_location,
8635 RESULT_DECL, NULL_TREE,
8636 TREE_TYPE (TREE_TYPE (current_function_decl)));
8637 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8638 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8639 start_fname_decls ();
8640 store_parm_decls_from (params);
8641 #endif
8643 TREE_USED (current_function_decl) = 1;
8646 /* - Generate an identifier for the function. the format is "_n_cls",
8647 where 1 <= n <= nMethods, and cls is the name the implementation we
8648 are processing.
8649 - Install the return type from the method declaration.
8650 - If we have a prototype, check for type consistency. */
8652 static void
8653 really_start_method (tree method,
8654 #ifdef OBJCPLUS
8655 tree parmlist
8656 #else
8657 struct c_arg_info *parmlist
8658 #endif
8661 tree ret_type, meth_type;
8662 tree method_id;
8663 const char *sel_name, *class_name, *cat_name;
8664 char *buf;
8666 /* Synth the storage class & assemble the return type. */
8667 ret_type = TREE_VALUE (TREE_TYPE (method));
8669 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8670 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8671 cat_name = ((TREE_CODE (objc_implementation_context)
8672 == CLASS_IMPLEMENTATION_TYPE)
8673 ? NULL
8674 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8675 method_slot++;
8677 /* Make sure this is big enough for any plausible method label. */
8678 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8679 + (cat_name ? strlen (cat_name) : 0));
8681 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8682 class_name, cat_name, sel_name, method_slot);
8684 method_id = get_identifier (buf);
8686 #ifdef OBJCPLUS
8687 /* Objective-C methods cannot be overloaded, so we don't need
8688 the type encoding appended. It looks bad anyway... */
8689 push_lang_context (lang_name_c);
8690 #endif
8692 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8693 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8695 /* Set self_decl from the first argument. */
8696 self_decl = DECL_ARGUMENTS (current_function_decl);
8698 /* Suppress unused warnings. */
8699 TREE_USED (self_decl) = 1;
8700 DECL_READ_P (self_decl) = 1;
8701 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8702 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8703 #ifdef OBJCPLUS
8704 pop_lang_context ();
8705 #endif
8707 METHOD_DEFINITION (method) = current_function_decl;
8709 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8711 if (implementation_template != objc_implementation_context)
8713 tree proto
8714 = lookup_method_static (implementation_template,
8715 METHOD_SEL_NAME (method),
8716 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8717 | OBJC_LOOKUP_NO_SUPER));
8719 if (proto)
8721 if (!comp_proto_with_proto (method, proto, 1))
8723 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8725 warning_at (DECL_SOURCE_LOCATION (method), 0,
8726 "conflicting types for %<%c%s%>",
8727 (type ? '-' : '+'),
8728 identifier_to_locale (gen_method_decl (method)));
8729 inform (DECL_SOURCE_LOCATION (proto),
8730 "previous declaration of %<%c%s%>",
8731 (type ? '-' : '+'),
8732 identifier_to_locale (gen_method_decl (proto)));
8734 else
8736 /* If the method in the @interface was deprecated, mark
8737 the implemented method as deprecated too. It should
8738 never be used for messaging (when the deprecation
8739 warnings are produced), but just in case. */
8740 if (TREE_DEPRECATED (proto))
8741 TREE_DEPRECATED (method) = 1;
8743 /* If the method in the @interface was marked as
8744 'noreturn', mark the function implementing the method
8745 as 'noreturn' too. */
8746 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8749 else
8751 /* We have a method @implementation even though we did not
8752 see a corresponding @interface declaration (which is allowed
8753 by Objective-C rules). Go ahead and place the method in
8754 the @interface anyway, so that message dispatch lookups
8755 will see it. */
8756 tree interface = implementation_template;
8758 if (TREE_CODE (objc_implementation_context)
8759 == CATEGORY_IMPLEMENTATION_TYPE)
8760 interface = lookup_category
8761 (interface,
8762 CLASS_SUPER_NAME (objc_implementation_context));
8764 if (interface)
8765 objc_add_method (interface, copy_node (method),
8766 TREE_CODE (method) == CLASS_METHOD_DECL,
8767 /* is_optional= */ false);
8772 static void *UOBJC_SUPER_scope = 0;
8774 /* _n_Method (id self, SEL sel, ...)
8776 struct objc_super _S;
8777 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8778 } */
8780 static tree
8781 get_super_receiver (void)
8783 if (objc_method_context)
8785 tree super_expr, super_expr_list, class_expr;
8786 bool inst_meth;
8787 if (!UOBJC_SUPER_decl)
8789 UOBJC_SUPER_decl = build_decl (input_location,
8790 VAR_DECL, get_identifier (TAG_SUPER),
8791 objc_super_template);
8792 /* This prevents `unused variable' warnings when compiling with -Wall. */
8793 TREE_USED (UOBJC_SUPER_decl) = 1;
8794 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8795 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8796 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8797 NULL_TREE);
8798 UOBJC_SUPER_scope = objc_get_current_scope ();
8801 /* Set receiver to self. */
8802 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8803 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8804 NOP_EXPR, input_location, self_decl,
8805 NULL_TREE);
8806 super_expr_list = super_expr;
8808 /* Set class to begin searching. */
8809 /* Get the ident for the superclass class field & build a ref to it.
8810 ??? maybe we should just name the field the same for all runtimes. */
8811 super_expr = (*runtime.super_superclassfield_ident) ();
8812 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8814 gcc_assert (imp_list->imp_context == objc_implementation_context
8815 && imp_list->imp_template == implementation_template);
8816 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8818 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8819 class_expr = (*runtime.get_class_super_ref) (input_location,
8820 imp_list, inst_meth);
8821 else
8822 /* We have a category. */
8824 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8825 tree super_class;
8827 /* Barf if super used in a category of a root object. */
8828 if (!super_name)
8830 error ("no super class declared in interface for %qE",
8831 CLASS_NAME (imp_list->imp_template));
8832 return error_mark_node;
8835 super_class = (*runtime.get_category_super_ref) (input_location,
8836 imp_list, inst_meth);
8837 class_expr = build_c_cast (input_location,
8838 TREE_TYPE (super_expr), super_class);
8841 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8842 NOP_EXPR,
8843 input_location, class_expr, NULL_TREE);
8845 super_expr_list = build_compound_expr (input_location,
8846 super_expr_list, super_expr);
8848 super_expr = build_unary_op (input_location,
8849 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8850 super_expr_list = build_compound_expr (input_location,
8851 super_expr_list, super_expr);
8853 return super_expr_list;
8855 else
8857 error ("[super ...] must appear in a method context");
8858 return error_mark_node;
8862 /* When exiting a scope, sever links to a 'super' declaration (if any)
8863 therein contained. */
8865 void
8866 objc_clear_super_receiver (void)
8868 if (objc_method_context
8869 && UOBJC_SUPER_scope == objc_get_current_scope ())
8871 UOBJC_SUPER_decl = 0;
8872 UOBJC_SUPER_scope = 0;
8876 void
8877 objc_finish_method_definition (tree fndecl)
8879 /* We cannot validly inline ObjC methods, at least not without a language
8880 extension to declare that a method need not be dynamically
8881 dispatched, so suppress all thoughts of doing so. */
8882 DECL_UNINLINABLE (fndecl) = 1;
8884 #ifndef OBJCPLUS
8885 /* The C++ front-end will have called finish_function() for us. */
8886 finish_function ();
8887 #endif
8889 METHOD_ENCODING (objc_method_context)
8890 = encode_method_prototype (objc_method_context);
8892 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8893 since the optimizer may find "may be used before set" errors. */
8894 objc_method_context = NULL_TREE;
8896 if (should_call_super_dealloc)
8897 warning (0, "method possibly missing a [super dealloc] call");
8900 /* Given a tree DECL node, produce a printable description of it in the given
8901 buffer, overwriting the buffer. */
8903 static char *
8904 gen_declaration (tree decl)
8906 errbuf[0] = '\0';
8908 if (DECL_P (decl))
8910 gen_type_name_0 (TREE_TYPE (decl));
8912 if (DECL_NAME (decl))
8914 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8915 strcat (errbuf, " ");
8917 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
8920 if (DECL_INITIAL (decl)
8921 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
8922 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8923 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
8926 return errbuf;
8929 /* Given a tree TYPE node, produce a printable description of it in the given
8930 buffer, overwriting the buffer. */
8932 static char *
8933 gen_type_name_0 (tree type)
8935 tree orig = type, proto;
8937 if (TYPE_P (type) && TYPE_NAME (type))
8938 type = TYPE_NAME (type);
8939 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
8941 tree inner = TREE_TYPE (type);
8943 while (TREE_CODE (inner) == ARRAY_TYPE)
8944 inner = TREE_TYPE (inner);
8946 gen_type_name_0 (inner);
8948 if (!POINTER_TYPE_P (inner))
8949 strcat (errbuf, " ");
8951 if (POINTER_TYPE_P (type))
8952 strcat (errbuf, "*");
8953 else
8954 while (type != inner)
8956 strcat (errbuf, "[");
8958 if (TYPE_DOMAIN (type))
8960 char sz[20];
8962 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8963 (TREE_INT_CST_LOW
8964 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8965 strcat (errbuf, sz);
8968 strcat (errbuf, "]");
8969 type = TREE_TYPE (type);
8972 goto exit_function;
8975 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8976 type = DECL_NAME (type);
8978 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
8979 ? IDENTIFIER_POINTER (type)
8980 : "");
8982 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8983 if (objc_is_id (orig))
8984 orig = TREE_TYPE (orig);
8986 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8988 if (proto)
8990 strcat (errbuf, " <");
8992 while (proto) {
8993 strcat (errbuf,
8994 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
8995 proto = TREE_CHAIN (proto);
8996 strcat (errbuf, proto ? ", " : ">");
9000 exit_function:
9001 return errbuf;
9004 static char *
9005 gen_type_name (tree type)
9007 errbuf[0] = '\0';
9009 return gen_type_name_0 (type);
9012 /* Given a method tree, put a printable description into the given
9013 buffer (overwriting) and return a pointer to the buffer. */
9015 static char *
9016 gen_method_decl (tree method)
9018 tree chain;
9020 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9021 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9022 strcat (errbuf, ")");
9023 chain = METHOD_SEL_ARGS (method);
9025 if (chain)
9027 /* We have a chain of keyword_decls. */
9030 if (KEYWORD_KEY_NAME (chain))
9031 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9033 strcat (errbuf, ":(");
9034 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9035 strcat (errbuf, ")");
9037 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9038 if ((chain = DECL_CHAIN (chain)))
9039 strcat (errbuf, " ");
9041 while (chain);
9043 if (METHOD_ADD_ARGS (method))
9045 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9047 /* Know we have a chain of parm_decls. */
9048 while (chain)
9050 strcat (errbuf, ", ");
9051 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9052 chain = TREE_CHAIN (chain);
9055 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9056 strcat (errbuf, ", ...");
9060 else
9061 /* We have a unary selector. */
9062 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9064 return errbuf;
9067 /* Debug info. */
9070 /* Dump an @interface declaration of the supplied class CHAIN to the
9071 supplied file FP. Used to implement the -gen-decls option (which
9072 prints out an @interface declaration of all classes compiled in
9073 this run); potentially useful for debugging the compiler too. */
9074 void
9075 dump_interface (FILE *fp, tree chain)
9077 /* FIXME: A heap overflow here whenever a method (or ivar)
9078 declaration is so long that it doesn't fit in the buffer. The
9079 code and all the related functions should be rewritten to avoid
9080 using fixed size buffers. */
9081 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9082 tree ivar_decls = CLASS_RAW_IVARS (chain);
9083 tree nst_methods = CLASS_NST_METHODS (chain);
9084 tree cls_methods = CLASS_CLS_METHODS (chain);
9086 fprintf (fp, "\n@interface %s", my_name);
9088 /* CLASS_SUPER_NAME is used to store the superclass name for
9089 classes, and the category name for categories. */
9090 if (CLASS_SUPER_NAME (chain))
9092 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9094 switch (TREE_CODE (chain))
9096 case CATEGORY_IMPLEMENTATION_TYPE:
9097 case CATEGORY_INTERFACE_TYPE:
9098 fprintf (fp, " (%s)\n", name);
9099 break;
9100 default:
9101 fprintf (fp, " : %s\n", name);
9102 break;
9105 else
9106 fprintf (fp, "\n");
9108 /* FIXME - the following doesn't seem to work at the moment. */
9109 if (ivar_decls)
9111 fprintf (fp, "{\n");
9114 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9115 ivar_decls = TREE_CHAIN (ivar_decls);
9117 while (ivar_decls);
9118 fprintf (fp, "}\n");
9121 while (nst_methods)
9123 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9124 nst_methods = TREE_CHAIN (nst_methods);
9127 while (cls_methods)
9129 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9130 cls_methods = TREE_CHAIN (cls_methods);
9133 fprintf (fp, "@end\n");
9136 #if 0
9137 /* Produce the pretty printing for an Objective-C method. This is
9138 currently unused, but could be handy while reorganizing the pretty
9139 printing to be more robust. */
9140 static const char *
9141 objc_pretty_print_method (bool is_class_method,
9142 const char *class_name,
9143 const char *category_name,
9144 const char *selector)
9146 if (category_name)
9148 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9149 + strlen (selector) + 7);
9151 if (is_class_method)
9152 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9153 else
9154 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9156 return result;
9158 else
9160 char *result = XNEWVEC (char, strlen (class_name)
9161 + strlen (selector) + 5);
9163 if (is_class_method)
9164 sprintf (result, "+[%s %s]", class_name, selector);
9165 else
9166 sprintf (result, "-[%s %s]", class_name, selector);
9168 return result;
9171 #endif
9173 /* Demangle function for Objective-C. Attempt to demangle the
9174 function name associated with a method (eg, going from
9175 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9176 purpose of pretty printing or error messages. Return the demangled
9177 name, or NULL if the string is not an Objective-C mangled method
9178 name.
9180 Because of how the mangling is done, any method that has a '_' in
9181 its original name is at risk of being demangled incorrectly. In
9182 some cases there are multiple valid ways to demangle a method name
9183 and there is no way we can decide.
9185 TODO: objc_demangle() can't always get it right; the right way to
9186 get this correct for all method names would be to store the
9187 Objective-C method name somewhere in the function decl. Then,
9188 there is no demangling to do; we'd just pull the method name out of
9189 the decl. As an additional bonus, when printing error messages we
9190 could check for such a method name, and if we find it, we know the
9191 function is actually an Objective-C method and we could print error
9192 messages saying "In method '+[NSObject class]" instead of "In
9193 function '+[NSObject class]" as we do now. */
9194 static const char *
9195 objc_demangle (const char *mangled)
9197 char *demangled, *cp;
9199 /* First of all, if the name is too short it can't be an Objective-C
9200 mangled method name. */
9201 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9202 return NULL;
9204 /* If the name looks like an already demangled one, return it
9205 unchanged. This should only happen on Darwin, where method names
9206 are mangled differently into a pretty-print form (such as
9207 '+[NSObject class]', see darwin.h). In that case, demangling is
9208 a no-op, but we need to return the demangled name if it was an
9209 ObjC one, and return NULL if not. We should be safe as no C/C++
9210 function can start with "-[" or "+[". */
9211 if ((mangled[0] == '-' || mangled[0] == '+')
9212 && (mangled[1] == '['))
9213 return mangled;
9215 if (mangled[0] == '_' &&
9216 (mangled[1] == 'i' || mangled[1] == 'c') &&
9217 mangled[2] == '_')
9219 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9220 if (mangled[1] == 'i')
9221 *cp++ = '-'; /* for instance method */
9222 else
9223 *cp++ = '+'; /* for class method */
9224 *cp++ = '['; /* opening left brace */
9225 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9226 while (*cp && *cp == '_')
9227 cp++; /* skip any initial underbars in class name */
9228 cp = strchr(cp, '_'); /* find first non-initial underbar */
9229 if (cp == NULL)
9231 free(demangled); /* not mangled name */
9232 return NULL;
9234 if (cp[1] == '_') /* easy case: no category name */
9236 *cp++ = ' '; /* replace two '_' with one ' ' */
9237 strcpy(cp, mangled + (cp - demangled) + 2);
9239 else
9241 *cp++ = '('; /* less easy case: category name */
9242 cp = strchr(cp, '_');
9243 if (cp == 0)
9245 free(demangled); /* not mangled name */
9246 return NULL;
9248 *cp++ = ')';
9249 *cp++ = ' '; /* overwriting 1st char of method name... */
9250 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9252 /* Now we have the method name. We need to generally replace
9253 '_' with ':' but trying to preserve '_' if it could only have
9254 been in the mangled string because it was already in the
9255 original name. In cases where it's ambiguous, we assume that
9256 any '_' originated from a ':'. */
9258 /* Initial '_'s in method name can't have been generating by
9259 converting ':'s. Skip them. */
9260 while (*cp && *cp == '_')
9261 cp++;
9263 /* If the method name does not end with '_', then it has no
9264 arguments and there was no replacement of ':'s with '_'s
9265 during mangling. Check for that case, and skip any
9266 replacement if so. This at least guarantees that methods
9267 with no arguments are always demangled correctly (unless the
9268 original name ends with '_'). */
9269 if (*(mangled + strlen (mangled) - 1) != '_')
9271 /* Skip to the end. */
9272 for (; *cp; cp++)
9275 else
9277 /* Replace remaining '_' with ':'. This may get it wrong if
9278 there were '_'s in the original name. In most cases it
9279 is impossible to disambiguate. */
9280 for (; *cp; cp++)
9281 if (*cp == '_')
9282 *cp = ':';
9284 *cp++ = ']'; /* closing right brace */
9285 *cp++ = 0; /* string terminator */
9286 return demangled;
9288 else
9289 return NULL; /* not an objc mangled name */
9292 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9293 specific decl, return the printable name for it. If not, return
9294 NULL. */
9295 const char *
9296 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9298 switch (TREE_CODE (decl))
9300 case FUNCTION_DECL:
9301 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9302 break;
9304 /* The following happens when we are printing a deprecation
9305 warning for a method. The warn_deprecation() will end up
9306 trying to print the decl for INSTANCE_METHOD_DECL or
9307 CLASS_METHOD_DECL. It would be nice to be able to print
9308 "-[NSObject autorelease] is deprecated", but to do that, we'd
9309 need to store the class and method name in the method decl,
9310 which we currently don't do. For now, just return the name
9311 of the method. We don't return NULL, because that may
9312 trigger further attempts to pretty-print the decl in C/C++,
9313 but they wouldn't know how to pretty-print it. */
9314 case INSTANCE_METHOD_DECL:
9315 case CLASS_METHOD_DECL:
9316 return IDENTIFIER_POINTER (DECL_NAME (decl));
9317 break;
9318 /* This happens when printing a deprecation warning for a
9319 property. We may want to consider some sort of pretty
9320 printing (eg, include the class name where it was declared
9321 ?). */
9322 case PROPERTY_DECL:
9323 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9324 break;
9325 default:
9326 return NULL;
9327 break;
9331 /* Return a printable name for 'decl'. This first tries
9332 objc_maybe_printable_name(), and if that fails, it returns the name
9333 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9334 Objective-C; in Objective-C++, setting the hook is not enough
9335 because lots of C++ Front-End code calls cxx_printable_name,
9336 dump_decl and other C++ functions directly. So instead we have
9337 modified dump_decl to call objc_maybe_printable_name directly. */
9338 const char *
9339 objc_printable_name (tree decl, int v)
9341 const char *demangled_name = objc_maybe_printable_name (decl, v);
9343 if (demangled_name != NULL)
9344 return demangled_name;
9345 else
9346 return IDENTIFIER_POINTER (DECL_NAME (decl));
9349 /* Routine is called to issue diagnostic when reference to a private
9350 ivar is made and no other variable with same name is found in
9351 current scope. */
9352 bool
9353 objc_diagnose_private_ivar (tree id)
9355 tree ivar;
9356 if (!objc_method_context)
9357 return false;
9358 ivar = is_ivar (objc_ivar_chain, id);
9359 if (ivar && is_private (ivar))
9361 error ("instance variable %qs is declared private",
9362 IDENTIFIER_POINTER (id));
9363 return true;
9365 return false;
9368 /* Look up ID as an instance variable. OTHER contains the result of
9369 the C or C++ lookup, which we may want to use instead. */
9370 /* To use properties inside an instance method, use self.property. */
9371 tree
9372 objc_lookup_ivar (tree other, tree id)
9374 tree ivar;
9376 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9377 if (!objc_method_context)
9378 return other;
9380 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9381 /* We have a message to super. */
9382 return get_super_receiver ();
9384 /* In a class method, look up an instance variable only as a last
9385 resort. */
9386 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9387 && other && other != error_mark_node)
9388 return other;
9390 /* Don't look up the ivar if the user has explicitly advised against
9391 it with -fno-local-ivars. */
9393 if (!flag_local_ivars)
9394 return other;
9396 /* Look up the ivar, but do not use it if it is not accessible. */
9397 ivar = is_ivar (objc_ivar_chain, id);
9399 if (!ivar || is_private (ivar))
9400 return other;
9402 /* In an instance method, a local variable (or parameter) may hide the
9403 instance variable. */
9404 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9405 && other && other != error_mark_node
9406 #ifdef OBJCPLUS
9407 && CP_DECL_CONTEXT (other) != global_namespace)
9408 #else
9409 && !DECL_FILE_SCOPE_P (other))
9410 #endif
9412 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9413 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9414 "local declaration of %qE hides instance variable", id);
9417 return other;
9420 /* At this point, we are either in an instance method with no obscuring
9421 local definitions, or in a class method with no alternate definitions
9422 at all. */
9423 return build_ivar_reference (id);
9426 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9427 needs to be done if we are calling a function through a cast. */
9429 tree
9430 objc_rewrite_function_call (tree function, tree first_param)
9432 if (TREE_CODE (function) == NOP_EXPR
9433 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9434 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9435 == FUNCTION_DECL)
9437 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9438 TREE_OPERAND (function, 0),
9439 first_param, size_zero_node);
9442 return function;
9445 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9446 corresponding 'getter' function call. Note that we assume the
9447 PROPERTY_REF to be valid since we generated it while parsing. */
9448 static void
9449 objc_gimplify_property_ref (tree *expr_p)
9451 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9452 tree call_exp;
9454 if (getter == NULL_TREE)
9456 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9457 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9458 should be impossible for real properties, which always
9459 have a getter. */
9460 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9461 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9462 /* Try to recover from the error to prevent an ICE. We take
9463 zero and cast it to the type of the property. */
9464 *expr_p = convert (TREE_TYPE (property_decl),
9465 integer_zero_node);
9466 return;
9469 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9471 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9472 that is deprecated. */
9473 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9474 NULL_TREE);
9477 call_exp = getter;
9478 #ifdef OBJCPLUS
9479 /* In C++, a getter which returns an aggregate value results in a
9480 target_expr which initializes a temporary to the call
9481 expression. */
9482 if (TREE_CODE (getter) == TARGET_EXPR)
9484 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9485 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9486 call_exp = TREE_OPERAND (getter, 1);
9488 #endif
9489 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9491 *expr_p = call_exp;
9494 /* This is called when "gimplifying" the trees. We need to gimplify
9495 the Objective-C/Objective-C++ specific trees, then hand over the
9496 process to C/C++. */
9498 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9500 enum tree_code code = TREE_CODE (*expr_p);
9501 switch (code)
9503 /* Look for the special case of OBJC_TYPE_REF with the address
9504 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9505 or one of its cousins). */
9506 case OBJ_TYPE_REF:
9507 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9508 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9509 == FUNCTION_DECL)
9511 enum gimplify_status r0, r1;
9513 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9514 value of the OBJ_TYPE_REF, so force them to be emitted
9515 during subexpression evaluation rather than after the
9516 OBJ_TYPE_REF. This permits objc_msgSend calls in
9517 Objective C to use direct rather than indirect calls when
9518 the object expression has a postincrement. */
9519 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9520 is_gimple_val, fb_rvalue);
9521 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9522 is_gimple_val, fb_rvalue);
9524 return MIN (r0, r1);
9526 break;
9527 case PROPERTY_REF:
9528 objc_gimplify_property_ref (expr_p);
9529 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9530 break;
9531 default:
9532 break;
9535 #ifdef OBJCPLUS
9536 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9537 #else
9538 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9539 #endif
9542 /* --- FAST ENUMERATION --- */
9543 /* Begin code generation for fast enumeration (foreach) ... */
9545 /* Defines
9547 struct __objcFastEnumerationState
9549 unsigned long state;
9550 id *itemsPtr;
9551 unsigned long *mutationsPtr;
9552 unsigned long extra[5];
9555 Confusingly enough, NSFastEnumeration is then defined by libraries
9556 to be the same structure.
9559 static void
9560 build_fast_enumeration_state_template (void)
9562 tree decls, *chain = NULL;
9564 /* { */
9565 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9566 (TAG_FAST_ENUMERATION_STATE));
9568 /* unsigned long state; */
9569 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9571 /* id *itemsPtr; */
9572 add_field_decl (build_pointer_type (objc_object_type),
9573 "itemsPtr", &chain);
9575 /* unsigned long *mutationsPtr; */
9576 add_field_decl (build_pointer_type (long_unsigned_type_node),
9577 "mutationsPtr", &chain);
9579 /* unsigned long extra[5]; */
9580 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9581 "extra", &chain);
9583 /* } */
9584 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9588 'objc_finish_foreach_loop()' generates the code for an Objective-C
9589 foreach loop. The 'location' argument is the location of the 'for'
9590 that starts the loop. The 'object_expression' is the expression of
9591 the 'object' that iterates; the 'collection_expression' is the
9592 expression of the collection that we iterate over (we need to make
9593 sure we evaluate this only once); the 'for_body' is the set of
9594 statements to be executed in each iteration; 'break_label' and
9595 'continue_label' are the break and continue labels which we need to
9596 emit since the <statements> may be jumping to 'break_label' (if they
9597 contain 'break') or to 'continue_label' (if they contain
9598 'continue').
9600 The syntax is
9602 for (<object expression> in <collection expression>)
9603 <statements>
9605 which is compiled into the following blurb:
9608 id __objc_foreach_collection;
9609 __objc_fast_enumeration_state __objc_foreach_enum_state;
9610 unsigned long __objc_foreach_batchsize;
9611 id __objc_foreach_items[16];
9612 __objc_foreach_collection = <collection expression>;
9613 __objc_foreach_enum_state = { 0 };
9614 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9616 if (__objc_foreach_batchsize == 0)
9617 <object expression> = nil;
9618 else
9620 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9621 next_batch:
9623 unsigned long __objc_foreach_index;
9624 __objc_foreach_index = 0;
9626 next_object:
9627 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9628 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9629 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9631 continue_label:
9632 __objc_foreach_index++;
9633 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9634 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9636 if (__objc_foreach_batchsize != 0) goto next_batch;
9637 <object expression> = nil;
9638 break_label:
9642 'statements' may contain a 'continue' or 'break' instruction, which
9643 the user expects to 'continue' or 'break' the entire foreach loop.
9644 We are provided the labels that 'break' and 'continue' jump to, so
9645 we place them where we want them to jump to when they pick them.
9647 Optimization TODO: we could cache the IMP of
9648 countByEnumeratingWithState:objects:count:.
9651 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9652 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9654 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9655 #include "tree-pretty-print.h"
9656 #endif
9658 void
9659 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9660 tree break_label, tree continue_label)
9662 /* A tree representing the __objcFastEnumerationState struct type,
9663 or NSFastEnumerationState struct, whatever we are using. */
9664 tree objc_fast_enumeration_state_type;
9666 /* The trees representing the declarations of each of the local variables. */
9667 tree objc_foreach_collection_decl;
9668 tree objc_foreach_enum_state_decl;
9669 tree objc_foreach_items_decl;
9670 tree objc_foreach_batchsize_decl;
9671 tree objc_foreach_mutations_pointer_decl;
9672 tree objc_foreach_index_decl;
9674 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9675 tree selector_name;
9677 /* A tree representing the local bind. */
9678 tree bind;
9680 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9681 tree first_if;
9683 /* A tree representing the 'else' part of 'first_if' */
9684 tree first_else;
9686 /* A tree representing the 'next_batch' label. */
9687 tree next_batch_label_decl;
9689 /* A tree representing the binding after the 'next_batch' label. */
9690 tree next_batch_bind;
9692 /* A tree representing the 'next_object' label. */
9693 tree next_object_label_decl;
9695 /* Temporary variables. */
9696 tree t;
9697 int i;
9699 if (flag_objc1_only)
9700 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9702 if (object_expression == error_mark_node)
9703 return;
9705 if (collection_expression == error_mark_node)
9706 return;
9708 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9710 error_at (location, "iterating variable in fast enumeration is not an object");
9711 return;
9714 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9716 error_at (location, "collection in fast enumeration is not an object");
9717 return;
9720 /* TODO: Check that object_expression is either a variable
9721 declaration, or an lvalue. */
9723 /* This kludge is an idea from apple. We use the
9724 __objcFastEnumerationState struct implicitly defined by the
9725 compiler, unless a NSFastEnumerationState struct has been defined
9726 (by a Foundation library such as GNUstep Base) in which case, we
9727 use that one.
9729 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9731 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9733 if (objc_NSFastEnumeration_type)
9735 /* TODO: We really need to check that
9736 objc_NSFastEnumeration_type is the same as ours! */
9737 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9739 /* If it's a typedef, use the original type. */
9740 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9741 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9742 else
9743 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9748 /* { */
9749 /* Done by c-parser.c. */
9751 /* type object; */
9752 /* Done by c-parser.c. */
9754 /* Disable warnings that 'object' is unused. For example the code
9756 for (id object in collection)
9757 i++;
9759 which can be used to count how many objects there are in the
9760 collection is fine and should generate no warnings even if
9761 'object' is technically unused. */
9762 TREE_USED (object_expression) = 1;
9763 if (DECL_P (object_expression))
9764 DECL_READ_P (object_expression) = 1;
9766 /* id __objc_foreach_collection */
9767 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9769 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9770 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9771 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9773 /* id __objc_foreach_items[16]; */
9774 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9775 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9777 /* unsigned long __objc_foreach_batchsize; */
9778 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9779 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9781 /* Generate the local variable binding. */
9782 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9783 SET_EXPR_LOCATION (bind, location);
9784 TREE_SIDE_EFFECTS (bind) = 1;
9786 /* __objc_foreach_collection = <collection expression>; */
9787 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9788 SET_EXPR_LOCATION (t, location);
9789 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9790 /* We have used 'collection_expression'. */
9791 mark_exp_read (collection_expression);
9793 /* __objc_foreach_enum_state.state = 0; */
9794 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9795 get_identifier ("state")),
9796 build_int_cst (long_unsigned_type_node, 0));
9797 SET_EXPR_LOCATION (t, location);
9798 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9800 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9801 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9802 get_identifier ("itemsPtr")),
9803 null_pointer_node);
9804 SET_EXPR_LOCATION (t, location);
9805 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9807 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9808 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9809 get_identifier ("mutationsPtr")),
9810 null_pointer_node);
9811 SET_EXPR_LOCATION (t, location);
9812 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9814 /* __objc_foreach_enum_state.extra[0] = 0; */
9815 /* __objc_foreach_enum_state.extra[1] = 0; */
9816 /* __objc_foreach_enum_state.extra[2] = 0; */
9817 /* __objc_foreach_enum_state.extra[3] = 0; */
9818 /* __objc_foreach_enum_state.extra[4] = 0; */
9819 for (i = 0; i < 5 ; i++)
9821 t = build2 (MODIFY_EXPR, void_type_node,
9822 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9823 get_identifier ("extra")),
9824 build_int_cst (NULL_TREE, i)),
9825 build_int_cst (long_unsigned_type_node, 0));
9826 SET_EXPR_LOCATION (t, location);
9827 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9830 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9831 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9832 #ifdef OBJCPLUS
9833 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9834 /* Parameters. */
9835 tree_cons /* &__objc_foreach_enum_state */
9836 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9837 tree_cons /* __objc_foreach_items */
9838 (NULL_TREE, objc_foreach_items_decl,
9839 tree_cons /* 16 */
9840 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9841 #else
9842 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9844 struct c_expr array;
9845 array.value = objc_foreach_items_decl;
9846 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9847 /* Parameters. */
9848 tree_cons /* &__objc_foreach_enum_state */
9849 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9850 tree_cons /* __objc_foreach_items */
9851 (NULL_TREE, default_function_array_conversion (location, array).value,
9852 tree_cons /* 16 */
9853 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9855 #endif
9856 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9857 convert (long_unsigned_type_node, t));
9858 SET_EXPR_LOCATION (t, location);
9859 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9861 /* if (__objc_foreach_batchsize == 0) */
9862 first_if = build3 (COND_EXPR, void_type_node,
9863 /* Condition. */
9864 c_fully_fold
9865 (c_common_truthvalue_conversion
9866 (location,
9867 build_binary_op (location,
9868 EQ_EXPR,
9869 objc_foreach_batchsize_decl,
9870 build_int_cst (long_unsigned_type_node, 0), 1)),
9871 false, NULL),
9872 /* Then block (we fill it in later). */
9873 NULL_TREE,
9874 /* Else block (we fill it in later). */
9875 NULL_TREE);
9876 SET_EXPR_LOCATION (first_if, location);
9877 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9879 /* then <object expression> = nil; */
9880 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9881 SET_EXPR_LOCATION (t, location);
9882 COND_EXPR_THEN (first_if) = t;
9884 /* Now we build the 'else' part of the if; once we finish building
9885 it, we attach it to first_if as the 'else' part. */
9887 /* else */
9888 /* { */
9890 /* unsigned long __objc_foreach_mutations_pointer; */
9891 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
9893 /* Generate the local variable binding. */
9894 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9895 SET_EXPR_LOCATION (first_else, location);
9896 TREE_SIDE_EFFECTS (first_else) = 1;
9898 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9899 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9900 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9901 get_identifier ("mutationsPtr")),
9902 RO_UNARY_STAR));
9903 SET_EXPR_LOCATION (t, location);
9904 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9906 /* next_batch: */
9907 next_batch_label_decl = create_artificial_label (location);
9908 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
9909 SET_EXPR_LOCATION (t, location);
9910 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9912 /* { */
9914 /* unsigned long __objc_foreach_index; */
9915 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
9917 /* Generate the local variable binding. */
9918 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9919 SET_EXPR_LOCATION (next_batch_bind, location);
9920 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9921 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
9923 /* __objc_foreach_index = 0; */
9924 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9925 build_int_cst (long_unsigned_type_node, 0));
9926 SET_EXPR_LOCATION (t, location);
9927 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9929 /* next_object: */
9930 next_object_label_decl = create_artificial_label (location);
9931 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9932 SET_EXPR_LOCATION (t, location);
9933 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9935 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9936 t = build3 (COND_EXPR, void_type_node,
9937 /* Condition. */
9938 c_fully_fold
9939 (c_common_truthvalue_conversion
9940 (location,
9941 build_binary_op
9942 (location,
9943 NE_EXPR,
9944 objc_foreach_mutations_pointer_decl,
9945 build_indirect_ref (location,
9946 objc_build_component_ref (objc_foreach_enum_state_decl,
9947 get_identifier ("mutationsPtr")),
9948 RO_UNARY_STAR), 1)),
9949 false, NULL),
9950 /* Then block. */
9951 build_function_call (input_location,
9952 objc_enumeration_mutation_decl,
9953 tree_cons (NULL, collection_expression, NULL)),
9954 /* Else block. */
9955 NULL_TREE);
9956 SET_EXPR_LOCATION (t, location);
9957 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9959 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
9960 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9961 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9962 get_identifier ("itemsPtr")),
9963 objc_foreach_index_decl));
9964 SET_EXPR_LOCATION (t, location);
9965 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9967 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9968 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
9970 /* continue_label: */
9971 if (continue_label)
9973 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9974 SET_EXPR_LOCATION (t, location);
9975 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9978 /* __objc_foreach_index++; */
9979 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9980 build_binary_op (location,
9981 PLUS_EXPR,
9982 objc_foreach_index_decl,
9983 build_int_cst (long_unsigned_type_node, 1), 1));
9984 SET_EXPR_LOCATION (t, location);
9985 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9987 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
9988 t = build3 (COND_EXPR, void_type_node,
9989 /* Condition. */
9990 c_fully_fold
9991 (c_common_truthvalue_conversion
9992 (location,
9993 build_binary_op (location,
9994 LT_EXPR,
9995 objc_foreach_index_decl,
9996 objc_foreach_batchsize_decl, 1)),
9997 false, NULL),
9998 /* Then block. */
9999 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
10000 /* Else block. */
10001 NULL_TREE);
10002 SET_EXPR_LOCATION (t, location);
10003 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10005 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
10006 #ifdef OBJCPLUS
10007 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10008 /* Parameters. */
10009 tree_cons /* &__objc_foreach_enum_state */
10010 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10011 tree_cons /* __objc_foreach_items */
10012 (NULL_TREE, objc_foreach_items_decl,
10013 tree_cons /* 16 */
10014 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10015 #else
10016 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10018 struct c_expr array;
10019 array.value = objc_foreach_items_decl;
10020 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10021 /* Parameters. */
10022 tree_cons /* &__objc_foreach_enum_state */
10023 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10024 tree_cons /* __objc_foreach_items */
10025 (NULL_TREE, default_function_array_conversion (location, array).value,
10026 tree_cons /* 16 */
10027 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10029 #endif
10030 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
10031 convert (long_unsigned_type_node, t));
10032 SET_EXPR_LOCATION (t, location);
10033 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10035 /* } */
10037 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10038 t = build3 (COND_EXPR, void_type_node,
10039 /* Condition. */
10040 c_fully_fold
10041 (c_common_truthvalue_conversion
10042 (location,
10043 build_binary_op (location,
10044 NE_EXPR,
10045 objc_foreach_batchsize_decl,
10046 build_int_cst (long_unsigned_type_node, 0), 1)),
10047 false, NULL),
10048 /* Then block. */
10049 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10050 /* Else block. */
10051 NULL_TREE);
10052 SET_EXPR_LOCATION (t, location);
10053 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10055 /* <object expression> = nil; */
10056 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10057 SET_EXPR_LOCATION (t, location);
10058 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10060 /* break_label: */
10061 if (break_label)
10063 t = build1 (LABEL_EXPR, void_type_node, break_label);
10064 SET_EXPR_LOCATION (t, location);
10065 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10068 /* } */
10069 COND_EXPR_ELSE (first_if) = first_else;
10071 /* Do the whole thing. */
10072 add_stmt (bind);
10074 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10075 /* This will print to stderr the whole blurb generated by the
10076 compiler while compiling (assuming the compiler doesn't crash
10077 before getting here).
10079 debug_generic_stmt (bind);
10080 #endif
10082 /* } */
10083 /* Done by c-parser.c */
10086 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10087 /* Return true if we have an NxString object pointer. */
10089 bool
10090 objc_string_ref_type_p (tree strp)
10092 tree tmv;
10093 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10094 return false;
10096 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10097 tmv = OBJC_TYPE_NAME (tmv);
10098 return (tmv
10099 && TREE_CODE (tmv) == IDENTIFIER_NODE
10100 && IDENTIFIER_POINTER (tmv)
10101 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10104 /* At present the behavior of this is undefined and it does nothing. */
10105 void
10106 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10107 tree ARG_UNUSED (args_list))
10111 void
10112 objc_common_init_ts (void)
10114 c_common_init_ts ();
10116 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10117 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10118 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10119 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10121 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10122 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10123 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10125 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10126 MARK_TS_TYPED (PROPERTY_REF);
10129 size_t
10130 objc_common_tree_size (enum tree_code code)
10132 switch (code)
10134 case CLASS_METHOD_DECL:
10135 case INSTANCE_METHOD_DECL:
10136 case KEYWORD_DECL:
10137 case PROPERTY_DECL:
10138 return sizeof (struct tree_decl_non_common);
10139 default:
10140 gcc_unreachable ();
10146 #include "gt-objc-objc-act.h"