PR middle-end/66633
[official-gcc.git] / gcc / objc / objc-act.c
blobf1b56e807669a4ed5e22581acde5ef4646c1810b
1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992-2015 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 "alias.h"
26 #include "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
34 #ifdef OBJCPLUS
35 #include "cp/cp-tree.h"
36 #else
37 #include "c/c-tree.h"
38 #include "c/c-lang.h"
39 #endif
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "c-family/c-pragma.h"
44 #include "c-family/c-format.h"
45 #include "flags.h"
46 #include "langhooks.h"
47 #include "objc-act.h"
48 #include "objc-map.h"
49 #include "hard-reg-set.h"
50 #include "function.h"
51 #include "toplev.h"
52 #include "debug.h"
53 #include "c-family/c-target.h"
54 #include "diagnostic-core.h"
55 #include "intl.h"
56 #include "cgraph.h"
57 #include "tree-iterator.h"
58 #include "langhooks-def.h"
59 /* Different initialization, code gen and meta data generation for each
60 runtime. */
61 #include "objc-runtime-hooks.h"
62 /* Routines used mainly by the runtimes. */
63 #include "objc-runtime-shared-support.h"
64 /* For default_tree_printer (). */
65 #include "tree-pretty-print.h"
67 /* For enum gimplify_status */
68 #include "gimple-expr.h"
69 #include "gimplify.h"
71 /* For encode_method_prototype(). */
72 #include "objc-encoding.h"
74 static unsigned int should_call_super_dealloc = 0;
76 /* When building Objective-C++, we are not linking against the C front-end
77 and so need to replicate the C tree-construction functions in some way. */
78 #ifdef OBJCPLUS
79 #define OBJCP_REMAP_FUNCTIONS
80 #include "objcp-decl.h"
81 #endif /* OBJCPLUS */
83 /* This is the default way of generating a method name. */
84 /* This has the problem that "test_method:argument:" and
85 "test:method_argument:" will generate the same name
86 ("_i_Test__test_method_argument_" for an instance method of the
87 class "Test"), so you can't have them both in the same class!
88 Moreover, the demangling (going from
89 "_i_Test__test_method_argument" back to the original name) is
90 undefined because there are two correct ways of demangling the
91 name. */
92 #ifndef OBJC_GEN_METHOD_LABEL
93 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
94 do { \
95 char *temp; \
96 sprintf ((BUF), "_%s_%s_%s_%s", \
97 ((IS_INST) ? "i" : "c"), \
98 (CLASS_NAME), \
99 ((CAT_NAME)? (CAT_NAME) : ""), \
100 (SEL_NAME)); \
101 for (temp = (BUF); *temp; temp++) \
102 if (*temp == ':') *temp = '_'; \
103 } while (0)
104 #endif
106 /* These need specifying. */
107 #ifndef OBJC_FORWARDING_STACK_OFFSET
108 #define OBJC_FORWARDING_STACK_OFFSET 0
109 #endif
111 #ifndef OBJC_FORWARDING_MIN_OFFSET
112 #define OBJC_FORWARDING_MIN_OFFSET 0
113 #endif
115 /*** Private Interface (procedures) ***/
117 /* Init stuff. */
118 static void synth_module_prologue (void);
120 /* Code generation. */
122 static tree start_class (enum tree_code, tree, tree, tree, tree);
123 static tree continue_class (tree);
124 static void finish_class (tree);
125 static void start_method_def (tree, tree);
127 static tree start_protocol (enum tree_code, tree, tree, tree);
128 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
129 static tree objc_add_method (tree, tree, int, bool);
130 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
131 static tree build_ivar_reference (tree);
132 static tree is_ivar (tree, tree);
134 /* We only need the following for ObjC; ObjC++ will use C++'s definition
135 of DERIVED_FROM_P. */
136 #ifndef OBJCPLUS
137 static bool objc_derived_from_p (tree, tree);
138 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
139 #endif
141 /* Property. */
142 static void objc_gen_property_data (tree, tree);
143 static void objc_synthesize_getter (tree, tree, tree);
144 static void objc_synthesize_setter (tree, tree, tree);
145 static tree lookup_property (tree, tree);
146 static tree lookup_property_in_list (tree, tree);
147 static tree lookup_property_in_protocol_list (tree, tree);
148 static void build_common_objc_property_accessor_helpers (void);
150 static void objc_xref_basetypes (tree, tree);
152 static tree get_class_ivars (tree, bool);
154 static void build_fast_enumeration_state_template (void);
156 #ifdef OBJCPLUS
157 static void objc_generate_cxx_cdtors (void);
158 #endif
160 /* objc attribute */
161 static void objc_decl_method_attributes (tree*, tree, int);
162 static tree build_keyword_selector (tree);
164 static void hash_init (void);
166 /* Hash tables to manage the global pool of method prototypes. Each
167 of these maps map a method name (selector) identifier to either a
168 single tree (for methods with a single method prototype) or a
169 TREE_VEC (for methods with multiple method prototypes). */
170 static GTY(()) objc_map_t instance_method_map = 0;
171 static GTY(()) objc_map_t class_method_map = 0;
173 /* Hash tables to manage the global pool of class names. */
175 static GTY(()) objc_map_t class_name_map = 0;
176 static GTY(()) objc_map_t alias_name_map = 0;
178 static tree lookup_method (tree, tree);
179 static tree lookup_method_static (tree, tree, int);
181 static void interface_hash_init (void);
182 static tree add_interface (tree, tree);
183 static void add_category (tree, tree);
184 static inline tree lookup_category (tree, tree);
186 /* Protocols. */
188 static tree lookup_protocol (tree, bool, bool);
189 static tree lookup_and_install_protocols (tree, bool);
191 #ifdef OBJCPLUS
192 static void really_start_method (tree, tree);
193 #else
194 static void really_start_method (tree, struct c_arg_info *);
195 #endif
196 static int comp_proto_with_proto (tree, tree, int);
197 static tree objc_decay_parm_type (tree);
199 /* Utilities for debugging and error diagnostics. */
201 static char *gen_type_name (tree);
202 static char *gen_type_name_0 (tree);
203 static char *gen_method_decl (tree);
204 static char *gen_declaration (tree);
206 /* Everything else. */
208 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
210 static void mark_referenced_methods (void);
211 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
212 static tree check_duplicates (tree, int, int);
214 /*** Private Interface (data) ***/
215 /* Flags for lookup_method_static(). */
217 /* Look for class methods. */
218 #define OBJC_LOOKUP_CLASS 1
219 /* Do not examine superclasses. */
220 #define OBJC_LOOKUP_NO_SUPER 2
221 /* Disable returning an instance method of a root class when a class
222 method can't be found. */
223 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
225 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
226 tree objc_global_trees[OCTI_MAX];
228 struct imp_entry *imp_list = 0;
229 int imp_count = 0; /* `@implementation' */
230 int cat_count = 0; /* `@category' */
232 objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
234 /* Use to generate method labels. */
235 static int method_slot = 0;
237 /* Flag to say whether methods in a protocol are optional or
238 required. */
239 static bool objc_method_optional_flag = false;
241 static int objc_collecting_ivars = 0;
243 /* Flag that is set to 'true' while we are processing a class
244 extension. Since a class extension just "reopens" the main
245 @interface, this can be used to determine if we are in the main
246 @interface, or in a class extension. */
247 static bool objc_in_class_extension = false;
249 static char *errbuf; /* Buffer for error diagnostics */
251 /* An array of all the local variables in the current function that
252 need to be marked as volatile. */
253 vec<tree, va_gc> *local_variables_to_volatilize = NULL;
255 /* Store all constructed constant strings in a hash table so that
256 they get uniqued properly. */
258 struct GTY((for_user)) string_descriptor {
259 /* The literal argument . */
260 tree literal;
262 /* The resulting constant string. */
263 tree constructor;
266 struct objc_string_hasher : ggc_ptr_hash<string_descriptor>
268 static hashval_t hash (string_descriptor *);
269 static bool equal (string_descriptor *, string_descriptor *);
272 static GTY(()) hash_table<objc_string_hasher> *string_htab;
274 FILE *gen_declaration_file;
276 /* Hooks for stuff that differs between runtimes. */
277 objc_runtime_hooks runtime;
279 /* Create a temporary variable of type 'type'. If 'name' is set, uses
280 the specified name, else use no name. Returns the declaration of
281 the type. The 'name' is mostly useful for debugging.
283 tree
284 objc_create_temporary_var (tree type, const char *name)
286 tree decl;
288 if (name != NULL)
290 decl = build_decl (input_location,
291 VAR_DECL, get_identifier (name), type);
293 else
295 decl = build_decl (input_location,
296 VAR_DECL, NULL_TREE, type);
298 TREE_USED (decl) = 1;
299 DECL_ARTIFICIAL (decl) = 1;
300 DECL_IGNORED_P (decl) = 1;
301 DECL_CONTEXT (decl) = current_function_decl;
303 return decl;
306 /* Some platforms pass small structures through registers versus
307 through an invisible pointer. Determine at what size structure is
308 the transition point between the two possibilities. */
310 static void
311 generate_struct_by_value_array (void)
313 tree type;
314 tree decls;
315 int i, j;
316 int aggregate_in_mem[32];
317 int found = 0;
319 /* Presumably no platform passes 32 byte structures in a register. */
320 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
321 in registers. */
322 for (i = 1; i < 32; i++)
324 char buffer[5];
325 tree *chain = NULL;
327 /* Create an unnamed struct that has `i' character components */
328 type = objc_start_struct (NULL_TREE);
330 strcpy (buffer, "c1");
331 decls = add_field_decl (char_type_node, buffer, &chain);
333 for (j = 1; j < i; j++)
335 sprintf (buffer, "c%d", j + 1);
336 add_field_decl (char_type_node, buffer, &chain);
338 objc_finish_struct (type, decls);
340 aggregate_in_mem[i] = aggregate_value_p (type, 0);
341 if (!aggregate_in_mem[i])
342 found = 1;
345 /* We found some structures that are returned in registers instead of memory
346 so output the necessary data. */
347 if (found)
349 for (i = 31; i >= 0; i--)
350 if (!aggregate_in_mem[i])
351 break;
352 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
355 exit (0);
358 bool
359 objc_init (void)
361 bool ok;
362 #ifdef OBJCPLUS
363 if (cxx_init () == false)
364 #else
365 if (c_objc_common_init () == false)
366 #endif
367 return false;
369 /* print_struct_values is triggered by -print-runtime-info (used
370 when building libobjc, with an empty file as input). It does not
371 require any ObjC setup, and it never returns.
373 -fcompare-debug is used to check the compiler output; we are
374 executed twice, once with flag_compare_debug set, and once with
375 it not set. If the flag is used together with
376 -print-runtime-info, we want to print the runtime info only once,
377 else it would be output in duplicate. So we check
378 flag_compare_debug to output it in only one of the invocations.
380 As a side effect, this also that means -fcompare-debug
381 -print-runtime-info will run the compiler twice, and compare the
382 generated assembler file; the first time the compiler exits
383 immediately (producing no file), and the second time it compiles
384 an empty file. This checks, as a side effect, that compiling an
385 empty file produces no assembler output. */
386 if (print_struct_values && !flag_compare_debug)
387 generate_struct_by_value_array ();
389 /* Set up stuff used by FE parser and all runtimes. */
390 errbuf = XNEWVEC (char, 1024 * 10);
391 interface_hash_init ();
392 hash_init ();
393 objc_encoding_init ();
394 /* ... and then check flags and set-up for the selected runtime ... */
395 if (flag_next_runtime && flag_objc_abi >= 2)
396 ok = objc_next_runtime_abi_02_init (&runtime);
397 else if (flag_next_runtime)
398 ok = objc_next_runtime_abi_01_init (&runtime);
399 else
400 ok = objc_gnu_runtime_abi_01_init (&runtime);
402 /* If that part of the setup failed - bail out immediately. */
403 if (!ok)
404 return false;
406 /* Determine the default visibility for instance variables. */
407 switch (default_ivar_visibility)
409 case IVAR_VISIBILITY_PRIVATE:
410 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
411 break;
412 case IVAR_VISIBILITY_PUBLIC:
413 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
414 break;
415 case IVAR_VISIBILITY_PACKAGE:
416 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
417 break;
418 default:
419 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
422 /* Generate general types and push runtime-specific decls to file scope. */
423 synth_module_prologue ();
425 return true;
428 /* This is called at the end of parsing by the C/C++ parsers. */
429 void
430 objc_write_global_declarations (void)
432 mark_referenced_methods ();
434 /* A missing @end might not be detected by the parser. */
435 if (objc_implementation_context)
437 warning (0, "%<@end%> missing in implementation context");
438 finish_class (objc_implementation_context);
439 objc_ivar_chain = NULL_TREE;
440 objc_implementation_context = NULL_TREE;
443 if (warn_selector)
445 objc_map_iterator_t i;
447 objc_map_iterator_initialize (class_method_map, &i);
448 while (objc_map_iterator_move_to_next (class_method_map, &i))
449 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
451 objc_map_iterator_initialize (instance_method_map, &i);
452 while (objc_map_iterator_move_to_next (instance_method_map, &i))
453 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
456 /* TODO: consider an early exit here if either errorcount or sorrycount
457 is non-zero. Not only is it wasting time to generate the metadata,
458 it needlessly imposes need to re-check for things that are already
459 determined to be errors. */
461 /* Finalize Objective-C runtime data. No need to generate tables
462 and code if only checking syntax, or if generating a PCH file. */
463 if (!flag_syntax_only && !pch_file)
465 location_t saved_location;
467 /* If gen_declaration desired, open the output file. */
468 if (flag_gen_declaration)
470 char * const dumpname = concat (dump_base_name, ".decl", NULL);
471 gen_declaration_file = fopen (dumpname, "w");
472 if (gen_declaration_file == 0)
473 fatal_error (input_location, "can%'t open %s: %m", dumpname);
474 free (dumpname);
477 /* Set the input location to BUILTINS_LOCATION. This is good
478 for error messages, in case any is generated while producing
479 the metadata, but it also silences warnings that would be
480 produced when compiling with -Wpadded in case when padding is
481 automatically added to the built-in runtime data structure
482 declarations. We know about this padding, and it is fine; we
483 don't want users to see any warnings about it if they use
484 -Wpadded. */
485 saved_location = input_location;
486 input_location = BUILTINS_LOCATION;
488 /* Compute and emit the meta-data tables for this runtime. */
489 (*runtime.generate_metadata) ();
491 /* Restore the original location, just in case it mattered. */
492 input_location = saved_location;
494 /* ... and then close any declaration file we opened. */
495 if (gen_declaration_file)
496 fclose (gen_declaration_file);
500 /* Return the first occurrence of a method declaration corresponding
501 to sel_name in rproto_list. Search rproto_list recursively.
502 If is_class is 0, search for instance methods, otherwise for class
503 methods. */
504 static tree
505 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
506 int is_class)
508 tree rproto, p, m;
510 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
512 p = TREE_VALUE (rproto);
513 m = NULL_TREE;
515 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
517 /* First, search the @required protocol methods. */
518 if (is_class)
519 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
520 else
521 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
523 if (m)
524 return m;
526 /* If still not found, search the @optional protocol methods. */
527 if (is_class)
528 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
529 else
530 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
532 if (m)
533 return m;
535 /* If still not found, search the attached protocols. */
536 if (PROTOCOL_LIST (p))
537 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
538 sel_name, is_class);
539 if (m)
540 return m;
542 else
544 ; /* An identifier...if we could not find a protocol. */
548 return 0;
551 static tree
552 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
554 tree rproto, p;
556 /* Make sure the protocol is supported by the object on the rhs. */
557 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
559 tree fnd = 0;
560 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
562 p = TREE_VALUE (rproto);
564 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
566 if (lproto == p)
567 fnd = lproto;
569 else if (PROTOCOL_LIST (p))
570 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
573 if (fnd)
574 return fnd;
577 else
579 ; /* An identifier...if we could not find a protocol. */
582 return 0;
585 void
586 objc_start_class_interface (tree klass, tree super_class,
587 tree protos, tree attributes)
589 if (flag_objc1_only && attributes)
590 error_at (input_location, "class attributes are not available in Objective-C 1.0");
592 objc_interface_context
593 = objc_ivar_context
594 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
595 objc_ivar_visibility = objc_default_ivar_visibility;
598 void
599 objc_start_category_interface (tree klass, tree categ,
600 tree protos, tree attributes)
602 if (attributes)
604 if (flag_objc1_only)
605 error_at (input_location, "category attributes are not available in Objective-C 1.0");
606 else
607 warning_at (input_location, OPT_Wattributes,
608 "category attributes are not available in this version"
609 " of the compiler, (ignored)");
611 if (categ == NULL_TREE)
613 if (flag_objc1_only)
614 error_at (input_location, "class extensions are not available in Objective-C 1.0");
615 else
617 /* Iterate over all the classes and categories implemented
618 up to now in this compilation unit. */
619 struct imp_entry *t;
621 for (t = imp_list; t; t = t->next)
623 /* If we find a class @implementation with the same name
624 as the one we are extending, produce an error. */
625 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
626 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
627 error_at (input_location,
628 "class extension for class %qE declared after its %<@implementation%>",
629 klass);
633 objc_interface_context
634 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
635 objc_ivar_chain
636 = continue_class (objc_interface_context);
639 void
640 objc_start_protocol (tree name, tree protos, tree attributes)
642 if (flag_objc1_only && attributes)
643 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
645 objc_interface_context
646 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
647 objc_method_optional_flag = false;
650 void
651 objc_continue_interface (void)
653 objc_ivar_chain
654 = continue_class (objc_interface_context);
657 void
658 objc_finish_interface (void)
660 finish_class (objc_interface_context);
661 objc_interface_context = NULL_TREE;
662 objc_method_optional_flag = false;
663 objc_in_class_extension = false;
666 void
667 objc_start_class_implementation (tree klass, tree super_class)
669 objc_implementation_context
670 = objc_ivar_context
671 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
672 NULL_TREE);
673 objc_ivar_visibility = objc_default_ivar_visibility;
676 void
677 objc_start_category_implementation (tree klass, tree categ)
679 objc_implementation_context
680 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
681 NULL_TREE);
682 objc_ivar_chain
683 = continue_class (objc_implementation_context);
686 void
687 objc_continue_implementation (void)
689 objc_ivar_chain
690 = continue_class (objc_implementation_context);
693 void
694 objc_finish_implementation (void)
696 #ifdef OBJCPLUS
697 if (flag_objc_call_cxx_cdtors)
698 objc_generate_cxx_cdtors ();
699 #endif
701 if (objc_implementation_context)
703 finish_class (objc_implementation_context);
704 objc_ivar_chain = NULL_TREE;
705 objc_implementation_context = NULL_TREE;
707 else
708 warning (0, "%<@end%> must appear in an @implementation context");
711 void
712 objc_set_visibility (objc_ivar_visibility_kind visibility)
714 if (visibility == OBJC_IVAR_VIS_PACKAGE)
716 if (flag_objc1_only)
717 error ("%<@package%> is not available in Objective-C 1.0");
718 else
719 warning (0, "%<@package%> presently has the same effect as %<@public%>");
721 objc_ivar_visibility = visibility;
724 void
725 objc_set_method_opt (bool optional)
727 if (flag_objc1_only)
729 if (optional)
730 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
731 else
732 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
735 objc_method_optional_flag = optional;
736 if (!objc_interface_context
737 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
739 if (optional)
740 error ("%<@optional%> is allowed in @protocol context only");
741 else
742 error ("%<@required%> is allowed in @protocol context only");
743 objc_method_optional_flag = false;
747 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
748 PROTOCOL. */
749 static tree
750 lookup_property_in_list (tree chain, tree property)
752 tree x;
753 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
754 if (PROPERTY_NAME (x) == property)
755 return x;
756 return NULL_TREE;
759 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
760 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
762 tree rproto, x;
763 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
765 tree p = TREE_VALUE (rproto);
766 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
768 if ((x = lookup_property_in_list (p, property)))
769 return x;
770 if (PROTOCOL_LIST (p))
771 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
773 else
775 ; /* An identifier...if we could not find a protocol. */
778 return NULL_TREE;
781 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
782 chain of interface hierarchy. */
783 static tree
784 lookup_property (tree interface_type, tree property)
786 tree inter = interface_type;
787 while (inter)
789 tree x, category;
790 if ((x = lookup_property_in_list (inter, property)))
791 return x;
792 /* Failing that, look for the property in each category of the class. */
793 category = inter;
794 while ((category = CLASS_CATEGORY_LIST (category)))
796 if ((x = lookup_property_in_list (category, property)))
797 return x;
799 /* When checking a category, also check the protocols
800 attached with the category itself. */
801 if (CLASS_PROTOCOL_LIST (category)
802 && (x = lookup_property_in_protocol_list
803 (CLASS_PROTOCOL_LIST (category), property)))
804 return x;
807 /* Failing to find in categories, look for property in protocol list. */
808 if (CLASS_PROTOCOL_LIST (inter)
809 && (x = lookup_property_in_protocol_list
810 (CLASS_PROTOCOL_LIST (inter), property)))
811 return x;
813 /* Failing that, climb up the inheritance hierarchy. */
814 inter = lookup_interface (CLASS_SUPER_NAME (inter));
816 return inter;
819 /* This routine is called by the parser when a
820 @property... declaration is found. 'decl' is the declaration of
821 the property (type/identifier), and the other arguments represent
822 property attributes that may have been specified in the Objective-C
823 declaration. 'parsed_property_readonly' is 'true' if the attribute
824 'readonly' was specified, and 'false' if not; similarly for the
825 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
826 if the attribute 'getter' was not specified, and is the identifier
827 corresponding to the specified getter if it was; similarly for
828 'parsed_property_setter_ident'. */
829 void
830 objc_add_property_declaration (location_t location, tree decl,
831 bool parsed_property_readonly, bool parsed_property_readwrite,
832 bool parsed_property_assign, bool parsed_property_retain,
833 bool parsed_property_copy, bool parsed_property_nonatomic,
834 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
836 tree property_decl;
837 tree x;
838 /* 'property_readonly' and 'property_assign_semantics' are the final
839 attributes of the property after all parsed attributes have been
840 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
841 parsed_property_readonly = false and parsed_property_readwrite =
842 false, then property_readonly will be false because the default
843 is readwrite). */
844 bool property_readonly = false;
845 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
846 bool property_extension_in_class_extension = false;
848 if (flag_objc1_only)
849 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
851 if (parsed_property_readonly && parsed_property_readwrite)
853 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
854 /* In case of conflicting attributes (here and below), after
855 producing an error, we pick one of the attributes and keep
856 going. */
857 property_readonly = false;
859 else
861 if (parsed_property_readonly)
862 property_readonly = true;
864 if (parsed_property_readwrite)
865 property_readonly = false;
868 if (parsed_property_readonly && parsed_property_setter_ident)
870 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
871 property_readonly = false;
874 if (parsed_property_assign && parsed_property_retain)
876 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
877 property_assign_semantics = OBJC_PROPERTY_RETAIN;
879 else if (parsed_property_assign && parsed_property_copy)
881 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
882 property_assign_semantics = OBJC_PROPERTY_COPY;
884 else if (parsed_property_retain && parsed_property_copy)
886 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
887 property_assign_semantics = OBJC_PROPERTY_COPY;
889 else
891 if (parsed_property_assign)
892 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
894 if (parsed_property_retain)
895 property_assign_semantics = OBJC_PROPERTY_RETAIN;
897 if (parsed_property_copy)
898 property_assign_semantics = OBJC_PROPERTY_COPY;
901 if (!objc_interface_context)
903 error_at (location, "property declaration not in @interface or @protocol context");
904 return;
907 /* At this point we know that we are either in an interface, a
908 category, or a protocol. */
910 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
911 something else, as that would confuse the checks below. */
912 if (TREE_CODE (decl) != FIELD_DECL)
914 error_at (location, "invalid property declaration");
915 return;
918 /* Do some spot-checks for the most obvious invalid types. */
920 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
922 error_at (location, "property can not be an array");
923 return;
926 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
927 parsing, while the C/ObjC parser accepts it and gives us a
928 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
929 to check for a bitfield when doing ObjC. */
930 #ifndef OBJCPLUS
931 if (DECL_INITIAL (decl))
933 /* A @property is not an actual variable, but it is a way to
934 describe a pair of accessor methods, so its type (which is
935 the type of the return value of the getter and the first
936 argument of the setter) can't be a bitfield (as return values
937 and arguments of functions can not be bitfields). The
938 underlying instance variable could be a bitfield, but that is
939 a different matter. */
940 error_at (location, "property can not be a bit-field");
941 return;
943 #endif
945 /* TODO: Check that the property type is an Objective-C object or a
946 "POD". */
948 /* Implement -Wproperty-assign-default (which is enabled by default). */
949 if (warn_property_assign_default
950 /* If garbage collection is not being used, then 'assign' is
951 valid for objects (and typically used for delegates) but it
952 is wrong in most cases (since most objects need to be
953 retained or copied in setters). Warn users when 'assign' is
954 used implicitly. */
955 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
956 /* Read-only properties are never assigned, so the assignment
957 semantics do not matter in that case. */
958 && !property_readonly
959 && !flag_objc_gc)
961 /* Please note that it would make sense to default to 'assign'
962 for non-{Objective-C objects}, and to 'retain' for
963 Objective-C objects. But that would break compatibility with
964 other compilers. */
965 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
967 /* Use 'false' so we do not warn for Class objects. */
968 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
970 warning_at (location,
972 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
973 decl);
974 inform (location,
975 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
980 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
981 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
982 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
984 if (property_assign_semantics == OBJC_PROPERTY_COPY
985 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
986 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
988 /* Now determine the final property getter and setter names. They
989 will be stored in the PROPERTY_DECL, from which they'll always be
990 extracted and used. */
992 /* Adjust, or fill in, setter and getter names. We overwrite the
993 parsed_property_setter_ident and parsed_property_getter_ident
994 with the final setter and getter identifiers that will be
995 used. */
996 if (parsed_property_setter_ident)
998 /* The setter should be terminated by ':', but the parser only
999 gives us an identifier without ':'. So, we need to add ':'
1000 at the end. */
1001 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1002 size_t length = strlen (parsed_setter);
1003 char *final_setter = (char *)alloca (length + 2);
1005 sprintf (final_setter, "%s:", parsed_setter);
1006 parsed_property_setter_ident = get_identifier (final_setter);
1008 else
1010 if (!property_readonly)
1011 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1012 (DECL_NAME (decl)));
1015 if (!parsed_property_getter_ident)
1016 parsed_property_getter_ident = DECL_NAME (decl);
1018 /* Check for duplicate property declarations. We first check the
1019 immediate context for a property with the same name. Any such
1020 declarations are an error, unless this is a class extension and
1021 we are extending a property from readonly to readwrite. */
1022 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1024 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1026 if (objc_in_class_extension
1027 && property_readonly == 0
1028 && PROPERTY_READONLY (x) == 1)
1030 /* This is a class extension, and we are extending an
1031 existing readonly property to a readwrite one.
1032 That's fine. :-) */
1033 property_extension_in_class_extension = true;
1034 break;
1036 else
1038 location_t original_location = DECL_SOURCE_LOCATION (x);
1040 error_at (location, "redeclaration of property %qD", decl);
1042 if (original_location != UNKNOWN_LOCATION)
1043 inform (original_location, "originally specified here");
1044 return;
1049 /* If x is not NULL_TREE, we must be in a class extension and we're
1050 extending a readonly property. In that case, no point in
1051 searching for another declaration. */
1052 if (x == NULL_TREE)
1054 /* We now need to check for existing property declarations (in
1055 the superclass, other categories or protocols) and check that
1056 the new declaration is not in conflict with existing
1057 ones. */
1059 /* Search for a previous, existing declaration of a property
1060 with the same name in superclasses, protocols etc. If one is
1061 found, it will be in the 'x' variable. */
1063 /* Note that, for simplicity, the following may search again the
1064 local context. That's Ok as nothing will be found (else we'd
1065 have thrown an error above); it's only a little inefficient,
1066 but the code is simpler. */
1067 switch (TREE_CODE (objc_interface_context))
1069 case CLASS_INTERFACE_TYPE:
1070 /* Look up the property in the current @interface (which
1071 will find nothing), then its protocols and categories and
1072 superclasses. */
1073 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1074 break;
1075 case CATEGORY_INTERFACE_TYPE:
1076 /* Look up the property in the main @interface, then
1077 protocols and categories (one of them is ours, and will
1078 find nothing) and superclasses. */
1079 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1080 DECL_NAME (decl));
1081 break;
1082 case PROTOCOL_INTERFACE_TYPE:
1083 /* Looks up the property in any protocols attached to the
1084 current protocol. */
1085 if (PROTOCOL_LIST (objc_interface_context))
1087 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1088 DECL_NAME (decl));
1090 break;
1091 default:
1092 gcc_unreachable ();
1096 if (x != NULL_TREE)
1098 /* An existing property was found; check that it has the same
1099 types, or it is compatible. */
1100 location_t original_location = DECL_SOURCE_LOCATION (x);
1102 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1104 warning_at (location, 0,
1105 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1107 if (original_location != UNKNOWN_LOCATION)
1108 inform (original_location, "originally specified here");
1109 return;
1112 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1114 warning_at (location, 0,
1115 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1117 if (original_location != UNKNOWN_LOCATION)
1118 inform (original_location, "originally specified here");
1119 return;
1122 /* We can only compare the setter names if both the old and new property have a setter. */
1123 if (!property_readonly && !PROPERTY_READONLY(x))
1125 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1127 warning_at (location, 0,
1128 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1130 if (original_location != UNKNOWN_LOCATION)
1131 inform (original_location, "originally specified here");
1132 return;
1136 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1138 warning_at (location, 0,
1139 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1141 if (original_location != UNKNOWN_LOCATION)
1142 inform (original_location, "originally specified here");
1143 return;
1146 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1147 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1149 warning_at (location, 0,
1150 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1152 if (original_location != UNKNOWN_LOCATION)
1153 inform (original_location, "originally specified here");
1154 return;
1157 /* We now check that the new and old property declarations have
1158 the same types (or compatible one). In the Objective-C
1159 tradition of loose type checking, we do type-checking but
1160 only generate warnings (not errors) if they do not match.
1161 For non-readonly properties, the types must match exactly;
1162 for readonly properties, it is allowed to use a "more
1163 specialized" type in the new property declaration. Eg, the
1164 superclass has a getter returning (NSArray *) and the
1165 subclass a getter returning (NSMutableArray *). The object's
1166 getter returns an (NSMutableArray *); but if you cast the
1167 object to the superclass, which is allowed, you'd still
1168 expect the getter to return an (NSArray *), which works since
1169 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1170 objects belonging to the type of the new @property should be
1171 a subset of the set of objects belonging to the type of the
1172 old @property. This is what "specialization" means. And the
1173 reason it only applies to readonly properties is that for a
1174 readwrite property the setter would have the opposite
1175 requirement - ie that the superclass type is more specialized
1176 then the subclass one; hence the only way to satisfy both
1177 constraints is that the types match. */
1179 /* If the types are not the same in the C sense, we warn ... */
1180 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1181 /* ... unless the property is readonly, in which case we
1182 allow a new, more specialized, declaration. */
1183 && (!property_readonly
1184 || !objc_compare_types (TREE_TYPE (x),
1185 TREE_TYPE (decl), -5, NULL_TREE)))
1187 warning_at (location, 0,
1188 "type of property %qD conflicts with previous declaration", decl);
1189 if (original_location != UNKNOWN_LOCATION)
1190 inform (original_location, "originally specified here");
1191 return;
1194 /* If we are in a class extension and we're extending a readonly
1195 property in the main @interface, we'll just update the
1196 existing property with the readwrite flag and potentially the
1197 new setter name. */
1198 if (property_extension_in_class_extension)
1200 PROPERTY_READONLY (x) = 0;
1201 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1202 return;
1206 /* Create a PROPERTY_DECL node. */
1207 property_decl = make_node (PROPERTY_DECL);
1209 /* Copy the basic information from the original decl. */
1210 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1211 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1212 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1214 /* Add property-specific information. */
1215 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1216 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1217 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1218 PROPERTY_READONLY (property_decl) = property_readonly;
1219 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1220 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1221 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1222 PROPERTY_DYNAMIC (property_decl) = 0;
1224 /* Remember the fact that the property was found in the @optional
1225 section in a @protocol, or not. */
1226 if (objc_method_optional_flag)
1227 PROPERTY_OPTIONAL (property_decl) = 1;
1228 else
1229 PROPERTY_OPTIONAL (property_decl) = 0;
1231 /* Note that PROPERTY_GETTER_NAME is always set for all
1232 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1233 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1234 with a getter or setter, we should get the PROPERTY_DECL and use
1235 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1236 names. */
1238 /* Add the PROPERTY_DECL to the list of properties for the class. */
1239 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1240 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1243 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1244 list of methods in the interface (and, failing that, the local list
1245 in the implementation, and failing that, the protocol list)
1246 provided for a 'setter' or 'getter' for 'component' with default
1247 names (ie, if 'component' is "name", then search for "name" and
1248 "setName:"). It is also possible to specify a different
1249 'getter_name' (this is used for @optional readonly properties). If
1250 any is found, then create an artificial property that uses them.
1251 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1252 static tree
1253 maybe_make_artificial_property_decl (tree interface, tree implementation,
1254 tree protocol_list, tree component, bool is_class,
1255 tree getter_name)
1257 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1258 tree getter = NULL_TREE;
1259 tree setter = NULL_TREE;
1261 if (getter_name == NULL_TREE)
1262 getter_name = component;
1264 /* First, check the @interface and all superclasses. */
1265 if (interface)
1267 int flags = 0;
1269 /* Using instance methods of the root class as accessors is most
1270 likely unwanted and can be extremely confusing (and, most
1271 importantly, other Objective-C 2.0 compilers do not do it).
1272 Turn it off. */
1273 if (is_class)
1274 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1276 getter = lookup_method_static (interface, getter_name, flags);
1277 setter = lookup_method_static (interface, setter_name, flags);
1280 /* Second, check the local @implementation context. */
1281 if (!getter && !setter)
1283 if (implementation)
1285 if (is_class)
1287 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1288 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1290 else
1292 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1293 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1298 /* Try the protocol_list if we didn't find anything in the
1299 @interface and in the @implementation. */
1300 if (!getter && !setter)
1302 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1303 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1306 /* There needs to be at least a getter or setter for this to be a
1307 valid 'object.component' syntax. */
1308 if (getter || setter)
1310 /* Yes ... determine the type of the expression. */
1311 tree property_decl;
1312 tree type;
1314 if (getter)
1315 type = TREE_VALUE (TREE_TYPE (getter));
1316 else
1317 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1319 /* Create an artificial property declaration with the
1320 information we collected on the type and getter/setter
1321 names. */
1322 property_decl = make_node (PROPERTY_DECL);
1324 TREE_TYPE (property_decl) = type;
1325 DECL_SOURCE_LOCATION (property_decl) = input_location;
1326 TREE_DEPRECATED (property_decl) = 0;
1327 DECL_ARTIFICIAL (property_decl) = 1;
1329 /* Add property-specific information. Note that one of
1330 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1331 non-existing method; this will generate an error when the
1332 expression is later compiled. At this stage we don't know if
1333 the getter or setter will be used, so we can't generate an
1334 error. */
1335 PROPERTY_NAME (property_decl) = component;
1336 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1337 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1338 PROPERTY_READONLY (property_decl) = 0;
1339 PROPERTY_NONATOMIC (property_decl) = 0;
1340 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1341 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1342 PROPERTY_DYNAMIC (property_decl) = 0;
1343 PROPERTY_OPTIONAL (property_decl) = 0;
1345 if (!getter)
1346 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1348 /* The following is currently unused, but it's nice to have
1349 there. We may use it if we need in the future. */
1350 if (!setter)
1351 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1353 return property_decl;
1356 return NULL_TREE;
1359 /* This hook routine is invoked by the parser when an expression such
1360 as 'xxx.yyy' is parsed. We get a chance to process these
1361 expressions in a way that is specified to Objective-C (to implement
1362 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1363 If the expression is not an Objective-C specified expression, we
1364 should return NULL_TREE; else we return the expression.
1366 At the moment this only implements dot-syntax and properties (not
1367 non-fragile ivars yet), ie 'object.property' or 'object.component'
1368 where 'component' is not a declared property, but a valid getter or
1369 setter for it could be found. */
1370 tree
1371 objc_maybe_build_component_ref (tree object, tree property_ident)
1373 tree x = NULL_TREE;
1374 tree rtype;
1376 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1377 not available. */
1378 if (flag_objc1_only)
1379 return NULL_TREE;
1381 /* Try to determine if 'object' is an Objective-C object or not. If
1382 not, return. */
1383 if (object == NULL_TREE || object == error_mark_node
1384 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1385 return NULL_TREE;
1387 if (property_ident == NULL_TREE || property_ident == error_mark_node
1388 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1389 return NULL_TREE;
1391 /* The following analysis of 'object' is similar to the one used for
1392 the 'receiver' of a method invocation. We need to determine what
1393 'object' is and find the appropriate property (either declared,
1394 or artificial) for it (in the same way as we need to find the
1395 appropriate method prototype for a method invocation). There are
1396 some simplifications here though: "object.property" is invalid if
1397 "object" has a type of "id" or "Class"; it must at least have a
1398 protocol attached to it, and "object" is never a class name as
1399 that is done by objc_build_class_component_ref. Finally, we
1400 don't know if this really is a dot-syntax expression, so we want
1401 to make a quick exit if it is not; for this reason, we try to
1402 postpone checks after determining that 'object' looks like an
1403 Objective-C object. */
1405 if (objc_is_id (rtype))
1407 /* This is the case that the 'object' is of type 'id' or
1408 'Class'. */
1410 /* Check if at least it is of type 'id <Protocol>' or 'Class
1411 <Protocol>'; if so, look the property up in the
1412 protocols. */
1413 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1415 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1417 if (rprotos)
1419 /* No point looking up declared @properties if we are
1420 dealing with a class. Classes have no declared
1421 properties. */
1422 if (!IS_CLASS (rtype))
1423 x = lookup_property_in_protocol_list (rprotos, property_ident);
1425 if (x == NULL_TREE)
1427 /* Ok, no property. Maybe it was an
1428 object.component dot-syntax without a declared
1429 property (this is valid for classes too). Look
1430 for getter/setter methods and internally declare
1431 an artificial property based on them if found. */
1432 x = maybe_make_artificial_property_decl (NULL_TREE,
1433 NULL_TREE,
1434 rprotos,
1435 property_ident,
1436 IS_CLASS (rtype),
1437 NULL_TREE);
1439 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1441 /* This is a special, complicated case. If the
1442 property is optional, and is read-only, then the
1443 property is always used for reading, but an
1444 eventual existing non-property setter can be used
1445 for writing. We create an artificial property
1446 decl copying the getter from the optional
1447 property, and looking up the setter in the
1448 interface. */
1449 x = maybe_make_artificial_property_decl (NULL_TREE,
1450 NULL_TREE,
1451 rprotos,
1452 property_ident,
1453 false,
1454 PROPERTY_GETTER_NAME (x));
1458 else if (objc_method_context)
1460 /* Else, if we are inside a method it could be the case of
1461 'super' or 'self'. */
1462 tree interface_type = NULL_TREE;
1463 tree t = object;
1464 while (TREE_CODE (t) == COMPOUND_EXPR
1465 || TREE_CODE (t) == MODIFY_EXPR
1466 || CONVERT_EXPR_P (t)
1467 || TREE_CODE (t) == COMPONENT_REF)
1468 t = TREE_OPERAND (t, 0);
1470 if (t == UOBJC_SUPER_decl)
1471 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1472 else if (t == self_decl)
1473 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1475 if (interface_type)
1477 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1478 x = lookup_property (interface_type, property_ident);
1480 if (x == NULL_TREE)
1482 /* Try the dot-syntax without a declared property.
1483 If this is an access to 'self', it is possible
1484 that they may refer to a setter/getter that is
1485 not declared in the interface, but exists locally
1486 in the implementation. In that case, get the
1487 implementation context and use it. */
1488 tree implementation = NULL_TREE;
1490 if (t == self_decl)
1491 implementation = objc_implementation_context;
1493 x = maybe_make_artificial_property_decl
1494 (interface_type, implementation, NULL_TREE,
1495 property_ident,
1496 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1497 NULL_TREE);
1499 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1501 tree implementation = NULL_TREE;
1503 if (t == self_decl)
1504 implementation = objc_implementation_context;
1506 x = maybe_make_artificial_property_decl (interface_type,
1507 implementation,
1508 NULL_TREE,
1509 property_ident,
1510 false,
1511 PROPERTY_GETTER_NAME (x));
1516 else
1518 /* This is the case where we have more information on 'rtype'. */
1519 tree basetype = TYPE_MAIN_VARIANT (rtype);
1521 /* Skip the pointer - if none, it's not an Objective-C object or
1522 class. */
1523 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1524 basetype = TREE_TYPE (basetype);
1525 else
1526 return NULL_TREE;
1528 /* Traverse typedefs. */
1529 while (basetype != NULL_TREE
1530 && TREE_CODE (basetype) == RECORD_TYPE
1531 && OBJC_TYPE_NAME (basetype)
1532 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1533 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1534 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1536 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1538 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1539 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1541 if (interface_type
1542 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1543 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1544 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1546 /* Not sure 'rtype' could ever be a class here! Just
1547 for safety we keep the checks. */
1548 if (!IS_CLASS (rtype))
1550 x = lookup_property (interface_type, property_ident);
1552 if (x == NULL_TREE)
1553 x = lookup_property_in_protocol_list (protocol_list,
1554 property_ident);
1557 if (x == NULL_TREE)
1559 /* Try the dot-syntax without a declared property.
1560 If we are inside a method implementation, it is
1561 possible that they may refer to a setter/getter
1562 that is not declared in the interface, but exists
1563 locally in the implementation. In that case, get
1564 the implementation context and use it. */
1565 tree implementation = NULL_TREE;
1567 if (objc_implementation_context
1568 && CLASS_NAME (objc_implementation_context)
1569 == OBJC_TYPE_NAME (interface_type))
1570 implementation = objc_implementation_context;
1572 x = maybe_make_artificial_property_decl (interface_type,
1573 implementation,
1574 protocol_list,
1575 property_ident,
1576 IS_CLASS (rtype),
1577 NULL_TREE);
1579 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1581 tree implementation = NULL_TREE;
1583 if (objc_implementation_context
1584 && CLASS_NAME (objc_implementation_context)
1585 == OBJC_TYPE_NAME (interface_type))
1586 implementation = objc_implementation_context;
1588 x = maybe_make_artificial_property_decl (interface_type,
1589 implementation,
1590 protocol_list,
1591 property_ident,
1592 false,
1593 PROPERTY_GETTER_NAME (x));
1599 if (x)
1601 tree expression;
1602 tree getter_call;
1603 tree deprecated_method_prototype = NULL_TREE;
1605 /* We have an additional nasty problem here; if this
1606 PROPERTY_REF needs to become a 'getter', then the conversion
1607 from PROPERTY_REF into a getter call happens in gimplify,
1608 after the selector table has already been generated and when
1609 it is too late to add another selector to it. To work around
1610 the problem, we always create the getter call at this stage,
1611 which puts the selector in the table. Note that if the
1612 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1613 we have added a selector too many to the selector table.
1614 This is a little inefficient.
1616 Also note that method calls to 'self' and 'super' require the
1617 context (self_decl, UOBJS_SUPER_decl,
1618 objc_implementation_context etc) to be built correctly; this
1619 is yet another reason why building the call at the gimplify
1620 stage (when this context has been lost) is not very
1621 practical. If we build it at this stage, we know it will
1622 always be built correctly.
1624 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1625 property decl created to deal with a dotsyntax not really
1626 referring to an existing property) then do not try to build a
1627 call to the getter as there is no getter. */
1628 if (PROPERTY_HAS_NO_GETTER (x))
1629 getter_call = NULL_TREE;
1630 else
1631 getter_call = objc_finish_message_expr
1632 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1633 /* Disable the immediate deprecation warning if the getter
1634 is deprecated, but record the fact that the getter is
1635 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1636 the method prototype. */
1637 &deprecated_method_prototype);
1639 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1640 deprecated_method_prototype);
1641 SET_EXPR_LOCATION (expression, input_location);
1642 TREE_SIDE_EFFECTS (expression) = 1;
1644 return expression;
1647 return NULL_TREE;
1650 /* This hook routine is invoked by the parser when an expression such
1651 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1652 Objective-C 2.0 dot-syntax applied to classes, so we need to
1653 convert it into a setter/getter call on the class. */
1654 tree
1655 objc_build_class_component_ref (tree class_name, tree property_ident)
1657 tree x = NULL_TREE;
1658 tree object, rtype;
1660 if (flag_objc1_only)
1661 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1663 if (class_name == NULL_TREE || class_name == error_mark_node
1664 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1665 return error_mark_node;
1667 if (property_ident == NULL_TREE || property_ident == error_mark_node
1668 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1669 return NULL_TREE;
1671 object = objc_get_class_reference (class_name);
1672 if (!object)
1674 /* We know that 'class_name' is an Objective-C class name as the
1675 parser won't call this function if it is not. This is only a
1676 double-check for safety. */
1677 error_at (input_location, "could not find class %qE", class_name);
1678 return error_mark_node;
1681 rtype = lookup_interface (class_name);
1682 if (!rtype)
1684 /* Again, this should never happen, but we do check. */
1685 error_at (input_location, "could not find interface for class %qE", class_name);
1686 return error_mark_node;
1688 else
1690 if (TREE_DEPRECATED (rtype))
1691 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1694 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1695 property_ident,
1696 true, NULL_TREE);
1698 if (x)
1700 tree expression;
1701 tree getter_call;
1702 tree deprecated_method_prototype = NULL_TREE;
1704 if (PROPERTY_HAS_NO_GETTER (x))
1705 getter_call = NULL_TREE;
1706 else
1707 getter_call = objc_finish_message_expr
1708 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1709 &deprecated_method_prototype);
1711 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1712 deprecated_method_prototype);
1713 SET_EXPR_LOCATION (expression, input_location);
1714 TREE_SIDE_EFFECTS (expression) = 1;
1716 return expression;
1718 else
1720 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1721 property_ident, class_name);
1722 return error_mark_node;
1725 return NULL_TREE;
1730 /* This is used because we don't want to expose PROPERTY_REF to the
1731 C/C++ frontends. Maybe we should! */
1732 bool
1733 objc_is_property_ref (tree node)
1735 if (node && TREE_CODE (node) == PROPERTY_REF)
1736 return true;
1737 else
1738 return false;
1741 /* This function builds a setter call for a PROPERTY_REF (real, for a
1742 declared property, or artificial, for a dot-syntax accessor which
1743 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1744 (the caller must check this beforehand). 'rhs' is the value to
1745 assign to the property. A plain setter call is returned, or
1746 error_mark_node if the property is readonly. */
1748 static tree
1749 objc_build_setter_call (tree lhs, tree rhs)
1751 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1752 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1754 if (PROPERTY_READONLY (property_decl))
1756 error ("readonly property can not be set");
1757 return error_mark_node;
1759 else
1761 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1762 tree setter;
1764 /* TODO: Check that the setter return type is 'void'. */
1766 /* TODO: Decay arguments in C. */
1767 setter = objc_finish_message_expr (object_expr,
1768 PROPERTY_SETTER_NAME (property_decl),
1769 setter_argument, NULL);
1770 return setter;
1773 /* Unreachable, but the compiler may not realize. */
1774 return error_mark_node;
1777 /* This hook routine is called when a MODIFY_EXPR is being built. We
1778 check what is being modified; if it is a PROPERTY_REF, we need to
1779 generate a 'setter' function call for the property. If this is not
1780 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1781 on creating their MODIFY_EXPR.
1783 This is used for example if you write
1785 object.count = 1;
1787 where 'count' is a property. The left-hand side creates a
1788 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1789 to assign something to it. We intercept that here, and generate a
1790 call to the 'setter' method instead. */
1791 tree
1792 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1794 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1796 /* Building a simple call to the setter method would work for cases such as
1798 object.count = 1;
1800 but wouldn't work for cases such as
1802 count = object2.count = 1;
1804 to get these to work with very little effort, we build a
1805 compound statement which does the setter call (to set the
1806 property to 'rhs'), but which can also be evaluated returning
1807 the 'rhs'. If the 'rhs' has no side effects, we can simply
1808 evaluate it twice, building
1810 ([object setProperty: rhs]; rhs)
1812 If it has side effects, we put it in a temporary variable first,
1813 so we create the following:
1815 (temp = rhs; [object setProperty: temp]; temp)
1817 setter_argument is rhs in the first case, and temp in the second
1818 case.
1820 tree setter_argument;
1822 /* s1, s2 and s3 are the tree statements that we need in the
1823 compound expression. */
1824 tree s1, s2, s3, compound_expr;
1826 if (TREE_SIDE_EFFECTS (rhs))
1828 tree bind;
1830 /* Declare __objc_property_temp in a local bind. */
1831 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1832 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1833 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1834 SET_EXPR_LOCATION (bind, input_location);
1835 TREE_SIDE_EFFECTS (bind) = 1;
1836 add_stmt (bind);
1838 /* s1: x = rhs */
1839 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1840 NOP_EXPR,
1841 input_location, rhs, NULL_TREE);
1842 SET_EXPR_LOCATION (s1, input_location);
1844 else
1846 /* No s1. */
1847 setter_argument = rhs;
1848 s1 = NULL_TREE;
1851 /* Now build the compound statement. */
1853 /* s2: [object setProperty: x] */
1854 s2 = objc_build_setter_call (lhs, setter_argument);
1856 /* This happens if building the setter failed because the
1857 property is readonly. */
1858 if (s2 == error_mark_node)
1859 return error_mark_node;
1861 SET_EXPR_LOCATION (s2, input_location);
1863 /* s3: x */
1864 s3 = convert (TREE_TYPE (lhs), setter_argument);
1866 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1867 appropriate. */
1868 if (s1)
1869 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1870 else
1871 compound_expr = build_compound_expr (input_location, s2, s3);
1873 /* Without this, with -Wall you get a 'valued computed is not
1874 used' every time there is a "object.property = x" where the
1875 value of the resulting MODIFY_EXPR is not used. That is
1876 correct (maybe a more sophisticated implementation could
1877 avoid generating the compound expression if not needed), but
1878 we need to turn it off. */
1879 TREE_NO_WARNING (compound_expr) = 1;
1880 return compound_expr;
1882 else
1883 return NULL_TREE;
1886 /* This hook is called by the frontend when one of the four unary
1887 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1888 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1889 argument which is a PROPERTY_REF. For example, this happens if you have
1891 object.count++;
1893 where 'count' is a property. We need to use the 'getter' and
1894 'setter' for the property in an appropriate way to build the
1895 appropriate expression. 'code' is the code for the expression (one
1896 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1897 'increment' is how much we need to add or subtract. */
1898 tree
1899 objc_build_incr_expr_for_property_ref (location_t location,
1900 enum tree_code code,
1901 tree argument, tree increment)
1903 /* Here are the expressions that we want to build:
1905 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1906 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1908 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1909 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1911 tree temp_variable_decl, bind;
1912 /* s1, s2 and s3 are the tree statements that we need in the
1913 compound expression. */
1914 tree s1, s2, s3, compound_expr;
1916 /* Safety check. */
1917 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1918 return error_mark_node;
1920 /* Declare __objc_property_temp in a local bind. */
1921 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1922 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1923 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1924 SET_EXPR_LOCATION (bind, location);
1925 TREE_SIDE_EFFECTS (bind) = 1;
1926 add_stmt (bind);
1928 /* Now build the compound statement. */
1930 /* Note that the 'getter' is generated at gimplify time; at this
1931 time, we can simply put the property_ref (ie, argument) wherever
1932 we want the getter ultimately to be. */
1934 /* s1: __objc_property_temp = [object property] <+/- increment> */
1935 switch (code)
1937 case PREINCREMENT_EXPR:
1938 /* __objc_property_temp = [object property] + increment */
1939 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1940 NOP_EXPR,
1941 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1942 argument, increment), NULL_TREE);
1943 break;
1944 case PREDECREMENT_EXPR:
1945 /* __objc_property_temp = [object property] - increment */
1946 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1947 NOP_EXPR,
1948 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1949 argument, increment), NULL_TREE);
1950 break;
1951 case POSTINCREMENT_EXPR:
1952 case POSTDECREMENT_EXPR:
1953 /* __objc_property_temp = [object property] */
1954 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1955 NOP_EXPR,
1956 location, argument, NULL_TREE);
1957 break;
1958 default:
1959 gcc_unreachable ();
1962 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1963 switch (code)
1965 case PREINCREMENT_EXPR:
1966 case PREDECREMENT_EXPR:
1967 /* [object setProperty: __objc_property_temp] */
1968 s2 = objc_build_setter_call (argument, temp_variable_decl);
1969 break;
1970 case POSTINCREMENT_EXPR:
1971 /* [object setProperty: __objc_property_temp + increment] */
1972 s2 = objc_build_setter_call (argument,
1973 build2 (PLUS_EXPR, TREE_TYPE (argument),
1974 temp_variable_decl, increment));
1975 break;
1976 case POSTDECREMENT_EXPR:
1977 /* [object setProperty: __objc_property_temp - increment] */
1978 s2 = objc_build_setter_call (argument,
1979 build2 (MINUS_EXPR, TREE_TYPE (argument),
1980 temp_variable_decl, increment));
1981 break;
1982 default:
1983 gcc_unreachable ();
1986 /* This happens if building the setter failed because the property
1987 is readonly. */
1988 if (s2 == error_mark_node)
1989 return error_mark_node;
1991 SET_EXPR_LOCATION (s2, location);
1993 /* s3: __objc_property_temp */
1994 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
1996 /* Now build the compound statement (s1, s2, s3) */
1997 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
1999 /* Prevent C++ from warning with -Wall that "right operand of comma
2000 operator has no effect". */
2001 TREE_NO_WARNING (compound_expr) = 1;
2002 return compound_expr;
2005 tree
2006 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
2007 tree optparms, bool ellipsis)
2009 if (is_class_method)
2010 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2011 optparms, ellipsis);
2012 else
2013 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2014 optparms, ellipsis);
2017 void
2018 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2020 if (!objc_interface_context)
2022 /* PS: At the moment, due to how the parser works, it should be
2023 impossible to get here. But it's good to have the check in
2024 case the parser changes.
2026 fatal_error (input_location,
2027 "method declaration not in @interface context");
2030 if (flag_objc1_only && attributes)
2031 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2033 objc_decl_method_attributes (&decl, attributes, 0);
2034 objc_add_method (objc_interface_context,
2035 decl,
2036 is_class_method,
2037 objc_method_optional_flag);
2040 /* Return 'true' if the method definition could be started, and
2041 'false' if not (because we are outside an @implementation context).
2042 EXPR is NULL or an expression that needs to be evaluated for the
2043 side effects of array size expressions in the parameters.
2045 bool
2046 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2047 tree expr)
2049 if (!objc_implementation_context)
2051 error ("method definition not in @implementation context");
2052 return false;
2055 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2056 return false;
2058 #ifndef OBJCPLUS
2059 /* Indicate no valid break/continue context by setting these variables
2060 to some non-null, non-label value. We'll notice and emit the proper
2061 error message in c_finish_bc_stmt. */
2062 c_break_label = c_cont_label = size_zero_node;
2063 #endif
2065 if (attributes)
2066 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2067 else
2068 objc_decl_method_attributes (&decl, attributes, 0);
2070 objc_add_method (objc_implementation_context,
2071 decl,
2072 is_class_method,
2073 /* is optional */ false);
2074 start_method_def (decl, expr);
2075 return true;
2078 void
2079 objc_add_instance_variable (tree decl)
2081 (void) add_instance_variable (objc_ivar_context,
2082 objc_ivar_visibility,
2083 decl);
2086 /* Construct a C struct with same name as KLASS, a base struct with tag
2087 SUPER_NAME (if any), and FIELDS indicated. */
2089 static tree
2090 objc_build_struct (tree klass, tree fields, tree super_name)
2092 tree name = CLASS_NAME (klass);
2093 tree s = objc_start_struct (name);
2094 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2095 tree t;
2096 vec<tree> objc_info = vNULL;
2097 int i;
2099 if (super)
2101 /* Prepend a packed variant of the base class into the layout. This
2102 is necessary to preserve ObjC ABI compatibility. */
2103 tree base = build_decl (input_location,
2104 FIELD_DECL, NULL_TREE, super);
2105 tree field = TYPE_FIELDS (super);
2107 while (field && DECL_CHAIN (field)
2108 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2109 field = DECL_CHAIN (field);
2111 /* For ObjC ABI purposes, the "packed" size of a base class is
2112 the sum of the offset and the size (in bits) of the last field
2113 in the class. */
2114 DECL_SIZE (base)
2115 = (field && TREE_CODE (field) == FIELD_DECL
2116 ? size_binop (PLUS_EXPR,
2117 size_binop (PLUS_EXPR,
2118 size_binop
2119 (MULT_EXPR,
2120 convert (bitsizetype,
2121 DECL_FIELD_OFFSET (field)),
2122 bitsize_int (BITS_PER_UNIT)),
2123 DECL_FIELD_BIT_OFFSET (field)),
2124 DECL_SIZE (field))
2125 : bitsize_zero_node);
2126 DECL_SIZE_UNIT (base)
2127 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2128 size_int (BITS_PER_UNIT));
2129 DECL_ARTIFICIAL (base) = 1;
2130 DECL_ALIGN (base) = 1;
2131 DECL_FIELD_CONTEXT (base) = s;
2132 #ifdef OBJCPLUS
2133 DECL_FIELD_IS_BASE (base) = 1;
2135 if (fields)
2136 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2137 #endif /* are following the ObjC ABI here. */
2138 DECL_CHAIN (base) = fields;
2139 fields = base;
2142 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2143 information in all variants of this RECORD_TYPE to be destroyed
2144 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2145 for something else and then will change all variants to use the
2146 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2147 it for ObjC protocols and that such propagation will make all
2148 variants use the same objc_info), but it is therein that we store
2149 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2150 Hence, we must save the ObjC-specific information before calling
2151 finish_struct(), and then reinstate it afterwards. */
2153 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2155 INIT_TYPE_OBJC_INFO (t);
2156 objc_info.safe_push (TYPE_OBJC_INFO (t));
2159 s = objc_finish_struct (s, fields);
2161 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2163 /* We now want to restore the different TYPE_OBJC_INFO, but we
2164 have the additional problem that the C frontend doesn't just
2165 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2166 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2167 we need a different TYPE_OBJC_INFO for each (and
2168 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2169 make a copy of each TYPE_LANG_SPECIFIC before we modify
2170 TYPE_OBJC_INFO. */
2171 if (TYPE_LANG_SPECIFIC (t))
2173 /* Create a copy of TYPE_LANG_SPECIFIC. */
2174 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2175 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2176 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2177 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2179 else
2181 /* Just create a new one. */
2182 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2184 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2185 protocol information that may have been associated with the
2186 type. */
2187 TYPE_OBJC_INFO (t) = objc_info[i];
2188 /* Replace the IDENTIFIER_NODE with an actual @interface now
2189 that we have it. */
2190 TYPE_OBJC_INTERFACE (t) = klass;
2192 objc_info.release ();
2194 /* Use TYPE_BINFO structures to point at the super class, if any. */
2195 objc_xref_basetypes (s, super);
2197 /* Mark this struct as a class template. */
2198 CLASS_STATIC_TEMPLATE (klass) = s;
2200 return s;
2203 /* Mark DECL as being 'volatile' for purposes of Darwin
2204 _setjmp()/_longjmp() exception handling. Called from
2205 objc_mark_locals_volatile(). */
2206 void
2207 objc_volatilize_decl (tree decl)
2209 /* Do not mess with variables that are 'static' or (already)
2210 'volatile'. */
2211 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2212 && (TREE_CODE (decl) == VAR_DECL
2213 || TREE_CODE (decl) == PARM_DECL))
2215 if (local_variables_to_volatilize == NULL)
2216 vec_alloc (local_variables_to_volatilize, 8);
2218 vec_safe_push (local_variables_to_volatilize, decl);
2222 /* Called when parsing of a function completes; if any local variables
2223 in the function were marked as variables to volatilize, change them
2224 to volatile. We do this at the end of the function when the
2225 warnings about discarding 'volatile' have already been produced.
2226 We are making the variables as volatile just to force the compiler
2227 to preserve them between setjmp/longjmp, but we don't want warnings
2228 for them as they aren't really volatile. */
2229 void
2230 objc_finish_function (void)
2232 /* If there are any local variables to volatilize, volatilize them. */
2233 if (local_variables_to_volatilize)
2235 int i;
2236 tree decl;
2237 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
2239 tree t = TREE_TYPE (decl);
2241 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2242 TREE_TYPE (decl) = t;
2243 TREE_THIS_VOLATILE (decl) = 1;
2244 TREE_SIDE_EFFECTS (decl) = 1;
2245 DECL_REGISTER (decl) = 0;
2246 #ifndef OBJCPLUS
2247 C_DECL_REGISTER (decl) = 0;
2248 #endif
2251 /* Now we delete the vector. This sets it to NULL as well. */
2252 vec_free (local_variables_to_volatilize);
2256 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2257 (including its categories and superclasses) or by object type TYP.
2258 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2260 static bool
2261 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2263 bool class_type = (cls != NULL_TREE);
2265 while (cls)
2267 tree c;
2269 /* Check protocols adopted by the class and its categories. */
2270 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2272 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2273 return true;
2276 /* Repeat for superclasses. */
2277 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2280 /* Check for any protocols attached directly to the object type. */
2281 if (TYPE_HAS_OBJC_INFO (typ))
2283 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2284 return true;
2287 if (warn)
2289 *errbuf = 0;
2290 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2291 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2292 "implementing" a given protocol, since they do not have an
2293 implementation. */
2294 if (class_type)
2295 warning (0, "class %qs does not implement the %qE protocol",
2296 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2297 else
2298 warning (0, "type %qs does not conform to the %qE protocol",
2299 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2302 return false;
2305 /* Check if class RCLS and instance struct type RTYP conform to at least the
2306 same protocols that LCLS and LTYP conform to. */
2308 static bool
2309 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2311 tree p;
2312 bool have_lproto = false;
2314 while (lcls)
2316 /* NB: We do _not_ look at categories defined for LCLS; these may or
2317 may not get loaded in, and therefore it is unreasonable to require
2318 that RCLS/RTYP must implement any of their protocols. */
2319 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2321 have_lproto = true;
2323 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2324 return warn;
2327 /* Repeat for superclasses. */
2328 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2331 /* Check for any protocols attached directly to the object type. */
2332 if (TYPE_HAS_OBJC_INFO (ltyp))
2334 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2336 have_lproto = true;
2338 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2339 return warn;
2343 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2344 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2345 away with simply checking for 'id' or 'Class' (!RCLS), since this
2346 routine will not get called in other cases. */
2347 return have_lproto || (rcls != NULL_TREE);
2350 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2351 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2352 compatible by objc_compare_types() below. */
2354 tree
2355 objc_common_type (tree type1, tree type2)
2357 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2359 while (POINTER_TYPE_P (inner1))
2361 inner1 = TREE_TYPE (inner1);
2362 inner2 = TREE_TYPE (inner2);
2365 /* If one type is derived from another, return the base type. */
2366 if (DERIVED_FROM_P (inner1, inner2))
2367 return type1;
2368 else if (DERIVED_FROM_P (inner2, inner1))
2369 return type2;
2371 /* If both types are 'Class', return 'Class'. */
2372 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2373 return objc_class_type;
2375 /* Otherwise, return 'id'. */
2376 return objc_object_type;
2379 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2380 an instance of RTYP to an instance of LTYP or to compare the two
2381 (if ARGNO is equal to -3), per ObjC type system rules. Before
2382 returning 'true', this routine may issue warnings related to, e.g.,
2383 protocol conformance. When returning 'false', the routine must
2384 produce absolutely no warnings; the C or C++ front-end will do so
2385 instead, if needed. If either LTYP or RTYP is not an Objective-C
2386 type, the routine must return 'false'.
2388 The ARGNO parameter is encoded as follows:
2389 >= 1 Parameter number (CALLEE contains function being called);
2390 0 Return value;
2391 -1 Assignment;
2392 -2 Initialization;
2393 -3 Comparison (LTYP and RTYP may match in either direction);
2394 -4 Silent comparison (for C++ overload resolution);
2395 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2396 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2397 so that each object of type RTYP is also of type LTYP). This is used
2398 when comparing property types. */
2400 bool
2401 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2403 tree lcls, rcls, lproto, rproto;
2404 bool pointers_compatible;
2406 /* We must be dealing with pointer types */
2407 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2408 return false;
2412 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2413 rtyp = TREE_TYPE (rtyp);
2415 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2417 /* We must also handle function pointers, since ObjC is a bit more
2418 lenient than C or C++ on this. */
2419 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2421 function_args_iterator liter, riter;
2423 /* Return types must be covariant. */
2424 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2425 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2426 argno, callee))
2427 return false;
2429 /* Argument types must be contravariant. */
2430 function_args_iter_init (&liter, ltyp);
2431 function_args_iter_init (&riter, rtyp);
2433 while (1)
2435 ltyp = function_args_iter_cond (&liter);
2436 rtyp = function_args_iter_cond (&riter);
2438 /* If we've exhaused both lists simulateously, we're done. */
2439 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2440 break;
2442 /* If one list is shorter than the other, they fail to match. */
2443 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2444 return false;
2446 if (!comptypes (rtyp, ltyp)
2447 && !objc_compare_types (rtyp, ltyp, argno, callee))
2448 return false;
2450 function_args_iter_next (&liter);
2451 function_args_iter_next (&riter);
2454 return true;
2457 /* Past this point, we are only interested in ObjC class instances,
2458 or 'id' or 'Class'. */
2459 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2460 return false;
2462 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2463 && !TYPE_HAS_OBJC_INFO (ltyp))
2464 return false;
2466 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2467 && !TYPE_HAS_OBJC_INFO (rtyp))
2468 return false;
2470 /* Past this point, we are committed to returning 'true' to the caller
2471 (unless performing a silent comparison; see below). However, we can
2472 still warn about type and/or protocol mismatches. */
2474 if (TYPE_HAS_OBJC_INFO (ltyp))
2476 lcls = TYPE_OBJC_INTERFACE (ltyp);
2477 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2479 else
2480 lcls = lproto = NULL_TREE;
2482 if (TYPE_HAS_OBJC_INFO (rtyp))
2484 rcls = TYPE_OBJC_INTERFACE (rtyp);
2485 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2487 else
2488 rcls = rproto = NULL_TREE;
2490 /* If we could not find an @interface declaration, we must have
2491 only seen a @class declaration; for purposes of type comparison,
2492 treat it as a stand-alone (root) class. */
2494 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2495 lcls = NULL_TREE;
2497 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2498 rcls = NULL_TREE;
2500 /* If either type is an unqualified 'id', we're done. This is because
2501 an 'id' can be assigned to or from any type with no warnings. */
2502 if (argno != -5)
2504 if ((!lproto && objc_is_object_id (ltyp))
2505 || (!rproto && objc_is_object_id (rtyp)))
2506 return true;
2508 else
2510 /* For property checks, though, an 'id' is considered the most
2511 general type of object, hence if you try to specialize an
2512 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2513 to warn. */
2514 if (!lproto && objc_is_object_id (ltyp))
2515 return true;
2518 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2520 /* If the underlying types are the same, and at most one of them has
2521 a protocol list, we do not need to issue any diagnostics. */
2522 if (pointers_compatible && (!lproto || !rproto))
2523 return true;
2525 /* If exactly one of the types is 'Class', issue a diagnostic; any
2526 exceptions of this rule have already been handled. */
2527 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2528 pointers_compatible = false;
2529 /* Otherwise, check for inheritance relations. */
2530 else
2532 if (!pointers_compatible)
2534 /* Again, if any of the two is an 'id', we're satisfied,
2535 unless we're comparing properties, in which case only an
2536 'id' on the left-hand side (old property) is good
2537 enough. */
2538 if (argno != -5)
2539 pointers_compatible
2540 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2541 else
2542 pointers_compatible = objc_is_object_id (ltyp);
2545 if (!pointers_compatible)
2546 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2548 if (!pointers_compatible && (argno == -3 || argno == -4))
2549 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2552 /* If the pointers match modulo protocols, check for protocol conformance
2553 mismatches. */
2554 if (pointers_compatible)
2556 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2557 argno != -3);
2559 if (!pointers_compatible && argno == -3)
2560 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2561 argno != -3);
2564 if (!pointers_compatible)
2566 /* The two pointers are not exactly compatible. Issue a warning, unless
2567 we are performing a silent comparison, in which case return 'false'
2568 instead. */
2569 /* NB: For the time being, we shall make our warnings look like their
2570 C counterparts. In the future, we may wish to make them more
2571 ObjC-specific. */
2572 switch (argno)
2574 case -5:
2575 case -4:
2576 return false;
2578 case -3:
2579 warning (0, "comparison of distinct Objective-C types lacks a cast");
2580 break;
2582 case -2:
2583 warning (0, "initialization from distinct Objective-C type");
2584 break;
2586 case -1:
2587 warning (0, "assignment from distinct Objective-C type");
2588 break;
2590 case 0:
2591 warning (0, "distinct Objective-C type in return");
2592 break;
2594 default:
2595 warning (0, "passing argument %d of %qE from distinct "
2596 "Objective-C type", argno, callee);
2597 break;
2601 return true;
2604 /* This routine is similar to objc_compare_types except that function-pointers are
2605 excluded. This is because, caller assumes that common types are of (id, Object*)
2606 variety and calls objc_common_type to obtain a common type. There is no commonolty
2607 between two function-pointers in this regard. */
2609 bool
2610 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2612 if (objc_compare_types (ltyp, rtyp, argno, callee))
2614 /* exclude function-pointer types. */
2617 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2618 rtyp = TREE_TYPE (rtyp);
2620 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2621 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2623 return false;
2626 #ifndef OBJCPLUS
2627 /* Determine if CHILD is derived from PARENT. The routine assumes that
2628 both parameters are RECORD_TYPEs, and is non-reflexive. */
2630 static bool
2631 objc_derived_from_p (tree parent, tree child)
2633 parent = TYPE_MAIN_VARIANT (parent);
2635 for (child = TYPE_MAIN_VARIANT (child);
2636 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2638 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2639 (TYPE_BINFO (child),
2640 0)));
2642 if (child == parent)
2643 return true;
2646 return false;
2648 #endif
2650 tree
2651 objc_build_component_ref (tree datum, tree component)
2653 /* If COMPONENT is NULL, the caller is referring to the anonymous
2654 base class field. */
2655 if (!component)
2657 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2659 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2662 /* The 'build_component_ref' routine has been removed from the C++
2663 front-end, but 'finish_class_member_access_expr' seems to be
2664 a worthy substitute. */
2665 #ifdef OBJCPLUS
2666 return finish_class_member_access_expr (datum, component, false,
2667 tf_warning_or_error);
2668 #else
2669 return build_component_ref (input_location, datum, component);
2670 #endif
2673 /* Recursively copy inheritance information rooted at BINFO. To do this,
2674 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2676 static tree
2677 objc_copy_binfo (tree binfo)
2679 tree btype = BINFO_TYPE (binfo);
2680 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2681 tree base_binfo;
2682 int ix;
2684 BINFO_TYPE (binfo2) = btype;
2685 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2686 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2688 /* Recursively copy base binfos of BINFO. */
2689 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2691 tree base_binfo2 = objc_copy_binfo (base_binfo);
2693 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2694 BINFO_BASE_APPEND (binfo2, base_binfo2);
2697 return binfo2;
2700 /* Record superclass information provided in BASETYPE for ObjC class REF.
2701 This is loosely based on cp/decl.c:xref_basetypes(). */
2703 static void
2704 objc_xref_basetypes (tree ref, tree basetype)
2706 tree variant;
2707 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2708 TYPE_BINFO (ref) = binfo;
2709 BINFO_OFFSET (binfo) = size_zero_node;
2710 BINFO_TYPE (binfo) = ref;
2712 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2713 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2714 TYPE_BINFO (variant) = binfo;
2716 if (basetype)
2718 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2720 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2721 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
2722 BINFO_BASE_APPEND (binfo, base_binfo);
2723 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2727 /* Called from finish_decl. */
2729 void
2730 objc_check_decl (tree decl)
2732 tree type = TREE_TYPE (decl);
2734 if (TREE_CODE (type) != RECORD_TYPE)
2735 return;
2736 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2737 error ("statically allocated instance of Objective-C class %qE",
2738 type);
2741 void
2742 objc_check_global_decl (tree decl)
2744 tree id = DECL_NAME (decl);
2745 if (objc_is_class_name (id) && global_bindings_p())
2746 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2749 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2750 INTERFACE may either name an Objective-C class, or refer to the
2751 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2752 type, just return it unchanged. This function is often called when
2753 PROTOCOLS is NULL_TREE, in which case we simply look up the
2754 appropriate INTERFACE. */
2756 tree
2757 objc_get_protocol_qualified_type (tree interface, tree protocols)
2759 /* If INTERFACE is not provided, default to 'id'. */
2760 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2761 bool is_ptr = (type != NULL_TREE);
2763 if (!is_ptr)
2765 type = objc_is_class_name (interface);
2767 if (type)
2769 /* If looking at a typedef, retrieve the precise type it
2770 describes. */
2771 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2772 interface = identifier_global_value (interface);
2774 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2775 && DECL_ORIGINAL_TYPE (interface))
2776 ? DECL_ORIGINAL_TYPE (interface)
2777 : xref_tag (RECORD_TYPE, type));
2779 else
2781 /* This case happens when we are given an 'interface' which
2782 is not a valid class name. For example if a typedef was
2783 used, and 'interface' really is the identifier of the
2784 typedef, but when you resolve it you don't get an
2785 Objective-C class, but something else, such as 'int'.
2786 This is an error; protocols make no sense unless you use
2787 them with Objective-C objects. */
2788 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2790 /* Try to recover. Ignore the invalid class name, and treat
2791 the object as an 'id' to silence further warnings about
2792 the class. */
2793 type = objc_object_type;
2794 is_ptr = true;
2798 if (protocols)
2800 type = build_variant_type_copy (type);
2802 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2803 to the pointee. */
2804 if (is_ptr)
2806 tree orig_pointee_type = TREE_TYPE (type);
2807 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2809 /* Set up the canonical type information. */
2810 TYPE_CANONICAL (type)
2811 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2813 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2814 type = TREE_TYPE (type);
2817 /* Look up protocols and install in lang specific list. */
2818 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2819 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2820 (protocols, /* definition_required */ false);
2822 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2823 return the pointer to the new pointee variant. */
2824 if (is_ptr)
2825 type = TYPE_POINTER_TO (type);
2826 else
2827 TYPE_OBJC_INTERFACE (type)
2828 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2831 return type;
2834 /* Check for circular dependencies in protocols. The arguments are
2835 PROTO, the protocol to check, and LIST, a list of protocol it
2836 conforms to. */
2838 static void
2839 check_protocol_recursively (tree proto, tree list)
2841 tree p;
2843 for (p = list; p; p = TREE_CHAIN (p))
2845 tree pp = TREE_VALUE (p);
2847 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2848 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2849 /* definition_required */ false);
2851 if (pp == proto)
2852 fatal_error (input_location, "protocol %qE has circular dependency",
2853 PROTOCOL_NAME (pp));
2854 if (pp)
2855 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2859 /* Look up PROTOCOLS, and return a list of those that are found. If
2860 none are found, return NULL. Note that this function will emit a
2861 warning if a protocol is found and is deprecated. If
2862 'definition_required', then warn if the protocol is found but is
2863 not defined (ie, if we only saw a forward-declaration of the
2864 protocol (as in "@protocol NSObject;") not a real definition with
2865 the list of methods). */
2866 static tree
2867 lookup_and_install_protocols (tree protocols, bool definition_required)
2869 tree proto;
2870 tree return_value = NULL_TREE;
2872 if (protocols == error_mark_node)
2873 return NULL;
2875 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2877 tree ident = TREE_VALUE (proto);
2878 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2879 definition_required);
2881 if (p)
2882 return_value = chainon (return_value,
2883 build_tree_list (NULL_TREE, p));
2884 else if (ident != error_mark_node)
2885 error ("cannot find protocol declaration for %qE",
2886 ident);
2889 return return_value;
2892 static void
2893 build_common_objc_exception_stuff (void)
2895 tree noreturn_list, nothrow_list, temp_type;
2897 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2898 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2900 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2901 /* void objc_sync_enter(id); */
2902 /* void objc_sync_exit(id); */
2903 temp_type = build_function_type_list (void_type_node,
2904 objc_object_type,
2905 NULL_TREE);
2906 objc_exception_throw_decl
2907 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2908 noreturn_list);
2909 /* Make sure that objc_exception_throw (id) claims that it may throw an
2910 exception. */
2911 TREE_NOTHROW (objc_exception_throw_decl) = 0;
2913 objc_sync_enter_decl
2914 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2915 NULL, nothrow_list);
2917 objc_sync_exit_decl
2918 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2919 NULL, nothrow_list);
2922 /* Purpose: "play" parser, creating/installing representations
2923 of the declarations that are required by Objective-C.
2925 Model:
2927 type_spec--------->sc_spec
2928 (tree_list) (tree_list)
2931 identifier_node identifier_node */
2933 static void
2934 synth_module_prologue (void)
2936 tree type;
2937 enum debug_info_type save_write_symbols = write_symbols;
2938 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2940 /* Suppress outputting debug symbols, because
2941 dbxout_init hasn't been called yet. */
2942 write_symbols = NO_DEBUG;
2943 debug_hooks = &do_nothing_debug_hooks;
2945 #ifdef OBJCPLUS
2946 push_lang_context (lang_name_c); /* extern "C" */
2947 #endif
2949 /* The following are also defined in <objc/objc.h> and friends. */
2951 objc_object_id = get_identifier (TAG_OBJECT);
2952 objc_class_id = get_identifier (TAG_CLASS);
2954 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2955 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2957 objc_object_type = build_pointer_type (objc_object_reference);
2958 objc_class_type = build_pointer_type (objc_class_reference);
2960 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2961 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2963 /* Declare the 'id' and 'Class' typedefs. */
2964 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2965 TYPE_DECL,
2966 objc_object_name,
2967 objc_object_type));
2968 TREE_NO_WARNING (type) = 1;
2970 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2971 TYPE_DECL,
2972 objc_class_name,
2973 objc_class_type));
2974 TREE_NO_WARNING (type) = 1;
2976 /* Forward-declare '@interface Protocol'. */
2977 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2978 objc_declare_class (type);
2979 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
2981 /* Declare receiver type used for dispatching messages to 'super'. */
2982 /* `struct objc_super *' */
2983 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2984 get_identifier (TAG_SUPER)));
2986 /* Declare pointers to method and ivar lists. */
2987 objc_method_list_ptr = build_pointer_type
2988 (xref_tag (RECORD_TYPE,
2989 get_identifier (UTAG_METHOD_LIST)));
2990 objc_method_proto_list_ptr
2991 = build_pointer_type (xref_tag (RECORD_TYPE,
2992 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2993 objc_ivar_list_ptr = build_pointer_type
2994 (xref_tag (RECORD_TYPE,
2995 get_identifier (UTAG_IVAR_LIST)));
2997 build_common_objc_exception_stuff ();
2999 /* Set-up runtime-specific templates, message and exception stuff. */
3000 (*runtime.initialize) ();
3002 /* Declare objc_getProperty, object_setProperty and other property
3003 accessor helpers. */
3004 build_common_objc_property_accessor_helpers ();
3006 /* Forward declare constant_string_id and constant_string_type. */
3007 if (!constant_string_class_name)
3008 constant_string_class_name = runtime.default_constant_string_class_name;
3009 constant_string_id = get_identifier (constant_string_class_name);
3010 objc_declare_class (constant_string_id);
3012 /* Pre-build the following entities - for speed/convenience. */
3013 self_id = get_identifier ("self");
3014 ucmd_id = get_identifier ("_cmd");
3016 /* Declare struct _objc_fast_enumeration_state { ... }; */
3017 build_fast_enumeration_state_template ();
3019 /* void objc_enumeration_mutation (id) */
3020 type = build_function_type_list (void_type_node,
3021 objc_object_type, NULL_TREE);
3022 objc_enumeration_mutation_decl
3023 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3024 NULL, NULL_TREE);
3025 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3027 #ifdef OBJCPLUS
3028 pop_lang_context ();
3029 #endif
3031 write_symbols = save_write_symbols;
3032 debug_hooks = save_hooks;
3035 /* --- const strings --- */
3037 /* Ensure that the ivar list for NSConstantString/NXConstantString
3038 (or whatever was specified via `-fconstant-string-class')
3039 contains fields at least as large as the following three, so that
3040 the runtime can stomp on them with confidence:
3042 struct STRING_OBJECT_CLASS_NAME
3044 Object isa;
3045 char *cString;
3046 unsigned int length;
3047 }; */
3049 static int
3050 check_string_class_template (void)
3052 tree field_decl = objc_get_class_ivars (constant_string_id);
3054 #define AT_LEAST_AS_LARGE_AS(F, T) \
3055 (F && TREE_CODE (F) == FIELD_DECL \
3056 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3057 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3059 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3060 return 0;
3062 field_decl = DECL_CHAIN (field_decl);
3063 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3064 return 0;
3066 field_decl = DECL_CHAIN (field_decl);
3067 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3069 #undef AT_LEAST_AS_LARGE_AS
3072 /* Avoid calling `check_string_class_template ()' more than once. */
3073 static GTY(()) int string_layout_checked;
3075 /* Construct an internal string layout to be used as a template for
3076 creating NSConstantString/NXConstantString instances. */
3078 static tree
3079 objc_build_internal_const_str_type (void)
3081 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3082 tree fields = build_decl (input_location,
3083 FIELD_DECL, NULL_TREE, ptr_type_node);
3084 tree field = build_decl (input_location,
3085 FIELD_DECL, NULL_TREE, ptr_type_node);
3087 DECL_CHAIN (field) = fields; fields = field;
3088 field = build_decl (input_location,
3089 FIELD_DECL, NULL_TREE, unsigned_type_node);
3090 DECL_CHAIN (field) = fields; fields = field;
3091 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3092 reverse order! */
3093 finish_builtin_struct (type, "__builtin_ObjCString",
3094 fields, NULL_TREE);
3096 return type;
3099 /* Custom build_string which sets TREE_TYPE! */
3101 tree
3102 my_build_string (int len, const char *str)
3104 return fix_string_type (build_string (len, str));
3107 /* Build a string with contents STR and length LEN and convert it to a
3108 pointer. */
3110 tree
3111 my_build_string_pointer (int len, const char *str)
3113 tree string = my_build_string (len, str);
3114 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3115 return build1 (ADDR_EXPR, ptrtype, string);
3118 hashval_t
3119 objc_string_hasher::hash (string_descriptor *ptr)
3121 const_tree const str = ptr->literal;
3122 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3123 int i, len = TREE_STRING_LENGTH (str);
3124 hashval_t h = len;
3126 for (i = 0; i < len; i++)
3127 h = ((h * 613) + p[i]);
3129 return h;
3132 bool
3133 objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2)
3135 const_tree const str1 = ptr1->literal;
3136 const_tree const str2 = ptr2->literal;
3137 int len1 = TREE_STRING_LENGTH (str1);
3139 return (len1 == TREE_STRING_LENGTH (str2)
3140 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3141 len1));
3144 /* Given a chain of STRING_CST's, build a static instance of
3145 NXConstantString which points at the concatenation of those
3146 strings. We place the string object in the __string_objects
3147 section of the __OBJC segment. The Objective-C runtime will
3148 initialize the isa pointers of the string objects to point at the
3149 NXConstantString class object. */
3151 tree
3152 objc_build_string_object (tree string)
3154 tree constant_string_class;
3155 int length;
3156 tree addr;
3157 struct string_descriptor *desc, key;
3159 /* We should be passed a STRING_CST. */
3160 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
3161 length = TREE_STRING_LENGTH (string) - 1;
3163 /* The target may have different ideas on how to construct an ObjC string
3164 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3165 constant CFString reference instead.
3166 At present, this is only supported for the NeXT runtime. */
3167 if (flag_next_runtime
3168 && targetcm.objc_construct_string_object)
3170 tree constructor = (*targetcm.objc_construct_string_object) (string);
3171 if (constructor)
3172 return build1 (NOP_EXPR, objc_object_type, constructor);
3175 /* Check whether the string class being used actually exists and has the
3176 correct ivar layout. */
3177 if (!string_layout_checked)
3179 string_layout_checked = -1;
3180 constant_string_class = lookup_interface (constant_string_id);
3181 internal_const_str_type = objc_build_internal_const_str_type ();
3183 if (!constant_string_class
3184 || !(constant_string_type
3185 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3186 error ("cannot find interface declaration for %qE",
3187 constant_string_id);
3188 /* The NSConstantString/NXConstantString ivar layout is now known. */
3189 else if (!check_string_class_template ())
3190 error ("interface %qE does not have valid constant string layout",
3191 constant_string_id);
3192 /* If the runtime can generate a literal reference to the string class,
3193 don't need to run a constructor. */
3194 else if (!(*runtime.setup_const_string_class_decl)())
3195 error ("cannot find reference tag for class %qE", constant_string_id);
3196 else
3198 string_layout_checked = 1; /* Success! */
3199 add_class_reference (constant_string_id);
3203 if (string_layout_checked == -1)
3204 return error_mark_node;
3206 /* Perhaps we already constructed a constant string just like this one? */
3207 key.literal = string;
3208 string_descriptor **loc = string_htab->find_slot (&key, INSERT);
3209 desc = *loc;
3211 if (!desc)
3213 *loc = desc = ggc_alloc<string_descriptor> ();
3214 desc->literal = string;
3215 desc->constructor =
3216 (*runtime.build_const_string_constructor) (input_location, string, length);
3219 addr = convert (build_pointer_type (constant_string_type),
3220 build_unary_op (input_location,
3221 ADDR_EXPR, desc->constructor, 1));
3223 return addr;
3226 /* Build a static constant CONSTRUCTOR
3227 with type TYPE and elements ELTS. */
3229 tree
3230 objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
3232 tree constructor = build_constructor (type, elts);
3234 TREE_CONSTANT (constructor) = 1;
3235 TREE_STATIC (constructor) = 1;
3236 TREE_READONLY (constructor) = 1;
3238 #ifdef OBJCPLUS
3239 /* Adjust for impedance mismatch. We should figure out how to build
3240 CONSTRUCTORs that consistently please both the C and C++ gods. */
3241 if (!(*elts)[0].index)
3242 TREE_TYPE (constructor) = init_list_type_node;
3243 #endif
3245 return constructor;
3248 /* Return the DECL of the string IDENT in the SECTION. */
3250 tree
3251 get_objc_string_decl (tree ident, enum string_section section)
3253 tree chain;
3255 switch (section)
3257 case class_names:
3258 chain = class_names_chain;
3259 break;
3260 case meth_var_names:
3261 chain = meth_var_names_chain;
3262 break;
3263 case meth_var_types:
3264 chain = meth_var_types_chain;
3265 break;
3266 case prop_names_attr:
3267 chain = prop_names_attr_chain;
3268 break;
3269 default:
3270 gcc_unreachable ();
3273 for (; chain != 0; chain = TREE_CHAIN (chain))
3274 if (TREE_VALUE (chain) == ident)
3275 return (TREE_PURPOSE (chain));
3277 /* We didn't find the entry. */
3278 return NULL_TREE;
3281 /* Create a class reference, but don't create a variable to reference
3282 it. */
3284 void
3285 add_class_reference (tree ident)
3287 tree chain;
3289 if ((chain = cls_ref_chain))
3291 tree tail;
3294 if (ident == TREE_VALUE (chain))
3295 return;
3297 tail = chain;
3298 chain = TREE_CHAIN (chain);
3300 while (chain);
3302 /* Append to the end of the list */
3303 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3305 else
3306 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3309 /* Get a class reference, creating it if necessary. Also create the
3310 reference variable. */
3311 tree
3312 objc_get_class_reference (tree ident)
3314 tree orig_ident = (DECL_P (ident)
3315 ? DECL_NAME (ident)
3316 : TYPE_P (ident)
3317 ? OBJC_TYPE_NAME (ident)
3318 : ident);
3319 bool local_scope = false;
3321 #ifdef OBJCPLUS
3322 if (processing_template_decl)
3323 /* Must wait until template instantiation time. */
3324 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
3325 #endif
3327 if (TREE_CODE (ident) == TYPE_DECL)
3328 ident = (DECL_ORIGINAL_TYPE (ident)
3329 ? DECL_ORIGINAL_TYPE (ident)
3330 : TREE_TYPE (ident));
3332 #ifdef OBJCPLUS
3333 if (TYPE_P (ident)
3334 && CP_TYPE_CONTEXT (ident) != global_namespace)
3335 local_scope = true;
3336 #endif
3338 if (local_scope || !(ident = objc_is_class_name (ident)))
3340 error ("%qE is not an Objective-C class name or alias",
3341 orig_ident);
3342 return error_mark_node;
3345 return (*runtime.get_class_reference) (ident);
3348 void
3349 objc_declare_alias (tree alias_ident, tree class_ident)
3351 tree underlying_class;
3353 #ifdef OBJCPLUS
3354 if (current_namespace != global_namespace) {
3355 error ("Objective-C declarations may only appear in global scope");
3357 #endif /* OBJCPLUS */
3359 if (!(underlying_class = objc_is_class_name (class_ident)))
3360 warning (0, "cannot find class %qE", class_ident);
3361 else if (objc_is_class_name (alias_ident))
3362 warning (0, "class %qE already exists", alias_ident);
3363 else
3365 /* Implement @compatibility_alias as a typedef. */
3366 #ifdef OBJCPLUS
3367 push_lang_context (lang_name_c); /* extern "C" */
3368 #endif
3369 lang_hooks.decls.pushdecl (build_decl
3370 (input_location,
3371 TYPE_DECL,
3372 alias_ident,
3373 xref_tag (RECORD_TYPE, underlying_class)));
3374 #ifdef OBJCPLUS
3375 pop_lang_context ();
3376 #endif
3377 objc_map_put (alias_name_map, alias_ident, underlying_class);
3381 void
3382 objc_declare_class (tree identifier)
3384 #ifdef OBJCPLUS
3385 if (current_namespace != global_namespace) {
3386 error ("Objective-C declarations may only appear in global scope");
3388 #endif /* OBJCPLUS */
3390 if (! objc_is_class_name (identifier))
3392 tree record = lookup_name (identifier), type = record;
3394 if (record)
3396 if (TREE_CODE (record) == TYPE_DECL)
3397 type = DECL_ORIGINAL_TYPE (record)
3398 ? DECL_ORIGINAL_TYPE (record)
3399 : TREE_TYPE (record);
3401 if (!TYPE_HAS_OBJC_INFO (type)
3402 || !TYPE_OBJC_INTERFACE (type))
3404 error ("%qE redeclared as different kind of symbol",
3405 identifier);
3406 error ("previous declaration of %q+D",
3407 record);
3411 record = xref_tag (RECORD_TYPE, identifier);
3412 INIT_TYPE_OBJC_INFO (record);
3413 /* In the case of a @class declaration, we store the ident in
3414 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3415 we'll replace the ident with the interface. */
3416 TYPE_OBJC_INTERFACE (record) = identifier;
3417 objc_map_put (class_name_map, identifier, NULL_TREE);
3421 tree
3422 objc_is_class_name (tree ident)
3424 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3426 tree t = identifier_global_value (ident);
3427 if (t)
3428 ident = t;
3431 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3432 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3434 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3435 ident = OBJC_TYPE_NAME (ident);
3436 #ifdef OBJCPLUS
3437 if (ident && TREE_CODE (ident) == TYPE_DECL)
3439 tree type = TREE_TYPE (ident);
3440 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3441 return NULL_TREE;
3442 ident = DECL_NAME (ident);
3444 #endif
3445 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3446 return NULL_TREE;
3448 if (lookup_interface (ident))
3449 return ident;
3452 tree target;
3454 target = objc_map_get (class_name_map, ident);
3455 if (target != OBJC_MAP_NOT_FOUND)
3456 return ident;
3458 target = objc_map_get (alias_name_map, ident);
3459 if (target != OBJC_MAP_NOT_FOUND)
3460 return target;
3463 return 0;
3466 /* Check whether TYPE is either 'id' or 'Class'. */
3468 tree
3469 objc_is_id (tree type)
3471 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3473 tree t = identifier_global_value (type);
3474 if (t)
3475 type = t;
3478 if (type && TREE_CODE (type) == TYPE_DECL)
3479 type = TREE_TYPE (type);
3481 /* NB: This function may be called before the ObjC front-end has
3482 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3483 return (objc_object_type && type
3484 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3485 ? type
3486 : NULL_TREE);
3489 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3490 class instance. This is needed by other parts of the compiler to
3491 handle ObjC types gracefully. */
3493 tree
3494 objc_is_object_ptr (tree type)
3496 tree ret;
3498 type = TYPE_MAIN_VARIANT (type);
3499 if (!POINTER_TYPE_P (type))
3500 return 0;
3502 ret = objc_is_id (type);
3503 if (!ret)
3504 ret = objc_is_class_name (TREE_TYPE (type));
3506 return ret;
3509 static int
3510 objc_is_gcable_type (tree type, int or_strong_p)
3512 tree name;
3514 if (!TYPE_P (type))
3515 return 0;
3516 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3517 return 1;
3518 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3519 return 1;
3520 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3521 return 0;
3522 type = TREE_TYPE (type);
3523 if (TREE_CODE (type) != RECORD_TYPE)
3524 return 0;
3525 name = TYPE_NAME (type);
3526 return (objc_is_class_name (name) != NULL_TREE);
3529 static tree
3530 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3532 if (expr == oldexpr)
3533 return newexpr;
3535 switch (TREE_CODE (expr))
3537 case COMPONENT_REF:
3538 return objc_build_component_ref
3539 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3540 oldexpr,
3541 newexpr),
3542 DECL_NAME (TREE_OPERAND (expr, 1)));
3543 case ARRAY_REF:
3544 return build_array_ref (input_location,
3545 objc_substitute_decl (TREE_OPERAND (expr, 0),
3546 oldexpr,
3547 newexpr),
3548 TREE_OPERAND (expr, 1));
3549 case INDIRECT_REF:
3550 return build_indirect_ref (input_location,
3551 objc_substitute_decl (TREE_OPERAND (expr, 0),
3552 oldexpr,
3553 newexpr), RO_ARROW);
3554 default:
3555 return expr;
3559 static tree
3560 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3562 tree func_params;
3563 /* The LHS parameter contains the expression 'outervar->memberspec';
3564 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3565 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3567 tree offs
3568 = objc_substitute_decl
3569 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3570 tree func
3571 = (flag_objc_direct_dispatch
3572 ? objc_assign_ivar_fast_decl
3573 : objc_assign_ivar_decl);
3575 offs = convert (integer_type_node, build_unary_op (input_location,
3576 ADDR_EXPR, offs, 0));
3577 offs = fold (offs);
3578 func_params = tree_cons (NULL_TREE,
3579 convert (objc_object_type, rhs),
3580 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3581 tree_cons (NULL_TREE, offs,
3582 NULL_TREE)));
3584 return build_function_call (input_location, func, func_params);
3587 static tree
3588 objc_build_global_assignment (tree lhs, tree rhs)
3590 tree func_params = tree_cons (NULL_TREE,
3591 convert (objc_object_type, rhs),
3592 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3593 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3594 NULL_TREE));
3596 return build_function_call (input_location,
3597 objc_assign_global_decl, func_params);
3600 static tree
3601 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3603 tree func_params = tree_cons (NULL_TREE,
3604 convert (objc_object_type, rhs),
3605 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3606 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3607 NULL_TREE));
3609 return build_function_call (input_location,
3610 objc_assign_strong_cast_decl, func_params);
3613 static int
3614 objc_is_gcable_p (tree expr)
3616 return (TREE_CODE (expr) == COMPONENT_REF
3617 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3618 : TREE_CODE (expr) == ARRAY_REF
3619 ? (objc_is_gcable_p (TREE_TYPE (expr))
3620 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3621 : TREE_CODE (expr) == ARRAY_TYPE
3622 ? objc_is_gcable_p (TREE_TYPE (expr))
3623 : TYPE_P (expr)
3624 ? objc_is_gcable_type (expr, 1)
3625 : (objc_is_gcable_p (TREE_TYPE (expr))
3626 || (DECL_P (expr)
3627 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3630 static int
3631 objc_is_ivar_reference_p (tree expr)
3633 return (TREE_CODE (expr) == ARRAY_REF
3634 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3635 : TREE_CODE (expr) == COMPONENT_REF
3636 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3637 : 0);
3640 static int
3641 objc_is_global_reference_p (tree expr)
3643 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3644 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3645 : DECL_P (expr)
3646 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3647 : 0);
3650 tree
3651 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3653 tree result = NULL_TREE, outer;
3654 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3656 /* This function is currently only used with the next runtime with
3657 garbage collection enabled (-fobjc-gc). */
3658 gcc_assert (flag_next_runtime);
3660 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3661 will have been transformed to the form '*(type *)&expr'. */
3662 if (TREE_CODE (lhs) == INDIRECT_REF)
3664 outer = TREE_OPERAND (lhs, 0);
3666 while (!strong_cast_p
3667 && (CONVERT_EXPR_P (outer)
3668 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3670 tree lhstype = TREE_TYPE (outer);
3672 /* Descend down the cast chain, and record the first objc_gc
3673 attribute found. */
3674 if (POINTER_TYPE_P (lhstype))
3676 tree attr
3677 = lookup_attribute ("objc_gc",
3678 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3680 if (attr)
3681 strong_cast_p = 1;
3684 outer = TREE_OPERAND (outer, 0);
3688 /* If we have a __strong cast, it trumps all else. */
3689 if (strong_cast_p)
3691 if (modifycode != NOP_EXPR)
3692 goto invalid_pointer_arithmetic;
3694 if (warn_assign_intercept)
3695 warning (0, "strong-cast assignment has been intercepted");
3697 result = objc_build_strong_cast_assignment (lhs, rhs);
3699 goto exit_point;
3702 /* the lhs must be of a suitable type, regardless of its underlying
3703 structure. */
3704 if (!objc_is_gcable_p (lhs))
3705 goto exit_point;
3707 outer = lhs;
3709 while (outer
3710 && (TREE_CODE (outer) == COMPONENT_REF
3711 || TREE_CODE (outer) == ARRAY_REF))
3712 outer = TREE_OPERAND (outer, 0);
3714 if (TREE_CODE (outer) == INDIRECT_REF)
3716 outer = TREE_OPERAND (outer, 0);
3717 indirect_p = 1;
3720 outer_gc_p = objc_is_gcable_p (outer);
3722 /* Handle ivar assignments. */
3723 if (objc_is_ivar_reference_p (lhs))
3725 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3726 doesn't cut it here), the best we can do here is suggest a cast. */
3727 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3729 /* We may still be able to use the global write barrier... */
3730 if (!indirect_p && objc_is_global_reference_p (outer))
3731 goto global_reference;
3733 suggest_cast:
3734 if (modifycode == NOP_EXPR)
3736 if (warn_assign_intercept)
3737 warning (0, "strong-cast may possibly be needed");
3740 goto exit_point;
3743 if (modifycode != NOP_EXPR)
3744 goto invalid_pointer_arithmetic;
3746 if (warn_assign_intercept)
3747 warning (0, "instance variable assignment has been intercepted");
3749 result = objc_build_ivar_assignment (outer, lhs, rhs);
3751 goto exit_point;
3754 /* Likewise, intercept assignment to global/static variables if their type is
3755 GC-marked. */
3756 if (objc_is_global_reference_p (outer))
3758 if (indirect_p)
3759 goto suggest_cast;
3761 global_reference:
3762 if (modifycode != NOP_EXPR)
3764 invalid_pointer_arithmetic:
3765 if (outer_gc_p)
3766 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3768 goto exit_point;
3771 if (warn_assign_intercept)
3772 warning (0, "global/static variable assignment has been intercepted");
3774 result = objc_build_global_assignment (lhs, rhs);
3777 /* In all other cases, fall back to the normal mechanism. */
3778 exit_point:
3779 return result;
3782 /* Implementation of the table mapping a class name (as an identifier)
3783 to a class node. The two public functions for it are
3784 lookup_interface() and add_interface(). add_interface() is only
3785 used in this file, so we can make it static. */
3787 static GTY(()) objc_map_t interface_map;
3789 static void
3790 interface_hash_init (void)
3792 interface_map = objc_map_alloc_ggc (200);
3795 static tree
3796 add_interface (tree class_name, tree name)
3798 /* Put interfaces on list in reverse order. */
3799 TREE_CHAIN (class_name) = interface_chain;
3800 interface_chain = class_name;
3802 /* Add it to the map. */
3803 objc_map_put (interface_map, name, class_name);
3805 return interface_chain;
3808 tree
3809 lookup_interface (tree ident)
3811 #ifdef OBJCPLUS
3812 if (ident && TREE_CODE (ident) == TYPE_DECL)
3813 ident = DECL_NAME (ident);
3814 #endif
3816 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3817 return NULL_TREE;
3820 tree interface = objc_map_get (interface_map, ident);
3822 if (interface == OBJC_MAP_NOT_FOUND)
3823 return NULL_TREE;
3824 else
3825 return interface;
3831 /* Implement @defs (<classname>) within struct bodies. */
3833 tree
3834 objc_get_class_ivars (tree class_name)
3836 tree interface = lookup_interface (class_name);
3838 if (interface)
3839 return get_class_ivars (interface, true);
3841 error ("cannot find interface declaration for %qE",
3842 class_name);
3844 return error_mark_node;
3848 /* Functions used by the hashtable for field duplicates in
3849 objc_detect_field_duplicates(). Ideally, we'd use a standard
3850 key-value dictionary hashtable , and store as keys the field names,
3851 and as values the actual declarations (used to print nice error
3852 messages with the locations). But, the hashtable we are using only
3853 allows us to store keys in the hashtable, without values (it looks
3854 more like a set). So, we store the DECLs, but define equality as
3855 DECLs having the same name, and hash as the hash of the name. */
3857 struct decl_name_hash : nofree_ptr_hash <tree_node>
3859 static inline hashval_t hash (const tree_node *);
3860 static inline bool equal (const tree_node *, const tree_node *);
3863 inline hashval_t
3864 decl_name_hash::hash (const tree_node *q)
3866 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3869 inline bool
3870 decl_name_hash::equal (const tree_node *a, const tree_node *b)
3872 return DECL_NAME (a) == DECL_NAME (b);
3875 /* Called when checking the variables in a struct. If we are not
3876 doing the ivars list inside an @interface context, then return
3877 false. Else, perform the check for duplicate ivars, then return
3878 true. The check for duplicates checks if an instance variable with
3879 the same name exists in the class or in a superclass. If
3880 'check_superclasses_only' is set to true, then it is assumed that
3881 checks for instance variables in the same class has already been
3882 performed (this is the case for ObjC++) and only the instance
3883 variables of superclasses are checked. */
3884 bool
3885 objc_detect_field_duplicates (bool check_superclasses_only)
3887 if (!objc_collecting_ivars || !objc_interface_context
3888 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3889 return false;
3891 /* We have two ways of doing this check:
3893 "direct comparison": we iterate over the instance variables and
3894 compare them directly. This works great for small numbers of
3895 instance variables (such as 10 or 20), which are extremely common.
3896 But it will potentially take forever for the pathological case with
3897 a huge number (eg, 10k) of instance variables.
3899 "hashtable": we use a hashtable, which requires a single sweep
3900 through the list of instances variables. This is much slower for a
3901 small number of variables, and we only use it for large numbers.
3903 To decide which one to use, we need to get an idea of how many
3904 instance variables we have to compare. */
3906 unsigned int number_of_ivars_to_check = 0;
3908 tree ivar;
3909 for (ivar = CLASS_RAW_IVARS (objc_interface_context);
3910 ivar; ivar = DECL_CHAIN (ivar))
3912 /* Ignore anonymous ivars. */
3913 if (DECL_NAME (ivar))
3914 number_of_ivars_to_check++;
3918 /* Exit if there is nothing to do. */
3919 if (number_of_ivars_to_check == 0)
3920 return true;
3922 /* In case that there are only 1 or 2 instance variables to check,
3923 we always use direct comparison. If there are more, it is
3924 worth iterating over the instance variables in the superclass
3925 to count how many there are (note that this has the same cost
3926 as checking 1 instance variable by direct comparison, which is
3927 why we skip this check in the case of 1 or 2 ivars and just do
3928 the direct comparison) and then decide if it worth using a
3929 hashtable. */
3930 if (number_of_ivars_to_check > 2)
3932 unsigned int number_of_superclass_ivars = 0;
3934 tree interface;
3935 for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context));
3936 interface; interface = lookup_interface (CLASS_SUPER_NAME (interface)))
3938 tree ivar;
3939 for (ivar = CLASS_RAW_IVARS (interface);
3940 ivar; ivar = DECL_CHAIN (ivar))
3941 number_of_superclass_ivars++;
3945 /* We use a hashtable if we have over 10k comparisons. */
3946 if (number_of_ivars_to_check * (number_of_superclass_ivars
3947 + (number_of_ivars_to_check / 2))
3948 > 10000)
3950 /* First, build the hashtable by putting all the instance
3951 variables of superclasses in it. */
3952 hash_table<decl_name_hash> htab (37);
3953 tree interface;
3954 for (interface = lookup_interface (CLASS_SUPER_NAME
3955 (objc_interface_context));
3956 interface; interface = lookup_interface
3957 (CLASS_SUPER_NAME (interface)))
3959 tree ivar;
3960 for (ivar = CLASS_RAW_IVARS (interface); ivar;
3961 ivar = DECL_CHAIN (ivar))
3963 if (DECL_NAME (ivar) != NULL_TREE)
3965 tree_node **slot = htab.find_slot (ivar, INSERT);
3966 /* Do not check for duplicate instance
3967 variables in superclasses. Errors have
3968 already been generated. */
3969 *slot = ivar;
3974 /* Now, we go through all the instance variables in the
3975 class, and check that they are not in the
3976 hashtable. */
3977 if (check_superclasses_only)
3979 tree ivar;
3980 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
3981 ivar = DECL_CHAIN (ivar))
3983 if (DECL_NAME (ivar) != NULL_TREE)
3985 tree duplicate_ivar = htab.find (ivar);
3986 if (duplicate_ivar != HTAB_EMPTY_ENTRY)
3988 error_at (DECL_SOURCE_LOCATION (ivar),
3989 "duplicate instance variable %q+D",
3990 ivar);
3991 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
3992 "previous declaration of %q+D",
3993 duplicate_ivar);
3994 /* FIXME: Do we need the following ? */
3995 /* DECL_NAME (ivar) = NULL_TREE; */
4000 else
4002 /* If we're checking for duplicates in the class as
4003 well, we insert variables in the hashtable as we
4004 check them, so if a duplicate follows, it will be
4005 caught. */
4006 tree ivar;
4007 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
4008 ivar = DECL_CHAIN (ivar))
4010 if (DECL_NAME (ivar) != NULL_TREE)
4012 tree_node **slot = htab.find_slot (ivar, INSERT);
4013 if (*slot)
4015 tree duplicate_ivar = (tree)(*slot);
4016 error_at (DECL_SOURCE_LOCATION (ivar),
4017 "duplicate instance variable %q+D",
4018 ivar);
4019 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4020 "previous declaration of %q+D",
4021 duplicate_ivar);
4022 /* FIXME: Do we need the following ? */
4023 /* DECL_NAME (ivar) = NULL_TREE; */
4025 *slot = ivar;
4029 return true;
4034 /* This is the "direct comparison" approach, which is used in most
4035 non-pathological cases. */
4037 /* Walk up to class hierarchy, starting with this class (this is
4038 the external loop, because lookup_interface() is expensive, and
4039 we want to do it few times). */
4040 tree interface = objc_interface_context;
4042 if (check_superclasses_only)
4043 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4045 for ( ; interface; interface = lookup_interface
4046 (CLASS_SUPER_NAME (interface)))
4048 tree ivar_being_checked;
4050 for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context);
4051 ivar_being_checked;
4052 ivar_being_checked = DECL_CHAIN (ivar_being_checked))
4054 tree decl;
4056 /* Ignore anonymous ivars. */
4057 if (DECL_NAME (ivar_being_checked) == NULL_TREE)
4058 continue;
4060 /* Note how we stop when we find the ivar we are checking
4061 (this can only happen in the main class, not
4062 superclasses), to avoid comparing things twice
4063 (otherwise, for each ivar, you'd compare A to B then B
4064 to A, and get duplicated error messages). */
4065 for (decl = CLASS_RAW_IVARS (interface);
4066 decl && decl != ivar_being_checked;
4067 decl = DECL_CHAIN (decl))
4069 if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl))
4071 error_at (DECL_SOURCE_LOCATION (ivar_being_checked),
4072 "duplicate instance variable %q+D",
4073 ivar_being_checked);
4074 inform (DECL_SOURCE_LOCATION (decl),
4075 "previous declaration of %q+D",
4076 decl);
4077 /* FIXME: Do we need the following ? */
4078 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4084 return true;
4087 /* Used by: build_private_template, continue_class,
4088 and for @defs constructs. */
4090 static tree
4091 get_class_ivars (tree interface, bool inherited)
4093 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4095 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4096 by the current class (i.e., they do not include super-class ivars).
4097 However, the CLASS_IVARS list will be side-effected by a call to
4098 finish_struct(), which will fill in field offsets. */
4099 if (!CLASS_IVARS (interface))
4100 CLASS_IVARS (interface) = ivar_chain;
4102 if (!inherited)
4103 return ivar_chain;
4105 while (CLASS_SUPER_NAME (interface))
4107 /* Prepend super-class ivars. */
4108 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4109 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4110 ivar_chain);
4113 return ivar_chain;
4116 void
4117 objc_maybe_warn_exceptions (location_t loc)
4119 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4120 For example, on Darwin, ObjC exceptions require a sufficiently
4121 recent version of the runtime, so the user must ask for them
4122 explicitly. On other platforms, at the moment -fobjc-exceptions
4123 triggers -fexceptions which again is required for exceptions to
4124 work. */
4125 if (!flag_objc_exceptions)
4127 /* Warn only once per compilation unit. */
4128 static bool warned = false;
4130 if (!warned)
4132 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4133 warned = true;
4138 static struct objc_try_context *cur_try_context;
4140 /* Called just after parsing the @try and its associated BODY. We now
4141 must prepare for the tricky bits -- handling the catches and finally. */
4143 void
4144 objc_begin_try_stmt (location_t try_locus, tree body)
4146 struct objc_try_context *c = XCNEW (struct objc_try_context);
4147 c->outer = cur_try_context;
4148 c->try_body = body;
4149 c->try_locus = try_locus;
4150 c->end_try_locus = input_location;
4151 cur_try_context = c;
4153 /* Collect the list of local variables. We'll mark them as volatile
4154 at the end of compilation of this function to prevent them being
4155 clobbered by setjmp/longjmp. */
4156 if (flag_objc_sjlj_exceptions)
4157 objc_mark_locals_volatile (NULL);
4160 /* Called just after parsing "@catch (parm)". Open a binding level,
4161 enter DECL into the binding level, and initialize it. Leave the
4162 binding level open while the body of the compound statement is
4163 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4164 which we compile as "@catch(id tmp_variable)". */
4166 void
4167 objc_begin_catch_clause (tree decl)
4169 tree compound, type, t;
4170 bool ellipsis = false;
4172 /* Begin a new scope that the entire catch clause will live in. */
4173 compound = c_begin_compound_stmt (true);
4175 /* Create the appropriate declaration for the argument. */
4176 if (decl == error_mark_node)
4177 type = error_mark_node;
4178 else
4180 if (decl == NULL_TREE)
4182 /* If @catch(...) was specified, create a temporary variable of
4183 type 'id' and use it. */
4184 decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var");
4185 DECL_SOURCE_LOCATION (decl) = input_location;
4186 /* ... but allow the runtime to differentiate between ellipsis and the
4187 case of @catch (id xyz). */
4188 ellipsis = true;
4190 else
4192 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4193 decl = build_decl (input_location,
4194 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4196 lang_hooks.decls.pushdecl (decl);
4198 /* Mark the declaration as used so you never any warnings whether
4199 you use the exception argument or not. TODO: Implement a
4200 -Wunused-exception-parameter flag, which would cause warnings
4201 if exception parameter is not used. */
4202 TREE_USED (decl) = 1;
4203 DECL_READ_P (decl) = 1;
4205 type = TREE_TYPE (decl);
4208 /* Verify that the type of the catch is valid. It must be a pointer
4209 to an Objective-C class, or "id" (which is catch-all). */
4210 if (type == error_mark_node)
4212 ;/* Just keep going. */
4214 else if (!objc_type_valid_for_messaging (type, false))
4216 error ("@catch parameter is not a known Objective-C class type");
4217 type = error_mark_node;
4219 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type))
4220 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type)))
4222 error ("@catch parameter can not be protocol-qualified");
4223 type = error_mark_node;
4225 else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4226 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4227 identify 'id'. */
4229 else
4231 /* If 'type' was built using typedefs, we need to get rid of
4232 them and get a simple pointer to the class. */
4233 bool is_typedef = false;
4234 tree x = TYPE_MAIN_VARIANT (type);
4236 /* Skip from the pointer to the pointee. */
4237 if (TREE_CODE (x) == POINTER_TYPE)
4238 x = TREE_TYPE (x);
4240 /* Traverse typedef aliases */
4241 while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x)
4242 && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL
4243 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)))
4245 is_typedef = true;
4246 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4249 /* If it was a typedef, build a pointer to the final, original
4250 class. */
4251 if (is_typedef)
4252 type = build_pointer_type (x);
4254 if (cur_try_context->catch_list)
4256 /* Examine previous @catch clauses and see if we've already
4257 caught the type in question. */
4258 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4259 for (; !tsi_end_p (i); tsi_next (&i))
4261 tree stmt = tsi_stmt (i);
4262 t = CATCH_TYPES (stmt);
4263 if (t == error_mark_node)
4264 continue;
4265 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4267 warning (0, "exception of type %<%T%> will be caught",
4268 TREE_TYPE (type));
4269 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4270 TREE_TYPE (t ? t : objc_object_type));
4271 break;
4277 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4278 add_stmt (t);
4281 /* Called just after parsing the closing brace of a @catch clause. Close
4282 the open binding level, and record a CATCH_EXPR for it. */
4284 void
4285 objc_finish_catch_clause (void)
4287 tree c = cur_try_context->current_catch;
4288 cur_try_context->current_catch = NULL;
4289 cur_try_context->end_catch_locus = input_location;
4291 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4293 (*runtime.finish_catch) (&cur_try_context, c);
4296 /* Called after parsing a @finally clause and its associated BODY.
4297 Record the body for later placement. */
4299 void
4300 objc_build_finally_clause (location_t finally_locus, tree body)
4302 cur_try_context->finally_body = body;
4303 cur_try_context->finally_locus = finally_locus;
4304 cur_try_context->end_finally_locus = input_location;
4307 /* Called to finalize a @try construct. */
4309 tree
4310 objc_finish_try_stmt (void)
4312 struct objc_try_context *c = cur_try_context;
4313 tree stmt;
4315 if (c->catch_list == NULL && c->finally_body == NULL)
4316 error ("%<@try%> without %<@catch%> or %<@finally%>");
4318 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4319 add_stmt (stmt);
4321 cur_try_context = c->outer;
4322 free (c);
4323 return stmt;
4326 tree
4327 objc_build_throw_stmt (location_t loc, tree throw_expr)
4329 bool rethrown = false;
4331 objc_maybe_warn_exceptions (loc);
4333 /* Don't waste time trying to build something if we're already dead. */
4334 if (throw_expr == error_mark_node)
4335 return error_mark_node;
4337 if (throw_expr == NULL)
4339 /* If we're not inside a @catch block, there is no "current
4340 exception" to be rethrown. */
4341 if (cur_try_context == NULL
4342 || cur_try_context->current_catch == NULL)
4344 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4345 return error_mark_node;
4348 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4349 value that we get from the runtime. */
4350 throw_expr = (*runtime.build_exc_ptr) (&cur_try_context);
4351 rethrown = true;
4353 else
4355 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
4357 error_at (loc, "%<@throw%> argument is not an object");
4358 return error_mark_node;
4362 return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown);
4365 tree
4366 objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
4368 /* object_expr should never be NULL; but in case it is, convert it to
4369 error_mark_node. */
4370 if (object_expr == NULL)
4371 object_expr = error_mark_node;
4373 /* Validate object_expr. If not valid, set it to error_mark_node. */
4374 if (object_expr != error_mark_node)
4376 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true))
4378 error_at (start_locus, "%<@synchronized%> argument is not an object");
4379 object_expr = error_mark_node;
4383 if (object_expr == error_mark_node)
4385 /* If we found an error, we simply ignore the '@synchronized'.
4386 Compile the body so we can keep going with minimal
4387 casualties. */
4388 return add_stmt (body);
4390 else
4392 tree call;
4393 tree args;
4395 /* objc_sync_enter (object_expr); */
4396 object_expr = save_expr (object_expr);
4397 args = tree_cons (NULL, object_expr, NULL);
4398 call = build_function_call (input_location,
4399 objc_sync_enter_decl, args);
4400 SET_EXPR_LOCATION (call, start_locus);
4401 add_stmt (call);
4403 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4404 it goes inside the @finalize() clause. */
4405 args = tree_cons (NULL, object_expr, NULL);
4406 call = build_function_call (input_location,
4407 objc_sync_exit_decl, args);
4408 SET_EXPR_LOCATION (call, input_location);
4410 /* @try { body; } */
4411 objc_begin_try_stmt (start_locus, body);
4413 /* @finally { objc_sync_exit (object_expr); } */
4414 objc_build_finally_clause (input_location, call);
4416 /* End of try statement. */
4417 return objc_finish_try_stmt ();
4421 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4422 name as the class:
4424 struct <classname> {
4425 struct _objc_class *isa;
4427 }; */
4429 static void
4430 build_private_template (tree klass)
4432 if (!CLASS_STATIC_TEMPLATE (klass))
4434 tree record = objc_build_struct (klass,
4435 get_class_ivars (klass, false),
4436 CLASS_SUPER_NAME (klass));
4438 /* Set the TREE_USED bit for this struct, so that stab generator
4439 can emit stabs for this struct type. */
4440 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4441 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4443 /* Copy the attributes from the class to the type. */
4444 if (TREE_DEPRECATED (klass))
4445 TREE_DEPRECATED (record) = 1;
4449 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4450 current class. */
4451 #ifdef OBJCPLUS
4452 static void
4453 objc_generate_cxx_ctor_or_dtor (bool dtor)
4455 tree fn, body, compound_stmt, ivar;
4457 /* - (id) .cxx_construct { ... return self; } */
4458 /* - (void) .cxx_construct { ... } */
4460 objc_start_method_definition
4461 (false /* is_class_method */,
4462 objc_build_method_signature (false /* is_class_method */,
4463 build_tree_list (NULL_TREE,
4464 dtor
4465 ? void_type_node
4466 : objc_object_type),
4467 get_identifier (dtor
4468 ? TAG_CXX_DESTRUCT
4469 : TAG_CXX_CONSTRUCT),
4470 make_node (TREE_LIST),
4471 false), NULL, NULL_TREE);
4472 body = begin_function_body ();
4473 compound_stmt = begin_compound_stmt (0);
4475 ivar = CLASS_IVARS (implementation_template);
4476 /* Destroy ivars in reverse order. */
4477 if (dtor)
4478 ivar = nreverse (copy_list (ivar));
4480 for (; ivar; ivar = TREE_CHAIN (ivar))
4482 if (TREE_CODE (ivar) == FIELD_DECL)
4484 tree type = TREE_TYPE (ivar);
4486 /* Call the ivar's default constructor or destructor. Do not
4487 call the destructor unless a corresponding constructor call
4488 has also been made (or is not needed). */
4489 if (MAYBE_CLASS_TYPE_P (type)
4490 && (dtor
4491 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4492 && (!TYPE_NEEDS_CONSTRUCTING (type)
4493 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4494 : (TYPE_NEEDS_CONSTRUCTING (type)
4495 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4496 finish_expr_stmt
4497 (build_special_member_call
4498 (build_ivar_reference (DECL_NAME (ivar)),
4499 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4500 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
4504 /* The constructor returns 'self'. */
4505 if (!dtor)
4506 finish_return_stmt (self_decl);
4508 finish_compound_stmt (compound_stmt);
4509 finish_function_body (body);
4510 fn = current_function_decl;
4511 finish_function ();
4512 objc_finish_method_definition (fn);
4515 /* The following routine will examine the current @interface for any
4516 non-POD C++ ivars requiring non-trivial construction and/or
4517 destruction, and then synthesize special '- .cxx_construct' and/or
4518 '- .cxx_destruct' methods which will run the appropriate
4519 construction or destruction code. Note that ivars inherited from
4520 super-classes are _not_ considered. */
4521 static void
4522 objc_generate_cxx_cdtors (void)
4524 bool need_ctor = false, need_dtor = false;
4525 tree ivar;
4527 /* Error case, due to possibly an extra @end. */
4528 if (!objc_implementation_context)
4529 return;
4531 /* We do not want to do this for categories, since they do not have
4532 their own ivars. */
4534 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4535 return;
4537 /* First, determine if we even need a constructor and/or destructor. */
4539 for (ivar = CLASS_IVARS (implementation_template); ivar;
4540 ivar = TREE_CHAIN (ivar))
4542 if (TREE_CODE (ivar) == FIELD_DECL)
4544 tree type = TREE_TYPE (ivar);
4546 if (MAYBE_CLASS_TYPE_P (type))
4548 if (TYPE_NEEDS_CONSTRUCTING (type)
4549 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4550 /* NB: If a default constructor is not available, we will not
4551 be able to initialize this ivar; the add_instance_variable()
4552 routine will already have warned about this. */
4553 need_ctor = true;
4555 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4556 && (!TYPE_NEEDS_CONSTRUCTING (type)
4557 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4558 /* NB: If a default constructor is not available, we will not
4559 call the destructor either, for symmetry. */
4560 need_dtor = true;
4565 /* Generate '- .cxx_construct' if needed. */
4567 if (need_ctor)
4568 objc_generate_cxx_ctor_or_dtor (false);
4570 /* Generate '- .cxx_destruct' if needed. */
4572 if (need_dtor)
4573 objc_generate_cxx_ctor_or_dtor (true);
4575 /* The 'imp_list' variable points at an imp_entry record for the current
4576 @implementation. Record the existence of '- .cxx_construct' and/or
4577 '- .cxx_destruct' methods therein; it will be included in the
4578 metadata for the class if the runtime needs it. */
4579 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
4581 #endif
4583 static void
4584 error_with_ivar (const char *message, tree decl)
4586 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4587 message, identifier_to_locale (gen_declaration (decl)));
4591 static void
4592 check_ivars (tree inter, tree imp)
4594 tree intdecls = CLASS_RAW_IVARS (inter);
4595 tree impdecls = CLASS_RAW_IVARS (imp);
4597 while (1)
4599 tree t1, t2;
4601 #ifdef OBJCPLUS
4602 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4603 intdecls = TREE_CHAIN (intdecls);
4604 #endif
4605 if (intdecls == 0 && impdecls == 0)
4606 break;
4607 if (intdecls == 0 || impdecls == 0)
4609 error ("inconsistent instance variable specification");
4610 break;
4613 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
4615 if (!comptypes (t1, t2)
4616 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4617 DECL_INITIAL (impdecls)))
4619 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4621 error_with_ivar ("conflicting instance variable type",
4622 impdecls);
4623 error_with_ivar ("previous declaration of",
4624 intdecls);
4626 else /* both the type and the name don't match */
4628 error ("inconsistent instance variable specification");
4629 break;
4633 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4635 error_with_ivar ("conflicting instance variable name",
4636 impdecls);
4637 error_with_ivar ("previous declaration of",
4638 intdecls);
4641 intdecls = DECL_CHAIN (intdecls);
4642 impdecls = DECL_CHAIN (impdecls);
4647 static void
4648 mark_referenced_methods (void)
4650 struct imp_entry *impent;
4651 tree chain;
4653 for (impent = imp_list; impent; impent = impent->next)
4655 chain = CLASS_CLS_METHODS (impent->imp_context);
4656 while (chain)
4658 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4659 chain = DECL_CHAIN (chain);
4662 chain = CLASS_NST_METHODS (impent->imp_context);
4663 while (chain)
4665 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4666 chain = DECL_CHAIN (chain);
4671 /* If type is empty or only type qualifiers are present, add default
4672 type of id (otherwise grokdeclarator will default to int). */
4673 static inline tree
4674 adjust_type_for_id_default (tree type)
4676 if (!type)
4677 type = make_node (TREE_LIST);
4679 if (!TREE_VALUE (type))
4680 TREE_VALUE (type) = objc_object_type;
4681 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4682 && TYPED_OBJECT (TREE_VALUE (type)))
4683 error ("can not use an object as parameter to a method");
4685 return type;
4688 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4689 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4690 OBJC_METHOD_PARM_DECL ?)
4692 A KEYWORD_DECL is a tree representing the declaration of a
4693 parameter of an Objective-C method. It is produced when parsing a
4694 fragment of Objective-C method declaration of the form
4696 keyworddecl:
4697 selector ':' '(' typename ')' identifier
4699 For example, take the Objective-C method
4701 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4703 the two fragments "pathForResource:(NSString *)resource" and
4704 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4705 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4706 "pathForResource"), the 'arg_type' (eg, tree representing a
4707 NSString *), the 'arg_name' (eg identifier for "resource") and
4708 potentially some attributes (for example, a tree representing
4709 __attribute__ ((unused)) if such an attribute was attached to a
4710 certain parameter). You can access this information using the
4711 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4712 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4714 'key_name' is an identifier node (and is optional as you can omit
4715 it in Objective-C methods).
4716 'arg_type' is a tree list (and is optional too if no parameter type
4717 was specified).
4718 'arg_name' is an identifier node and is required.
4719 'attributes' is an optional tree containing parameter attributes. */
4720 tree
4721 objc_build_keyword_decl (tree key_name, tree arg_type,
4722 tree arg_name, tree attributes)
4724 tree keyword_decl;
4726 if (flag_objc1_only && attributes)
4727 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4729 /* If no type is specified, default to "id". */
4730 arg_type = adjust_type_for_id_default (arg_type);
4732 keyword_decl = make_node (KEYWORD_DECL);
4734 TREE_TYPE (keyword_decl) = arg_type;
4735 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4736 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4737 DECL_ATTRIBUTES (keyword_decl) = attributes;
4739 return keyword_decl;
4742 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4743 static tree
4744 build_keyword_selector (tree selector)
4746 int len = 0;
4747 tree key_chain, key_name;
4748 char *buf;
4750 /* Scan the selector to see how much space we'll need. */
4751 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4753 switch (TREE_CODE (selector))
4755 case KEYWORD_DECL:
4756 key_name = KEYWORD_KEY_NAME (key_chain);
4757 break;
4758 case TREE_LIST:
4759 key_name = TREE_PURPOSE (key_chain);
4760 break;
4761 default:
4762 gcc_unreachable ();
4765 if (key_name)
4766 len += IDENTIFIER_LENGTH (key_name) + 1;
4767 else
4768 /* Just a ':' arg. */
4769 len++;
4772 buf = (char *) alloca (len + 1);
4773 /* Start the buffer out as an empty string. */
4774 buf[0] = '\0';
4776 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4778 switch (TREE_CODE (selector))
4780 case KEYWORD_DECL:
4781 key_name = KEYWORD_KEY_NAME (key_chain);
4782 break;
4783 case TREE_LIST:
4784 key_name = TREE_PURPOSE (key_chain);
4785 /* The keyword decl chain will later be used as a function
4786 argument chain. Unhook the selector itself so as to not
4787 confuse other parts of the compiler. */
4788 TREE_PURPOSE (key_chain) = NULL_TREE;
4789 break;
4790 default:
4791 gcc_unreachable ();
4794 if (key_name)
4795 strcat (buf, IDENTIFIER_POINTER (key_name));
4796 strcat (buf, ":");
4799 return get_identifier_with_length (buf, len);
4802 /* Used for declarations and definitions. */
4804 static tree
4805 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4806 tree add_args, bool ellipsis)
4808 tree method_decl;
4810 /* If no type is specified, default to "id". */
4811 ret_type = adjust_type_for_id_default (ret_type);
4813 /* Note how a method_decl has a TREE_TYPE which is not the function
4814 type of the function implementing the method, but only the return
4815 type of the method. We may want to change this, and store the
4816 entire function type in there (eg, it may be used to simplify
4817 dealing with attributes below). */
4818 method_decl = make_node (code);
4819 TREE_TYPE (method_decl) = ret_type;
4821 /* If we have a keyword selector, create an identifier_node that
4822 represents the full selector name (`:' included)... */
4823 if (TREE_CODE (selector) == KEYWORD_DECL)
4825 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4826 METHOD_SEL_ARGS (method_decl) = selector;
4827 METHOD_ADD_ARGS (method_decl) = add_args;
4828 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4830 else
4832 METHOD_SEL_NAME (method_decl) = selector;
4833 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4834 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4837 return method_decl;
4840 /* This routine processes objective-c method attributes. */
4842 static void
4843 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4845 /* TODO: Replace the hackery below. An idea would be to store the
4846 full function type in the method declaration (for example in
4847 TREE_TYPE) and then expose ObjC method declarations to c-family
4848 and they could deal with them by simply treating them as
4849 functions. */
4851 /* Because of the dangers in the hackery below, we filter out any
4852 attribute that we do not know about. For the ones we know about,
4853 we know that they work with the hackery. For the other ones,
4854 there is no guarantee, so we have to filter them out. */
4855 tree filtered_attributes = NULL_TREE;
4857 if (attributes)
4859 tree attribute;
4860 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4862 tree name = TREE_PURPOSE (attribute);
4864 if (is_attribute_p ("deprecated", name)
4865 || is_attribute_p ("sentinel", name)
4866 || is_attribute_p ("noreturn", name))
4868 /* An attribute that we support; add it to the filtered
4869 attributes. */
4870 filtered_attributes = chainon (filtered_attributes,
4871 copy_node (attribute));
4873 else if (is_attribute_p ("format", name))
4875 /* "format" is special because before adding it to the
4876 filtered attributes we need to adjust the specified
4877 format by adding the hidden function parameters for
4878 an Objective-C method (self, _cmd). */
4879 tree new_attribute = copy_node (attribute);
4881 /* Check the arguments specified with the attribute, and
4882 modify them adding 2 for the two hidden arguments.
4883 Note how this differs from C++; according to the
4884 specs, C++ does not do it so you have to add the +1
4885 yourself. For Objective-C, instead, the compiler
4886 adds the +2 for you. */
4888 /* The attribute arguments have not been checked yet, so
4889 we need to be careful as they could be missing or
4890 invalid. If anything looks wrong, we skip the
4891 process and the compiler will complain about it later
4892 when it validates the attribute. */
4893 /* Check that we have at least three arguments. */
4894 if (TREE_VALUE (new_attribute)
4895 && TREE_CHAIN (TREE_VALUE (new_attribute))
4896 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4898 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4899 tree third_argument = TREE_CHAIN (second_argument);
4900 tree number;
4902 /* This is the second argument, the "string-index",
4903 which specifies the index of the format string
4904 argument. Add 2. */
4905 number = TREE_VALUE (second_argument);
4906 if (number
4907 && TREE_CODE (number) == INTEGER_CST
4908 && !wi::eq_p (number, 0))
4909 TREE_VALUE (second_argument)
4910 = wide_int_to_tree (TREE_TYPE (number),
4911 wi::add (number, 2));
4913 /* This is the third argument, the "first-to-check",
4914 which specifies the index of the first argument to
4915 check. This could be 0, meaning it is not available,
4916 in which case we don't need to add 2. Add 2 if not
4917 0. */
4918 number = TREE_VALUE (third_argument);
4919 if (number
4920 && TREE_CODE (number) == INTEGER_CST
4921 && !wi::eq_p (number, 0))
4922 TREE_VALUE (third_argument)
4923 = wide_int_to_tree (TREE_TYPE (number),
4924 wi::add (number, 2));
4926 filtered_attributes = chainon (filtered_attributes,
4927 new_attribute);
4929 else if (is_attribute_p ("nonnull", name))
4931 /* We need to fixup all the argument indexes by adding 2
4932 for the two hidden arguments of an Objective-C method
4933 invocation, similat to what we do above for the
4934 "format" attribute. */
4935 /* FIXME: This works great in terms of implementing the
4936 functionality, but the warnings that are produced by
4937 nonnull do mention the argument index (while the
4938 format ones don't). For example, you could get
4939 "warning: null argument where non-null required
4940 (argument 3)". Now in that message, "argument 3"
4941 includes the 2 hidden arguments; it would be much
4942 more friendly to call it "argument 1", as that would
4943 be consistent with __attribute__ ((nonnnull (1))).
4944 To do this, we'd need to have the C family code that
4945 checks the arguments know about adding/removing 2 to
4946 the argument index ... or alternatively we could
4947 maybe store the "printable" argument index in
4948 addition to the actual argument index ? Some
4949 refactoring is needed to do this elegantly. */
4950 tree new_attribute = copy_node (attribute);
4951 tree argument = TREE_VALUE (attribute);
4952 while (argument != NULL_TREE)
4954 /* Get the value of the argument and add 2. */
4955 tree number = TREE_VALUE (argument);
4956 if (number && TREE_CODE (number) == INTEGER_CST
4957 && !wi::eq_p (number, 0))
4958 TREE_VALUE (argument)
4959 = wide_int_to_tree (TREE_TYPE (number),
4960 wi::add (number, 2));
4961 argument = TREE_CHAIN (argument);
4964 filtered_attributes = chainon (filtered_attributes,
4965 new_attribute);
4967 else
4968 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
4972 if (filtered_attributes)
4974 /* This hackery changes the TREE_TYPE of the ObjC method
4975 declaration to be a function type, so that decl_attributes
4976 will treat the ObjC method as if it was a function. Some
4977 attributes (sentinel, format) will be applied to the function
4978 type, changing it in place; so after calling decl_attributes,
4979 we extract the function type attributes and store them in
4980 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4981 deprecated) are applied directly to the method declaration
4982 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4983 is nothing to do. */
4984 tree saved_type = TREE_TYPE (*node);
4985 TREE_TYPE (*node)
4986 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
4987 METHOD_REF, 0);
4988 decl_attributes (node, filtered_attributes, flags);
4989 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
4990 TREE_TYPE (*node) = saved_type;
4994 bool
4995 objc_method_decl (enum tree_code opcode)
4997 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
5000 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
5001 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
5002 method or calling one. SUPER_FLAG indicates whether this is a send
5003 to super; this makes a difference for the NeXT calling sequence in
5004 which the lookup and the method call are done together. If METHOD is
5005 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5006 represented as varargs. */
5008 tree
5009 build_function_type_for_method (tree return_type, tree method,
5010 int context, bool super_flag)
5012 vec<tree, va_gc> *argtypes = make_tree_vector ();
5013 tree t, ftype;
5014 bool is_varargs = false;
5016 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5018 /* No actual method prototype given; remaining args passed as varargs. */
5019 if (method == NULL_TREE)
5021 is_varargs = true;
5022 goto build_ftype;
5025 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5027 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5029 /* Decay argument types for the underlying C function as
5030 appropriate. */
5031 arg_type = objc_decay_parm_type (arg_type);
5033 vec_safe_push (argtypes, arg_type);
5036 if (METHOD_ADD_ARGS (method))
5038 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5039 t; t = TREE_CHAIN (t))
5041 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5043 arg_type = objc_decay_parm_type (arg_type);
5045 vec_safe_push (argtypes, arg_type);
5048 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5049 is_varargs = true;
5052 build_ftype:
5053 if (is_varargs)
5054 ftype = build_varargs_function_type_vec (return_type, argtypes);
5055 else
5056 ftype = build_function_type_vec (return_type, argtypes);
5058 release_tree_vector (argtypes);
5059 return ftype;
5062 /* The 'method' argument is a tree; this tree could either be a single
5063 method, which is returned, or could be a TREE_VEC containing a list
5064 of methods. In that case, the first one is returned, and warnings
5065 are issued as appropriate. */
5066 static tree
5067 check_duplicates (tree method, int methods, int is_class)
5069 tree first_method;
5070 size_t i;
5072 if (method == NULL_TREE)
5073 return NULL_TREE;
5075 if (TREE_CODE (method) != TREE_VEC)
5076 return method;
5078 /* We have two or more methods with the same name but different
5079 types. */
5080 first_method = TREE_VEC_ELT (method, 0);
5082 /* But just how different are those types? If
5083 -Wno-strict-selector-match is specified, we shall not complain if
5084 the differences are solely among types with identical size and
5085 alignment. */
5086 if (!warn_strict_selector_match)
5088 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5089 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5090 goto issue_warning;
5092 return first_method;
5095 issue_warning:
5096 if (methods)
5098 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5100 warning_at (input_location, 0,
5101 "multiple methods named %<%c%E%> found",
5102 (is_class ? '+' : '-'),
5103 METHOD_SEL_NAME (first_method));
5104 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5105 (type ? '-' : '+'),
5106 identifier_to_locale (gen_method_decl (first_method)));
5108 else
5110 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5112 warning_at (input_location, 0,
5113 "multiple selectors named %<%c%E%> found",
5114 (is_class ? '+' : '-'),
5115 METHOD_SEL_NAME (first_method));
5116 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5117 (type ? '-' : '+'),
5118 identifier_to_locale (gen_method_decl (first_method)));
5121 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5123 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5125 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5126 (type ? '-' : '+'),
5127 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5130 return first_method;
5133 /* If RECEIVER is a class reference, return the identifier node for
5134 the referenced class. RECEIVER is created by objc_get_class_reference,
5135 so we check the exact form created depending on which runtimes are
5136 used. */
5138 static tree
5139 receiver_is_class_object (tree receiver, int self, int super)
5141 tree exp, arg;
5143 /* The receiver is 'self' or 'super' in the context of a class method. */
5144 if (objc_method_context
5145 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5146 && (self || super))
5147 return (super
5148 ? CLASS_SUPER_NAME (implementation_template)
5149 : CLASS_NAME (implementation_template));
5151 /* The runtime might encapsulate things its own way. */
5152 exp = (*runtime.receiver_is_class_object) (receiver);
5153 if (exp)
5154 return exp;
5156 /* The receiver is a function call that returns an id. Check if
5157 it is a call to objc_getClass, if so, pick up the class name.
5159 This is required by the GNU runtime, which compiles
5161 [NSObject alloc]
5163 into
5165 [objc_get_class ("NSObject") alloc];
5167 and then, to check that the receiver responds to the +alloc
5168 method, needs to be able to determine that the objc_get_class()
5169 call returns the NSObject class and not just a generic Class
5170 pointer.
5172 But, traditionally this is enabled for all runtimes, not just the
5173 GNU one, which means that the compiler is smarter than you'd
5174 expect when dealing with objc_getClass(). For example, with the
5175 Apple runtime, in the code
5177 [objc_getClass ("NSObject") alloc];
5179 the compiler will recognize the objc_getClass() call as special
5180 (due to the code below) and so will know that +alloc is called on
5181 the 'NSObject' class, and can perform the corresponding checks.
5183 Programmers can disable this behaviour by casting the results of
5184 objc_getClass() to 'Class' (this may seem weird because
5185 objc_getClass() is already declared to return 'Class', but the
5186 compiler treats it as a special function). This may be useful if
5187 the class is never declared, and the compiler would complain
5188 about a missing @interface for it. Then, you can do
5190 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5192 to silence the warnings. */
5193 if (TREE_CODE (receiver) == CALL_EXPR
5194 && (exp = CALL_EXPR_FN (receiver))
5195 && TREE_CODE (exp) == ADDR_EXPR
5196 && (exp = TREE_OPERAND (exp, 0))
5197 && TREE_CODE (exp) == FUNCTION_DECL
5198 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5199 prototypes for objc_get_class(). Thankfully, they seem to share the
5200 same function type. */
5201 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5202 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5203 /* We have a call to objc_get_class/objc_getClass! */
5204 && (arg = CALL_EXPR_ARG (receiver, 0)))
5206 STRIP_NOPS (arg);
5207 if (TREE_CODE (arg) == ADDR_EXPR
5208 && (arg = TREE_OPERAND (arg, 0))
5209 && TREE_CODE (arg) == STRING_CST)
5210 /* Finally, we have the class name. */
5211 return get_identifier (TREE_STRING_POINTER (arg));
5213 return 0;
5216 /* If we are currently building a message expr, this holds
5217 the identifier of the selector of the message. This is
5218 used when printing warnings about argument mismatches. */
5220 static tree current_objc_message_selector = 0;
5222 tree
5223 objc_message_selector (void)
5225 return current_objc_message_selector;
5228 /* Construct an expression for sending a message.
5229 MESS has the object to send to in TREE_PURPOSE
5230 and the argument list (including selector) in TREE_VALUE.
5232 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5233 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5235 tree
5236 objc_build_message_expr (tree receiver, tree message_args)
5238 tree sel_name;
5239 #ifdef OBJCPLUS
5240 tree args = TREE_PURPOSE (message_args);
5241 #else
5242 tree args = message_args;
5243 #endif
5244 tree method_params = NULL_TREE;
5246 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5247 return error_mark_node;
5249 /* Obtain the full selector name. */
5250 switch (TREE_CODE (args))
5252 case IDENTIFIER_NODE:
5253 /* A unary selector. */
5254 sel_name = args;
5255 break;
5256 case TREE_LIST:
5257 sel_name = build_keyword_selector (args);
5258 break;
5259 default:
5260 gcc_unreachable ();
5263 /* Build the parameter list to give to the method. */
5264 if (TREE_CODE (args) == TREE_LIST)
5265 #ifdef OBJCPLUS
5266 method_params = chainon (args, TREE_VALUE (message_args));
5267 #else
5269 tree chain = args, prev = NULL_TREE;
5271 /* We have a keyword selector--check for comma expressions. */
5272 while (chain)
5274 tree element = TREE_VALUE (chain);
5276 /* We have a comma expression, must collapse... */
5277 if (TREE_CODE (element) == TREE_LIST)
5279 if (prev)
5280 TREE_CHAIN (prev) = element;
5281 else
5282 args = element;
5284 prev = chain;
5285 chain = TREE_CHAIN (chain);
5287 method_params = args;
5289 #endif
5291 #ifdef OBJCPLUS
5292 if (processing_template_decl)
5293 /* Must wait until template instantiation time. */
5294 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5295 sel_name, method_params);
5296 #endif
5298 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5301 /* Look up method SEL_NAME that would be suitable for receiver
5302 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5303 nonzero), and report on any duplicates. */
5305 static tree
5306 lookup_method_in_hash_lists (tree sel_name, int is_class)
5308 tree method_prototype = OBJC_MAP_NOT_FOUND;
5310 if (!is_class)
5311 method_prototype = objc_map_get (instance_method_map, sel_name);
5313 if (method_prototype == OBJC_MAP_NOT_FOUND)
5315 method_prototype = objc_map_get (class_method_map, sel_name);
5316 is_class = 1;
5318 if (method_prototype == OBJC_MAP_NOT_FOUND)
5319 return NULL_TREE;
5322 return check_duplicates (method_prototype, 1, is_class);
5325 /* The 'objc_finish_message_expr' routine is called from within
5326 'objc_build_message_expr' for non-template functions. In the case of
5327 C++ template functions, it is called from 'build_expr_from_tree'
5328 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5330 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5331 if the method being used is deprecated. If it is not NULL, instead
5332 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5333 prototype that was used and is deprecated. This is useful for
5334 getter calls that are always generated when compiling dot-syntax
5335 expressions, even if they may not be used. In that case, we don't
5336 want the warning immediately; we produce it (if needed) at gimplify
5337 stage when we are sure that the deprecated getter is being
5338 used. */
5339 tree
5340 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5341 tree *deprecated_method_prototype)
5343 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5344 tree retval, class_tree;
5345 int self, super, have_cast;
5347 /* We have used the receiver, so mark it as read. */
5348 mark_exp_read (receiver);
5350 /* Extract the receiver of the message, as well as its type
5351 (where the latter may take the form of a cast or be inferred
5352 from the implementation context). */
5353 rtype = receiver;
5354 while (TREE_CODE (rtype) == COMPOUND_EXPR
5355 || TREE_CODE (rtype) == MODIFY_EXPR
5356 || CONVERT_EXPR_P (rtype)
5357 || TREE_CODE (rtype) == COMPONENT_REF)
5358 rtype = TREE_OPERAND (rtype, 0);
5360 /* self is 1 if this is a message to self, 0 otherwise */
5361 self = (rtype == self_decl);
5363 /* super is 1 if this is a message to super, 0 otherwise. */
5364 super = (rtype == UOBJC_SUPER_decl);
5366 /* rtype is the type of the receiver. */
5367 rtype = TREE_TYPE (receiver);
5369 /* have_cast is 1 if the receiver is casted. */
5370 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5371 || (TREE_CODE (receiver) == COMPOUND_EXPR
5372 && !IS_SUPER (rtype)));
5374 /* If we are calling [super dealloc], reset our warning flag. */
5375 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5376 should_call_super_dealloc = 0;
5378 /* If the receiver is a class object, retrieve the corresponding
5379 @interface, if one exists. class_tree is the class name
5380 identifier, or NULL_TREE if this is not a class method or the
5381 class name could not be determined (as in the case "Class c; [c
5382 method];"). */
5383 class_tree = receiver_is_class_object (receiver, self, super);
5385 /* Now determine the receiver type (if an explicit cast has not been
5386 provided). */
5387 if (!have_cast)
5389 if (class_tree)
5391 /* We are here when we have no cast, and we have a class
5392 name. So, this is a plain method to a class object, as
5393 in [NSObject alloc]. Find the interface corresponding to
5394 the class name. */
5395 rtype = lookup_interface (class_tree);
5397 if (rtype == NULL_TREE)
5399 /* If 'rtype' is NULL_TREE at this point it means that
5400 we have seen no @interface corresponding to that
5401 class name, only a @class declaration (alternatively,
5402 this was a call such as [objc_getClass("SomeClass")
5403 alloc], where we've never seen the @interface of
5404 SomeClass). So, we have a class name (class_tree)
5405 but no actual details of the class methods. We won't
5406 be able to check that the class responds to the
5407 method, and we will have to guess the method
5408 prototype. Emit a warning, then keep going (this
5409 will use any method with a matching name, as if the
5410 receiver was of type 'Class'). */
5411 warning (0, "@interface of class %qE not found", class_tree);
5414 /* Handle `self' and `super'. */
5415 else if (super)
5417 if (!CLASS_SUPER_NAME (implementation_template))
5419 error ("no super class declared in @interface for %qE",
5420 CLASS_NAME (implementation_template));
5421 return error_mark_node;
5423 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5425 else if (self)
5426 rtype = lookup_interface (CLASS_NAME (implementation_template));
5429 if (objc_is_id (rtype))
5431 /* The receiver is of type 'id' or 'Class' (with or without some
5432 protocols attached to it). */
5434 /* We set class_tree to the identifier for 'Class' if this is a
5435 class method, and to NULL_TREE if not. */
5436 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5438 /* 'rprotos' is the list of protocols that the receiver
5439 supports. */
5440 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5441 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5442 : NULL_TREE);
5444 /* We have no information on the type, and we set it to
5445 NULL_TREE. */
5446 rtype = NULL_TREE;
5448 /* If there are any protocols, check that the method we are
5449 calling appears in the protocol list. If there are no
5450 protocols, this is a message to 'id' or 'Class' and we accept
5451 any method that exists. */
5452 if (rprotos)
5454 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5455 search in protocols themselves for the method
5456 prototype. */
5457 method_prototype
5458 = lookup_method_in_protocol_list (rprotos, sel_name,
5459 class_tree != NULL_TREE);
5461 /* If messaging 'Class <Proto>' but did not find a class
5462 method prototype, search for an instance method instead,
5463 and warn about having done so. */
5464 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5466 method_prototype
5467 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5469 if (method_prototype)
5470 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5471 sel_name, sel_name);
5475 else if (rtype)
5477 /* We have a receiver type which is more specific than 'id' or
5478 'Class'. */
5479 tree orig_rtype = rtype;
5481 if (TREE_CODE (rtype) == POINTER_TYPE)
5482 rtype = TREE_TYPE (rtype);
5483 /* Traverse typedef aliases */
5484 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5485 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5486 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5487 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5488 if (TYPED_OBJECT (rtype))
5490 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5491 rtype = TYPE_OBJC_INTERFACE (rtype);
5493 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5495 /* If we could not find an @interface declaration, we must
5496 have only seen a @class declaration; so, we cannot say
5497 anything more intelligent about which methods the
5498 receiver will understand. Note that this only happens
5499 for instance methods; for class methods to a class where
5500 we have only seen a @class declaration,
5501 lookup_interface() above would have set rtype to
5502 NULL_TREE. */
5503 if (rprotos)
5505 /* We could not find an @interface declaration, yet, if
5506 there are protocols attached to the type, we can
5507 still look up the method in the protocols. Ie, we
5508 are in the following case:
5510 @class MyClass;
5511 MyClass<MyProtocol> *x;
5512 [x method];
5514 If 'MyProtocol' has the method 'method', we can check
5515 and retrieve the method prototype. */
5516 method_prototype
5517 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5519 /* At this point, if we have found the method_prototype,
5520 we are quite happy. The details of the class are
5521 irrelevant. If we haven't found it, a warning will
5522 have been produced that the method could not be found
5523 in the protocol, and we won't produce further
5524 warnings (please note that this means that "@class
5525 MyClass; MyClass <MyProtocol> *x;" is exactly
5526 equivalent to "id <MyProtocol> x", which isn't too
5527 satisfactory but it's not easy to see how to do
5528 better). */
5530 else
5532 if (rtype)
5534 /* We could not find an @interface declaration, and
5535 there are no protocols attached to the receiver,
5536 so we can't complete the check that the receiver
5537 responds to the method, and we can't retrieve the
5538 method prototype. But, because the receiver has
5539 a well-specified class, the programmer did want
5540 this check to be performed. Emit a warning, then
5541 keep going as if it was an 'id'. To remove the
5542 warning, either include an @interface for the
5543 class, or cast the receiver to 'id'. Note that
5544 rtype is an IDENTIFIER_NODE at this point. */
5545 warning (0, "@interface of class %qE not found", rtype);
5549 rtype = NULL_TREE;
5551 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5552 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5554 /* We have a valid ObjC class name with an associated
5555 @interface. Look up the method name in the published
5556 @interface for the class (and its superclasses). */
5557 method_prototype
5558 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5560 /* If the method was not found in the @interface, it may still
5561 exist locally as part of the @implementation. */
5562 if (!method_prototype && objc_implementation_context
5563 && CLASS_NAME (objc_implementation_context)
5564 == OBJC_TYPE_NAME (rtype))
5565 method_prototype
5566 = lookup_method
5567 ((class_tree
5568 ? CLASS_CLS_METHODS (objc_implementation_context)
5569 : CLASS_NST_METHODS (objc_implementation_context)),
5570 sel_name);
5572 /* If we haven't found a candidate method by now, try looking for
5573 it in the protocol list. */
5574 if (!method_prototype && rprotos)
5575 method_prototype
5576 = lookup_method_in_protocol_list (rprotos, sel_name,
5577 class_tree != NULL_TREE);
5579 else
5581 /* We have a type, but it's not an Objective-C type (!). */
5582 warning (0, "invalid receiver type %qs",
5583 identifier_to_locale (gen_type_name (orig_rtype)));
5584 /* After issuing the "invalid receiver" warning, perform method
5585 lookup as if we were messaging 'id'. */
5586 rtype = rprotos = NULL_TREE;
5589 /* Note that rtype could also be NULL_TREE. This happens if we are
5590 messaging a class by name, but the class was only
5591 forward-declared using @class. */
5593 /* For 'id' or 'Class' receivers, search in the global hash table as
5594 a last resort. For all receivers, warn if protocol searches have
5595 failed. */
5596 if (!method_prototype)
5598 if (rprotos)
5599 warning (0, "%<%c%E%> not found in protocol(s)",
5600 (class_tree ? '+' : '-'),
5601 sel_name);
5603 if (!rtype)
5604 method_prototype
5605 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5608 if (!method_prototype)
5610 static bool warn_missing_methods = false;
5612 if (rtype)
5613 warning (0, "%qE may not respond to %<%c%E%>",
5614 OBJC_TYPE_NAME (rtype),
5615 (class_tree ? '+' : '-'),
5616 sel_name);
5617 /* If we are messaging an 'id' or 'Class' object and made it here,
5618 then we have failed to find _any_ instance or class method,
5619 respectively. */
5620 else
5621 warning (0, "no %<%c%E%> method found",
5622 (class_tree ? '+' : '-'),
5623 sel_name);
5625 if (!warn_missing_methods)
5627 warning_at (input_location,
5628 0, "(Messages without a matching method signature");
5629 warning_at (input_location,
5630 0, "will be assumed to return %<id%> and accept");
5631 warning_at (input_location,
5632 0, "%<...%> as arguments.)");
5633 warn_missing_methods = true;
5636 else
5638 /* Warn if the method is deprecated, but not if the receiver is
5639 a generic 'id'. 'id' is used to cast an object to a generic
5640 object of an unspecified class; in that case, we'll use
5641 whatever method prototype we can find to get the method
5642 argument and return types, but it is not appropriate to
5643 produce deprecation warnings since we don't know the class
5644 that the object will be of at runtime. The @interface(s) for
5645 that class may not even be available to the compiler right
5646 now, and it is perfectly possible that the method is marked
5647 as non-deprecated in such @interface(s).
5649 In practice this makes sense since casting an object to 'id'
5650 is often used precisely to turn off warnings associated with
5651 the object being of a particular class. */
5652 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5654 if (deprecated_method_prototype)
5655 *deprecated_method_prototype = method_prototype;
5656 else
5657 warn_deprecated_use (method_prototype, NULL_TREE);
5661 /* Save the selector name for printing error messages. */
5662 current_objc_message_selector = sel_name;
5664 /* Build the method call.
5665 TODO: Get the location from somewhere that will work for delayed
5666 expansion. */
5668 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5669 receiver, rtype, sel_name,
5670 method_params, super);
5672 current_objc_message_selector = 0;
5674 return retval;
5678 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5679 expression. This variable will be initialized to global protocol_t meta-data
5680 pointer. */
5682 /* This function is called by the parser when (and only when) a
5683 @protocol() expression is found, in order to compile it. */
5684 tree
5685 objc_build_protocol_expr (tree protoname)
5687 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5688 /* definition_required */ false);
5690 if (!p)
5692 error ("cannot find protocol declaration for %qE", protoname);
5693 return error_mark_node;
5696 return (*runtime.get_protocol_reference) (input_location, p);
5699 /* This function is called by the parser when a @selector() expression
5700 is found, in order to compile it. It is only called by the parser
5701 and only to compile a @selector(). LOC is the location of the
5702 @selector. */
5703 tree
5704 objc_build_selector_expr (location_t loc, tree selnamelist)
5706 tree selname;
5708 /* Obtain the full selector name. */
5709 switch (TREE_CODE (selnamelist))
5711 case IDENTIFIER_NODE:
5712 /* A unary selector. */
5713 selname = selnamelist;
5714 break;
5715 case TREE_LIST:
5716 selname = build_keyword_selector (selnamelist);
5717 break;
5718 default:
5719 gcc_unreachable ();
5722 /* If we are required to check @selector() expressions as they
5723 are found, check that the selector has been declared. */
5724 if (warn_undeclared_selector)
5726 /* Look the selector up in the list of all known class and
5727 instance methods (up to this line) to check that the selector
5728 exists. */
5729 tree method;
5731 /* First try with instance methods. */
5732 method = objc_map_get (instance_method_map, selname);
5734 /* If not found, try with class methods. */
5735 if (method == OBJC_MAP_NOT_FOUND)
5737 method = objc_map_get (class_method_map, selname);
5739 /* If still not found, print out a warning. */
5740 if (method == OBJC_MAP_NOT_FOUND)
5741 warning (0, "undeclared selector %qE", selname);
5745 /* The runtimes do this differently, most particularly, GNU has typed
5746 selectors, whilst NeXT does not. */
5747 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5750 static tree
5751 build_ivar_reference (tree id)
5753 tree base;
5754 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5756 /* Historically, a class method that produced objects (factory
5757 method) would assign `self' to the instance that it
5758 allocated. This would effectively turn the class method into
5759 an instance method. Following this assignment, the instance
5760 variables could be accessed. That practice, while safe,
5761 violates the simple rule that a class method should not refer
5762 to an instance variable. It's better to catch the cases
5763 where this is done unknowingly than to support the above
5764 paradigm. */
5765 warning (0, "instance variable %qE accessed in class method",
5766 id);
5767 self_decl = convert (objc_instance_type, self_decl); /* cast */
5770 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5771 return (*runtime.build_ivar_reference) (input_location, base, id);
5774 static void
5775 hash_init (void)
5777 instance_method_map = objc_map_alloc_ggc (1000);
5778 class_method_map = objc_map_alloc_ggc (1000);
5780 class_name_map = objc_map_alloc_ggc (200);
5781 alias_name_map = objc_map_alloc_ggc (200);
5783 /* Initialize the hash table used to hold the constant string objects. */
5784 string_htab = hash_table<objc_string_hasher>::create_ggc (31);
5787 /* Use the following to add a method to class_method_map or
5788 instance_method_map. It will add the method, keyed by the
5789 METHOD_SEL_NAME. If the method already exists, but with one or
5790 more different prototypes, it will store a TREE_VEC in the map,
5791 with the method prototypes in the vector. */
5792 static void
5793 insert_method_into_method_map (bool class_method, tree method)
5795 tree method_name = METHOD_SEL_NAME (method);
5796 tree existing_entry;
5797 objc_map_t map;
5799 if (class_method)
5800 map = class_method_map;
5801 else
5802 map = instance_method_map;
5804 /* Check if the method already exists in the map. */
5805 existing_entry = objc_map_get (map, method_name);
5807 /* If not, we simply add it to the map. */
5808 if (existing_entry == OBJC_MAP_NOT_FOUND)
5809 objc_map_put (map, method_name, method);
5810 else
5812 tree new_entry;
5814 /* If an entry already exists, it's more complicated. We'll
5815 have to check whether the method prototype is the same or
5816 not. */
5817 if (TREE_CODE (existing_entry) != TREE_VEC)
5819 /* If the method prototypes are the same, there is nothing
5820 to do. */
5821 if (comp_proto_with_proto (method, existing_entry, 1))
5822 return;
5824 /* If not, create a vector to store both the method already
5825 in the map, and the new one that we are adding. */
5826 new_entry = make_tree_vec (2);
5828 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5829 TREE_VEC_ELT (new_entry, 1) = method;
5831 else
5833 /* An entry already exists, and it's already a vector. This
5834 means that at least 2 different method prototypes were
5835 already found, and we're considering registering yet
5836 another one. */
5837 size_t i;
5839 /* Check all the existing prototypes. If any matches the
5840 one we need to add, there is nothing to do because it's
5841 already there. */
5842 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5843 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5844 return;
5846 /* Else, create a new, bigger vector and add the new method
5847 at the end of it. This is inefficient but extremely
5848 rare; in any sane program most methods have a single
5849 prototype, and very few, if any, will have more than
5850 2! */
5851 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5853 /* Copy the methods from the existing vector. */
5854 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5855 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5857 /* Add the new method at the end. */
5858 TREE_VEC_ELT (new_entry, i) = method;
5861 /* Store the new vector in the map. */
5862 objc_map_put (map, method_name, new_entry);
5867 static tree
5868 lookup_method (tree mchain, tree method)
5870 tree key;
5872 if (TREE_CODE (method) == IDENTIFIER_NODE)
5873 key = method;
5874 else
5875 key = METHOD_SEL_NAME (method);
5877 while (mchain)
5879 if (METHOD_SEL_NAME (mchain) == key)
5880 return mchain;
5882 mchain = DECL_CHAIN (mchain);
5884 return NULL_TREE;
5887 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5888 method in INTERFACE, along with any categories and protocols
5889 attached thereto. If method is not found, and the
5890 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5891 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5892 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5893 be found in INTERFACE or any of its superclasses, look for an
5894 _instance_ method of the same name in the root class as a last
5895 resort. This behaviour can be turned off by using
5896 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5898 If a suitable method cannot be found, return NULL_TREE. */
5900 static tree
5901 lookup_method_static (tree interface, tree ident, int flags)
5903 tree meth = NULL_TREE, root_inter = NULL_TREE;
5904 tree inter = interface;
5905 int is_class = (flags & OBJC_LOOKUP_CLASS);
5906 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5907 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
5909 while (inter)
5911 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5912 tree category = inter;
5914 /* First, look up the method in the class itself. */
5915 if ((meth = lookup_method (chain, ident)))
5916 return meth;
5918 /* Failing that, look for the method in each category of the class. */
5919 while ((category = CLASS_CATEGORY_LIST (category)))
5921 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
5923 /* Check directly in each category. */
5924 if ((meth = lookup_method (chain, ident)))
5925 return meth;
5927 /* Failing that, check in each category's protocols. */
5928 if (CLASS_PROTOCOL_LIST (category))
5930 if ((meth = (lookup_method_in_protocol_list
5931 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5932 return meth;
5936 /* If not found in categories, check in protocols of the main class. */
5937 if (CLASS_PROTOCOL_LIST (inter))
5939 if ((meth = (lookup_method_in_protocol_list
5940 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5941 return meth;
5944 /* If we were instructed not to look in superclasses, don't. */
5945 if (no_superclasses)
5946 return NULL_TREE;
5948 /* Failing that, climb up the inheritance hierarchy. */
5949 root_inter = inter;
5950 inter = lookup_interface (CLASS_SUPER_NAME (inter));
5952 while (inter);
5954 if (is_class && !no_instance_methods_of_root_class)
5956 /* If no class (factory) method was found, check if an _instance_
5957 method of the same name exists in the root class. This is what
5958 the Objective-C runtime will do. */
5959 return lookup_method_static (root_inter, ident, 0);
5961 else
5963 /* If an instance method was not found, return 0. */
5964 return NULL_TREE;
5968 static tree
5969 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
5971 tree existing_method = NULL_TREE;
5973 /* The first thing we do is look up the method in the list of
5974 methods already defined in the interface (or implementation). */
5975 if (is_class)
5976 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5977 else
5978 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
5980 /* In the case of protocols, we have a second list of methods to
5981 consider, the list of optional ones. */
5982 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
5984 /* @required methods are added to the protocol's normal list.
5985 @optional methods are added to the protocol's OPTIONAL lists.
5986 Note that adding the methods to the optional lists disables
5987 checking that the methods are implemented by classes
5988 implementing the protocol, since these checks only use the
5989 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
5991 /* First of all, if the method to add is @optional, and we found
5992 it already existing as @required, emit an error. */
5993 if (is_optional && existing_method)
5995 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
5996 (is_class ? '+' : '-'),
5997 METHOD_SEL_NAME (existing_method));
5998 inform (DECL_SOURCE_LOCATION (existing_method),
5999 "previous declaration of %<%c%E%> as %<@required%>",
6000 (is_class ? '+' : '-'),
6001 METHOD_SEL_NAME (existing_method));
6004 /* Now check the list of @optional methods if we didn't find the
6005 method in the @required list. */
6006 if (!existing_method)
6008 if (is_class)
6009 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6010 else
6011 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
6013 if (!is_optional && existing_method)
6015 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6016 (is_class ? '+' : '-'),
6017 METHOD_SEL_NAME (existing_method));
6018 inform (DECL_SOURCE_LOCATION (existing_method),
6019 "previous declaration of %<%c%E%> as %<@optional%>",
6020 (is_class ? '+' : '-'),
6021 METHOD_SEL_NAME (existing_method));
6026 /* If the method didn't exist already, add it. */
6027 if (!existing_method)
6029 if (is_optional)
6031 if (is_class)
6033 /* Put the method on the list in reverse order. */
6034 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6035 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6037 else
6039 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6040 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6043 else
6045 if (is_class)
6047 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6048 CLASS_CLS_METHODS (klass) = method;
6050 else
6052 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6053 CLASS_NST_METHODS (klass) = method;
6057 else
6059 /* The method was already defined. Check that the types match
6060 for an @interface for a class or category, or for a
6061 @protocol. Give hard errors on methods with identical
6062 selectors but differing argument and/or return types. We do
6063 not do this for @implementations, because C/C++ will do it
6064 for us (i.e., there will be duplicate function definition
6065 errors). */
6066 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6067 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6068 /* Starting with GCC 4.6, we emit the same error for
6069 protocols too. The situation is identical to
6070 @interfaces as there is no possible meaningful reason
6071 for defining the same method with different signatures
6072 in the very same @protocol. If that was allowed,
6073 whenever the protocol is used (both at compile and run
6074 time) there wouldn't be any meaningful way to decide
6075 which of the two method signatures should be used. */
6076 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6077 && !comp_proto_with_proto (method, existing_method, 1))
6079 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6080 (is_class ? '+' : '-'),
6081 METHOD_SEL_NAME (existing_method));
6082 inform (DECL_SOURCE_LOCATION (existing_method),
6083 "previous declaration of %<%c%E%>",
6084 (is_class ? '+' : '-'),
6085 METHOD_SEL_NAME (existing_method));
6089 if (is_class)
6090 insert_method_into_method_map (true, method);
6091 else
6093 insert_method_into_method_map (false, method);
6095 /* Instance methods in root classes (and categories thereof)
6096 may act as class methods as a last resort. We also add
6097 instance methods listed in @protocol declarations to
6098 the class hash table, on the assumption that @protocols
6099 may be adopted by root classes or categories. */
6100 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6101 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6102 klass = lookup_interface (CLASS_NAME (klass));
6104 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6105 || !CLASS_SUPER_NAME (klass))
6106 insert_method_into_method_map (true, method);
6109 return method;
6112 static void
6113 add_category (tree klass, tree category)
6115 /* Put categories on list in reverse order. */
6116 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6118 if (cat)
6120 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6121 CLASS_NAME (klass),
6122 CLASS_SUPER_NAME (category));
6124 else
6126 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6127 CLASS_CATEGORY_LIST (klass) = category;
6131 #ifndef OBJCPLUS
6132 /* A flexible array member is a C99 extension where you can use
6133 "type[]" at the end of a struct to mean a variable-length array.
6135 In Objective-C, instance variables are fundamentally members of a
6136 struct, but the struct can always be extended by subclassing; hence
6137 we need to detect and forbid all instance variables declared using
6138 flexible array members.
6140 No check for this is needed in Objective-C++, since C++ does not
6141 have flexible array members. */
6143 /* Determine whether TYPE is a structure with a flexible array member,
6144 a union containing such a structure (possibly recursively) or an
6145 array of such structures or unions. These are all invalid as
6146 instance variable. */
6147 static bool
6148 flexible_array_type_p (tree type)
6150 tree x;
6151 switch (TREE_CODE (type))
6153 case RECORD_TYPE:
6154 x = TYPE_FIELDS (type);
6155 if (x == NULL_TREE)
6156 return false;
6157 while (DECL_CHAIN (x) != NULL_TREE)
6158 x = DECL_CHAIN (x);
6159 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6160 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6161 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6162 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6163 return true;
6164 return false;
6165 case UNION_TYPE:
6166 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6168 if (flexible_array_type_p (TREE_TYPE (x)))
6169 return true;
6171 return false;
6172 /* Note that we also check for arrays of something that uses a flexible array member. */
6173 case ARRAY_TYPE:
6174 if (flexible_array_type_p (TREE_TYPE (type)))
6175 return true;
6176 return false;
6177 default:
6178 return false;
6181 #endif
6183 /* Produce a printable version of an ivar name. This is only used
6184 inside add_instance_variable. */
6185 static const char *
6186 printable_ivar_name (tree field_decl)
6188 if (DECL_NAME (field_decl))
6189 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6190 else
6191 return _("<unnamed>");
6194 /* Called after parsing each instance variable declaration. Necessary to
6195 preserve typedefs and implement public/private...
6197 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6199 static tree
6200 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6201 tree field_decl)
6203 tree field_type = TREE_TYPE (field_decl);
6205 #ifdef OBJCPLUS
6206 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6208 error ("illegal reference type specified for instance variable %qs",
6209 printable_ivar_name (field_decl));
6210 /* Return class as is without adding this ivar. */
6211 return klass;
6213 #endif
6215 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6216 || TYPE_SIZE (field_type) == error_mark_node)
6217 /* 'type[0]' is allowed, but 'type[]' is not! */
6219 error ("instance variable %qs has unknown size",
6220 printable_ivar_name (field_decl));
6221 /* Return class as is without adding this ivar. */
6222 return klass;
6225 #ifndef OBJCPLUS
6226 /* Also, in C reject a struct with a flexible array member. Ie,
6228 struct A { int x; int[] y; };
6230 @interface X
6232 struct A instance_variable;
6234 @end
6236 is not valid because if the class is subclassed, we wouldn't be able
6237 to calculate the offset of the next instance variable. */
6238 if (flexible_array_type_p (field_type))
6240 error ("instance variable %qs uses flexible array member",
6241 printable_ivar_name (field_decl));
6242 /* Return class as is without adding this ivar. */
6243 return klass;
6245 #endif
6247 #ifdef OBJCPLUS
6248 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6249 need to either (1) warn the user about it or (2) generate suitable
6250 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6251 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6252 if (MAYBE_CLASS_TYPE_P (field_type)
6253 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6254 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6255 || TYPE_POLYMORPHIC_P (field_type)))
6257 tree type_name = OBJC_TYPE_NAME (field_type);
6259 if (flag_objc_call_cxx_cdtors)
6261 /* Since the ObjC runtime will be calling the constructors and
6262 destructors for us, the only thing we can't handle is the lack
6263 of a default constructor. */
6264 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6265 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6267 warning (0, "type %qE has no default constructor to call",
6268 type_name);
6270 /* If we cannot call a constructor, we should also avoid
6271 calling the destructor, for symmetry. */
6272 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6273 warning (0, "destructor for %qE shall not be run either",
6274 type_name);
6277 else
6279 static bool warn_cxx_ivars = false;
6281 if (TYPE_POLYMORPHIC_P (field_type))
6283 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6284 initialize them. */
6285 error ("type %qE has virtual member functions", type_name);
6286 error ("illegal aggregate type %qE specified "
6287 "for instance variable %qs",
6288 type_name, printable_ivar_name (field_decl));
6289 /* Return class as is without adding this ivar. */
6290 return klass;
6293 /* User-defined constructors and destructors are not known to Obj-C
6294 and hence will not be called. This may or may not be a problem. */
6295 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6296 warning (0, "type %qE has a user-defined constructor", type_name);
6297 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6298 warning (0, "type %qE has a user-defined destructor", type_name);
6300 if (!warn_cxx_ivars)
6302 warning (0, "C++ constructors and destructors will not "
6303 "be invoked for Objective-C fields");
6304 warn_cxx_ivars = true;
6308 #endif
6310 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6311 switch (visibility)
6313 case OBJC_IVAR_VIS_PROTECTED:
6314 TREE_PUBLIC (field_decl) = 0;
6315 TREE_PRIVATE (field_decl) = 0;
6316 TREE_PROTECTED (field_decl) = 1;
6317 break;
6319 case OBJC_IVAR_VIS_PACKAGE:
6320 /* TODO: Implement the package variant. */
6321 case OBJC_IVAR_VIS_PUBLIC:
6322 TREE_PUBLIC (field_decl) = 1;
6323 TREE_PRIVATE (field_decl) = 0;
6324 TREE_PROTECTED (field_decl) = 0;
6325 break;
6327 case OBJC_IVAR_VIS_PRIVATE:
6328 TREE_PUBLIC (field_decl) = 0;
6329 TREE_PRIVATE (field_decl) = 1;
6330 TREE_PROTECTED (field_decl) = 0;
6331 break;
6335 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6337 return klass;
6340 /* True if the ivar is private and we are not in its implementation. */
6342 static int
6343 is_private (tree decl)
6345 return (TREE_PRIVATE (decl)
6346 && ! is_ivar (CLASS_IVARS (implementation_template),
6347 DECL_NAME (decl)));
6350 /* Searches all the instance variables of 'klass' and of its
6351 superclasses for an instance variable whose name (identifier) is
6352 'ivar_name_ident'. Return the declaration (DECL) of the instance
6353 variable, if found, or NULL_TREE, if not found. */
6354 static inline tree
6355 ivar_of_class (tree klass, tree ivar_name_ident)
6357 /* First, look up the ivar in CLASS_RAW_IVARS. */
6358 tree decl_chain = CLASS_RAW_IVARS (klass);
6360 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6361 if (DECL_NAME (decl_chain) == ivar_name_ident)
6362 return decl_chain;
6364 /* If not found, search up the class hierarchy. */
6365 while (CLASS_SUPER_NAME (klass))
6367 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6369 decl_chain = CLASS_RAW_IVARS (klass);
6371 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6372 if (DECL_NAME (decl_chain) == ivar_name_ident)
6373 return decl_chain;
6376 return NULL_TREE;
6379 /* We have an instance variable reference;, check to see if it is public. */
6382 objc_is_public (tree expr, tree identifier)
6384 tree basetype, decl;
6386 #ifdef OBJCPLUS
6387 if (processing_template_decl)
6388 return 1;
6389 #endif
6391 if (TREE_TYPE (expr) == error_mark_node)
6392 return 1;
6394 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6396 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6398 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6400 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6402 if (!klass)
6404 error ("cannot find interface declaration for %qE",
6405 OBJC_TYPE_NAME (basetype));
6406 return 0;
6409 if ((decl = ivar_of_class (klass, identifier)))
6411 if (TREE_PUBLIC (decl))
6412 return 1;
6414 /* Important difference between the Stepstone translator:
6415 all instance variables should be public within the context
6416 of the implementation. */
6417 if (objc_implementation_context
6418 && ((TREE_CODE (objc_implementation_context)
6419 == CLASS_IMPLEMENTATION_TYPE)
6420 || (TREE_CODE (objc_implementation_context)
6421 == CATEGORY_IMPLEMENTATION_TYPE)))
6423 tree curtype = TYPE_MAIN_VARIANT
6424 (CLASS_STATIC_TEMPLATE
6425 (implementation_template));
6427 if (basetype == curtype
6428 || DERIVED_FROM_P (basetype, curtype))
6430 int priv = is_private (decl);
6432 if (priv)
6433 error ("instance variable %qE is declared private",
6434 DECL_NAME (decl));
6436 return !priv;
6440 /* The 2.95.2 compiler sometimes allowed C functions to access
6441 non-@public ivars. We will let this slide for now... */
6442 if (!objc_method_context)
6444 warning (0, "instance variable %qE is %s; "
6445 "this will be a hard error in the future",
6446 identifier,
6447 TREE_PRIVATE (decl) ? "@private" : "@protected");
6448 return 1;
6451 error ("instance variable %qE is declared %s",
6452 identifier,
6453 TREE_PRIVATE (decl) ? "private" : "protected");
6454 return 0;
6459 return 1;
6462 /* Make sure all methods in CHAIN (a list of method declarations from
6463 an @interface or a @protocol) are in IMPLEMENTATION (the
6464 implementation context). This is used to check for example that
6465 all methods declared in an @interface were implemented in an
6466 @implementation.
6468 Some special methods (property setters/getters) are special and if
6469 they are not found in IMPLEMENTATION, we look them up in its
6470 superclasses. */
6472 static int
6473 check_methods (tree chain, tree implementation, int mtype)
6475 int first = 1;
6476 tree list;
6478 if (mtype == (int)'+')
6479 list = CLASS_CLS_METHODS (implementation);
6480 else
6481 list = CLASS_NST_METHODS (implementation);
6483 while (chain)
6485 /* If the method is associated with a dynamic property, then it
6486 is Ok not to have the method implementation, as it will be
6487 generated dynamically at runtime. To decide if the method is
6488 associated with a @dynamic property, we search the list of
6489 @synthesize and @dynamic for this implementation, and look
6490 for any @dynamic property with the same setter or getter name
6491 as this method. */
6492 tree x;
6493 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6494 if (PROPERTY_DYNAMIC (x)
6495 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6496 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6497 break;
6499 if (x != NULL_TREE)
6501 chain = TREE_CHAIN (chain); /* next method... */
6502 continue;
6505 if (!lookup_method (list, chain))
6507 /* If the method is a property setter/getter, we'll still
6508 allow it to be missing if it is implemented by
6509 'interface' or any of its superclasses. */
6510 tree property = METHOD_PROPERTY_CONTEXT (chain);
6511 if (property)
6513 /* Note that since this is a property getter/setter, it
6514 is obviously an instance method. */
6515 tree interface = NULL_TREE;
6517 /* For a category, first check the main class
6518 @interface. */
6519 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6521 interface = lookup_interface (CLASS_NAME (implementation));
6523 /* If the method is found in the main class, it's Ok. */
6524 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6526 chain = DECL_CHAIN (chain);
6527 continue;
6530 /* Else, get the superclass. */
6531 if (CLASS_SUPER_NAME (interface))
6532 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6533 else
6534 interface = NULL_TREE;
6537 /* Get the superclass for classes. */
6538 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6540 if (CLASS_SUPER_NAME (implementation))
6541 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6542 else
6543 interface = NULL_TREE;
6546 /* Now, interface is the superclass, if any; go check it. */
6547 if (interface)
6549 if (lookup_method_static (interface, chain, 0))
6551 chain = DECL_CHAIN (chain);
6552 continue;
6555 /* Else, fall through - warn. */
6557 if (first)
6559 switch (TREE_CODE (implementation))
6561 case CLASS_IMPLEMENTATION_TYPE:
6562 warning (0, "incomplete implementation of class %qE",
6563 CLASS_NAME (implementation));
6564 break;
6565 case CATEGORY_IMPLEMENTATION_TYPE:
6566 warning (0, "incomplete implementation of category %qE",
6567 CLASS_SUPER_NAME (implementation));
6568 break;
6569 default:
6570 gcc_unreachable ();
6572 first = 0;
6575 warning (0, "method definition for %<%c%E%> not found",
6576 mtype, METHOD_SEL_NAME (chain));
6579 chain = DECL_CHAIN (chain);
6582 return first;
6585 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6587 static int
6588 conforms_to_protocol (tree klass, tree protocol)
6590 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6592 tree p = CLASS_PROTOCOL_LIST (klass);
6593 while (p && TREE_VALUE (p) != protocol)
6594 p = TREE_CHAIN (p);
6596 if (!p)
6598 tree super = (CLASS_SUPER_NAME (klass)
6599 ? lookup_interface (CLASS_SUPER_NAME (klass))
6600 : NULL_TREE);
6601 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6602 if (!tmp)
6603 return 0;
6607 return 1;
6610 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6611 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6613 static int
6614 check_methods_accessible (tree chain, tree context, int mtype)
6616 int first = 1;
6617 tree list;
6618 tree base_context = context;
6620 while (chain)
6622 /* If the method is associated with a dynamic property, then it
6623 is Ok not to have the method implementation, as it will be
6624 generated dynamically at runtime. Search for any @dynamic
6625 property with the same setter or getter name as this
6626 method. TODO: Use a hashtable lookup. */
6627 tree x;
6628 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6629 if (PROPERTY_DYNAMIC (x)
6630 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6631 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6632 break;
6634 if (x != NULL_TREE)
6636 chain = TREE_CHAIN (chain); /* next method... */
6637 continue;
6640 context = base_context;
6641 while (context)
6643 if (mtype == '+')
6644 list = CLASS_CLS_METHODS (context);
6645 else
6646 list = CLASS_NST_METHODS (context);
6648 if (lookup_method (list, chain))
6649 break;
6651 switch (TREE_CODE (context))
6653 case CLASS_IMPLEMENTATION_TYPE:
6654 case CLASS_INTERFACE_TYPE:
6655 context = (CLASS_SUPER_NAME (context)
6656 ? lookup_interface (CLASS_SUPER_NAME (context))
6657 : NULL_TREE);
6658 break;
6659 case CATEGORY_IMPLEMENTATION_TYPE:
6660 case CATEGORY_INTERFACE_TYPE:
6661 context = (CLASS_NAME (context)
6662 ? lookup_interface (CLASS_NAME (context))
6663 : NULL_TREE);
6664 break;
6665 default:
6666 gcc_unreachable ();
6670 if (context == NULL_TREE)
6672 if (first)
6674 switch (TREE_CODE (objc_implementation_context))
6676 case CLASS_IMPLEMENTATION_TYPE:
6677 warning (0, "incomplete implementation of class %qE",
6678 CLASS_NAME (objc_implementation_context));
6679 break;
6680 case CATEGORY_IMPLEMENTATION_TYPE:
6681 warning (0, "incomplete implementation of category %qE",
6682 CLASS_SUPER_NAME (objc_implementation_context));
6683 break;
6684 default:
6685 gcc_unreachable ();
6687 first = 0;
6689 warning (0, "method definition for %<%c%E%> not found",
6690 mtype, METHOD_SEL_NAME (chain));
6693 chain = TREE_CHAIN (chain); /* next method... */
6695 return first;
6698 /* Check whether the current interface (accessible via
6699 'objc_implementation_context') actually implements protocol P, along
6700 with any protocols that P inherits. */
6702 static void
6703 check_protocol (tree p, const char *type, tree name)
6705 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6707 int f1, f2;
6709 /* Ensure that all protocols have bodies! */
6710 if (warn_protocol)
6712 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6713 objc_implementation_context,
6714 '+');
6715 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6716 objc_implementation_context,
6717 '-');
6719 else
6721 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6722 objc_implementation_context,
6723 '+');
6724 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6725 objc_implementation_context,
6726 '-');
6729 if (!f1 || !f2)
6730 warning (0, "%s %qE does not fully implement the %qE protocol",
6731 type, name, PROTOCOL_NAME (p));
6734 /* Check protocols recursively. */
6735 if (PROTOCOL_LIST (p))
6737 tree subs = PROTOCOL_LIST (p);
6738 tree super_class =
6739 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6741 while (subs)
6743 tree sub = TREE_VALUE (subs);
6745 /* If the superclass does not conform to the protocols
6746 inherited by P, then we must! */
6747 if (!super_class || !conforms_to_protocol (super_class, sub))
6748 check_protocol (sub, type, name);
6749 subs = TREE_CHAIN (subs);
6754 /* Check whether the current interface (accessible via
6755 'objc_implementation_context') actually implements the protocols listed
6756 in PROTO_LIST. */
6758 static void
6759 check_protocols (tree proto_list, const char *type, tree name)
6761 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6763 tree p = TREE_VALUE (proto_list);
6765 check_protocol (p, type, name);
6769 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6770 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6771 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6772 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6773 SUPER_NAME is the name of the category. For a class extension,
6774 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6775 static tree
6776 start_class (enum tree_code code, tree class_name, tree super_name,
6777 tree protocol_list, tree attributes)
6779 tree klass = NULL_TREE;
6780 tree decl;
6782 #ifdef OBJCPLUS
6783 if (current_namespace != global_namespace)
6785 error ("Objective-C declarations may only appear in global scope");
6787 #endif /* OBJCPLUS */
6789 if (objc_implementation_context)
6791 warning (0, "%<@end%> missing in implementation context");
6792 finish_class (objc_implementation_context);
6793 objc_ivar_chain = NULL_TREE;
6794 objc_implementation_context = NULL_TREE;
6797 /* If this is a class extension, we'll be "reopening" the existing
6798 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6799 a new node. */
6800 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6802 klass = make_node (code);
6803 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6806 /* Check for existence of the super class, if one was specified. Note
6807 that we must have seen an @interface, not just a @class. If we
6808 are looking at a @compatibility_alias, traverse it first. */
6809 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6810 && super_name)
6812 tree super = objc_is_class_name (super_name);
6813 tree super_interface = NULL_TREE;
6815 if (super)
6816 super_interface = lookup_interface (super);
6818 if (!super_interface)
6820 error ("cannot find interface declaration for %qE, superclass of %qE",
6821 super ? super : super_name,
6822 class_name);
6823 super_name = NULL_TREE;
6825 else
6827 if (TREE_DEPRECATED (super_interface))
6828 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6829 super);
6830 super_name = super;
6834 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6836 CLASS_NAME (klass) = class_name;
6837 CLASS_SUPER_NAME (klass) = super_name;
6838 CLASS_CLS_METHODS (klass) = NULL_TREE;
6841 if (! objc_is_class_name (class_name)
6842 && (decl = lookup_name (class_name)))
6844 error ("%qE redeclared as different kind of symbol",
6845 class_name);
6846 error ("previous declaration of %q+D",
6847 decl);
6850 switch (code)
6852 case CLASS_IMPLEMENTATION_TYPE:
6854 tree chain;
6856 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6857 if (TREE_VALUE (chain) == class_name)
6859 error ("reimplementation of class %qE",
6860 class_name);
6861 /* TODO: error message saying where it was previously
6862 implemented. */
6863 break;
6865 if (chain == NULL_TREE)
6866 implemented_classes = tree_cons (NULL_TREE, class_name,
6867 implemented_classes);
6870 /* Reset for multiple classes per file. */
6871 method_slot = 0;
6873 objc_implementation_context = klass;
6875 /* Lookup the interface for this implementation. */
6877 if (!(implementation_template = lookup_interface (class_name)))
6879 warning (0, "cannot find interface declaration for %qE",
6880 class_name);
6881 add_interface (implementation_template = objc_implementation_context,
6882 class_name);
6885 /* If a super class has been specified in the implementation,
6886 insure it conforms to the one specified in the interface. */
6888 if (super_name
6889 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6891 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6892 error ("conflicting super class name %qE",
6893 super_name);
6894 if (previous_name)
6895 error ("previous declaration of %qE", previous_name);
6896 else
6897 error ("previous declaration");
6900 else if (! super_name)
6902 CLASS_SUPER_NAME (objc_implementation_context)
6903 = CLASS_SUPER_NAME (implementation_template);
6905 break;
6907 case CLASS_INTERFACE_TYPE:
6908 if (lookup_interface (class_name))
6909 #ifdef OBJCPLUS
6910 error ("duplicate interface declaration for class %qE", class_name);
6911 #else
6912 warning (0, "duplicate interface declaration for class %qE", class_name);
6913 #endif
6914 else
6915 add_interface (klass, class_name);
6917 if (protocol_list)
6918 CLASS_PROTOCOL_LIST (klass)
6919 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
6921 if (attributes)
6923 tree attribute;
6924 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6926 tree name = TREE_PURPOSE (attribute);
6928 /* TODO: Document what the objc_exception attribute is/does. */
6929 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6930 attributes. */
6931 if (is_attribute_p ("deprecated", name))
6932 TREE_DEPRECATED (klass) = 1;
6933 else if (is_attribute_p ("objc_exception", name))
6934 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6935 else
6936 /* Warn about and ignore all others for now, but store them. */
6937 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6939 TYPE_ATTRIBUTES (klass) = attributes;
6941 break;
6943 case CATEGORY_INTERFACE_TYPE:
6945 tree class_category_is_assoc_with;
6947 /* For a category, class_name is really the name of the class that
6948 the following set of methods will be associated with. We must
6949 find the interface so that can derive the objects template. */
6950 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6952 error ("cannot find interface declaration for %qE",
6953 class_name);
6954 exit (FATAL_EXIT_CODE);
6956 else
6958 if (TREE_DEPRECATED (class_category_is_assoc_with))
6959 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6960 class_name);
6962 if (super_name == NULL_TREE)
6964 /* This is a class extension. Get the original
6965 interface, and continue working on it. */
6966 objc_in_class_extension = true;
6967 klass = class_category_is_assoc_with;
6969 if (protocol_list)
6971 /* Append protocols to the original protocol
6972 list. */
6973 CLASS_PROTOCOL_LIST (klass)
6974 = chainon (CLASS_PROTOCOL_LIST (klass),
6975 lookup_and_install_protocols
6976 (protocol_list,
6977 /* definition_required */ true));
6980 else
6982 add_category (class_category_is_assoc_with, klass);
6984 if (protocol_list)
6985 CLASS_PROTOCOL_LIST (klass)
6986 = lookup_and_install_protocols
6987 (protocol_list, /* definition_required */ true);
6991 break;
6993 case CATEGORY_IMPLEMENTATION_TYPE:
6994 /* Reset for multiple classes per file. */
6995 method_slot = 0;
6997 objc_implementation_context = klass;
6999 /* For a category, class_name is really the name of the class that
7000 the following set of methods will be associated with. We must
7001 find the interface so that can derive the objects template. */
7003 if (!(implementation_template = lookup_interface (class_name)))
7005 error ("cannot find interface declaration for %qE",
7006 class_name);
7007 exit (FATAL_EXIT_CODE);
7009 break;
7010 default:
7011 gcc_unreachable ();
7013 return klass;
7016 static tree
7017 continue_class (tree klass)
7019 switch (TREE_CODE (klass))
7021 case CLASS_IMPLEMENTATION_TYPE:
7022 case CATEGORY_IMPLEMENTATION_TYPE:
7024 struct imp_entry *imp_entry;
7026 /* Check consistency of the instance variables. */
7028 if (CLASS_RAW_IVARS (klass))
7029 check_ivars (implementation_template, klass);
7031 /* code generation */
7032 #ifdef OBJCPLUS
7033 push_lang_context (lang_name_c);
7034 #endif
7035 build_private_template (implementation_template);
7036 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7037 objc_instance_type = build_pointer_type (uprivate_record);
7039 imp_entry = ggc_alloc<struct imp_entry> ();
7041 imp_entry->next = imp_list;
7042 imp_entry->imp_context = klass;
7043 imp_entry->imp_template = implementation_template;
7044 ucls_super_ref = uucls_super_ref = NULL;
7045 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7047 imp_entry->class_decl = (*runtime.class_decl) (klass);
7048 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7050 else
7052 imp_entry->class_decl = (*runtime.category_decl) (klass);
7053 imp_entry->meta_decl = NULL;
7055 imp_entry->has_cxx_cdtors = 0;
7057 /* Append to front and increment count. */
7058 imp_list = imp_entry;
7059 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7060 imp_count++;
7061 else
7062 cat_count++;
7063 #ifdef OBJCPLUS
7064 pop_lang_context ();
7065 #endif /* OBJCPLUS */
7067 return get_class_ivars (implementation_template, true);
7068 break;
7070 case CLASS_INTERFACE_TYPE:
7072 if (objc_in_class_extension)
7073 return NULL_TREE;
7074 #ifdef OBJCPLUS
7075 push_lang_context (lang_name_c);
7076 #endif /* OBJCPLUS */
7077 objc_collecting_ivars = 1;
7078 build_private_template (klass);
7079 objc_collecting_ivars = 0;
7080 #ifdef OBJCPLUS
7081 pop_lang_context ();
7082 #endif /* OBJCPLUS */
7083 return NULL_TREE;
7084 break;
7086 default:
7087 return error_mark_node;
7091 /* This routine builds name of the setter synthesized function. */
7092 char *
7093 objc_build_property_setter_name (tree ident)
7095 /* TODO: Use alloca to allocate buffer of appropriate size. */
7096 static char string[BUFSIZE];
7097 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7098 string[3] = TOUPPER (string[3]);
7099 return string;
7102 /* This routine prepares the declarations of the property accessor
7103 helper functions (objc_getProperty(), etc) that are used when
7104 @synthesize is used.
7106 runtime-specific routines are built in the respective runtime
7107 initialize functions. */
7108 static void
7109 build_common_objc_property_accessor_helpers (void)
7111 tree type;
7113 /* Declare the following function:
7115 objc_getProperty (id self, SEL _cmd,
7116 ptrdiff_t offset, BOOL is_atomic); */
7117 type = build_function_type_list (objc_object_type,
7118 objc_object_type,
7119 objc_selector_type,
7120 ptrdiff_type_node,
7121 boolean_type_node,
7122 NULL_TREE);
7123 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7124 type, 0, NOT_BUILT_IN,
7125 NULL, NULL_TREE);
7126 TREE_NOTHROW (objc_getProperty_decl) = 0;
7128 /* Declare the following function:
7129 void
7130 objc_setProperty (id self, SEL _cmd,
7131 ptrdiff_t offset, id new_value,
7132 BOOL is_atomic, BOOL should_copy); */
7133 type = build_function_type_list (void_type_node,
7134 objc_object_type,
7135 objc_selector_type,
7136 ptrdiff_type_node,
7137 objc_object_type,
7138 boolean_type_node,
7139 boolean_type_node,
7140 NULL_TREE);
7141 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7142 type, 0, NOT_BUILT_IN,
7143 NULL, NULL_TREE);
7144 TREE_NOTHROW (objc_setProperty_decl) = 0;
7147 /* This looks up an ivar in a class (including superclasses). */
7148 static tree
7149 lookup_ivar (tree interface, tree instance_variable_name)
7151 while (interface)
7153 tree decl_chain;
7155 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7156 if (DECL_NAME (decl_chain) == instance_variable_name)
7157 return decl_chain;
7159 /* Not found. Search superclass if any. */
7160 if (CLASS_SUPER_NAME (interface))
7161 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7164 return NULL_TREE;
7167 /* This routine synthesizes a 'getter' method. This is only called
7168 for @synthesize properties. */
7169 static void
7170 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7172 location_t location = DECL_SOURCE_LOCATION (property);
7173 tree fn, decl;
7174 tree body;
7175 tree ret_val;
7177 /* If user has implemented a getter with same name then do nothing. */
7178 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7179 PROPERTY_GETTER_NAME (property)))
7180 return;
7182 /* Find declaration of the property getter in the interface (or
7183 superclass, or protocol). There must be one. */
7184 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7186 /* If one not declared in the interface, this condition has already
7187 been reported as user error (because property was not declared in
7188 the interface). */
7189 if (!decl)
7190 return;
7192 /* Adapt the 'decl'. Use the source location of the @synthesize
7193 statement for error messages. */
7194 decl = copy_node (decl);
7195 DECL_SOURCE_LOCATION (decl) = location;
7197 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7198 NULL_TREE);
7199 body = c_begin_compound_stmt (true);
7201 /* Now we need to decide how we build the getter. There are three
7202 cases:
7204 for 'copy' or 'retain' properties we need to use the
7205 objc_getProperty() accessor helper which knows about retain and
7206 copy. It supports both 'nonatomic' and 'atomic' access.
7208 for 'nonatomic, assign' properties we can access the instance
7209 variable directly. 'nonatomic' means we don't have to use locks,
7210 and 'assign' means we don't have to worry about retain or copy.
7211 If you combine the two, it means we can just access the instance
7212 variable directly.
7214 for 'atomic, assign' properties we use objc_copyStruct() (for the
7215 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7216 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7218 case OBJC_PROPERTY_RETAIN:
7219 case OBJC_PROPERTY_COPY:
7221 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7222 tree cmd, ivar, offset, is_atomic;
7223 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7225 /* Find the ivar to compute the offset. */
7226 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7227 if (!ivar || is_private (ivar))
7229 /* This should never happen. */
7230 error_at (location,
7231 "can not find instance variable associated with property");
7232 ret_val = error_mark_node;
7233 break;
7235 offset = byte_position (ivar);
7237 if (PROPERTY_NONATOMIC (property))
7238 is_atomic = boolean_false_node;
7239 else
7240 is_atomic = boolean_true_node;
7242 ret_val = build_function_call
7243 (location,
7244 /* Function prototype. */
7245 objc_getProperty_decl,
7246 /* Parameters. */
7247 tree_cons /* self */
7248 (NULL_TREE, self_decl,
7249 tree_cons /* _cmd */
7250 (NULL_TREE, cmd,
7251 tree_cons /* offset */
7252 (NULL_TREE, offset,
7253 tree_cons /* is_atomic */
7254 (NULL_TREE, is_atomic, NULL_TREE)))));
7256 break;
7257 case OBJC_PROPERTY_ASSIGN:
7258 if (PROPERTY_NONATOMIC (property))
7260 /* We build "return self->PROPERTY_IVAR_NAME;" */
7261 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7262 break;
7264 else
7266 /* We build
7267 <property type> __objc_property_temp;
7268 objc_getPropertyStruct (&__objc_property_temp,
7269 &(self->PROPERTY_IVAR_NAME),
7270 sizeof (type of self->PROPERTY_IVAR_NAME),
7271 is_atomic,
7272 false)
7273 return __objc_property_temp;
7275 For the NeXT runtime, we need to use objc_copyStruct
7276 instead of objc_getPropertyStruct. */
7277 tree objc_property_temp_decl, function_decl, function_call;
7278 tree size_of, is_atomic;
7280 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7281 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7282 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7284 /* sizeof (ivar type). Since the ivar and the property have
7285 the same type, there is no need to lookup the ivar. */
7286 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7287 true /* is_sizeof */,
7288 false /* min_alignof */,
7289 false /* complain */);
7291 if (PROPERTY_NONATOMIC (property))
7292 is_atomic = boolean_false_node;
7293 else
7294 is_atomic = boolean_true_node;
7296 if (objc_copyStruct_decl)
7297 function_decl = objc_copyStruct_decl;
7298 else
7299 function_decl = objc_getPropertyStruct_decl;
7301 function_call = build_function_call
7302 (location,
7303 /* Function prototype. */
7304 function_decl,
7305 /* Parameters. */
7306 tree_cons /* &__objc_property_temp_decl */
7307 /* Warning: note that using build_fold_addr_expr_loc()
7308 here causes invalid code to be generated. */
7309 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7310 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7311 (NULL_TREE, build_fold_addr_expr_loc (location,
7312 objc_lookup_ivar
7313 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7314 tree_cons /* sizeof (PROPERTY_IVAR) */
7315 (NULL_TREE, size_of,
7316 tree_cons /* is_atomic */
7317 (NULL_TREE, is_atomic,
7318 /* TODO: This is currently ignored by the GNU
7319 runtime, but what about the next one ? */
7320 tree_cons /* has_strong */
7321 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7323 add_stmt (function_call);
7325 ret_val = objc_property_temp_decl;
7327 break;
7328 default:
7329 gcc_unreachable ();
7332 gcc_assert (ret_val);
7334 #ifdef OBJCPLUS
7335 finish_return_stmt (ret_val);
7336 #else
7337 c_finish_return (location, ret_val, NULL_TREE);
7338 #endif
7340 add_stmt (c_end_compound_stmt (location, body, true));
7341 fn = current_function_decl;
7342 #ifdef OBJCPLUS
7343 finish_function ();
7344 #endif
7345 objc_finish_method_definition (fn);
7348 /* This routine synthesizes a 'setter' method. */
7350 static void
7351 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7353 location_t location = DECL_SOURCE_LOCATION (property);
7354 tree fn, decl;
7355 tree body;
7356 tree new_value, statement;
7358 /* If user has implemented a setter with same name then do nothing. */
7359 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7360 PROPERTY_SETTER_NAME (property)))
7361 return;
7363 /* Find declaration of the property setter in the interface (or
7364 superclass, or protocol). There must be one. */
7365 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7367 /* If one not declared in the interface, this condition has already
7368 been reported as user error (because property was not declared in
7369 the interface). */
7370 if (!decl)
7371 return;
7373 /* Adapt the 'decl'. Use the source location of the @synthesize
7374 statement for error messages. */
7375 decl = copy_node (decl);
7376 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7378 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7379 NULL_TREE);
7381 body = c_begin_compound_stmt (true);
7383 /* The 'new_value' is the only argument to the method, which is the
7384 3rd argument of the function, after self and _cmd. We use twice
7385 TREE_CHAIN to move forward two arguments. */
7386 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7388 /* This would presumably happen if the user has specified a
7389 prototype for the setter that does not have an argument! */
7390 if (new_value == NULL_TREE)
7392 /* TODO: This should be caught much earlier than this. */
7393 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7394 /* Try to recover somehow. */
7395 new_value = error_mark_node;
7398 /* Now we need to decide how we build the setter. There are three
7399 cases:
7401 for 'copy' or 'retain' properties we need to use the
7402 objc_setProperty() accessor helper which knows about retain and
7403 copy. It supports both 'nonatomic' and 'atomic' access.
7405 for 'nonatomic, assign' properties we can access the instance
7406 variable directly. 'nonatomic' means we don't have to use locks,
7407 and 'assign' means we don't have to worry about retain or copy.
7408 If you combine the two, it means we can just access the instance
7409 variable directly.
7411 for 'atomic, assign' properties we use objc_copyStruct() (for the
7412 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7413 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7415 case OBJC_PROPERTY_RETAIN:
7416 case OBJC_PROPERTY_COPY:
7418 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7419 tree cmd, ivar, offset, is_atomic, should_copy;
7420 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7422 /* Find the ivar to compute the offset. */
7423 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7424 if (!ivar || is_private (ivar))
7426 error_at (location,
7427 "can not find instance variable associated with property");
7428 statement = error_mark_node;
7429 break;
7431 offset = byte_position (ivar);
7433 if (PROPERTY_NONATOMIC (property))
7434 is_atomic = boolean_false_node;
7435 else
7436 is_atomic = boolean_true_node;
7438 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7439 should_copy = boolean_true_node;
7440 else
7441 should_copy = boolean_false_node;
7443 statement = build_function_call
7444 (location,
7445 /* Function prototype. */
7446 objc_setProperty_decl,
7447 /* Parameters. */
7448 tree_cons /* self */
7449 (NULL_TREE, self_decl,
7450 tree_cons /* _cmd */
7451 (NULL_TREE, cmd,
7452 tree_cons /* offset */
7453 (NULL_TREE, offset,
7454 tree_cons /* new_value */
7455 (NULL_TREE, new_value,
7456 tree_cons /* is_atomic */
7457 (NULL_TREE, is_atomic,
7458 tree_cons /* should_copy */
7459 (NULL_TREE, should_copy, NULL_TREE)))))));
7461 break;
7462 case OBJC_PROPERTY_ASSIGN:
7463 if (PROPERTY_NONATOMIC (property))
7465 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7466 statement = build_modify_expr
7467 (location,
7468 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7469 NULL_TREE, NOP_EXPR,
7470 location, new_value, NULL_TREE);
7471 break;
7473 else
7475 /* We build
7476 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7477 &new_value,
7478 sizeof (type of self->PROPERTY_IVAR_NAME),
7479 is_atomic,
7480 false)
7482 For the NeXT runtime, we need to use objc_copyStruct
7483 instead of objc_getPropertyStruct. */
7484 tree function_decl, size_of, is_atomic;
7486 /* sizeof (ivar type). Since the ivar and the property have
7487 the same type, there is no need to lookup the ivar. */
7488 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7489 true /* is_sizeof */,
7490 false /* min_alignof */,
7491 false /* complain */);
7493 if (PROPERTY_NONATOMIC (property))
7494 is_atomic = boolean_false_node;
7495 else
7496 is_atomic = boolean_true_node;
7498 if (objc_copyStruct_decl)
7499 function_decl = objc_copyStruct_decl;
7500 else
7501 function_decl = objc_setPropertyStruct_decl;
7503 statement = build_function_call
7504 (location,
7505 /* Function prototype. */
7506 function_decl,
7507 /* Parameters. */
7508 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7509 (NULL_TREE, build_fold_addr_expr_loc (location,
7510 objc_lookup_ivar
7511 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7512 tree_cons /* &new_value */
7513 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7514 tree_cons /* sizeof (PROPERTY_IVAR) */
7515 (NULL_TREE, size_of,
7516 tree_cons /* is_atomic */
7517 (NULL_TREE, is_atomic,
7518 /* TODO: This is currently ignored by the GNU
7519 runtime, but what about the next one ? */
7520 tree_cons /* has_strong */
7521 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7523 break;
7524 default:
7525 gcc_unreachable ();
7527 gcc_assert (statement);
7529 add_stmt (statement);
7530 add_stmt (c_end_compound_stmt (location, body, true));
7531 fn = current_function_decl;
7532 #ifdef OBJCPLUS
7533 finish_function ();
7534 #endif
7535 objc_finish_method_definition (fn);
7538 /* This function is a sub-routine of objc_add_synthesize_declaration.
7539 It is called for each property to synthesize once we have
7540 determined that the context is Ok. */
7541 static void
7542 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7543 tree property_name, tree ivar_name)
7545 /* Find the @property declaration. */
7546 tree property;
7547 tree x;
7549 /* Check that synthesize or dynamic has not already been used for
7550 the same property. */
7551 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7552 if (PROPERTY_NAME (property) == property_name)
7554 location_t original_location = DECL_SOURCE_LOCATION (property);
7556 if (PROPERTY_DYNAMIC (property))
7557 error_at (location, "property %qs already specified in %<@dynamic%>",
7558 IDENTIFIER_POINTER (property_name));
7559 else
7560 error_at (location, "property %qs already specified in %<@synthesize%>",
7561 IDENTIFIER_POINTER (property_name));
7563 if (original_location != UNKNOWN_LOCATION)
7564 inform (original_location, "originally specified here");
7565 return;
7568 /* Check that the property is declared in the interface. It could
7569 also be declared in a superclass or protocol. */
7570 property = lookup_property (interface, property_name);
7572 if (!property)
7574 error_at (location, "no declaration of property %qs found in the interface",
7575 IDENTIFIER_POINTER (property_name));
7576 return;
7578 else
7580 /* We have to copy the property, because we want to chain it to
7581 the implementation context, and we want to store the source
7582 location of the @synthesize, not of the original
7583 @property. */
7584 property = copy_node (property);
7585 DECL_SOURCE_LOCATION (property) = location;
7588 /* Determine PROPERTY_IVAR_NAME. */
7589 if (ivar_name == NULL_TREE)
7590 ivar_name = property_name;
7592 /* Check that the instance variable exists. You can only use an
7593 instance variable from the same class, not one from the
7594 superclass (this makes sense as it allows us to check that an
7595 instance variable is only used in one synthesized property). */
7597 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7598 tree type_of_ivar;
7599 if (!ivar)
7601 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7602 IDENTIFIER_POINTER (property_name));
7603 return;
7606 if (DECL_BIT_FIELD_TYPE (ivar))
7607 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7608 else
7609 type_of_ivar = TREE_TYPE (ivar);
7611 /* If the instance variable has a different C type, we throw an error ... */
7612 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7613 /* ... unless the property is readonly, in which case we allow
7614 the instance variable to be more specialized (this means we
7615 can generate the getter all right and it works). */
7616 && (!PROPERTY_READONLY (property)
7617 || !objc_compare_types (TREE_TYPE (property),
7618 type_of_ivar, -5, NULL_TREE)))
7620 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7622 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7623 IDENTIFIER_POINTER (property_name),
7624 IDENTIFIER_POINTER (ivar_name));
7626 if (original_location != UNKNOWN_LOCATION)
7627 inform (original_location, "originally specified here");
7630 /* If the instance variable is a bitfield, the property must be
7631 'assign', 'nonatomic' because the runtime getter/setter helper
7632 do not work with bitfield instance variables. */
7633 if (DECL_BIT_FIELD_TYPE (ivar))
7635 /* If there is an error, we return and not generate any
7636 getter/setter because trying to set up the runtime
7637 getter/setter helper calls with bitfields is at high risk
7638 of ICE. */
7640 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7642 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7644 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
7645 IDENTIFIER_POINTER (property_name),
7646 IDENTIFIER_POINTER (ivar_name));
7648 if (original_location != UNKNOWN_LOCATION)
7649 inform (original_location, "originally specified here");
7650 return;
7653 if (!PROPERTY_NONATOMIC (property))
7655 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7657 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
7658 IDENTIFIER_POINTER (property_name),
7659 IDENTIFIER_POINTER (ivar_name));
7661 if (original_location != UNKNOWN_LOCATION)
7662 inform (original_location, "originally specified here");
7663 return;
7668 /* Check that no other property is using the same instance
7669 variable. */
7670 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7671 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7673 location_t original_location = DECL_SOURCE_LOCATION (x);
7675 error_at (location, "property %qs is using the same instance variable as property %qs",
7676 IDENTIFIER_POINTER (property_name),
7677 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7679 if (original_location != UNKNOWN_LOCATION)
7680 inform (original_location, "originally specified here");
7682 /* We keep going on. This won't cause the compiler to fail;
7683 the failure would most likely be at runtime. */
7686 /* Note that a @synthesize (and only a @synthesize) always sets
7687 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7688 @synthesize by that. */
7689 PROPERTY_IVAR_NAME (property) = ivar_name;
7691 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7692 original declaration; they are always set (with the exception of
7693 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7695 /* Add the property to the list of properties for current implementation. */
7696 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7697 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7699 /* Note how we don't actually synthesize the getter/setter here; it
7700 would be very natural, but we may miss the fact that the user has
7701 implemented his own getter/setter later on in the @implementation
7702 (in which case we shouldn't generate getter/setter). We wait
7703 until we have parsed it all before generating the code. */
7706 /* This function is called by the parser after a @synthesize
7707 expression is parsed. 'location' is the location of the
7708 @synthesize expression, and 'property_and_ivar_list' is a chained
7709 list of the property and ivar names. */
7710 void
7711 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7713 tree interface, chain;
7715 if (flag_objc1_only)
7716 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7718 if (property_and_ivar_list == error_mark_node)
7719 return;
7721 if (!objc_implementation_context)
7723 /* We can get here only in Objective-C; the Objective-C++ parser
7724 detects the problem while parsing, outputs the error
7725 "misplaced '@synthesize' Objective-C++ construct" and skips
7726 the declaration. */
7727 error_at (location, "%<@synthesize%> not in @implementation context");
7728 return;
7731 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7733 error_at (location, "%<@synthesize%> can not be used in categories");
7734 return;
7737 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7738 if (!interface)
7740 /* I can't see how this could happen, but it is good as a safety check. */
7741 error_at (location,
7742 "%<@synthesize%> requires the @interface of the class to be available");
7743 return;
7746 /* Now, iterate over the properties and do each of them. */
7747 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7749 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7750 TREE_PURPOSE (chain));
7754 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7755 is called for each property to mark as dynamic once we have
7756 determined that the context is Ok. */
7757 static void
7758 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7759 tree property_name)
7761 /* Find the @property declaration. */
7762 tree property;
7764 /* Check that synthesize or dynamic has not already been used for
7765 the same property. */
7766 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7767 if (PROPERTY_NAME (property) == property_name)
7769 location_t original_location = DECL_SOURCE_LOCATION (property);
7771 if (PROPERTY_DYNAMIC (property))
7772 error_at (location, "property %qs already specified in %<@dynamic%>",
7773 IDENTIFIER_POINTER (property_name));
7774 else
7775 error_at (location, "property %qs already specified in %<@synthesize%>",
7776 IDENTIFIER_POINTER (property_name));
7778 if (original_location != UNKNOWN_LOCATION)
7779 inform (original_location, "originally specified here");
7780 return;
7783 /* Check that the property is declared in the interface. It could
7784 also be declared in a superclass or protocol. */
7785 property = lookup_property (interface, property_name);
7787 if (!property)
7789 error_at (location, "no declaration of property %qs found in the interface",
7790 IDENTIFIER_POINTER (property_name));
7791 return;
7793 else
7795 /* We have to copy the property, because we want to chain it to
7796 the implementation context, and we want to store the source
7797 location of the @synthesize, not of the original
7798 @property. */
7799 property = copy_node (property);
7800 DECL_SOURCE_LOCATION (property) = location;
7803 /* Note that a @dynamic (and only a @dynamic) always sets
7804 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7805 (actually, as explained above, PROPERTY_DECL generated by
7806 @property and associated with a @dynamic property are also marked
7807 as PROPERTY_DYNAMIC). */
7808 PROPERTY_DYNAMIC (property) = 1;
7810 /* Add the property to the list of properties for current implementation. */
7811 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7812 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7815 /* This function is called by the parser after a @dynamic expression
7816 is parsed. 'location' is the location of the @dynamic expression,
7817 and 'property_list' is a chained list of all the property
7818 names. */
7819 void
7820 objc_add_dynamic_declaration (location_t location, tree property_list)
7822 tree interface, chain;
7824 if (flag_objc1_only)
7825 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7827 if (property_list == error_mark_node)
7828 return;
7830 if (!objc_implementation_context)
7832 /* We can get here only in Objective-C; the Objective-C++ parser
7833 detects the problem while parsing, outputs the error
7834 "misplaced '@dynamic' Objective-C++ construct" and skips the
7835 declaration. */
7836 error_at (location, "%<@dynamic%> not in @implementation context");
7837 return;
7840 /* @dynamic is allowed in categories. */
7841 switch (TREE_CODE (objc_implementation_context))
7843 case CLASS_IMPLEMENTATION_TYPE:
7844 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7845 break;
7846 case CATEGORY_IMPLEMENTATION_TYPE:
7847 interface = lookup_category (implementation_template,
7848 CLASS_SUPER_NAME (objc_implementation_context));
7849 break;
7850 default:
7851 gcc_unreachable ();
7854 if (!interface)
7856 /* I can't see how this could happen, but it is good as a safety check. */
7857 error_at (location,
7858 "%<@dynamic%> requires the @interface of the class to be available");
7859 return;
7862 /* Now, iterate over the properties and do each of them. */
7863 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7865 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7869 /* Main routine to generate code/data for all the property information for
7870 current implementation (class or category). CLASS is the interface where
7871 ivars are declared. CLASS_METHODS is where methods are found which
7872 could be a class or a category depending on whether we are implementing
7873 property of a class or a category. */
7875 static void
7876 objc_gen_property_data (tree klass, tree class_methods)
7878 tree x;
7880 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7882 /* @dynamic property - nothing to check or synthesize. */
7883 if (PROPERTY_DYNAMIC (x))
7884 continue;
7886 /* @synthesize property - need to synthesize the accessors. */
7887 if (PROPERTY_IVAR_NAME (x))
7889 objc_synthesize_getter (klass, class_methods, x);
7891 if (PROPERTY_READONLY (x) == 0)
7892 objc_synthesize_setter (klass, class_methods, x);
7894 continue;
7897 gcc_unreachable ();
7901 /* This is called once we see the "@end" in an interface/implementation. */
7903 static void
7904 finish_class (tree klass)
7906 switch (TREE_CODE (klass))
7908 case CLASS_IMPLEMENTATION_TYPE:
7910 /* All metadata generation is done in runtime.generate_metadata(). */
7912 /* Generate what needed for property; setters, getters, etc. */
7913 objc_gen_property_data (implementation_template, implementation_template);
7915 if (implementation_template != objc_implementation_context)
7917 /* Ensure that all method listed in the interface contain bodies. */
7918 check_methods (CLASS_CLS_METHODS (implementation_template),
7919 objc_implementation_context, '+');
7920 check_methods (CLASS_NST_METHODS (implementation_template),
7921 objc_implementation_context, '-');
7923 if (CLASS_PROTOCOL_LIST (implementation_template))
7924 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7925 "class",
7926 CLASS_NAME (objc_implementation_context));
7928 break;
7930 case CATEGORY_IMPLEMENTATION_TYPE:
7932 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7934 if (category)
7936 /* Generate what needed for property; setters, getters, etc. */
7937 objc_gen_property_data (implementation_template, category);
7939 /* Ensure all method listed in the interface contain bodies. */
7940 check_methods (CLASS_CLS_METHODS (category),
7941 objc_implementation_context, '+');
7942 check_methods (CLASS_NST_METHODS (category),
7943 objc_implementation_context, '-');
7945 if (CLASS_PROTOCOL_LIST (category))
7946 check_protocols (CLASS_PROTOCOL_LIST (category),
7947 "category",
7948 CLASS_SUPER_NAME (objc_implementation_context));
7950 break;
7952 case CLASS_INTERFACE_TYPE:
7953 case CATEGORY_INTERFACE_TYPE:
7954 case PROTOCOL_INTERFACE_TYPE:
7956 /* Process properties of the class. */
7957 tree x;
7958 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7960 /* Now we check that the appropriate getter is declared,
7961 and if not, we declare one ourselves. */
7962 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7963 PROPERTY_GETTER_NAME (x));
7965 if (getter_decl)
7967 /* TODO: Check that the declaration is consistent with the property. */
7970 else
7972 /* Generate an instance method declaration for the
7973 getter; for example "- (id) name;". In general it
7974 will be of the form
7975 -(type)property_getter_name; */
7976 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
7977 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7978 rettype, PROPERTY_GETTER_NAME (x),
7979 NULL_TREE, false);
7980 if (PROPERTY_OPTIONAL (x))
7981 objc_add_method (objc_interface_context, getter_decl, false, true);
7982 else
7983 objc_add_method (objc_interface_context, getter_decl, false, false);
7984 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
7985 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
7988 if (PROPERTY_READONLY (x) == 0)
7990 /* Now we check that the appropriate setter is declared,
7991 and if not, we declare on ourselves. */
7992 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
7993 PROPERTY_SETTER_NAME (x));
7995 if (setter_decl)
7997 /* TODO: Check that the declaration is consistent with the property. */
8000 else
8002 /* The setter name is something like 'setName:'.
8003 We need the substring 'setName' to build the
8004 method declaration due to how the declaration
8005 works. TODO: build_method_decl() will then
8006 generate back 'setName:' from 'setName'; it
8007 would be more efficient to hook into there. */
8008 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8009 size_t length = strlen (full_setter_name);
8010 char *setter_name = (char *) alloca (length);
8011 tree ret_type, selector, arg_type, arg_name;
8013 strcpy (setter_name, full_setter_name);
8014 setter_name[length - 1] = '\0';
8015 ret_type = build_tree_list (NULL_TREE, void_type_node);
8016 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8017 arg_name = get_identifier ("_value");
8018 selector = objc_build_keyword_decl (get_identifier (setter_name),
8019 arg_type, arg_name, NULL);
8020 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8021 ret_type, selector,
8022 build_tree_list (NULL_TREE, NULL_TREE),
8023 false);
8024 if (PROPERTY_OPTIONAL (x))
8025 objc_add_method (objc_interface_context, setter_decl, false, true);
8026 else
8027 objc_add_method (objc_interface_context, setter_decl, false, false);
8028 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8029 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8033 break;
8035 default:
8036 gcc_unreachable ();
8037 break;
8041 static tree
8042 add_protocol (tree protocol)
8044 /* Put protocol on list in reverse order. */
8045 TREE_CHAIN (protocol) = protocol_chain;
8046 protocol_chain = protocol;
8047 return protocol_chain;
8050 /* Check that a protocol is defined, and, recursively, that all
8051 protocols that this protocol conforms to are defined too. */
8052 static void
8053 check_that_protocol_is_defined (tree protocol)
8055 if (!PROTOCOL_DEFINED (protocol))
8056 warning (0, "definition of protocol %qE not found",
8057 PROTOCOL_NAME (protocol));
8059 /* If the protocol itself conforms to other protocols, check them
8060 too, recursively. */
8061 if (PROTOCOL_LIST (protocol))
8063 tree p;
8065 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8066 check_that_protocol_is_defined (TREE_VALUE (p));
8070 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8071 emitted if the protocol is deprecated. If 'definition_required' is
8072 true, a warning is emitted if a full @protocol definition has not
8073 been seen. */
8074 static tree
8075 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8077 tree chain;
8079 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8080 if (ident == PROTOCOL_NAME (chain))
8082 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8084 /* It would be nice to use warn_deprecated_use() here, but
8085 we are using TREE_CHAIN (which is supposed to be the
8086 TYPE_STUB_DECL for a TYPE) for something different. */
8087 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8088 PROTOCOL_NAME (chain));
8091 if (definition_required)
8092 check_that_protocol_is_defined (chain);
8094 return chain;
8097 return NULL_TREE;
8100 /* This function forward declares the protocols named by NAMES. If
8101 they are already declared or defined, the function has no effect. */
8103 void
8104 objc_declare_protocol (tree name, tree attributes)
8106 bool deprecated = false;
8108 #ifdef OBJCPLUS
8109 if (current_namespace != global_namespace) {
8110 error ("Objective-C declarations may only appear in global scope");
8112 #endif /* OBJCPLUS */
8114 /* Determine if 'deprecated', the only attribute we recognize for
8115 protocols, was used. Ignore all other attributes. */
8116 if (attributes)
8118 tree attribute;
8119 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8121 tree name = TREE_PURPOSE (attribute);
8123 if (is_attribute_p ("deprecated", name))
8124 deprecated = true;
8125 else
8126 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8130 if (lookup_protocol (name, /* warn if deprecated */ false,
8131 /* definition_required */ false) == NULL_TREE)
8133 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8135 TYPE_LANG_SLOT_1 (protocol)
8136 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8137 PROTOCOL_NAME (protocol) = name;
8138 PROTOCOL_LIST (protocol) = NULL_TREE;
8139 add_protocol (protocol);
8140 PROTOCOL_DEFINED (protocol) = 0;
8141 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8143 if (attributes)
8145 /* TODO: Do we need to store the attributes here ? */
8146 TYPE_ATTRIBUTES (protocol) = attributes;
8147 if (deprecated)
8148 TREE_DEPRECATED (protocol) = 1;
8153 static tree
8154 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8156 tree protocol;
8157 bool deprecated = false;
8159 #ifdef OBJCPLUS
8160 if (current_namespace != global_namespace) {
8161 error ("Objective-C declarations may only appear in global scope");
8163 #endif /* OBJCPLUS */
8165 /* Determine if 'deprecated', the only attribute we recognize for
8166 protocols, was used. Ignore all other attributes. */
8167 if (attributes)
8169 tree attribute;
8170 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8172 tree name = TREE_PURPOSE (attribute);
8174 if (is_attribute_p ("deprecated", name))
8175 deprecated = true;
8176 else
8177 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8181 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8182 /* definition_required */ false);
8184 if (!protocol)
8186 protocol = make_node (code);
8187 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8189 PROTOCOL_NAME (protocol) = name;
8190 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8191 add_protocol (protocol);
8192 PROTOCOL_DEFINED (protocol) = 1;
8193 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8195 check_protocol_recursively (protocol, list);
8197 else if (! PROTOCOL_DEFINED (protocol))
8199 PROTOCOL_DEFINED (protocol) = 1;
8200 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8202 check_protocol_recursively (protocol, list);
8204 else
8206 warning (0, "duplicate declaration for protocol %qE",
8207 name);
8210 if (attributes)
8212 TYPE_ATTRIBUTES (protocol) = attributes;
8213 if (deprecated)
8214 TREE_DEPRECATED (protocol) = 1;
8217 return protocol;
8220 /* Decay array and function parameters into pointers. */
8222 static tree
8223 objc_decay_parm_type (tree type)
8225 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8226 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8227 ? TREE_TYPE (type)
8228 : type);
8230 return type;
8233 static GTY(()) tree objc_parmlist = NULL_TREE;
8235 /* Append PARM to a list of formal parameters of a method, making a necessary
8236 array-to-pointer adjustment along the way. */
8238 void
8239 objc_push_parm (tree parm)
8241 tree type;
8243 if (TREE_TYPE (parm) == error_mark_node)
8245 objc_parmlist = chainon (objc_parmlist, parm);
8246 return;
8249 /* Decay arrays and functions into pointers. */
8250 type = objc_decay_parm_type (TREE_TYPE (parm));
8252 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8253 built as well. */
8254 if (type != TREE_TYPE (parm))
8255 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8257 DECL_ARG_TYPE (parm)
8258 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8260 /* Record constancy and volatility. */
8261 c_apply_type_quals_to_decl
8262 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8263 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8264 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
8265 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8267 objc_parmlist = chainon (objc_parmlist, parm);
8270 /* Retrieve the formal parameter list constructed via preceding calls to
8271 objc_push_parm(). */
8273 #ifdef OBJCPLUS
8274 tree
8275 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8276 tree expr ATTRIBUTE_UNUSED)
8278 tree parm_info = objc_parmlist;
8279 objc_parmlist = NULL_TREE;
8281 return parm_info;
8283 #else
8284 struct c_arg_info *
8285 objc_get_parm_info (int have_ellipsis, tree expr)
8287 tree parm_info = objc_parmlist;
8288 struct c_arg_info *arg_info;
8289 /* The C front-end requires an elaborate song and dance at
8290 this point. */
8291 push_scope ();
8292 declare_parm_level ();
8293 while (parm_info)
8295 tree next = DECL_CHAIN (parm_info);
8297 DECL_CHAIN (parm_info) = NULL_TREE;
8298 parm_info = pushdecl (parm_info);
8299 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8300 parm_info = next;
8302 arg_info = get_parm_info (have_ellipsis, expr);
8303 pop_scope ();
8304 objc_parmlist = NULL_TREE;
8305 return arg_info;
8307 #endif
8309 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8310 method definitions. In the case of instance methods, we can be more
8311 specific as to the type of 'self'. */
8313 static void
8314 synth_self_and_ucmd_args (void)
8316 tree self_type;
8318 if (objc_method_context
8319 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8320 self_type = objc_instance_type;
8321 else
8322 /* Really a `struct objc_class *'. However, we allow people to
8323 assign to self, which changes its type midstream. */
8324 self_type = objc_object_type;
8326 /* id self; */
8327 objc_push_parm (build_decl (input_location,
8328 PARM_DECL, self_id, self_type));
8330 /* SEL _cmd; */
8331 objc_push_parm (build_decl (input_location,
8332 PARM_DECL, ucmd_id, objc_selector_type));
8335 /* Transform an Objective-C method definition into a static C function
8336 definition, synthesizing the first two arguments, "self" and "_cmd",
8337 in the process. EXPR is NULL or an expression that needs to be
8338 evaluated for the side effects of array size expressions in the
8339 parameters. */
8341 static void
8342 start_method_def (tree method, tree expr)
8344 tree parmlist;
8345 #ifdef OBJCPLUS
8346 tree parm_info;
8347 #else
8348 struct c_arg_info *parm_info;
8349 #endif
8350 int have_ellipsis = 0;
8352 /* If we are defining a "dealloc" method in a non-root class, we
8353 will need to check if a [super dealloc] is missing, and warn if
8354 it is. */
8355 if(CLASS_SUPER_NAME (objc_implementation_context)
8356 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8357 should_call_super_dealloc = 1;
8358 else
8359 should_call_super_dealloc = 0;
8361 /* Required to implement _msgSuper. */
8362 objc_method_context = method;
8363 UOBJC_SUPER_decl = NULL_TREE;
8365 /* Generate prototype declarations for arguments..."new-style". */
8366 synth_self_and_ucmd_args ();
8368 /* Generate argument declarations if a keyword_decl. */
8369 parmlist = METHOD_SEL_ARGS (method);
8370 while (parmlist)
8372 /* parmlist is a KEYWORD_DECL. */
8373 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8374 tree parm;
8376 parm = build_decl (input_location,
8377 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8378 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8379 objc_push_parm (parm);
8380 parmlist = DECL_CHAIN (parmlist);
8383 if (METHOD_ADD_ARGS (method))
8385 tree akey;
8387 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8388 akey; akey = TREE_CHAIN (akey))
8390 objc_push_parm (TREE_VALUE (akey));
8393 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8394 have_ellipsis = 1;
8397 parm_info = objc_get_parm_info (have_ellipsis, expr);
8399 really_start_method (objc_method_context, parm_info);
8402 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8403 overloading. */
8404 static int
8405 objc_types_are_equivalent (tree type1, tree type2)
8407 if (type1 == type2)
8408 return 1;
8410 /* Strip away indirections. */
8411 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8412 && (TREE_CODE (type1) == TREE_CODE (type2)))
8413 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8414 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8415 return 0;
8417 /* Compare the protocol lists. */
8418 type1 = (TYPE_HAS_OBJC_INFO (type1)
8419 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8420 : NULL_TREE);
8421 type2 = (TYPE_HAS_OBJC_INFO (type2)
8422 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8423 : NULL_TREE);
8425 /* If there are no protocols (most common case), the types are
8426 identical. */
8427 if (type1 == NULL_TREE && type2 == NULL_TREE)
8428 return 1;
8430 /* If one has protocols, and the other one hasn't, they are not
8431 identical. */
8432 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8433 || (type1 != NULL_TREE && type2 == NULL_TREE))
8434 return 0;
8435 else
8437 /* Else, both have protocols, and we need to do the full
8438 comparison. It is possible that either type1 or type2
8439 contain some duplicate protocols in the list, so we can't
8440 even just compare list_length as a first check. */
8441 tree t;
8443 for (t = type2; t; t = TREE_CHAIN (t))
8444 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8445 return 0;
8447 for (t = type1; t; t = TREE_CHAIN (t))
8448 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8449 return 0;
8451 return 1;
8455 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8457 static int
8458 objc_types_share_size_and_alignment (tree type1, tree type2)
8460 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8461 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8464 /* Return 1 if PROTO1 is equivalent to PROTO2
8465 for purposes of method overloading. Ordinarily, the type signatures
8466 should match up exactly, unless STRICT is zero, in which case we
8467 shall allow differences in which the size and alignment of a type
8468 is the same. */
8470 static int
8471 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8473 tree type1, type2;
8475 /* The following test is needed in case there are hashing
8476 collisions. */
8477 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8478 return 0;
8480 /* Compare return types. */
8481 type1 = TREE_VALUE (TREE_TYPE (proto1));
8482 type2 = TREE_VALUE (TREE_TYPE (proto2));
8484 if (!objc_types_are_equivalent (type1, type2)
8485 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8486 return 0;
8488 /* Compare argument types. */
8490 /* The first argument (objc_object_type) is always the same, no need
8491 to compare. */
8493 /* The second argument (objc_selector_type) is always the same, no
8494 need to compare. */
8496 /* Compare the other arguments. */
8498 tree arg1, arg2;
8500 /* Compare METHOD_SEL_ARGS. */
8501 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8502 arg1 && arg2;
8503 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8505 type1 = TREE_VALUE (TREE_TYPE (arg1));
8506 type2 = TREE_VALUE (TREE_TYPE (arg2));
8508 /* FIXME: Do we need to decay argument types to compare them ? */
8509 type1 = objc_decay_parm_type (type1);
8510 type2 = objc_decay_parm_type (type2);
8512 if (!objc_types_are_equivalent (type1, type2)
8513 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8514 return 0;
8517 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8518 both NULL. */
8519 if (arg1 != arg2)
8520 return 0;
8522 /* Compare METHOD_ADD_ARGS. */
8523 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8524 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8525 return 0;
8527 if (METHOD_ADD_ARGS (proto1))
8529 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8530 arg1 && arg2;
8531 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8533 type1 = TREE_TYPE (TREE_VALUE (arg1));
8534 type2 = TREE_TYPE (TREE_VALUE (arg2));
8536 /* FIXME: Do we need to decay argument types to compare them ? */
8537 type1 = objc_decay_parm_type (type1);
8538 type2 = objc_decay_parm_type (type2);
8540 if (!objc_types_are_equivalent (type1, type2)
8541 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8542 return 0;
8546 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8547 both NULL. */
8548 if (arg1 != arg2)
8549 return 0;
8551 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8552 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8553 return 0;
8556 /* Success. */
8557 return 1;
8560 /* This routine returns true if TYPE is a valid objc object type,
8561 suitable for messaging; false otherwise. If 'accept_class' is
8562 'true', then a Class object is considered valid for messaging and
8563 'true' is returned if 'type' refers to a Class. If 'accept_class'
8564 is 'false', then a Class object is not considered valid for
8565 messaging and 'false' is returned in that case. */
8567 static bool
8568 objc_type_valid_for_messaging (tree type, bool accept_classes)
8570 if (!POINTER_TYPE_P (type))
8571 return false;
8573 /* Remove the pointer indirection; don't remove more than one
8574 otherwise we'd consider "NSObject **" a valid type for messaging,
8575 which it isn't. */
8576 type = TREE_TYPE (type);
8578 if (TREE_CODE (type) != RECORD_TYPE)
8579 return false;
8581 if (objc_is_object_id (type))
8582 return true;
8584 if (objc_is_class_id (type))
8585 return accept_classes;
8587 if (TYPE_HAS_OBJC_INFO (type))
8588 return true;
8590 return false;
8593 void
8594 objc_start_function (tree name, tree type, tree attrs,
8595 #ifdef OBJCPLUS
8596 tree params
8597 #else
8598 struct c_arg_info *params
8599 #endif
8602 tree fndecl = build_decl (input_location,
8603 FUNCTION_DECL, name, type);
8605 #ifdef OBJCPLUS
8606 DECL_ARGUMENTS (fndecl) = params;
8607 DECL_INITIAL (fndecl) = error_mark_node;
8608 DECL_EXTERNAL (fndecl) = 0;
8609 TREE_STATIC (fndecl) = 1;
8610 retrofit_lang_decl (fndecl);
8611 cplus_decl_attributes (&fndecl, attrs, 0);
8612 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8613 #else
8614 current_function_returns_value = 0; /* Assume, until we see it does. */
8615 current_function_returns_null = 0;
8616 decl_attributes (&fndecl, attrs, 0);
8617 announce_function (fndecl);
8618 DECL_INITIAL (fndecl) = error_mark_node;
8619 DECL_EXTERNAL (fndecl) = 0;
8620 TREE_STATIC (fndecl) = 1;
8621 current_function_decl = pushdecl (fndecl);
8622 push_scope ();
8623 declare_parm_level ();
8624 DECL_RESULT (current_function_decl)
8625 = build_decl (input_location,
8626 RESULT_DECL, NULL_TREE,
8627 TREE_TYPE (TREE_TYPE (current_function_decl)));
8628 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8629 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8630 start_fname_decls ();
8631 store_parm_decls_from (params);
8632 #endif
8634 TREE_USED (current_function_decl) = 1;
8637 /* - Generate an identifier for the function. the format is "_n_cls",
8638 where 1 <= n <= nMethods, and cls is the name the implementation we
8639 are processing.
8640 - Install the return type from the method declaration.
8641 - If we have a prototype, check for type consistency. */
8643 static void
8644 really_start_method (tree method,
8645 #ifdef OBJCPLUS
8646 tree parmlist
8647 #else
8648 struct c_arg_info *parmlist
8649 #endif
8652 tree ret_type, meth_type;
8653 tree method_id;
8654 const char *sel_name, *class_name, *cat_name;
8655 char *buf;
8657 /* Synth the storage class & assemble the return type. */
8658 ret_type = TREE_VALUE (TREE_TYPE (method));
8660 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8661 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8662 cat_name = ((TREE_CODE (objc_implementation_context)
8663 == CLASS_IMPLEMENTATION_TYPE)
8664 ? NULL
8665 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8666 method_slot++;
8668 /* Make sure this is big enough for any plausible method label. */
8669 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8670 + (cat_name ? strlen (cat_name) : 0));
8672 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8673 class_name, cat_name, sel_name, method_slot);
8675 method_id = get_identifier (buf);
8677 #ifdef OBJCPLUS
8678 /* Objective-C methods cannot be overloaded, so we don't need
8679 the type encoding appended. It looks bad anyway... */
8680 push_lang_context (lang_name_c);
8681 #endif
8683 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8684 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8686 /* Set self_decl from the first argument. */
8687 self_decl = DECL_ARGUMENTS (current_function_decl);
8689 /* Suppress unused warnings. */
8690 TREE_USED (self_decl) = 1;
8691 DECL_READ_P (self_decl) = 1;
8692 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8693 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8694 #ifdef OBJCPLUS
8695 pop_lang_context ();
8696 #endif
8698 METHOD_DEFINITION (method) = current_function_decl;
8700 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8702 if (implementation_template != objc_implementation_context)
8704 tree proto
8705 = lookup_method_static (implementation_template,
8706 METHOD_SEL_NAME (method),
8707 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8708 | OBJC_LOOKUP_NO_SUPER));
8710 if (proto)
8712 if (!comp_proto_with_proto (method, proto, 1))
8714 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8716 warning_at (DECL_SOURCE_LOCATION (method), 0,
8717 "conflicting types for %<%c%s%>",
8718 (type ? '-' : '+'),
8719 identifier_to_locale (gen_method_decl (method)));
8720 inform (DECL_SOURCE_LOCATION (proto),
8721 "previous declaration of %<%c%s%>",
8722 (type ? '-' : '+'),
8723 identifier_to_locale (gen_method_decl (proto)));
8725 else
8727 /* If the method in the @interface was deprecated, mark
8728 the implemented method as deprecated too. It should
8729 never be used for messaging (when the deprecation
8730 warnings are produced), but just in case. */
8731 if (TREE_DEPRECATED (proto))
8732 TREE_DEPRECATED (method) = 1;
8734 /* If the method in the @interface was marked as
8735 'noreturn', mark the function implementing the method
8736 as 'noreturn' too. */
8737 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8740 else
8742 /* We have a method @implementation even though we did not
8743 see a corresponding @interface declaration (which is allowed
8744 by Objective-C rules). Go ahead and place the method in
8745 the @interface anyway, so that message dispatch lookups
8746 will see it. */
8747 tree interface = implementation_template;
8749 if (TREE_CODE (objc_implementation_context)
8750 == CATEGORY_IMPLEMENTATION_TYPE)
8751 interface = lookup_category
8752 (interface,
8753 CLASS_SUPER_NAME (objc_implementation_context));
8755 if (interface)
8756 objc_add_method (interface, copy_node (method),
8757 TREE_CODE (method) == CLASS_METHOD_DECL,
8758 /* is_optional= */ false);
8763 static void *UOBJC_SUPER_scope = 0;
8765 /* _n_Method (id self, SEL sel, ...)
8767 struct objc_super _S;
8768 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8769 } */
8771 static tree
8772 get_super_receiver (void)
8774 if (objc_method_context)
8776 tree super_expr, super_expr_list, class_expr;
8777 bool inst_meth;
8778 if (!UOBJC_SUPER_decl)
8780 UOBJC_SUPER_decl = build_decl (input_location,
8781 VAR_DECL, get_identifier (TAG_SUPER),
8782 objc_super_template);
8783 /* This prevents `unused variable' warnings when compiling with -Wall. */
8784 TREE_USED (UOBJC_SUPER_decl) = 1;
8785 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8786 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8787 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8788 NULL_TREE);
8789 UOBJC_SUPER_scope = objc_get_current_scope ();
8792 /* Set receiver to self. */
8793 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8794 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8795 NOP_EXPR, input_location, self_decl,
8796 NULL_TREE);
8797 super_expr_list = super_expr;
8799 /* Set class to begin searching. */
8800 /* Get the ident for the superclass class field & build a ref to it.
8801 ??? maybe we should just name the field the same for all runtimes. */
8802 super_expr = (*runtime.super_superclassfield_ident) ();
8803 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8805 gcc_assert (imp_list->imp_context == objc_implementation_context
8806 && imp_list->imp_template == implementation_template);
8807 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8809 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8810 class_expr = (*runtime.get_class_super_ref) (input_location,
8811 imp_list, inst_meth);
8812 else
8813 /* We have a category. */
8815 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8816 tree super_class;
8818 /* Barf if super used in a category of a root object. */
8819 if (!super_name)
8821 error ("no super class declared in interface for %qE",
8822 CLASS_NAME (imp_list->imp_template));
8823 return error_mark_node;
8826 super_class = (*runtime.get_category_super_ref) (input_location,
8827 imp_list, inst_meth);
8828 class_expr = build_c_cast (input_location,
8829 TREE_TYPE (super_expr), super_class);
8832 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8833 NOP_EXPR,
8834 input_location, class_expr, NULL_TREE);
8836 super_expr_list = build_compound_expr (input_location,
8837 super_expr_list, super_expr);
8839 super_expr = build_unary_op (input_location,
8840 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8841 super_expr_list = build_compound_expr (input_location,
8842 super_expr_list, super_expr);
8844 return super_expr_list;
8846 else
8848 error ("[super ...] must appear in a method context");
8849 return error_mark_node;
8853 /* When exiting a scope, sever links to a 'super' declaration (if any)
8854 therein contained. */
8856 void
8857 objc_clear_super_receiver (void)
8859 if (objc_method_context
8860 && UOBJC_SUPER_scope == objc_get_current_scope ())
8862 UOBJC_SUPER_decl = 0;
8863 UOBJC_SUPER_scope = 0;
8867 void
8868 objc_finish_method_definition (tree fndecl)
8870 /* We cannot validly inline ObjC methods, at least not without a language
8871 extension to declare that a method need not be dynamically
8872 dispatched, so suppress all thoughts of doing so. */
8873 DECL_UNINLINABLE (fndecl) = 1;
8875 #ifndef OBJCPLUS
8876 /* The C++ front-end will have called finish_function() for us. */
8877 finish_function ();
8878 #endif
8880 METHOD_ENCODING (objc_method_context)
8881 = encode_method_prototype (objc_method_context);
8883 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8884 since the optimizer may find "may be used before set" errors. */
8885 objc_method_context = NULL_TREE;
8887 if (should_call_super_dealloc)
8888 warning (0, "method possibly missing a [super dealloc] call");
8891 /* Given a tree DECL node, produce a printable description of it in the given
8892 buffer, overwriting the buffer. */
8894 static char *
8895 gen_declaration (tree decl)
8897 errbuf[0] = '\0';
8899 if (DECL_P (decl))
8901 gen_type_name_0 (TREE_TYPE (decl));
8903 if (DECL_NAME (decl))
8905 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8906 strcat (errbuf, " ");
8908 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
8911 if (DECL_INITIAL (decl)
8912 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
8913 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8914 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
8917 return errbuf;
8920 /* Given a tree TYPE node, produce a printable description of it in the given
8921 buffer, overwriting the buffer. */
8923 static char *
8924 gen_type_name_0 (tree type)
8926 tree orig = type, proto;
8928 if (TYPE_P (type) && TYPE_NAME (type))
8929 type = TYPE_NAME (type);
8930 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
8932 tree inner = TREE_TYPE (type);
8934 while (TREE_CODE (inner) == ARRAY_TYPE)
8935 inner = TREE_TYPE (inner);
8937 gen_type_name_0 (inner);
8939 if (!POINTER_TYPE_P (inner))
8940 strcat (errbuf, " ");
8942 if (POINTER_TYPE_P (type))
8943 strcat (errbuf, "*");
8944 else
8945 while (type != inner)
8947 strcat (errbuf, "[");
8949 if (TYPE_DOMAIN (type))
8951 char sz[20];
8953 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8954 (TREE_INT_CST_LOW
8955 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8956 strcat (errbuf, sz);
8959 strcat (errbuf, "]");
8960 type = TREE_TYPE (type);
8963 goto exit_function;
8966 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8967 type = DECL_NAME (type);
8969 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
8970 ? IDENTIFIER_POINTER (type)
8971 : "");
8973 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8974 if (objc_is_id (orig))
8975 orig = TREE_TYPE (orig);
8977 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8979 if (proto)
8981 strcat (errbuf, " <");
8983 while (proto) {
8984 strcat (errbuf,
8985 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
8986 proto = TREE_CHAIN (proto);
8987 strcat (errbuf, proto ? ", " : ">");
8991 exit_function:
8992 return errbuf;
8995 static char *
8996 gen_type_name (tree type)
8998 errbuf[0] = '\0';
9000 return gen_type_name_0 (type);
9003 /* Given a method tree, put a printable description into the given
9004 buffer (overwriting) and return a pointer to the buffer. */
9006 static char *
9007 gen_method_decl (tree method)
9009 tree chain;
9011 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9012 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9013 strcat (errbuf, ")");
9014 chain = METHOD_SEL_ARGS (method);
9016 if (chain)
9018 /* We have a chain of keyword_decls. */
9021 if (KEYWORD_KEY_NAME (chain))
9022 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9024 strcat (errbuf, ":(");
9025 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9026 strcat (errbuf, ")");
9028 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9029 if ((chain = DECL_CHAIN (chain)))
9030 strcat (errbuf, " ");
9032 while (chain);
9034 if (METHOD_ADD_ARGS (method))
9036 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9038 /* Know we have a chain of parm_decls. */
9039 while (chain)
9041 strcat (errbuf, ", ");
9042 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9043 chain = TREE_CHAIN (chain);
9046 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9047 strcat (errbuf, ", ...");
9051 else
9052 /* We have a unary selector. */
9053 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9055 return errbuf;
9058 /* Debug info. */
9061 /* Dump an @interface declaration of the supplied class CHAIN to the
9062 supplied file FP. Used to implement the -gen-decls option (which
9063 prints out an @interface declaration of all classes compiled in
9064 this run); potentially useful for debugging the compiler too. */
9065 void
9066 dump_interface (FILE *fp, tree chain)
9068 /* FIXME: A heap overflow here whenever a method (or ivar)
9069 declaration is so long that it doesn't fit in the buffer. The
9070 code and all the related functions should be rewritten to avoid
9071 using fixed size buffers. */
9072 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9073 tree ivar_decls = CLASS_RAW_IVARS (chain);
9074 tree nst_methods = CLASS_NST_METHODS (chain);
9075 tree cls_methods = CLASS_CLS_METHODS (chain);
9077 fprintf (fp, "\n@interface %s", my_name);
9079 /* CLASS_SUPER_NAME is used to store the superclass name for
9080 classes, and the category name for categories. */
9081 if (CLASS_SUPER_NAME (chain))
9083 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9085 switch (TREE_CODE (chain))
9087 case CATEGORY_IMPLEMENTATION_TYPE:
9088 case CATEGORY_INTERFACE_TYPE:
9089 fprintf (fp, " (%s)\n", name);
9090 break;
9091 default:
9092 fprintf (fp, " : %s\n", name);
9093 break;
9096 else
9097 fprintf (fp, "\n");
9099 /* FIXME - the following doesn't seem to work at the moment. */
9100 if (ivar_decls)
9102 fprintf (fp, "{\n");
9105 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9106 ivar_decls = TREE_CHAIN (ivar_decls);
9108 while (ivar_decls);
9109 fprintf (fp, "}\n");
9112 while (nst_methods)
9114 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9115 nst_methods = TREE_CHAIN (nst_methods);
9118 while (cls_methods)
9120 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9121 cls_methods = TREE_CHAIN (cls_methods);
9124 fprintf (fp, "@end\n");
9127 #if 0
9128 /* Produce the pretty printing for an Objective-C method. This is
9129 currently unused, but could be handy while reorganizing the pretty
9130 printing to be more robust. */
9131 static const char *
9132 objc_pretty_print_method (bool is_class_method,
9133 const char *class_name,
9134 const char *category_name,
9135 const char *selector)
9137 if (category_name)
9139 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9140 + strlen (selector) + 7);
9142 if (is_class_method)
9143 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9144 else
9145 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9147 return result;
9149 else
9151 char *result = XNEWVEC (char, strlen (class_name)
9152 + strlen (selector) + 5);
9154 if (is_class_method)
9155 sprintf (result, "+[%s %s]", class_name, selector);
9156 else
9157 sprintf (result, "-[%s %s]", class_name, selector);
9159 return result;
9162 #endif
9164 /* Demangle function for Objective-C. Attempt to demangle the
9165 function name associated with a method (eg, going from
9166 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9167 purpose of pretty printing or error messages. Return the demangled
9168 name, or NULL if the string is not an Objective-C mangled method
9169 name.
9171 Because of how the mangling is done, any method that has a '_' in
9172 its original name is at risk of being demangled incorrectly. In
9173 some cases there are multiple valid ways to demangle a method name
9174 and there is no way we can decide.
9176 TODO: objc_demangle() can't always get it right; the right way to
9177 get this correct for all method names would be to store the
9178 Objective-C method name somewhere in the function decl. Then,
9179 there is no demangling to do; we'd just pull the method name out of
9180 the decl. As an additional bonus, when printing error messages we
9181 could check for such a method name, and if we find it, we know the
9182 function is actually an Objective-C method and we could print error
9183 messages saying "In method '+[NSObject class]" instead of "In
9184 function '+[NSObject class]" as we do now. */
9185 static const char *
9186 objc_demangle (const char *mangled)
9188 char *demangled, *cp;
9190 /* First of all, if the name is too short it can't be an Objective-C
9191 mangled method name. */
9192 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9193 return NULL;
9195 /* If the name looks like an already demangled one, return it
9196 unchanged. This should only happen on Darwin, where method names
9197 are mangled differently into a pretty-print form (such as
9198 '+[NSObject class]', see darwin.h). In that case, demangling is
9199 a no-op, but we need to return the demangled name if it was an
9200 ObjC one, and return NULL if not. We should be safe as no C/C++
9201 function can start with "-[" or "+[". */
9202 if ((mangled[0] == '-' || mangled[0] == '+')
9203 && (mangled[1] == '['))
9204 return mangled;
9206 if (mangled[0] == '_' &&
9207 (mangled[1] == 'i' || mangled[1] == 'c') &&
9208 mangled[2] == '_')
9210 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9211 if (mangled[1] == 'i')
9212 *cp++ = '-'; /* for instance method */
9213 else
9214 *cp++ = '+'; /* for class method */
9215 *cp++ = '['; /* opening left brace */
9216 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9217 while (*cp && *cp == '_')
9218 cp++; /* skip any initial underbars in class name */
9219 cp = strchr(cp, '_'); /* find first non-initial underbar */
9220 if (cp == NULL)
9222 free(demangled); /* not mangled name */
9223 return NULL;
9225 if (cp[1] == '_') /* easy case: no category name */
9227 *cp++ = ' '; /* replace two '_' with one ' ' */
9228 strcpy(cp, mangled + (cp - demangled) + 2);
9230 else
9232 *cp++ = '('; /* less easy case: category name */
9233 cp = strchr(cp, '_');
9234 if (cp == 0)
9236 free(demangled); /* not mangled name */
9237 return NULL;
9239 *cp++ = ')';
9240 *cp++ = ' '; /* overwriting 1st char of method name... */
9241 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9243 /* Now we have the method name. We need to generally replace
9244 '_' with ':' but trying to preserve '_' if it could only have
9245 been in the mangled string because it was already in the
9246 original name. In cases where it's ambiguous, we assume that
9247 any '_' originated from a ':'. */
9249 /* Initial '_'s in method name can't have been generating by
9250 converting ':'s. Skip them. */
9251 while (*cp && *cp == '_')
9252 cp++;
9254 /* If the method name does not end with '_', then it has no
9255 arguments and there was no replacement of ':'s with '_'s
9256 during mangling. Check for that case, and skip any
9257 replacement if so. This at least guarantees that methods
9258 with no arguments are always demangled correctly (unless the
9259 original name ends with '_'). */
9260 if (*(mangled + strlen (mangled) - 1) != '_')
9262 /* Skip to the end. */
9263 for (; *cp; cp++)
9266 else
9268 /* Replace remaining '_' with ':'. This may get it wrong if
9269 there were '_'s in the original name. In most cases it
9270 is impossible to disambiguate. */
9271 for (; *cp; cp++)
9272 if (*cp == '_')
9273 *cp = ':';
9275 *cp++ = ']'; /* closing right brace */
9276 *cp++ = 0; /* string terminator */
9277 return demangled;
9279 else
9280 return NULL; /* not an objc mangled name */
9283 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9284 specific decl, return the printable name for it. If not, return
9285 NULL. */
9286 const char *
9287 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9289 switch (TREE_CODE (decl))
9291 case FUNCTION_DECL:
9292 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9293 break;
9295 /* The following happens when we are printing a deprecation
9296 warning for a method. The warn_deprecation() will end up
9297 trying to print the decl for INSTANCE_METHOD_DECL or
9298 CLASS_METHOD_DECL. It would be nice to be able to print
9299 "-[NSObject autorelease] is deprecated", but to do that, we'd
9300 need to store the class and method name in the method decl,
9301 which we currently don't do. For now, just return the name
9302 of the method. We don't return NULL, because that may
9303 trigger further attempts to pretty-print the decl in C/C++,
9304 but they wouldn't know how to pretty-print it. */
9305 case INSTANCE_METHOD_DECL:
9306 case CLASS_METHOD_DECL:
9307 return IDENTIFIER_POINTER (DECL_NAME (decl));
9308 break;
9309 /* This happens when printing a deprecation warning for a
9310 property. We may want to consider some sort of pretty
9311 printing (eg, include the class name where it was declared
9312 ?). */
9313 case PROPERTY_DECL:
9314 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9315 break;
9316 default:
9317 return NULL;
9318 break;
9322 /* Return a printable name for 'decl'. This first tries
9323 objc_maybe_printable_name(), and if that fails, it returns the name
9324 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9325 Objective-C; in Objective-C++, setting the hook is not enough
9326 because lots of C++ Front-End code calls cxx_printable_name,
9327 dump_decl and other C++ functions directly. So instead we have
9328 modified dump_decl to call objc_maybe_printable_name directly. */
9329 const char *
9330 objc_printable_name (tree decl, int v)
9332 const char *demangled_name = objc_maybe_printable_name (decl, v);
9334 if (demangled_name != NULL)
9335 return demangled_name;
9336 else
9337 return IDENTIFIER_POINTER (DECL_NAME (decl));
9340 /* Routine is called to issue diagnostic when reference to a private
9341 ivar is made and no other variable with same name is found in
9342 current scope. */
9343 bool
9344 objc_diagnose_private_ivar (tree id)
9346 tree ivar;
9347 if (!objc_method_context)
9348 return false;
9349 ivar = is_ivar (objc_ivar_chain, id);
9350 if (ivar && is_private (ivar))
9352 error ("instance variable %qs is declared private",
9353 IDENTIFIER_POINTER (id));
9354 return true;
9356 return false;
9359 /* Look up ID as an instance variable. OTHER contains the result of
9360 the C or C++ lookup, which we may want to use instead. */
9361 /* To use properties inside an instance method, use self.property. */
9362 tree
9363 objc_lookup_ivar (tree other, tree id)
9365 tree ivar;
9367 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9368 if (!objc_method_context)
9369 return other;
9371 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9372 /* We have a message to super. */
9373 return get_super_receiver ();
9375 /* In a class method, look up an instance variable only as a last
9376 resort. */
9377 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9378 && other && other != error_mark_node)
9379 return other;
9381 /* Don't look up the ivar if the user has explicitly advised against
9382 it with -fno-local-ivars. */
9384 if (!flag_local_ivars)
9385 return other;
9387 /* Look up the ivar, but do not use it if it is not accessible. */
9388 ivar = is_ivar (objc_ivar_chain, id);
9390 if (!ivar || is_private (ivar))
9391 return other;
9393 /* In an instance method, a local variable (or parameter) may hide the
9394 instance variable. */
9395 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9396 && other && other != error_mark_node
9397 #ifdef OBJCPLUS
9398 && CP_DECL_CONTEXT (other) != global_namespace)
9399 #else
9400 && !DECL_FILE_SCOPE_P (other))
9401 #endif
9403 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9404 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9405 "local declaration of %qE hides instance variable", id);
9408 return other;
9411 /* At this point, we are either in an instance method with no obscuring
9412 local definitions, or in a class method with no alternate definitions
9413 at all. */
9414 return build_ivar_reference (id);
9417 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9418 needs to be done if we are calling a function through a cast. */
9420 tree
9421 objc_rewrite_function_call (tree function, tree first_param)
9423 if (TREE_CODE (function) == NOP_EXPR
9424 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9425 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9426 == FUNCTION_DECL)
9428 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9429 TREE_OPERAND (function, 0),
9430 first_param, size_zero_node);
9433 return function;
9436 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9437 corresponding 'getter' function call. Note that we assume the
9438 PROPERTY_REF to be valid since we generated it while parsing. */
9439 static void
9440 objc_gimplify_property_ref (tree *expr_p)
9442 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9443 tree call_exp;
9445 if (getter == NULL_TREE)
9447 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9448 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9449 should be impossible for real properties, which always
9450 have a getter. */
9451 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9452 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9453 /* Try to recover from the error to prevent an ICE. We take
9454 zero and cast it to the type of the property. */
9455 *expr_p = convert (TREE_TYPE (property_decl),
9456 integer_zero_node);
9457 return;
9460 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9462 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9463 that is deprecated. */
9464 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9465 NULL_TREE);
9468 call_exp = getter;
9469 #ifdef OBJCPLUS
9470 /* In C++, a getter which returns an aggregate value results in a
9471 target_expr which initializes a temporary to the call
9472 expression. */
9473 if (TREE_CODE (getter) == TARGET_EXPR)
9475 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9476 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9477 call_exp = TREE_OPERAND (getter, 1);
9479 #endif
9480 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9482 *expr_p = call_exp;
9485 /* This is called when "gimplifying" the trees. We need to gimplify
9486 the Objective-C/Objective-C++ specific trees, then hand over the
9487 process to C/C++. */
9489 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9491 enum tree_code code = TREE_CODE (*expr_p);
9492 switch (code)
9494 /* Look for the special case of OBJC_TYPE_REF with the address
9495 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9496 or one of its cousins). */
9497 case OBJ_TYPE_REF:
9498 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9499 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9500 == FUNCTION_DECL)
9502 enum gimplify_status r0, r1;
9504 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9505 value of the OBJ_TYPE_REF, so force them to be emitted
9506 during subexpression evaluation rather than after the
9507 OBJ_TYPE_REF. This permits objc_msgSend calls in
9508 Objective C to use direct rather than indirect calls when
9509 the object expression has a postincrement. */
9510 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9511 is_gimple_val, fb_rvalue);
9512 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9513 is_gimple_val, fb_rvalue);
9515 return MIN (r0, r1);
9517 break;
9518 case PROPERTY_REF:
9519 objc_gimplify_property_ref (expr_p);
9520 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9521 break;
9522 default:
9523 break;
9526 #ifdef OBJCPLUS
9527 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9528 #else
9529 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9530 #endif
9533 /* --- FAST ENUMERATION --- */
9534 /* Begin code generation for fast enumeration (foreach) ... */
9536 /* Defines
9538 struct __objcFastEnumerationState
9540 unsigned long state;
9541 id *itemsPtr;
9542 unsigned long *mutationsPtr;
9543 unsigned long extra[5];
9546 Confusingly enough, NSFastEnumeration is then defined by libraries
9547 to be the same structure.
9550 static void
9551 build_fast_enumeration_state_template (void)
9553 tree decls, *chain = NULL;
9555 /* { */
9556 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9557 (TAG_FAST_ENUMERATION_STATE));
9559 /* unsigned long state; */
9560 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9562 /* id *itemsPtr; */
9563 add_field_decl (build_pointer_type (objc_object_type),
9564 "itemsPtr", &chain);
9566 /* unsigned long *mutationsPtr; */
9567 add_field_decl (build_pointer_type (long_unsigned_type_node),
9568 "mutationsPtr", &chain);
9570 /* unsigned long extra[5]; */
9571 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9572 "extra", &chain);
9574 /* } */
9575 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9579 'objc_finish_foreach_loop()' generates the code for an Objective-C
9580 foreach loop. The 'location' argument is the location of the 'for'
9581 that starts the loop. The 'object_expression' is the expression of
9582 the 'object' that iterates; the 'collection_expression' is the
9583 expression of the collection that we iterate over (we need to make
9584 sure we evaluate this only once); the 'for_body' is the set of
9585 statements to be executed in each iteration; 'break_label' and
9586 'continue_label' are the break and continue labels which we need to
9587 emit since the <statements> may be jumping to 'break_label' (if they
9588 contain 'break') or to 'continue_label' (if they contain
9589 'continue').
9591 The syntax is
9593 for (<object expression> in <collection expression>)
9594 <statements>
9596 which is compiled into the following blurb:
9599 id __objc_foreach_collection;
9600 __objc_fast_enumeration_state __objc_foreach_enum_state;
9601 unsigned long __objc_foreach_batchsize;
9602 id __objc_foreach_items[16];
9603 __objc_foreach_collection = <collection expression>;
9604 __objc_foreach_enum_state = { 0 };
9605 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9607 if (__objc_foreach_batchsize == 0)
9608 <object expression> = nil;
9609 else
9611 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9612 next_batch:
9614 unsigned long __objc_foreach_index;
9615 __objc_foreach_index = 0;
9617 next_object:
9618 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9619 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9620 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9622 continue_label:
9623 __objc_foreach_index++;
9624 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9625 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9627 if (__objc_foreach_batchsize != 0) goto next_batch;
9628 <object expression> = nil;
9629 break_label:
9633 'statements' may contain a 'continue' or 'break' instruction, which
9634 the user expects to 'continue' or 'break' the entire foreach loop.
9635 We are provided the labels that 'break' and 'continue' jump to, so
9636 we place them where we want them to jump to when they pick them.
9638 Optimization TODO: we could cache the IMP of
9639 countByEnumeratingWithState:objects:count:.
9642 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9643 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9645 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9646 #include "tree-pretty-print.h"
9647 #endif
9649 void
9650 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9651 tree break_label, tree continue_label)
9653 /* A tree representing the __objcFastEnumerationState struct type,
9654 or NSFastEnumerationState struct, whatever we are using. */
9655 tree objc_fast_enumeration_state_type;
9657 /* The trees representing the declarations of each of the local variables. */
9658 tree objc_foreach_collection_decl;
9659 tree objc_foreach_enum_state_decl;
9660 tree objc_foreach_items_decl;
9661 tree objc_foreach_batchsize_decl;
9662 tree objc_foreach_mutations_pointer_decl;
9663 tree objc_foreach_index_decl;
9665 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9666 tree selector_name;
9668 /* A tree representing the local bind. */
9669 tree bind;
9671 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9672 tree first_if;
9674 /* A tree representing the 'else' part of 'first_if' */
9675 tree first_else;
9677 /* A tree representing the 'next_batch' label. */
9678 tree next_batch_label_decl;
9680 /* A tree representing the binding after the 'next_batch' label. */
9681 tree next_batch_bind;
9683 /* A tree representing the 'next_object' label. */
9684 tree next_object_label_decl;
9686 /* Temporary variables. */
9687 tree t;
9688 int i;
9690 if (flag_objc1_only)
9691 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9693 if (object_expression == error_mark_node)
9694 return;
9696 if (collection_expression == error_mark_node)
9697 return;
9699 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9701 error_at (location, "iterating variable in fast enumeration is not an object");
9702 return;
9705 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9707 error_at (location, "collection in fast enumeration is not an object");
9708 return;
9711 /* TODO: Check that object_expression is either a variable
9712 declaration, or an lvalue. */
9714 /* This kludge is an idea from apple. We use the
9715 __objcFastEnumerationState struct implicitly defined by the
9716 compiler, unless a NSFastEnumerationState struct has been defined
9717 (by a Foundation library such as GNUstep Base) in which case, we
9718 use that one.
9720 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9722 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9724 if (objc_NSFastEnumeration_type)
9726 /* TODO: We really need to check that
9727 objc_NSFastEnumeration_type is the same as ours! */
9728 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9730 /* If it's a typedef, use the original type. */
9731 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9732 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9733 else
9734 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9739 /* { */
9740 /* Done by c-parser.c. */
9742 /* type object; */
9743 /* Done by c-parser.c. */
9745 /* Disable warnings that 'object' is unused. For example the code
9747 for (id object in collection)
9748 i++;
9750 which can be used to count how many objects there are in the
9751 collection is fine and should generate no warnings even if
9752 'object' is technically unused. */
9753 TREE_USED (object_expression) = 1;
9754 if (DECL_P (object_expression))
9755 DECL_READ_P (object_expression) = 1;
9757 /* id __objc_foreach_collection */
9758 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9760 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9761 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9762 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9764 /* id __objc_foreach_items[16]; */
9765 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9766 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9768 /* unsigned long __objc_foreach_batchsize; */
9769 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9770 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9772 /* Generate the local variable binding. */
9773 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9774 SET_EXPR_LOCATION (bind, location);
9775 TREE_SIDE_EFFECTS (bind) = 1;
9777 /* __objc_foreach_collection = <collection expression>; */
9778 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9779 SET_EXPR_LOCATION (t, location);
9780 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9781 /* We have used 'collection_expression'. */
9782 mark_exp_read (collection_expression);
9784 /* __objc_foreach_enum_state.state = 0; */
9785 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9786 get_identifier ("state")),
9787 build_int_cst (long_unsigned_type_node, 0));
9788 SET_EXPR_LOCATION (t, location);
9789 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9791 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9792 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9793 get_identifier ("itemsPtr")),
9794 null_pointer_node);
9795 SET_EXPR_LOCATION (t, location);
9796 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9798 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9799 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9800 get_identifier ("mutationsPtr")),
9801 null_pointer_node);
9802 SET_EXPR_LOCATION (t, location);
9803 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9805 /* __objc_foreach_enum_state.extra[0] = 0; */
9806 /* __objc_foreach_enum_state.extra[1] = 0; */
9807 /* __objc_foreach_enum_state.extra[2] = 0; */
9808 /* __objc_foreach_enum_state.extra[3] = 0; */
9809 /* __objc_foreach_enum_state.extra[4] = 0; */
9810 for (i = 0; i < 5 ; i++)
9812 t = build2 (MODIFY_EXPR, void_type_node,
9813 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9814 get_identifier ("extra")),
9815 build_int_cst (NULL_TREE, i)),
9816 build_int_cst (long_unsigned_type_node, 0));
9817 SET_EXPR_LOCATION (t, location);
9818 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9821 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9822 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9823 #ifdef OBJCPLUS
9824 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9825 /* Parameters. */
9826 tree_cons /* &__objc_foreach_enum_state */
9827 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9828 tree_cons /* __objc_foreach_items */
9829 (NULL_TREE, objc_foreach_items_decl,
9830 tree_cons /* 16 */
9831 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9832 #else
9833 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9835 struct c_expr array;
9836 array.value = objc_foreach_items_decl;
9837 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9838 /* Parameters. */
9839 tree_cons /* &__objc_foreach_enum_state */
9840 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9841 tree_cons /* __objc_foreach_items */
9842 (NULL_TREE, default_function_array_conversion (location, array).value,
9843 tree_cons /* 16 */
9844 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9846 #endif
9847 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9848 convert (long_unsigned_type_node, t));
9849 SET_EXPR_LOCATION (t, location);
9850 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9852 /* if (__objc_foreach_batchsize == 0) */
9853 first_if = build3 (COND_EXPR, void_type_node,
9854 /* Condition. */
9855 c_fully_fold
9856 (c_common_truthvalue_conversion
9857 (location,
9858 build_binary_op (location,
9859 EQ_EXPR,
9860 objc_foreach_batchsize_decl,
9861 build_int_cst (long_unsigned_type_node, 0), 1)),
9862 false, NULL),
9863 /* Then block (we fill it in later). */
9864 NULL_TREE,
9865 /* Else block (we fill it in later). */
9866 NULL_TREE);
9867 SET_EXPR_LOCATION (first_if, location);
9868 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9870 /* then <object expression> = nil; */
9871 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9872 SET_EXPR_LOCATION (t, location);
9873 COND_EXPR_THEN (first_if) = t;
9875 /* Now we build the 'else' part of the if; once we finish building
9876 it, we attach it to first_if as the 'else' part. */
9878 /* else */
9879 /* { */
9881 /* unsigned long __objc_foreach_mutations_pointer; */
9882 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
9884 /* Generate the local variable binding. */
9885 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9886 SET_EXPR_LOCATION (first_else, location);
9887 TREE_SIDE_EFFECTS (first_else) = 1;
9889 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9890 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9891 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9892 get_identifier ("mutationsPtr")),
9893 RO_UNARY_STAR));
9894 SET_EXPR_LOCATION (t, location);
9895 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9897 /* next_batch: */
9898 next_batch_label_decl = create_artificial_label (location);
9899 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
9900 SET_EXPR_LOCATION (t, location);
9901 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9903 /* { */
9905 /* unsigned long __objc_foreach_index; */
9906 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
9908 /* Generate the local variable binding. */
9909 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9910 SET_EXPR_LOCATION (next_batch_bind, location);
9911 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9912 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
9914 /* __objc_foreach_index = 0; */
9915 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9916 build_int_cst (long_unsigned_type_node, 0));
9917 SET_EXPR_LOCATION (t, location);
9918 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9920 /* next_object: */
9921 next_object_label_decl = create_artificial_label (location);
9922 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9923 SET_EXPR_LOCATION (t, location);
9924 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9926 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9927 t = build3 (COND_EXPR, void_type_node,
9928 /* Condition. */
9929 c_fully_fold
9930 (c_common_truthvalue_conversion
9931 (location,
9932 build_binary_op
9933 (location,
9934 NE_EXPR,
9935 objc_foreach_mutations_pointer_decl,
9936 build_indirect_ref (location,
9937 objc_build_component_ref (objc_foreach_enum_state_decl,
9938 get_identifier ("mutationsPtr")),
9939 RO_UNARY_STAR), 1)),
9940 false, NULL),
9941 /* Then block. */
9942 build_function_call (input_location,
9943 objc_enumeration_mutation_decl,
9944 tree_cons (NULL, collection_expression, NULL)),
9945 /* Else block. */
9946 NULL_TREE);
9947 SET_EXPR_LOCATION (t, location);
9948 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9950 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
9951 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9952 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9953 get_identifier ("itemsPtr")),
9954 objc_foreach_index_decl));
9955 SET_EXPR_LOCATION (t, location);
9956 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9958 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9959 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
9961 /* continue_label: */
9962 if (continue_label)
9964 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9965 SET_EXPR_LOCATION (t, location);
9966 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9969 /* __objc_foreach_index++; */
9970 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9971 build_binary_op (location,
9972 PLUS_EXPR,
9973 objc_foreach_index_decl,
9974 build_int_cst (long_unsigned_type_node, 1), 1));
9975 SET_EXPR_LOCATION (t, location);
9976 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9978 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
9979 t = build3 (COND_EXPR, void_type_node,
9980 /* Condition. */
9981 c_fully_fold
9982 (c_common_truthvalue_conversion
9983 (location,
9984 build_binary_op (location,
9985 LT_EXPR,
9986 objc_foreach_index_decl,
9987 objc_foreach_batchsize_decl, 1)),
9988 false, NULL),
9989 /* Then block. */
9990 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
9991 /* Else block. */
9992 NULL_TREE);
9993 SET_EXPR_LOCATION (t, location);
9994 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9996 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9997 #ifdef OBJCPLUS
9998 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9999 /* Parameters. */
10000 tree_cons /* &__objc_foreach_enum_state */
10001 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10002 tree_cons /* __objc_foreach_items */
10003 (NULL_TREE, objc_foreach_items_decl,
10004 tree_cons /* 16 */
10005 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10006 #else
10007 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10009 struct c_expr array;
10010 array.value = objc_foreach_items_decl;
10011 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10012 /* Parameters. */
10013 tree_cons /* &__objc_foreach_enum_state */
10014 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10015 tree_cons /* __objc_foreach_items */
10016 (NULL_TREE, default_function_array_conversion (location, array).value,
10017 tree_cons /* 16 */
10018 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10020 #endif
10021 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
10022 convert (long_unsigned_type_node, t));
10023 SET_EXPR_LOCATION (t, location);
10024 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10026 /* } */
10028 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10029 t = build3 (COND_EXPR, void_type_node,
10030 /* Condition. */
10031 c_fully_fold
10032 (c_common_truthvalue_conversion
10033 (location,
10034 build_binary_op (location,
10035 NE_EXPR,
10036 objc_foreach_batchsize_decl,
10037 build_int_cst (long_unsigned_type_node, 0), 1)),
10038 false, NULL),
10039 /* Then block. */
10040 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10041 /* Else block. */
10042 NULL_TREE);
10043 SET_EXPR_LOCATION (t, location);
10044 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10046 /* <object expression> = nil; */
10047 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10048 SET_EXPR_LOCATION (t, location);
10049 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10051 /* break_label: */
10052 if (break_label)
10054 t = build1 (LABEL_EXPR, void_type_node, break_label);
10055 SET_EXPR_LOCATION (t, location);
10056 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10059 /* } */
10060 COND_EXPR_ELSE (first_if) = first_else;
10062 /* Do the whole thing. */
10063 add_stmt (bind);
10065 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10066 /* This will print to stderr the whole blurb generated by the
10067 compiler while compiling (assuming the compiler doesn't crash
10068 before getting here).
10070 debug_generic_stmt (bind);
10071 #endif
10073 /* } */
10074 /* Done by c-parser.c */
10077 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10078 /* Return true if we have an NxString object pointer. */
10080 bool
10081 objc_string_ref_type_p (tree strp)
10083 tree tmv;
10084 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10085 return false;
10087 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10088 tmv = OBJC_TYPE_NAME (tmv);
10089 return (tmv
10090 && TREE_CODE (tmv) == IDENTIFIER_NODE
10091 && IDENTIFIER_POINTER (tmv)
10092 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10095 /* At present the behavior of this is undefined and it does nothing. */
10096 void
10097 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10098 tree ARG_UNUSED (args_list))
10102 void
10103 objc_common_init_ts (void)
10105 c_common_init_ts ();
10107 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10108 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10109 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10110 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10112 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10113 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10114 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10116 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10117 MARK_TS_TYPED (PROPERTY_REF);
10120 size_t
10121 objc_common_tree_size (enum tree_code code)
10123 switch (code)
10125 case CLASS_METHOD_DECL:
10126 case INSTANCE_METHOD_DECL:
10127 case KEYWORD_DECL:
10128 case PROPERTY_DECL:
10129 return sizeof (struct tree_decl_non_common);
10130 default:
10131 gcc_unreachable ();
10137 #include "gt-objc-objc-act.h"