Re: Refactor copying decl section names
[official-gcc.git] / gcc / objc / objc-act.c
blobb9ed32d51d07ea1d421d49f66dc1b0b9cc10cc1f
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992-2020 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-objc.h"
38 #include "langhooks.h"
39 #include "objc-act.h"
40 #include "objc-map.h"
41 #include "function.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-target.h"
45 #include "intl.h"
46 #include "cgraph.h"
47 #include "tree-iterator.h"
48 /* Different initialization, code gen and meta data generation for each
49 runtime. */
50 #include "objc-runtime-hooks.h"
51 /* Routines used mainly by the runtimes. */
52 #include "objc-runtime-shared-support.h"
53 /* For default_tree_printer (). */
55 /* For enum gimplify_status */
56 #include "gimple-expr.h"
57 #include "gimplify.h"
59 /* For encode_method_prototype(). */
60 #include "objc-encoding.h"
62 static unsigned int should_call_super_dealloc = 0;
64 /* When building Objective-C++, we are not linking against the C front-end
65 and so need to replicate the C tree-construction functions in some way. */
66 #ifdef OBJCPLUS
67 #define OBJCP_REMAP_FUNCTIONS
68 #include "objcp-decl.h"
69 #endif /* OBJCPLUS */
71 /* This is the default way of generating a method name. */
72 /* This has the problem that "test_method:argument:" and
73 "test:method_argument:" will generate the same name
74 ("_i_Test__test_method_argument_" for an instance method of the
75 class "Test"), so you can't have them both in the same class!
76 Moreover, the demangling (going from
77 "_i_Test__test_method_argument" back to the original name) is
78 undefined because there are two correct ways of demangling the
79 name. */
80 #ifndef OBJC_GEN_METHOD_LABEL
81 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
82 do { \
83 char *temp; \
84 sprintf ((BUF), "_%s_%s_%s_%s", \
85 ((IS_INST) ? "i" : "c"), \
86 (CLASS_NAME), \
87 ((CAT_NAME)? (CAT_NAME) : ""), \
88 (SEL_NAME)); \
89 for (temp = (BUF); *temp; temp++) \
90 if (*temp == ':') *temp = '_'; \
91 } while (0)
92 #endif
94 /* These need specifying. */
95 #ifndef OBJC_FORWARDING_STACK_OFFSET
96 #define OBJC_FORWARDING_STACK_OFFSET 0
97 #endif
99 #ifndef OBJC_FORWARDING_MIN_OFFSET
100 #define OBJC_FORWARDING_MIN_OFFSET 0
101 #endif
103 /*** Private Interface (procedures) ***/
105 /* Init stuff. */
106 static void synth_module_prologue (void);
108 /* Code generation. */
110 static tree start_class (enum tree_code, tree, tree, tree, tree);
111 static tree continue_class (tree);
112 static void finish_class (tree);
113 static void start_method_def (tree, tree);
115 static tree start_protocol (enum tree_code, tree, tree, tree);
116 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
117 static tree objc_add_method (tree, tree, int, bool);
118 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
119 static tree build_ivar_reference (tree);
120 static tree is_ivar (tree, tree);
122 /* We only need the following for ObjC; ObjC++ will use C++'s definition
123 of DERIVED_FROM_P. */
124 #ifndef OBJCPLUS
125 static bool objc_derived_from_p (tree, tree);
126 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
127 #endif
129 /* Property. */
130 static void objc_gen_property_data (tree, tree);
131 static void objc_synthesize_getter (tree, tree, tree);
132 static void objc_synthesize_setter (tree, tree, tree);
133 static tree lookup_property (tree, tree);
134 static tree lookup_property_in_list (tree, tree);
135 static tree lookup_property_in_protocol_list (tree, tree);
136 static void build_common_objc_property_accessor_helpers (void);
138 static void objc_xref_basetypes (tree, tree);
140 static tree get_class_ivars (tree, bool);
142 static void build_fast_enumeration_state_template (void);
144 #ifdef OBJCPLUS
145 static void objc_generate_cxx_cdtors (void);
146 #endif
148 /* objc attribute */
149 static void objc_decl_method_attributes (tree*, tree, int);
150 static tree build_keyword_selector (tree);
152 static void hash_init (void);
154 /* Hash tables to manage the global pool of method prototypes. Each
155 of these maps map a method name (selector) identifier to either a
156 single tree (for methods with a single method prototype) or a
157 TREE_VEC (for methods with multiple method prototypes). */
158 static GTY(()) objc_map_t instance_method_map = 0;
159 static GTY(()) objc_map_t class_method_map = 0;
161 /* Hash tables to manage the global pool of class names. */
163 static GTY(()) objc_map_t class_name_map = 0;
164 static GTY(()) objc_map_t alias_name_map = 0;
166 static tree lookup_method (tree, tree);
167 static tree lookup_method_static (tree, tree, int);
169 static void interface_hash_init (void);
170 static tree add_interface (tree, tree);
171 static void add_category (tree, tree);
172 static inline tree lookup_category (tree, tree);
174 /* Protocols. */
176 static tree lookup_protocol (tree, bool, bool);
177 static tree lookup_and_install_protocols (tree, bool);
179 #ifdef OBJCPLUS
180 static void really_start_method (tree, tree);
181 #else
182 static void really_start_method (tree, struct c_arg_info *);
183 #endif
184 static int comp_proto_with_proto (tree, tree, int);
185 static tree objc_decay_parm_type (tree);
187 /* Utilities for debugging and error diagnostics. */
189 static char *gen_type_name (tree);
190 static char *gen_type_name_0 (tree);
191 static char *gen_method_decl (tree);
192 static char *gen_declaration (tree);
194 /* Everything else. */
196 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
198 static void mark_referenced_methods (void);
199 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
200 static tree check_duplicates (tree, int, int);
202 /*** Private Interface (data) ***/
203 /* Flags for lookup_method_static(). */
205 /* Look for class methods. */
206 #define OBJC_LOOKUP_CLASS 1
207 /* Do not examine superclasses. */
208 #define OBJC_LOOKUP_NO_SUPER 2
209 /* Disable returning an instance method of a root class when a class
210 method can't be found. */
211 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
213 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
214 tree objc_global_trees[OCTI_MAX];
216 struct imp_entry *imp_list = 0;
217 int imp_count = 0; /* `@implementation' */
218 int cat_count = 0; /* `@category' */
220 objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
222 /* Use to generate method labels. */
223 static int method_slot = 0;
225 /* Flag to say whether methods in a protocol are optional or
226 required. */
227 static bool objc_method_optional_flag = false;
229 static int objc_collecting_ivars = 0;
231 /* Flag that is set to 'true' while we are processing a class
232 extension. Since a class extension just "reopens" the main
233 @interface, this can be used to determine if we are in the main
234 @interface, or in a class extension. */
235 static bool objc_in_class_extension = false;
237 static char *errbuf; /* Buffer for error diagnostics */
239 /* An array of all the local variables in the current function that
240 need to be marked as volatile. */
241 vec<tree, va_gc> *local_variables_to_volatilize = NULL;
243 /* Store all constructed constant strings in a hash table so that
244 they get uniqued properly. */
246 struct GTY((for_user)) string_descriptor {
247 /* The literal argument . */
248 tree literal;
250 /* The resulting constant string. */
251 tree constructor;
254 struct objc_string_hasher : ggc_ptr_hash<string_descriptor>
256 static hashval_t hash (string_descriptor *);
257 static bool equal (string_descriptor *, string_descriptor *);
260 static GTY(()) hash_table<objc_string_hasher> *string_htab;
262 FILE *gen_declaration_file;
264 /* Hooks for stuff that differs between runtimes. */
265 objc_runtime_hooks runtime;
267 /* Create a temporary variable of type 'type'. If 'name' is set, uses
268 the specified name, else use no name. Returns the declaration of
269 the type. The 'name' is mostly useful for debugging.
271 tree
272 objc_create_temporary_var (tree type, const char *name)
274 tree decl;
276 if (name != NULL)
278 decl = build_decl (input_location,
279 VAR_DECL, get_identifier (name), type);
281 else
283 decl = build_decl (input_location,
284 VAR_DECL, NULL_TREE, type);
286 TREE_USED (decl) = 1;
287 DECL_ARTIFICIAL (decl) = 1;
288 DECL_IGNORED_P (decl) = 1;
289 DECL_CONTEXT (decl) = current_function_decl;
291 return decl;
294 /* Some platforms pass small structures through registers versus
295 through an invisible pointer. Determine at what size structure is
296 the transition point between the two possibilities. */
298 static void
299 generate_struct_by_value_array (void)
301 tree type;
302 tree decls;
303 int i, j;
304 int aggregate_in_mem[32];
305 int found = 0;
307 /* Presumably no platform passes 32 byte structures in a register. */
308 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
309 in registers. */
310 for (i = 1; i < 32; i++)
312 char buffer[5];
313 tree *chain = NULL;
315 /* Create an unnamed struct that has `i' character components */
316 type = objc_start_struct (NULL_TREE);
318 strcpy (buffer, "c1");
319 decls = add_field_decl (char_type_node, buffer, &chain);
321 for (j = 1; j < i; j++)
323 sprintf (buffer, "c%d", j + 1);
324 add_field_decl (char_type_node, buffer, &chain);
326 objc_finish_struct (type, decls);
328 aggregate_in_mem[i] = aggregate_value_p (type, 0);
329 if (!aggregate_in_mem[i])
330 found = 1;
333 /* We found some structures that are returned in registers instead of memory
334 so output the necessary data. */
335 if (found)
337 for (i = 31; i >= 0; i--)
338 if (!aggregate_in_mem[i])
339 break;
340 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
343 exit (0);
346 bool
347 objc_init (void)
349 bool ok;
350 #ifdef OBJCPLUS
351 if (cxx_init () == false)
352 #else
353 if (c_objc_common_init () == false)
354 #endif
355 return false;
357 /* print_struct_values is triggered by -print-runtime-info (used
358 when building libobjc, with an empty file as input). It does not
359 require any ObjC setup, and it never returns.
361 -fcompare-debug is used to check the compiler output; we are
362 executed twice, once with flag_compare_debug set, and once with
363 it not set. If the flag is used together with
364 -print-runtime-info, we want to print the runtime info only once,
365 else it would be output in duplicate. So we check
366 flag_compare_debug to output it in only one of the invocations.
368 As a side effect, this also that means -fcompare-debug
369 -print-runtime-info will run the compiler twice, and compare the
370 generated assembler file; the first time the compiler exits
371 immediately (producing no file), and the second time it compiles
372 an empty file. This checks, as a side effect, that compiling an
373 empty file produces no assembler output. */
374 if (print_struct_values && !flag_compare_debug)
375 generate_struct_by_value_array ();
377 /* Set up stuff used by FE parser and all runtimes. */
378 errbuf = XNEWVEC (char, 1024 * 10);
379 interface_hash_init ();
380 hash_init ();
381 objc_encoding_init ();
382 /* ... and then check flags and set-up for the selected runtime ... */
383 if (flag_next_runtime && flag_objc_abi >= 2)
384 ok = objc_next_runtime_abi_02_init (&runtime);
385 else if (flag_next_runtime)
386 ok = objc_next_runtime_abi_01_init (&runtime);
387 else
388 ok = objc_gnu_runtime_abi_01_init (&runtime);
390 /* If that part of the setup failed - bail out immediately. */
391 if (!ok)
392 return false;
394 /* Determine the default visibility for instance variables. */
395 switch (default_ivar_visibility)
397 case IVAR_VISIBILITY_PRIVATE:
398 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
399 break;
400 case IVAR_VISIBILITY_PUBLIC:
401 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
402 break;
403 case IVAR_VISIBILITY_PACKAGE:
404 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
405 break;
406 default:
407 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
410 /* Generate general types and push runtime-specific decls to file scope. */
411 synth_module_prologue ();
413 return true;
416 /* This is called at the end of parsing by the C/C++ parsers. */
417 void
418 objc_write_global_declarations (void)
420 mark_referenced_methods ();
422 /* A missing @end might not be detected by the parser. */
423 if (objc_implementation_context)
425 warning (0, "%<@end%> missing in implementation context");
426 finish_class (objc_implementation_context);
427 objc_ivar_chain = NULL_TREE;
428 objc_implementation_context = NULL_TREE;
431 if (warn_selector)
433 objc_map_iterator_t i;
435 objc_map_iterator_initialize (class_method_map, &i);
436 while (objc_map_iterator_move_to_next (class_method_map, &i))
437 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
439 objc_map_iterator_initialize (instance_method_map, &i);
440 while (objc_map_iterator_move_to_next (instance_method_map, &i))
441 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
444 /* TODO: consider an early exit here if either errorcount or sorrycount
445 is non-zero. Not only is it wasting time to generate the metadata,
446 it needlessly imposes need to re-check for things that are already
447 determined to be errors. */
449 /* Finalize Objective-C runtime data. No need to generate tables
450 and code if only checking syntax, or if generating a PCH file. */
451 if (!flag_syntax_only && !pch_file)
453 location_t saved_location;
455 /* If gen_declaration desired, open the output file. */
456 if (flag_gen_declaration)
458 char * const dumpname = concat (dump_base_name, ".decl", NULL);
459 gen_declaration_file = fopen (dumpname, "w");
460 if (gen_declaration_file == 0)
461 fatal_error (input_location, "cannot open %s: %m", dumpname);
462 free (dumpname);
465 /* Set the input location to BUILTINS_LOCATION. This is good
466 for error messages, in case any is generated while producing
467 the metadata, but it also silences warnings that would be
468 produced when compiling with -Wpadded in case when padding is
469 automatically added to the built-in runtime data structure
470 declarations. We know about this padding, and it is fine; we
471 don't want users to see any warnings about it if they use
472 -Wpadded. */
473 saved_location = input_location;
474 input_location = BUILTINS_LOCATION;
476 /* Compute and emit the meta-data tables for this runtime. */
477 (*runtime.generate_metadata) ();
479 /* Restore the original location, just in case it mattered. */
480 input_location = saved_location;
482 /* ... and then close any declaration file we opened. */
483 if (gen_declaration_file)
484 fclose (gen_declaration_file);
488 /* Return the first occurrence of a method declaration corresponding
489 to sel_name in rproto_list. Search rproto_list recursively.
490 If is_class is 0, search for instance methods, otherwise for class
491 methods. */
492 static tree
493 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
494 int is_class)
496 tree rproto, p, m;
498 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
500 p = TREE_VALUE (rproto);
501 m = NULL_TREE;
503 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
505 /* First, search the @required protocol methods. */
506 if (is_class)
507 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
508 else
509 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
511 if (m)
512 return m;
514 /* If still not found, search the @optional protocol methods. */
515 if (is_class)
516 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
517 else
518 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
520 if (m)
521 return m;
523 /* If still not found, search the attached protocols. */
524 if (PROTOCOL_LIST (p))
525 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
526 sel_name, is_class);
527 if (m)
528 return m;
530 else
532 ; /* An identifier...if we could not find a protocol. */
536 return 0;
539 static tree
540 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
542 tree rproto, p;
544 /* Make sure the protocol is supported by the object on the rhs. */
545 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
547 tree fnd = 0;
548 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
550 p = TREE_VALUE (rproto);
552 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
554 if (lproto == p)
555 fnd = lproto;
557 else if (PROTOCOL_LIST (p))
558 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
561 if (fnd)
562 return fnd;
565 else
567 ; /* An identifier...if we could not find a protocol. */
570 return 0;
573 void
574 objc_start_class_interface (tree klass, tree super_class,
575 tree protos, tree attributes)
577 if (flag_objc1_only && attributes)
578 error_at (input_location, "class attributes are not available in Objective-C 1.0");
580 objc_interface_context
581 = objc_ivar_context
582 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
583 objc_ivar_visibility = objc_default_ivar_visibility;
586 void
587 objc_start_category_interface (tree klass, tree categ,
588 tree protos, tree attributes)
590 if (attributes)
592 if (flag_objc1_only)
593 error_at (input_location, "category attributes are not available in Objective-C 1.0");
594 else
595 warning_at (input_location, OPT_Wattributes,
596 "category attributes are not available in this version"
597 " of the compiler, (ignored)");
599 if (categ == NULL_TREE)
601 if (flag_objc1_only)
602 error_at (input_location, "class extensions are not available in Objective-C 1.0");
603 else
605 /* Iterate over all the classes and categories implemented
606 up to now in this compilation unit. */
607 struct imp_entry *t;
609 for (t = imp_list; t; t = t->next)
611 /* If we find a class @implementation with the same name
612 as the one we are extending, produce an error. */
613 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
614 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
615 error_at (input_location,
616 "class extension for class %qE declared after its %<@implementation%>",
617 klass);
621 objc_interface_context
622 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
623 objc_ivar_chain
624 = continue_class (objc_interface_context);
627 void
628 objc_start_protocol (tree name, tree protos, tree attributes)
630 if (flag_objc1_only && attributes)
631 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
633 objc_interface_context
634 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
635 objc_method_optional_flag = false;
638 void
639 objc_continue_interface (void)
641 objc_ivar_chain
642 = continue_class (objc_interface_context);
645 void
646 objc_finish_interface (void)
648 finish_class (objc_interface_context);
649 objc_interface_context = NULL_TREE;
650 objc_method_optional_flag = false;
651 objc_in_class_extension = false;
654 void
655 objc_start_class_implementation (tree klass, tree super_class)
657 objc_implementation_context
658 = objc_ivar_context
659 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
660 NULL_TREE);
661 objc_ivar_visibility = objc_default_ivar_visibility;
664 void
665 objc_start_category_implementation (tree klass, tree categ)
667 objc_implementation_context
668 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
669 NULL_TREE);
670 objc_ivar_chain
671 = continue_class (objc_implementation_context);
674 void
675 objc_continue_implementation (void)
677 objc_ivar_chain
678 = continue_class (objc_implementation_context);
681 void
682 objc_finish_implementation (void)
684 #ifdef OBJCPLUS
685 if (flag_objc_call_cxx_cdtors)
686 objc_generate_cxx_cdtors ();
687 #endif
689 if (objc_implementation_context)
691 finish_class (objc_implementation_context);
692 objc_ivar_chain = NULL_TREE;
693 objc_implementation_context = NULL_TREE;
695 else
696 warning (0, "%<@end%> must appear in an @implementation context");
699 void
700 objc_set_visibility (objc_ivar_visibility_kind visibility)
702 if (visibility == OBJC_IVAR_VIS_PACKAGE)
704 if (flag_objc1_only)
705 error ("%<@package%> is not available in Objective-C 1.0");
706 else
707 warning (0, "%<@package%> presently has the same effect as %<@public%>");
709 objc_ivar_visibility = visibility;
712 void
713 objc_set_method_opt (bool optional)
715 if (flag_objc1_only)
717 if (optional)
718 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
719 else
720 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
723 objc_method_optional_flag = optional;
724 if (!objc_interface_context
725 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
727 if (optional)
728 error ("%<@optional%> is allowed in @protocol context only");
729 else
730 error ("%<@required%> is allowed in @protocol context only");
731 objc_method_optional_flag = false;
735 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
736 PROTOCOL. */
737 static tree
738 lookup_property_in_list (tree chain, tree property)
740 tree x;
741 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
742 if (PROPERTY_NAME (x) == property)
743 return x;
744 return NULL_TREE;
747 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
748 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
750 tree rproto, x;
751 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
753 tree p = TREE_VALUE (rproto);
754 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
756 if ((x = lookup_property_in_list (p, property)))
757 return x;
758 if (PROTOCOL_LIST (p))
759 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
761 else
763 ; /* An identifier...if we could not find a protocol. */
766 return NULL_TREE;
769 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
770 chain of interface hierarchy. */
771 static tree
772 lookup_property (tree interface_type, tree property)
774 tree inter = interface_type;
775 while (inter)
777 tree x, category;
778 if ((x = lookup_property_in_list (inter, property)))
779 return x;
780 /* Failing that, look for the property in each category of the class. */
781 category = inter;
782 while ((category = CLASS_CATEGORY_LIST (category)))
784 if ((x = lookup_property_in_list (category, property)))
785 return x;
787 /* When checking a category, also check the protocols
788 attached with the category itself. */
789 if (CLASS_PROTOCOL_LIST (category)
790 && (x = lookup_property_in_protocol_list
791 (CLASS_PROTOCOL_LIST (category), property)))
792 return x;
795 /* Failing to find in categories, look for property in protocol list. */
796 if (CLASS_PROTOCOL_LIST (inter)
797 && (x = lookup_property_in_protocol_list
798 (CLASS_PROTOCOL_LIST (inter), property)))
799 return x;
801 /* Failing that, climb up the inheritance hierarchy. */
802 inter = lookup_interface (CLASS_SUPER_NAME (inter));
804 return inter;
807 /* This routine returns a PROPERTY_KIND for the front end RID code supplied. */
809 enum objc_property_attribute_kind
810 objc_prop_attr_kind_for_rid (enum rid prop_rid)
812 switch (prop_rid)
814 default: return OBJC_PROPERTY_ATTR_UNKNOWN;
815 case RID_GETTER: return OBJC_PROPERTY_ATTR_GETTER;
816 case RID_SETTER: return OBJC_PROPERTY_ATTR_SETTER;
818 case RID_READONLY: return OBJC_PROPERTY_ATTR_READONLY;
819 case RID_READWRITE: return OBJC_PROPERTY_ATTR_READWRITE;
821 case RID_ASSIGN: return OBJC_PROPERTY_ATTR_ASSIGN;
822 case RID_RETAIN: return OBJC_PROPERTY_ATTR_RETAIN;
823 case RID_COPY: return OBJC_PROPERTY_ATTR_COPY;
825 case RID_PROPATOMIC: return OBJC_PROPERTY_ATTR_ATOMIC;
826 case RID_NONATOMIC: return OBJC_PROPERTY_ATTR_NONATOMIC;
828 case RID_CLASS: return OBJC_PROPERTY_ATTR_CLASS;
832 /* This routine is called by the parser when a
833 @property... declaration is found. 'decl' is the declaration of
834 the property (type/identifier), and the other arguments represent
835 property attributes that may have been specified in the Objective-C
836 declaration. 'parsed_property_readonly' is 'true' if the attribute
837 'readonly' was specified, and 'false' if not; similarly for the
838 other bool parameters. 'property_getter_ident' is NULL_TREE
839 if the attribute 'getter' was not specified, and is the identifier
840 corresponding to the specified getter if it was; similarly for
841 'property_setter_ident'. */
842 void
843 objc_add_property_declaration (location_t location, tree decl,
844 vec<property_attribute_info *>& prop_attr_list)
846 if (flag_objc1_only)
847 /* FIXME: we probably ought to bail out at this point. */
848 error_at (location, "%<@property%> is not available in Objective-C 1.0");
850 /* We must be in an interface, category, or protocol. */
851 if (!objc_interface_context)
853 error_at (location, "property declaration not in %<@interface%>,"
854 " %<@protocol%> or %<category%> context");
855 return;
858 /* Do some spot-checks for the most obvious invalid cases. */
860 gcc_checking_assert (decl && TREE_CODE (decl) == FIELD_DECL);
862 if (decl && !DECL_NAME (decl))
864 error_at (location, "properties must be named");
865 return;
868 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
869 decl_loc = make_location (decl_loc, location, decl_loc);
870 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
872 error_at (decl_loc, "property cannot be an array");
873 return;
876 if (DECL_C_BIT_FIELD (decl))
878 /* A @property is not an actual variable, but it is a way to
879 describe a pair of accessor methods, so its type (which is
880 the type of the return value of the getter and the first
881 argument of the setter) can't be a bitfield (as return values
882 and arguments of functions cannot be bitfields). The
883 underlying instance variable could be a bitfield, but that is
884 a different matter. */
885 error_at (decl_loc, "property cannot be a bit-field");
886 return;
889 /* The final results of parsing the (growing number) of property
890 attributes. */
891 property_attribute_info *attrs[OBJC_PROPATTR_GROUP_MAX] = { nullptr };
893 tree property_getter_ident = NULL_TREE;
894 tree property_setter_ident = NULL_TREE;
895 for (unsigned pn = 0; pn < prop_attr_list.length (); ++pn)
897 if (prop_attr_list[pn]->parse_error)
898 continue; /* Ignore attributes known to be wrongly parsed. */
900 switch (int g = (int) prop_attr_list[pn]->group())
902 case OBJC_PROPATTR_GROUP_UNKNOWN:
903 continue;
904 case OBJC_PROPATTR_GROUP_SETTER:
905 case OBJC_PROPATTR_GROUP_GETTER:
906 if (attrs[g])
908 warning_at (prop_attr_list[pn]->prop_loc, OPT_Wattributes,
909 "multiple property %qE methods specified, the latest"
910 " one will be used", attrs[g]->name);
911 inform (attrs[g]->prop_loc, "previous specification");
913 attrs[g] = prop_attr_list[pn];
914 if (g == OBJC_PROPATTR_GROUP_SETTER)
915 property_setter_ident = attrs[g]->ident;
916 else
917 property_getter_ident = attrs[g]->ident;
918 continue;
919 default:
921 if (!attrs[g])
923 else if (attrs[g]->prop_kind != prop_attr_list[pn]->prop_kind)
925 error_at (prop_attr_list[pn]->prop_loc,
926 "%qE attribute conflicts with %qE attribute",
927 prop_attr_list[pn]->name, attrs[g]->name);
928 inform (attrs[g]->prop_loc, "%qE specified here",
929 attrs[g]->name );
931 else
933 warning_at (prop_attr_list[pn]->prop_loc, OPT_Wattributes,
934 "duplicate %qE attribute", attrs[g]->name);
935 inform (attrs[g]->prop_loc, "first specified here");
937 attrs[g] = prop_attr_list[pn];
939 continue;
943 /* The defaults for atomicity (atomic) and write-ability (readwrite) apply
944 even if the user provides no specified attributes. */
945 bool property_nonatomic = false;
946 bool property_readonly = false;
948 /* Set the values from any specified by the user; these are easy, only two
949 states. */
950 if (attrs[OBJC_PROPATTR_GROUP_ATOMIC])
951 property_nonatomic = attrs[OBJC_PROPATTR_GROUP_ATOMIC]->prop_kind
952 == OBJC_PROPERTY_ATTR_NONATOMIC;
954 if (attrs[OBJC_PROPATTR_GROUP_READWRITE])
955 property_readonly = attrs[OBJC_PROPATTR_GROUP_READWRITE]->prop_kind
956 == OBJC_PROPERTY_ATTR_READONLY;
958 /* One can't set a readonly value; we issue an error, but force the property
959 to readwrite as well. */
960 if (property_readonly && property_setter_ident)
962 error_at (attrs[OBJC_PROPATTR_GROUP_READWRITE]->prop_loc, "%<readonly%>"
963 " attribute conflicts with %<setter%> attribute");
964 gcc_checking_assert (attrs[OBJC_PROPATTR_GROUP_SETTER]);
965 inform (attrs[OBJC_PROPATTR_GROUP_SETTER]->prop_loc, "%<setter%>"
966 " specified here");
967 property_readonly = false;
970 /* Assign semantics is a tri-state property, and also needs some further
971 checking against the object type. */
972 objc_property_assign_semantics property_assign_semantics
973 = OBJC_PROPERTY_ASSIGN;
975 if (attrs[OBJC_PROPATTR_GROUP_ASSIGN])
977 if (attrs[OBJC_PROPATTR_GROUP_ASSIGN]->prop_kind
978 == OBJC_PROPERTY_ATTR_ASSIGN)
979 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
980 else if (attrs[OBJC_PROPATTR_GROUP_ASSIGN]->prop_kind
981 == OBJC_PROPERTY_ATTR_RETAIN)
982 property_assign_semantics = OBJC_PROPERTY_RETAIN;
983 else if (attrs[OBJC_PROPATTR_GROUP_ASSIGN]->prop_kind
984 == OBJC_PROPERTY_ATTR_COPY)
985 property_assign_semantics = OBJC_PROPERTY_COPY;
986 else
987 gcc_unreachable ();
990 /* An attribute that indicates this property manipulates a class variable.
991 In this case, both the variable and the getter/setter must be provided
992 by the user. */
993 bool property_class = false;
994 if (attrs[OBJC_PROPATTR_GROUP_CLASS])
995 property_nonatomic = attrs[OBJC_PROPATTR_GROUP_CLASS]->prop_kind
996 == OBJC_PROPERTY_ATTR_CLASS;
998 /* TODO: Check that the property type is an Objective-C object or a
999 "POD". */
1001 /* Implement -Wproperty-assign-default (which is enabled by default). */
1002 if (warn_property_assign_default
1003 /* If garbage collection is not being used, then 'assign' is
1004 valid for objects (and typically used for delegates) but it
1005 is wrong in most cases (since most objects need to be
1006 retained or copied in setters). Warn users when 'assign' is
1007 used implicitly. */
1008 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
1009 /* Read-only properties are never assigned, so the assignment
1010 semantics do not matter in that case. */
1011 && !property_readonly
1012 && !flag_objc_gc)
1014 /* Please note that it would make sense to default to 'assign'
1015 for non-{Objective-C objects}, and to 'retain' for
1016 Objective-C objects. But that would break compatibility with
1017 other compilers. */
1018 if (!attrs[OBJC_PROPATTR_GROUP_ASSIGN])
1020 /* Use 'false' so we do not warn for Class objects. */
1021 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
1023 warning_at (decl_loc, 0, "object property %qD has no %<assign%>,"
1024 " %<retain%> or %<copy%> attribute; assuming"
1025 " %<assign%>", decl);
1026 inform (decl_loc, "%<assign%> can be unsafe for Objective-C"
1027 " objects; please state explicitly if you need it");
1032 /* Some attributes make no sense unless applied to an Objective-C object. */
1033 bool prop_objc_object_p
1034 = objc_type_valid_for_messaging (TREE_TYPE (decl), true);
1035 if (!prop_objc_object_p)
1037 tree p_name = NULL_TREE;
1038 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
1039 || property_assign_semantics == OBJC_PROPERTY_COPY)
1040 p_name = attrs[OBJC_PROPATTR_GROUP_ASSIGN]->name;
1042 if (p_name)
1043 error_at (decl_loc, "%qE attribute is only valid for Objective-C"
1044 " objects", p_name);
1047 /* Now determine the final property getter and setter names. They
1048 will be stored in the PROPERTY_DECL, from which they'll always be
1049 extracted and used. */
1051 /* Adjust, or fill in, setter and getter names. We overwrite the
1052 property_setter_ident and property_getter_ident
1053 with the final setter and getter identifiers that will be
1054 used. */
1055 if (property_setter_ident)
1057 /* The setter should be terminated by ':', but the parser only
1058 gives us an identifier without ':'. So, we need to add ':'
1059 at the end. */
1060 const char *parsed_setter = IDENTIFIER_POINTER (property_setter_ident);
1061 size_t length = strlen (parsed_setter);
1062 char *final_setter = (char *)alloca (length + 2);
1064 sprintf (final_setter, "%s:", parsed_setter);
1065 property_setter_ident = get_identifier (final_setter);
1067 else
1069 if (!property_readonly)
1070 property_setter_ident = get_identifier (objc_build_property_setter_name
1071 (DECL_NAME (decl)));
1074 if (!property_getter_ident)
1075 property_getter_ident = DECL_NAME (decl);
1077 /* Check for duplicate property declarations. We first check the
1078 immediate context for a property with the same name. Any such
1079 declarations are an error, unless this is a class extension and
1080 we are extending a property from readonly to readwrite. */
1081 bool property_extension_in_class_extension = false;
1082 tree x = NULL_TREE;
1083 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1085 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1087 if (objc_in_class_extension
1088 && !property_readonly
1089 && PROPERTY_READONLY (x) == 1)
1091 /* This is a class extension, and we are extending an
1092 existing readonly property to a readwrite one.
1093 That's fine. :-) */
1094 property_extension_in_class_extension = true;
1095 break;
1097 else
1099 location_t original_location = DECL_SOURCE_LOCATION (x);
1101 error_at (location, "redeclaration of property %qD", decl);
1103 if (original_location != UNKNOWN_LOCATION)
1104 inform (original_location, "originally specified here");
1105 return;
1110 /* If x is not NULL_TREE, we must be in a class extension and we're
1111 extending a readonly property. In that case, no point in
1112 searching for another declaration. */
1113 if (x == NULL_TREE)
1115 /* We now need to check for existing property declarations (in
1116 the superclass, other categories or protocols) and check that
1117 the new declaration is not in conflict with existing
1118 ones. */
1120 /* Search for a previous, existing declaration of a property
1121 with the same name in superclasses, protocols etc. If one is
1122 found, it will be in the 'x' variable. */
1124 /* Note that, for simplicity, the following may search again the
1125 local context. That's Ok as nothing will be found (else we'd
1126 have thrown an error above); it's only a little inefficient,
1127 but the code is simpler. */
1128 switch (TREE_CODE (objc_interface_context))
1130 case CLASS_INTERFACE_TYPE:
1131 /* Look up the property in the current @interface (which
1132 will find nothing), then its protocols and categories and
1133 superclasses. */
1134 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1135 break;
1136 case CATEGORY_INTERFACE_TYPE:
1137 /* Look up the property in the main @interface, then
1138 protocols and categories (one of them is ours, and will
1139 find nothing) and superclasses. */
1140 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1141 DECL_NAME (decl));
1142 break;
1143 case PROTOCOL_INTERFACE_TYPE:
1144 /* Looks up the property in any protocols attached to the
1145 current protocol. */
1146 if (PROTOCOL_LIST (objc_interface_context))
1148 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1149 DECL_NAME (decl));
1151 break;
1152 default:
1153 gcc_unreachable ();
1157 if (x != NULL_TREE)
1159 /* An existing property was found; check that it has the same
1160 types, or it is compatible. */
1161 location_t original_location = DECL_SOURCE_LOCATION (x);
1163 if (PROPERTY_NONATOMIC (x) != property_nonatomic)
1165 warning_at (location, 0,
1166 "%<nonatomic%> attribute of property %qD conflicts with "
1167 "previous declaration", decl);
1169 if (original_location != UNKNOWN_LOCATION)
1170 inform (original_location, "originally specified here");
1171 return;
1174 if (PROPERTY_GETTER_NAME (x) != property_getter_ident)
1176 warning_at (location, 0,
1177 "%<getter%> attribute of property %qD conflicts with "
1178 "previous declaration", decl);
1180 if (original_location != UNKNOWN_LOCATION)
1181 inform (original_location, "originally specified here");
1182 return;
1185 /* We can only compare the setter names if both the old and new property have a setter. */
1186 if (!property_readonly && !PROPERTY_READONLY(x))
1188 if (PROPERTY_SETTER_NAME (x) != property_setter_ident)
1190 warning_at (location, 0,
1191 "%<setter%> attribute of property %qD conflicts with "
1192 "previous declaration", decl);
1194 if (original_location != UNKNOWN_LOCATION)
1195 inform (original_location, "originally specified here");
1196 return;
1200 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1202 warning_at (location, 0,
1203 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1205 if (original_location != UNKNOWN_LOCATION)
1206 inform (original_location, "originally specified here");
1207 return;
1210 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1211 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1213 warning_at (location, 0,
1214 "%<readonly%> attribute of property %qD conflicts with "
1215 "previous declaration", decl);
1217 if (original_location != UNKNOWN_LOCATION)
1218 inform (original_location, "originally specified here");
1219 return;
1222 /* We now check that the new and old property declarations have
1223 the same types (or compatible one). In the Objective-C
1224 tradition of loose type checking, we do type-checking but
1225 only generate warnings (not errors) if they do not match.
1226 For non-readonly properties, the types must match exactly;
1227 for readonly properties, it is allowed to use a "more
1228 specialized" type in the new property declaration. Eg, the
1229 superclass has a getter returning (NSArray *) and the
1230 subclass a getter returning (NSMutableArray *). The object's
1231 getter returns an (NSMutableArray *); but if you cast the
1232 object to the superclass, which is allowed, you'd still
1233 expect the getter to return an (NSArray *), which works since
1234 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1235 objects belonging to the type of the new @property should be
1236 a subset of the set of objects belonging to the type of the
1237 old @property. This is what "specialization" means. And the
1238 reason it only applies to readonly properties is that for a
1239 readwrite property the setter would have the opposite
1240 requirement - ie that the superclass type is more specialized
1241 then the subclass one; hence the only way to satisfy both
1242 constraints is that the types match. */
1244 /* If the types are not the same in the C sense, we warn ... */
1245 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1246 /* ... unless the property is readonly, in which case we
1247 allow a new, more specialized, declaration. */
1248 && (!property_readonly
1249 || !objc_compare_types (TREE_TYPE (x),
1250 TREE_TYPE (decl), -5, NULL_TREE)))
1252 warning_at (location, 0,
1253 "type of property %qD conflicts with previous declaration", decl);
1254 if (original_location != UNKNOWN_LOCATION)
1255 inform (original_location, "originally specified here");
1256 return;
1259 /* If we are in a class extension and we're extending a readonly
1260 property in the main @interface, we'll just update the
1261 existing property with the readwrite flag and potentially the
1262 new setter name. */
1263 if (property_extension_in_class_extension)
1265 PROPERTY_READONLY (x) = 0;
1266 PROPERTY_SETTER_NAME (x) = property_setter_ident;
1267 return;
1271 /* Create a PROPERTY_DECL node. */
1272 tree property_decl = make_node (PROPERTY_DECL);
1274 /* Copy the basic information from the original decl. */
1275 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1276 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1277 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1279 /* Add property-specific information. */
1280 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1281 PROPERTY_GETTER_NAME (property_decl) = property_getter_ident;
1282 PROPERTY_SETTER_NAME (property_decl) = property_setter_ident;
1283 PROPERTY_READONLY (property_decl) = property_readonly;
1284 PROPERTY_NONATOMIC (property_decl) = property_nonatomic;
1285 PROPERTY_CLASS (property_decl) = property_class;
1286 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1287 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1288 PROPERTY_DYNAMIC (property_decl) = 0;
1290 /* Remember the fact that the property was found in the @optional
1291 section in a @protocol, or not. */
1292 if (objc_method_optional_flag)
1293 PROPERTY_OPTIONAL (property_decl) = 1;
1294 else
1295 PROPERTY_OPTIONAL (property_decl) = 0;
1297 /* Note that PROPERTY_GETTER_NAME is always set for all
1298 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1299 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1300 with a getter or setter, we should get the PROPERTY_DECL and use
1301 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1302 names. */
1304 /* Add the PROPERTY_DECL to the list of properties for the class. */
1305 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1306 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1309 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1310 list of methods in the interface (and, failing that, the local list
1311 in the implementation, and failing that, the protocol list)
1312 provided for a 'setter' or 'getter' for 'component' with default
1313 names (ie, if 'component' is "name", then search for "name" and
1314 "setName:"). It is also possible to specify a different
1315 'getter_name' (this is used for @optional readonly properties). If
1316 any is found, then create an artificial property that uses them.
1317 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1318 static tree
1319 maybe_make_artificial_property_decl (tree interface, tree implementation,
1320 tree protocol_list, tree component, bool is_class,
1321 tree getter_name)
1323 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1324 tree getter = NULL_TREE;
1325 tree setter = NULL_TREE;
1327 if (getter_name == NULL_TREE)
1328 getter_name = component;
1330 /* First, check the @interface and all superclasses. */
1331 if (interface)
1333 int flags = 0;
1335 /* Using instance methods of the root class as accessors is most
1336 likely unwanted and can be extremely confusing (and, most
1337 importantly, other Objective-C 2.0 compilers do not do it).
1338 Turn it off. */
1339 if (is_class)
1340 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1342 getter = lookup_method_static (interface, getter_name, flags);
1343 setter = lookup_method_static (interface, setter_name, flags);
1346 /* Second, check the local @implementation context. */
1347 if (!getter && !setter)
1349 if (implementation)
1351 if (is_class)
1353 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1354 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1356 else
1358 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1359 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1364 /* Try the protocol_list if we didn't find anything in the
1365 @interface and in the @implementation. */
1366 if (!getter && !setter)
1368 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1369 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1372 /* There needs to be at least a getter or setter for this to be a
1373 valid 'object.component' syntax. */
1374 if (getter || setter)
1376 /* Yes ... determine the type of the expression. */
1377 tree property_decl;
1378 tree type;
1380 if (getter)
1381 type = TREE_VALUE (TREE_TYPE (getter));
1382 else
1383 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1385 /* Create an artificial property declaration with the
1386 information we collected on the type and getter/setter
1387 names. */
1388 property_decl = make_node (PROPERTY_DECL);
1390 TREE_TYPE (property_decl) = type;
1391 DECL_SOURCE_LOCATION (property_decl) = input_location;
1392 TREE_DEPRECATED (property_decl) = 0;
1393 DECL_ARTIFICIAL (property_decl) = 1;
1395 /* Add property-specific information. Note that one of
1396 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1397 non-existing method; this will generate an error when the
1398 expression is later compiled. At this stage we don't know if
1399 the getter or setter will be used, so we can't generate an
1400 error. */
1401 PROPERTY_NAME (property_decl) = component;
1402 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1403 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1404 PROPERTY_READONLY (property_decl) = 0;
1405 PROPERTY_NONATOMIC (property_decl) = 0;
1406 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1407 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1408 PROPERTY_DYNAMIC (property_decl) = 0;
1409 PROPERTY_OPTIONAL (property_decl) = 0;
1411 if (!getter)
1412 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1414 /* The following is currently unused, but it's nice to have
1415 there. We may use it if we need in the future. */
1416 if (!setter)
1417 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1419 return property_decl;
1422 return NULL_TREE;
1425 /* This hook routine is invoked by the parser when an expression such
1426 as 'xxx.yyy' is parsed. We get a chance to process these
1427 expressions in a way that is specified to Objective-C (to implement
1428 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1429 If the expression is not an Objective-C specified expression, we
1430 should return NULL_TREE; else we return the expression.
1432 At the moment this only implements dot-syntax and properties (not
1433 non-fragile ivars yet), ie 'object.property' or 'object.component'
1434 where 'component' is not a declared property, but a valid getter or
1435 setter for it could be found. */
1436 tree
1437 objc_maybe_build_component_ref (tree object, tree property_ident)
1439 tree x = NULL_TREE;
1440 tree rtype;
1442 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1443 not available. */
1444 if (flag_objc1_only)
1445 return NULL_TREE;
1447 /* Try to determine if 'object' is an Objective-C object or not. If
1448 not, return. */
1449 if (object == NULL_TREE || object == error_mark_node
1450 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1451 return NULL_TREE;
1453 if (property_ident == NULL_TREE || property_ident == error_mark_node
1454 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1455 return NULL_TREE;
1457 /* The following analysis of 'object' is similar to the one used for
1458 the 'receiver' of a method invocation. We need to determine what
1459 'object' is and find the appropriate property (either declared,
1460 or artificial) for it (in the same way as we need to find the
1461 appropriate method prototype for a method invocation). There are
1462 some simplifications here though: "object.property" is invalid if
1463 "object" has a type of "id" or "Class"; it must at least have a
1464 protocol attached to it, and "object" is never a class name as
1465 that is done by objc_build_class_component_ref. Finally, we
1466 don't know if this really is a dot-syntax expression, so we want
1467 to make a quick exit if it is not; for this reason, we try to
1468 postpone checks after determining that 'object' looks like an
1469 Objective-C object. */
1471 if (objc_is_id (rtype))
1473 /* This is the case that the 'object' is of type 'id' or
1474 'Class'. */
1476 /* Check if at least it is of type 'id <Protocol>' or 'Class
1477 <Protocol>'; if so, look the property up in the
1478 protocols. */
1479 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1481 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1483 if (rprotos)
1485 /* No point looking up declared @properties if we are
1486 dealing with a class. Classes have no declared
1487 properties. */
1488 if (!IS_CLASS (rtype))
1489 x = lookup_property_in_protocol_list (rprotos, property_ident);
1491 if (x == NULL_TREE)
1493 /* Ok, no property. Maybe it was an
1494 object.component dot-syntax without a declared
1495 property (this is valid for classes too). Look
1496 for getter/setter methods and internally declare
1497 an artificial property based on them if found. */
1498 x = maybe_make_artificial_property_decl (NULL_TREE,
1499 NULL_TREE,
1500 rprotos,
1501 property_ident,
1502 IS_CLASS (rtype),
1503 NULL_TREE);
1505 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1507 /* This is a special, complicated case. If the
1508 property is optional, and is read-only, then the
1509 property is always used for reading, but an
1510 eventual existing non-property setter can be used
1511 for writing. We create an artificial property
1512 decl copying the getter from the optional
1513 property, and looking up the setter in the
1514 interface. */
1515 x = maybe_make_artificial_property_decl (NULL_TREE,
1516 NULL_TREE,
1517 rprotos,
1518 property_ident,
1519 false,
1520 PROPERTY_GETTER_NAME (x));
1524 else if (objc_method_context)
1526 /* Else, if we are inside a method it could be the case of
1527 'super' or 'self'. */
1528 tree interface_type = NULL_TREE;
1529 tree t = object;
1530 while (TREE_CODE (t) == COMPOUND_EXPR
1531 || TREE_CODE (t) == MODIFY_EXPR
1532 || CONVERT_EXPR_P (t)
1533 || TREE_CODE (t) == COMPONENT_REF)
1534 t = TREE_OPERAND (t, 0);
1536 STRIP_ANY_LOCATION_WRAPPER (t);
1538 if (t == UOBJC_SUPER_decl)
1539 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1540 else if (t == self_decl)
1541 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1543 if (interface_type)
1545 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1546 x = lookup_property (interface_type, property_ident);
1548 if (x == NULL_TREE)
1550 /* Try the dot-syntax without a declared property.
1551 If this is an access to 'self', it is possible
1552 that they may refer to a setter/getter that is
1553 not declared in the interface, but exists locally
1554 in the implementation. In that case, get the
1555 implementation context and use it. */
1556 tree implementation = NULL_TREE;
1558 if (t == self_decl)
1559 implementation = objc_implementation_context;
1561 x = maybe_make_artificial_property_decl
1562 (interface_type, implementation, NULL_TREE,
1563 property_ident,
1564 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1565 NULL_TREE);
1567 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1569 tree implementation = NULL_TREE;
1571 if (t == self_decl)
1572 implementation = objc_implementation_context;
1574 x = maybe_make_artificial_property_decl (interface_type,
1575 implementation,
1576 NULL_TREE,
1577 property_ident,
1578 false,
1579 PROPERTY_GETTER_NAME (x));
1584 else
1586 /* This is the case where we have more information on 'rtype'. */
1587 tree basetype = TYPE_MAIN_VARIANT (rtype);
1589 /* Skip the pointer - if none, it's not an Objective-C object or
1590 class. */
1591 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1592 basetype = TREE_TYPE (basetype);
1593 else
1594 return NULL_TREE;
1596 /* Traverse typedefs. */
1597 while (basetype != NULL_TREE
1598 && TREE_CODE (basetype) == RECORD_TYPE
1599 && OBJC_TYPE_NAME (basetype)
1600 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1601 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1602 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1604 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1606 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1607 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1609 if (interface_type
1610 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1611 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1612 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1614 /* Not sure 'rtype' could ever be a class here! Just
1615 for safety we keep the checks. */
1616 if (!IS_CLASS (rtype))
1618 x = lookup_property (interface_type, property_ident);
1620 if (x == NULL_TREE)
1621 x = lookup_property_in_protocol_list (protocol_list,
1622 property_ident);
1625 if (x == NULL_TREE)
1627 /* Try the dot-syntax without a declared property.
1628 If we are inside a method implementation, it is
1629 possible that they may refer to a setter/getter
1630 that is not declared in the interface, but exists
1631 locally in the implementation. In that case, get
1632 the implementation context and use it. */
1633 tree implementation = NULL_TREE;
1635 if (objc_implementation_context
1636 && CLASS_NAME (objc_implementation_context)
1637 == OBJC_TYPE_NAME (interface_type))
1638 implementation = objc_implementation_context;
1640 x = maybe_make_artificial_property_decl (interface_type,
1641 implementation,
1642 protocol_list,
1643 property_ident,
1644 IS_CLASS (rtype),
1645 NULL_TREE);
1647 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1649 tree implementation = NULL_TREE;
1651 if (objc_implementation_context
1652 && CLASS_NAME (objc_implementation_context)
1653 == OBJC_TYPE_NAME (interface_type))
1654 implementation = objc_implementation_context;
1656 x = maybe_make_artificial_property_decl (interface_type,
1657 implementation,
1658 protocol_list,
1659 property_ident,
1660 false,
1661 PROPERTY_GETTER_NAME (x));
1667 if (x)
1669 tree expression;
1670 tree getter_call;
1671 tree deprecated_method_prototype = NULL_TREE;
1673 /* We have an additional nasty problem here; if this
1674 PROPERTY_REF needs to become a 'getter', then the conversion
1675 from PROPERTY_REF into a getter call happens in gimplify,
1676 after the selector table has already been generated and when
1677 it is too late to add another selector to it. To work around
1678 the problem, we always create the getter call at this stage,
1679 which puts the selector in the table. Note that if the
1680 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1681 we have added a selector too many to the selector table.
1682 This is a little inefficient.
1684 Also note that method calls to 'self' and 'super' require the
1685 context (self_decl, UOBJS_SUPER_decl,
1686 objc_implementation_context etc) to be built correctly; this
1687 is yet another reason why building the call at the gimplify
1688 stage (when this context has been lost) is not very
1689 practical. If we build it at this stage, we know it will
1690 always be built correctly.
1692 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1693 property decl created to deal with a dotsyntax not really
1694 referring to an existing property) then do not try to build a
1695 call to the getter as there is no getter. */
1696 if (PROPERTY_HAS_NO_GETTER (x))
1697 getter_call = NULL_TREE;
1698 else
1699 getter_call = objc_finish_message_expr
1700 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1701 /* Disable the immediate deprecation warning if the getter
1702 is deprecated, but record the fact that the getter is
1703 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1704 the method prototype. */
1705 &deprecated_method_prototype);
1707 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1708 deprecated_method_prototype);
1709 SET_EXPR_LOCATION (expression, input_location);
1710 TREE_SIDE_EFFECTS (expression) = 1;
1712 return expression;
1715 return NULL_TREE;
1718 /* This hook routine is invoked by the parser when an expression such
1719 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1720 Objective-C 2.0 dot-syntax applied to classes, so we need to
1721 convert it into a setter/getter call on the class. */
1722 tree
1723 objc_build_class_component_ref (tree class_name, tree property_ident)
1725 tree x = NULL_TREE;
1726 tree object, rtype;
1728 if (flag_objc1_only)
1729 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1731 if (class_name == NULL_TREE || class_name == error_mark_node
1732 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1733 return error_mark_node;
1735 if (property_ident == NULL_TREE || property_ident == error_mark_node
1736 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1737 return NULL_TREE;
1739 object = objc_get_class_reference (class_name);
1740 if (!object)
1742 /* We know that 'class_name' is an Objective-C class name as the
1743 parser won't call this function if it is not. This is only a
1744 double-check for safety. */
1745 error_at (input_location, "could not find class %qE", class_name);
1746 return error_mark_node;
1749 rtype = lookup_interface (class_name);
1750 if (!rtype)
1752 /* Again, this should never happen, but we do check. */
1753 error_at (input_location, "could not find interface for class %qE", class_name);
1754 return error_mark_node;
1756 else
1758 if (TREE_DEPRECATED (rtype))
1759 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1762 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1763 property_ident,
1764 true, NULL_TREE);
1766 if (x)
1768 tree expression;
1769 tree getter_call;
1770 tree deprecated_method_prototype = NULL_TREE;
1772 if (PROPERTY_HAS_NO_GETTER (x))
1773 getter_call = NULL_TREE;
1774 else
1775 getter_call = objc_finish_message_expr
1776 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1777 &deprecated_method_prototype);
1779 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1780 deprecated_method_prototype);
1781 SET_EXPR_LOCATION (expression, input_location);
1782 TREE_SIDE_EFFECTS (expression) = 1;
1784 return expression;
1786 else
1788 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1789 property_ident, class_name);
1790 return error_mark_node;
1793 return NULL_TREE;
1797 /* This is used because we don't want to expose PROPERTY_REF to the
1798 C/C++ frontends. Maybe we should! */
1799 bool
1800 objc_is_property_ref (tree node)
1802 if (node && TREE_CODE (node) == PROPERTY_REF)
1803 return true;
1804 else
1805 return false;
1808 /* We use this to report tree codes that are known to be invalid in const-
1809 expression contexts. */
1810 bool
1811 objc_non_constant_expr_p (tree node)
1813 switch (TREE_CODE (node))
1815 default:
1816 return false;
1817 case MESSAGE_SEND_EXPR:
1818 case PROPERTY_REF:
1819 return true;
1823 /* This function builds a setter call for a PROPERTY_REF (real, for a
1824 declared property, or artificial, for a dot-syntax accessor which
1825 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1826 (the caller must check this beforehand). 'rhs' is the value to
1827 assign to the property. A plain setter call is returned, or
1828 error_mark_node if the property is readonly. */
1830 static tree
1831 objc_build_setter_call (tree lhs, tree rhs)
1833 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1834 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1836 if (PROPERTY_READONLY (property_decl))
1838 error ("%qs property cannot be set", "readonly");
1839 return error_mark_node;
1841 else
1843 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1844 tree setter;
1846 /* TODO: Check that the setter return type is 'void'. */
1848 /* TODO: Decay arguments in C. */
1849 setter = objc_finish_message_expr (object_expr,
1850 PROPERTY_SETTER_NAME (property_decl),
1851 setter_argument, NULL);
1852 return setter;
1855 /* Unreachable, but the compiler may not realize. */
1856 return error_mark_node;
1859 /* This hook routine is called when a MODIFY_EXPR is being built. We
1860 check what is being modified; if it is a PROPERTY_REF, we need to
1861 generate a 'setter' function call for the property. If this is not
1862 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1863 on creating their MODIFY_EXPR.
1865 This is used for example if you write
1867 object.count = 1;
1869 where 'count' is a property. The left-hand side creates a
1870 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1871 to assign something to it. We intercept that here, and generate a
1872 call to the 'setter' method instead. */
1873 tree
1874 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1876 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1878 /* Building a simple call to the setter method would work for cases such as
1880 object.count = 1;
1882 but wouldn't work for cases such as
1884 count = object2.count = 1;
1886 to get these to work with very little effort, we build a
1887 compound statement which does the setter call (to set the
1888 property to 'rhs'), but which can also be evaluated returning
1889 the 'rhs'. If the 'rhs' has no side effects, we can simply
1890 evaluate it twice, building
1892 ([object setProperty: rhs]; rhs)
1894 If it has side effects, we put it in a temporary variable first,
1895 so we create the following:
1897 (temp = rhs; [object setProperty: temp]; temp)
1899 setter_argument is rhs in the first case, and temp in the second
1900 case.
1902 tree setter_argument;
1904 /* s1, s2 and s3 are the tree statements that we need in the
1905 compound expression. */
1906 tree s1, s2, s3, compound_expr;
1908 if (TREE_SIDE_EFFECTS (rhs))
1910 tree bind;
1912 /* Declare __objc_property_temp in a local bind. */
1913 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1914 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1915 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1916 SET_EXPR_LOCATION (bind, input_location);
1917 TREE_SIDE_EFFECTS (bind) = 1;
1918 add_stmt (bind);
1920 /* s1: x = rhs */
1921 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1922 NOP_EXPR,
1923 input_location, rhs, NULL_TREE);
1924 SET_EXPR_LOCATION (s1, input_location);
1926 else
1928 /* No s1. */
1929 setter_argument = rhs;
1930 s1 = NULL_TREE;
1933 /* Now build the compound statement. */
1935 /* s2: [object setProperty: x] */
1936 s2 = objc_build_setter_call (lhs, setter_argument);
1938 /* This happens if building the setter failed because the
1939 property is readonly. */
1940 if (s2 == error_mark_node)
1941 return error_mark_node;
1943 SET_EXPR_LOCATION (s2, input_location);
1945 /* s3: x */
1946 s3 = convert (TREE_TYPE (lhs), setter_argument);
1948 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1949 appropriate. */
1950 if (s1)
1951 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1952 else
1953 compound_expr = build_compound_expr (input_location, s2, s3);
1955 /* Without this, with -Wall you get a 'valued computed is not
1956 used' every time there is a "object.property = x" where the
1957 value of the resulting MODIFY_EXPR is not used. That is
1958 correct (maybe a more sophisticated implementation could
1959 avoid generating the compound expression if not needed), but
1960 we need to turn it off. */
1961 TREE_NO_WARNING (compound_expr) = 1;
1962 return compound_expr;
1964 else
1965 return NULL_TREE;
1968 /* This hook is called by the frontend when one of the four unary
1969 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1970 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1971 argument which is a PROPERTY_REF. For example, this happens if you have
1973 object.count++;
1975 where 'count' is a property. We need to use the 'getter' and
1976 'setter' for the property in an appropriate way to build the
1977 appropriate expression. 'code' is the code for the expression (one
1978 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1979 'increment' is how much we need to add or subtract. */
1980 tree
1981 objc_build_incr_expr_for_property_ref (location_t location,
1982 enum tree_code code,
1983 tree argument, tree increment)
1985 /* Here are the expressions that we want to build:
1987 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1988 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1990 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1991 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1993 tree temp_variable_decl, bind;
1994 /* s1, s2 and s3 are the tree statements that we need in the
1995 compound expression. */
1996 tree s1, s2, s3, compound_expr;
1998 /* Safety check. */
1999 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
2000 return error_mark_node;
2002 /* Declare __objc_property_temp in a local bind. */
2003 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
2004 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
2005 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
2006 SET_EXPR_LOCATION (bind, location);
2007 TREE_SIDE_EFFECTS (bind) = 1;
2008 add_stmt (bind);
2010 /* Now build the compound statement. */
2012 /* Note that the 'getter' is generated at gimplify time; at this
2013 time, we can simply put the property_ref (ie, argument) wherever
2014 we want the getter ultimately to be. */
2016 /* s1: __objc_property_temp = [object property] <+/- increment> */
2017 switch (code)
2019 case PREINCREMENT_EXPR:
2020 /* __objc_property_temp = [object property] + increment */
2021 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
2022 NOP_EXPR,
2023 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
2024 argument, increment), NULL_TREE);
2025 break;
2026 case PREDECREMENT_EXPR:
2027 /* __objc_property_temp = [object property] - increment */
2028 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
2029 NOP_EXPR,
2030 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
2031 argument, increment), NULL_TREE);
2032 break;
2033 case POSTINCREMENT_EXPR:
2034 case POSTDECREMENT_EXPR:
2035 /* __objc_property_temp = [object property] */
2036 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
2037 NOP_EXPR,
2038 location, argument, NULL_TREE);
2039 break;
2040 default:
2041 gcc_unreachable ();
2044 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
2045 switch (code)
2047 case PREINCREMENT_EXPR:
2048 case PREDECREMENT_EXPR:
2049 /* [object setProperty: __objc_property_temp] */
2050 s2 = objc_build_setter_call (argument, temp_variable_decl);
2051 break;
2052 case POSTINCREMENT_EXPR:
2053 /* [object setProperty: __objc_property_temp + increment] */
2054 s2 = objc_build_setter_call (argument,
2055 build2 (PLUS_EXPR, TREE_TYPE (argument),
2056 temp_variable_decl, increment));
2057 break;
2058 case POSTDECREMENT_EXPR:
2059 /* [object setProperty: __objc_property_temp - increment] */
2060 s2 = objc_build_setter_call (argument,
2061 build2 (MINUS_EXPR, TREE_TYPE (argument),
2062 temp_variable_decl, increment));
2063 break;
2064 default:
2065 gcc_unreachable ();
2068 /* This happens if building the setter failed because the property
2069 is readonly. */
2070 if (s2 == error_mark_node)
2071 return error_mark_node;
2073 SET_EXPR_LOCATION (s2, location);
2075 /* s3: __objc_property_temp */
2076 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
2078 /* Now build the compound statement (s1, s2, s3) */
2079 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
2081 /* Prevent C++ from warning with -Wall that "right operand of comma
2082 operator has no effect". */
2083 TREE_NO_WARNING (compound_expr) = 1;
2084 return compound_expr;
2087 tree
2088 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
2089 tree optparms, bool ellipsis)
2091 if (is_class_method)
2092 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2093 optparms, ellipsis);
2094 else
2095 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2096 optparms, ellipsis);
2099 void
2100 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2102 if (!objc_interface_context)
2104 /* PS: At the moment, due to how the parser works, it should be
2105 impossible to get here. But it's good to have the check in
2106 case the parser changes.
2108 fatal_error (input_location,
2109 "method declaration not in @interface context");
2112 if (flag_objc1_only && attributes)
2113 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2115 objc_decl_method_attributes (&decl, attributes, 0);
2116 objc_add_method (objc_interface_context,
2117 decl,
2118 is_class_method,
2119 objc_method_optional_flag);
2122 /* Return 'true' if the method definition could be started, and
2123 'false' if not (because we are outside an @implementation context).
2124 EXPR is NULL or an expression that needs to be evaluated for the
2125 side effects of array size expressions in the parameters.
2127 bool
2128 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2129 tree expr)
2131 if (!objc_implementation_context)
2133 error ("method definition not in @implementation context");
2134 return false;
2137 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2138 return false;
2140 #ifndef OBJCPLUS
2141 /* Indicate no valid break/continue context. */
2142 in_statement = 0;
2143 #endif
2145 if (attributes)
2146 warning_at (input_location, 0, "method attributes cannot be specified in @implementation context");
2147 else
2148 objc_decl_method_attributes (&decl, attributes, 0);
2150 objc_add_method (objc_implementation_context,
2151 decl,
2152 is_class_method,
2153 /* is optional */ false);
2154 start_method_def (decl, expr);
2155 return true;
2158 void
2159 objc_add_instance_variable (tree decl)
2161 (void) add_instance_variable (objc_ivar_context,
2162 objc_ivar_visibility,
2163 decl);
2166 /* Construct a C struct with same name as KLASS, a base struct with tag
2167 SUPER_NAME (if any), and FIELDS indicated. */
2169 static tree
2170 objc_build_struct (tree klass, tree fields, tree super_name)
2172 tree name = CLASS_NAME (klass);
2173 tree s = objc_start_struct (name);
2174 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2175 tree t;
2176 vec<tree> objc_info = vNULL;
2177 int i;
2179 if (super)
2181 /* Prepend a packed variant of the base class into the layout. This
2182 is necessary to preserve ObjC ABI compatibility. */
2183 tree base = build_decl (input_location,
2184 FIELD_DECL, NULL_TREE, super);
2185 tree field = TYPE_FIELDS (super);
2187 while (field && DECL_CHAIN (field)
2188 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2189 field = DECL_CHAIN (field);
2191 /* For ObjC ABI purposes, the "packed" size of a base class is
2192 the sum of the offset and the size (in bits) of the last field
2193 in the class. */
2194 DECL_SIZE (base)
2195 = (field && TREE_CODE (field) == FIELD_DECL
2196 ? size_binop (PLUS_EXPR,
2197 size_binop (PLUS_EXPR,
2198 size_binop
2199 (MULT_EXPR,
2200 convert (bitsizetype,
2201 DECL_FIELD_OFFSET (field)),
2202 bitsize_int (BITS_PER_UNIT)),
2203 DECL_FIELD_BIT_OFFSET (field)),
2204 DECL_SIZE (field))
2205 : bitsize_zero_node);
2206 DECL_SIZE_UNIT (base)
2207 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2208 size_int (BITS_PER_UNIT));
2209 DECL_ARTIFICIAL (base) = 1;
2210 SET_DECL_ALIGN (base, 1);
2211 DECL_FIELD_CONTEXT (base) = s;
2212 #ifdef OBJCPLUS
2213 DECL_FIELD_IS_BASE (base) = 1;
2215 if (fields)
2216 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2217 #endif /* are following the ObjC ABI here. */
2218 DECL_CHAIN (base) = fields;
2219 fields = base;
2222 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2223 information in all variants of this RECORD_TYPE to be destroyed
2224 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2225 for something else and then will change all variants to use the
2226 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2227 it for ObjC protocols and that such propagation will make all
2228 variants use the same objc_info), but it is therein that we store
2229 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2230 Hence, we must save the ObjC-specific information before calling
2231 finish_struct(), and then reinstate it afterwards. */
2233 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2235 INIT_TYPE_OBJC_INFO (t);
2236 objc_info.safe_push (TYPE_OBJC_INFO (t));
2239 s = objc_finish_struct (s, fields);
2241 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2243 /* We now want to restore the different TYPE_OBJC_INFO, but we
2244 have the additional problem that the C frontend doesn't just
2245 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2246 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2247 we need a different TYPE_OBJC_INFO for each (and
2248 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2249 make a copy of each TYPE_LANG_SPECIFIC before we modify
2250 TYPE_OBJC_INFO. */
2251 if (TYPE_LANG_SPECIFIC (t))
2253 /* Create a copy of TYPE_LANG_SPECIFIC. */
2254 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2255 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2256 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2257 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2259 else
2261 /* Just create a new one. */
2262 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2264 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2265 protocol information that may have been associated with the
2266 type. */
2267 TYPE_OBJC_INFO (t) = objc_info[i];
2268 /* Replace the IDENTIFIER_NODE with an actual @interface now
2269 that we have it. */
2270 TYPE_OBJC_INTERFACE (t) = klass;
2272 objc_info.release ();
2274 /* Use TYPE_BINFO structures to point at the super class, if any. */
2275 objc_xref_basetypes (s, super);
2277 /* Mark this struct as a class template. */
2278 CLASS_STATIC_TEMPLATE (klass) = s;
2280 return s;
2283 /* Mark DECL as being 'volatile' for purposes of Darwin
2284 _setjmp()/_longjmp() exception handling. Called from
2285 objc_mark_locals_volatile(). */
2286 void
2287 objc_volatilize_decl (tree decl)
2289 /* Do not mess with variables that are 'static' or (already)
2290 'volatile'. */
2291 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2292 && (TREE_CODE (decl) == VAR_DECL
2293 || TREE_CODE (decl) == PARM_DECL))
2295 if (local_variables_to_volatilize == NULL)
2296 vec_alloc (local_variables_to_volatilize, 8);
2298 vec_safe_push (local_variables_to_volatilize, decl);
2302 /* Called when parsing of a function completes; if any local variables
2303 in the function were marked as variables to volatilize, change them
2304 to volatile. We do this at the end of the function when the
2305 warnings about discarding 'volatile' have already been produced.
2306 We are making the variables as volatile just to force the compiler
2307 to preserve them between setjmp/longjmp, but we don't want warnings
2308 for them as they aren't really volatile. */
2309 void
2310 objc_finish_function (void)
2312 /* If there are any local variables to volatilize, volatilize them. */
2313 if (local_variables_to_volatilize)
2315 int i;
2316 tree decl;
2317 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
2319 tree t = TREE_TYPE (decl);
2321 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2322 TREE_TYPE (decl) = t;
2323 TREE_THIS_VOLATILE (decl) = 1;
2324 TREE_SIDE_EFFECTS (decl) = 1;
2325 DECL_REGISTER (decl) = 0;
2326 #ifndef OBJCPLUS
2327 C_DECL_REGISTER (decl) = 0;
2328 #endif
2331 /* Now we delete the vector. This sets it to NULL as well. */
2332 vec_free (local_variables_to_volatilize);
2336 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2337 (including its categories and superclasses) or by object type TYP.
2338 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2340 static bool
2341 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2343 bool class_type = (cls != NULL_TREE);
2345 while (cls)
2347 tree c;
2349 /* Check protocols adopted by the class and its categories. */
2350 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2352 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2353 return true;
2356 /* Repeat for superclasses. */
2357 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2360 /* Check for any protocols attached directly to the object type. */
2361 if (TYPE_HAS_OBJC_INFO (typ))
2363 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2364 return true;
2367 if (warn)
2369 *errbuf = 0;
2370 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2371 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2372 "implementing" a given protocol, since they do not have an
2373 implementation. */
2374 if (class_type)
2375 warning (0, "class %qs does not implement the %qE protocol",
2376 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2377 else
2378 warning (0, "type %qs does not conform to the %qE protocol",
2379 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2382 return false;
2385 /* Check if class RCLS and instance struct type RTYP conform to at least the
2386 same protocols that LCLS and LTYP conform to. */
2388 static bool
2389 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2391 tree p;
2392 bool have_lproto = false;
2394 while (lcls)
2396 /* NB: We do _not_ look at categories defined for LCLS; these may or
2397 may not get loaded in, and therefore it is unreasonable to require
2398 that RCLS/RTYP must implement any of their protocols. */
2399 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2401 have_lproto = true;
2403 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2404 return warn;
2407 /* Repeat for superclasses. */
2408 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2411 /* Check for any protocols attached directly to the object type. */
2412 if (TYPE_HAS_OBJC_INFO (ltyp))
2414 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2416 have_lproto = true;
2418 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2419 return warn;
2423 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2424 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2425 away with simply checking for 'id' or 'Class' (!RCLS), since this
2426 routine will not get called in other cases. */
2427 return have_lproto || (rcls != NULL_TREE);
2430 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2431 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2432 compatible by objc_compare_types() below. */
2434 tree
2435 objc_common_type (tree type1, tree type2)
2437 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2439 while (POINTER_TYPE_P (inner1))
2441 inner1 = TREE_TYPE (inner1);
2442 inner2 = TREE_TYPE (inner2);
2445 /* If one type is derived from another, return the base type. */
2446 if (DERIVED_FROM_P (inner1, inner2))
2447 return type1;
2448 else if (DERIVED_FROM_P (inner2, inner1))
2449 return type2;
2451 /* If both types are 'Class', return 'Class'. */
2452 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2453 return objc_class_type;
2455 /* Otherwise, return 'id'. */
2456 return objc_object_type;
2459 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2460 an instance of RTYP to an instance of LTYP or to compare the two
2461 (if ARGNO is equal to -3), per ObjC type system rules. Before
2462 returning 'true', this routine may issue warnings related to, e.g.,
2463 protocol conformance. When returning 'false', the routine must
2464 produce absolutely no warnings; the C or C++ front-end will do so
2465 instead, if needed. If either LTYP or RTYP is not an Objective-C
2466 type, the routine must return 'false'.
2468 The ARGNO parameter is encoded as follows:
2469 >= 1 Parameter number (CALLEE contains function being called);
2470 0 Return value;
2471 -1 Assignment;
2472 -2 Initialization;
2473 -3 Comparison (LTYP and RTYP may match in either direction);
2474 -4 Silent comparison (for C++ overload resolution);
2475 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2476 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2477 so that each object of type RTYP is also of type LTYP). This is used
2478 when comparing property types. */
2480 bool
2481 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2483 tree lcls, rcls, lproto, rproto;
2484 bool pointers_compatible;
2486 /* We must be dealing with pointer types */
2487 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2488 return false;
2490 tree ltyp_attr, rtyp_attr;
2493 /* Remove indirections, but keep the type attributes from the innermost
2494 pointer type, to check for NSObject. */
2495 ltyp_attr = TYPE_ATTRIBUTES (ltyp);
2496 ltyp = TREE_TYPE (ltyp);
2497 rtyp_attr = TYPE_ATTRIBUTES (rtyp);
2498 rtyp = TREE_TYPE (rtyp);
2500 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2502 /* We must also handle function pointers, since ObjC is a bit more
2503 lenient than C or C++ on this. */
2504 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2506 function_args_iterator liter, riter;
2508 /* Return types must be covariant. */
2509 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2510 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2511 argno, callee))
2512 return false;
2514 /* Argument types must be contravariant. */
2515 function_args_iter_init (&liter, ltyp);
2516 function_args_iter_init (&riter, rtyp);
2518 while (1)
2520 ltyp = function_args_iter_cond (&liter);
2521 rtyp = function_args_iter_cond (&riter);
2523 /* If we've exhaused both lists simulateously, we're done. */
2524 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2525 break;
2527 /* If one list is shorter than the other, they fail to match. */
2528 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2529 return false;
2531 if (!comptypes (rtyp, ltyp)
2532 && !objc_compare_types (rtyp, ltyp, argno, callee))
2533 return false;
2535 function_args_iter_next (&liter);
2536 function_args_iter_next (&riter);
2539 return true;
2542 /* We might have void * with NSObject type attr. */
2543 bool l_NSObject_p = ltyp_attr && lookup_attribute ("NSObject", ltyp_attr);
2544 bool r_NSObject_p = rtyp_attr && lookup_attribute ("NSObject", rtyp_attr);
2546 /* Past this point, we are only interested in ObjC class instances,
2547 or 'id' or 'Class' (except if the user applied the NSObject type
2548 attribute). */
2549 if ((TREE_CODE (ltyp) != RECORD_TYPE && !l_NSObject_p)
2550 || (TREE_CODE (rtyp) != RECORD_TYPE && !r_NSObject_p))
2551 return false;
2553 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2554 && !TYPE_HAS_OBJC_INFO (ltyp) && !l_NSObject_p)
2555 return false;
2557 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2558 && !TYPE_HAS_OBJC_INFO (rtyp) && !r_NSObject_p)
2559 return false;
2561 /* Past this point, we are committed to returning 'true' to the caller
2562 (unless performing a silent comparison; see below). However, we can
2563 still warn about type and/or protocol mismatches. */
2565 if (TYPE_HAS_OBJC_INFO (ltyp))
2567 lcls = TYPE_OBJC_INTERFACE (ltyp);
2568 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2570 else
2571 lcls = lproto = NULL_TREE;
2573 if (TYPE_HAS_OBJC_INFO (rtyp))
2575 rcls = TYPE_OBJC_INTERFACE (rtyp);
2576 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2578 else
2579 rcls = rproto = NULL_TREE;
2581 /* If we could not find an @interface declaration, we must have
2582 only seen a @class declaration; for purposes of type comparison,
2583 treat it as a stand-alone (root) class. */
2585 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2586 lcls = NULL_TREE;
2588 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2589 rcls = NULL_TREE;
2591 /* If either type is an unqualified 'id', we're done. This is because
2592 an 'id' can be assigned to or from any type with no warnings. When
2593 the pointer has NSObject attribute, consider that to be equivalent. */
2594 if (argno != -5)
2596 if ((!lproto && objc_is_object_id (ltyp))
2597 || (!rproto && objc_is_object_id (rtyp)))
2598 return true;
2599 if (l_NSObject_p || r_NSObject_p)
2600 return true;
2602 else
2604 /* For property checks, though, an 'id' is considered the most
2605 general type of object, hence if you try to specialize an
2606 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2607 to warn. */
2608 if (!lproto && (objc_is_object_id (ltyp) || l_NSObject_p))
2609 return true;
2612 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2614 /* If the underlying types are the same, and at most one of them has
2615 a protocol list, we do not need to issue any diagnostics. */
2616 if (pointers_compatible && (!lproto || !rproto))
2617 return true;
2619 /* If exactly one of the types is 'Class', issue a diagnostic; any
2620 exceptions of this rule have already been handled. */
2621 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2622 pointers_compatible = false;
2623 /* Otherwise, check for inheritance relations. */
2624 else
2626 if (!pointers_compatible)
2628 /* Again, if any of the two is an 'id', we're satisfied,
2629 unless we're comparing properties, in which case only an
2630 'id' on the left-hand side (old property) is good
2631 enough. */
2632 if (argno != -5)
2633 pointers_compatible
2634 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2635 else
2636 pointers_compatible = objc_is_object_id (ltyp);
2639 if (!pointers_compatible)
2640 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2642 if (!pointers_compatible && (argno == -3 || argno == -4))
2643 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2646 /* If the pointers match modulo protocols, check for protocol conformance
2647 mismatches. */
2648 if (pointers_compatible)
2650 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2651 argno != -3);
2653 if (!pointers_compatible && argno == -3)
2654 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2655 argno != -3);
2658 if (!pointers_compatible)
2660 /* The two pointers are not exactly compatible. Issue a warning, unless
2661 we are performing a silent comparison, in which case return 'false'
2662 instead. */
2663 /* NB: For the time being, we shall make our warnings look like their
2664 C counterparts. In the future, we may wish to make them more
2665 ObjC-specific. */
2666 switch (argno)
2668 case -5:
2669 case -4:
2670 return false;
2672 case -3:
2673 warning (0, "comparison of distinct Objective-C types lacks a cast");
2674 break;
2676 case -2:
2677 warning (0, "initialization from distinct Objective-C type");
2678 break;
2680 case -1:
2681 warning (0, "assignment from distinct Objective-C type");
2682 break;
2684 case 0:
2685 warning (0, "distinct Objective-C type in return");
2686 break;
2688 default:
2689 warning (0, "passing argument %d of %qE from distinct "
2690 "Objective-C type", argno, callee);
2691 break;
2695 return true;
2698 /* This routine is similar to objc_compare_types except that function-pointers are
2699 excluded. This is because, caller assumes that common types are of (id, Object*)
2700 variety and calls objc_common_type to obtain a common type. There is no commonolty
2701 between two function-pointers in this regard. */
2703 bool
2704 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2706 if (objc_compare_types (ltyp, rtyp, argno, callee))
2708 /* exclude function-pointer types. */
2711 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2712 rtyp = TREE_TYPE (rtyp);
2714 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2715 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2717 return false;
2720 #ifndef OBJCPLUS
2721 /* Determine if CHILD is derived from PARENT. The routine assumes that
2722 both parameters are RECORD_TYPEs, and is non-reflexive. */
2724 static bool
2725 objc_derived_from_p (tree parent, tree child)
2727 parent = TYPE_MAIN_VARIANT (parent);
2729 for (child = TYPE_MAIN_VARIANT (child);
2730 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2732 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2733 (TYPE_BINFO (child),
2734 0)));
2736 if (child == parent)
2737 return true;
2740 return false;
2742 #endif
2744 tree
2745 objc_build_component_ref (tree datum, tree component)
2747 /* If COMPONENT is NULL, the caller is referring to the anonymous
2748 base class field. */
2749 if (!component)
2751 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2753 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2756 /* The 'build_component_ref' routine has been removed from the C++
2757 front-end, but 'finish_class_member_access_expr' seems to be
2758 a worthy substitute. */
2759 #ifdef OBJCPLUS
2760 return finish_class_member_access_expr (datum, component, false,
2761 tf_warning_or_error);
2762 #else
2763 return build_component_ref (input_location, datum, component,
2764 UNKNOWN_LOCATION);
2765 #endif
2768 /* Recursively copy inheritance information rooted at BINFO. To do this,
2769 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2771 static tree
2772 objc_copy_binfo (tree binfo)
2774 tree btype = BINFO_TYPE (binfo);
2775 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2776 tree base_binfo;
2777 int ix;
2779 BINFO_TYPE (binfo2) = btype;
2780 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2781 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2783 /* Recursively copy base binfos of BINFO. */
2784 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2786 tree base_binfo2 = objc_copy_binfo (base_binfo);
2788 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2789 BINFO_BASE_APPEND (binfo2, base_binfo2);
2792 return binfo2;
2795 /* Record superclass information provided in BASETYPE for ObjC class REF.
2796 This is loosely based on cp/decl.c:xref_basetypes(). */
2798 static void
2799 objc_xref_basetypes (tree ref, tree basetype)
2801 tree variant;
2802 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2803 TYPE_BINFO (ref) = binfo;
2804 BINFO_OFFSET (binfo) = size_zero_node;
2805 BINFO_TYPE (binfo) = ref;
2807 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2808 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2809 TYPE_BINFO (variant) = binfo;
2811 if (basetype)
2813 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2815 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2816 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
2817 BINFO_BASE_APPEND (binfo, base_binfo);
2818 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2822 /* Called from finish_decl. */
2824 void
2825 objc_check_decl (tree decl)
2827 tree type = TREE_TYPE (decl);
2829 if (TREE_CODE (type) != RECORD_TYPE)
2830 return;
2831 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2832 error ("statically allocated instance of Objective-C class %qE",
2833 type);
2836 void
2837 objc_check_global_decl (tree decl)
2839 tree id = DECL_NAME (decl);
2840 if (objc_is_class_name (id) && global_bindings_p())
2841 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2844 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2845 INTERFACE may either name an Objective-C class, or refer to the
2846 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2847 type, just return it unchanged. This function is often called when
2848 PROTOCOLS is NULL_TREE, in which case we simply look up the
2849 appropriate INTERFACE. */
2851 tree
2852 objc_get_protocol_qualified_type (tree interface, tree protocols)
2854 /* If INTERFACE is not provided, default to 'id'. */
2855 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2856 bool is_ptr = (type != NULL_TREE);
2858 if (!is_ptr)
2860 type = objc_is_class_name (interface);
2862 if (type)
2864 /* If looking at a typedef, retrieve the precise type it
2865 describes. */
2866 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2867 interface = identifier_global_value (interface);
2869 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2870 && DECL_ORIGINAL_TYPE (interface))
2871 ? DECL_ORIGINAL_TYPE (interface)
2872 : xref_tag (RECORD_TYPE, type));
2874 else
2876 /* This case happens when we are given an 'interface' which
2877 is not a valid class name. For example if a typedef was
2878 used, and 'interface' really is the identifier of the
2879 typedef, but when you resolve it you don't get an
2880 Objective-C class, but something else, such as 'int'.
2881 This is an error; protocols make no sense unless you use
2882 them with Objective-C objects. */
2883 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2885 /* Try to recover. Ignore the invalid class name, and treat
2886 the object as an 'id' to silence further warnings about
2887 the class. */
2888 type = objc_object_type;
2889 is_ptr = true;
2893 if (protocols)
2895 type = build_variant_type_copy (type);
2897 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2898 to the pointee. */
2899 if (is_ptr)
2901 tree orig_pointee_type = TREE_TYPE (type);
2902 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2904 /* Set up the canonical type information. */
2905 TYPE_CANONICAL (type)
2906 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2908 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2909 type = TREE_TYPE (type);
2912 /* Look up protocols and install in lang specific list. */
2913 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2914 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2915 (protocols, /* definition_required */ false);
2917 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2918 return the pointer to the new pointee variant. */
2919 if (is_ptr)
2920 type = TYPE_POINTER_TO (type);
2921 else
2922 TYPE_OBJC_INTERFACE (type)
2923 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2926 return type;
2929 /* Check for circular dependencies in protocols. The arguments are
2930 PROTO, the protocol to check, and LIST, a list of protocol it
2931 conforms to. */
2933 static void
2934 check_protocol_recursively (tree proto, tree list)
2936 tree p;
2938 for (p = list; p; p = TREE_CHAIN (p))
2940 tree pp = TREE_VALUE (p);
2942 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2943 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2944 /* definition_required */ false);
2946 if (pp == proto)
2947 fatal_error (input_location, "protocol %qE has circular dependency",
2948 PROTOCOL_NAME (pp));
2949 if (pp)
2950 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2954 /* Look up PROTOCOLS, and return a list of those that are found. If
2955 none are found, return NULL. Note that this function will emit a
2956 warning if a protocol is found and is deprecated. If
2957 'definition_required', then warn if the protocol is found but is
2958 not defined (ie, if we only saw a forward-declaration of the
2959 protocol (as in "@protocol NSObject;") not a real definition with
2960 the list of methods). */
2961 static tree
2962 lookup_and_install_protocols (tree protocols, bool definition_required)
2964 tree proto;
2965 tree return_value = NULL_TREE;
2967 if (protocols == error_mark_node)
2968 return NULL;
2970 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2972 tree ident = TREE_VALUE (proto);
2973 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2974 definition_required);
2976 if (p)
2977 return_value = chainon (return_value,
2978 build_tree_list (NULL_TREE, p));
2979 else if (ident != error_mark_node)
2980 error ("cannot find protocol declaration for %qE",
2981 ident);
2984 return return_value;
2987 static void
2988 build_common_objc_exception_stuff (void)
2990 tree noreturn_list, nothrow_list, temp_type;
2992 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2993 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2995 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2996 /* void objc_sync_enter(id); */
2997 /* void objc_sync_exit(id); */
2998 temp_type = build_function_type_list (void_type_node,
2999 objc_object_type,
3000 NULL_TREE);
3001 objc_exception_throw_decl
3002 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
3003 noreturn_list);
3004 /* Make sure that objc_exception_throw (id) claims that it may throw an
3005 exception. */
3006 TREE_NOTHROW (objc_exception_throw_decl) = 0;
3008 objc_sync_enter_decl
3009 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
3010 NULL, nothrow_list);
3012 objc_sync_exit_decl
3013 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
3014 NULL, nothrow_list);
3017 /* Purpose: "play" parser, creating/installing representations
3018 of the declarations that are required by Objective-C.
3020 Model:
3022 type_spec--------->sc_spec
3023 (tree_list) (tree_list)
3026 identifier_node identifier_node */
3028 static void
3029 synth_module_prologue (void)
3031 tree type;
3032 enum debug_info_type save_write_symbols = write_symbols;
3033 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
3035 /* Suppress outputting debug symbols, because
3036 dbxout_init hasn't been called yet. */
3037 write_symbols = NO_DEBUG;
3038 debug_hooks = &do_nothing_debug_hooks;
3040 #ifdef OBJCPLUS
3041 push_lang_context (lang_name_c); /* extern "C" */
3042 #endif
3044 /* The following are also defined in <objc/objc.h> and friends. */
3046 objc_object_id = get_identifier (TAG_OBJECT);
3047 objc_class_id = get_identifier (TAG_CLASS);
3049 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
3050 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
3052 objc_object_type = build_pointer_type (objc_object_reference);
3053 objc_instancetype_type = build_pointer_type (objc_object_reference);
3054 objc_class_type = build_pointer_type (objc_class_reference);
3056 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
3057 objc_instancetype_name = get_identifier (INSTANCE_TYPEDEF_NAME);
3058 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
3059 objc_selector_name = get_identifier (SEL_TYPEDEF_NAME);
3061 /* Declare the 'id', 'instancetype' and 'Class' typedefs. */
3062 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3063 TYPE_DECL,
3064 objc_object_name,
3065 objc_object_type));
3066 TREE_NO_WARNING (type) = 1;
3068 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3069 TYPE_DECL,
3070 objc_instancetype_name,
3071 objc_instancetype_type));
3072 TREE_NO_WARNING (type) = 1;
3074 type = lang_hooks.decls.pushdecl (build_decl (input_location,
3075 TYPE_DECL,
3076 objc_class_name,
3077 objc_class_type));
3078 TREE_NO_WARNING (type) = 1;
3080 /* Forward-declare '@interface Protocol'. */
3081 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
3082 objc_declare_class (type);
3083 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
3085 /* Declare receiver type used for dispatching messages to 'super'. */
3086 /* `struct objc_super *' */
3087 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
3088 get_identifier (TAG_SUPER)));
3090 /* Declare pointers to method and ivar lists. */
3091 objc_method_list_ptr = build_pointer_type
3092 (xref_tag (RECORD_TYPE,
3093 get_identifier (UTAG_METHOD_LIST)));
3094 objc_method_proto_list_ptr
3095 = build_pointer_type (xref_tag (RECORD_TYPE,
3096 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3097 objc_ivar_list_ptr = build_pointer_type
3098 (xref_tag (RECORD_TYPE,
3099 get_identifier (UTAG_IVAR_LIST)));
3101 build_common_objc_exception_stuff ();
3103 /* Set-up runtime-specific templates, message and exception stuff. */
3104 (*runtime.initialize) ();
3106 /* Declare objc_getProperty, object_setProperty and other property
3107 accessor helpers. */
3108 build_common_objc_property_accessor_helpers ();
3110 /* Forward declare constant_string_id and constant_string_type. */
3111 if (!constant_string_class_name)
3112 constant_string_class_name = runtime.default_constant_string_class_name;
3113 constant_string_id = get_identifier (constant_string_class_name);
3114 objc_declare_class (constant_string_id);
3116 /* Pre-build the following entities - for speed/convenience. */
3117 self_id = get_identifier ("self");
3118 ucmd_id = get_identifier ("_cmd");
3120 /* Declare struct _objc_fast_enumeration_state { ... }; */
3121 build_fast_enumeration_state_template ();
3123 /* void objc_enumeration_mutation (id) */
3124 type = build_function_type_list (void_type_node,
3125 objc_object_type, NULL_TREE);
3126 objc_enumeration_mutation_decl
3127 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3128 NULL, NULL_TREE);
3129 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3131 #ifdef OBJCPLUS
3132 pop_lang_context ();
3133 #endif
3135 write_symbols = save_write_symbols;
3136 debug_hooks = save_hooks;
3139 /* --- const strings --- */
3141 /* Ensure that the ivar list for NSConstantString/NXConstantString
3142 (or whatever was specified via `-fconstant-string-class')
3143 contains fields at least as large as the following three, so that
3144 the runtime can stomp on them with confidence:
3146 struct STRING_OBJECT_CLASS_NAME
3148 Object isa;
3149 char *cString;
3150 unsigned int length;
3151 }; */
3153 static int
3154 check_string_class_template (void)
3156 tree field_decl = objc_get_class_ivars (constant_string_id);
3158 #define AT_LEAST_AS_LARGE_AS(F, T) \
3159 (F && TREE_CODE (F) == FIELD_DECL \
3160 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3161 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3163 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3164 return 0;
3166 field_decl = DECL_CHAIN (field_decl);
3167 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3168 return 0;
3170 field_decl = DECL_CHAIN (field_decl);
3171 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3173 #undef AT_LEAST_AS_LARGE_AS
3176 /* Avoid calling `check_string_class_template ()' more than once. */
3177 static GTY(()) int string_layout_checked;
3179 /* Construct an internal string layout to be used as a template for
3180 creating NSConstantString/NXConstantString instances. */
3182 static tree
3183 objc_build_internal_const_str_type (void)
3185 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3186 tree fields = build_decl (input_location,
3187 FIELD_DECL, NULL_TREE, ptr_type_node);
3188 tree field = build_decl (input_location,
3189 FIELD_DECL, NULL_TREE, ptr_type_node);
3191 DECL_CHAIN (field) = fields; fields = field;
3192 field = build_decl (input_location,
3193 FIELD_DECL, NULL_TREE, unsigned_type_node);
3194 DECL_CHAIN (field) = fields; fields = field;
3195 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3196 reverse order! */
3197 finish_builtin_struct (type, "__builtin_ObjCString",
3198 fields, NULL_TREE);
3200 return type;
3203 /* Custom build_string which sets TREE_TYPE! */
3205 tree
3206 my_build_string (int len, const char *str)
3208 return fix_string_type (build_string (len, str));
3211 /* Build a string with contents STR and length LEN and convert it to a
3212 pointer. */
3214 tree
3215 my_build_string_pointer (int len, const char *str)
3217 tree string = my_build_string (len, str);
3218 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3219 return build1 (ADDR_EXPR, ptrtype, string);
3222 hashval_t
3223 objc_string_hasher::hash (string_descriptor *ptr)
3225 const_tree const str = ptr->literal;
3226 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3227 int i, len = TREE_STRING_LENGTH (str);
3228 hashval_t h = len;
3230 for (i = 0; i < len; i++)
3231 h = ((h * 613) + p[i]);
3233 return h;
3236 bool
3237 objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2)
3239 const_tree const str1 = ptr1->literal;
3240 const_tree const str2 = ptr2->literal;
3241 int len1 = TREE_STRING_LENGTH (str1);
3243 return (len1 == TREE_STRING_LENGTH (str2)
3244 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3245 len1));
3248 /* Given a chain of STRING_CST's, build a static instance of
3249 NXConstantString which points at the concatenation of those
3250 strings. We place the string object in the __string_objects
3251 section of the __OBJC segment. The Objective-C runtime will
3252 initialize the isa pointers of the string objects to point at the
3253 NXConstantString class object. */
3255 tree
3256 objc_build_string_object (tree string)
3258 tree constant_string_class;
3259 int length;
3260 tree addr;
3261 struct string_descriptor *desc, key;
3263 /* We should be passed a STRING_CST. */
3264 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
3265 length = TREE_STRING_LENGTH (string) - 1;
3267 /* The target may have different ideas on how to construct an ObjC string
3268 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3269 constant CFString reference instead.
3270 At present, this is only supported for the NeXT runtime. */
3271 if (flag_next_runtime
3272 && targetcm.objc_construct_string_object)
3274 tree constructor = (*targetcm.objc_construct_string_object) (string);
3275 if (constructor)
3276 return build1 (NOP_EXPR, objc_object_type, constructor);
3279 /* Check whether the string class being used actually exists and has the
3280 correct ivar layout. */
3281 if (!string_layout_checked)
3283 string_layout_checked = -1;
3284 constant_string_class = lookup_interface (constant_string_id);
3285 internal_const_str_type = objc_build_internal_const_str_type ();
3287 if (!constant_string_class
3288 || !(constant_string_type
3289 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3290 error ("cannot find interface declaration for %qE",
3291 constant_string_id);
3292 /* The NSConstantString/NXConstantString ivar layout is now known. */
3293 else if (!check_string_class_template ())
3294 error ("interface %qE does not have valid constant string layout",
3295 constant_string_id);
3296 /* If the runtime can generate a literal reference to the string class,
3297 don't need to run a constructor. */
3298 else if (!(*runtime.setup_const_string_class_decl)())
3299 error ("cannot find reference tag for class %qE", constant_string_id);
3300 else
3302 string_layout_checked = 1; /* Success! */
3303 add_class_reference (constant_string_id);
3307 if (string_layout_checked == -1)
3308 return error_mark_node;
3310 /* Perhaps we already constructed a constant string just like this one? */
3311 key.literal = string;
3312 string_descriptor **loc = string_htab->find_slot (&key, INSERT);
3313 desc = *loc;
3315 if (!desc)
3317 *loc = desc = ggc_alloc<string_descriptor> ();
3318 desc->literal = string;
3319 desc->constructor =
3320 (*runtime.build_const_string_constructor) (input_location, string, length);
3323 addr = convert (build_pointer_type (constant_string_type),
3324 build_unary_op (input_location,
3325 ADDR_EXPR, desc->constructor, 1));
3327 return addr;
3330 /* Build a static constant CONSTRUCTOR
3331 with type TYPE and elements ELTS. */
3333 tree
3334 objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
3336 tree constructor = build_constructor (type, elts);
3338 TREE_CONSTANT (constructor) = 1;
3339 TREE_STATIC (constructor) = 1;
3340 TREE_READONLY (constructor) = 1;
3342 #ifdef OBJCPLUS
3343 /* Adjust for impedance mismatch. We should figure out how to build
3344 CONSTRUCTORs that consistently please both the C and C++ gods. */
3345 if (!(*elts)[0].index)
3346 TREE_TYPE (constructor) = init_list_type_node;
3347 #endif
3349 return constructor;
3352 /* Return the DECL of the string IDENT in the SECTION. */
3354 tree
3355 get_objc_string_decl (tree ident, enum string_section section)
3357 tree chain;
3359 switch (section)
3361 case class_names:
3362 chain = class_names_chain;
3363 break;
3364 case meth_var_names:
3365 chain = meth_var_names_chain;
3366 break;
3367 case meth_var_types:
3368 chain = meth_var_types_chain;
3369 break;
3370 case prop_names_attr:
3371 chain = prop_names_attr_chain;
3372 break;
3373 default:
3374 gcc_unreachable ();
3377 for (; chain != 0; chain = TREE_CHAIN (chain))
3378 if (TREE_VALUE (chain) == ident)
3379 return (TREE_PURPOSE (chain));
3381 /* We didn't find the entry. */
3382 return NULL_TREE;
3385 /* Create a class reference, but don't create a variable to reference
3386 it. */
3388 void
3389 add_class_reference (tree ident)
3391 tree chain;
3393 if ((chain = cls_ref_chain))
3395 tree tail;
3398 if (ident == TREE_VALUE (chain))
3399 return;
3401 tail = chain;
3402 chain = TREE_CHAIN (chain);
3404 while (chain);
3406 /* Append to the end of the list */
3407 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3409 else
3410 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3413 /* Get a class reference, creating it if necessary. Also create the
3414 reference variable. */
3415 tree
3416 objc_get_class_reference (tree ident)
3418 tree orig_ident = (DECL_P (ident)
3419 ? DECL_NAME (ident)
3420 : TYPE_P (ident)
3421 ? OBJC_TYPE_NAME (ident)
3422 : ident);
3423 bool local_scope = false;
3425 #ifdef OBJCPLUS
3426 if (processing_template_decl)
3427 /* Must wait until template instantiation time. */
3428 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
3429 #endif
3431 if (TREE_CODE (ident) == TYPE_DECL)
3432 ident = (DECL_ORIGINAL_TYPE (ident)
3433 ? DECL_ORIGINAL_TYPE (ident)
3434 : TREE_TYPE (ident));
3436 #ifdef OBJCPLUS
3437 if (TYPE_P (ident)
3438 && CP_TYPE_CONTEXT (ident) != global_namespace)
3439 local_scope = true;
3440 #endif
3442 if (local_scope || !(ident = objc_is_class_name (ident)))
3444 error ("%qE is not an Objective-C class name or alias",
3445 orig_ident);
3446 return error_mark_node;
3449 return (*runtime.get_class_reference) (ident);
3452 void
3453 objc_declare_alias (tree alias_ident, tree class_ident)
3455 tree underlying_class;
3457 #ifdef OBJCPLUS
3458 if (current_namespace != global_namespace) {
3459 error ("Objective-C declarations may only appear in global scope");
3461 #endif /* OBJCPLUS */
3463 if (!(underlying_class = objc_is_class_name (class_ident)))
3464 warning (0, "cannot find class %qE", class_ident);
3465 else if (objc_is_class_name (alias_ident))
3466 warning (0, "class %qE already exists", alias_ident);
3467 else
3469 /* Implement @compatibility_alias as a typedef. */
3470 #ifdef OBJCPLUS
3471 push_lang_context (lang_name_c); /* extern "C" */
3472 #endif
3473 lang_hooks.decls.pushdecl (build_decl
3474 (input_location,
3475 TYPE_DECL,
3476 alias_ident,
3477 xref_tag (RECORD_TYPE, underlying_class)));
3478 #ifdef OBJCPLUS
3479 pop_lang_context ();
3480 #endif
3481 objc_map_put (alias_name_map, alias_ident, underlying_class);
3485 void
3486 objc_declare_class (tree identifier)
3488 #ifdef OBJCPLUS
3489 if (current_namespace != global_namespace) {
3490 error ("Objective-C declarations may only appear in global scope");
3492 #endif /* OBJCPLUS */
3494 if (! objc_is_class_name (identifier))
3496 tree record = lookup_name (identifier), type = record;
3498 if (record)
3500 if (TREE_CODE (record) == TYPE_DECL)
3501 type = DECL_ORIGINAL_TYPE (record)
3502 ? DECL_ORIGINAL_TYPE (record)
3503 : TREE_TYPE (record);
3505 if (!TYPE_HAS_OBJC_INFO (type)
3506 || !TYPE_OBJC_INTERFACE (type))
3508 error ("%qE redeclared as different kind of symbol",
3509 identifier);
3510 error ("previous declaration of %q+D",
3511 record);
3515 record = xref_tag (RECORD_TYPE, identifier);
3516 INIT_TYPE_OBJC_INFO (record);
3517 /* In the case of a @class declaration, we store the ident in
3518 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3519 we'll replace the ident with the interface. */
3520 TYPE_OBJC_INTERFACE (record) = identifier;
3521 objc_map_put (class_name_map, identifier, NULL_TREE);
3525 tree
3526 objc_is_class_name (tree ident)
3528 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3530 tree t = identifier_global_value (ident);
3531 if (t)
3532 ident = t;
3535 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3536 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3538 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3539 ident = OBJC_TYPE_NAME (ident);
3540 #ifdef OBJCPLUS
3541 if (ident && TREE_CODE (ident) == TYPE_DECL)
3543 tree type = TREE_TYPE (ident);
3544 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3545 return NULL_TREE;
3546 ident = DECL_NAME (ident);
3548 #endif
3549 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3550 return NULL_TREE;
3552 if (lookup_interface (ident))
3553 return ident;
3556 tree target;
3558 target = objc_map_get (class_name_map, ident);
3559 if (target != OBJC_MAP_NOT_FOUND)
3560 return ident;
3562 target = objc_map_get (alias_name_map, ident);
3563 if (target != OBJC_MAP_NOT_FOUND)
3564 return target;
3567 return 0;
3570 /* Check whether TYPE is either 'id' or 'Class'. */
3572 tree
3573 objc_is_id (tree type)
3575 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3577 tree t = identifier_global_value (type);
3578 if (t)
3579 type = t;
3582 if (type && TREE_CODE (type) == TYPE_DECL)
3583 type = TREE_TYPE (type);
3585 /* NB: This function may be called before the ObjC front-end has
3586 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3587 return (objc_object_type && type
3588 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3589 ? type
3590 : NULL_TREE);
3593 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3594 class instance. This is needed by other parts of the compiler to
3595 handle ObjC types gracefully. */
3597 tree
3598 objc_is_object_ptr (tree type)
3600 tree ret;
3602 type = TYPE_MAIN_VARIANT (type);
3603 if (!POINTER_TYPE_P (type))
3604 return 0;
3606 ret = objc_is_id (type);
3607 if (!ret)
3608 ret = objc_is_class_name (TREE_TYPE (type));
3610 return ret;
3613 static int
3614 objc_is_gcable_type (tree type, int or_strong_p)
3616 tree name;
3618 if (!TYPE_P (type))
3619 return 0;
3620 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3621 return 1;
3622 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3623 return 1;
3624 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3625 return 0;
3626 type = TREE_TYPE (type);
3627 if (TREE_CODE (type) != RECORD_TYPE)
3628 return 0;
3629 name = TYPE_NAME (type);
3630 return (objc_is_class_name (name) != NULL_TREE);
3633 static tree
3634 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3636 if (expr == oldexpr)
3637 return newexpr;
3639 switch (TREE_CODE (expr))
3641 case COMPONENT_REF:
3642 return objc_build_component_ref
3643 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3644 oldexpr,
3645 newexpr),
3646 DECL_NAME (TREE_OPERAND (expr, 1)));
3647 case ARRAY_REF:
3648 return build_array_ref (input_location,
3649 objc_substitute_decl (TREE_OPERAND (expr, 0),
3650 oldexpr,
3651 newexpr),
3652 TREE_OPERAND (expr, 1));
3653 case INDIRECT_REF:
3654 return build_indirect_ref (input_location,
3655 objc_substitute_decl (TREE_OPERAND (expr, 0),
3656 oldexpr,
3657 newexpr), RO_ARROW);
3658 default:
3659 return expr;
3663 static tree
3664 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3666 tree func_params;
3667 /* The LHS parameter contains the expression 'outervar->memberspec';
3668 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3669 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3671 tree offs
3672 = objc_substitute_decl
3673 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3674 tree func
3675 = (flag_objc_direct_dispatch
3676 ? objc_assign_ivar_fast_decl
3677 : objc_assign_ivar_decl);
3679 offs = convert (integer_type_node, build_unary_op (input_location,
3680 ADDR_EXPR, offs, 0));
3681 offs = fold (offs);
3682 func_params = tree_cons (NULL_TREE,
3683 convert (objc_object_type, rhs),
3684 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3685 tree_cons (NULL_TREE, offs,
3686 NULL_TREE)));
3688 return build_function_call (input_location, func, func_params);
3691 static tree
3692 objc_build_global_assignment (tree lhs, tree rhs)
3694 tree func_params = tree_cons (NULL_TREE,
3695 convert (objc_object_type, rhs),
3696 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3697 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3698 NULL_TREE));
3700 return build_function_call (input_location,
3701 objc_assign_global_decl, func_params);
3704 static tree
3705 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3707 tree func_params = tree_cons (NULL_TREE,
3708 convert (objc_object_type, rhs),
3709 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3710 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3711 NULL_TREE));
3713 return build_function_call (input_location,
3714 objc_assign_strong_cast_decl, func_params);
3717 static int
3718 objc_is_gcable_p (tree expr)
3720 return (TREE_CODE (expr) == COMPONENT_REF
3721 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3722 : TREE_CODE (expr) == ARRAY_REF
3723 ? (objc_is_gcable_p (TREE_TYPE (expr))
3724 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3725 : TREE_CODE (expr) == ARRAY_TYPE
3726 ? objc_is_gcable_p (TREE_TYPE (expr))
3727 : TYPE_P (expr)
3728 ? objc_is_gcable_type (expr, 1)
3729 : (objc_is_gcable_p (TREE_TYPE (expr))
3730 || (DECL_P (expr)
3731 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3734 static int
3735 objc_is_ivar_reference_p (tree expr)
3737 return (TREE_CODE (expr) == ARRAY_REF
3738 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3739 : TREE_CODE (expr) == COMPONENT_REF
3740 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3741 : 0);
3744 static int
3745 objc_is_global_reference_p (tree expr)
3747 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3748 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3749 : DECL_P (expr)
3750 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3751 : 0);
3754 tree
3755 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3757 tree result = NULL_TREE, outer;
3758 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3760 /* This function is currently only used with the next runtime with
3761 garbage collection enabled (-fobjc-gc). */
3762 gcc_assert (flag_next_runtime);
3764 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3765 will have been transformed to the form '*(type *)&expr'. */
3766 if (TREE_CODE (lhs) == INDIRECT_REF)
3768 outer = TREE_OPERAND (lhs, 0);
3770 while (!strong_cast_p
3771 && (CONVERT_EXPR_P (outer)
3772 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3774 tree lhstype = TREE_TYPE (outer);
3776 /* Descend down the cast chain, and record the first objc_gc
3777 attribute found. */
3778 if (POINTER_TYPE_P (lhstype))
3780 tree attr
3781 = lookup_attribute ("objc_gc",
3782 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3784 if (attr)
3785 strong_cast_p = 1;
3788 outer = TREE_OPERAND (outer, 0);
3792 /* If we have a __strong cast, it trumps all else. */
3793 if (strong_cast_p)
3795 if (modifycode != NOP_EXPR)
3796 goto invalid_pointer_arithmetic;
3798 if (warn_assign_intercept)
3799 warning (0, "strong-cast assignment has been intercepted");
3801 result = objc_build_strong_cast_assignment (lhs, rhs);
3803 goto exit_point;
3806 /* the lhs must be of a suitable type, regardless of its underlying
3807 structure. */
3808 if (!objc_is_gcable_p (lhs))
3809 goto exit_point;
3811 outer = lhs;
3813 while (outer
3814 && (TREE_CODE (outer) == COMPONENT_REF
3815 || TREE_CODE (outer) == ARRAY_REF))
3816 outer = TREE_OPERAND (outer, 0);
3818 if (TREE_CODE (outer) == INDIRECT_REF)
3820 outer = TREE_OPERAND (outer, 0);
3821 indirect_p = 1;
3824 outer_gc_p = objc_is_gcable_p (outer);
3826 /* Handle ivar assignments. */
3827 if (objc_is_ivar_reference_p (lhs))
3829 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3830 doesn't cut it here), the best we can do here is suggest a cast. */
3831 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3833 /* We may still be able to use the global write barrier... */
3834 if (!indirect_p && objc_is_global_reference_p (outer))
3835 goto global_reference;
3837 suggest_cast:
3838 if (modifycode == NOP_EXPR)
3840 if (warn_assign_intercept)
3841 warning (0, "strong-cast may possibly be needed");
3844 goto exit_point;
3847 if (modifycode != NOP_EXPR)
3848 goto invalid_pointer_arithmetic;
3850 if (warn_assign_intercept)
3851 warning (0, "instance variable assignment has been intercepted");
3853 result = objc_build_ivar_assignment (outer, lhs, rhs);
3855 goto exit_point;
3858 /* Likewise, intercept assignment to global/static variables if their type is
3859 GC-marked. */
3860 if (objc_is_global_reference_p (outer))
3862 if (indirect_p)
3863 goto suggest_cast;
3865 global_reference:
3866 if (modifycode != NOP_EXPR)
3868 invalid_pointer_arithmetic:
3869 if (outer_gc_p)
3870 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3872 goto exit_point;
3875 if (warn_assign_intercept)
3876 warning (0, "global/static variable assignment has been intercepted");
3878 result = objc_build_global_assignment (lhs, rhs);
3881 /* In all other cases, fall back to the normal mechanism. */
3882 exit_point:
3883 return result;
3886 /* Implementation of the table mapping a class name (as an identifier)
3887 to a class node. The two public functions for it are
3888 lookup_interface() and add_interface(). add_interface() is only
3889 used in this file, so we can make it static. */
3891 static GTY(()) objc_map_t interface_map;
3893 static void
3894 interface_hash_init (void)
3896 interface_map = objc_map_alloc_ggc (200);
3899 static tree
3900 add_interface (tree class_name, tree name)
3902 /* Put interfaces on list in reverse order. */
3903 TREE_CHAIN (class_name) = interface_chain;
3904 interface_chain = class_name;
3906 /* Add it to the map. */
3907 objc_map_put (interface_map, name, class_name);
3909 return interface_chain;
3912 tree
3913 lookup_interface (tree ident)
3915 #ifdef OBJCPLUS
3916 if (ident && TREE_CODE (ident) == TYPE_DECL)
3917 ident = DECL_NAME (ident);
3918 #endif
3920 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3921 return NULL_TREE;
3924 tree interface = objc_map_get (interface_map, ident);
3926 if (interface == OBJC_MAP_NOT_FOUND)
3927 return NULL_TREE;
3928 else
3929 return interface;
3935 /* Implement @defs (<classname>) within struct bodies. */
3937 tree
3938 objc_get_class_ivars (tree class_name)
3940 tree interface = lookup_interface (class_name);
3942 if (interface)
3943 return get_class_ivars (interface, true);
3945 error ("cannot find interface declaration for %qE",
3946 class_name);
3948 return error_mark_node;
3952 /* Functions used by the hashtable for field duplicates in
3953 objc_detect_field_duplicates(). Ideally, we'd use a standard
3954 key-value dictionary hashtable , and store as keys the field names,
3955 and as values the actual declarations (used to print nice error
3956 messages with the locations). But, the hashtable we are using only
3957 allows us to store keys in the hashtable, without values (it looks
3958 more like a set). So, we store the DECLs, but define equality as
3959 DECLs having the same name, and hash as the hash of the name. */
3961 struct decl_name_hash : nofree_ptr_hash <tree_node>
3963 static inline hashval_t hash (const tree_node *);
3964 static inline bool equal (const tree_node *, const tree_node *);
3967 inline hashval_t
3968 decl_name_hash::hash (const tree_node *q)
3970 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3973 inline bool
3974 decl_name_hash::equal (const tree_node *a, const tree_node *b)
3976 return DECL_NAME (a) == DECL_NAME (b);
3979 /* Called when checking the variables in a struct. If we are not
3980 doing the ivars list inside an @interface context, then return
3981 false. Else, perform the check for duplicate ivars, then return
3982 true. The check for duplicates checks if an instance variable with
3983 the same name exists in the class or in a superclass. If
3984 'check_superclasses_only' is set to true, then it is assumed that
3985 checks for instance variables in the same class has already been
3986 performed (this is the case for ObjC++) and only the instance
3987 variables of superclasses are checked. */
3988 bool
3989 objc_detect_field_duplicates (bool check_superclasses_only)
3991 if (!objc_collecting_ivars || !objc_interface_context
3992 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3993 return false;
3995 /* We have two ways of doing this check:
3997 "direct comparison": we iterate over the instance variables and
3998 compare them directly. This works great for small numbers of
3999 instance variables (such as 10 or 20), which are extremely common.
4000 But it will potentially take forever for the pathological case with
4001 a huge number (eg, 10k) of instance variables.
4003 "hashtable": we use a hashtable, which requires a single sweep
4004 through the list of instances variables. This is much slower for a
4005 small number of variables, and we only use it for large numbers.
4007 To decide which one to use, we need to get an idea of how many
4008 instance variables we have to compare. */
4010 unsigned int number_of_ivars_to_check = 0;
4012 tree ivar;
4013 for (ivar = CLASS_RAW_IVARS (objc_interface_context);
4014 ivar; ivar = DECL_CHAIN (ivar))
4016 /* Ignore anonymous ivars. */
4017 if (DECL_NAME (ivar))
4018 number_of_ivars_to_check++;
4022 /* Exit if there is nothing to do. */
4023 if (number_of_ivars_to_check == 0)
4024 return true;
4026 /* In case that there are only 1 or 2 instance variables to check,
4027 we always use direct comparison. If there are more, it is
4028 worth iterating over the instance variables in the superclass
4029 to count how many there are (note that this has the same cost
4030 as checking 1 instance variable by direct comparison, which is
4031 why we skip this check in the case of 1 or 2 ivars and just do
4032 the direct comparison) and then decide if it worth using a
4033 hashtable. */
4034 if (number_of_ivars_to_check > 2)
4036 unsigned int number_of_superclass_ivars = 0;
4038 tree interface;
4039 for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context));
4040 interface; interface = lookup_interface (CLASS_SUPER_NAME (interface)))
4042 tree ivar;
4043 for (ivar = CLASS_RAW_IVARS (interface);
4044 ivar; ivar = DECL_CHAIN (ivar))
4045 number_of_superclass_ivars++;
4049 /* We use a hashtable if we have over 10k comparisons. */
4050 if (number_of_ivars_to_check * (number_of_superclass_ivars
4051 + (number_of_ivars_to_check / 2))
4052 > 10000)
4054 /* First, build the hashtable by putting all the instance
4055 variables of superclasses in it. */
4056 hash_table<decl_name_hash> htab (37);
4057 tree interface;
4058 for (interface = lookup_interface (CLASS_SUPER_NAME
4059 (objc_interface_context));
4060 interface; interface = lookup_interface
4061 (CLASS_SUPER_NAME (interface)))
4063 tree ivar;
4064 for (ivar = CLASS_RAW_IVARS (interface); ivar;
4065 ivar = DECL_CHAIN (ivar))
4067 if (DECL_NAME (ivar) != NULL_TREE)
4069 tree_node **slot = htab.find_slot (ivar, INSERT);
4070 /* Do not check for duplicate instance
4071 variables in superclasses. Errors have
4072 already been generated. */
4073 *slot = ivar;
4078 /* Now, we go through all the instance variables in the
4079 class, and check that they are not in the
4080 hashtable. */
4081 if (check_superclasses_only)
4083 tree ivar;
4084 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
4085 ivar = DECL_CHAIN (ivar))
4087 if (DECL_NAME (ivar) != NULL_TREE)
4089 tree duplicate_ivar = htab.find (ivar);
4090 if (duplicate_ivar != HTAB_EMPTY_ENTRY)
4092 error_at (DECL_SOURCE_LOCATION (ivar),
4093 "duplicate instance variable %q+D",
4094 ivar);
4095 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4096 "previous declaration of %q+D",
4097 duplicate_ivar);
4098 /* FIXME: Do we need the following ? */
4099 /* DECL_NAME (ivar) = NULL_TREE; */
4104 else
4106 /* If we're checking for duplicates in the class as
4107 well, we insert variables in the hashtable as we
4108 check them, so if a duplicate follows, it will be
4109 caught. */
4110 tree ivar;
4111 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
4112 ivar = DECL_CHAIN (ivar))
4114 if (DECL_NAME (ivar) != NULL_TREE)
4116 tree_node **slot = htab.find_slot (ivar, INSERT);
4117 if (*slot)
4119 tree duplicate_ivar = (tree)(*slot);
4120 error_at (DECL_SOURCE_LOCATION (ivar),
4121 "duplicate instance variable %q+D",
4122 ivar);
4123 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4124 "previous declaration of %q+D",
4125 duplicate_ivar);
4126 /* FIXME: Do we need the following ? */
4127 /* DECL_NAME (ivar) = NULL_TREE; */
4129 *slot = ivar;
4133 return true;
4138 /* This is the "direct comparison" approach, which is used in most
4139 non-pathological cases. */
4141 /* Walk up to class hierarchy, starting with this class (this is
4142 the external loop, because lookup_interface() is expensive, and
4143 we want to do it few times). */
4144 tree interface = objc_interface_context;
4146 if (check_superclasses_only)
4147 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4149 for ( ; interface; interface = lookup_interface
4150 (CLASS_SUPER_NAME (interface)))
4152 tree ivar_being_checked;
4154 for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context);
4155 ivar_being_checked;
4156 ivar_being_checked = DECL_CHAIN (ivar_being_checked))
4158 tree decl;
4160 /* Ignore anonymous ivars. */
4161 if (DECL_NAME (ivar_being_checked) == NULL_TREE)
4162 continue;
4164 /* Note how we stop when we find the ivar we are checking
4165 (this can only happen in the main class, not
4166 superclasses), to avoid comparing things twice
4167 (otherwise, for each ivar, you'd compare A to B then B
4168 to A, and get duplicated error messages). */
4169 for (decl = CLASS_RAW_IVARS (interface);
4170 decl && decl != ivar_being_checked;
4171 decl = DECL_CHAIN (decl))
4173 if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl))
4175 error_at (DECL_SOURCE_LOCATION (ivar_being_checked),
4176 "duplicate instance variable %q+D",
4177 ivar_being_checked);
4178 inform (DECL_SOURCE_LOCATION (decl),
4179 "previous declaration of %q+D",
4180 decl);
4181 /* FIXME: Do we need the following ? */
4182 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4188 return true;
4191 /* Used by: build_private_template, continue_class,
4192 and for @defs constructs. */
4194 static tree
4195 get_class_ivars (tree interface, bool inherited)
4197 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4199 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4200 by the current class (i.e., they do not include super-class ivars).
4201 However, the CLASS_IVARS list will be side-effected by a call to
4202 finish_struct(), which will fill in field offsets. */
4203 if (!CLASS_IVARS (interface))
4204 CLASS_IVARS (interface) = ivar_chain;
4206 if (!inherited)
4207 return ivar_chain;
4209 while (CLASS_SUPER_NAME (interface))
4211 /* Prepend super-class ivars. */
4212 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4213 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4214 ivar_chain);
4217 return ivar_chain;
4220 void
4221 objc_maybe_warn_exceptions (location_t loc)
4223 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4224 For example, on Darwin, ObjC exceptions require a sufficiently
4225 recent version of the runtime, so the user must ask for them
4226 explicitly. On other platforms, at the moment -fobjc-exceptions
4227 triggers -fexceptions which again is required for exceptions to
4228 work. */
4229 if (!flag_objc_exceptions)
4231 /* Warn only once per compilation unit. */
4232 static bool warned = false;
4234 if (!warned)
4236 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4237 warned = true;
4242 static struct objc_try_context *cur_try_context;
4244 /* Called just after parsing the @try and its associated BODY. We now
4245 must prepare for the tricky bits -- handling the catches and finally. */
4247 void
4248 objc_begin_try_stmt (location_t try_locus, tree body)
4250 struct objc_try_context *c = XCNEW (struct objc_try_context);
4251 c->outer = cur_try_context;
4252 c->try_body = body;
4253 c->try_locus = try_locus;
4254 c->end_try_locus = input_location;
4255 cur_try_context = c;
4257 /* Collect the list of local variables. We'll mark them as volatile
4258 at the end of compilation of this function to prevent them being
4259 clobbered by setjmp/longjmp. */
4260 if (flag_objc_sjlj_exceptions)
4261 objc_mark_locals_volatile (NULL);
4264 /* Called just after parsing "@catch (parm)". Open a binding level,
4265 enter DECL into the binding level, and initialize it. Leave the
4266 binding level open while the body of the compound statement is
4267 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4268 which we compile as "@catch(id tmp_variable)". */
4270 void
4271 objc_begin_catch_clause (tree decl)
4273 tree compound, type, t;
4274 bool ellipsis = false;
4276 /* Begin a new scope that the entire catch clause will live in. */
4277 compound = c_begin_compound_stmt (true);
4279 /* Create the appropriate declaration for the argument. */
4280 if (decl == error_mark_node)
4281 type = error_mark_node;
4282 else
4284 if (decl == NULL_TREE)
4286 /* If @catch(...) was specified, create a temporary variable of
4287 type 'id' and use it. */
4288 decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var");
4289 DECL_SOURCE_LOCATION (decl) = input_location;
4290 /* ... but allow the runtime to differentiate between ellipsis and the
4291 case of @catch (id xyz). */
4292 ellipsis = true;
4294 else
4296 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4297 decl = build_decl (input_location,
4298 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4300 lang_hooks.decls.pushdecl (decl);
4302 /* Mark the declaration as used so you never any warnings whether
4303 you use the exception argument or not. TODO: Implement a
4304 -Wunused-exception-parameter flag, which would cause warnings
4305 if exception parameter is not used. */
4306 TREE_USED (decl) = 1;
4307 DECL_READ_P (decl) = 1;
4309 type = TREE_TYPE (decl);
4312 /* Verify that the type of the catch is valid. It must be a pointer
4313 to an Objective-C class, or "id" (which is catch-all). */
4314 if (type == error_mark_node)
4316 ;/* Just keep going. */
4318 else if (!objc_type_valid_for_messaging (type, false))
4320 error ("%<@catch%> parameter is not a known Objective-C class type");
4321 type = error_mark_node;
4323 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type))
4324 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type)))
4326 error ("%<@catch%> parameter cannot be protocol-qualified");
4327 type = error_mark_node;
4329 else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4330 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4331 identify 'id'. */
4333 else
4335 /* If 'type' was built using typedefs, we need to get rid of
4336 them and get a simple pointer to the class. */
4337 bool is_typedef = false;
4338 tree x = TYPE_MAIN_VARIANT (type);
4340 /* Skip from the pointer to the pointee. */
4341 if (TREE_CODE (x) == POINTER_TYPE)
4342 x = TREE_TYPE (x);
4344 /* Traverse typedef aliases */
4345 while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x)
4346 && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL
4347 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)))
4349 is_typedef = true;
4350 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4353 /* If it was a typedef, build a pointer to the final, original
4354 class. */
4355 if (is_typedef)
4356 type = build_pointer_type (x);
4358 if (cur_try_context->catch_list)
4360 /* Examine previous @catch clauses and see if we've already
4361 caught the type in question. */
4362 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4363 for (; !tsi_end_p (i); tsi_next (&i))
4365 tree stmt = tsi_stmt (i);
4366 t = CATCH_TYPES (stmt);
4367 if (t == error_mark_node)
4368 continue;
4369 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4371 warning (0, "exception of type %<%T%> will be caught",
4372 TREE_TYPE (type));
4373 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4374 TREE_TYPE (t ? t : objc_object_type));
4375 break;
4381 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4382 add_stmt (t);
4385 /* Called just after parsing the closing brace of a @catch clause. Close
4386 the open binding level, and record a CATCH_EXPR for it. */
4388 void
4389 objc_finish_catch_clause (void)
4391 tree c = cur_try_context->current_catch;
4392 cur_try_context->current_catch = NULL;
4393 cur_try_context->end_catch_locus = input_location;
4395 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4397 (*runtime.finish_catch) (&cur_try_context, c);
4400 /* Called after parsing a @finally clause and its associated BODY.
4401 Record the body for later placement. */
4403 void
4404 objc_build_finally_clause (location_t finally_locus, tree body)
4406 cur_try_context->finally_body = body;
4407 cur_try_context->finally_locus = finally_locus;
4408 cur_try_context->end_finally_locus = input_location;
4411 /* Called to finalize a @try construct. */
4413 tree
4414 objc_finish_try_stmt (void)
4416 struct objc_try_context *c = cur_try_context;
4417 tree stmt;
4419 if (c->catch_list == NULL && c->finally_body == NULL)
4420 error ("%<@try%> without %<@catch%> or %<@finally%>");
4422 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4423 add_stmt (stmt);
4425 cur_try_context = c->outer;
4426 free (c);
4427 return stmt;
4430 tree
4431 objc_build_throw_stmt (location_t loc, tree throw_expr)
4433 bool rethrown = false;
4435 objc_maybe_warn_exceptions (loc);
4437 /* Don't waste time trying to build something if we're already dead. */
4438 if (throw_expr == error_mark_node)
4439 return error_mark_node;
4441 if (throw_expr == NULL)
4443 /* If we're not inside a @catch block, there is no "current
4444 exception" to be rethrown. */
4445 if (cur_try_context == NULL
4446 || cur_try_context->current_catch == NULL)
4448 error_at (loc,
4449 "%<@throw%> (rethrow) used outside of a %<@catch%> block");
4450 return error_mark_node;
4453 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4454 value that we get from the runtime. */
4455 throw_expr = (*runtime.build_exc_ptr) (&cur_try_context);
4456 rethrown = true;
4458 else
4460 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
4462 error_at (loc, "%<@throw%> argument is not an object");
4463 return error_mark_node;
4467 return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown);
4470 tree
4471 objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
4473 /* object_expr should never be NULL; but in case it is, convert it to
4474 error_mark_node. */
4475 if (object_expr == NULL)
4476 object_expr = error_mark_node;
4478 /* Validate object_expr. If not valid, set it to error_mark_node. */
4479 if (object_expr != error_mark_node)
4481 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true))
4483 error_at (start_locus, "%<@synchronized%> argument is not an object");
4484 object_expr = error_mark_node;
4488 if (object_expr == error_mark_node)
4490 /* If we found an error, we simply ignore the '@synchronized'.
4491 Compile the body so we can keep going with minimal
4492 casualties. */
4493 return add_stmt (body);
4495 else
4497 tree call;
4498 tree args;
4500 /* objc_sync_enter (object_expr); */
4501 object_expr = save_expr (object_expr);
4502 args = tree_cons (NULL, object_expr, NULL);
4503 call = build_function_call (input_location,
4504 objc_sync_enter_decl, args);
4505 SET_EXPR_LOCATION (call, start_locus);
4506 add_stmt (call);
4508 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4509 it goes inside the @finalize() clause. */
4510 args = tree_cons (NULL, object_expr, NULL);
4511 call = build_function_call (input_location,
4512 objc_sync_exit_decl, args);
4513 SET_EXPR_LOCATION (call, input_location);
4515 /* @try { body; } */
4516 objc_begin_try_stmt (start_locus, body);
4518 /* @finally { objc_sync_exit (object_expr); } */
4519 objc_build_finally_clause (input_location, call);
4521 /* End of try statement. */
4522 return objc_finish_try_stmt ();
4526 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4527 name as the class:
4529 struct <classname> {
4530 struct _objc_class *isa;
4532 }; */
4534 static void
4535 build_private_template (tree klass)
4537 if (!CLASS_STATIC_TEMPLATE (klass))
4539 tree record = objc_build_struct (klass,
4540 get_class_ivars (klass, false),
4541 CLASS_SUPER_NAME (klass));
4543 /* Set the TREE_USED bit for this struct, so that stab generator
4544 can emit stabs for this struct type. */
4545 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4546 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4548 /* Copy the attributes from the class to the type. */
4549 if (TREE_DEPRECATED (klass))
4550 TREE_DEPRECATED (record) = 1;
4554 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4555 current class. */
4556 #ifdef OBJCPLUS
4557 static void
4558 objc_generate_cxx_ctor_or_dtor (bool dtor)
4560 tree fn, body, compound_stmt, ivar;
4562 /* - (id) .cxx_construct { ... return self; } */
4563 /* - (void) .cxx_construct { ... } */
4565 objc_start_method_definition
4566 (false /* is_class_method */,
4567 objc_build_method_signature (false /* is_class_method */,
4568 build_tree_list (NULL_TREE,
4569 dtor
4570 ? void_type_node
4571 : objc_object_type),
4572 get_identifier (dtor
4573 ? TAG_CXX_DESTRUCT
4574 : TAG_CXX_CONSTRUCT),
4575 make_node (TREE_LIST),
4576 false), NULL, NULL_TREE);
4577 body = begin_function_body ();
4578 compound_stmt = begin_compound_stmt (0);
4580 ivar = CLASS_IVARS (implementation_template);
4581 /* Destroy ivars in reverse order. */
4582 if (dtor)
4583 ivar = nreverse (copy_list (ivar));
4585 for (; ivar; ivar = TREE_CHAIN (ivar))
4587 if (TREE_CODE (ivar) == FIELD_DECL)
4589 tree type = TREE_TYPE (ivar);
4591 /* Call the ivar's default constructor or destructor. Do not
4592 call the destructor unless a corresponding constructor call
4593 has also been made (or is not needed). */
4594 if (MAYBE_CLASS_TYPE_P (type)
4595 && (dtor
4596 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4597 && (!TYPE_NEEDS_CONSTRUCTING (type)
4598 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4599 : (TYPE_NEEDS_CONSTRUCTING (type)
4600 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4601 finish_expr_stmt
4602 (build_special_member_call
4603 (build_ivar_reference (DECL_NAME (ivar)),
4604 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4605 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
4609 /* The constructor returns 'self'. */
4610 if (!dtor)
4611 finish_return_stmt (self_decl);
4613 finish_compound_stmt (compound_stmt);
4614 finish_function_body (body);
4615 fn = current_function_decl;
4616 finish_function ();
4617 objc_finish_method_definition (fn);
4620 /* The following routine will examine the current @interface for any
4621 non-POD C++ ivars requiring non-trivial construction and/or
4622 destruction, and then synthesize special '- .cxx_construct' and/or
4623 '- .cxx_destruct' methods which will run the appropriate
4624 construction or destruction code. Note that ivars inherited from
4625 super-classes are _not_ considered. */
4626 static void
4627 objc_generate_cxx_cdtors (void)
4629 bool need_ctor = false, need_dtor = false;
4630 tree ivar;
4632 /* Error case, due to possibly an extra @end. */
4633 if (!objc_implementation_context)
4634 return;
4636 /* We do not want to do this for categories, since they do not have
4637 their own ivars. */
4639 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4640 return;
4642 /* First, determine if we even need a constructor and/or destructor. */
4644 for (ivar = CLASS_IVARS (implementation_template); ivar;
4645 ivar = TREE_CHAIN (ivar))
4647 if (TREE_CODE (ivar) == FIELD_DECL)
4649 tree type = TREE_TYPE (ivar);
4651 if (MAYBE_CLASS_TYPE_P (type))
4653 if (TYPE_NEEDS_CONSTRUCTING (type)
4654 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4655 /* NB: If a default constructor is not available, we will not
4656 be able to initialize this ivar; the add_instance_variable()
4657 routine will already have warned about this. */
4658 need_ctor = true;
4660 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4661 && (!TYPE_NEEDS_CONSTRUCTING (type)
4662 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4663 /* NB: If a default constructor is not available, we will not
4664 call the destructor either, for symmetry. */
4665 need_dtor = true;
4670 /* Generate '- .cxx_construct' if needed. */
4672 if (need_ctor)
4673 objc_generate_cxx_ctor_or_dtor (false);
4675 /* Generate '- .cxx_destruct' if needed. */
4677 if (need_dtor)
4678 objc_generate_cxx_ctor_or_dtor (true);
4680 /* The 'imp_list' variable points at an imp_entry record for the current
4681 @implementation. Record the existence of '- .cxx_construct' and/or
4682 '- .cxx_destruct' methods therein; it will be included in the
4683 metadata for the class if the runtime needs it. */
4684 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
4686 #endif
4688 static void
4689 error_with_ivar (const char *message, tree decl)
4691 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4692 message, identifier_to_locale (gen_declaration (decl)));
4696 static void
4697 check_ivars (tree inter, tree imp)
4699 tree intdecls = CLASS_RAW_IVARS (inter);
4700 tree impdecls = CLASS_RAW_IVARS (imp);
4702 while (1)
4704 tree t1, t2;
4706 #ifdef OBJCPLUS
4707 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4708 intdecls = TREE_CHAIN (intdecls);
4709 #endif
4710 if (intdecls == 0 && impdecls == 0)
4711 break;
4712 if (intdecls == 0 || impdecls == 0)
4714 error ("inconsistent instance variable specification");
4715 break;
4718 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
4720 if (!comptypes (t1, t2)
4721 #ifdef OBJCPLUS
4722 || !tree_int_cst_equal (DECL_BIT_FIELD_REPRESENTATIVE (intdecls),
4723 DECL_BIT_FIELD_REPRESENTATIVE (impdecls))
4724 #else
4725 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4726 DECL_INITIAL (impdecls))
4727 #endif
4730 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4732 error_with_ivar ("conflicting instance variable type",
4733 impdecls);
4734 error_with_ivar ("previous declaration of",
4735 intdecls);
4737 else /* both the type and the name don't match */
4739 error ("inconsistent instance variable specification");
4740 break;
4744 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4746 error_with_ivar ("conflicting instance variable name",
4747 impdecls);
4748 error_with_ivar ("previous declaration of",
4749 intdecls);
4752 intdecls = DECL_CHAIN (intdecls);
4753 impdecls = DECL_CHAIN (impdecls);
4758 static void
4759 mark_referenced_methods (void)
4761 struct imp_entry *impent;
4762 tree chain;
4764 for (impent = imp_list; impent; impent = impent->next)
4766 chain = CLASS_CLS_METHODS (impent->imp_context);
4767 while (chain)
4769 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4770 chain = DECL_CHAIN (chain);
4773 chain = CLASS_NST_METHODS (impent->imp_context);
4774 while (chain)
4776 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4777 chain = DECL_CHAIN (chain);
4782 /* If type is empty or only type qualifiers are present, add default
4783 type of id (otherwise grokdeclarator will default to int). */
4784 static inline tree
4785 adjust_type_for_id_default (tree type)
4787 if (!type)
4788 type = make_node (TREE_LIST);
4790 if (!TREE_VALUE (type))
4791 TREE_VALUE (type) = objc_object_type;
4792 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4793 && TYPED_OBJECT (TREE_VALUE (type)))
4794 error ("cannot use an object as parameter to a method");
4796 return type;
4799 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4800 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4801 OBJC_METHOD_PARM_DECL ?)
4803 A KEYWORD_DECL is a tree representing the declaration of a
4804 parameter of an Objective-C method. It is produced when parsing a
4805 fragment of Objective-C method declaration of the form
4807 keyworddecl:
4808 selector ':' '(' typename ')' identifier
4810 For example, take the Objective-C method
4812 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4814 the two fragments "pathForResource:(NSString *)resource" and
4815 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4816 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4817 "pathForResource"), the 'arg_type' (eg, tree representing a
4818 NSString *), the 'arg_name' (eg identifier for "resource") and
4819 potentially some attributes (for example, a tree representing
4820 __attribute__ ((unused)) if such an attribute was attached to a
4821 certain parameter). You can access this information using the
4822 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4823 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4825 'key_name' is an identifier node (and is optional as you can omit
4826 it in Objective-C methods).
4827 'arg_type' is a tree list (and is optional too if no parameter type
4828 was specified).
4829 'arg_name' is an identifier node and is required.
4830 'attributes' is an optional tree containing parameter attributes. */
4831 tree
4832 objc_build_keyword_decl (tree key_name, tree arg_type,
4833 tree arg_name, tree attributes)
4835 tree keyword_decl;
4837 if (flag_objc1_only && attributes)
4838 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4840 /* If no type is specified, default to "id". */
4841 arg_type = adjust_type_for_id_default (arg_type);
4843 keyword_decl = make_node (KEYWORD_DECL);
4845 TREE_TYPE (keyword_decl) = arg_type;
4846 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4847 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4848 DECL_ATTRIBUTES (keyword_decl) = attributes;
4850 return keyword_decl;
4853 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4854 static tree
4855 build_keyword_selector (tree selector)
4857 int len = 0;
4858 tree key_chain, key_name;
4859 char *buf;
4861 /* Scan the selector to see how much space we'll need. */
4862 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4864 switch (TREE_CODE (selector))
4866 case KEYWORD_DECL:
4867 key_name = KEYWORD_KEY_NAME (key_chain);
4868 break;
4869 case TREE_LIST:
4870 key_name = TREE_PURPOSE (key_chain);
4871 break;
4872 default:
4873 gcc_unreachable ();
4876 if (key_name)
4877 len += IDENTIFIER_LENGTH (key_name) + 1;
4878 else
4879 /* Just a ':' arg. */
4880 len++;
4883 buf = (char *) alloca (len + 1);
4884 /* Start the buffer out as an empty string. */
4885 buf[0] = '\0';
4887 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4889 switch (TREE_CODE (selector))
4891 case KEYWORD_DECL:
4892 key_name = KEYWORD_KEY_NAME (key_chain);
4893 break;
4894 case TREE_LIST:
4895 key_name = TREE_PURPOSE (key_chain);
4896 /* The keyword decl chain will later be used as a function
4897 argument chain. Unhook the selector itself so as to not
4898 confuse other parts of the compiler. */
4899 TREE_PURPOSE (key_chain) = NULL_TREE;
4900 break;
4901 default:
4902 gcc_unreachable ();
4905 if (key_name)
4906 strcat (buf, IDENTIFIER_POINTER (key_name));
4907 strcat (buf, ":");
4910 return get_identifier_with_length (buf, len);
4913 /* Used for declarations and definitions. */
4915 static tree
4916 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4917 tree add_args, bool ellipsis)
4919 tree method_decl;
4921 /* If no type is specified, default to "id". */
4922 ret_type = adjust_type_for_id_default (ret_type);
4924 /* Note how a method_decl has a TREE_TYPE which is not the function
4925 type of the function implementing the method, but only the return
4926 type of the method. We may want to change this, and store the
4927 entire function type in there (eg, it may be used to simplify
4928 dealing with attributes below). */
4929 method_decl = make_node (code);
4930 TREE_TYPE (method_decl) = ret_type;
4932 /* If we have a keyword selector, create an identifier_node that
4933 represents the full selector name (`:' included)... */
4934 if (TREE_CODE (selector) == KEYWORD_DECL)
4936 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4937 METHOD_SEL_ARGS (method_decl) = selector;
4938 METHOD_ADD_ARGS (method_decl) = add_args;
4939 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4941 else
4943 METHOD_SEL_NAME (method_decl) = selector;
4944 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4945 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4948 return method_decl;
4951 /* This routine processes objective-c method attributes. */
4953 static void
4954 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4956 /* TODO: Replace the hackery below. An idea would be to store the
4957 full function type in the method declaration (for example in
4958 TREE_TYPE) and then expose ObjC method declarations to c-family
4959 and they could deal with them by simply treating them as
4960 functions. */
4962 /* Because of the dangers in the hackery below, we filter out any
4963 attribute that we do not know about. For the ones we know about,
4964 we know that they work with the hackery. For the other ones,
4965 there is no guarantee, so we have to filter them out. */
4966 tree filtered_attributes = NULL_TREE;
4968 if (attributes)
4970 tree attribute;
4971 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4973 tree name = TREE_PURPOSE (attribute);
4975 if (is_attribute_p ("deprecated", name)
4976 || is_attribute_p ("sentinel", name)
4977 || is_attribute_p ("noreturn", name))
4979 /* An attribute that we support; add it to the filtered
4980 attributes. */
4981 filtered_attributes = chainon (filtered_attributes,
4982 copy_node (attribute));
4984 else if (is_attribute_p ("format", name))
4986 /* "format" is special because before adding it to the
4987 filtered attributes we need to adjust the specified
4988 format by adding the hidden function parameters for
4989 an Objective-C method (self, _cmd). */
4990 tree new_attribute = copy_node (attribute);
4992 /* Check the arguments specified with the attribute, and
4993 modify them adding 2 for the two hidden arguments.
4994 Note how this differs from C++; according to the
4995 specs, C++ does not do it so you have to add the +1
4996 yourself. For Objective-C, instead, the compiler
4997 adds the +2 for you. */
4999 /* The attribute arguments have not been checked yet, so
5000 we need to be careful as they could be missing or
5001 invalid. If anything looks wrong, we skip the
5002 process and the compiler will complain about it later
5003 when it validates the attribute. */
5004 /* Check that we have at least three arguments. */
5005 if (TREE_VALUE (new_attribute)
5006 && TREE_CHAIN (TREE_VALUE (new_attribute))
5007 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
5009 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
5010 tree third_argument = TREE_CHAIN (second_argument);
5011 tree number;
5013 /* This is the second argument, the "string-index",
5014 which specifies the index of the format string
5015 argument. Add 2. */
5016 number = TREE_VALUE (second_argument);
5017 if (number
5018 && TREE_CODE (number) == INTEGER_CST
5019 && wi::to_wide (number) != 0)
5020 TREE_VALUE (second_argument)
5021 = wide_int_to_tree (TREE_TYPE (number),
5022 wi::to_wide (number) + 2);
5024 /* This is the third argument, the "first-to-check",
5025 which specifies the index of the first argument to
5026 check. This could be 0, meaning it is not available,
5027 in which case we don't need to add 2. Add 2 if not
5028 0. */
5029 number = TREE_VALUE (third_argument);
5030 if (number
5031 && TREE_CODE (number) == INTEGER_CST
5032 && wi::to_wide (number) != 0)
5033 TREE_VALUE (third_argument)
5034 = wide_int_to_tree (TREE_TYPE (number),
5035 wi::to_wide (number) + 2);
5037 filtered_attributes = chainon (filtered_attributes,
5038 new_attribute);
5040 else if (is_attribute_p ("nonnull", name))
5042 /* We need to fixup all the argument indexes by adding 2
5043 for the two hidden arguments of an Objective-C method
5044 invocation, similat to what we do above for the
5045 "format" attribute. */
5046 /* FIXME: This works great in terms of implementing the
5047 functionality, but the warnings that are produced by
5048 nonnull do mention the argument index (while the
5049 format ones don't). For example, you could get
5050 "warning: null argument where non-null required
5051 (argument 3)". Now in that message, "argument 3"
5052 includes the 2 hidden arguments; it would be much
5053 more friendly to call it "argument 1", as that would
5054 be consistent with __attribute__ ((nonnnull (1))).
5055 To do this, we'd need to have the C family code that
5056 checks the arguments know about adding/removing 2 to
5057 the argument index ... or alternatively we could
5058 maybe store the "printable" argument index in
5059 addition to the actual argument index ? Some
5060 refactoring is needed to do this elegantly. */
5061 tree new_attribute = copy_node (attribute);
5062 tree argument = TREE_VALUE (attribute);
5063 while (argument != NULL_TREE)
5065 /* Get the value of the argument and add 2. */
5066 tree number = TREE_VALUE (argument);
5067 if (number && TREE_CODE (number) == INTEGER_CST
5068 && wi::to_wide (number) != 0)
5069 TREE_VALUE (argument)
5070 = wide_int_to_tree (TREE_TYPE (number),
5071 wi::to_wide (number) + 2);
5072 argument = TREE_CHAIN (argument);
5075 filtered_attributes = chainon (filtered_attributes,
5076 new_attribute);
5078 else
5079 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
5083 if (filtered_attributes)
5085 /* This hackery changes the TREE_TYPE of the ObjC method
5086 declaration to be a function type, so that decl_attributes
5087 will treat the ObjC method as if it was a function. Some
5088 attributes (sentinel, format) will be applied to the function
5089 type, changing it in place; so after calling decl_attributes,
5090 we extract the function type attributes and store them in
5091 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
5092 deprecated) are applied directly to the method declaration
5093 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
5094 is nothing to do. */
5095 tree saved_type = TREE_TYPE (*node);
5096 TREE_TYPE (*node)
5097 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
5098 METHOD_REF, 0);
5099 decl_attributes (node, filtered_attributes, flags);
5100 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
5101 TREE_TYPE (*node) = saved_type;
5105 bool
5106 objc_method_decl (enum tree_code opcode)
5108 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
5111 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
5112 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
5113 method or calling one. SUPER_FLAG indicates whether this is a send
5114 to super; this makes a difference for the NeXT calling sequence in
5115 which the lookup and the method call are done together. If METHOD is
5116 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5117 represented as varargs. */
5119 tree
5120 build_function_type_for_method (tree return_type, tree method,
5121 int context, bool super_flag)
5123 vec<tree, va_gc> *argtypes = make_tree_vector ();
5124 tree t, ftype;
5125 bool is_varargs = false;
5127 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5129 /* No actual method prototype given; remaining args passed as varargs. */
5130 if (method == NULL_TREE)
5132 is_varargs = true;
5133 goto build_ftype;
5136 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5138 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5140 /* Decay argument types for the underlying C function as
5141 appropriate. */
5142 arg_type = objc_decay_parm_type (arg_type);
5144 vec_safe_push (argtypes, arg_type);
5147 if (METHOD_ADD_ARGS (method))
5149 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5150 t; t = TREE_CHAIN (t))
5152 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5154 arg_type = objc_decay_parm_type (arg_type);
5156 vec_safe_push (argtypes, arg_type);
5159 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5160 is_varargs = true;
5163 build_ftype:
5164 if (is_varargs)
5165 ftype = build_varargs_function_type_vec (return_type, argtypes);
5166 else
5167 ftype = build_function_type_vec (return_type, argtypes);
5169 release_tree_vector (argtypes);
5170 return ftype;
5173 /* The 'method' argument is a tree; this tree could either be a single
5174 method, which is returned, or could be a TREE_VEC containing a list
5175 of methods. In that case, the first one is returned, and warnings
5176 are issued as appropriate. */
5177 static tree
5178 check_duplicates (tree method, int methods, int is_class)
5180 tree first_method;
5181 size_t i;
5183 if (method == NULL_TREE)
5184 return NULL_TREE;
5186 if (TREE_CODE (method) != TREE_VEC)
5187 return method;
5189 /* We have two or more methods with the same name but different
5190 types. */
5191 first_method = TREE_VEC_ELT (method, 0);
5193 /* But just how different are those types? If
5194 -Wno-strict-selector-match is specified, we shall not complain if
5195 the differences are solely among types with identical size and
5196 alignment. */
5197 if (!warn_strict_selector_match)
5199 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5200 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5201 goto issue_warning;
5203 return first_method;
5206 issue_warning:
5207 if (methods)
5209 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5211 warning_at (input_location, 0,
5212 "multiple methods named %<%c%E%> found",
5213 (is_class ? '+' : '-'),
5214 METHOD_SEL_NAME (first_method));
5215 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5216 (type ? '-' : '+'),
5217 identifier_to_locale (gen_method_decl (first_method)));
5219 else
5221 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5223 warning_at (input_location, 0,
5224 "multiple selectors named %<%c%E%> found",
5225 (is_class ? '+' : '-'),
5226 METHOD_SEL_NAME (first_method));
5227 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5228 (type ? '-' : '+'),
5229 identifier_to_locale (gen_method_decl (first_method)));
5232 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5234 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5236 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5237 (type ? '-' : '+'),
5238 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5241 return first_method;
5244 /* If RECEIVER is a class reference, return the identifier node for
5245 the referenced class. RECEIVER is created by objc_get_class_reference,
5246 so we check the exact form created depending on which runtimes are
5247 used. */
5249 static tree
5250 receiver_is_class_object (tree receiver, int self, int super)
5252 tree exp, arg;
5254 /* The receiver is 'self' or 'super' in the context of a class method. */
5255 if (objc_method_context
5256 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5257 && (self || super))
5258 return (super
5259 ? CLASS_SUPER_NAME (implementation_template)
5260 : CLASS_NAME (implementation_template));
5262 /* The runtime might encapsulate things its own way. */
5263 exp = (*runtime.receiver_is_class_object) (receiver);
5264 if (exp)
5265 return exp;
5267 /* The receiver is a function call that returns an id. Check if
5268 it is a call to objc_getClass, if so, pick up the class name.
5270 This is required by the GNU runtime, which compiles
5272 [NSObject alloc]
5274 into
5276 [objc_get_class ("NSObject") alloc];
5278 and then, to check that the receiver responds to the +alloc
5279 method, needs to be able to determine that the objc_get_class()
5280 call returns the NSObject class and not just a generic Class
5281 pointer.
5283 But, traditionally this is enabled for all runtimes, not just the
5284 GNU one, which means that the compiler is smarter than you'd
5285 expect when dealing with objc_getClass(). For example, with the
5286 Apple runtime, in the code
5288 [objc_getClass ("NSObject") alloc];
5290 the compiler will recognize the objc_getClass() call as special
5291 (due to the code below) and so will know that +alloc is called on
5292 the 'NSObject' class, and can perform the corresponding checks.
5294 Programmers can disable this behavior by casting the results of
5295 objc_getClass() to 'Class' (this may seem weird because
5296 objc_getClass() is already declared to return 'Class', but the
5297 compiler treats it as a special function). This may be useful if
5298 the class is never declared, and the compiler would complain
5299 about a missing @interface for it. Then, you can do
5301 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5303 to silence the warnings. */
5304 if (TREE_CODE (receiver) == CALL_EXPR
5305 && (exp = CALL_EXPR_FN (receiver))
5306 && TREE_CODE (exp) == ADDR_EXPR
5307 && (exp = TREE_OPERAND (exp, 0))
5308 && TREE_CODE (exp) == FUNCTION_DECL
5309 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5310 prototypes for objc_get_class(). Thankfully, they seem to share the
5311 same function type. */
5312 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5313 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5314 /* We have a call to objc_get_class/objc_getClass! */
5315 && (arg = CALL_EXPR_ARG (receiver, 0)))
5317 STRIP_NOPS (arg);
5318 if (TREE_CODE (arg) == ADDR_EXPR
5319 && (arg = TREE_OPERAND (arg, 0))
5320 && TREE_CODE (arg) == STRING_CST)
5321 /* Finally, we have the class name. */
5322 return get_identifier (TREE_STRING_POINTER (arg));
5324 return 0;
5327 /* If we are currently building a message expr, this holds
5328 the identifier of the selector of the message. This is
5329 used when printing warnings about argument mismatches. */
5331 static tree current_objc_message_selector = 0;
5333 tree
5334 objc_message_selector (void)
5336 return current_objc_message_selector;
5339 /* Construct an expression for sending a message.
5340 MESS has the object to send to in TREE_PURPOSE
5341 and the argument list (including selector) in TREE_VALUE.
5343 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5344 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5346 tree
5347 objc_build_message_expr (tree receiver, tree message_args)
5349 tree sel_name;
5350 #ifdef OBJCPLUS
5351 tree args = TREE_PURPOSE (message_args);
5352 #else
5353 tree args = message_args;
5354 #endif
5355 tree method_params = NULL_TREE;
5357 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5358 return error_mark_node;
5360 /* Obtain the full selector name. */
5361 switch (TREE_CODE (args))
5363 case IDENTIFIER_NODE:
5364 /* A unary selector. */
5365 sel_name = args;
5366 break;
5367 case TREE_LIST:
5368 sel_name = build_keyword_selector (args);
5369 break;
5370 default:
5371 gcc_unreachable ();
5374 /* Build the parameter list to give to the method. */
5375 if (TREE_CODE (args) == TREE_LIST)
5376 #ifdef OBJCPLUS
5377 method_params = chainon (args, TREE_VALUE (message_args));
5378 #else
5380 tree chain = args, prev = NULL_TREE;
5382 /* We have a keyword selector--check for comma expressions. */
5383 while (chain)
5385 tree element = TREE_VALUE (chain);
5387 /* We have a comma expression, must collapse... */
5388 if (TREE_CODE (element) == TREE_LIST)
5390 if (prev)
5391 TREE_CHAIN (prev) = element;
5392 else
5393 args = element;
5395 prev = chain;
5396 chain = TREE_CHAIN (chain);
5398 method_params = args;
5400 #endif
5402 #ifdef OBJCPLUS
5403 if (processing_template_decl)
5404 /* Must wait until template instantiation time. */
5405 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5406 sel_name, method_params);
5407 #endif
5409 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5412 /* Look up method SEL_NAME that would be suitable for receiver
5413 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5414 nonzero), and report on any duplicates. */
5416 static tree
5417 lookup_method_in_hash_lists (tree sel_name, int is_class)
5419 tree method_prototype = OBJC_MAP_NOT_FOUND;
5421 if (!is_class)
5422 method_prototype = objc_map_get (instance_method_map, sel_name);
5424 if (method_prototype == OBJC_MAP_NOT_FOUND)
5426 method_prototype = objc_map_get (class_method_map, sel_name);
5427 is_class = 1;
5429 if (method_prototype == OBJC_MAP_NOT_FOUND)
5430 return NULL_TREE;
5433 return check_duplicates (method_prototype, 1, is_class);
5436 /* The 'objc_finish_message_expr' routine is called from within
5437 'objc_build_message_expr' for non-template functions. In the case of
5438 C++ template functions, it is called from 'build_expr_from_tree'
5439 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5441 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5442 if the method being used is deprecated. If it is not NULL, instead
5443 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5444 prototype that was used and is deprecated. This is useful for
5445 getter calls that are always generated when compiling dot-syntax
5446 expressions, even if they may not be used. In that case, we don't
5447 want the warning immediately; we produce it (if needed) at gimplify
5448 stage when we are sure that the deprecated getter is being
5449 used. */
5450 tree
5451 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5452 tree *deprecated_method_prototype)
5454 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5455 tree retval, class_tree;
5456 int self, super, have_cast;
5458 STRIP_ANY_LOCATION_WRAPPER (receiver);
5460 /* We have used the receiver, so mark it as read. */
5461 mark_exp_read (receiver);
5463 /* Extract the receiver of the message, as well as its type
5464 (where the latter may take the form of a cast or be inferred
5465 from the implementation context). */
5466 rtype = receiver;
5467 while (TREE_CODE (rtype) == COMPOUND_EXPR
5468 || TREE_CODE (rtype) == MODIFY_EXPR
5469 || CONVERT_EXPR_P (rtype)
5470 || TREE_CODE (rtype) == COMPONENT_REF)
5471 rtype = TREE_OPERAND (rtype, 0);
5473 /* self is 1 if this is a message to self, 0 otherwise */
5474 self = (rtype == self_decl);
5476 /* super is 1 if this is a message to super, 0 otherwise. */
5477 super = (rtype == UOBJC_SUPER_decl);
5479 /* rtype is the type of the receiver. */
5480 rtype = TREE_TYPE (receiver);
5482 /* have_cast is 1 if the receiver is casted. */
5483 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5484 || (TREE_CODE (receiver) == COMPOUND_EXPR
5485 && !IS_SUPER (rtype)));
5487 /* If we are calling [super dealloc], reset our warning flag. */
5488 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5489 should_call_super_dealloc = 0;
5491 /* If the receiver is a class object, retrieve the corresponding
5492 @interface, if one exists. class_tree is the class name
5493 identifier, or NULL_TREE if this is not a class method or the
5494 class name could not be determined (as in the case "Class c; [c
5495 method];"). */
5496 class_tree = receiver_is_class_object (receiver, self, super);
5498 /* Now determine the receiver type (if an explicit cast has not been
5499 provided). */
5500 if (!have_cast)
5502 if (class_tree)
5504 /* We are here when we have no cast, and we have a class
5505 name. So, this is a plain method to a class object, as
5506 in [NSObject alloc]. Find the interface corresponding to
5507 the class name. */
5508 rtype = lookup_interface (class_tree);
5510 if (rtype == NULL_TREE)
5512 /* If 'rtype' is NULL_TREE at this point it means that
5513 we have seen no @interface corresponding to that
5514 class name, only a @class declaration (alternatively,
5515 this was a call such as [objc_getClass("SomeClass")
5516 alloc], where we've never seen the @interface of
5517 SomeClass). So, we have a class name (class_tree)
5518 but no actual details of the class methods. We won't
5519 be able to check that the class responds to the
5520 method, and we will have to guess the method
5521 prototype. Emit a warning, then keep going (this
5522 will use any method with a matching name, as if the
5523 receiver was of type 'Class'). */
5524 warning (0, "%<@interface%> of class %qE not found",
5525 class_tree);
5528 /* Handle `self' and `super'. */
5529 else if (super)
5531 if (!CLASS_SUPER_NAME (implementation_template))
5533 error ("no super class declared in @interface for %qE",
5534 CLASS_NAME (implementation_template));
5535 return error_mark_node;
5537 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5539 else if (self)
5540 rtype = lookup_interface (CLASS_NAME (implementation_template));
5543 if (objc_is_id (rtype))
5545 /* The receiver is of type 'id' or 'Class' (with or without some
5546 protocols attached to it). */
5548 /* We set class_tree to the identifier for 'Class' if this is a
5549 class method, and to NULL_TREE if not. */
5550 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5552 /* 'rprotos' is the list of protocols that the receiver
5553 supports. */
5554 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5555 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5556 : NULL_TREE);
5558 /* We have no information on the type, and we set it to
5559 NULL_TREE. */
5560 rtype = NULL_TREE;
5562 /* If there are any protocols, check that the method we are
5563 calling appears in the protocol list. If there are no
5564 protocols, this is a message to 'id' or 'Class' and we accept
5565 any method that exists. */
5566 if (rprotos)
5568 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5569 search in protocols themselves for the method
5570 prototype. */
5571 method_prototype
5572 = lookup_method_in_protocol_list (rprotos, sel_name,
5573 class_tree != NULL_TREE);
5575 /* If messaging 'Class <Proto>' but did not find a class
5576 method prototype, search for an instance method instead,
5577 and warn about having done so. */
5578 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5580 method_prototype
5581 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5583 if (method_prototype)
5584 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5585 sel_name, sel_name);
5589 else if (rtype)
5591 /* We have a receiver type which is more specific than 'id' or
5592 'Class'. */
5593 tree orig_rtype = rtype;
5595 if (TREE_CODE (rtype) == POINTER_TYPE)
5596 rtype = TREE_TYPE (rtype);
5597 /* Traverse typedef aliases */
5598 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5599 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5600 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5601 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5602 if (TYPED_OBJECT (rtype))
5604 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5605 rtype = TYPE_OBJC_INTERFACE (rtype);
5607 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5609 /* If we could not find an @interface declaration, we must
5610 have only seen a @class declaration; so, we cannot say
5611 anything more intelligent about which methods the
5612 receiver will understand. Note that this only happens
5613 for instance methods; for class methods to a class where
5614 we have only seen a @class declaration,
5615 lookup_interface() above would have set rtype to
5616 NULL_TREE. */
5617 if (rprotos)
5619 /* We could not find an @interface declaration, yet, if
5620 there are protocols attached to the type, we can
5621 still look up the method in the protocols. Ie, we
5622 are in the following case:
5624 @class MyClass;
5625 MyClass<MyProtocol> *x;
5626 [x method];
5628 If 'MyProtocol' has the method 'method', we can check
5629 and retrieve the method prototype. */
5630 method_prototype
5631 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5633 /* At this point, if we have found the method_prototype,
5634 we are quite happy. The details of the class are
5635 irrelevant. If we haven't found it, a warning will
5636 have been produced that the method could not be found
5637 in the protocol, and we won't produce further
5638 warnings (please note that this means that "@class
5639 MyClass; MyClass <MyProtocol> *x;" is exactly
5640 equivalent to "id <MyProtocol> x", which isn't too
5641 satisfactory but it's not easy to see how to do
5642 better). */
5644 else
5646 if (rtype)
5648 /* We could not find an @interface declaration, and
5649 there are no protocols attached to the receiver,
5650 so we can't complete the check that the receiver
5651 responds to the method, and we can't retrieve the
5652 method prototype. But, because the receiver has
5653 a well-specified class, the programmer did want
5654 this check to be performed. Emit a warning, then
5655 keep going as if it was an 'id'. To remove the
5656 warning, either include an @interface for the
5657 class, or cast the receiver to 'id'. Note that
5658 rtype is an IDENTIFIER_NODE at this point. */
5659 warning (0, "%<@interface%> of class %qE not found", rtype);
5663 rtype = NULL_TREE;
5665 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5666 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5668 /* We have a valid ObjC class name with an associated
5669 @interface. Look up the method name in the published
5670 @interface for the class (and its superclasses). */
5671 method_prototype
5672 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5674 /* If the method was not found in the @interface, it may still
5675 exist locally as part of the @implementation. */
5676 if (!method_prototype && objc_implementation_context
5677 && CLASS_NAME (objc_implementation_context)
5678 == OBJC_TYPE_NAME (rtype))
5679 method_prototype
5680 = lookup_method
5681 ((class_tree
5682 ? CLASS_CLS_METHODS (objc_implementation_context)
5683 : CLASS_NST_METHODS (objc_implementation_context)),
5684 sel_name);
5686 /* If we haven't found a candidate method by now, try looking for
5687 it in the protocol list. */
5688 if (!method_prototype && rprotos)
5689 method_prototype
5690 = lookup_method_in_protocol_list (rprotos, sel_name,
5691 class_tree != NULL_TREE);
5693 else
5695 /* We have a type, but it's not an Objective-C type (!). */
5696 warning (0, "invalid receiver type %qs",
5697 identifier_to_locale (gen_type_name (orig_rtype)));
5698 /* After issuing the "invalid receiver" warning, perform method
5699 lookup as if we were messaging 'id'. */
5700 rtype = rprotos = NULL_TREE;
5703 /* Note that rtype could also be NULL_TREE. This happens if we are
5704 messaging a class by name, but the class was only
5705 forward-declared using @class. */
5707 /* For 'id' or 'Class' receivers, search in the global hash table as
5708 a last resort. For all receivers, warn if protocol searches have
5709 failed. */
5710 if (!method_prototype)
5712 if (rprotos)
5713 warning (0, "%<%c%E%> not found in protocol(s)",
5714 (class_tree ? '+' : '-'),
5715 sel_name);
5717 if (!rtype)
5718 method_prototype
5719 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5722 if (!method_prototype)
5724 static bool warn_missing_methods = false;
5726 if (rtype)
5727 warning (0, "%qE may not respond to %<%c%E%>",
5728 OBJC_TYPE_NAME (rtype),
5729 (class_tree ? '+' : '-'),
5730 sel_name);
5731 /* If we are messaging an 'id' or 'Class' object and made it here,
5732 then we have failed to find _any_ instance or class method,
5733 respectively. */
5734 else
5735 warning (0, "no %<%c%E%> method found",
5736 (class_tree ? '+' : '-'),
5737 sel_name);
5739 if (!warn_missing_methods)
5741 warning_at (input_location,
5742 0, "(messages without a matching method signature "
5743 "will be assumed to return %<id%> and accept "
5744 "%<...%> as arguments)");
5745 warn_missing_methods = true;
5748 else
5750 /* Warn if the method is deprecated, but not if the receiver is
5751 a generic 'id'. 'id' is used to cast an object to a generic
5752 object of an unspecified class; in that case, we'll use
5753 whatever method prototype we can find to get the method
5754 argument and return types, but it is not appropriate to
5755 produce deprecation warnings since we don't know the class
5756 that the object will be of at runtime. The @interface(s) for
5757 that class may not even be available to the compiler right
5758 now, and it is perfectly possible that the method is marked
5759 as non-deprecated in such @interface(s).
5761 In practice this makes sense since casting an object to 'id'
5762 is often used precisely to turn off warnings associated with
5763 the object being of a particular class. */
5764 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5766 if (deprecated_method_prototype)
5767 *deprecated_method_prototype = method_prototype;
5768 else
5769 warn_deprecated_use (method_prototype, NULL_TREE);
5773 /* Save the selector name for printing error messages. */
5774 current_objc_message_selector = sel_name;
5776 /* Build the method call.
5777 TODO: Get the location from somewhere that will work for delayed
5778 expansion. */
5780 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5781 receiver, rtype, sel_name,
5782 method_params, super);
5784 current_objc_message_selector = 0;
5786 return retval;
5790 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5791 expression. This variable will be initialized to global protocol_t meta-data
5792 pointer. */
5794 /* This function is called by the parser when (and only when) a
5795 @protocol() expression is found, in order to compile it. */
5796 tree
5797 objc_build_protocol_expr (tree protoname)
5799 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5800 /* definition_required */ false);
5802 if (!p)
5804 error ("cannot find protocol declaration for %qE", protoname);
5805 return error_mark_node;
5808 return (*runtime.get_protocol_reference) (input_location, p);
5811 /* This function is called by the parser when a @selector() expression
5812 is found, in order to compile it. It is only called by the parser
5813 and only to compile a @selector(). LOC is the location of the
5814 @selector. */
5815 tree
5816 objc_build_selector_expr (location_t loc, tree selnamelist)
5818 tree selname;
5820 /* Obtain the full selector name. */
5821 switch (TREE_CODE (selnamelist))
5823 case IDENTIFIER_NODE:
5824 /* A unary selector. */
5825 selname = selnamelist;
5826 break;
5827 case TREE_LIST:
5828 selname = build_keyword_selector (selnamelist);
5829 break;
5830 default:
5831 gcc_unreachable ();
5834 /* If we are required to check @selector() expressions as they
5835 are found, check that the selector has been declared. */
5836 if (warn_undeclared_selector)
5838 /* Look the selector up in the list of all known class and
5839 instance methods (up to this line) to check that the selector
5840 exists. */
5841 tree method;
5843 /* First try with instance methods. */
5844 method = objc_map_get (instance_method_map, selname);
5846 /* If not found, try with class methods. */
5847 if (method == OBJC_MAP_NOT_FOUND)
5849 method = objc_map_get (class_method_map, selname);
5851 /* If still not found, print out a warning. */
5852 if (method == OBJC_MAP_NOT_FOUND)
5853 warning (0, "undeclared selector %qE", selname);
5857 /* The runtimes do this differently, most particularly, GNU has typed
5858 selectors, whilst NeXT does not. */
5859 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5862 static tree
5863 build_ivar_reference (tree id)
5865 tree base;
5866 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5868 /* Historically, a class method that produced objects (factory
5869 method) would assign `self' to the instance that it
5870 allocated. This would effectively turn the class method into
5871 an instance method. Following this assignment, the instance
5872 variables could be accessed. That practice, while safe,
5873 violates the simple rule that a class method should not refer
5874 to an instance variable. It's better to catch the cases
5875 where this is done unknowingly than to support the above
5876 paradigm. */
5877 warning (0, "instance variable %qE accessed in class method",
5878 id);
5879 self_decl = convert (objc_instance_type, self_decl); /* cast */
5882 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5883 return (*runtime.build_ivar_reference) (input_location, base, id);
5886 static void
5887 hash_init (void)
5889 instance_method_map = objc_map_alloc_ggc (1000);
5890 class_method_map = objc_map_alloc_ggc (1000);
5892 class_name_map = objc_map_alloc_ggc (200);
5893 alias_name_map = objc_map_alloc_ggc (200);
5895 /* Initialize the hash table used to hold the constant string objects. */
5896 string_htab = hash_table<objc_string_hasher>::create_ggc (31);
5899 /* Use the following to add a method to class_method_map or
5900 instance_method_map. It will add the method, keyed by the
5901 METHOD_SEL_NAME. If the method already exists, but with one or
5902 more different prototypes, it will store a TREE_VEC in the map,
5903 with the method prototypes in the vector. */
5904 static void
5905 insert_method_into_method_map (bool class_method, tree method)
5907 tree method_name = METHOD_SEL_NAME (method);
5908 tree existing_entry;
5909 objc_map_t map;
5911 if (class_method)
5912 map = class_method_map;
5913 else
5914 map = instance_method_map;
5916 /* Check if the method already exists in the map. */
5917 existing_entry = objc_map_get (map, method_name);
5919 /* If not, we simply add it to the map. */
5920 if (existing_entry == OBJC_MAP_NOT_FOUND)
5921 objc_map_put (map, method_name, method);
5922 else
5924 tree new_entry;
5926 /* If an entry already exists, it's more complicated. We'll
5927 have to check whether the method prototype is the same or
5928 not. */
5929 if (TREE_CODE (existing_entry) != TREE_VEC)
5931 /* If the method prototypes are the same, there is nothing
5932 to do. */
5933 if (comp_proto_with_proto (method, existing_entry, 1))
5934 return;
5936 /* If not, create a vector to store both the method already
5937 in the map, and the new one that we are adding. */
5938 new_entry = make_tree_vec (2);
5940 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5941 TREE_VEC_ELT (new_entry, 1) = method;
5943 else
5945 /* An entry already exists, and it's already a vector. This
5946 means that at least 2 different method prototypes were
5947 already found, and we're considering registering yet
5948 another one. */
5949 size_t i;
5951 /* Check all the existing prototypes. If any matches the
5952 one we need to add, there is nothing to do because it's
5953 already there. */
5954 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5955 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5956 return;
5958 /* Else, create a new, bigger vector and add the new method
5959 at the end of it. This is inefficient but extremely
5960 rare; in any sane program most methods have a single
5961 prototype, and very few, if any, will have more than
5962 2! */
5963 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5965 /* Copy the methods from the existing vector. */
5966 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5967 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5969 /* Add the new method at the end. */
5970 TREE_VEC_ELT (new_entry, i) = method;
5973 /* Store the new vector in the map. */
5974 objc_map_put (map, method_name, new_entry);
5979 static tree
5980 lookup_method (tree mchain, tree method)
5982 tree key;
5984 if (TREE_CODE (method) == IDENTIFIER_NODE)
5985 key = method;
5986 else
5987 key = METHOD_SEL_NAME (method);
5989 while (mchain)
5991 if (METHOD_SEL_NAME (mchain) == key)
5992 return mchain;
5994 mchain = DECL_CHAIN (mchain);
5996 return NULL_TREE;
5999 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
6000 method in INTERFACE, along with any categories and protocols
6001 attached thereto. If method is not found, and the
6002 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
6003 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
6004 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
6005 be found in INTERFACE or any of its superclasses, look for an
6006 _instance_ method of the same name in the root class as a last
6007 resort. This behavior can be turned off by using
6008 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
6010 If a suitable method cannot be found, return NULL_TREE. */
6012 static tree
6013 lookup_method_static (tree interface, tree ident, int flags)
6015 tree meth = NULL_TREE, root_inter = NULL_TREE;
6016 tree inter = interface;
6017 int is_class = (flags & OBJC_LOOKUP_CLASS);
6018 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
6019 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
6021 while (inter)
6023 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
6024 tree category = inter;
6026 /* First, look up the method in the class itself. */
6027 if ((meth = lookup_method (chain, ident)))
6028 return meth;
6030 /* Failing that, look for the method in each category of the class. */
6031 while ((category = CLASS_CATEGORY_LIST (category)))
6033 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
6035 /* Check directly in each category. */
6036 if ((meth = lookup_method (chain, ident)))
6037 return meth;
6039 /* Failing that, check in each category's protocols. */
6040 if (CLASS_PROTOCOL_LIST (category))
6042 if ((meth = (lookup_method_in_protocol_list
6043 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
6044 return meth;
6048 /* If not found in categories, check in protocols of the main class. */
6049 if (CLASS_PROTOCOL_LIST (inter))
6051 if ((meth = (lookup_method_in_protocol_list
6052 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
6053 return meth;
6056 /* If we were instructed not to look in superclasses, don't. */
6057 if (no_superclasses)
6058 return NULL_TREE;
6060 /* Failing that, climb up the inheritance hierarchy. */
6061 root_inter = inter;
6062 inter = lookup_interface (CLASS_SUPER_NAME (inter));
6064 while (inter);
6066 if (is_class && !no_instance_methods_of_root_class)
6068 /* If no class (factory) method was found, check if an _instance_
6069 method of the same name exists in the root class. This is what
6070 the Objective-C runtime will do. */
6071 return lookup_method_static (root_inter, ident, 0);
6073 else
6075 /* If an instance method was not found, return 0. */
6076 return NULL_TREE;
6080 static tree
6081 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
6083 tree existing_method = NULL_TREE;
6085 /* The first thing we do is look up the method in the list of
6086 methods already defined in the interface (or implementation). */
6087 if (is_class)
6088 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
6089 else
6090 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
6092 /* In the case of protocols, we have a second list of methods to
6093 consider, the list of optional ones. */
6094 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6096 /* @required methods are added to the protocol's normal list.
6097 @optional methods are added to the protocol's OPTIONAL lists.
6098 Note that adding the methods to the optional lists disables
6099 checking that the methods are implemented by classes
6100 implementing the protocol, since these checks only use the
6101 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
6103 /* First of all, if the method to add is @optional, and we found
6104 it already existing as @required, emit an error. */
6105 if (is_optional && existing_method)
6107 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6108 (is_class ? '+' : '-'),
6109 METHOD_SEL_NAME (existing_method));
6110 inform (DECL_SOURCE_LOCATION (existing_method),
6111 "previous declaration of %<%c%E%> as %<@required%>",
6112 (is_class ? '+' : '-'),
6113 METHOD_SEL_NAME (existing_method));
6116 /* Now check the list of @optional methods if we didn't find the
6117 method in the @required list. */
6118 if (!existing_method)
6120 if (is_class)
6121 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6122 else
6123 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
6125 if (!is_optional && existing_method)
6127 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6128 (is_class ? '+' : '-'),
6129 METHOD_SEL_NAME (existing_method));
6130 inform (DECL_SOURCE_LOCATION (existing_method),
6131 "previous declaration of %<%c%E%> as %<@optional%>",
6132 (is_class ? '+' : '-'),
6133 METHOD_SEL_NAME (existing_method));
6138 /* If the method didn't exist already, add it. */
6139 if (!existing_method)
6141 if (is_optional)
6143 if (is_class)
6145 /* Put the method on the list in reverse order. */
6146 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6147 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6149 else
6151 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6152 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6155 else
6157 if (is_class)
6159 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6160 CLASS_CLS_METHODS (klass) = method;
6162 else
6164 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6165 CLASS_NST_METHODS (klass) = method;
6169 else
6171 /* The method was already defined. Check that the types match
6172 for an @interface for a class or category, or for a
6173 @protocol. Give hard errors on methods with identical
6174 selectors but differing argument and/or return types. We do
6175 not do this for @implementations, because C/C++ will do it
6176 for us (i.e., there will be duplicate function definition
6177 errors). */
6178 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6179 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6180 /* Starting with GCC 4.6, we emit the same error for
6181 protocols too. The situation is identical to
6182 @interfaces as there is no possible meaningful reason
6183 for defining the same method with different signatures
6184 in the very same @protocol. If that was allowed,
6185 whenever the protocol is used (both at compile and run
6186 time) there wouldn't be any meaningful way to decide
6187 which of the two method signatures should be used. */
6188 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6189 && !comp_proto_with_proto (method, existing_method, 1))
6191 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6192 (is_class ? '+' : '-'),
6193 METHOD_SEL_NAME (existing_method));
6194 inform (DECL_SOURCE_LOCATION (existing_method),
6195 "previous declaration of %<%c%E%>",
6196 (is_class ? '+' : '-'),
6197 METHOD_SEL_NAME (existing_method));
6201 if (is_class)
6202 insert_method_into_method_map (true, method);
6203 else
6205 insert_method_into_method_map (false, method);
6207 /* Instance methods in root classes (and categories thereof)
6208 may act as class methods as a last resort. We also add
6209 instance methods listed in @protocol declarations to
6210 the class hash table, on the assumption that @protocols
6211 may be adopted by root classes or categories. */
6212 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6213 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6214 klass = lookup_interface (CLASS_NAME (klass));
6216 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6217 || !CLASS_SUPER_NAME (klass))
6218 insert_method_into_method_map (true, method);
6221 return method;
6224 static void
6225 add_category (tree klass, tree category)
6227 /* Put categories on list in reverse order. */
6228 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6230 if (cat)
6232 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6233 CLASS_NAME (klass),
6234 CLASS_SUPER_NAME (category));
6236 else
6238 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6239 CLASS_CATEGORY_LIST (klass) = category;
6243 #ifndef OBJCPLUS
6244 /* A flexible array member is a C99 extension where you can use
6245 "type[]" at the end of a struct to mean a variable-length array.
6247 In Objective-C, instance variables are fundamentally members of a
6248 struct, but the struct can always be extended by subclassing; hence
6249 we need to detect and forbid all instance variables declared using
6250 flexible array members.
6252 No check for this is needed in Objective-C++, since C++ does not
6253 have flexible array members. */
6255 /* Determine whether TYPE is a structure with a flexible array member,
6256 a union containing such a structure (possibly recursively) or an
6257 array of such structures or unions. These are all invalid as
6258 instance variable. */
6259 static bool
6260 flexible_array_type_p (tree type)
6262 tree x;
6263 switch (TREE_CODE (type))
6265 case RECORD_TYPE:
6266 x = TYPE_FIELDS (type);
6267 if (x == NULL_TREE)
6268 return false;
6269 while (DECL_CHAIN (x) != NULL_TREE)
6270 x = DECL_CHAIN (x);
6271 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6272 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6273 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6274 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6275 return true;
6276 return false;
6277 case UNION_TYPE:
6278 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6280 if (flexible_array_type_p (TREE_TYPE (x)))
6281 return true;
6283 return false;
6284 /* Note that we also check for arrays of something that uses a flexible array member. */
6285 case ARRAY_TYPE:
6286 if (flexible_array_type_p (TREE_TYPE (type)))
6287 return true;
6288 return false;
6289 default:
6290 return false;
6293 #endif
6295 /* Produce a printable version of an ivar name. This is only used
6296 inside add_instance_variable. */
6297 static const char *
6298 printable_ivar_name (tree field_decl)
6300 if (DECL_NAME (field_decl))
6301 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6302 else
6303 return _("<unnamed>");
6306 /* Called after parsing each instance variable declaration. Necessary to
6307 preserve typedefs and implement public/private...
6309 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6311 static tree
6312 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6313 tree field_decl)
6315 tree field_type = TREE_TYPE (field_decl);
6317 #ifdef OBJCPLUS
6318 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6320 error ("illegal reference type specified for instance variable %qs",
6321 printable_ivar_name (field_decl));
6322 /* Return class as is without adding this ivar. */
6323 return klass;
6325 #endif
6327 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6328 || TYPE_SIZE (field_type) == error_mark_node)
6329 /* 'type[0]' is allowed, but 'type[]' is not! */
6331 error ("instance variable %qs has unknown size",
6332 printable_ivar_name (field_decl));
6333 /* Return class as is without adding this ivar. */
6334 return klass;
6337 #ifndef OBJCPLUS
6338 /* Also, in C reject a struct with a flexible array member. Ie,
6340 struct A { int x; int[] y; };
6342 @interface X
6344 struct A instance_variable;
6346 @end
6348 is not valid because if the class is subclassed, we wouldn't be able
6349 to calculate the offset of the next instance variable. */
6350 if (flexible_array_type_p (field_type))
6352 error ("instance variable %qs uses flexible array member",
6353 printable_ivar_name (field_decl));
6354 /* Return class as is without adding this ivar. */
6355 return klass;
6357 #endif
6359 #ifdef OBJCPLUS
6360 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6361 need to either (1) warn the user about it or (2) generate suitable
6362 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6363 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6364 if (MAYBE_CLASS_TYPE_P (field_type)
6365 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6366 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6367 || TYPE_POLYMORPHIC_P (field_type)))
6369 tree type_name = OBJC_TYPE_NAME (field_type);
6371 if (flag_objc_call_cxx_cdtors)
6373 /* Since the ObjC runtime will be calling the constructors and
6374 destructors for us, the only thing we can't handle is the lack
6375 of a default constructor. */
6376 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6377 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6379 warning (0, "type %qE has no default constructor to call",
6380 type_name);
6382 /* If we cannot call a constructor, we should also avoid
6383 calling the destructor, for symmetry. */
6384 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6385 warning (0, "destructor for %qE shall not be run either",
6386 type_name);
6389 else
6391 static bool warn_cxx_ivars = false;
6393 if (TYPE_POLYMORPHIC_P (field_type))
6395 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6396 initialize them. */
6397 error ("type %qE has virtual member functions", type_name);
6398 error ("illegal aggregate type %qE specified "
6399 "for instance variable %qs",
6400 type_name, printable_ivar_name (field_decl));
6401 /* Return class as is without adding this ivar. */
6402 return klass;
6405 /* User-defined constructors and destructors are not known to Obj-C
6406 and hence will not be called. This may or may not be a problem. */
6407 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6408 warning (0, "type %qE has a user-defined constructor", type_name);
6409 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6410 warning (0, "type %qE has a user-defined destructor", type_name);
6412 if (!warn_cxx_ivars)
6414 warning (0, "C++ constructors and destructors will not "
6415 "be invoked for Objective-C fields");
6416 warn_cxx_ivars = true;
6420 #endif
6422 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6423 switch (visibility)
6425 case OBJC_IVAR_VIS_PROTECTED:
6426 TREE_PUBLIC (field_decl) = 0;
6427 TREE_PRIVATE (field_decl) = 0;
6428 TREE_PROTECTED (field_decl) = 1;
6429 break;
6431 case OBJC_IVAR_VIS_PACKAGE:
6432 /* TODO: Implement the package variant. */
6433 case OBJC_IVAR_VIS_PUBLIC:
6434 TREE_PUBLIC (field_decl) = 1;
6435 TREE_PRIVATE (field_decl) = 0;
6436 TREE_PROTECTED (field_decl) = 0;
6437 break;
6439 case OBJC_IVAR_VIS_PRIVATE:
6440 TREE_PUBLIC (field_decl) = 0;
6441 TREE_PRIVATE (field_decl) = 1;
6442 TREE_PROTECTED (field_decl) = 0;
6443 break;
6447 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6449 return klass;
6452 /* True if the ivar is private and we are not in its implementation. */
6454 static int
6455 is_private (tree decl)
6457 return (TREE_PRIVATE (decl)
6458 && ! is_ivar (CLASS_IVARS (implementation_template),
6459 DECL_NAME (decl)));
6462 /* Searches all the instance variables of 'klass' and of its
6463 superclasses for an instance variable whose name (identifier) is
6464 'ivar_name_ident'. Return the declaration (DECL) of the instance
6465 variable, if found, or NULL_TREE, if not found. */
6466 static inline tree
6467 ivar_of_class (tree klass, tree ivar_name_ident)
6469 /* First, look up the ivar in CLASS_RAW_IVARS. */
6470 tree decl_chain = CLASS_RAW_IVARS (klass);
6472 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6473 if (DECL_NAME (decl_chain) == ivar_name_ident)
6474 return decl_chain;
6476 /* If not found, search up the class hierarchy. */
6477 while (CLASS_SUPER_NAME (klass))
6479 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6481 decl_chain = CLASS_RAW_IVARS (klass);
6483 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6484 if (DECL_NAME (decl_chain) == ivar_name_ident)
6485 return decl_chain;
6488 return NULL_TREE;
6491 /* We have an instance variable reference;, check to see if it is public. */
6494 objc_is_public (tree expr, tree identifier)
6496 tree basetype, decl;
6498 #ifdef OBJCPLUS
6499 if (processing_template_decl)
6500 return 1;
6501 #endif
6503 if (TREE_TYPE (expr) == error_mark_node)
6504 return 1;
6506 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6508 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6510 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6512 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6514 if (!klass)
6516 error ("cannot find interface declaration for %qE",
6517 OBJC_TYPE_NAME (basetype));
6518 return 0;
6521 if ((decl = ivar_of_class (klass, identifier)))
6523 if (TREE_PUBLIC (decl))
6524 return 1;
6526 /* Important difference between the Stepstone translator:
6527 all instance variables should be public within the context
6528 of the implementation. */
6529 if (objc_implementation_context
6530 && ((TREE_CODE (objc_implementation_context)
6531 == CLASS_IMPLEMENTATION_TYPE)
6532 || (TREE_CODE (objc_implementation_context)
6533 == CATEGORY_IMPLEMENTATION_TYPE)))
6535 tree curtype = TYPE_MAIN_VARIANT
6536 (CLASS_STATIC_TEMPLATE
6537 (implementation_template));
6539 if (basetype == curtype
6540 || DERIVED_FROM_P (basetype, curtype))
6542 int priv = is_private (decl);
6544 if (priv)
6545 error ("instance variable %qE is declared private",
6546 DECL_NAME (decl));
6548 return !priv;
6552 /* The 2.95.2 compiler sometimes allowed C functions to access
6553 non-@public ivars. We will let this slide for now... */
6554 if (!objc_method_context)
6556 warning (0, "instance variable %qE is %s; "
6557 "this will be a hard error in the future",
6558 identifier,
6559 TREE_PRIVATE (decl) ? "@private" : "@protected");
6560 return 1;
6563 error ("instance variable %qE is declared %s",
6564 identifier,
6565 TREE_PRIVATE (decl) ? "private" : "protected");
6566 return 0;
6571 return 1;
6574 /* Make sure all methods in CHAIN (a list of method declarations from
6575 an @interface or a @protocol) are in IMPLEMENTATION (the
6576 implementation context). This is used to check for example that
6577 all methods declared in an @interface were implemented in an
6578 @implementation.
6580 Some special methods (property setters/getters) are special and if
6581 they are not found in IMPLEMENTATION, we look them up in its
6582 superclasses. */
6584 static int
6585 check_methods (tree chain, tree implementation, int mtype)
6587 int first = 1;
6588 tree list;
6590 if (mtype == (int)'+')
6591 list = CLASS_CLS_METHODS (implementation);
6592 else
6593 list = CLASS_NST_METHODS (implementation);
6595 while (chain)
6597 /* If the method is associated with a dynamic property, then it
6598 is Ok not to have the method implementation, as it will be
6599 generated dynamically at runtime. To decide if the method is
6600 associated with a @dynamic property, we search the list of
6601 @synthesize and @dynamic for this implementation, and look
6602 for any @dynamic property with the same setter or getter name
6603 as this method. */
6604 tree x;
6605 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6606 if (PROPERTY_DYNAMIC (x)
6607 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6608 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6609 break;
6611 if (x != NULL_TREE)
6613 chain = TREE_CHAIN (chain); /* next method... */
6614 continue;
6617 if (!lookup_method (list, chain))
6619 /* If the method is a property setter/getter, we'll still
6620 allow it to be missing if it is implemented by
6621 'interface' or any of its superclasses. */
6622 tree property = METHOD_PROPERTY_CONTEXT (chain);
6623 if (property)
6625 /* Note that since this is a property getter/setter, it
6626 is obviously an instance method. */
6627 tree interface = NULL_TREE;
6629 /* For a category, first check the main class
6630 @interface. */
6631 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6633 interface = lookup_interface (CLASS_NAME (implementation));
6635 /* If the method is found in the main class, it's Ok. */
6636 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6638 chain = DECL_CHAIN (chain);
6639 continue;
6642 /* Else, get the superclass. */
6643 if (CLASS_SUPER_NAME (interface))
6644 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6645 else
6646 interface = NULL_TREE;
6649 /* Get the superclass for classes. */
6650 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6652 if (CLASS_SUPER_NAME (implementation))
6653 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6654 else
6655 interface = NULL_TREE;
6658 /* Now, interface is the superclass, if any; go check it. */
6659 if (interface)
6661 if (lookup_method_static (interface, chain, 0))
6663 chain = DECL_CHAIN (chain);
6664 continue;
6667 /* Else, fall through - warn. */
6669 if (first)
6671 switch (TREE_CODE (implementation))
6673 case CLASS_IMPLEMENTATION_TYPE:
6674 warning (0, "incomplete implementation of class %qE",
6675 CLASS_NAME (implementation));
6676 break;
6677 case CATEGORY_IMPLEMENTATION_TYPE:
6678 warning (0, "incomplete implementation of category %qE",
6679 CLASS_SUPER_NAME (implementation));
6680 break;
6681 default:
6682 gcc_unreachable ();
6684 first = 0;
6687 warning (0, "method definition for %<%c%E%> not found",
6688 mtype, METHOD_SEL_NAME (chain));
6691 chain = DECL_CHAIN (chain);
6694 return first;
6697 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6699 static int
6700 conforms_to_protocol (tree klass, tree protocol)
6702 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6704 tree p = CLASS_PROTOCOL_LIST (klass);
6705 while (p && TREE_VALUE (p) != protocol)
6706 p = TREE_CHAIN (p);
6708 if (!p)
6710 tree super = (CLASS_SUPER_NAME (klass)
6711 ? lookup_interface (CLASS_SUPER_NAME (klass))
6712 : NULL_TREE);
6713 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6714 if (!tmp)
6715 return 0;
6719 return 1;
6722 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6723 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6725 static int
6726 check_methods_accessible (tree chain, tree context, int mtype)
6728 int first = 1;
6729 tree list;
6730 tree base_context = context;
6732 while (chain)
6734 /* If the method is associated with a dynamic property, then it
6735 is Ok not to have the method implementation, as it will be
6736 generated dynamically at runtime. Search for any @dynamic
6737 property with the same setter or getter name as this
6738 method. TODO: Use a hashtable lookup. */
6739 tree x;
6740 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6741 if (PROPERTY_DYNAMIC (x)
6742 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6743 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6744 break;
6746 if (x != NULL_TREE)
6748 chain = TREE_CHAIN (chain); /* next method... */
6749 continue;
6752 context = base_context;
6753 while (context)
6755 if (mtype == '+')
6756 list = CLASS_CLS_METHODS (context);
6757 else
6758 list = CLASS_NST_METHODS (context);
6760 if (lookup_method (list, chain))
6761 break;
6763 switch (TREE_CODE (context))
6765 case CLASS_IMPLEMENTATION_TYPE:
6766 case CLASS_INTERFACE_TYPE:
6767 context = (CLASS_SUPER_NAME (context)
6768 ? lookup_interface (CLASS_SUPER_NAME (context))
6769 : NULL_TREE);
6770 break;
6771 case CATEGORY_IMPLEMENTATION_TYPE:
6772 case CATEGORY_INTERFACE_TYPE:
6773 context = (CLASS_NAME (context)
6774 ? lookup_interface (CLASS_NAME (context))
6775 : NULL_TREE);
6776 break;
6777 default:
6778 gcc_unreachable ();
6782 if (context == NULL_TREE)
6784 if (first)
6786 switch (TREE_CODE (objc_implementation_context))
6788 case CLASS_IMPLEMENTATION_TYPE:
6789 warning (0, "incomplete implementation of class %qE",
6790 CLASS_NAME (objc_implementation_context));
6791 break;
6792 case CATEGORY_IMPLEMENTATION_TYPE:
6793 warning (0, "incomplete implementation of category %qE",
6794 CLASS_SUPER_NAME (objc_implementation_context));
6795 break;
6796 default:
6797 gcc_unreachable ();
6799 first = 0;
6801 warning (0, "method definition for %<%c%E%> not found",
6802 mtype, METHOD_SEL_NAME (chain));
6805 chain = TREE_CHAIN (chain); /* next method... */
6807 return first;
6810 /* Check whether the current interface (accessible via
6811 'objc_implementation_context') actually implements protocol P, along
6812 with any protocols that P inherits. */
6814 static void
6815 check_protocol (tree p, const char *type, tree name)
6817 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6819 int f1, f2;
6821 /* Ensure that all protocols have bodies! */
6822 if (warn_protocol)
6824 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6825 objc_implementation_context,
6826 '+');
6827 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6828 objc_implementation_context,
6829 '-');
6831 else
6833 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6834 objc_implementation_context,
6835 '+');
6836 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6837 objc_implementation_context,
6838 '-');
6841 if (!f1 || !f2)
6842 warning (0, "%s %qE does not fully implement the %qE protocol",
6843 type, name, PROTOCOL_NAME (p));
6846 /* Check protocols recursively. */
6847 if (PROTOCOL_LIST (p))
6849 tree subs = PROTOCOL_LIST (p);
6850 tree super_class =
6851 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6853 while (subs)
6855 tree sub = TREE_VALUE (subs);
6857 /* If the superclass does not conform to the protocols
6858 inherited by P, then we must! */
6859 if (!super_class || !conforms_to_protocol (super_class, sub))
6860 check_protocol (sub, type, name);
6861 subs = TREE_CHAIN (subs);
6866 /* Check whether the current interface (accessible via
6867 'objc_implementation_context') actually implements the protocols listed
6868 in PROTO_LIST. */
6870 static void
6871 check_protocols (tree proto_list, const char *type, tree name)
6873 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6875 tree p = TREE_VALUE (proto_list);
6877 check_protocol (p, type, name);
6881 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6882 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6883 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6884 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6885 SUPER_NAME is the name of the category. For a class extension,
6886 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6887 static tree
6888 start_class (enum tree_code code, tree class_name, tree super_name,
6889 tree protocol_list, tree attributes)
6891 tree klass = NULL_TREE;
6892 tree decl;
6894 #ifdef OBJCPLUS
6895 if (current_namespace != global_namespace)
6897 error ("Objective-C declarations may only appear in global scope");
6899 #endif /* OBJCPLUS */
6901 if (objc_implementation_context)
6903 warning (0, "%<@end%> missing in implementation context");
6904 finish_class (objc_implementation_context);
6905 objc_ivar_chain = NULL_TREE;
6906 objc_implementation_context = NULL_TREE;
6909 /* If this is a class extension, we'll be "reopening" the existing
6910 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6911 a new node. */
6912 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6914 klass = make_node (code);
6915 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6918 /* Check for existence of the super class, if one was specified. Note
6919 that we must have seen an @interface, not just a @class. If we
6920 are looking at a @compatibility_alias, traverse it first. */
6921 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6922 && super_name)
6924 tree super = objc_is_class_name (super_name);
6925 tree super_interface = NULL_TREE;
6927 if (super)
6928 super_interface = lookup_interface (super);
6930 if (!super_interface)
6932 error ("cannot find interface declaration for %qE, superclass of %qE",
6933 super ? super : super_name,
6934 class_name);
6935 super_name = NULL_TREE;
6937 else
6939 if (TREE_DEPRECATED (super_interface))
6940 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6941 super);
6942 super_name = super;
6946 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6948 CLASS_NAME (klass) = class_name;
6949 CLASS_SUPER_NAME (klass) = super_name;
6950 CLASS_CLS_METHODS (klass) = NULL_TREE;
6953 if (! objc_is_class_name (class_name)
6954 && (decl = lookup_name (class_name)))
6956 error ("%qE redeclared as different kind of symbol",
6957 class_name);
6958 error ("previous declaration of %q+D",
6959 decl);
6962 switch (code)
6964 case CLASS_IMPLEMENTATION_TYPE:
6966 tree chain;
6968 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6969 if (TREE_VALUE (chain) == class_name)
6971 error ("reimplementation of class %qE",
6972 class_name);
6973 /* TODO: error message saying where it was previously
6974 implemented. */
6975 break;
6977 if (chain == NULL_TREE)
6978 implemented_classes = tree_cons (NULL_TREE, class_name,
6979 implemented_classes);
6982 /* Reset for multiple classes per file. */
6983 method_slot = 0;
6985 objc_implementation_context = klass;
6987 /* Lookup the interface for this implementation. */
6989 if (!(implementation_template = lookup_interface (class_name)))
6991 warning (0, "cannot find interface declaration for %qE",
6992 class_name);
6993 add_interface (implementation_template = objc_implementation_context,
6994 class_name);
6997 /* If a super class has been specified in the implementation,
6998 insure it conforms to the one specified in the interface. */
7000 if (super_name
7001 && (super_name != CLASS_SUPER_NAME (implementation_template)))
7003 tree previous_name = CLASS_SUPER_NAME (implementation_template);
7004 error ("conflicting super class name %qE",
7005 super_name);
7006 if (previous_name)
7007 error ("previous declaration of %qE", previous_name);
7008 else
7009 error ("previous declaration");
7012 else if (! super_name)
7014 CLASS_SUPER_NAME (objc_implementation_context)
7015 = CLASS_SUPER_NAME (implementation_template);
7017 break;
7019 case CLASS_INTERFACE_TYPE:
7020 if (lookup_interface (class_name))
7021 #ifdef OBJCPLUS
7022 error ("duplicate interface declaration for class %qE", class_name);
7023 #else
7024 warning (0, "duplicate interface declaration for class %qE", class_name);
7025 #endif
7026 else
7027 add_interface (klass, class_name);
7029 if (protocol_list)
7030 CLASS_PROTOCOL_LIST (klass)
7031 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
7033 if (attributes)
7035 tree attribute;
7036 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
7038 tree name = TREE_PURPOSE (attribute);
7040 /* TODO: Document what the objc_exception attribute is/does. */
7041 /* We handle the 'deprecated', 'visibility' and (undocumented)
7042 'objc_exception' attributes. */
7043 if (is_attribute_p ("deprecated", name))
7044 TREE_DEPRECATED (klass) = 1;
7045 else if (is_attribute_p ("objc_exception", name))
7046 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
7047 else if (is_attribute_p ("visibility", name))
7049 else
7050 /* Warn about and ignore all others for now, but store them. */
7051 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
7053 TYPE_ATTRIBUTES (klass) = attributes;
7055 break;
7057 case CATEGORY_INTERFACE_TYPE:
7059 tree class_category_is_assoc_with;
7061 /* For a category, class_name is really the name of the class that
7062 the following set of methods will be associated with. We must
7063 find the interface so that can derive the objects template. */
7064 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
7066 error ("cannot find interface declaration for %qE",
7067 class_name);
7068 exit (FATAL_EXIT_CODE);
7070 else
7072 if (TREE_DEPRECATED (class_category_is_assoc_with))
7073 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
7074 class_name);
7076 if (super_name == NULL_TREE)
7078 /* This is a class extension. Get the original
7079 interface, and continue working on it. */
7080 objc_in_class_extension = true;
7081 klass = class_category_is_assoc_with;
7083 if (protocol_list)
7085 /* Append protocols to the original protocol
7086 list. */
7087 CLASS_PROTOCOL_LIST (klass)
7088 = chainon (CLASS_PROTOCOL_LIST (klass),
7089 lookup_and_install_protocols
7090 (protocol_list,
7091 /* definition_required */ true));
7094 else
7096 add_category (class_category_is_assoc_with, klass);
7098 if (protocol_list)
7099 CLASS_PROTOCOL_LIST (klass)
7100 = lookup_and_install_protocols
7101 (protocol_list, /* definition_required */ true);
7105 break;
7107 case CATEGORY_IMPLEMENTATION_TYPE:
7108 /* Reset for multiple classes per file. */
7109 method_slot = 0;
7111 objc_implementation_context = klass;
7113 /* For a category, class_name is really the name of the class that
7114 the following set of methods will be associated with. We must
7115 find the interface so that can derive the objects template. */
7117 if (!(implementation_template = lookup_interface (class_name)))
7119 error ("cannot find interface declaration for %qE",
7120 class_name);
7121 exit (FATAL_EXIT_CODE);
7123 break;
7124 default:
7125 gcc_unreachable ();
7127 return klass;
7130 static tree
7131 continue_class (tree klass)
7133 switch (TREE_CODE (klass))
7135 case CLASS_IMPLEMENTATION_TYPE:
7136 case CATEGORY_IMPLEMENTATION_TYPE:
7138 struct imp_entry *imp_entry;
7140 /* Check consistency of the instance variables. */
7142 if (CLASS_RAW_IVARS (klass))
7143 check_ivars (implementation_template, klass);
7145 /* code generation */
7146 #ifdef OBJCPLUS
7147 push_lang_context (lang_name_c);
7148 #endif
7149 build_private_template (implementation_template);
7150 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7151 objc_instance_type = build_pointer_type (uprivate_record);
7153 imp_entry = ggc_alloc<struct imp_entry> ();
7155 imp_entry->next = imp_list;
7156 imp_entry->imp_context = klass;
7157 imp_entry->imp_template = implementation_template;
7158 ucls_super_ref = uucls_super_ref = NULL;
7159 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7161 imp_entry->class_decl = (*runtime.class_decl) (klass);
7162 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7164 else
7166 imp_entry->class_decl = (*runtime.category_decl) (klass);
7167 imp_entry->meta_decl = NULL;
7169 imp_entry->has_cxx_cdtors = 0;
7171 /* Append to front and increment count. */
7172 imp_list = imp_entry;
7173 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7174 imp_count++;
7175 else
7176 cat_count++;
7177 #ifdef OBJCPLUS
7178 pop_lang_context ();
7179 #endif /* OBJCPLUS */
7181 return get_class_ivars (implementation_template, true);
7183 case CLASS_INTERFACE_TYPE:
7185 if (objc_in_class_extension)
7186 return NULL_TREE;
7187 #ifdef OBJCPLUS
7188 push_lang_context (lang_name_c);
7189 #endif /* OBJCPLUS */
7190 objc_collecting_ivars = 1;
7191 build_private_template (klass);
7192 objc_collecting_ivars = 0;
7193 #ifdef OBJCPLUS
7194 pop_lang_context ();
7195 #endif /* OBJCPLUS */
7196 return NULL_TREE;
7198 default:
7199 return error_mark_node;
7203 /* This routine builds name of the setter synthesized function. */
7204 char *
7205 objc_build_property_setter_name (tree ident)
7207 /* TODO: Use alloca to allocate buffer of appropriate size. */
7208 static char string[BUFSIZE];
7209 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7210 string[3] = TOUPPER (string[3]);
7211 return string;
7214 /* This routine prepares the declarations of the property accessor
7215 helper functions (objc_getProperty(), etc) that are used when
7216 @synthesize is used.
7218 runtime-specific routines are built in the respective runtime
7219 initialize functions. */
7220 static void
7221 build_common_objc_property_accessor_helpers (void)
7223 tree type;
7225 /* Declare the following function:
7227 objc_getProperty (id self, SEL _cmd,
7228 ptrdiff_t offset, BOOL is_atomic); */
7229 type = build_function_type_list (objc_object_type,
7230 objc_object_type,
7231 objc_selector_type,
7232 ptrdiff_type_node,
7233 boolean_type_node,
7234 NULL_TREE);
7235 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7236 type, 0, NOT_BUILT_IN,
7237 NULL, NULL_TREE);
7238 TREE_NOTHROW (objc_getProperty_decl) = 0;
7240 /* Declare the following function:
7241 void
7242 objc_setProperty (id self, SEL _cmd,
7243 ptrdiff_t offset, id new_value,
7244 BOOL is_atomic, BOOL should_copy); */
7245 type = build_function_type_list (void_type_node,
7246 objc_object_type,
7247 objc_selector_type,
7248 ptrdiff_type_node,
7249 objc_object_type,
7250 boolean_type_node,
7251 boolean_type_node,
7252 NULL_TREE);
7253 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7254 type, 0, NOT_BUILT_IN,
7255 NULL, NULL_TREE);
7256 TREE_NOTHROW (objc_setProperty_decl) = 0;
7259 /* This looks up an ivar in a class (including superclasses). */
7260 static tree
7261 lookup_ivar (tree interface, tree instance_variable_name)
7263 while (interface)
7265 tree decl_chain;
7267 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7268 if (DECL_NAME (decl_chain) == instance_variable_name)
7269 return decl_chain;
7271 /* Not found. Search superclass if any. */
7272 if (CLASS_SUPER_NAME (interface))
7273 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7276 return NULL_TREE;
7279 /* This routine synthesizes a 'getter' method. This is only called
7280 for @synthesize properties. */
7281 static void
7282 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7284 location_t location = DECL_SOURCE_LOCATION (property);
7285 tree fn, decl;
7286 tree body;
7287 tree ret_val;
7289 /* If user has implemented a getter with same name then do nothing. */
7290 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7291 PROPERTY_GETTER_NAME (property)))
7292 return;
7294 /* Find declaration of the property getter in the interface (or
7295 superclass, or protocol). There must be one. */
7296 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7298 /* If one not declared in the interface, this condition has already
7299 been reported as user error (because property was not declared in
7300 the interface). */
7301 if (!decl)
7302 return;
7304 /* Adapt the 'decl'. Use the source location of the @synthesize
7305 statement for error messages. */
7306 decl = copy_node (decl);
7307 DECL_SOURCE_LOCATION (decl) = location;
7309 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7310 NULL_TREE);
7311 body = c_begin_compound_stmt (true);
7313 /* Now we need to decide how we build the getter. There are three
7314 cases:
7316 for 'copy' or 'retain' properties we need to use the
7317 objc_getProperty() accessor helper which knows about retain and
7318 copy. It supports both 'nonatomic' and 'atomic' access.
7320 for 'nonatomic, assign' properties we can access the instance
7321 variable directly. 'nonatomic' means we don't have to use locks,
7322 and 'assign' means we don't have to worry about retain or copy.
7323 If you combine the two, it means we can just access the instance
7324 variable directly.
7326 for 'atomic, assign' properties we use objc_copyStruct() (for the
7327 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7328 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7330 case OBJC_PROPERTY_RETAIN:
7331 case OBJC_PROPERTY_COPY:
7333 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7334 tree cmd, ivar, offset, is_atomic;
7335 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7337 /* Find the ivar to compute the offset. */
7338 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7339 if (!ivar || is_private (ivar))
7341 /* This should never happen. */
7342 error_at (location,
7343 "cannot find instance variable associated with property");
7344 ret_val = error_mark_node;
7345 break;
7347 offset = byte_position (ivar);
7349 if (PROPERTY_NONATOMIC (property))
7350 is_atomic = boolean_false_node;
7351 else
7352 is_atomic = boolean_true_node;
7354 ret_val = build_function_call
7355 (location,
7356 /* Function prototype. */
7357 objc_getProperty_decl,
7358 /* Parameters. */
7359 tree_cons /* self */
7360 (NULL_TREE, self_decl,
7361 tree_cons /* _cmd */
7362 (NULL_TREE, cmd,
7363 tree_cons /* offset */
7364 (NULL_TREE, offset,
7365 tree_cons /* is_atomic */
7366 (NULL_TREE, is_atomic, NULL_TREE)))));
7368 break;
7369 case OBJC_PROPERTY_ASSIGN:
7370 if (PROPERTY_NONATOMIC (property))
7372 /* We build "return self->PROPERTY_IVAR_NAME;" */
7373 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7374 break;
7376 else
7378 /* We build
7379 <property type> __objc_property_temp;
7380 objc_getPropertyStruct (&__objc_property_temp,
7381 &(self->PROPERTY_IVAR_NAME),
7382 sizeof (type of self->PROPERTY_IVAR_NAME),
7383 is_atomic,
7384 false)
7385 return __objc_property_temp;
7387 For the NeXT runtime, we need to use objc_copyStruct
7388 instead of objc_getPropertyStruct. */
7389 tree objc_property_temp_decl, function_decl, function_call;
7390 tree size_of, is_atomic;
7392 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7393 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7394 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7396 /* sizeof (ivar type). Since the ivar and the property have
7397 the same type, there is no need to lookup the ivar. */
7398 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7399 true /* is_sizeof */,
7400 false /* min_alignof */,
7401 false /* complain */);
7403 if (PROPERTY_NONATOMIC (property))
7404 is_atomic = boolean_false_node;
7405 else
7406 is_atomic = boolean_true_node;
7408 if (objc_copyStruct_decl)
7409 function_decl = objc_copyStruct_decl;
7410 else
7411 function_decl = objc_getPropertyStruct_decl;
7413 function_call = build_function_call
7414 (location,
7415 /* Function prototype. */
7416 function_decl,
7417 /* Parameters. */
7418 tree_cons /* &__objc_property_temp_decl */
7419 /* Warning: note that using build_fold_addr_expr_loc()
7420 here causes invalid code to be generated. */
7421 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7422 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7423 (NULL_TREE, build_fold_addr_expr_loc (location,
7424 objc_lookup_ivar
7425 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7426 tree_cons /* sizeof (PROPERTY_IVAR) */
7427 (NULL_TREE, size_of,
7428 tree_cons /* is_atomic */
7429 (NULL_TREE, is_atomic,
7430 /* TODO: This is currently ignored by the GNU
7431 runtime, but what about the next one ? */
7432 tree_cons /* has_strong */
7433 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7435 add_stmt (function_call);
7437 ret_val = objc_property_temp_decl;
7439 break;
7440 default:
7441 gcc_unreachable ();
7444 gcc_assert (ret_val);
7446 #ifdef OBJCPLUS
7447 finish_return_stmt (ret_val);
7448 #else
7449 c_finish_return (location, ret_val, NULL_TREE);
7450 #endif
7452 add_stmt (c_end_compound_stmt (location, body, true));
7453 fn = current_function_decl;
7454 #ifdef OBJCPLUS
7455 finish_function ();
7456 #endif
7457 objc_finish_method_definition (fn);
7460 /* This routine synthesizes a 'setter' method. */
7462 static void
7463 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7465 location_t location = DECL_SOURCE_LOCATION (property);
7466 tree fn, decl;
7467 tree body;
7468 tree new_value, statement;
7470 /* If user has implemented a setter with same name then do nothing. */
7471 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7472 PROPERTY_SETTER_NAME (property)))
7473 return;
7475 /* Find declaration of the property setter in the interface (or
7476 superclass, or protocol). There must be one. */
7477 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7479 /* If one not declared in the interface, this condition has already
7480 been reported as user error (because property was not declared in
7481 the interface). */
7482 if (!decl)
7483 return;
7485 /* Adapt the 'decl'. Use the source location of the @synthesize
7486 statement for error messages. */
7487 decl = copy_node (decl);
7488 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7490 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7491 NULL_TREE);
7493 body = c_begin_compound_stmt (true);
7495 /* The 'new_value' is the only argument to the method, which is the
7496 3rd argument of the function, after self and _cmd. We use twice
7497 TREE_CHAIN to move forward two arguments. */
7498 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7500 /* This would presumably happen if the user has specified a
7501 prototype for the setter that does not have an argument! */
7502 if (new_value == NULL_TREE)
7504 /* TODO: This should be caught much earlier than this. */
7505 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7506 /* Try to recover somehow. */
7507 new_value = error_mark_node;
7510 /* Now we need to decide how we build the setter. There are three
7511 cases:
7513 for 'copy' or 'retain' properties we need to use the
7514 objc_setProperty() accessor helper which knows about retain and
7515 copy. It supports both 'nonatomic' and 'atomic' access.
7517 for 'nonatomic, assign' properties we can access the instance
7518 variable directly. 'nonatomic' means we don't have to use locks,
7519 and 'assign' means we don't have to worry about retain or copy.
7520 If you combine the two, it means we can just access the instance
7521 variable directly.
7523 for 'atomic, assign' properties we use objc_copyStruct() (for the
7524 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7525 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7527 case OBJC_PROPERTY_RETAIN:
7528 case OBJC_PROPERTY_COPY:
7530 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7531 tree cmd, ivar, offset, is_atomic, should_copy;
7532 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7534 /* Find the ivar to compute the offset. */
7535 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7536 if (!ivar || is_private (ivar))
7538 error_at (location,
7539 "cannot find instance variable associated with property");
7540 statement = error_mark_node;
7541 break;
7543 offset = byte_position (ivar);
7545 if (PROPERTY_NONATOMIC (property))
7546 is_atomic = boolean_false_node;
7547 else
7548 is_atomic = boolean_true_node;
7550 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7551 should_copy = boolean_true_node;
7552 else
7553 should_copy = boolean_false_node;
7555 statement = build_function_call
7556 (location,
7557 /* Function prototype. */
7558 objc_setProperty_decl,
7559 /* Parameters. */
7560 tree_cons /* self */
7561 (NULL_TREE, self_decl,
7562 tree_cons /* _cmd */
7563 (NULL_TREE, cmd,
7564 tree_cons /* offset */
7565 (NULL_TREE, offset,
7566 tree_cons /* new_value */
7567 (NULL_TREE, new_value,
7568 tree_cons /* is_atomic */
7569 (NULL_TREE, is_atomic,
7570 tree_cons /* should_copy */
7571 (NULL_TREE, should_copy, NULL_TREE)))))));
7573 break;
7574 case OBJC_PROPERTY_ASSIGN:
7575 if (PROPERTY_NONATOMIC (property))
7577 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7578 statement = build_modify_expr
7579 (location,
7580 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7581 NULL_TREE, NOP_EXPR,
7582 location, new_value, NULL_TREE);
7583 break;
7585 else
7587 /* We build
7588 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7589 &new_value,
7590 sizeof (type of self->PROPERTY_IVAR_NAME),
7591 is_atomic,
7592 false)
7594 For the NeXT runtime, we need to use objc_copyStruct
7595 instead of objc_getPropertyStruct. */
7596 tree function_decl, size_of, is_atomic;
7598 /* sizeof (ivar type). Since the ivar and the property have
7599 the same type, there is no need to lookup the ivar. */
7600 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7601 true /* is_sizeof */,
7602 false /* min_alignof */,
7603 false /* complain */);
7605 if (PROPERTY_NONATOMIC (property))
7606 is_atomic = boolean_false_node;
7607 else
7608 is_atomic = boolean_true_node;
7610 if (objc_copyStruct_decl)
7611 function_decl = objc_copyStruct_decl;
7612 else
7613 function_decl = objc_setPropertyStruct_decl;
7615 statement = build_function_call
7616 (location,
7617 /* Function prototype. */
7618 function_decl,
7619 /* Parameters. */
7620 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7621 (NULL_TREE, build_fold_addr_expr_loc (location,
7622 objc_lookup_ivar
7623 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7624 tree_cons /* &new_value */
7625 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7626 tree_cons /* sizeof (PROPERTY_IVAR) */
7627 (NULL_TREE, size_of,
7628 tree_cons /* is_atomic */
7629 (NULL_TREE, is_atomic,
7630 /* TODO: This is currently ignored by the GNU
7631 runtime, but what about the next one ? */
7632 tree_cons /* has_strong */
7633 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7635 break;
7636 default:
7637 gcc_unreachable ();
7639 gcc_assert (statement);
7641 add_stmt (statement);
7642 add_stmt (c_end_compound_stmt (location, body, true));
7643 fn = current_function_decl;
7644 #ifdef OBJCPLUS
7645 finish_function ();
7646 #endif
7647 objc_finish_method_definition (fn);
7650 /* This function is a sub-routine of objc_add_synthesize_declaration.
7651 It is called for each property to synthesize once we have
7652 determined that the context is Ok. */
7653 static void
7654 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7655 tree property_name, tree ivar_name)
7657 /* Find the @property declaration. */
7658 tree property;
7659 tree x;
7661 /* Check that synthesize or dynamic has not already been used for
7662 the same property. */
7663 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7664 if (PROPERTY_NAME (property) == property_name)
7666 location_t original_location = DECL_SOURCE_LOCATION (property);
7668 if (PROPERTY_DYNAMIC (property))
7669 error_at (location, "property %qs already specified in %<@dynamic%>",
7670 IDENTIFIER_POINTER (property_name));
7671 else
7672 error_at (location, "property %qs already specified in %<@synthesize%>",
7673 IDENTIFIER_POINTER (property_name));
7675 if (original_location != UNKNOWN_LOCATION)
7676 inform (original_location, "originally specified here");
7677 return;
7680 /* Check that the property is declared in the interface. It could
7681 also be declared in a superclass or protocol. */
7682 property = lookup_property (interface, property_name);
7684 if (!property)
7686 error_at (location, "no declaration of property %qs found in the interface",
7687 IDENTIFIER_POINTER (property_name));
7688 return;
7690 else
7692 /* We have to copy the property, because we want to chain it to
7693 the implementation context, and we want to store the source
7694 location of the @synthesize, not of the original
7695 @property. */
7696 property = copy_node (property);
7697 DECL_SOURCE_LOCATION (property) = location;
7700 /* Determine PROPERTY_IVAR_NAME. */
7701 if (ivar_name == NULL_TREE)
7702 ivar_name = property_name;
7704 /* Check that the instance variable exists. You can only use an
7705 instance variable from the same class, not one from the
7706 superclass (this makes sense as it allows us to check that an
7707 instance variable is only used in one synthesized property). */
7709 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7710 tree type_of_ivar;
7711 if (!ivar)
7713 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7714 IDENTIFIER_POINTER (property_name));
7715 return;
7718 if (DECL_BIT_FIELD_TYPE (ivar))
7719 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7720 else
7721 type_of_ivar = TREE_TYPE (ivar);
7723 /* If the instance variable has a different C type, we throw an error ... */
7724 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7725 /* ... unless the property is readonly, in which case we allow
7726 the instance variable to be more specialized (this means we
7727 can generate the getter all right and it works). */
7728 && (!PROPERTY_READONLY (property)
7729 || !objc_compare_types (TREE_TYPE (property),
7730 type_of_ivar, -5, NULL_TREE)))
7732 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7734 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7735 IDENTIFIER_POINTER (property_name),
7736 IDENTIFIER_POINTER (ivar_name));
7738 if (original_location != UNKNOWN_LOCATION)
7739 inform (original_location, "originally specified here");
7742 /* If the instance variable is a bitfield, the property must be
7743 'assign', 'nonatomic' because the runtime getter/setter helper
7744 do not work with bitfield instance variables. */
7745 if (DECL_BIT_FIELD_TYPE (ivar))
7747 /* If there is an error, we return and not generate any
7748 getter/setter because trying to set up the runtime
7749 getter/setter helper calls with bitfields is at high risk
7750 of ICE. */
7752 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7754 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7756 error_at (location, "%<assign%> property %qs is using bit-field "
7757 "instance variable %qs",
7758 IDENTIFIER_POINTER (property_name),
7759 IDENTIFIER_POINTER (ivar_name));
7761 if (original_location != UNKNOWN_LOCATION)
7762 inform (original_location, "originally specified here");
7763 return;
7766 if (!PROPERTY_NONATOMIC (property))
7768 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7770 error_at (location, "%<atomic%> property %qs is using bit-field "
7771 "instance variable %qs",
7772 IDENTIFIER_POINTER (property_name),
7773 IDENTIFIER_POINTER (ivar_name));
7775 if (original_location != UNKNOWN_LOCATION)
7776 inform (original_location, "originally specified here");
7777 return;
7782 /* Check that no other property is using the same instance
7783 variable. */
7784 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7785 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7787 location_t original_location = DECL_SOURCE_LOCATION (x);
7789 error_at (location, "property %qs is using the same instance variable as property %qs",
7790 IDENTIFIER_POINTER (property_name),
7791 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7793 if (original_location != UNKNOWN_LOCATION)
7794 inform (original_location, "originally specified here");
7796 /* We keep going on. This won't cause the compiler to fail;
7797 the failure would most likely be at runtime. */
7800 /* Note that a @synthesize (and only a @synthesize) always sets
7801 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7802 @synthesize by that. */
7803 PROPERTY_IVAR_NAME (property) = ivar_name;
7805 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7806 original declaration; they are always set (with the exception of
7807 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7809 /* Add the property to the list of properties for current implementation. */
7810 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7811 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7813 /* Note how we don't actually synthesize the getter/setter here; it
7814 would be very natural, but we may miss the fact that the user has
7815 implemented his own getter/setter later on in the @implementation
7816 (in which case we shouldn't generate getter/setter). We wait
7817 until we have parsed it all before generating the code. */
7820 /* This function is called by the parser after a @synthesize
7821 expression is parsed. 'location' is the location of the
7822 @synthesize expression, and 'property_and_ivar_list' is a chained
7823 list of the property and ivar names. */
7824 void
7825 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7827 tree interface, chain;
7829 if (flag_objc1_only)
7830 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7832 if (property_and_ivar_list == error_mark_node)
7833 return;
7835 if (!objc_implementation_context)
7837 /* We can get here only in Objective-C; the Objective-C++ parser
7838 detects the problem while parsing, outputs the error
7839 "misplaced '@synthesize' Objective-C++ construct" and skips
7840 the declaration. */
7841 error_at (location, "%<@synthesize%> not in @implementation context");
7842 return;
7845 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7847 error_at (location, "%<@synthesize%> cannot be used in categories");
7848 return;
7851 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7852 if (!interface)
7854 /* I can't see how this could happen, but it is good as a safety check. */
7855 error_at (location,
7856 "%<@synthesize%> requires the @interface of the class to be available");
7857 return;
7860 /* Now, iterate over the properties and do each of them. */
7861 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7863 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7864 TREE_PURPOSE (chain));
7868 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7869 is called for each property to mark as dynamic once we have
7870 determined that the context is Ok. */
7871 static void
7872 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7873 tree property_name)
7875 /* Find the @property declaration. */
7876 tree property;
7878 /* Check that synthesize or dynamic has not already been used for
7879 the same property. */
7880 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7881 if (PROPERTY_NAME (property) == property_name)
7883 location_t original_location = DECL_SOURCE_LOCATION (property);
7885 if (PROPERTY_DYNAMIC (property))
7886 error_at (location, "property %qs already specified in %<@dynamic%>",
7887 IDENTIFIER_POINTER (property_name));
7888 else
7889 error_at (location, "property %qs already specified in %<@synthesize%>",
7890 IDENTIFIER_POINTER (property_name));
7892 if (original_location != UNKNOWN_LOCATION)
7893 inform (original_location, "originally specified here");
7894 return;
7897 /* Check that the property is declared in the interface. It could
7898 also be declared in a superclass or protocol. */
7899 property = lookup_property (interface, property_name);
7901 if (!property)
7903 error_at (location, "no declaration of property %qs found in the interface",
7904 IDENTIFIER_POINTER (property_name));
7905 return;
7907 else
7909 /* We have to copy the property, because we want to chain it to
7910 the implementation context, and we want to store the source
7911 location of the @synthesize, not of the original
7912 @property. */
7913 property = copy_node (property);
7914 DECL_SOURCE_LOCATION (property) = location;
7917 /* Note that a @dynamic (and only a @dynamic) always sets
7918 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7919 (actually, as explained above, PROPERTY_DECL generated by
7920 @property and associated with a @dynamic property are also marked
7921 as PROPERTY_DYNAMIC). */
7922 PROPERTY_DYNAMIC (property) = 1;
7924 /* Add the property to the list of properties for current implementation. */
7925 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7926 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7929 /* This function is called by the parser after a @dynamic expression
7930 is parsed. 'location' is the location of the @dynamic expression,
7931 and 'property_list' is a chained list of all the property
7932 names. */
7933 void
7934 objc_add_dynamic_declaration (location_t location, tree property_list)
7936 tree interface, chain;
7938 if (flag_objc1_only)
7939 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7941 if (property_list == error_mark_node)
7942 return;
7944 if (!objc_implementation_context)
7946 /* We can get here only in Objective-C; the Objective-C++ parser
7947 detects the problem while parsing, outputs the error
7948 "misplaced '@dynamic' Objective-C++ construct" and skips the
7949 declaration. */
7950 error_at (location, "%<@dynamic%> not in @implementation context");
7951 return;
7954 /* @dynamic is allowed in categories. */
7955 switch (TREE_CODE (objc_implementation_context))
7957 case CLASS_IMPLEMENTATION_TYPE:
7958 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7959 break;
7960 case CATEGORY_IMPLEMENTATION_TYPE:
7961 interface = lookup_category (implementation_template,
7962 CLASS_SUPER_NAME (objc_implementation_context));
7963 break;
7964 default:
7965 gcc_unreachable ();
7968 if (!interface)
7970 /* I can't see how this could happen, but it is good as a safety check. */
7971 error_at (location,
7972 "%<@dynamic%> requires the @interface of the class to be available");
7973 return;
7976 /* Now, iterate over the properties and do each of them. */
7977 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7979 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7983 /* Main routine to generate code/data for all the property information for
7984 current implementation (class or category). CLASS is the interface where
7985 ivars are declared. CLASS_METHODS is where methods are found which
7986 could be a class or a category depending on whether we are implementing
7987 property of a class or a category. */
7989 static void
7990 objc_gen_property_data (tree klass, tree class_methods)
7992 tree x;
7994 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7996 /* @dynamic property - nothing to check or synthesize. */
7997 if (PROPERTY_DYNAMIC (x))
7998 continue;
8000 /* @synthesize property - need to synthesize the accessors. */
8001 if (PROPERTY_IVAR_NAME (x))
8003 objc_synthesize_getter (klass, class_methods, x);
8005 if (PROPERTY_READONLY (x) == 0)
8006 objc_synthesize_setter (klass, class_methods, x);
8008 continue;
8011 gcc_unreachable ();
8015 /* This is called once we see the "@end" in an interface/implementation. */
8017 static void
8018 finish_class (tree klass)
8020 switch (TREE_CODE (klass))
8022 case CLASS_IMPLEMENTATION_TYPE:
8024 /* All metadata generation is done in runtime.generate_metadata(). */
8026 /* Generate what needed for property; setters, getters, etc. */
8027 objc_gen_property_data (implementation_template, implementation_template);
8029 if (implementation_template != objc_implementation_context)
8031 /* Ensure that all method listed in the interface contain bodies. */
8032 check_methods (CLASS_CLS_METHODS (implementation_template),
8033 objc_implementation_context, '+');
8034 check_methods (CLASS_NST_METHODS (implementation_template),
8035 objc_implementation_context, '-');
8037 if (CLASS_PROTOCOL_LIST (implementation_template))
8038 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
8039 "class",
8040 CLASS_NAME (objc_implementation_context));
8042 break;
8044 case CATEGORY_IMPLEMENTATION_TYPE:
8046 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
8048 if (category)
8050 /* Generate what needed for property; setters, getters, etc. */
8051 objc_gen_property_data (implementation_template, category);
8053 /* Ensure all method listed in the interface contain bodies. */
8054 check_methods (CLASS_CLS_METHODS (category),
8055 objc_implementation_context, '+');
8056 check_methods (CLASS_NST_METHODS (category),
8057 objc_implementation_context, '-');
8059 if (CLASS_PROTOCOL_LIST (category))
8060 check_protocols (CLASS_PROTOCOL_LIST (category),
8061 "category",
8062 CLASS_SUPER_NAME (objc_implementation_context));
8064 break;
8066 case CLASS_INTERFACE_TYPE:
8067 case CATEGORY_INTERFACE_TYPE:
8068 case PROTOCOL_INTERFACE_TYPE:
8070 /* Process properties of the class. */
8071 tree x;
8072 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
8074 /* Now we check that the appropriate getter is declared,
8075 and if not, we declare one ourselves. */
8076 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
8077 PROPERTY_GETTER_NAME (x));
8079 if (getter_decl)
8081 /* TODO: Check that the declaration is consistent with the property. */
8084 else
8086 /* Generate an instance method declaration for the
8087 getter; for example "- (id) name;". In general it
8088 will be of the form
8089 -(type)property_getter_name; */
8090 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
8091 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8092 rettype, PROPERTY_GETTER_NAME (x),
8093 NULL_TREE, false);
8094 if (PROPERTY_OPTIONAL (x))
8095 objc_add_method (objc_interface_context, getter_decl, false, true);
8096 else
8097 objc_add_method (objc_interface_context, getter_decl, false, false);
8098 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
8099 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
8102 if (PROPERTY_READONLY (x) == 0)
8104 /* Now we check that the appropriate setter is declared,
8105 and if not, we declare on ourselves. */
8106 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
8107 PROPERTY_SETTER_NAME (x));
8109 if (setter_decl)
8111 /* TODO: Check that the declaration is consistent with the property. */
8114 else
8116 /* The setter name is something like 'setName:'.
8117 We need the substring 'setName' to build the
8118 method declaration due to how the declaration
8119 works. TODO: build_method_decl() will then
8120 generate back 'setName:' from 'setName'; it
8121 would be more efficient to hook into there. */
8122 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8123 size_t length = strlen (full_setter_name);
8124 char *setter_name = (char *) alloca (length);
8125 tree ret_type, selector, arg_type, arg_name;
8127 memcpy (setter_name, full_setter_name, length - 1);
8128 setter_name[length - 1] = '\0';
8129 ret_type = build_tree_list (NULL_TREE, void_type_node);
8130 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8131 arg_name = get_identifier ("_value");
8132 selector = objc_build_keyword_decl (get_identifier (setter_name),
8133 arg_type, arg_name, NULL);
8134 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8135 ret_type, selector,
8136 build_tree_list (NULL_TREE, NULL_TREE),
8137 false);
8138 if (PROPERTY_OPTIONAL (x))
8139 objc_add_method (objc_interface_context, setter_decl, false, true);
8140 else
8141 objc_add_method (objc_interface_context, setter_decl, false, false);
8142 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8143 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8147 break;
8149 default:
8150 gcc_unreachable ();
8151 break;
8155 static tree
8156 add_protocol (tree protocol)
8158 /* Put protocol on list in reverse order. */
8159 TREE_CHAIN (protocol) = protocol_chain;
8160 protocol_chain = protocol;
8161 return protocol_chain;
8164 /* Check that a protocol is defined, and, recursively, that all
8165 protocols that this protocol conforms to are defined too. */
8166 static void
8167 check_that_protocol_is_defined (tree protocol)
8169 if (!PROTOCOL_DEFINED (protocol))
8170 warning (0, "definition of protocol %qE not found",
8171 PROTOCOL_NAME (protocol));
8173 /* If the protocol itself conforms to other protocols, check them
8174 too, recursively. */
8175 if (PROTOCOL_LIST (protocol))
8177 tree p;
8179 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8180 check_that_protocol_is_defined (TREE_VALUE (p));
8184 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8185 emitted if the protocol is deprecated. If 'definition_required' is
8186 true, a warning is emitted if a full @protocol definition has not
8187 been seen. */
8188 static tree
8189 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8191 tree chain;
8193 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8194 if (ident == PROTOCOL_NAME (chain))
8196 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8198 /* It would be nice to use warn_deprecated_use() here, but
8199 we are using TREE_CHAIN (which is supposed to be the
8200 TYPE_STUB_DECL for a TYPE) for something different. */
8201 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8202 PROTOCOL_NAME (chain));
8205 if (definition_required)
8206 check_that_protocol_is_defined (chain);
8208 return chain;
8211 return NULL_TREE;
8214 /* This function forward declares the protocols named by NAMES. If
8215 they are already declared or defined, the function has no effect. */
8217 void
8218 objc_declare_protocol (tree name, tree attributes)
8220 bool deprecated = false;
8222 #ifdef OBJCPLUS
8223 if (current_namespace != global_namespace) {
8224 error ("Objective-C declarations may only appear in global scope");
8226 #endif /* OBJCPLUS */
8228 /* Determine if 'deprecated', the only attribute we recognize for
8229 protocols, was used. Ignore all other attributes. */
8230 if (attributes)
8232 tree attribute;
8233 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8235 tree name = TREE_PURPOSE (attribute);
8237 if (is_attribute_p ("deprecated", name))
8238 deprecated = true;
8239 else
8240 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8244 if (lookup_protocol (name, /* warn if deprecated */ false,
8245 /* definition_required */ false) == NULL_TREE)
8247 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8249 TYPE_LANG_SLOT_1 (protocol)
8250 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8251 PROTOCOL_NAME (protocol) = name;
8252 PROTOCOL_LIST (protocol) = NULL_TREE;
8253 add_protocol (protocol);
8254 PROTOCOL_DEFINED (protocol) = 0;
8255 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8257 if (attributes)
8259 /* TODO: Do we need to store the attributes here ? */
8260 TYPE_ATTRIBUTES (protocol) = attributes;
8261 if (deprecated)
8262 TREE_DEPRECATED (protocol) = 1;
8267 static tree
8268 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8270 tree protocol;
8271 bool deprecated = false;
8273 #ifdef OBJCPLUS
8274 if (current_namespace != global_namespace) {
8275 error ("Objective-C declarations may only appear in global scope");
8277 #endif /* OBJCPLUS */
8279 /* Determine if 'deprecated', the only attribute we recognize for
8280 protocols, was used. Ignore all other attributes. */
8281 if (attributes)
8283 tree attribute;
8284 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8286 tree name = TREE_PURPOSE (attribute);
8288 if (is_attribute_p ("deprecated", name))
8289 deprecated = true;
8290 else
8291 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8295 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8296 /* definition_required */ false);
8298 if (!protocol)
8300 protocol = make_node (code);
8301 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8303 PROTOCOL_NAME (protocol) = name;
8304 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8305 add_protocol (protocol);
8306 PROTOCOL_DEFINED (protocol) = 1;
8307 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8309 check_protocol_recursively (protocol, list);
8311 else if (! PROTOCOL_DEFINED (protocol))
8313 PROTOCOL_DEFINED (protocol) = 1;
8314 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8316 check_protocol_recursively (protocol, list);
8318 else
8320 warning (0, "duplicate declaration for protocol %qE",
8321 name);
8324 if (attributes)
8326 TYPE_ATTRIBUTES (protocol) = attributes;
8327 if (deprecated)
8328 TREE_DEPRECATED (protocol) = 1;
8331 return protocol;
8334 /* Decay array and function parameters into pointers. */
8336 static tree
8337 objc_decay_parm_type (tree type)
8339 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8340 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8341 ? TREE_TYPE (type)
8342 : type);
8344 return type;
8347 static GTY(()) tree objc_parmlist = NULL_TREE;
8349 /* Append PARM to a list of formal parameters of a method, making a necessary
8350 array-to-pointer adjustment along the way. */
8352 void
8353 objc_push_parm (tree parm)
8355 tree type;
8357 if (TREE_TYPE (parm) == error_mark_node)
8359 objc_parmlist = chainon (objc_parmlist, parm);
8360 return;
8363 /* Decay arrays and functions into pointers. */
8364 type = objc_decay_parm_type (TREE_TYPE (parm));
8366 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8367 built as well. */
8368 if (type != TREE_TYPE (parm))
8369 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8371 DECL_ARG_TYPE (parm)
8372 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8374 /* Record constancy and volatility. */
8375 c_apply_type_quals_to_decl
8376 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8377 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8378 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
8379 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8381 objc_parmlist = chainon (objc_parmlist, parm);
8384 /* Retrieve the formal parameter list constructed via preceding calls to
8385 objc_push_parm(). */
8387 #ifdef OBJCPLUS
8388 tree
8389 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8390 tree expr ATTRIBUTE_UNUSED)
8392 tree parm_info = objc_parmlist;
8393 objc_parmlist = NULL_TREE;
8395 return parm_info;
8397 #else
8398 struct c_arg_info *
8399 objc_get_parm_info (int have_ellipsis, tree expr)
8401 tree parm_info = objc_parmlist;
8402 struct c_arg_info *arg_info;
8403 /* The C front-end requires an elaborate song and dance at
8404 this point. */
8405 push_scope ();
8406 declare_parm_level ();
8407 while (parm_info)
8409 tree next = DECL_CHAIN (parm_info);
8411 DECL_CHAIN (parm_info) = NULL_TREE;
8412 parm_info = pushdecl (parm_info);
8413 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8414 parm_info = next;
8416 arg_info = get_parm_info (have_ellipsis, expr);
8417 pop_scope ();
8418 objc_parmlist = NULL_TREE;
8419 return arg_info;
8421 #endif
8423 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8424 method definitions. In the case of instance methods, we can be more
8425 specific as to the type of 'self'. */
8427 static void
8428 synth_self_and_ucmd_args (void)
8430 tree self_type;
8432 if (objc_method_context
8433 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8434 self_type = objc_instance_type;
8435 else
8436 /* Really a `struct objc_class *'. However, we allow people to
8437 assign to self, which changes its type midstream. */
8438 self_type = objc_object_type;
8440 /* id self; */
8441 objc_push_parm (build_decl (input_location,
8442 PARM_DECL, self_id, self_type));
8444 /* SEL _cmd; */
8445 objc_push_parm (build_decl (input_location,
8446 PARM_DECL, ucmd_id, objc_selector_type));
8449 /* Transform an Objective-C method definition into a static C function
8450 definition, synthesizing the first two arguments, "self" and "_cmd",
8451 in the process. EXPR is NULL or an expression that needs to be
8452 evaluated for the side effects of array size expressions in the
8453 parameters. */
8455 static void
8456 start_method_def (tree method, tree expr)
8458 tree parmlist;
8459 #ifdef OBJCPLUS
8460 tree parm_info;
8461 #else
8462 struct c_arg_info *parm_info;
8463 #endif
8464 int have_ellipsis = 0;
8466 /* If we are defining a "dealloc" method in a non-root class, we
8467 will need to check if a [super dealloc] is missing, and warn if
8468 it is. */
8469 if(CLASS_SUPER_NAME (objc_implementation_context)
8470 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8471 should_call_super_dealloc = 1;
8472 else
8473 should_call_super_dealloc = 0;
8475 /* Required to implement _msgSuper. */
8476 objc_method_context = method;
8477 UOBJC_SUPER_decl = NULL_TREE;
8479 /* Generate prototype declarations for arguments..."new-style". */
8480 synth_self_and_ucmd_args ();
8482 /* Generate argument declarations if a keyword_decl. */
8483 parmlist = METHOD_SEL_ARGS (method);
8484 while (parmlist)
8486 /* parmlist is a KEYWORD_DECL. */
8487 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8488 tree parm;
8490 parm = build_decl (input_location,
8491 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8492 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8493 objc_push_parm (parm);
8494 parmlist = DECL_CHAIN (parmlist);
8497 if (METHOD_ADD_ARGS (method))
8499 tree akey;
8501 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8502 akey; akey = TREE_CHAIN (akey))
8504 objc_push_parm (TREE_VALUE (akey));
8507 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8508 have_ellipsis = 1;
8511 parm_info = objc_get_parm_info (have_ellipsis, expr);
8513 really_start_method (objc_method_context, parm_info);
8516 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8517 overloading. */
8518 static int
8519 objc_types_are_equivalent (tree type1, tree type2)
8521 if (type1 == type2)
8522 return 1;
8524 /* Strip away indirections. */
8525 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8526 && (TREE_CODE (type1) == TREE_CODE (type2)))
8527 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8528 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8529 return 0;
8531 /* Compare the protocol lists. */
8532 type1 = (TYPE_HAS_OBJC_INFO (type1)
8533 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8534 : NULL_TREE);
8535 type2 = (TYPE_HAS_OBJC_INFO (type2)
8536 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8537 : NULL_TREE);
8539 /* If there are no protocols (most common case), the types are
8540 identical. */
8541 if (type1 == NULL_TREE && type2 == NULL_TREE)
8542 return 1;
8544 /* If one has protocols, and the other one hasn't, they are not
8545 identical. */
8546 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8547 || (type1 != NULL_TREE && type2 == NULL_TREE))
8548 return 0;
8549 else
8551 /* Else, both have protocols, and we need to do the full
8552 comparison. It is possible that either type1 or type2
8553 contain some duplicate protocols in the list, so we can't
8554 even just compare list_length as a first check. */
8555 tree t;
8557 for (t = type2; t; t = TREE_CHAIN (t))
8558 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8559 return 0;
8561 for (t = type1; t; t = TREE_CHAIN (t))
8562 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8563 return 0;
8565 return 1;
8569 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8571 static int
8572 objc_types_share_size_and_alignment (tree type1, tree type2)
8574 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8575 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8578 /* Return 1 if PROTO1 is equivalent to PROTO2
8579 for purposes of method overloading. Ordinarily, the type signatures
8580 should match up exactly, unless STRICT is zero, in which case we
8581 shall allow differences in which the size and alignment of a type
8582 is the same. */
8584 static int
8585 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8587 tree type1, type2;
8589 /* The following test is needed in case there are hashing
8590 collisions. */
8591 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8592 return 0;
8594 /* Compare return types. */
8595 type1 = TREE_VALUE (TREE_TYPE (proto1));
8596 type2 = TREE_VALUE (TREE_TYPE (proto2));
8598 if (!objc_types_are_equivalent (type1, type2)
8599 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8600 return 0;
8602 /* Compare argument types. */
8604 /* The first argument (objc_object_type) is always the same, no need
8605 to compare. */
8607 /* The second argument (objc_selector_type) is always the same, no
8608 need to compare. */
8610 /* Compare the other arguments. */
8612 tree arg1, arg2;
8614 /* Compare METHOD_SEL_ARGS. */
8615 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8616 arg1 && arg2;
8617 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8619 type1 = TREE_VALUE (TREE_TYPE (arg1));
8620 type2 = TREE_VALUE (TREE_TYPE (arg2));
8622 /* FIXME: Do we need to decay argument types to compare them ? */
8623 type1 = objc_decay_parm_type (type1);
8624 type2 = objc_decay_parm_type (type2);
8626 if (!objc_types_are_equivalent (type1, type2)
8627 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8628 return 0;
8631 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8632 both NULL. */
8633 if (arg1 != arg2)
8634 return 0;
8636 /* Compare METHOD_ADD_ARGS. */
8637 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8638 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8639 return 0;
8641 if (METHOD_ADD_ARGS (proto1))
8643 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8644 arg1 && arg2;
8645 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8647 type1 = TREE_TYPE (TREE_VALUE (arg1));
8648 type2 = TREE_TYPE (TREE_VALUE (arg2));
8650 /* FIXME: Do we need to decay argument types to compare them ? */
8651 type1 = objc_decay_parm_type (type1);
8652 type2 = objc_decay_parm_type (type2);
8654 if (!objc_types_are_equivalent (type1, type2)
8655 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8656 return 0;
8660 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8661 both NULL. */
8662 if (arg1 != arg2)
8663 return 0;
8665 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8666 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8667 return 0;
8670 /* Success. */
8671 return 1;
8674 /* This routine returns true if TYPE is a valid objc object type,
8675 suitable for messaging; false otherwise. If 'accept_class' is
8676 'true', then a Class object is considered valid for messaging and
8677 'true' is returned if 'type' refers to a Class. If 'accept_class'
8678 is 'false', then a Class object is not considered valid for
8679 messaging and 'false' is returned in that case. */
8681 static bool
8682 objc_type_valid_for_messaging (tree type, bool accept_classes)
8684 if (!POINTER_TYPE_P (type))
8685 return false;
8687 /* We will check for an NSObject type attribute on the pointer if other
8688 tests fail. */
8689 tree type_attr = TYPE_ATTRIBUTES (type);
8691 /* Remove the pointer indirection; don't remove more than one
8692 otherwise we'd consider "NSObject **" a valid type for messaging,
8693 which it isn't. */
8694 type = TREE_TYPE (type);
8696 /* We allow void * to have an NSObject type attr. */
8697 if (VOID_TYPE_P (type) && type_attr)
8698 return lookup_attribute ("NSObject", type_attr) != NULL_TREE;
8700 if (TREE_CODE (type) != RECORD_TYPE)
8701 return false;
8703 if (objc_is_object_id (type))
8704 return true;
8706 if (objc_is_class_id (type))
8707 return accept_classes;
8709 if (TYPE_HAS_OBJC_INFO (type))
8710 return true;
8712 if (type_attr)
8713 return lookup_attribute ("NSObject", type_attr) != NULL_TREE;
8715 return false;
8718 void
8719 objc_start_function (tree name, tree type, tree attrs,
8720 #ifdef OBJCPLUS
8721 tree params
8722 #else
8723 struct c_arg_info *params
8724 #endif
8727 tree fndecl = build_decl (input_location,
8728 FUNCTION_DECL, name, type);
8730 #ifdef OBJCPLUS
8731 DECL_ARGUMENTS (fndecl) = params;
8732 DECL_INITIAL (fndecl) = error_mark_node;
8733 DECL_EXTERNAL (fndecl) = 0;
8734 TREE_STATIC (fndecl) = 1;
8735 retrofit_lang_decl (fndecl);
8736 cplus_decl_attributes (&fndecl, attrs, 0);
8737 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8738 #else
8739 current_function_returns_value = 0; /* Assume, until we see it does. */
8740 current_function_returns_null = 0;
8741 decl_attributes (&fndecl, attrs, 0);
8742 announce_function (fndecl);
8743 DECL_INITIAL (fndecl) = error_mark_node;
8744 DECL_EXTERNAL (fndecl) = 0;
8745 TREE_STATIC (fndecl) = 1;
8746 current_function_decl = pushdecl (fndecl);
8747 push_scope ();
8748 declare_parm_level ();
8749 DECL_RESULT (current_function_decl)
8750 = build_decl (input_location,
8751 RESULT_DECL, NULL_TREE,
8752 TREE_TYPE (TREE_TYPE (current_function_decl)));
8753 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8754 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8755 start_fname_decls ();
8756 store_parm_decls_from (params);
8757 #endif
8759 TREE_USED (current_function_decl) = 1;
8762 /* - Generate an identifier for the function. the format is "_n_cls",
8763 where 1 <= n <= nMethods, and cls is the name the implementation we
8764 are processing.
8765 - Install the return type from the method declaration.
8766 - If we have a prototype, check for type consistency. */
8768 static void
8769 really_start_method (tree method,
8770 #ifdef OBJCPLUS
8771 tree parmlist
8772 #else
8773 struct c_arg_info *parmlist
8774 #endif
8777 tree ret_type, meth_type;
8778 tree method_id;
8779 const char *sel_name, *class_name, *cat_name;
8780 char *buf;
8782 /* Synth the storage class & assemble the return type. */
8783 ret_type = TREE_VALUE (TREE_TYPE (method));
8785 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8786 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8787 cat_name = ((TREE_CODE (objc_implementation_context)
8788 == CLASS_IMPLEMENTATION_TYPE)
8789 ? NULL
8790 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8791 method_slot++;
8793 /* Make sure this is big enough for any plausible method label. */
8794 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8795 + (cat_name ? strlen (cat_name) : 0));
8797 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8798 class_name, cat_name, sel_name, method_slot);
8800 method_id = get_identifier (buf);
8802 #ifdef OBJCPLUS
8803 /* Objective-C methods cannot be overloaded, so we don't need
8804 the type encoding appended. It looks bad anyway... */
8805 push_lang_context (lang_name_c);
8806 #endif
8808 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8809 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8811 /* Set self_decl from the first argument. */
8812 self_decl = DECL_ARGUMENTS (current_function_decl);
8814 /* Suppress unused warnings. */
8815 TREE_USED (self_decl) = 1;
8816 DECL_READ_P (self_decl) = 1;
8817 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8818 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8819 #ifdef OBJCPLUS
8820 pop_lang_context ();
8821 #endif
8823 METHOD_DEFINITION (method) = current_function_decl;
8825 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8827 if (implementation_template != objc_implementation_context)
8829 tree proto
8830 = lookup_method_static (implementation_template,
8831 METHOD_SEL_NAME (method),
8832 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8833 | OBJC_LOOKUP_NO_SUPER));
8835 if (proto)
8837 if (!comp_proto_with_proto (method, proto, 1))
8839 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8841 warning_at (DECL_SOURCE_LOCATION (method), 0,
8842 "conflicting types for %<%c%s%>",
8843 (type ? '-' : '+'),
8844 identifier_to_locale (gen_method_decl (method)));
8845 inform (DECL_SOURCE_LOCATION (proto),
8846 "previous declaration of %<%c%s%>",
8847 (type ? '-' : '+'),
8848 identifier_to_locale (gen_method_decl (proto)));
8850 else
8852 /* If the method in the @interface was deprecated, mark
8853 the implemented method as deprecated too. It should
8854 never be used for messaging (when the deprecation
8855 warnings are produced), but just in case. */
8856 if (TREE_DEPRECATED (proto))
8857 TREE_DEPRECATED (method) = 1;
8859 /* If the method in the @interface was marked as
8860 'noreturn', mark the function implementing the method
8861 as 'noreturn' too. */
8862 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8865 else
8867 /* We have a method @implementation even though we did not
8868 see a corresponding @interface declaration (which is allowed
8869 by Objective-C rules). Go ahead and place the method in
8870 the @interface anyway, so that message dispatch lookups
8871 will see it. */
8872 tree interface = implementation_template;
8874 if (TREE_CODE (objc_implementation_context)
8875 == CATEGORY_IMPLEMENTATION_TYPE)
8876 interface = lookup_category
8877 (interface,
8878 CLASS_SUPER_NAME (objc_implementation_context));
8880 if (interface)
8881 objc_add_method (interface, copy_node (method),
8882 TREE_CODE (method) == CLASS_METHOD_DECL,
8883 /* is_optional= */ false);
8888 static void *UOBJC_SUPER_scope = 0;
8890 /* _n_Method (id self, SEL sel, ...)
8892 struct objc_super _S;
8893 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8894 } */
8896 static tree
8897 get_super_receiver (void)
8899 if (objc_method_context)
8901 tree super_expr, super_expr_list, class_expr;
8902 bool inst_meth;
8903 if (!UOBJC_SUPER_decl)
8905 UOBJC_SUPER_decl = build_decl (input_location,
8906 VAR_DECL, get_identifier (TAG_SUPER),
8907 objc_super_template);
8908 /* This prevents `unused variable' warnings when compiling with -Wall. */
8909 TREE_USED (UOBJC_SUPER_decl) = 1;
8910 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8911 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8912 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8913 NULL_TREE);
8914 UOBJC_SUPER_scope = objc_get_current_scope ();
8917 /* Set receiver to self. */
8918 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8919 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8920 NOP_EXPR, input_location, self_decl,
8921 NULL_TREE);
8922 super_expr_list = super_expr;
8924 /* Set class to begin searching. */
8925 /* Get the ident for the superclass class field & build a ref to it.
8926 ??? maybe we should just name the field the same for all runtimes. */
8927 super_expr = (*runtime.super_superclassfield_ident) ();
8928 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8930 gcc_assert (imp_list->imp_context == objc_implementation_context
8931 && imp_list->imp_template == implementation_template);
8932 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8934 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8935 class_expr = (*runtime.get_class_super_ref) (input_location,
8936 imp_list, inst_meth);
8937 else
8938 /* We have a category. */
8940 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8941 tree super_class;
8943 /* Barf if super used in a category of a root object. */
8944 if (!super_name)
8946 error ("no super class declared in interface for %qE",
8947 CLASS_NAME (imp_list->imp_template));
8948 return error_mark_node;
8951 super_class = (*runtime.get_category_super_ref) (input_location,
8952 imp_list, inst_meth);
8953 class_expr = build_c_cast (input_location,
8954 TREE_TYPE (super_expr), super_class);
8957 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8958 NOP_EXPR,
8959 input_location, class_expr, NULL_TREE);
8961 super_expr_list = build_compound_expr (input_location,
8962 super_expr_list, super_expr);
8964 super_expr = build_unary_op (input_location,
8965 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8966 super_expr_list = build_compound_expr (input_location,
8967 super_expr_list, super_expr);
8969 return super_expr_list;
8971 else
8973 error ("%<[super ...]%> must appear in a method context");
8974 return error_mark_node;
8978 /* When exiting a scope, sever links to a 'super' declaration (if any)
8979 therein contained. */
8981 void
8982 objc_clear_super_receiver (void)
8984 if (objc_method_context
8985 && UOBJC_SUPER_scope == objc_get_current_scope ())
8987 UOBJC_SUPER_decl = 0;
8988 UOBJC_SUPER_scope = 0;
8992 void
8993 objc_finish_method_definition (tree fndecl)
8995 /* We cannot validly inline ObjC methods, at least not without a language
8996 extension to declare that a method need not be dynamically
8997 dispatched, so suppress all thoughts of doing so. */
8998 DECL_UNINLINABLE (fndecl) = 1;
9000 #ifndef OBJCPLUS
9001 /* The C++ front-end will have called finish_function() for us. */
9002 finish_function ();
9003 #endif
9005 METHOD_ENCODING (objc_method_context)
9006 = encode_method_prototype (objc_method_context);
9008 /* Required to implement _msgSuper. This must be done AFTER finish_function,
9009 since the optimizer may find "may be used before set" errors. */
9010 objc_method_context = NULL_TREE;
9012 if (should_call_super_dealloc)
9013 warning (0, "method possibly missing a [super dealloc] call");
9016 /* Given a tree DECL node, produce a printable description of it in the given
9017 buffer, overwriting the buffer. */
9019 static char *
9020 gen_declaration (tree decl)
9022 errbuf[0] = '\0';
9024 if (DECL_P (decl))
9026 gen_type_name_0 (TREE_TYPE (decl));
9028 if (DECL_NAME (decl))
9030 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
9031 strcat (errbuf, " ");
9033 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
9036 #ifdef OBJCPLUS
9037 tree w = DECL_BIT_FIELD_REPRESENTATIVE (decl);
9038 #else
9039 tree w = DECL_INITIAL (decl);
9040 #endif
9041 if (w)
9043 STRIP_ANY_LOCATION_WRAPPER (w);
9044 if (TREE_CODE (w) == INTEGER_CST)
9045 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
9046 TREE_INT_CST_LOW (w));
9050 return errbuf;
9053 /* Given a tree TYPE node, produce a printable description of it in the given
9054 buffer, overwriting the buffer. */
9056 static char *
9057 gen_type_name_0 (tree type)
9059 tree orig = type, proto;
9061 if (TYPE_P (type) && TYPE_NAME (type))
9062 type = TYPE_NAME (type);
9063 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
9065 tree inner = TREE_TYPE (type);
9067 while (TREE_CODE (inner) == ARRAY_TYPE)
9068 inner = TREE_TYPE (inner);
9070 gen_type_name_0 (inner);
9072 if (!POINTER_TYPE_P (inner))
9073 strcat (errbuf, " ");
9075 if (POINTER_TYPE_P (type))
9076 strcat (errbuf, "*");
9077 else
9078 while (type != inner)
9080 strcat (errbuf, "[");
9082 if (TYPE_DOMAIN (type))
9084 char sz[20];
9086 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
9087 (TREE_INT_CST_LOW
9088 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
9089 strcat (errbuf, sz);
9092 strcat (errbuf, "]");
9093 type = TREE_TYPE (type);
9096 goto exit_function;
9099 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
9100 type = DECL_NAME (type);
9102 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
9103 ? IDENTIFIER_POINTER (type)
9104 : "");
9106 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
9107 if (objc_is_id (orig))
9108 orig = TREE_TYPE (orig);
9110 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
9112 if (proto)
9114 strcat (errbuf, " <");
9116 while (proto) {
9117 strcat (errbuf,
9118 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
9119 proto = TREE_CHAIN (proto);
9120 strcat (errbuf, proto ? ", " : ">");
9124 exit_function:
9125 return errbuf;
9128 static char *
9129 gen_type_name (tree type)
9131 errbuf[0] = '\0';
9133 return gen_type_name_0 (type);
9136 /* Given a method tree, put a printable description into the given
9137 buffer (overwriting) and return a pointer to the buffer. */
9139 static char *
9140 gen_method_decl (tree method)
9142 tree chain;
9144 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9145 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9146 strcat (errbuf, ")");
9147 chain = METHOD_SEL_ARGS (method);
9149 if (chain)
9151 /* We have a chain of keyword_decls. */
9154 if (KEYWORD_KEY_NAME (chain))
9155 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9157 strcat (errbuf, ":(");
9158 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9159 strcat (errbuf, ")");
9161 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9162 if ((chain = DECL_CHAIN (chain)))
9163 strcat (errbuf, " ");
9165 while (chain);
9167 if (METHOD_ADD_ARGS (method))
9169 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9171 /* Know we have a chain of parm_decls. */
9172 while (chain)
9174 strcat (errbuf, ", ");
9175 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9176 chain = TREE_CHAIN (chain);
9179 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9180 strcat (errbuf, ", ...");
9184 else
9185 /* We have a unary selector. */
9186 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9188 return errbuf;
9191 /* Debug info. */
9194 /* Dump an @interface declaration of the supplied class CHAIN to the
9195 supplied file FP. Used to implement the -gen-decls option (which
9196 prints out an @interface declaration of all classes compiled in
9197 this run); potentially useful for debugging the compiler too. */
9198 void
9199 dump_interface (FILE *fp, tree chain)
9201 /* FIXME: A heap overflow here whenever a method (or ivar)
9202 declaration is so long that it doesn't fit in the buffer. The
9203 code and all the related functions should be rewritten to avoid
9204 using fixed size buffers. */
9205 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9206 tree ivar_decls = CLASS_RAW_IVARS (chain);
9207 tree nst_methods = CLASS_NST_METHODS (chain);
9208 tree cls_methods = CLASS_CLS_METHODS (chain);
9210 fprintf (fp, "\n@interface %s", my_name);
9212 /* CLASS_SUPER_NAME is used to store the superclass name for
9213 classes, and the category name for categories. */
9214 if (CLASS_SUPER_NAME (chain))
9216 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9218 switch (TREE_CODE (chain))
9220 case CATEGORY_IMPLEMENTATION_TYPE:
9221 case CATEGORY_INTERFACE_TYPE:
9222 fprintf (fp, " (%s)\n", name);
9223 break;
9224 default:
9225 fprintf (fp, " : %s\n", name);
9226 break;
9229 else
9230 fprintf (fp, "\n");
9232 /* FIXME - the following doesn't seem to work at the moment. */
9233 if (ivar_decls)
9235 fprintf (fp, "{\n");
9238 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9239 ivar_decls = TREE_CHAIN (ivar_decls);
9241 while (ivar_decls);
9242 fprintf (fp, "}\n");
9245 while (nst_methods)
9247 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9248 nst_methods = TREE_CHAIN (nst_methods);
9251 while (cls_methods)
9253 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9254 cls_methods = TREE_CHAIN (cls_methods);
9257 fprintf (fp, "@end\n");
9260 #if 0
9261 /* Produce the pretty printing for an Objective-C method. This is
9262 currently unused, but could be handy while reorganizing the pretty
9263 printing to be more robust. */
9264 static const char *
9265 objc_pretty_print_method (bool is_class_method,
9266 const char *class_name,
9267 const char *category_name,
9268 const char *selector)
9270 if (category_name)
9272 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9273 + strlen (selector) + 7);
9275 if (is_class_method)
9276 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9277 else
9278 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9280 return result;
9282 else
9284 char *result = XNEWVEC (char, strlen (class_name)
9285 + strlen (selector) + 5);
9287 if (is_class_method)
9288 sprintf (result, "+[%s %s]", class_name, selector);
9289 else
9290 sprintf (result, "-[%s %s]", class_name, selector);
9292 return result;
9295 #endif
9297 /* Demangle function for Objective-C. Attempt to demangle the
9298 function name associated with a method (eg, going from
9299 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9300 purpose of pretty printing or error messages. Return the demangled
9301 name, or NULL if the string is not an Objective-C mangled method
9302 name.
9304 Because of how the mangling is done, any method that has a '_' in
9305 its original name is at risk of being demangled incorrectly. In
9306 some cases there are multiple valid ways to demangle a method name
9307 and there is no way we can decide.
9309 TODO: objc_demangle() can't always get it right; the right way to
9310 get this correct for all method names would be to store the
9311 Objective-C method name somewhere in the function decl. Then,
9312 there is no demangling to do; we'd just pull the method name out of
9313 the decl. As an additional bonus, when printing error messages we
9314 could check for such a method name, and if we find it, we know the
9315 function is actually an Objective-C method and we could print error
9316 messages saying "In method '+[NSObject class]" instead of "In
9317 function '+[NSObject class]" as we do now. */
9318 static const char *
9319 objc_demangle (const char *mangled)
9321 char *demangled, *cp;
9323 /* First of all, if the name is too short it can't be an Objective-C
9324 mangled method name. */
9325 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9326 return NULL;
9328 /* If the name looks like an already demangled one, return it
9329 unchanged. This should only happen on Darwin, where method names
9330 are mangled differently into a pretty-print form (such as
9331 '+[NSObject class]', see darwin.h). In that case, demangling is
9332 a no-op, but we need to return the demangled name if it was an
9333 ObjC one, and return NULL if not. We should be safe as no C/C++
9334 function can start with "-[" or "+[". */
9335 if ((mangled[0] == '-' || mangled[0] == '+')
9336 && (mangled[1] == '['))
9337 return mangled;
9339 if (mangled[0] == '_' &&
9340 (mangled[1] == 'i' || mangled[1] == 'c') &&
9341 mangled[2] == '_')
9343 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9344 if (mangled[1] == 'i')
9345 *cp++ = '-'; /* for instance method */
9346 else
9347 *cp++ = '+'; /* for class method */
9348 *cp++ = '['; /* opening left brace */
9349 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9350 while (*cp && *cp == '_')
9351 cp++; /* skip any initial underbars in class name */
9352 cp = strchr(cp, '_'); /* find first non-initial underbar */
9353 if (cp == NULL)
9355 free(demangled); /* not mangled name */
9356 return NULL;
9358 if (cp[1] == '_') /* easy case: no category name */
9360 *cp++ = ' '; /* replace two '_' with one ' ' */
9361 strcpy(cp, mangled + (cp - demangled) + 2);
9363 else
9365 *cp++ = '('; /* less easy case: category name */
9366 cp = strchr(cp, '_');
9367 if (cp == 0)
9369 free(demangled); /* not mangled name */
9370 return NULL;
9372 *cp++ = ')';
9373 *cp++ = ' '; /* overwriting 1st char of method name... */
9374 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9376 /* Now we have the method name. We need to generally replace
9377 '_' with ':' but trying to preserve '_' if it could only have
9378 been in the mangled string because it was already in the
9379 original name. In cases where it's ambiguous, we assume that
9380 any '_' originated from a ':'. */
9382 /* Initial '_'s in method name can't have been generating by
9383 converting ':'s. Skip them. */
9384 while (*cp && *cp == '_')
9385 cp++;
9387 /* If the method name does not end with '_', then it has no
9388 arguments and there was no replacement of ':'s with '_'s
9389 during mangling. Check for that case, and skip any
9390 replacement if so. This at least guarantees that methods
9391 with no arguments are always demangled correctly (unless the
9392 original name ends with '_'). */
9393 if (*(mangled + strlen (mangled) - 1) != '_')
9395 /* Skip to the end. */
9396 for (; *cp; cp++)
9399 else
9401 /* Replace remaining '_' with ':'. This may get it wrong if
9402 there were '_'s in the original name. In most cases it
9403 is impossible to disambiguate. */
9404 for (; *cp; cp++)
9405 if (*cp == '_')
9406 *cp = ':';
9408 *cp++ = ']'; /* closing right brace */
9409 *cp++ = 0; /* string terminator */
9410 return demangled;
9412 else
9413 return NULL; /* not an objc mangled name */
9416 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9417 specific decl, return the printable name for it. If not, return
9418 NULL. */
9419 const char *
9420 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9422 switch (TREE_CODE (decl))
9424 case FUNCTION_DECL:
9425 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9427 /* The following happens when we are printing a deprecation
9428 warning for a method. The warn_deprecation() will end up
9429 trying to print the decl for INSTANCE_METHOD_DECL or
9430 CLASS_METHOD_DECL. It would be nice to be able to print
9431 "-[NSObject autorelease] is deprecated", but to do that, we'd
9432 need to store the class and method name in the method decl,
9433 which we currently don't do. For now, just return the name
9434 of the method. We don't return NULL, because that may
9435 trigger further attempts to pretty-print the decl in C/C++,
9436 but they wouldn't know how to pretty-print it. */
9437 case INSTANCE_METHOD_DECL:
9438 case CLASS_METHOD_DECL:
9439 return IDENTIFIER_POINTER (DECL_NAME (decl));
9440 /* This happens when printing a deprecation warning for a
9441 property. We may want to consider some sort of pretty
9442 printing (eg, include the class name where it was declared
9443 ?). */
9444 case PROPERTY_DECL:
9445 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9446 default:
9447 return NULL;
9451 /* Return a printable name for 'decl'. This first tries
9452 objc_maybe_printable_name(), and if that fails, it returns the name
9453 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9454 Objective-C; in Objective-C++, setting the hook is not enough
9455 because lots of C++ Front-End code calls cxx_printable_name,
9456 dump_decl and other C++ functions directly. So instead we have
9457 modified dump_decl to call objc_maybe_printable_name directly. */
9458 const char *
9459 objc_printable_name (tree decl, int v)
9461 const char *demangled_name = objc_maybe_printable_name (decl, v);
9463 if (demangled_name != NULL)
9464 return demangled_name;
9465 else
9466 return IDENTIFIER_POINTER (DECL_NAME (decl));
9469 /* Routine is called to issue diagnostic when reference to a private
9470 ivar is made and no other variable with same name is found in
9471 current scope. */
9472 bool
9473 objc_diagnose_private_ivar (tree id)
9475 tree ivar;
9476 if (!objc_method_context)
9477 return false;
9478 ivar = is_ivar (objc_ivar_chain, id);
9479 if (ivar && is_private (ivar))
9481 error ("instance variable %qs is declared private",
9482 IDENTIFIER_POINTER (id));
9483 return true;
9485 return false;
9488 /* Look up ID as an instance variable. OTHER contains the result of
9489 the C or C++ lookup, which we may want to use instead. */
9490 /* To use properties inside an instance method, use self.property. */
9491 tree
9492 objc_lookup_ivar (tree other, tree id)
9494 tree ivar;
9496 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9497 if (!objc_method_context)
9498 return other;
9500 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9501 /* We have a message to super. */
9502 return get_super_receiver ();
9504 /* In a class method, look up an instance variable only as a last
9505 resort. */
9506 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9507 && other && other != error_mark_node)
9508 return other;
9510 /* Don't look up the ivar if the user has explicitly advised against
9511 it with -fno-local-ivars. */
9513 if (!flag_local_ivars)
9514 return other;
9516 /* Look up the ivar, but do not use it if it is not accessible. */
9517 ivar = is_ivar (objc_ivar_chain, id);
9519 if (!ivar || is_private (ivar))
9520 return other;
9522 /* In an instance method, a local variable (or parameter) may hide the
9523 instance variable. */
9524 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9525 && other && other != error_mark_node
9526 #ifdef OBJCPLUS
9527 && CP_DECL_CONTEXT (other) != global_namespace)
9528 #else
9529 && !DECL_FILE_SCOPE_P (other))
9530 #endif
9532 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9533 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9534 "local declaration of %qE hides instance variable", id);
9537 return other;
9540 /* At this point, we are either in an instance method with no obscuring
9541 local definitions, or in a class method with no alternate definitions
9542 at all. */
9543 return build_ivar_reference (id);
9546 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9547 needs to be done if we are calling a function through a cast. */
9549 tree
9550 objc_rewrite_function_call (tree function, tree first_param)
9552 if (TREE_CODE (function) == NOP_EXPR
9553 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9554 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9555 == FUNCTION_DECL)
9557 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9558 TREE_OPERAND (function, 0),
9559 first_param, size_zero_node);
9562 return function;
9565 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9566 corresponding 'getter' function call. Note that we assume the
9567 PROPERTY_REF to be valid since we generated it while parsing. */
9568 static void
9569 objc_gimplify_property_ref (tree *expr_p)
9571 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9572 tree call_exp;
9574 if (getter == NULL_TREE)
9576 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9577 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9578 should be impossible for real properties, which always
9579 have a getter. */
9580 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9581 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9582 /* Try to recover from the error to prevent an ICE. We take
9583 zero and cast it to the type of the property. */
9584 *expr_p = convert (TREE_TYPE (property_decl),
9585 integer_zero_node);
9586 return;
9589 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9591 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9592 that is deprecated. */
9593 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9594 NULL_TREE);
9597 call_exp = getter;
9598 #ifdef OBJCPLUS
9599 /* In C++, a getter which returns an aggregate value results in a
9600 target_expr which initializes a temporary to the call
9601 expression. */
9602 if (TREE_CODE (getter) == TARGET_EXPR)
9604 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9605 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9606 call_exp = TREE_OPERAND (getter, 1);
9608 #endif
9609 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9611 *expr_p = call_exp;
9614 /* This is called when "gimplifying" the trees. We need to gimplify
9615 the Objective-C/Objective-C++ specific trees, then hand over the
9616 process to C/C++. */
9618 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9620 enum tree_code code = TREE_CODE (*expr_p);
9621 switch (code)
9623 /* Look for the special case of OBJC_TYPE_REF with the address
9624 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9625 or one of its cousins). */
9626 case OBJ_TYPE_REF:
9627 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9628 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9629 == FUNCTION_DECL)
9631 enum gimplify_status r0, r1;
9633 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9634 value of the OBJ_TYPE_REF, so force them to be emitted
9635 during subexpression evaluation rather than after the
9636 OBJ_TYPE_REF. This permits objc_msgSend calls in
9637 Objective C to use direct rather than indirect calls when
9638 the object expression has a postincrement. */
9639 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9640 is_gimple_val, fb_rvalue);
9641 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9642 is_gimple_val, fb_rvalue);
9644 return MIN (r0, r1);
9646 break;
9647 case PROPERTY_REF:
9648 objc_gimplify_property_ref (expr_p);
9649 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9650 break;
9651 default:
9652 break;
9655 #ifdef OBJCPLUS
9656 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9657 #else
9658 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9659 #endif
9662 /* --- FAST ENUMERATION --- */
9663 /* Begin code generation for fast enumeration (foreach) ... */
9665 /* Defines
9667 struct __objcFastEnumerationState
9669 unsigned long state;
9670 id *itemsPtr;
9671 unsigned long *mutationsPtr;
9672 unsigned long extra[5];
9675 Confusingly enough, NSFastEnumeration is then defined by libraries
9676 to be the same structure.
9679 static void
9680 build_fast_enumeration_state_template (void)
9682 tree decls, *chain = NULL;
9684 /* { */
9685 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9686 (TAG_FAST_ENUMERATION_STATE));
9688 /* unsigned long state; */
9689 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9691 /* id *itemsPtr; */
9692 add_field_decl (build_pointer_type (objc_object_type),
9693 "itemsPtr", &chain);
9695 /* unsigned long *mutationsPtr; */
9696 add_field_decl (build_pointer_type (long_unsigned_type_node),
9697 "mutationsPtr", &chain);
9699 /* unsigned long extra[5]; */
9700 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9701 "extra", &chain);
9703 /* } */
9704 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9708 'objc_finish_foreach_loop()' generates the code for an Objective-C
9709 foreach loop. The 'location' argument is the location of the 'for'
9710 that starts the loop. The 'object_expression' is the expression of
9711 the 'object' that iterates; the 'collection_expression' is the
9712 expression of the collection that we iterate over (we need to make
9713 sure we evaluate this only once); the 'for_body' is the set of
9714 statements to be executed in each iteration; 'break_label' and
9715 'continue_label' are the break and continue labels which we need to
9716 emit since the <statements> may be jumping to 'break_label' (if they
9717 contain 'break') or to 'continue_label' (if they contain
9718 'continue').
9720 The syntax is
9722 for (<object expression> in <collection expression>)
9723 <statements>
9725 which is compiled into the following blurb:
9728 id __objc_foreach_collection;
9729 __objc_fast_enumeration_state __objc_foreach_enum_state;
9730 unsigned long __objc_foreach_batchsize;
9731 id __objc_foreach_items[16];
9732 __objc_foreach_collection = <collection expression>;
9733 __objc_foreach_enum_state = { 0 };
9734 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9736 if (__objc_foreach_batchsize == 0)
9737 <object expression> = nil;
9738 else
9740 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9741 next_batch:
9743 unsigned long __objc_foreach_index;
9744 __objc_foreach_index = 0;
9746 next_object:
9747 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9748 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9749 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9751 continue_label:
9752 __objc_foreach_index++;
9753 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9754 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9756 if (__objc_foreach_batchsize != 0) goto next_batch;
9757 <object expression> = nil;
9758 break_label:
9762 'statements' may contain a 'continue' or 'break' instruction, which
9763 the user expects to 'continue' or 'break' the entire foreach loop.
9764 We are provided the labels that 'break' and 'continue' jump to, so
9765 we place them where we want them to jump to when they pick them.
9767 Optimization TODO: we could cache the IMP of
9768 countByEnumeratingWithState:objects:count:.
9771 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9772 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9774 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9775 #include "tree-pretty-print.h"
9776 #endif
9778 void
9779 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9780 tree break_label, tree continue_label)
9782 /* A tree representing the __objcFastEnumerationState struct type,
9783 or NSFastEnumerationState struct, whatever we are using. */
9784 tree objc_fast_enumeration_state_type;
9786 /* The trees representing the declarations of each of the local variables. */
9787 tree objc_foreach_collection_decl;
9788 tree objc_foreach_enum_state_decl;
9789 tree objc_foreach_items_decl;
9790 tree objc_foreach_batchsize_decl;
9791 tree objc_foreach_mutations_pointer_decl;
9792 tree objc_foreach_index_decl;
9794 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9795 tree selector_name;
9797 /* A tree representing the local bind. */
9798 tree bind;
9800 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9801 tree first_if;
9803 /* A tree representing the 'else' part of 'first_if' */
9804 tree first_else;
9806 /* A tree representing the 'next_batch' label. */
9807 tree next_batch_label_decl;
9809 /* A tree representing the binding after the 'next_batch' label. */
9810 tree next_batch_bind;
9812 /* A tree representing the 'next_object' label. */
9813 tree next_object_label_decl;
9815 /* Temporary variables. */
9816 tree t;
9817 int i;
9819 if (flag_objc1_only)
9820 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9822 if (object_expression == error_mark_node)
9823 return;
9825 if (collection_expression == error_mark_node)
9826 return;
9828 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9830 error_at (location, "iterating variable in fast enumeration is not an object");
9831 return;
9834 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9836 error_at (location, "collection in fast enumeration is not an object");
9837 return;
9840 /* TODO: Check that object_expression is either a variable
9841 declaration, or an lvalue. */
9843 /* This kludge is an idea from apple. We use the
9844 __objcFastEnumerationState struct implicitly defined by the
9845 compiler, unless a NSFastEnumerationState struct has been defined
9846 (by a Foundation library such as GNUstep Base) in which case, we
9847 use that one.
9849 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9851 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9853 if (objc_NSFastEnumeration_type)
9855 /* TODO: We really need to check that
9856 objc_NSFastEnumeration_type is the same as ours! */
9857 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9859 /* If it's a typedef, use the original type. */
9860 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9861 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9862 else
9863 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9868 /* { */
9869 /* Done by c-parser.c. */
9871 /* type object; */
9872 /* Done by c-parser.c. */
9874 /* Disable warnings that 'object' is unused. For example the code
9876 for (id object in collection)
9877 i++;
9879 which can be used to count how many objects there are in the
9880 collection is fine and should generate no warnings even if
9881 'object' is technically unused. */
9882 TREE_USED (object_expression) = 1;
9883 if (DECL_P (object_expression))
9884 DECL_READ_P (object_expression) = 1;
9886 /* id __objc_foreach_collection */
9887 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9889 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9890 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9891 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9893 /* id __objc_foreach_items[16]; */
9894 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9895 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9897 /* unsigned long __objc_foreach_batchsize; */
9898 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9899 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9901 /* Generate the local variable binding. */
9902 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9903 SET_EXPR_LOCATION (bind, location);
9904 TREE_SIDE_EFFECTS (bind) = 1;
9906 /* __objc_foreach_collection = <collection expression>; */
9907 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9908 SET_EXPR_LOCATION (t, location);
9909 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9910 /* We have used 'collection_expression'. */
9911 mark_exp_read (collection_expression);
9913 /* __objc_foreach_enum_state.state = 0; */
9914 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9915 get_identifier ("state")),
9916 build_int_cst (long_unsigned_type_node, 0));
9917 SET_EXPR_LOCATION (t, location);
9918 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9920 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9921 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9922 get_identifier ("itemsPtr")),
9923 null_pointer_node);
9924 SET_EXPR_LOCATION (t, location);
9925 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9927 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9928 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9929 get_identifier ("mutationsPtr")),
9930 null_pointer_node);
9931 SET_EXPR_LOCATION (t, location);
9932 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9934 /* __objc_foreach_enum_state.extra[0] = 0; */
9935 /* __objc_foreach_enum_state.extra[1] = 0; */
9936 /* __objc_foreach_enum_state.extra[2] = 0; */
9937 /* __objc_foreach_enum_state.extra[3] = 0; */
9938 /* __objc_foreach_enum_state.extra[4] = 0; */
9939 for (i = 0; i < 5 ; i++)
9941 t = build2 (MODIFY_EXPR, void_type_node,
9942 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9943 get_identifier ("extra")),
9944 build_int_cst (NULL_TREE, i)),
9945 build_int_cst (long_unsigned_type_node, 0));
9946 SET_EXPR_LOCATION (t, location);
9947 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9950 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9951 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9952 #ifdef OBJCPLUS
9953 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9954 /* Parameters. */
9955 tree_cons /* &__objc_foreach_enum_state */
9956 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9957 tree_cons /* __objc_foreach_items */
9958 (NULL_TREE, objc_foreach_items_decl,
9959 tree_cons /* 16 */
9960 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9961 #else
9962 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9964 struct c_expr array;
9965 array.value = objc_foreach_items_decl;
9966 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9967 /* Parameters. */
9968 tree_cons /* &__objc_foreach_enum_state */
9969 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9970 tree_cons /* __objc_foreach_items */
9971 (NULL_TREE, default_function_array_conversion (location, array).value,
9972 tree_cons /* 16 */
9973 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9975 #endif
9976 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9977 convert (long_unsigned_type_node, t));
9978 SET_EXPR_LOCATION (t, location);
9979 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9981 /* if (__objc_foreach_batchsize == 0) */
9982 first_if = build3 (COND_EXPR, void_type_node,
9983 /* Condition. */
9984 c_fully_fold
9985 (c_common_truthvalue_conversion
9986 (location,
9987 build_binary_op (location,
9988 EQ_EXPR,
9989 objc_foreach_batchsize_decl,
9990 build_int_cst (long_unsigned_type_node, 0), 1)),
9991 false, NULL),
9992 /* Then block (we fill it in later). */
9993 NULL_TREE,
9994 /* Else block (we fill it in later). */
9995 NULL_TREE);
9996 SET_EXPR_LOCATION (first_if, location);
9997 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9999 /* then <object expression> = nil; */
10000 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10001 SET_EXPR_LOCATION (t, location);
10002 COND_EXPR_THEN (first_if) = t;
10004 /* Now we build the 'else' part of the if; once we finish building
10005 it, we attach it to first_if as the 'else' part. */
10007 /* else */
10008 /* { */
10010 /* unsigned long __objc_foreach_mutations_pointer; */
10011 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
10013 /* Generate the local variable binding. */
10014 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
10015 SET_EXPR_LOCATION (first_else, location);
10016 TREE_SIDE_EFFECTS (first_else) = 1;
10018 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
10019 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
10020 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
10021 get_identifier ("mutationsPtr")),
10022 RO_UNARY_STAR));
10023 SET_EXPR_LOCATION (t, location);
10024 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10026 /* next_batch: */
10027 next_batch_label_decl = create_artificial_label (location);
10028 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
10029 SET_EXPR_LOCATION (t, location);
10030 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10032 /* { */
10034 /* unsigned long __objc_foreach_index; */
10035 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
10037 /* Generate the local variable binding. */
10038 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
10039 SET_EXPR_LOCATION (next_batch_bind, location);
10040 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
10041 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
10043 /* __objc_foreach_index = 0; */
10044 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
10045 build_int_cst (long_unsigned_type_node, 0));
10046 SET_EXPR_LOCATION (t, location);
10047 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10049 /* next_object: */
10050 next_object_label_decl = create_artificial_label (location);
10051 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
10052 SET_EXPR_LOCATION (t, location);
10053 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10055 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
10056 t = build3 (COND_EXPR, void_type_node,
10057 /* Condition. */
10058 c_fully_fold
10059 (c_common_truthvalue_conversion
10060 (location,
10061 build_binary_op
10062 (location,
10063 NE_EXPR,
10064 objc_foreach_mutations_pointer_decl,
10065 build_indirect_ref (location,
10066 objc_build_component_ref (objc_foreach_enum_state_decl,
10067 get_identifier ("mutationsPtr")),
10068 RO_UNARY_STAR), 1)),
10069 false, NULL),
10070 /* Then block. */
10071 build_function_call (input_location,
10072 objc_enumeration_mutation_decl,
10073 tree_cons (NULL, collection_expression, NULL)),
10074 /* Else block. */
10075 NULL_TREE);
10076 SET_EXPR_LOCATION (t, location);
10077 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10079 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
10080 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
10081 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
10082 get_identifier ("itemsPtr")),
10083 objc_foreach_index_decl));
10084 SET_EXPR_LOCATION (t, location);
10085 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10087 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
10088 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
10090 /* continue_label: */
10091 if (continue_label)
10093 t = build1 (LABEL_EXPR, void_type_node, continue_label);
10094 SET_EXPR_LOCATION (t, location);
10095 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10098 /* __objc_foreach_index++; */
10099 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
10100 build_binary_op (location,
10101 PLUS_EXPR,
10102 objc_foreach_index_decl,
10103 build_int_cst (long_unsigned_type_node, 1), 1));
10104 SET_EXPR_LOCATION (t, location);
10105 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10107 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
10108 t = build3 (COND_EXPR, void_type_node,
10109 /* Condition. */
10110 c_fully_fold
10111 (c_common_truthvalue_conversion
10112 (location,
10113 build_binary_op (location,
10114 LT_EXPR,
10115 objc_foreach_index_decl,
10116 objc_foreach_batchsize_decl, 1)),
10117 false, NULL),
10118 /* Then block. */
10119 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
10120 /* Else block. */
10121 NULL_TREE);
10122 SET_EXPR_LOCATION (t, location);
10123 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10125 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
10126 #ifdef OBJCPLUS
10127 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10128 /* Parameters. */
10129 tree_cons /* &__objc_foreach_enum_state */
10130 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10131 tree_cons /* __objc_foreach_items */
10132 (NULL_TREE, objc_foreach_items_decl,
10133 tree_cons /* 16 */
10134 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10135 #else
10136 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10138 struct c_expr array;
10139 array.value = objc_foreach_items_decl;
10140 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10141 /* Parameters. */
10142 tree_cons /* &__objc_foreach_enum_state */
10143 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10144 tree_cons /* __objc_foreach_items */
10145 (NULL_TREE, default_function_array_conversion (location, array).value,
10146 tree_cons /* 16 */
10147 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10149 #endif
10150 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
10151 convert (long_unsigned_type_node, t));
10152 SET_EXPR_LOCATION (t, location);
10153 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10155 /* } */
10157 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10158 t = build3 (COND_EXPR, void_type_node,
10159 /* Condition. */
10160 c_fully_fold
10161 (c_common_truthvalue_conversion
10162 (location,
10163 build_binary_op (location,
10164 NE_EXPR,
10165 objc_foreach_batchsize_decl,
10166 build_int_cst (long_unsigned_type_node, 0), 1)),
10167 false, NULL),
10168 /* Then block. */
10169 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10170 /* Else block. */
10171 NULL_TREE);
10172 SET_EXPR_LOCATION (t, location);
10173 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10175 /* <object expression> = nil; */
10176 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10177 SET_EXPR_LOCATION (t, location);
10178 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10180 /* break_label: */
10181 if (break_label)
10183 t = build1 (LABEL_EXPR, void_type_node, break_label);
10184 SET_EXPR_LOCATION (t, location);
10185 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10188 /* } */
10189 COND_EXPR_ELSE (first_if) = first_else;
10191 /* Do the whole thing. */
10192 add_stmt (bind);
10194 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10195 /* This will print to stderr the whole blurb generated by the
10196 compiler while compiling (assuming the compiler doesn't crash
10197 before getting here).
10199 debug_generic_stmt (bind);
10200 #endif
10202 /* } */
10203 /* Done by c-parser.c */
10206 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10207 /* Return true if we have an NxString object pointer. */
10209 bool
10210 objc_string_ref_type_p (tree strp)
10212 tree tmv;
10213 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10214 return false;
10216 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10217 tmv = OBJC_TYPE_NAME (tmv);
10218 return (tmv
10219 && TREE_CODE (tmv) == IDENTIFIER_NODE
10220 && IDENTIFIER_POINTER (tmv)
10221 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10224 /* At present the behavior of this is undefined and it does nothing. */
10225 void
10226 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10227 tree ARG_UNUSED (args_list))
10231 void
10232 objc_common_init_ts (void)
10234 c_common_init_ts ();
10236 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10237 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10238 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10239 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10241 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10242 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10243 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10245 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10246 MARK_TS_TYPED (PROPERTY_REF);
10249 size_t
10250 objc_common_tree_size (enum tree_code code)
10252 switch (code)
10254 case CLASS_METHOD_DECL:
10255 case INSTANCE_METHOD_DECL:
10256 case KEYWORD_DECL:
10257 case PROPERTY_DECL: return sizeof (tree_decl_non_common);
10258 case CLASS_INTERFACE_TYPE:
10259 case CLASS_IMPLEMENTATION_TYPE:
10260 case CATEGORY_INTERFACE_TYPE:
10261 case CATEGORY_IMPLEMENTATION_TYPE:
10262 case PROTOCOL_INTERFACE_TYPE: return sizeof (tree_type_non_common);
10263 default:
10264 gcc_unreachable ();
10269 #include "gt-objc-objc-act.h"