PR c++/63283
[official-gcc.git] / gcc / objc / objc-act.c
blob610e770e8895281637b54c19ecf640597a2adbab
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "stringpool.h"
38 #include "stor-layout.h"
39 #include "attribs.h"
41 #ifdef OBJCPLUS
42 #include "cp/cp-tree.h"
43 #else
44 #include "c/c-tree.h"
45 #include "c/c-lang.h"
46 #endif
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-pragma.h"
51 #include "c-family/c-format.h"
52 #include "flags.h"
53 #include "langhooks.h"
54 #include "objc-act.h"
55 #include "objc-map.h"
56 #include "input.h"
57 #include "hard-reg-set.h"
58 #include "function.h"
59 #include "toplev.h"
60 #include "debug.h"
61 #include "c-family/c-target.h"
62 #include "diagnostic-core.h"
63 #include "intl.h"
64 #include "hash-map.h"
65 #include "is-a.h"
66 #include "plugin-api.h"
67 #include "ipa-ref.h"
68 #include "cgraph.h"
69 #include "tree-iterator.h"
70 #include "hash-table.h"
71 #include "wide-int.h"
72 #include "langhooks-def.h"
73 /* Different initialization, code gen and meta data generation for each
74 runtime. */
75 #include "objc-runtime-hooks.h"
76 /* Routines used mainly by the runtimes. */
77 #include "objc-runtime-shared-support.h"
78 /* For default_tree_printer (). */
79 #include "tree-pretty-print.h"
81 /* For enum gimplify_status */
82 #include "gimple-expr.h"
83 #include "gimplify.h"
85 /* For encode_method_prototype(). */
86 #include "objc-encoding.h"
88 static unsigned int should_call_super_dealloc = 0;
90 /* When building Objective-C++, we are not linking against the C front-end
91 and so need to replicate the C tree-construction functions in some way. */
92 #ifdef OBJCPLUS
93 #define OBJCP_REMAP_FUNCTIONS
94 #include "objcp-decl.h"
95 #endif /* OBJCPLUS */
97 /* This is the default way of generating a method name. */
98 /* This has the problem that "test_method:argument:" and
99 "test:method_argument:" will generate the same name
100 ("_i_Test__test_method_argument_" for an instance method of the
101 class "Test"), so you can't have them both in the same class!
102 Moreover, the demangling (going from
103 "_i_Test__test_method_argument" back to the original name) is
104 undefined because there are two correct ways of demangling the
105 name. */
106 #ifndef OBJC_GEN_METHOD_LABEL
107 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
108 do { \
109 char *temp; \
110 sprintf ((BUF), "_%s_%s_%s_%s", \
111 ((IS_INST) ? "i" : "c"), \
112 (CLASS_NAME), \
113 ((CAT_NAME)? (CAT_NAME) : ""), \
114 (SEL_NAME)); \
115 for (temp = (BUF); *temp; temp++) \
116 if (*temp == ':') *temp = '_'; \
117 } while (0)
118 #endif
120 /* These need specifying. */
121 #ifndef OBJC_FORWARDING_STACK_OFFSET
122 #define OBJC_FORWARDING_STACK_OFFSET 0
123 #endif
125 #ifndef OBJC_FORWARDING_MIN_OFFSET
126 #define OBJC_FORWARDING_MIN_OFFSET 0
127 #endif
129 /*** Private Interface (procedures) ***/
131 /* Init stuff. */
132 static void synth_module_prologue (void);
134 /* Code generation. */
136 static tree start_class (enum tree_code, tree, tree, tree, tree);
137 static tree continue_class (tree);
138 static void finish_class (tree);
139 static void start_method_def (tree, tree);
141 static tree start_protocol (enum tree_code, tree, tree, tree);
142 static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
143 static tree objc_add_method (tree, tree, int, bool);
144 static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
145 static tree build_ivar_reference (tree);
146 static tree is_ivar (tree, tree);
148 /* We only need the following for ObjC; ObjC++ will use C++'s definition
149 of DERIVED_FROM_P. */
150 #ifndef OBJCPLUS
151 static bool objc_derived_from_p (tree, tree);
152 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
153 #endif
155 /* Property. */
156 static void objc_gen_property_data (tree, tree);
157 static void objc_synthesize_getter (tree, tree, tree);
158 static void objc_synthesize_setter (tree, tree, tree);
159 static tree lookup_property (tree, tree);
160 static tree lookup_property_in_list (tree, tree);
161 static tree lookup_property_in_protocol_list (tree, tree);
162 static void build_common_objc_property_accessor_helpers (void);
164 static void objc_xref_basetypes (tree, tree);
166 static tree get_class_ivars (tree, bool);
168 static void build_fast_enumeration_state_template (void);
170 #ifdef OBJCPLUS
171 static void objc_generate_cxx_cdtors (void);
172 #endif
174 /* objc attribute */
175 static void objc_decl_method_attributes (tree*, tree, int);
176 static tree build_keyword_selector (tree);
178 static void hash_init (void);
180 /* Hash tables to manage the global pool of method prototypes. Each
181 of these maps map a method name (selector) identifier to either a
182 single tree (for methods with a single method prototype) or a
183 TREE_VEC (for methods with multiple method prototypes). */
184 static GTY(()) objc_map_t instance_method_map = 0;
185 static GTY(()) objc_map_t class_method_map = 0;
187 /* Hash tables to manage the global pool of class names. */
189 static GTY(()) objc_map_t class_name_map = 0;
190 static GTY(()) objc_map_t alias_name_map = 0;
192 static tree lookup_method (tree, tree);
193 static tree lookup_method_static (tree, tree, int);
195 static void interface_hash_init (void);
196 static tree add_interface (tree, tree);
197 static void add_category (tree, tree);
198 static inline tree lookup_category (tree, tree);
200 /* Protocols. */
202 static tree lookup_protocol (tree, bool, bool);
203 static tree lookup_and_install_protocols (tree, bool);
205 #ifdef OBJCPLUS
206 static void really_start_method (tree, tree);
207 #else
208 static void really_start_method (tree, struct c_arg_info *);
209 #endif
210 static int comp_proto_with_proto (tree, tree, int);
211 static tree objc_decay_parm_type (tree);
213 /* Utilities for debugging and error diagnostics. */
215 static char *gen_type_name (tree);
216 static char *gen_type_name_0 (tree);
217 static char *gen_method_decl (tree);
218 static char *gen_declaration (tree);
220 /* Everything else. */
222 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
224 static void mark_referenced_methods (void);
225 static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
226 static tree check_duplicates (tree, int, int);
228 /*** Private Interface (data) ***/
229 /* Flags for lookup_method_static(). */
231 /* Look for class methods. */
232 #define OBJC_LOOKUP_CLASS 1
233 /* Do not examine superclasses. */
234 #define OBJC_LOOKUP_NO_SUPER 2
235 /* Disable returning an instance method of a root class when a class
236 method can't be found. */
237 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
239 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
240 tree objc_global_trees[OCTI_MAX];
242 struct imp_entry *imp_list = 0;
243 int imp_count = 0; /* `@implementation' */
244 int cat_count = 0; /* `@category' */
246 objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
248 /* Use to generate method labels. */
249 static int method_slot = 0;
251 /* Flag to say whether methods in a protocol are optional or
252 required. */
253 static bool objc_method_optional_flag = false;
255 static int objc_collecting_ivars = 0;
257 /* Flag that is set to 'true' while we are processing a class
258 extension. Since a class extension just "reopens" the main
259 @interface, this can be used to determine if we are in the main
260 @interface, or in a class extension. */
261 static bool objc_in_class_extension = false;
263 static char *errbuf; /* Buffer for error diagnostics */
265 /* An array of all the local variables in the current function that
266 need to be marked as volatile. */
267 vec<tree, va_gc> *local_variables_to_volatilize = NULL;
269 /* Store all constructed constant strings in a hash table so that
270 they get uniqued properly. */
272 struct GTY((for_user)) string_descriptor {
273 /* The literal argument . */
274 tree literal;
276 /* The resulting constant string. */
277 tree constructor;
280 struct objc_string_hasher : ggc_hasher<string_descriptor *>
282 static hashval_t hash (string_descriptor *);
283 static bool equal (string_descriptor *, string_descriptor *);
286 static GTY(()) hash_table<objc_string_hasher> *string_htab;
288 FILE *gen_declaration_file;
290 /* Hooks for stuff that differs between runtimes. */
291 objc_runtime_hooks runtime;
293 /* Create a temporary variable of type 'type'. If 'name' is set, uses
294 the specified name, else use no name. Returns the declaration of
295 the type. The 'name' is mostly useful for debugging.
297 tree
298 objc_create_temporary_var (tree type, const char *name)
300 tree decl;
302 if (name != NULL)
304 decl = build_decl (input_location,
305 VAR_DECL, get_identifier (name), type);
307 else
309 decl = build_decl (input_location,
310 VAR_DECL, NULL_TREE, type);
312 TREE_USED (decl) = 1;
313 DECL_ARTIFICIAL (decl) = 1;
314 DECL_IGNORED_P (decl) = 1;
315 DECL_CONTEXT (decl) = current_function_decl;
317 return decl;
320 /* Some platforms pass small structures through registers versus
321 through an invisible pointer. Determine at what size structure is
322 the transition point between the two possibilities. */
324 static void
325 generate_struct_by_value_array (void)
327 tree type;
328 tree decls;
329 int i, j;
330 int aggregate_in_mem[32];
331 int found = 0;
333 /* Presumably no platform passes 32 byte structures in a register. */
334 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
335 in registers. */
336 for (i = 1; i < 32; i++)
338 char buffer[5];
339 tree *chain = NULL;
341 /* Create an unnamed struct that has `i' character components */
342 type = objc_start_struct (NULL_TREE);
344 strcpy (buffer, "c1");
345 decls = add_field_decl (char_type_node, buffer, &chain);
347 for (j = 1; j < i; j++)
349 sprintf (buffer, "c%d", j + 1);
350 add_field_decl (char_type_node, buffer, &chain);
352 objc_finish_struct (type, decls);
354 aggregate_in_mem[i] = aggregate_value_p (type, 0);
355 if (!aggregate_in_mem[i])
356 found = 1;
359 /* We found some structures that are returned in registers instead of memory
360 so output the necessary data. */
361 if (found)
363 for (i = 31; i >= 0; i--)
364 if (!aggregate_in_mem[i])
365 break;
366 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
369 exit (0);
372 bool
373 objc_init (void)
375 bool ok;
376 #ifdef OBJCPLUS
377 if (cxx_init () == false)
378 #else
379 if (c_objc_common_init () == false)
380 #endif
381 return false;
383 /* print_struct_values is triggered by -print-runtime-info (used
384 when building libobjc, with an empty file as input). It does not
385 require any ObjC setup, and it never returns.
387 -fcompare-debug is used to check the compiler output; we are
388 executed twice, once with flag_compare_debug set, and once with
389 it not set. If the flag is used together with
390 -print-runtime-info, we want to print the runtime info only once,
391 else it would be output in duplicate. So we check
392 flag_compare_debug to output it in only one of the invocations.
394 As a side effect, this also that means -fcompare-debug
395 -print-runtime-info will run the compiler twice, and compare the
396 generated assembler file; the first time the compiler exits
397 immediately (producing no file), and the second time it compiles
398 an empty file. This checks, as a side effect, that compiling an
399 empty file produces no assembler output. */
400 if (print_struct_values && !flag_compare_debug)
401 generate_struct_by_value_array ();
403 /* Set up stuff used by FE parser and all runtimes. */
404 errbuf = XNEWVEC (char, 1024 * 10);
405 interface_hash_init ();
406 hash_init ();
407 objc_encoding_init ();
408 /* ... and then check flags and set-up for the selected runtime ... */
409 if (flag_next_runtime && flag_objc_abi >= 2)
410 ok = objc_next_runtime_abi_02_init (&runtime);
411 else if (flag_next_runtime)
412 ok = objc_next_runtime_abi_01_init (&runtime);
413 else
414 ok = objc_gnu_runtime_abi_01_init (&runtime);
416 /* If that part of the setup failed - bail out immediately. */
417 if (!ok)
418 return false;
420 /* Determine the default visibility for instance variables. */
421 switch (default_ivar_visibility)
423 case IVAR_VISIBILITY_PRIVATE:
424 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
425 break;
426 case IVAR_VISIBILITY_PUBLIC:
427 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
428 break;
429 case IVAR_VISIBILITY_PACKAGE:
430 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
431 break;
432 default:
433 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
436 /* Generate general types and push runtime-specific decls to file scope. */
437 synth_module_prologue ();
439 return true;
442 /* This is called automatically (at the very end of compilation) by
443 c_write_global_declarations and cp_write_global_declarations. */
444 void
445 objc_write_global_declarations (void)
447 mark_referenced_methods ();
449 /* A missing @end might not be detected by the parser. */
450 if (objc_implementation_context)
452 warning (0, "%<@end%> missing in implementation context");
453 finish_class (objc_implementation_context);
454 objc_ivar_chain = NULL_TREE;
455 objc_implementation_context = NULL_TREE;
458 if (warn_selector)
460 objc_map_iterator_t i;
462 objc_map_iterator_initialize (class_method_map, &i);
463 while (objc_map_iterator_move_to_next (class_method_map, &i))
464 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
466 objc_map_iterator_initialize (instance_method_map, &i);
467 while (objc_map_iterator_move_to_next (instance_method_map, &i))
468 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
471 /* TODO: consider an early exit here if either errorcount or sorrycount
472 is non-zero. Not only is it wasting time to generate the metadata,
473 it needlessly imposes need to re-check for things that are already
474 determined to be errors. */
476 /* Finalize Objective-C runtime data. No need to generate tables
477 and code if only checking syntax, or if generating a PCH file. */
478 if (!flag_syntax_only && !pch_file)
480 location_t saved_location;
482 /* If gen_declaration desired, open the output file. */
483 if (flag_gen_declaration)
485 char * const dumpname = concat (dump_base_name, ".decl", NULL);
486 gen_declaration_file = fopen (dumpname, "w");
487 if (gen_declaration_file == 0)
488 fatal_error ("can%'t open %s: %m", dumpname);
489 free (dumpname);
492 /* Set the input location to BUILTINS_LOCATION. This is good
493 for error messages, in case any is generated while producing
494 the metadata, but it also silences warnings that would be
495 produced when compiling with -Wpadded in case when padding is
496 automatically added to the built-in runtime data structure
497 declarations. We know about this padding, and it is fine; we
498 don't want users to see any warnings about it if they use
499 -Wpadded. */
500 saved_location = input_location;
501 input_location = BUILTINS_LOCATION;
503 /* Compute and emit the meta-data tables for this runtime. */
504 (*runtime.generate_metadata) ();
506 /* Restore the original location, just in case it mattered. */
507 input_location = saved_location;
509 /* ... and then close any declaration file we opened. */
510 if (gen_declaration_file)
511 fclose (gen_declaration_file);
515 /* Return the first occurrence of a method declaration corresponding
516 to sel_name in rproto_list. Search rproto_list recursively.
517 If is_class is 0, search for instance methods, otherwise for class
518 methods. */
519 static tree
520 lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
521 int is_class)
523 tree rproto, p, m;
525 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
527 p = TREE_VALUE (rproto);
528 m = NULL_TREE;
530 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
532 /* First, search the @required protocol methods. */
533 if (is_class)
534 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
535 else
536 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
538 if (m)
539 return m;
541 /* If still not found, search the @optional protocol methods. */
542 if (is_class)
543 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
544 else
545 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
547 if (m)
548 return m;
550 /* If still not found, search the attached protocols. */
551 if (PROTOCOL_LIST (p))
552 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
553 sel_name, is_class);
554 if (m)
555 return m;
557 else
559 ; /* An identifier...if we could not find a protocol. */
563 return 0;
566 static tree
567 lookup_protocol_in_reflist (tree rproto_list, tree lproto)
569 tree rproto, p;
571 /* Make sure the protocol is supported by the object on the rhs. */
572 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
574 tree fnd = 0;
575 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
577 p = TREE_VALUE (rproto);
579 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
581 if (lproto == p)
582 fnd = lproto;
584 else if (PROTOCOL_LIST (p))
585 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
588 if (fnd)
589 return fnd;
592 else
594 ; /* An identifier...if we could not find a protocol. */
597 return 0;
600 void
601 objc_start_class_interface (tree klass, tree super_class,
602 tree protos, tree attributes)
604 if (flag_objc1_only && attributes)
605 error_at (input_location, "class attributes are not available in Objective-C 1.0");
607 objc_interface_context
608 = objc_ivar_context
609 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
610 objc_ivar_visibility = objc_default_ivar_visibility;
613 void
614 objc_start_category_interface (tree klass, tree categ,
615 tree protos, tree attributes)
617 if (attributes)
619 if (flag_objc1_only)
620 error_at (input_location, "category attributes are not available in Objective-C 1.0");
621 else
622 warning_at (input_location, OPT_Wattributes,
623 "category attributes are not available in this version"
624 " of the compiler, (ignored)");
626 if (categ == NULL_TREE)
628 if (flag_objc1_only)
629 error_at (input_location, "class extensions are not available in Objective-C 1.0");
630 else
632 /* Iterate over all the classes and categories implemented
633 up to now in this compilation unit. */
634 struct imp_entry *t;
636 for (t = imp_list; t; t = t->next)
638 /* If we find a class @implementation with the same name
639 as the one we are extending, produce an error. */
640 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
641 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
642 error_at (input_location,
643 "class extension for class %qE declared after its %<@implementation%>",
644 klass);
648 objc_interface_context
649 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
650 objc_ivar_chain
651 = continue_class (objc_interface_context);
654 void
655 objc_start_protocol (tree name, tree protos, tree attributes)
657 if (flag_objc1_only && attributes)
658 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
660 objc_interface_context
661 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
662 objc_method_optional_flag = false;
665 void
666 objc_continue_interface (void)
668 objc_ivar_chain
669 = continue_class (objc_interface_context);
672 void
673 objc_finish_interface (void)
675 finish_class (objc_interface_context);
676 objc_interface_context = NULL_TREE;
677 objc_method_optional_flag = false;
678 objc_in_class_extension = false;
681 void
682 objc_start_class_implementation (tree klass, tree super_class)
684 objc_implementation_context
685 = objc_ivar_context
686 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
687 NULL_TREE);
688 objc_ivar_visibility = objc_default_ivar_visibility;
691 void
692 objc_start_category_implementation (tree klass, tree categ)
694 objc_implementation_context
695 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
696 NULL_TREE);
697 objc_ivar_chain
698 = continue_class (objc_implementation_context);
701 void
702 objc_continue_implementation (void)
704 objc_ivar_chain
705 = continue_class (objc_implementation_context);
708 void
709 objc_finish_implementation (void)
711 #ifdef OBJCPLUS
712 if (flag_objc_call_cxx_cdtors)
713 objc_generate_cxx_cdtors ();
714 #endif
716 if (objc_implementation_context)
718 finish_class (objc_implementation_context);
719 objc_ivar_chain = NULL_TREE;
720 objc_implementation_context = NULL_TREE;
722 else
723 warning (0, "%<@end%> must appear in an @implementation context");
726 void
727 objc_set_visibility (objc_ivar_visibility_kind visibility)
729 if (visibility == OBJC_IVAR_VIS_PACKAGE)
731 if (flag_objc1_only)
732 error ("%<@package%> is not available in Objective-C 1.0");
733 else
734 warning (0, "%<@package%> presently has the same effect as %<@public%>");
736 objc_ivar_visibility = visibility;
739 void
740 objc_set_method_opt (bool optional)
742 if (flag_objc1_only)
744 if (optional)
745 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
746 else
747 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
750 objc_method_optional_flag = optional;
751 if (!objc_interface_context
752 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
754 if (optional)
755 error ("%<@optional%> is allowed in @protocol context only");
756 else
757 error ("%<@required%> is allowed in @protocol context only");
758 objc_method_optional_flag = false;
762 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
763 PROTOCOL. */
764 static tree
765 lookup_property_in_list (tree chain, tree property)
767 tree x;
768 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
769 if (PROPERTY_NAME (x) == property)
770 return x;
771 return NULL_TREE;
774 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
775 static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
777 tree rproto, x;
778 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
780 tree p = TREE_VALUE (rproto);
781 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
783 if ((x = lookup_property_in_list (p, property)))
784 return x;
785 if (PROTOCOL_LIST (p))
786 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
788 else
790 ; /* An identifier...if we could not find a protocol. */
793 return NULL_TREE;
796 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
797 chain of interface hierarchy. */
798 static tree
799 lookup_property (tree interface_type, tree property)
801 tree inter = interface_type;
802 while (inter)
804 tree x, category;
805 if ((x = lookup_property_in_list (inter, property)))
806 return x;
807 /* Failing that, look for the property in each category of the class. */
808 category = inter;
809 while ((category = CLASS_CATEGORY_LIST (category)))
811 if ((x = lookup_property_in_list (category, property)))
812 return x;
814 /* When checking a category, also check the protocols
815 attached with the category itself. */
816 if (CLASS_PROTOCOL_LIST (category)
817 && (x = lookup_property_in_protocol_list
818 (CLASS_PROTOCOL_LIST (category), property)))
819 return x;
822 /* Failing to find in categories, look for property in protocol list. */
823 if (CLASS_PROTOCOL_LIST (inter)
824 && (x = lookup_property_in_protocol_list
825 (CLASS_PROTOCOL_LIST (inter), property)))
826 return x;
828 /* Failing that, climb up the inheritance hierarchy. */
829 inter = lookup_interface (CLASS_SUPER_NAME (inter));
831 return inter;
834 /* This routine is called by the parser when a
835 @property... declaration is found. 'decl' is the declaration of
836 the property (type/identifier), and the other arguments represent
837 property attributes that may have been specified in the Objective-C
838 declaration. 'parsed_property_readonly' is 'true' if the attribute
839 'readonly' was specified, and 'false' if not; similarly for the
840 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
841 if the attribute 'getter' was not specified, and is the identifier
842 corresponding to the specified getter if it was; similarly for
843 'parsed_property_setter_ident'. */
844 void
845 objc_add_property_declaration (location_t location, tree decl,
846 bool parsed_property_readonly, bool parsed_property_readwrite,
847 bool parsed_property_assign, bool parsed_property_retain,
848 bool parsed_property_copy, bool parsed_property_nonatomic,
849 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
851 tree property_decl;
852 tree x;
853 /* 'property_readonly' and 'property_assign_semantics' are the final
854 attributes of the property after all parsed attributes have been
855 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
856 parsed_property_readonly = false and parsed_property_readwrite =
857 false, then property_readonly will be false because the default
858 is readwrite). */
859 bool property_readonly = false;
860 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
861 bool property_extension_in_class_extension = false;
863 if (flag_objc1_only)
864 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
866 if (parsed_property_readonly && parsed_property_readwrite)
868 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
869 /* In case of conflicting attributes (here and below), after
870 producing an error, we pick one of the attributes and keep
871 going. */
872 property_readonly = false;
874 else
876 if (parsed_property_readonly)
877 property_readonly = true;
879 if (parsed_property_readwrite)
880 property_readonly = false;
883 if (parsed_property_readonly && parsed_property_setter_ident)
885 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
886 property_readonly = false;
889 if (parsed_property_assign && parsed_property_retain)
891 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
892 property_assign_semantics = OBJC_PROPERTY_RETAIN;
894 else if (parsed_property_assign && parsed_property_copy)
896 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
897 property_assign_semantics = OBJC_PROPERTY_COPY;
899 else if (parsed_property_retain && parsed_property_copy)
901 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
902 property_assign_semantics = OBJC_PROPERTY_COPY;
904 else
906 if (parsed_property_assign)
907 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
909 if (parsed_property_retain)
910 property_assign_semantics = OBJC_PROPERTY_RETAIN;
912 if (parsed_property_copy)
913 property_assign_semantics = OBJC_PROPERTY_COPY;
916 if (!objc_interface_context)
918 error_at (location, "property declaration not in @interface or @protocol context");
919 return;
922 /* At this point we know that we are either in an interface, a
923 category, or a protocol. */
925 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
926 something else, as that would confuse the checks below. */
927 if (TREE_CODE (decl) != FIELD_DECL)
929 error_at (location, "invalid property declaration");
930 return;
933 /* Do some spot-checks for the most obvious invalid types. */
935 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
937 error_at (location, "property can not be an array");
938 return;
941 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
942 parsing, while the C/ObjC parser accepts it and gives us a
943 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
944 to check for a bitfield when doing ObjC. */
945 #ifndef OBJCPLUS
946 if (DECL_INITIAL (decl))
948 /* A @property is not an actual variable, but it is a way to
949 describe a pair of accessor methods, so its type (which is
950 the type of the return value of the getter and the first
951 argument of the setter) can't be a bitfield (as return values
952 and arguments of functions can not be bitfields). The
953 underlying instance variable could be a bitfield, but that is
954 a different matter. */
955 error_at (location, "property can not be a bit-field");
956 return;
958 #endif
960 /* TODO: Check that the property type is an Objective-C object or a
961 "POD". */
963 /* Implement -Wproperty-assign-default (which is enabled by default). */
964 if (warn_property_assign_default
965 /* If garbage collection is not being used, then 'assign' is
966 valid for objects (and typically used for delegates) but it
967 is wrong in most cases (since most objects need to be
968 retained or copied in setters). Warn users when 'assign' is
969 used implicitly. */
970 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
971 /* Read-only properties are never assigned, so the assignment
972 semantics do not matter in that case. */
973 && !property_readonly
974 && !flag_objc_gc)
976 /* Please note that it would make sense to default to 'assign'
977 for non-{Objective-C objects}, and to 'retain' for
978 Objective-C objects. But that would break compatibility with
979 other compilers. */
980 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
982 /* Use 'false' so we do not warn for Class objects. */
983 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
985 warning_at (location,
987 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
988 decl);
989 inform (location,
990 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
995 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
996 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
997 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
999 if (property_assign_semantics == OBJC_PROPERTY_COPY
1000 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
1001 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
1003 /* Now determine the final property getter and setter names. They
1004 will be stored in the PROPERTY_DECL, from which they'll always be
1005 extracted and used. */
1007 /* Adjust, or fill in, setter and getter names. We overwrite the
1008 parsed_property_setter_ident and parsed_property_getter_ident
1009 with the final setter and getter identifiers that will be
1010 used. */
1011 if (parsed_property_setter_ident)
1013 /* The setter should be terminated by ':', but the parser only
1014 gives us an identifier without ':'. So, we need to add ':'
1015 at the end. */
1016 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1017 size_t length = strlen (parsed_setter);
1018 char *final_setter = (char *)alloca (length + 2);
1020 sprintf (final_setter, "%s:", parsed_setter);
1021 parsed_property_setter_ident = get_identifier (final_setter);
1023 else
1025 if (!property_readonly)
1026 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
1027 (DECL_NAME (decl)));
1030 if (!parsed_property_getter_ident)
1031 parsed_property_getter_ident = DECL_NAME (decl);
1033 /* Check for duplicate property declarations. We first check the
1034 immediate context for a property with the same name. Any such
1035 declarations are an error, unless this is a class extension and
1036 we are extending a property from readonly to readwrite. */
1037 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1039 if (PROPERTY_NAME (x) == DECL_NAME (decl))
1041 if (objc_in_class_extension
1042 && property_readonly == 0
1043 && PROPERTY_READONLY (x) == 1)
1045 /* This is a class extension, and we are extending an
1046 existing readonly property to a readwrite one.
1047 That's fine. :-) */
1048 property_extension_in_class_extension = true;
1049 break;
1051 else
1053 location_t original_location = DECL_SOURCE_LOCATION (x);
1055 error_at (location, "redeclaration of property %qD", decl);
1057 if (original_location != UNKNOWN_LOCATION)
1058 inform (original_location, "originally specified here");
1059 return;
1064 /* If x is not NULL_TREE, we must be in a class extension and we're
1065 extending a readonly property. In that case, no point in
1066 searching for another declaration. */
1067 if (x == NULL_TREE)
1069 /* We now need to check for existing property declarations (in
1070 the superclass, other categories or protocols) and check that
1071 the new declaration is not in conflict with existing
1072 ones. */
1074 /* Search for a previous, existing declaration of a property
1075 with the same name in superclasses, protocols etc. If one is
1076 found, it will be in the 'x' variable. */
1078 /* Note that, for simplicity, the following may search again the
1079 local context. That's Ok as nothing will be found (else we'd
1080 have thrown an error above); it's only a little inefficient,
1081 but the code is simpler. */
1082 switch (TREE_CODE (objc_interface_context))
1084 case CLASS_INTERFACE_TYPE:
1085 /* Look up the property in the current @interface (which
1086 will find nothing), then its protocols and categories and
1087 superclasses. */
1088 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1089 break;
1090 case CATEGORY_INTERFACE_TYPE:
1091 /* Look up the property in the main @interface, then
1092 protocols and categories (one of them is ours, and will
1093 find nothing) and superclasses. */
1094 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1095 DECL_NAME (decl));
1096 break;
1097 case PROTOCOL_INTERFACE_TYPE:
1098 /* Looks up the property in any protocols attached to the
1099 current protocol. */
1100 if (PROTOCOL_LIST (objc_interface_context))
1102 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1103 DECL_NAME (decl));
1105 break;
1106 default:
1107 gcc_unreachable ();
1111 if (x != NULL_TREE)
1113 /* An existing property was found; check that it has the same
1114 types, or it is compatible. */
1115 location_t original_location = DECL_SOURCE_LOCATION (x);
1117 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1119 warning_at (location, 0,
1120 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
1122 if (original_location != UNKNOWN_LOCATION)
1123 inform (original_location, "originally specified here");
1124 return;
1127 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1129 warning_at (location, 0,
1130 "'getter' attribute of property %qD conflicts with previous declaration", decl);
1132 if (original_location != UNKNOWN_LOCATION)
1133 inform (original_location, "originally specified here");
1134 return;
1137 /* We can only compare the setter names if both the old and new property have a setter. */
1138 if (!property_readonly && !PROPERTY_READONLY(x))
1140 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1142 warning_at (location, 0,
1143 "'setter' attribute of property %qD conflicts with previous declaration", decl);
1145 if (original_location != UNKNOWN_LOCATION)
1146 inform (original_location, "originally specified here");
1147 return;
1151 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1153 warning_at (location, 0,
1154 "assign semantics attributes of property %qD conflict with previous declaration", decl);
1156 if (original_location != UNKNOWN_LOCATION)
1157 inform (original_location, "originally specified here");
1158 return;
1161 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1162 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1164 warning_at (location, 0,
1165 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
1167 if (original_location != UNKNOWN_LOCATION)
1168 inform (original_location, "originally specified here");
1169 return;
1172 /* We now check that the new and old property declarations have
1173 the same types (or compatible one). In the Objective-C
1174 tradition of loose type checking, we do type-checking but
1175 only generate warnings (not errors) if they do not match.
1176 For non-readonly properties, the types must match exactly;
1177 for readonly properties, it is allowed to use a "more
1178 specialized" type in the new property declaration. Eg, the
1179 superclass has a getter returning (NSArray *) and the
1180 subclass a getter returning (NSMutableArray *). The object's
1181 getter returns an (NSMutableArray *); but if you cast the
1182 object to the superclass, which is allowed, you'd still
1183 expect the getter to return an (NSArray *), which works since
1184 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1185 objects belonging to the type of the new @property should be
1186 a subset of the set of objects belonging to the type of the
1187 old @property. This is what "specialization" means. And the
1188 reason it only applies to readonly properties is that for a
1189 readwrite property the setter would have the opposite
1190 requirement - ie that the superclass type is more specialized
1191 then the subclass one; hence the only way to satisfy both
1192 constraints is that the types match. */
1194 /* If the types are not the same in the C sense, we warn ... */
1195 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1196 /* ... unless the property is readonly, in which case we
1197 allow a new, more specialized, declaration. */
1198 && (!property_readonly
1199 || !objc_compare_types (TREE_TYPE (x),
1200 TREE_TYPE (decl), -5, NULL_TREE)))
1202 warning_at (location, 0,
1203 "type of property %qD conflicts with previous declaration", decl);
1204 if (original_location != UNKNOWN_LOCATION)
1205 inform (original_location, "originally specified here");
1206 return;
1209 /* If we are in a class extension and we're extending a readonly
1210 property in the main @interface, we'll just update the
1211 existing property with the readwrite flag and potentially the
1212 new setter name. */
1213 if (property_extension_in_class_extension)
1215 PROPERTY_READONLY (x) = 0;
1216 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1217 return;
1221 /* Create a PROPERTY_DECL node. */
1222 property_decl = make_node (PROPERTY_DECL);
1224 /* Copy the basic information from the original decl. */
1225 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1226 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1227 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
1229 /* Add property-specific information. */
1230 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1231 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1232 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1233 PROPERTY_READONLY (property_decl) = property_readonly;
1234 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1235 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1236 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1237 PROPERTY_DYNAMIC (property_decl) = 0;
1239 /* Remember the fact that the property was found in the @optional
1240 section in a @protocol, or not. */
1241 if (objc_method_optional_flag)
1242 PROPERTY_OPTIONAL (property_decl) = 1;
1243 else
1244 PROPERTY_OPTIONAL (property_decl) = 0;
1246 /* Note that PROPERTY_GETTER_NAME is always set for all
1247 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1248 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1249 with a getter or setter, we should get the PROPERTY_DECL and use
1250 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1251 names. */
1253 /* Add the PROPERTY_DECL to the list of properties for the class. */
1254 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1255 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
1258 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1259 list of methods in the interface (and, failing that, the local list
1260 in the implementation, and failing that, the protocol list)
1261 provided for a 'setter' or 'getter' for 'component' with default
1262 names (ie, if 'component' is "name", then search for "name" and
1263 "setName:"). It is also possible to specify a different
1264 'getter_name' (this is used for @optional readonly properties). If
1265 any is found, then create an artificial property that uses them.
1266 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1267 static tree
1268 maybe_make_artificial_property_decl (tree interface, tree implementation,
1269 tree protocol_list, tree component, bool is_class,
1270 tree getter_name)
1272 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1273 tree getter = NULL_TREE;
1274 tree setter = NULL_TREE;
1276 if (getter_name == NULL_TREE)
1277 getter_name = component;
1279 /* First, check the @interface and all superclasses. */
1280 if (interface)
1282 int flags = 0;
1284 /* Using instance methods of the root class as accessors is most
1285 likely unwanted and can be extremely confusing (and, most
1286 importantly, other Objective-C 2.0 compilers do not do it).
1287 Turn it off. */
1288 if (is_class)
1289 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
1291 getter = lookup_method_static (interface, getter_name, flags);
1292 setter = lookup_method_static (interface, setter_name, flags);
1295 /* Second, check the local @implementation context. */
1296 if (!getter && !setter)
1298 if (implementation)
1300 if (is_class)
1302 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1303 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1305 else
1307 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
1308 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
1313 /* Try the protocol_list if we didn't find anything in the
1314 @interface and in the @implementation. */
1315 if (!getter && !setter)
1317 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1318 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1321 /* There needs to be at least a getter or setter for this to be a
1322 valid 'object.component' syntax. */
1323 if (getter || setter)
1325 /* Yes ... determine the type of the expression. */
1326 tree property_decl;
1327 tree type;
1329 if (getter)
1330 type = TREE_VALUE (TREE_TYPE (getter));
1331 else
1332 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
1334 /* Create an artificial property declaration with the
1335 information we collected on the type and getter/setter
1336 names. */
1337 property_decl = make_node (PROPERTY_DECL);
1339 TREE_TYPE (property_decl) = type;
1340 DECL_SOURCE_LOCATION (property_decl) = input_location;
1341 TREE_DEPRECATED (property_decl) = 0;
1342 DECL_ARTIFICIAL (property_decl) = 1;
1344 /* Add property-specific information. Note that one of
1345 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1346 non-existing method; this will generate an error when the
1347 expression is later compiled. At this stage we don't know if
1348 the getter or setter will be used, so we can't generate an
1349 error. */
1350 PROPERTY_NAME (property_decl) = component;
1351 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1352 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1353 PROPERTY_READONLY (property_decl) = 0;
1354 PROPERTY_NONATOMIC (property_decl) = 0;
1355 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1356 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1357 PROPERTY_DYNAMIC (property_decl) = 0;
1358 PROPERTY_OPTIONAL (property_decl) = 0;
1360 if (!getter)
1361 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1363 /* The following is currently unused, but it's nice to have
1364 there. We may use it if we need in the future. */
1365 if (!setter)
1366 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1368 return property_decl;
1371 return NULL_TREE;
1374 /* This hook routine is invoked by the parser when an expression such
1375 as 'xxx.yyy' is parsed. We get a chance to process these
1376 expressions in a way that is specified to Objective-C (to implement
1377 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1378 If the expression is not an Objective-C specified expression, we
1379 should return NULL_TREE; else we return the expression.
1381 At the moment this only implements dot-syntax and properties (not
1382 non-fragile ivars yet), ie 'object.property' or 'object.component'
1383 where 'component' is not a declared property, but a valid getter or
1384 setter for it could be found. */
1385 tree
1386 objc_maybe_build_component_ref (tree object, tree property_ident)
1388 tree x = NULL_TREE;
1389 tree rtype;
1391 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1392 not available. */
1393 if (flag_objc1_only)
1394 return NULL_TREE;
1396 /* Try to determine if 'object' is an Objective-C object or not. If
1397 not, return. */
1398 if (object == NULL_TREE || object == error_mark_node
1399 || (rtype = TREE_TYPE (object)) == NULL_TREE)
1400 return NULL_TREE;
1402 if (property_ident == NULL_TREE || property_ident == error_mark_node
1403 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1404 return NULL_TREE;
1406 /* The following analysis of 'object' is similar to the one used for
1407 the 'receiver' of a method invocation. We need to determine what
1408 'object' is and find the appropriate property (either declared,
1409 or artificial) for it (in the same way as we need to find the
1410 appropriate method prototype for a method invocation). There are
1411 some simplifications here though: "object.property" is invalid if
1412 "object" has a type of "id" or "Class"; it must at least have a
1413 protocol attached to it, and "object" is never a class name as
1414 that is done by objc_build_class_component_ref. Finally, we
1415 don't know if this really is a dot-syntax expression, so we want
1416 to make a quick exit if it is not; for this reason, we try to
1417 postpone checks after determining that 'object' looks like an
1418 Objective-C object. */
1420 if (objc_is_id (rtype))
1422 /* This is the case that the 'object' is of type 'id' or
1423 'Class'. */
1425 /* Check if at least it is of type 'id <Protocol>' or 'Class
1426 <Protocol>'; if so, look the property up in the
1427 protocols. */
1428 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
1430 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
1432 if (rprotos)
1434 /* No point looking up declared @properties if we are
1435 dealing with a class. Classes have no declared
1436 properties. */
1437 if (!IS_CLASS (rtype))
1438 x = lookup_property_in_protocol_list (rprotos, property_ident);
1440 if (x == NULL_TREE)
1442 /* Ok, no property. Maybe it was an
1443 object.component dot-syntax without a declared
1444 property (this is valid for classes too). Look
1445 for getter/setter methods and internally declare
1446 an artificial property based on them if found. */
1447 x = maybe_make_artificial_property_decl (NULL_TREE,
1448 NULL_TREE,
1449 rprotos,
1450 property_ident,
1451 IS_CLASS (rtype),
1452 NULL_TREE);
1454 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1456 /* This is a special, complicated case. If the
1457 property is optional, and is read-only, then the
1458 property is always used for reading, but an
1459 eventual existing non-property setter can be used
1460 for writing. We create an artificial property
1461 decl copying the getter from the optional
1462 property, and looking up the setter in the
1463 interface. */
1464 x = maybe_make_artificial_property_decl (NULL_TREE,
1465 NULL_TREE,
1466 rprotos,
1467 property_ident,
1468 false,
1469 PROPERTY_GETTER_NAME (x));
1473 else if (objc_method_context)
1475 /* Else, if we are inside a method it could be the case of
1476 'super' or 'self'. */
1477 tree interface_type = NULL_TREE;
1478 tree t = object;
1479 while (TREE_CODE (t) == COMPOUND_EXPR
1480 || TREE_CODE (t) == MODIFY_EXPR
1481 || CONVERT_EXPR_P (t)
1482 || TREE_CODE (t) == COMPONENT_REF)
1483 t = TREE_OPERAND (t, 0);
1485 if (t == UOBJC_SUPER_decl)
1486 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
1487 else if (t == self_decl)
1488 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1490 if (interface_type)
1492 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
1493 x = lookup_property (interface_type, property_ident);
1495 if (x == NULL_TREE)
1497 /* Try the dot-syntax without a declared property.
1498 If this is an access to 'self', it is possible
1499 that they may refer to a setter/getter that is
1500 not declared in the interface, but exists locally
1501 in the implementation. In that case, get the
1502 implementation context and use it. */
1503 tree implementation = NULL_TREE;
1505 if (t == self_decl)
1506 implementation = objc_implementation_context;
1508 x = maybe_make_artificial_property_decl
1509 (interface_type, implementation, NULL_TREE,
1510 property_ident,
1511 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1512 NULL_TREE);
1514 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1516 tree implementation = NULL_TREE;
1518 if (t == self_decl)
1519 implementation = objc_implementation_context;
1521 x = maybe_make_artificial_property_decl (interface_type,
1522 implementation,
1523 NULL_TREE,
1524 property_ident,
1525 false,
1526 PROPERTY_GETTER_NAME (x));
1531 else
1533 /* This is the case where we have more information on 'rtype'. */
1534 tree basetype = TYPE_MAIN_VARIANT (rtype);
1536 /* Skip the pointer - if none, it's not an Objective-C object or
1537 class. */
1538 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1539 basetype = TREE_TYPE (basetype);
1540 else
1541 return NULL_TREE;
1543 /* Traverse typedefs. */
1544 while (basetype != NULL_TREE
1545 && TREE_CODE (basetype) == RECORD_TYPE
1546 && OBJC_TYPE_NAME (basetype)
1547 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1548 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1549 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1551 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1553 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
1554 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
1556 if (interface_type
1557 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1558 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1559 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
1561 /* Not sure 'rtype' could ever be a class here! Just
1562 for safety we keep the checks. */
1563 if (!IS_CLASS (rtype))
1565 x = lookup_property (interface_type, property_ident);
1567 if (x == NULL_TREE)
1568 x = lookup_property_in_protocol_list (protocol_list,
1569 property_ident);
1572 if (x == NULL_TREE)
1574 /* Try the dot-syntax without a declared property.
1575 If we are inside a method implementation, it is
1576 possible that they may refer to a setter/getter
1577 that is not declared in the interface, but exists
1578 locally in the implementation. In that case, get
1579 the implementation context and use it. */
1580 tree implementation = NULL_TREE;
1582 if (objc_implementation_context
1583 && CLASS_NAME (objc_implementation_context)
1584 == OBJC_TYPE_NAME (interface_type))
1585 implementation = objc_implementation_context;
1587 x = maybe_make_artificial_property_decl (interface_type,
1588 implementation,
1589 protocol_list,
1590 property_ident,
1591 IS_CLASS (rtype),
1592 NULL_TREE);
1594 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1596 tree implementation = NULL_TREE;
1598 if (objc_implementation_context
1599 && CLASS_NAME (objc_implementation_context)
1600 == OBJC_TYPE_NAME (interface_type))
1601 implementation = objc_implementation_context;
1603 x = maybe_make_artificial_property_decl (interface_type,
1604 implementation,
1605 protocol_list,
1606 property_ident,
1607 false,
1608 PROPERTY_GETTER_NAME (x));
1614 if (x)
1616 tree expression;
1617 tree getter_call;
1618 tree deprecated_method_prototype = NULL_TREE;
1620 /* We have an additional nasty problem here; if this
1621 PROPERTY_REF needs to become a 'getter', then the conversion
1622 from PROPERTY_REF into a getter call happens in gimplify,
1623 after the selector table has already been generated and when
1624 it is too late to add another selector to it. To work around
1625 the problem, we always create the getter call at this stage,
1626 which puts the selector in the table. Note that if the
1627 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1628 we have added a selector too many to the selector table.
1629 This is a little inefficient.
1631 Also note that method calls to 'self' and 'super' require the
1632 context (self_decl, UOBJS_SUPER_decl,
1633 objc_implementation_context etc) to be built correctly; this
1634 is yet another reason why building the call at the gimplify
1635 stage (when this context has been lost) is not very
1636 practical. If we build it at this stage, we know it will
1637 always be built correctly.
1639 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1640 property decl created to deal with a dotsyntax not really
1641 referring to an existing property) then do not try to build a
1642 call to the getter as there is no getter. */
1643 if (PROPERTY_HAS_NO_GETTER (x))
1644 getter_call = NULL_TREE;
1645 else
1646 getter_call = objc_finish_message_expr
1647 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1648 /* Disable the immediate deprecation warning if the getter
1649 is deprecated, but record the fact that the getter is
1650 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1651 the method prototype. */
1652 &deprecated_method_prototype);
1654 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1655 deprecated_method_prototype);
1656 SET_EXPR_LOCATION (expression, input_location);
1657 TREE_SIDE_EFFECTS (expression) = 1;
1659 return expression;
1662 return NULL_TREE;
1665 /* This hook routine is invoked by the parser when an expression such
1666 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1667 Objective-C 2.0 dot-syntax applied to classes, so we need to
1668 convert it into a setter/getter call on the class. */
1669 tree
1670 objc_build_class_component_ref (tree class_name, tree property_ident)
1672 tree x = NULL_TREE;
1673 tree object, rtype;
1675 if (flag_objc1_only)
1676 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
1678 if (class_name == NULL_TREE || class_name == error_mark_node
1679 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1680 return error_mark_node;
1682 if (property_ident == NULL_TREE || property_ident == error_mark_node
1683 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1684 return NULL_TREE;
1686 object = objc_get_class_reference (class_name);
1687 if (!object)
1689 /* We know that 'class_name' is an Objective-C class name as the
1690 parser won't call this function if it is not. This is only a
1691 double-check for safety. */
1692 error_at (input_location, "could not find class %qE", class_name);
1693 return error_mark_node;
1696 rtype = lookup_interface (class_name);
1697 if (!rtype)
1699 /* Again, this should never happen, but we do check. */
1700 error_at (input_location, "could not find interface for class %qE", class_name);
1701 return error_mark_node;
1703 else
1705 if (TREE_DEPRECATED (rtype))
1706 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
1709 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
1710 property_ident,
1711 true, NULL_TREE);
1713 if (x)
1715 tree expression;
1716 tree getter_call;
1717 tree deprecated_method_prototype = NULL_TREE;
1719 if (PROPERTY_HAS_NO_GETTER (x))
1720 getter_call = NULL_TREE;
1721 else
1722 getter_call = objc_finish_message_expr
1723 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1724 &deprecated_method_prototype);
1726 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1727 deprecated_method_prototype);
1728 SET_EXPR_LOCATION (expression, input_location);
1729 TREE_SIDE_EFFECTS (expression) = 1;
1731 return expression;
1733 else
1735 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1736 property_ident, class_name);
1737 return error_mark_node;
1740 return NULL_TREE;
1745 /* This is used because we don't want to expose PROPERTY_REF to the
1746 C/C++ frontends. Maybe we should! */
1747 bool
1748 objc_is_property_ref (tree node)
1750 if (node && TREE_CODE (node) == PROPERTY_REF)
1751 return true;
1752 else
1753 return false;
1756 /* This function builds a setter call for a PROPERTY_REF (real, for a
1757 declared property, or artificial, for a dot-syntax accessor which
1758 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1759 (the caller must check this beforehand). 'rhs' is the value to
1760 assign to the property. A plain setter call is returned, or
1761 error_mark_node if the property is readonly. */
1763 static tree
1764 objc_build_setter_call (tree lhs, tree rhs)
1766 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1767 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
1769 if (PROPERTY_READONLY (property_decl))
1771 error ("readonly property can not be set");
1772 return error_mark_node;
1774 else
1776 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1777 tree setter;
1779 /* TODO: Check that the setter return type is 'void'. */
1781 /* TODO: Decay arguments in C. */
1782 setter = objc_finish_message_expr (object_expr,
1783 PROPERTY_SETTER_NAME (property_decl),
1784 setter_argument, NULL);
1785 return setter;
1788 /* Unreachable, but the compiler may not realize. */
1789 return error_mark_node;
1792 /* This hook routine is called when a MODIFY_EXPR is being built. We
1793 check what is being modified; if it is a PROPERTY_REF, we need to
1794 generate a 'setter' function call for the property. If this is not
1795 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1796 on creating their MODIFY_EXPR.
1798 This is used for example if you write
1800 object.count = 1;
1802 where 'count' is a property. The left-hand side creates a
1803 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1804 to assign something to it. We intercept that here, and generate a
1805 call to the 'setter' method instead. */
1806 tree
1807 objc_maybe_build_modify_expr (tree lhs, tree rhs)
1809 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
1811 /* Building a simple call to the setter method would work for cases such as
1813 object.count = 1;
1815 but wouldn't work for cases such as
1817 count = object2.count = 1;
1819 to get these to work with very little effort, we build a
1820 compound statement which does the setter call (to set the
1821 property to 'rhs'), but which can also be evaluated returning
1822 the 'rhs'. If the 'rhs' has no side effects, we can simply
1823 evaluate it twice, building
1825 ([object setProperty: rhs]; rhs)
1827 If it has side effects, we put it in a temporary variable first,
1828 so we create the following:
1830 (temp = rhs; [object setProperty: temp]; temp)
1832 setter_argument is rhs in the first case, and temp in the second
1833 case.
1835 tree setter_argument;
1837 /* s1, s2 and s3 are the tree statements that we need in the
1838 compound expression. */
1839 tree s1, s2, s3, compound_expr;
1841 if (TREE_SIDE_EFFECTS (rhs))
1843 tree bind;
1845 /* Declare __objc_property_temp in a local bind. */
1846 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1847 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1848 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1849 SET_EXPR_LOCATION (bind, input_location);
1850 TREE_SIDE_EFFECTS (bind) = 1;
1851 add_stmt (bind);
1853 /* s1: x = rhs */
1854 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1855 NOP_EXPR,
1856 input_location, rhs, NULL_TREE);
1857 SET_EXPR_LOCATION (s1, input_location);
1859 else
1861 /* No s1. */
1862 setter_argument = rhs;
1863 s1 = NULL_TREE;
1866 /* Now build the compound statement. */
1868 /* s2: [object setProperty: x] */
1869 s2 = objc_build_setter_call (lhs, setter_argument);
1871 /* This happens if building the setter failed because the
1872 property is readonly. */
1873 if (s2 == error_mark_node)
1874 return error_mark_node;
1876 SET_EXPR_LOCATION (s2, input_location);
1878 /* s3: x */
1879 s3 = convert (TREE_TYPE (lhs), setter_argument);
1881 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1882 appropriate. */
1883 if (s1)
1884 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1885 else
1886 compound_expr = build_compound_expr (input_location, s2, s3);
1888 /* Without this, with -Wall you get a 'valued computed is not
1889 used' every time there is a "object.property = x" where the
1890 value of the resulting MODIFY_EXPR is not used. That is
1891 correct (maybe a more sophisticated implementation could
1892 avoid generating the compound expression if not needed), but
1893 we need to turn it off. */
1894 TREE_NO_WARNING (compound_expr) = 1;
1895 return compound_expr;
1897 else
1898 return NULL_TREE;
1901 /* This hook is called by the frontend when one of the four unary
1902 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1903 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1904 argument which is a PROPERTY_REF. For example, this happens if you have
1906 object.count++;
1908 where 'count' is a property. We need to use the 'getter' and
1909 'setter' for the property in an appropriate way to build the
1910 appropriate expression. 'code' is the code for the expression (one
1911 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1912 'increment' is how much we need to add or subtract. */
1913 tree
1914 objc_build_incr_expr_for_property_ref (location_t location,
1915 enum tree_code code,
1916 tree argument, tree increment)
1918 /* Here are the expressions that we want to build:
1920 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1921 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1923 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1924 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1926 tree temp_variable_decl, bind;
1927 /* s1, s2 and s3 are the tree statements that we need in the
1928 compound expression. */
1929 tree s1, s2, s3, compound_expr;
1931 /* Safety check. */
1932 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1933 return error_mark_node;
1935 /* Declare __objc_property_temp in a local bind. */
1936 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1937 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1938 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1939 SET_EXPR_LOCATION (bind, location);
1940 TREE_SIDE_EFFECTS (bind) = 1;
1941 add_stmt (bind);
1943 /* Now build the compound statement. */
1945 /* Note that the 'getter' is generated at gimplify time; at this
1946 time, we can simply put the property_ref (ie, argument) wherever
1947 we want the getter ultimately to be. */
1949 /* s1: __objc_property_temp = [object property] <+/- increment> */
1950 switch (code)
1952 case PREINCREMENT_EXPR:
1953 /* __objc_property_temp = [object property] + increment */
1954 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1955 NOP_EXPR,
1956 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
1957 argument, increment), NULL_TREE);
1958 break;
1959 case PREDECREMENT_EXPR:
1960 /* __objc_property_temp = [object property] - increment */
1961 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1962 NOP_EXPR,
1963 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
1964 argument, increment), NULL_TREE);
1965 break;
1966 case POSTINCREMENT_EXPR:
1967 case POSTDECREMENT_EXPR:
1968 /* __objc_property_temp = [object property] */
1969 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1970 NOP_EXPR,
1971 location, argument, NULL_TREE);
1972 break;
1973 default:
1974 gcc_unreachable ();
1977 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1978 switch (code)
1980 case PREINCREMENT_EXPR:
1981 case PREDECREMENT_EXPR:
1982 /* [object setProperty: __objc_property_temp] */
1983 s2 = objc_build_setter_call (argument, temp_variable_decl);
1984 break;
1985 case POSTINCREMENT_EXPR:
1986 /* [object setProperty: __objc_property_temp + increment] */
1987 s2 = objc_build_setter_call (argument,
1988 build2 (PLUS_EXPR, TREE_TYPE (argument),
1989 temp_variable_decl, increment));
1990 break;
1991 case POSTDECREMENT_EXPR:
1992 /* [object setProperty: __objc_property_temp - increment] */
1993 s2 = objc_build_setter_call (argument,
1994 build2 (MINUS_EXPR, TREE_TYPE (argument),
1995 temp_variable_decl, increment));
1996 break;
1997 default:
1998 gcc_unreachable ();
2001 /* This happens if building the setter failed because the property
2002 is readonly. */
2003 if (s2 == error_mark_node)
2004 return error_mark_node;
2006 SET_EXPR_LOCATION (s2, location);
2008 /* s3: __objc_property_temp */
2009 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
2011 /* Now build the compound statement (s1, s2, s3) */
2012 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
2014 /* Prevent C++ from warning with -Wall that "right operand of comma
2015 operator has no effect". */
2016 TREE_NO_WARNING (compound_expr) = 1;
2017 return compound_expr;
2020 tree
2021 objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
2022 tree optparms, bool ellipsis)
2024 if (is_class_method)
2025 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2026 optparms, ellipsis);
2027 else
2028 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2029 optparms, ellipsis);
2032 void
2033 objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
2035 if (!objc_interface_context)
2037 /* PS: At the moment, due to how the parser works, it should be
2038 impossible to get here. But it's good to have the check in
2039 case the parser changes.
2041 fatal_error ("method declaration not in @interface context");
2044 if (flag_objc1_only && attributes)
2045 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2047 objc_decl_method_attributes (&decl, attributes, 0);
2048 objc_add_method (objc_interface_context,
2049 decl,
2050 is_class_method,
2051 objc_method_optional_flag);
2054 /* Return 'true' if the method definition could be started, and
2055 'false' if not (because we are outside an @implementation context).
2056 EXPR is NULL or an expression that needs to be evaluated for the
2057 side effects of array size expressions in the parameters.
2059 bool
2060 objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2061 tree expr)
2063 if (!objc_implementation_context)
2065 error ("method definition not in @implementation context");
2066 return false;
2069 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2070 return false;
2072 #ifndef OBJCPLUS
2073 /* Indicate no valid break/continue context by setting these variables
2074 to some non-null, non-label value. We'll notice and emit the proper
2075 error message in c_finish_bc_stmt. */
2076 c_break_label = c_cont_label = size_zero_node;
2077 #endif
2079 if (attributes)
2080 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2081 else
2082 objc_decl_method_attributes (&decl, attributes, 0);
2084 objc_add_method (objc_implementation_context,
2085 decl,
2086 is_class_method,
2087 /* is optional */ false);
2088 start_method_def (decl, expr);
2089 return true;
2092 void
2093 objc_add_instance_variable (tree decl)
2095 (void) add_instance_variable (objc_ivar_context,
2096 objc_ivar_visibility,
2097 decl);
2100 /* Construct a C struct with same name as KLASS, a base struct with tag
2101 SUPER_NAME (if any), and FIELDS indicated. */
2103 static tree
2104 objc_build_struct (tree klass, tree fields, tree super_name)
2106 tree name = CLASS_NAME (klass);
2107 tree s = objc_start_struct (name);
2108 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
2109 tree t;
2110 vec<tree> objc_info = vNULL;
2111 int i;
2113 if (super)
2115 /* Prepend a packed variant of the base class into the layout. This
2116 is necessary to preserve ObjC ABI compatibility. */
2117 tree base = build_decl (input_location,
2118 FIELD_DECL, NULL_TREE, super);
2119 tree field = TYPE_FIELDS (super);
2121 while (field && DECL_CHAIN (field)
2122 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2123 field = DECL_CHAIN (field);
2125 /* For ObjC ABI purposes, the "packed" size of a base class is
2126 the sum of the offset and the size (in bits) of the last field
2127 in the class. */
2128 DECL_SIZE (base)
2129 = (field && TREE_CODE (field) == FIELD_DECL
2130 ? size_binop (PLUS_EXPR,
2131 size_binop (PLUS_EXPR,
2132 size_binop
2133 (MULT_EXPR,
2134 convert (bitsizetype,
2135 DECL_FIELD_OFFSET (field)),
2136 bitsize_int (BITS_PER_UNIT)),
2137 DECL_FIELD_BIT_OFFSET (field)),
2138 DECL_SIZE (field))
2139 : bitsize_zero_node);
2140 DECL_SIZE_UNIT (base)
2141 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2142 size_int (BITS_PER_UNIT));
2143 DECL_ARTIFICIAL (base) = 1;
2144 DECL_ALIGN (base) = 1;
2145 DECL_FIELD_CONTEXT (base) = s;
2146 #ifdef OBJCPLUS
2147 DECL_FIELD_IS_BASE (base) = 1;
2149 if (fields)
2150 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2151 #endif /* are following the ObjC ABI here. */
2152 DECL_CHAIN (base) = fields;
2153 fields = base;
2156 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2157 information in all variants of this RECORD_TYPE to be destroyed
2158 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2159 for something else and then will change all variants to use the
2160 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2161 it for ObjC protocols and that such propagation will make all
2162 variants use the same objc_info), but it is therein that we store
2163 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2164 Hence, we must save the ObjC-specific information before calling
2165 finish_struct(), and then reinstate it afterwards. */
2167 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
2169 INIT_TYPE_OBJC_INFO (t);
2170 objc_info.safe_push (TYPE_OBJC_INFO (t));
2173 s = objc_finish_struct (s, fields);
2175 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
2177 /* We now want to restore the different TYPE_OBJC_INFO, but we
2178 have the additional problem that the C frontend doesn't just
2179 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2180 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2181 we need a different TYPE_OBJC_INFO for each (and
2182 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2183 make a copy of each TYPE_LANG_SPECIFIC before we modify
2184 TYPE_OBJC_INFO. */
2185 if (TYPE_LANG_SPECIFIC (t))
2187 /* Create a copy of TYPE_LANG_SPECIFIC. */
2188 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2189 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2190 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2191 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2193 else
2195 /* Just create a new one. */
2196 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2198 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2199 protocol information that may have been associated with the
2200 type. */
2201 TYPE_OBJC_INFO (t) = objc_info[i];
2202 /* Replace the IDENTIFIER_NODE with an actual @interface now
2203 that we have it. */
2204 TYPE_OBJC_INTERFACE (t) = klass;
2206 objc_info.release ();
2208 /* Use TYPE_BINFO structures to point at the super class, if any. */
2209 objc_xref_basetypes (s, super);
2211 /* Mark this struct as a class template. */
2212 CLASS_STATIC_TEMPLATE (klass) = s;
2214 return s;
2217 /* Mark DECL as being 'volatile' for purposes of Darwin
2218 _setjmp()/_longjmp() exception handling. Called from
2219 objc_mark_locals_volatile(). */
2220 void
2221 objc_volatilize_decl (tree decl)
2223 /* Do not mess with variables that are 'static' or (already)
2224 'volatile'. */
2225 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2226 && (TREE_CODE (decl) == VAR_DECL
2227 || TREE_CODE (decl) == PARM_DECL))
2229 if (local_variables_to_volatilize == NULL)
2230 vec_alloc (local_variables_to_volatilize, 8);
2232 vec_safe_push (local_variables_to_volatilize, decl);
2236 /* Called when parsing of a function completes; if any local variables
2237 in the function were marked as variables to volatilize, change them
2238 to volatile. We do this at the end of the function when the
2239 warnings about discarding 'volatile' have already been produced.
2240 We are making the variables as volatile just to force the compiler
2241 to preserve them between setjmp/longjmp, but we don't want warnings
2242 for them as they aren't really volatile. */
2243 void
2244 objc_finish_function (void)
2246 /* If there are any local variables to volatilize, volatilize them. */
2247 if (local_variables_to_volatilize)
2249 int i;
2250 tree decl;
2251 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
2253 tree t = TREE_TYPE (decl);
2255 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2256 TREE_TYPE (decl) = t;
2257 TREE_THIS_VOLATILE (decl) = 1;
2258 TREE_SIDE_EFFECTS (decl) = 1;
2259 DECL_REGISTER (decl) = 0;
2260 #ifndef OBJCPLUS
2261 C_DECL_REGISTER (decl) = 0;
2262 #endif
2265 /* Now we delete the vector. This sets it to NULL as well. */
2266 vec_free (local_variables_to_volatilize);
2270 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2271 (including its categories and superclasses) or by object type TYP.
2272 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2274 static bool
2275 objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
2277 bool class_type = (cls != NULL_TREE);
2279 while (cls)
2281 tree c;
2283 /* Check protocols adopted by the class and its categories. */
2284 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2286 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2287 return true;
2290 /* Repeat for superclasses. */
2291 cls = lookup_interface (CLASS_SUPER_NAME (cls));
2294 /* Check for any protocols attached directly to the object type. */
2295 if (TYPE_HAS_OBJC_INFO (typ))
2297 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2298 return true;
2301 if (warn)
2303 *errbuf = 0;
2304 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
2305 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2306 "implementing" a given protocol, since they do not have an
2307 implementation. */
2308 if (class_type)
2309 warning (0, "class %qs does not implement the %qE protocol",
2310 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2311 else
2312 warning (0, "type %qs does not conform to the %qE protocol",
2313 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2316 return false;
2319 /* Check if class RCLS and instance struct type RTYP conform to at least the
2320 same protocols that LCLS and LTYP conform to. */
2322 static bool
2323 objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2325 tree p;
2326 bool have_lproto = false;
2328 while (lcls)
2330 /* NB: We do _not_ look at categories defined for LCLS; these may or
2331 may not get loaded in, and therefore it is unreasonable to require
2332 that RCLS/RTYP must implement any of their protocols. */
2333 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2335 have_lproto = true;
2337 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2338 return warn;
2341 /* Repeat for superclasses. */
2342 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2345 /* Check for any protocols attached directly to the object type. */
2346 if (TYPE_HAS_OBJC_INFO (ltyp))
2348 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2350 have_lproto = true;
2352 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2353 return warn;
2357 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2358 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2359 away with simply checking for 'id' or 'Class' (!RCLS), since this
2360 routine will not get called in other cases. */
2361 return have_lproto || (rcls != NULL_TREE);
2364 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2365 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2366 compatible by objc_compare_types() below. */
2368 tree
2369 objc_common_type (tree type1, tree type2)
2371 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2373 while (POINTER_TYPE_P (inner1))
2375 inner1 = TREE_TYPE (inner1);
2376 inner2 = TREE_TYPE (inner2);
2379 /* If one type is derived from another, return the base type. */
2380 if (DERIVED_FROM_P (inner1, inner2))
2381 return type1;
2382 else if (DERIVED_FROM_P (inner2, inner1))
2383 return type2;
2385 /* If both types are 'Class', return 'Class'. */
2386 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2387 return objc_class_type;
2389 /* Otherwise, return 'id'. */
2390 return objc_object_type;
2393 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2394 an instance of RTYP to an instance of LTYP or to compare the two
2395 (if ARGNO is equal to -3), per ObjC type system rules. Before
2396 returning 'true', this routine may issue warnings related to, e.g.,
2397 protocol conformance. When returning 'false', the routine must
2398 produce absolutely no warnings; the C or C++ front-end will do so
2399 instead, if needed. If either LTYP or RTYP is not an Objective-C
2400 type, the routine must return 'false'.
2402 The ARGNO parameter is encoded as follows:
2403 >= 1 Parameter number (CALLEE contains function being called);
2404 0 Return value;
2405 -1 Assignment;
2406 -2 Initialization;
2407 -3 Comparison (LTYP and RTYP may match in either direction);
2408 -4 Silent comparison (for C++ overload resolution);
2409 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2410 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2411 so that each object of type RTYP is also of type LTYP). This is used
2412 when comparing property types. */
2414 bool
2415 objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
2417 tree lcls, rcls, lproto, rproto;
2418 bool pointers_compatible;
2420 /* We must be dealing with pointer types */
2421 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2422 return false;
2426 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2427 rtyp = TREE_TYPE (rtyp);
2429 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2431 /* We must also handle function pointers, since ObjC is a bit more
2432 lenient than C or C++ on this. */
2433 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2435 function_args_iterator liter, riter;
2437 /* Return types must be covariant. */
2438 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2439 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2440 argno, callee))
2441 return false;
2443 /* Argument types must be contravariant. */
2444 function_args_iter_init (&liter, ltyp);
2445 function_args_iter_init (&riter, rtyp);
2447 while (1)
2449 ltyp = function_args_iter_cond (&liter);
2450 rtyp = function_args_iter_cond (&riter);
2452 /* If we've exhaused both lists simulateously, we're done. */
2453 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2454 break;
2456 /* If one list is shorter than the other, they fail to match. */
2457 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
2458 return false;
2460 if (!comptypes (rtyp, ltyp)
2461 && !objc_compare_types (rtyp, ltyp, argno, callee))
2462 return false;
2464 function_args_iter_next (&liter);
2465 function_args_iter_next (&riter);
2468 return true;
2471 /* Past this point, we are only interested in ObjC class instances,
2472 or 'id' or 'Class'. */
2473 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2474 return false;
2476 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2477 && !TYPE_HAS_OBJC_INFO (ltyp))
2478 return false;
2480 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2481 && !TYPE_HAS_OBJC_INFO (rtyp))
2482 return false;
2484 /* Past this point, we are committed to returning 'true' to the caller
2485 (unless performing a silent comparison; see below). However, we can
2486 still warn about type and/or protocol mismatches. */
2488 if (TYPE_HAS_OBJC_INFO (ltyp))
2490 lcls = TYPE_OBJC_INTERFACE (ltyp);
2491 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2493 else
2494 lcls = lproto = NULL_TREE;
2496 if (TYPE_HAS_OBJC_INFO (rtyp))
2498 rcls = TYPE_OBJC_INTERFACE (rtyp);
2499 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2501 else
2502 rcls = rproto = NULL_TREE;
2504 /* If we could not find an @interface declaration, we must have
2505 only seen a @class declaration; for purposes of type comparison,
2506 treat it as a stand-alone (root) class. */
2508 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2509 lcls = NULL_TREE;
2511 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2512 rcls = NULL_TREE;
2514 /* If either type is an unqualified 'id', we're done. This is because
2515 an 'id' can be assigned to or from any type with no warnings. */
2516 if (argno != -5)
2518 if ((!lproto && objc_is_object_id (ltyp))
2519 || (!rproto && objc_is_object_id (rtyp)))
2520 return true;
2522 else
2524 /* For property checks, though, an 'id' is considered the most
2525 general type of object, hence if you try to specialize an
2526 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2527 to warn. */
2528 if (!lproto && objc_is_object_id (ltyp))
2529 return true;
2532 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
2534 /* If the underlying types are the same, and at most one of them has
2535 a protocol list, we do not need to issue any diagnostics. */
2536 if (pointers_compatible && (!lproto || !rproto))
2537 return true;
2539 /* If exactly one of the types is 'Class', issue a diagnostic; any
2540 exceptions of this rule have already been handled. */
2541 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2542 pointers_compatible = false;
2543 /* Otherwise, check for inheritance relations. */
2544 else
2546 if (!pointers_compatible)
2548 /* Again, if any of the two is an 'id', we're satisfied,
2549 unless we're comparing properties, in which case only an
2550 'id' on the left-hand side (old property) is good
2551 enough. */
2552 if (argno != -5)
2553 pointers_compatible
2554 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2555 else
2556 pointers_compatible = objc_is_object_id (ltyp);
2559 if (!pointers_compatible)
2560 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2562 if (!pointers_compatible && (argno == -3 || argno == -4))
2563 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2566 /* If the pointers match modulo protocols, check for protocol conformance
2567 mismatches. */
2568 if (pointers_compatible)
2570 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2571 argno != -3);
2573 if (!pointers_compatible && argno == -3)
2574 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2575 argno != -3);
2578 if (!pointers_compatible)
2580 /* The two pointers are not exactly compatible. Issue a warning, unless
2581 we are performing a silent comparison, in which case return 'false'
2582 instead. */
2583 /* NB: For the time being, we shall make our warnings look like their
2584 C counterparts. In the future, we may wish to make them more
2585 ObjC-specific. */
2586 switch (argno)
2588 case -5:
2589 case -4:
2590 return false;
2592 case -3:
2593 warning (0, "comparison of distinct Objective-C types lacks a cast");
2594 break;
2596 case -2:
2597 warning (0, "initialization from distinct Objective-C type");
2598 break;
2600 case -1:
2601 warning (0, "assignment from distinct Objective-C type");
2602 break;
2604 case 0:
2605 warning (0, "distinct Objective-C type in return");
2606 break;
2608 default:
2609 warning (0, "passing argument %d of %qE from distinct "
2610 "Objective-C type", argno, callee);
2611 break;
2615 return true;
2618 /* This routine is similar to objc_compare_types except that function-pointers are
2619 excluded. This is because, caller assumes that common types are of (id, Object*)
2620 variety and calls objc_common_type to obtain a common type. There is no commonolty
2621 between two function-pointers in this regard. */
2623 bool
2624 objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2626 if (objc_compare_types (ltyp, rtyp, argno, callee))
2628 /* exclude function-pointer types. */
2631 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2632 rtyp = TREE_TYPE (rtyp);
2634 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2635 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2637 return false;
2640 #ifndef OBJCPLUS
2641 /* Determine if CHILD is derived from PARENT. The routine assumes that
2642 both parameters are RECORD_TYPEs, and is non-reflexive. */
2644 static bool
2645 objc_derived_from_p (tree parent, tree child)
2647 parent = TYPE_MAIN_VARIANT (parent);
2649 for (child = TYPE_MAIN_VARIANT (child);
2650 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
2652 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2653 (TYPE_BINFO (child),
2654 0)));
2656 if (child == parent)
2657 return true;
2660 return false;
2662 #endif
2664 tree
2665 objc_build_component_ref (tree datum, tree component)
2667 /* If COMPONENT is NULL, the caller is referring to the anonymous
2668 base class field. */
2669 if (!component)
2671 tree base = TYPE_FIELDS (TREE_TYPE (datum));
2673 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2676 /* The 'build_component_ref' routine has been removed from the C++
2677 front-end, but 'finish_class_member_access_expr' seems to be
2678 a worthy substitute. */
2679 #ifdef OBJCPLUS
2680 return finish_class_member_access_expr (datum, component, false,
2681 tf_warning_or_error);
2682 #else
2683 return build_component_ref (input_location, datum, component);
2684 #endif
2687 /* Recursively copy inheritance information rooted at BINFO. To do this,
2688 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2690 static tree
2691 objc_copy_binfo (tree binfo)
2693 tree btype = BINFO_TYPE (binfo);
2694 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2695 tree base_binfo;
2696 int ix;
2698 BINFO_TYPE (binfo2) = btype;
2699 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2700 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2702 /* Recursively copy base binfos of BINFO. */
2703 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2705 tree base_binfo2 = objc_copy_binfo (base_binfo);
2707 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2708 BINFO_BASE_APPEND (binfo2, base_binfo2);
2711 return binfo2;
2714 /* Record superclass information provided in BASETYPE for ObjC class REF.
2715 This is loosely based on cp/decl.c:xref_basetypes(). */
2717 static void
2718 objc_xref_basetypes (tree ref, tree basetype)
2720 tree variant;
2721 tree binfo = make_tree_binfo (basetype ? 1 : 0);
2722 TYPE_BINFO (ref) = binfo;
2723 BINFO_OFFSET (binfo) = size_zero_node;
2724 BINFO_TYPE (binfo) = ref;
2726 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2727 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2728 TYPE_BINFO (variant) = binfo;
2730 if (basetype)
2732 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
2734 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
2735 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
2736 BINFO_BASE_APPEND (binfo, base_binfo);
2737 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
2741 /* Called from finish_decl. */
2743 void
2744 objc_check_decl (tree decl)
2746 tree type = TREE_TYPE (decl);
2748 if (TREE_CODE (type) != RECORD_TYPE)
2749 return;
2750 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
2751 error ("statically allocated instance of Objective-C class %qE",
2752 type);
2755 void
2756 objc_check_global_decl (tree decl)
2758 tree id = DECL_NAME (decl);
2759 if (objc_is_class_name (id) && global_bindings_p())
2760 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2763 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2764 INTERFACE may either name an Objective-C class, or refer to the
2765 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2766 type, just return it unchanged. This function is often called when
2767 PROTOCOLS is NULL_TREE, in which case we simply look up the
2768 appropriate INTERFACE. */
2770 tree
2771 objc_get_protocol_qualified_type (tree interface, tree protocols)
2773 /* If INTERFACE is not provided, default to 'id'. */
2774 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2775 bool is_ptr = (type != NULL_TREE);
2777 if (!is_ptr)
2779 type = objc_is_class_name (interface);
2781 if (type)
2783 /* If looking at a typedef, retrieve the precise type it
2784 describes. */
2785 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2786 interface = identifier_global_value (interface);
2788 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2789 && DECL_ORIGINAL_TYPE (interface))
2790 ? DECL_ORIGINAL_TYPE (interface)
2791 : xref_tag (RECORD_TYPE, type));
2793 else
2795 /* This case happens when we are given an 'interface' which
2796 is not a valid class name. For example if a typedef was
2797 used, and 'interface' really is the identifier of the
2798 typedef, but when you resolve it you don't get an
2799 Objective-C class, but something else, such as 'int'.
2800 This is an error; protocols make no sense unless you use
2801 them with Objective-C objects. */
2802 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2804 /* Try to recover. Ignore the invalid class name, and treat
2805 the object as an 'id' to silence further warnings about
2806 the class. */
2807 type = objc_object_type;
2808 is_ptr = true;
2812 if (protocols)
2814 type = build_variant_type_copy (type);
2816 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2817 to the pointee. */
2818 if (is_ptr)
2820 tree orig_pointee_type = TREE_TYPE (type);
2821 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2823 /* Set up the canonical type information. */
2824 TYPE_CANONICAL (type)
2825 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2827 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2828 type = TREE_TYPE (type);
2831 /* Look up protocols and install in lang specific list. */
2832 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
2833 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2834 (protocols, /* definition_required */ false);
2836 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2837 return the pointer to the new pointee variant. */
2838 if (is_ptr)
2839 type = TYPE_POINTER_TO (type);
2840 else
2841 TYPE_OBJC_INTERFACE (type)
2842 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
2845 return type;
2848 /* Check for circular dependencies in protocols. The arguments are
2849 PROTO, the protocol to check, and LIST, a list of protocol it
2850 conforms to. */
2852 static void
2853 check_protocol_recursively (tree proto, tree list)
2855 tree p;
2857 for (p = list; p; p = TREE_CHAIN (p))
2859 tree pp = TREE_VALUE (p);
2861 if (TREE_CODE (pp) == IDENTIFIER_NODE)
2862 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2863 /* definition_required */ false);
2865 if (pp == proto)
2866 fatal_error ("protocol %qE has circular dependency",
2867 PROTOCOL_NAME (pp));
2868 if (pp)
2869 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2873 /* Look up PROTOCOLS, and return a list of those that are found. If
2874 none are found, return NULL. Note that this function will emit a
2875 warning if a protocol is found and is deprecated. If
2876 'definition_required', then warn if the protocol is found but is
2877 not defined (ie, if we only saw a forward-declaration of the
2878 protocol (as in "@protocol NSObject;") not a real definition with
2879 the list of methods). */
2880 static tree
2881 lookup_and_install_protocols (tree protocols, bool definition_required)
2883 tree proto;
2884 tree return_value = NULL_TREE;
2886 if (protocols == error_mark_node)
2887 return NULL;
2889 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2891 tree ident = TREE_VALUE (proto);
2892 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2893 definition_required);
2895 if (p)
2896 return_value = chainon (return_value,
2897 build_tree_list (NULL_TREE, p));
2898 else if (ident != error_mark_node)
2899 error ("cannot find protocol declaration for %qE",
2900 ident);
2903 return return_value;
2906 static void
2907 build_common_objc_exception_stuff (void)
2909 tree noreturn_list, nothrow_list, temp_type;
2911 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2912 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
2914 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2915 /* void objc_sync_enter(id); */
2916 /* void objc_sync_exit(id); */
2917 temp_type = build_function_type_list (void_type_node,
2918 objc_object_type,
2919 NULL_TREE);
2920 objc_exception_throw_decl
2921 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2922 noreturn_list);
2923 /* Make sure that objc_exception_throw (id) claims that it may throw an
2924 exception. */
2925 TREE_NOTHROW (objc_exception_throw_decl) = 0;
2927 objc_sync_enter_decl
2928 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2929 NULL, nothrow_list);
2931 objc_sync_exit_decl
2932 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2933 NULL, nothrow_list);
2936 /* Purpose: "play" parser, creating/installing representations
2937 of the declarations that are required by Objective-C.
2939 Model:
2941 type_spec--------->sc_spec
2942 (tree_list) (tree_list)
2945 identifier_node identifier_node */
2947 static void
2948 synth_module_prologue (void)
2950 tree type;
2951 enum debug_info_type save_write_symbols = write_symbols;
2952 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2954 /* Suppress outputting debug symbols, because
2955 dbxout_init hasn't been called yet. */
2956 write_symbols = NO_DEBUG;
2957 debug_hooks = &do_nothing_debug_hooks;
2959 #ifdef OBJCPLUS
2960 push_lang_context (lang_name_c); /* extern "C" */
2961 #endif
2963 /* The following are also defined in <objc/objc.h> and friends. */
2965 objc_object_id = get_identifier (TAG_OBJECT);
2966 objc_class_id = get_identifier (TAG_CLASS);
2968 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
2969 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
2971 objc_object_type = build_pointer_type (objc_object_reference);
2972 objc_class_type = build_pointer_type (objc_class_reference);
2974 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2975 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
2977 /* Declare the 'id' and 'Class' typedefs. */
2978 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2979 TYPE_DECL,
2980 objc_object_name,
2981 objc_object_type));
2982 TREE_NO_WARNING (type) = 1;
2984 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2985 TYPE_DECL,
2986 objc_class_name,
2987 objc_class_type));
2988 TREE_NO_WARNING (type) = 1;
2990 /* Forward-declare '@interface Protocol'. */
2991 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
2992 objc_declare_class (type);
2993 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
2995 /* Declare receiver type used for dispatching messages to 'super'. */
2996 /* `struct objc_super *' */
2997 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2998 get_identifier (TAG_SUPER)));
3000 /* Declare pointers to method and ivar lists. */
3001 objc_method_list_ptr = build_pointer_type
3002 (xref_tag (RECORD_TYPE,
3003 get_identifier (UTAG_METHOD_LIST)));
3004 objc_method_proto_list_ptr
3005 = build_pointer_type (xref_tag (RECORD_TYPE,
3006 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
3007 objc_ivar_list_ptr = build_pointer_type
3008 (xref_tag (RECORD_TYPE,
3009 get_identifier (UTAG_IVAR_LIST)));
3011 build_common_objc_exception_stuff ();
3013 /* Set-up runtime-specific templates, message and exception stuff. */
3014 (*runtime.initialize) ();
3016 /* Declare objc_getProperty, object_setProperty and other property
3017 accessor helpers. */
3018 build_common_objc_property_accessor_helpers ();
3020 /* Forward declare constant_string_id and constant_string_type. */
3021 if (!constant_string_class_name)
3022 constant_string_class_name = runtime.default_constant_string_class_name;
3023 constant_string_id = get_identifier (constant_string_class_name);
3024 objc_declare_class (constant_string_id);
3026 /* Pre-build the following entities - for speed/convenience. */
3027 self_id = get_identifier ("self");
3028 ucmd_id = get_identifier ("_cmd");
3030 /* Declare struct _objc_fast_enumeration_state { ... }; */
3031 build_fast_enumeration_state_template ();
3033 /* void objc_enumeration_mutation (id) */
3034 type = build_function_type_list (void_type_node,
3035 objc_object_type, NULL_TREE);
3036 objc_enumeration_mutation_decl
3037 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
3038 NULL, NULL_TREE);
3039 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3041 #ifdef OBJCPLUS
3042 pop_lang_context ();
3043 #endif
3045 write_symbols = save_write_symbols;
3046 debug_hooks = save_hooks;
3049 /* --- const strings --- */
3051 /* Ensure that the ivar list for NSConstantString/NXConstantString
3052 (or whatever was specified via `-fconstant-string-class')
3053 contains fields at least as large as the following three, so that
3054 the runtime can stomp on them with confidence:
3056 struct STRING_OBJECT_CLASS_NAME
3058 Object isa;
3059 char *cString;
3060 unsigned int length;
3061 }; */
3063 static int
3064 check_string_class_template (void)
3066 tree field_decl = objc_get_class_ivars (constant_string_id);
3068 #define AT_LEAST_AS_LARGE_AS(F, T) \
3069 (F && TREE_CODE (F) == FIELD_DECL \
3070 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3071 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3073 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3074 return 0;
3076 field_decl = DECL_CHAIN (field_decl);
3077 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3078 return 0;
3080 field_decl = DECL_CHAIN (field_decl);
3081 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
3083 #undef AT_LEAST_AS_LARGE_AS
3086 /* Avoid calling `check_string_class_template ()' more than once. */
3087 static GTY(()) int string_layout_checked;
3089 /* Construct an internal string layout to be used as a template for
3090 creating NSConstantString/NXConstantString instances. */
3092 static tree
3093 objc_build_internal_const_str_type (void)
3095 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
3096 tree fields = build_decl (input_location,
3097 FIELD_DECL, NULL_TREE, ptr_type_node);
3098 tree field = build_decl (input_location,
3099 FIELD_DECL, NULL_TREE, ptr_type_node);
3101 DECL_CHAIN (field) = fields; fields = field;
3102 field = build_decl (input_location,
3103 FIELD_DECL, NULL_TREE, unsigned_type_node);
3104 DECL_CHAIN (field) = fields; fields = field;
3105 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3106 reverse order! */
3107 finish_builtin_struct (type, "__builtin_ObjCString",
3108 fields, NULL_TREE);
3110 return type;
3113 /* Custom build_string which sets TREE_TYPE! */
3115 tree
3116 my_build_string (int len, const char *str)
3118 return fix_string_type (build_string (len, str));
3121 /* Build a string with contents STR and length LEN and convert it to a
3122 pointer. */
3124 tree
3125 my_build_string_pointer (int len, const char *str)
3127 tree string = my_build_string (len, str);
3128 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3129 return build1 (ADDR_EXPR, ptrtype, string);
3132 hashval_t
3133 objc_string_hasher::hash (string_descriptor *ptr)
3135 const_tree const str = ptr->literal;
3136 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3137 int i, len = TREE_STRING_LENGTH (str);
3138 hashval_t h = len;
3140 for (i = 0; i < len; i++)
3141 h = ((h * 613) + p[i]);
3143 return h;
3146 bool
3147 objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2)
3149 const_tree const str1 = ptr1->literal;
3150 const_tree const str2 = ptr2->literal;
3151 int len1 = TREE_STRING_LENGTH (str1);
3153 return (len1 == TREE_STRING_LENGTH (str2)
3154 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3155 len1));
3158 /* Given a chain of STRING_CST's, build a static instance of
3159 NXConstantString which points at the concatenation of those
3160 strings. We place the string object in the __string_objects
3161 section of the __OBJC segment. The Objective-C runtime will
3162 initialize the isa pointers of the string objects to point at the
3163 NXConstantString class object. */
3165 tree
3166 objc_build_string_object (tree string)
3168 tree constant_string_class;
3169 int length;
3170 tree addr;
3171 struct string_descriptor *desc, key;
3173 /* We should be passed a STRING_CST. */
3174 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
3175 length = TREE_STRING_LENGTH (string) - 1;
3177 /* The target may have different ideas on how to construct an ObjC string
3178 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3179 constant CFString reference instead.
3180 At present, this is only supported for the NeXT runtime. */
3181 if (flag_next_runtime
3182 && targetcm.objc_construct_string_object)
3184 tree constructor = (*targetcm.objc_construct_string_object) (string);
3185 if (constructor)
3186 return build1 (NOP_EXPR, objc_object_type, constructor);
3189 /* Check whether the string class being used actually exists and has the
3190 correct ivar layout. */
3191 if (!string_layout_checked)
3193 string_layout_checked = -1;
3194 constant_string_class = lookup_interface (constant_string_id);
3195 internal_const_str_type = objc_build_internal_const_str_type ();
3197 if (!constant_string_class
3198 || !(constant_string_type
3199 = CLASS_STATIC_TEMPLATE (constant_string_class)))
3200 error ("cannot find interface declaration for %qE",
3201 constant_string_id);
3202 /* The NSConstantString/NXConstantString ivar layout is now known. */
3203 else if (!check_string_class_template ())
3204 error ("interface %qE does not have valid constant string layout",
3205 constant_string_id);
3206 /* If the runtime can generate a literal reference to the string class,
3207 don't need to run a constructor. */
3208 else if (!(*runtime.setup_const_string_class_decl)())
3209 error ("cannot find reference tag for class %qE", constant_string_id);
3210 else
3212 string_layout_checked = 1; /* Success! */
3213 add_class_reference (constant_string_id);
3217 if (string_layout_checked == -1)
3218 return error_mark_node;
3220 /* Perhaps we already constructed a constant string just like this one? */
3221 key.literal = string;
3222 string_descriptor **loc = string_htab->find_slot (&key, INSERT);
3223 desc = *loc;
3225 if (!desc)
3227 *loc = desc = ggc_alloc<string_descriptor> ();
3228 desc->literal = string;
3229 desc->constructor =
3230 (*runtime.build_const_string_constructor) (input_location, string, length);
3233 addr = convert (build_pointer_type (constant_string_type),
3234 build_unary_op (input_location,
3235 ADDR_EXPR, desc->constructor, 1));
3237 return addr;
3240 /* Build a static constant CONSTRUCTOR
3241 with type TYPE and elements ELTS. */
3243 tree
3244 objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
3246 tree constructor = build_constructor (type, elts);
3248 TREE_CONSTANT (constructor) = 1;
3249 TREE_STATIC (constructor) = 1;
3250 TREE_READONLY (constructor) = 1;
3252 #ifdef OBJCPLUS
3253 /* Adjust for impedance mismatch. We should figure out how to build
3254 CONSTRUCTORs that consistently please both the C and C++ gods. */
3255 if (!(*elts)[0].index)
3256 TREE_TYPE (constructor) = init_list_type_node;
3257 #endif
3259 return constructor;
3262 /* Return the DECL of the string IDENT in the SECTION. */
3264 tree
3265 get_objc_string_decl (tree ident, enum string_section section)
3267 tree chain;
3269 switch (section)
3271 case class_names:
3272 chain = class_names_chain;
3273 break;
3274 case meth_var_names:
3275 chain = meth_var_names_chain;
3276 break;
3277 case meth_var_types:
3278 chain = meth_var_types_chain;
3279 break;
3280 case prop_names_attr:
3281 chain = prop_names_attr_chain;
3282 break;
3283 default:
3284 gcc_unreachable ();
3287 for (; chain != 0; chain = TREE_CHAIN (chain))
3288 if (TREE_VALUE (chain) == ident)
3289 return (TREE_PURPOSE (chain));
3291 /* We didn't find the entry. */
3292 return NULL_TREE;
3295 /* Create a class reference, but don't create a variable to reference
3296 it. */
3298 void
3299 add_class_reference (tree ident)
3301 tree chain;
3303 if ((chain = cls_ref_chain))
3305 tree tail;
3308 if (ident == TREE_VALUE (chain))
3309 return;
3311 tail = chain;
3312 chain = TREE_CHAIN (chain);
3314 while (chain);
3316 /* Append to the end of the list */
3317 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3319 else
3320 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
3323 /* Get a class reference, creating it if necessary. Also create the
3324 reference variable. */
3325 tree
3326 objc_get_class_reference (tree ident)
3328 tree orig_ident = (DECL_P (ident)
3329 ? DECL_NAME (ident)
3330 : TYPE_P (ident)
3331 ? OBJC_TYPE_NAME (ident)
3332 : ident);
3333 bool local_scope = false;
3335 #ifdef OBJCPLUS
3336 if (processing_template_decl)
3337 /* Must wait until template instantiation time. */
3338 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
3339 #endif
3341 if (TREE_CODE (ident) == TYPE_DECL)
3342 ident = (DECL_ORIGINAL_TYPE (ident)
3343 ? DECL_ORIGINAL_TYPE (ident)
3344 : TREE_TYPE (ident));
3346 #ifdef OBJCPLUS
3347 if (TYPE_P (ident)
3348 && CP_TYPE_CONTEXT (ident) != global_namespace)
3349 local_scope = true;
3350 #endif
3352 if (local_scope || !(ident = objc_is_class_name (ident)))
3354 error ("%qE is not an Objective-C class name or alias",
3355 orig_ident);
3356 return error_mark_node;
3359 return (*runtime.get_class_reference) (ident);
3362 void
3363 objc_declare_alias (tree alias_ident, tree class_ident)
3365 tree underlying_class;
3367 #ifdef OBJCPLUS
3368 if (current_namespace != global_namespace) {
3369 error ("Objective-C declarations may only appear in global scope");
3371 #endif /* OBJCPLUS */
3373 if (!(underlying_class = objc_is_class_name (class_ident)))
3374 warning (0, "cannot find class %qE", class_ident);
3375 else if (objc_is_class_name (alias_ident))
3376 warning (0, "class %qE already exists", alias_ident);
3377 else
3379 /* Implement @compatibility_alias as a typedef. */
3380 #ifdef OBJCPLUS
3381 push_lang_context (lang_name_c); /* extern "C" */
3382 #endif
3383 lang_hooks.decls.pushdecl (build_decl
3384 (input_location,
3385 TYPE_DECL,
3386 alias_ident,
3387 xref_tag (RECORD_TYPE, underlying_class)));
3388 #ifdef OBJCPLUS
3389 pop_lang_context ();
3390 #endif
3391 objc_map_put (alias_name_map, alias_ident, underlying_class);
3395 void
3396 objc_declare_class (tree identifier)
3398 #ifdef OBJCPLUS
3399 if (current_namespace != global_namespace) {
3400 error ("Objective-C declarations may only appear in global scope");
3402 #endif /* OBJCPLUS */
3404 if (! objc_is_class_name (identifier))
3406 tree record = lookup_name (identifier), type = record;
3408 if (record)
3410 if (TREE_CODE (record) == TYPE_DECL)
3411 type = DECL_ORIGINAL_TYPE (record)
3412 ? DECL_ORIGINAL_TYPE (record)
3413 : TREE_TYPE (record);
3415 if (!TYPE_HAS_OBJC_INFO (type)
3416 || !TYPE_OBJC_INTERFACE (type))
3418 error ("%qE redeclared as different kind of symbol",
3419 identifier);
3420 error ("previous declaration of %q+D",
3421 record);
3425 record = xref_tag (RECORD_TYPE, identifier);
3426 INIT_TYPE_OBJC_INFO (record);
3427 /* In the case of a @class declaration, we store the ident in
3428 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3429 we'll replace the ident with the interface. */
3430 TYPE_OBJC_INTERFACE (record) = identifier;
3431 objc_map_put (class_name_map, identifier, NULL_TREE);
3435 tree
3436 objc_is_class_name (tree ident)
3438 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3440 tree t = identifier_global_value (ident);
3441 if (t)
3442 ident = t;
3445 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3446 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
3448 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3449 ident = OBJC_TYPE_NAME (ident);
3450 #ifdef OBJCPLUS
3451 if (ident && TREE_CODE (ident) == TYPE_DECL)
3453 tree type = TREE_TYPE (ident);
3454 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3455 return NULL_TREE;
3456 ident = DECL_NAME (ident);
3458 #endif
3459 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3460 return NULL_TREE;
3462 if (lookup_interface (ident))
3463 return ident;
3466 tree target;
3468 target = objc_map_get (class_name_map, ident);
3469 if (target != OBJC_MAP_NOT_FOUND)
3470 return ident;
3472 target = objc_map_get (alias_name_map, ident);
3473 if (target != OBJC_MAP_NOT_FOUND)
3474 return target;
3477 return 0;
3480 /* Check whether TYPE is either 'id' or 'Class'. */
3482 tree
3483 objc_is_id (tree type)
3485 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3487 tree t = identifier_global_value (type);
3488 if (t)
3489 type = t;
3492 if (type && TREE_CODE (type) == TYPE_DECL)
3493 type = TREE_TYPE (type);
3495 /* NB: This function may be called before the ObjC front-end has
3496 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3497 return (objc_object_type && type
3498 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3499 ? type
3500 : NULL_TREE);
3503 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3504 class instance. This is needed by other parts of the compiler to
3505 handle ObjC types gracefully. */
3507 tree
3508 objc_is_object_ptr (tree type)
3510 tree ret;
3512 type = TYPE_MAIN_VARIANT (type);
3513 if (!POINTER_TYPE_P (type))
3514 return 0;
3516 ret = objc_is_id (type);
3517 if (!ret)
3518 ret = objc_is_class_name (TREE_TYPE (type));
3520 return ret;
3523 static int
3524 objc_is_gcable_type (tree type, int or_strong_p)
3526 tree name;
3528 if (!TYPE_P (type))
3529 return 0;
3530 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3531 return 1;
3532 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3533 return 1;
3534 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3535 return 0;
3536 type = TREE_TYPE (type);
3537 if (TREE_CODE (type) != RECORD_TYPE)
3538 return 0;
3539 name = TYPE_NAME (type);
3540 return (objc_is_class_name (name) != NULL_TREE);
3543 static tree
3544 objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
3546 if (expr == oldexpr)
3547 return newexpr;
3549 switch (TREE_CODE (expr))
3551 case COMPONENT_REF:
3552 return objc_build_component_ref
3553 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3554 oldexpr,
3555 newexpr),
3556 DECL_NAME (TREE_OPERAND (expr, 1)));
3557 case ARRAY_REF:
3558 return build_array_ref (input_location,
3559 objc_substitute_decl (TREE_OPERAND (expr, 0),
3560 oldexpr,
3561 newexpr),
3562 TREE_OPERAND (expr, 1));
3563 case INDIRECT_REF:
3564 return build_indirect_ref (input_location,
3565 objc_substitute_decl (TREE_OPERAND (expr, 0),
3566 oldexpr,
3567 newexpr), RO_ARROW);
3568 default:
3569 return expr;
3573 static tree
3574 objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
3576 tree func_params;
3577 /* The LHS parameter contains the expression 'outervar->memberspec';
3578 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3579 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3581 tree offs
3582 = objc_substitute_decl
3583 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3584 tree func
3585 = (flag_objc_direct_dispatch
3586 ? objc_assign_ivar_fast_decl
3587 : objc_assign_ivar_decl);
3589 offs = convert (integer_type_node, build_unary_op (input_location,
3590 ADDR_EXPR, offs, 0));
3591 offs = fold (offs);
3592 func_params = tree_cons (NULL_TREE,
3593 convert (objc_object_type, rhs),
3594 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3595 tree_cons (NULL_TREE, offs,
3596 NULL_TREE)));
3598 return build_function_call (input_location, func, func_params);
3601 static tree
3602 objc_build_global_assignment (tree lhs, tree rhs)
3604 tree func_params = tree_cons (NULL_TREE,
3605 convert (objc_object_type, rhs),
3606 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3607 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3608 NULL_TREE));
3610 return build_function_call (input_location,
3611 objc_assign_global_decl, func_params);
3614 static tree
3615 objc_build_strong_cast_assignment (tree lhs, tree rhs)
3617 tree func_params = tree_cons (NULL_TREE,
3618 convert (objc_object_type, rhs),
3619 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3620 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3621 NULL_TREE));
3623 return build_function_call (input_location,
3624 objc_assign_strong_cast_decl, func_params);
3627 static int
3628 objc_is_gcable_p (tree expr)
3630 return (TREE_CODE (expr) == COMPONENT_REF
3631 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3632 : TREE_CODE (expr) == ARRAY_REF
3633 ? (objc_is_gcable_p (TREE_TYPE (expr))
3634 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3635 : TREE_CODE (expr) == ARRAY_TYPE
3636 ? objc_is_gcable_p (TREE_TYPE (expr))
3637 : TYPE_P (expr)
3638 ? objc_is_gcable_type (expr, 1)
3639 : (objc_is_gcable_p (TREE_TYPE (expr))
3640 || (DECL_P (expr)
3641 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
3644 static int
3645 objc_is_ivar_reference_p (tree expr)
3647 return (TREE_CODE (expr) == ARRAY_REF
3648 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3649 : TREE_CODE (expr) == COMPONENT_REF
3650 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3651 : 0);
3654 static int
3655 objc_is_global_reference_p (tree expr)
3657 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3658 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3659 : DECL_P (expr)
3660 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3661 : 0);
3664 tree
3665 objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3667 tree result = NULL_TREE, outer;
3668 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
3670 /* This function is currently only used with the next runtime with
3671 garbage collection enabled (-fobjc-gc). */
3672 gcc_assert (flag_next_runtime);
3674 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3675 will have been transformed to the form '*(type *)&expr'. */
3676 if (TREE_CODE (lhs) == INDIRECT_REF)
3678 outer = TREE_OPERAND (lhs, 0);
3680 while (!strong_cast_p
3681 && (CONVERT_EXPR_P (outer)
3682 || TREE_CODE (outer) == NON_LVALUE_EXPR))
3684 tree lhstype = TREE_TYPE (outer);
3686 /* Descend down the cast chain, and record the first objc_gc
3687 attribute found. */
3688 if (POINTER_TYPE_P (lhstype))
3690 tree attr
3691 = lookup_attribute ("objc_gc",
3692 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
3694 if (attr)
3695 strong_cast_p = 1;
3698 outer = TREE_OPERAND (outer, 0);
3702 /* If we have a __strong cast, it trumps all else. */
3703 if (strong_cast_p)
3705 if (modifycode != NOP_EXPR)
3706 goto invalid_pointer_arithmetic;
3708 if (warn_assign_intercept)
3709 warning (0, "strong-cast assignment has been intercepted");
3711 result = objc_build_strong_cast_assignment (lhs, rhs);
3713 goto exit_point;
3716 /* the lhs must be of a suitable type, regardless of its underlying
3717 structure. */
3718 if (!objc_is_gcable_p (lhs))
3719 goto exit_point;
3721 outer = lhs;
3723 while (outer
3724 && (TREE_CODE (outer) == COMPONENT_REF
3725 || TREE_CODE (outer) == ARRAY_REF))
3726 outer = TREE_OPERAND (outer, 0);
3728 if (TREE_CODE (outer) == INDIRECT_REF)
3730 outer = TREE_OPERAND (outer, 0);
3731 indirect_p = 1;
3734 outer_gc_p = objc_is_gcable_p (outer);
3736 /* Handle ivar assignments. */
3737 if (objc_is_ivar_reference_p (lhs))
3739 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3740 doesn't cut it here), the best we can do here is suggest a cast. */
3741 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3743 /* We may still be able to use the global write barrier... */
3744 if (!indirect_p && objc_is_global_reference_p (outer))
3745 goto global_reference;
3747 suggest_cast:
3748 if (modifycode == NOP_EXPR)
3750 if (warn_assign_intercept)
3751 warning (0, "strong-cast may possibly be needed");
3754 goto exit_point;
3757 if (modifycode != NOP_EXPR)
3758 goto invalid_pointer_arithmetic;
3760 if (warn_assign_intercept)
3761 warning (0, "instance variable assignment has been intercepted");
3763 result = objc_build_ivar_assignment (outer, lhs, rhs);
3765 goto exit_point;
3768 /* Likewise, intercept assignment to global/static variables if their type is
3769 GC-marked. */
3770 if (objc_is_global_reference_p (outer))
3772 if (indirect_p)
3773 goto suggest_cast;
3775 global_reference:
3776 if (modifycode != NOP_EXPR)
3778 invalid_pointer_arithmetic:
3779 if (outer_gc_p)
3780 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3782 goto exit_point;
3785 if (warn_assign_intercept)
3786 warning (0, "global/static variable assignment has been intercepted");
3788 result = objc_build_global_assignment (lhs, rhs);
3791 /* In all other cases, fall back to the normal mechanism. */
3792 exit_point:
3793 return result;
3796 /* Implementation of the table mapping a class name (as an identifier)
3797 to a class node. The two public functions for it are
3798 lookup_interface() and add_interface(). add_interface() is only
3799 used in this file, so we can make it static. */
3801 static GTY(()) objc_map_t interface_map;
3803 static void
3804 interface_hash_init (void)
3806 interface_map = objc_map_alloc_ggc (200);
3809 static tree
3810 add_interface (tree class_name, tree name)
3812 /* Put interfaces on list in reverse order. */
3813 TREE_CHAIN (class_name) = interface_chain;
3814 interface_chain = class_name;
3816 /* Add it to the map. */
3817 objc_map_put (interface_map, name, class_name);
3819 return interface_chain;
3822 tree
3823 lookup_interface (tree ident)
3825 #ifdef OBJCPLUS
3826 if (ident && TREE_CODE (ident) == TYPE_DECL)
3827 ident = DECL_NAME (ident);
3828 #endif
3830 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3831 return NULL_TREE;
3834 tree interface = objc_map_get (interface_map, ident);
3836 if (interface == OBJC_MAP_NOT_FOUND)
3837 return NULL_TREE;
3838 else
3839 return interface;
3845 /* Implement @defs (<classname>) within struct bodies. */
3847 tree
3848 objc_get_class_ivars (tree class_name)
3850 tree interface = lookup_interface (class_name);
3852 if (interface)
3853 return get_class_ivars (interface, true);
3855 error ("cannot find interface declaration for %qE",
3856 class_name);
3858 return error_mark_node;
3862 /* Functions used by the hashtable for field duplicates in
3863 objc_detect_field_duplicates(). Ideally, we'd use a standard
3864 key-value dictionary hashtable , and store as keys the field names,
3865 and as values the actual declarations (used to print nice error
3866 messages with the locations). But, the hashtable we are using only
3867 allows us to store keys in the hashtable, without values (it looks
3868 more like a set). So, we store the DECLs, but define equality as
3869 DECLs having the same name, and hash as the hash of the name. */
3871 struct decl_name_hash : typed_noop_remove <tree_node>
3873 typedef tree_node value_type;
3874 typedef tree_node compare_type;
3875 static inline hashval_t hash (const value_type *);
3876 static inline bool equal (const value_type *, const compare_type *);
3879 inline hashval_t
3880 decl_name_hash::hash (const value_type *q)
3882 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3885 inline bool
3886 decl_name_hash::equal (const value_type *a, const compare_type *b)
3888 return DECL_NAME (a) == DECL_NAME (b);
3891 /* Called when checking the variables in a struct. If we are not
3892 doing the ivars list inside an @interface context, then return
3893 false. Else, perform the check for duplicate ivars, then return
3894 true. The check for duplicates checks if an instance variable with
3895 the same name exists in the class or in a superclass. If
3896 'check_superclasses_only' is set to true, then it is assumed that
3897 checks for instance variables in the same class has already been
3898 performed (this is the case for ObjC++) and only the instance
3899 variables of superclasses are checked. */
3900 bool
3901 objc_detect_field_duplicates (bool check_superclasses_only)
3903 if (!objc_collecting_ivars || !objc_interface_context
3904 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3905 return false;
3907 /* We have two ways of doing this check:
3909 "direct comparison": we iterate over the instance variables and
3910 compare them directly. This works great for small numbers of
3911 instance variables (such as 10 or 20), which are extremely common.
3912 But it will potentially take forever for the pathological case with
3913 a huge number (eg, 10k) of instance variables.
3915 "hashtable": we use a hashtable, which requires a single sweep
3916 through the list of instances variables. This is much slower for a
3917 small number of variables, and we only use it for large numbers.
3919 To decide which one to use, we need to get an idea of how many
3920 instance variables we have to compare. */
3922 unsigned int number_of_ivars_to_check = 0;
3924 tree ivar;
3925 for (ivar = CLASS_RAW_IVARS (objc_interface_context);
3926 ivar; ivar = DECL_CHAIN (ivar))
3928 /* Ignore anonymous ivars. */
3929 if (DECL_NAME (ivar))
3930 number_of_ivars_to_check++;
3934 /* Exit if there is nothing to do. */
3935 if (number_of_ivars_to_check == 0)
3936 return true;
3938 /* In case that there are only 1 or 2 instance variables to check,
3939 we always use direct comparison. If there are more, it is
3940 worth iterating over the instance variables in the superclass
3941 to count how many there are (note that this has the same cost
3942 as checking 1 instance variable by direct comparison, which is
3943 why we skip this check in the case of 1 or 2 ivars and just do
3944 the direct comparison) and then decide if it worth using a
3945 hashtable. */
3946 if (number_of_ivars_to_check > 2)
3948 unsigned int number_of_superclass_ivars = 0;
3950 tree interface;
3951 for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context));
3952 interface; interface = lookup_interface (CLASS_SUPER_NAME (interface)))
3954 tree ivar;
3955 for (ivar = CLASS_RAW_IVARS (interface);
3956 ivar; ivar = DECL_CHAIN (ivar))
3957 number_of_superclass_ivars++;
3961 /* We use a hashtable if we have over 10k comparisons. */
3962 if (number_of_ivars_to_check * (number_of_superclass_ivars
3963 + (number_of_ivars_to_check / 2))
3964 > 10000)
3966 /* First, build the hashtable by putting all the instance
3967 variables of superclasses in it. */
3968 hash_table<decl_name_hash> htab (37);
3969 tree interface;
3970 for (interface = lookup_interface (CLASS_SUPER_NAME
3971 (objc_interface_context));
3972 interface; interface = lookup_interface
3973 (CLASS_SUPER_NAME (interface)))
3975 tree ivar;
3976 for (ivar = CLASS_RAW_IVARS (interface); ivar;
3977 ivar = DECL_CHAIN (ivar))
3979 if (DECL_NAME (ivar) != NULL_TREE)
3981 tree_node **slot = htab.find_slot (ivar, INSERT);
3982 /* Do not check for duplicate instance
3983 variables in superclasses. Errors have
3984 already been generated. */
3985 *slot = ivar;
3990 /* Now, we go through all the instance variables in the
3991 class, and check that they are not in the
3992 hashtable. */
3993 if (check_superclasses_only)
3995 tree ivar;
3996 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
3997 ivar = DECL_CHAIN (ivar))
3999 if (DECL_NAME (ivar) != NULL_TREE)
4001 tree duplicate_ivar = htab.find (ivar);
4002 if (duplicate_ivar != HTAB_EMPTY_ENTRY)
4004 error_at (DECL_SOURCE_LOCATION (ivar),
4005 "duplicate instance variable %q+D",
4006 ivar);
4007 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4008 "previous declaration of %q+D",
4009 duplicate_ivar);
4010 /* FIXME: Do we need the following ? */
4011 /* DECL_NAME (ivar) = NULL_TREE; */
4016 else
4018 /* If we're checking for duplicates in the class as
4019 well, we insert variables in the hashtable as we
4020 check them, so if a duplicate follows, it will be
4021 caught. */
4022 tree ivar;
4023 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
4024 ivar = DECL_CHAIN (ivar))
4026 if (DECL_NAME (ivar) != NULL_TREE)
4028 tree_node **slot = htab.find_slot (ivar, INSERT);
4029 if (*slot)
4031 tree duplicate_ivar = (tree)(*slot);
4032 error_at (DECL_SOURCE_LOCATION (ivar),
4033 "duplicate instance variable %q+D",
4034 ivar);
4035 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4036 "previous declaration of %q+D",
4037 duplicate_ivar);
4038 /* FIXME: Do we need the following ? */
4039 /* DECL_NAME (ivar) = NULL_TREE; */
4041 *slot = ivar;
4045 return true;
4050 /* This is the "direct comparison" approach, which is used in most
4051 non-pathological cases. */
4053 /* Walk up to class hierarchy, starting with this class (this is
4054 the external loop, because lookup_interface() is expensive, and
4055 we want to do it few times). */
4056 tree interface = objc_interface_context;
4058 if (check_superclasses_only)
4059 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4061 for ( ; interface; interface = lookup_interface
4062 (CLASS_SUPER_NAME (interface)))
4064 tree ivar_being_checked;
4066 for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context);
4067 ivar_being_checked;
4068 ivar_being_checked = DECL_CHAIN (ivar_being_checked))
4070 tree decl;
4072 /* Ignore anonymous ivars. */
4073 if (DECL_NAME (ivar_being_checked) == NULL_TREE)
4074 continue;
4076 /* Note how we stop when we find the ivar we are checking
4077 (this can only happen in the main class, not
4078 superclasses), to avoid comparing things twice
4079 (otherwise, for each ivar, you'd compare A to B then B
4080 to A, and get duplicated error messages). */
4081 for (decl = CLASS_RAW_IVARS (interface);
4082 decl && decl != ivar_being_checked;
4083 decl = DECL_CHAIN (decl))
4085 if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl))
4087 error_at (DECL_SOURCE_LOCATION (ivar_being_checked),
4088 "duplicate instance variable %q+D",
4089 ivar_being_checked);
4090 inform (DECL_SOURCE_LOCATION (decl),
4091 "previous declaration of %q+D",
4092 decl);
4093 /* FIXME: Do we need the following ? */
4094 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4100 return true;
4103 /* Used by: build_private_template, continue_class,
4104 and for @defs constructs. */
4106 static tree
4107 get_class_ivars (tree interface, bool inherited)
4109 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4111 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4112 by the current class (i.e., they do not include super-class ivars).
4113 However, the CLASS_IVARS list will be side-effected by a call to
4114 finish_struct(), which will fill in field offsets. */
4115 if (!CLASS_IVARS (interface))
4116 CLASS_IVARS (interface) = ivar_chain;
4118 if (!inherited)
4119 return ivar_chain;
4121 while (CLASS_SUPER_NAME (interface))
4123 /* Prepend super-class ivars. */
4124 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4125 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4126 ivar_chain);
4129 return ivar_chain;
4132 void
4133 objc_maybe_warn_exceptions (location_t loc)
4135 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4136 For example, on Darwin, ObjC exceptions require a sufficiently
4137 recent version of the runtime, so the user must ask for them
4138 explicitly. On other platforms, at the moment -fobjc-exceptions
4139 triggers -fexceptions which again is required for exceptions to
4140 work. */
4141 if (!flag_objc_exceptions)
4143 /* Warn only once per compilation unit. */
4144 static bool warned = false;
4146 if (!warned)
4148 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4149 warned = true;
4154 static struct objc_try_context *cur_try_context;
4156 /* Called just after parsing the @try and its associated BODY. We now
4157 must prepare for the tricky bits -- handling the catches and finally. */
4159 void
4160 objc_begin_try_stmt (location_t try_locus, tree body)
4162 struct objc_try_context *c = XCNEW (struct objc_try_context);
4163 c->outer = cur_try_context;
4164 c->try_body = body;
4165 c->try_locus = try_locus;
4166 c->end_try_locus = input_location;
4167 cur_try_context = c;
4169 /* Collect the list of local variables. We'll mark them as volatile
4170 at the end of compilation of this function to prevent them being
4171 clobbered by setjmp/longjmp. */
4172 if (flag_objc_sjlj_exceptions)
4173 objc_mark_locals_volatile (NULL);
4176 /* Called just after parsing "@catch (parm)". Open a binding level,
4177 enter DECL into the binding level, and initialize it. Leave the
4178 binding level open while the body of the compound statement is
4179 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4180 which we compile as "@catch(id tmp_variable)". */
4182 void
4183 objc_begin_catch_clause (tree decl)
4185 tree compound, type, t;
4186 bool ellipsis = false;
4188 /* Begin a new scope that the entire catch clause will live in. */
4189 compound = c_begin_compound_stmt (true);
4191 /* Create the appropriate declaration for the argument. */
4192 if (decl == error_mark_node)
4193 type = error_mark_node;
4194 else
4196 if (decl == NULL_TREE)
4198 /* If @catch(...) was specified, create a temporary variable of
4199 type 'id' and use it. */
4200 decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var");
4201 DECL_SOURCE_LOCATION (decl) = input_location;
4202 /* ... but allow the runtime to differentiate between ellipsis and the
4203 case of @catch (id xyz). */
4204 ellipsis = true;
4206 else
4208 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4209 decl = build_decl (input_location,
4210 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4212 lang_hooks.decls.pushdecl (decl);
4214 /* Mark the declaration as used so you never any warnings whether
4215 you use the exception argument or not. TODO: Implement a
4216 -Wunused-exception-parameter flag, which would cause warnings
4217 if exception parameter is not used. */
4218 TREE_USED (decl) = 1;
4219 DECL_READ_P (decl) = 1;
4221 type = TREE_TYPE (decl);
4224 /* Verify that the type of the catch is valid. It must be a pointer
4225 to an Objective-C class, or "id" (which is catch-all). */
4226 if (type == error_mark_node)
4228 ;/* Just keep going. */
4230 else if (!objc_type_valid_for_messaging (type, false))
4232 error ("@catch parameter is not a known Objective-C class type");
4233 type = error_mark_node;
4235 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type))
4236 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type)))
4238 error ("@catch parameter can not be protocol-qualified");
4239 type = error_mark_node;
4241 else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4242 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4243 identify 'id'. */
4245 else
4247 /* If 'type' was built using typedefs, we need to get rid of
4248 them and get a simple pointer to the class. */
4249 bool is_typedef = false;
4250 tree x = TYPE_MAIN_VARIANT (type);
4252 /* Skip from the pointer to the pointee. */
4253 if (TREE_CODE (x) == POINTER_TYPE)
4254 x = TREE_TYPE (x);
4256 /* Traverse typedef aliases */
4257 while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x)
4258 && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL
4259 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)))
4261 is_typedef = true;
4262 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4265 /* If it was a typedef, build a pointer to the final, original
4266 class. */
4267 if (is_typedef)
4268 type = build_pointer_type (x);
4270 if (cur_try_context->catch_list)
4272 /* Examine previous @catch clauses and see if we've already
4273 caught the type in question. */
4274 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4275 for (; !tsi_end_p (i); tsi_next (&i))
4277 tree stmt = tsi_stmt (i);
4278 t = CATCH_TYPES (stmt);
4279 if (t == error_mark_node)
4280 continue;
4281 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
4283 warning (0, "exception of type %<%T%> will be caught",
4284 TREE_TYPE (type));
4285 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4286 TREE_TYPE (t ? t : objc_object_type));
4287 break;
4293 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4294 add_stmt (t);
4297 /* Called just after parsing the closing brace of a @catch clause. Close
4298 the open binding level, and record a CATCH_EXPR for it. */
4300 void
4301 objc_finish_catch_clause (void)
4303 tree c = cur_try_context->current_catch;
4304 cur_try_context->current_catch = NULL;
4305 cur_try_context->end_catch_locus = input_location;
4307 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
4309 (*runtime.finish_catch) (&cur_try_context, c);
4312 /* Called after parsing a @finally clause and its associated BODY.
4313 Record the body for later placement. */
4315 void
4316 objc_build_finally_clause (location_t finally_locus, tree body)
4318 cur_try_context->finally_body = body;
4319 cur_try_context->finally_locus = finally_locus;
4320 cur_try_context->end_finally_locus = input_location;
4323 /* Called to finalize a @try construct. */
4325 tree
4326 objc_finish_try_stmt (void)
4328 struct objc_try_context *c = cur_try_context;
4329 tree stmt;
4331 if (c->catch_list == NULL && c->finally_body == NULL)
4332 error ("%<@try%> without %<@catch%> or %<@finally%>");
4334 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4335 add_stmt (stmt);
4337 cur_try_context = c->outer;
4338 free (c);
4339 return stmt;
4342 tree
4343 objc_build_throw_stmt (location_t loc, tree throw_expr)
4345 bool rethrown = false;
4347 objc_maybe_warn_exceptions (loc);
4349 /* Don't waste time trying to build something if we're already dead. */
4350 if (throw_expr == error_mark_node)
4351 return error_mark_node;
4353 if (throw_expr == NULL)
4355 /* If we're not inside a @catch block, there is no "current
4356 exception" to be rethrown. */
4357 if (cur_try_context == NULL
4358 || cur_try_context->current_catch == NULL)
4360 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4361 return error_mark_node;
4364 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4365 value that we get from the runtime. */
4366 throw_expr = (*runtime.build_exc_ptr) (&cur_try_context);
4367 rethrown = true;
4369 else
4371 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
4373 error_at (loc, "%<@throw%> argument is not an object");
4374 return error_mark_node;
4378 return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown);
4381 tree
4382 objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
4384 /* object_expr should never be NULL; but in case it is, convert it to
4385 error_mark_node. */
4386 if (object_expr == NULL)
4387 object_expr = error_mark_node;
4389 /* Validate object_expr. If not valid, set it to error_mark_node. */
4390 if (object_expr != error_mark_node)
4392 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true))
4394 error_at (start_locus, "%<@synchronized%> argument is not an object");
4395 object_expr = error_mark_node;
4399 if (object_expr == error_mark_node)
4401 /* If we found an error, we simply ignore the '@synchronized'.
4402 Compile the body so we can keep going with minimal
4403 casualties. */
4404 return add_stmt (body);
4406 else
4408 tree call;
4409 tree args;
4411 /* objc_sync_enter (object_expr); */
4412 object_expr = save_expr (object_expr);
4413 args = tree_cons (NULL, object_expr, NULL);
4414 call = build_function_call (input_location,
4415 objc_sync_enter_decl, args);
4416 SET_EXPR_LOCATION (call, start_locus);
4417 add_stmt (call);
4419 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4420 it goes inside the @finalize() clause. */
4421 args = tree_cons (NULL, object_expr, NULL);
4422 call = build_function_call (input_location,
4423 objc_sync_exit_decl, args);
4424 SET_EXPR_LOCATION (call, input_location);
4426 /* @try { body; } */
4427 objc_begin_try_stmt (start_locus, body);
4429 /* @finally { objc_sync_exit (object_expr); } */
4430 objc_build_finally_clause (input_location, call);
4432 /* End of try statement. */
4433 return objc_finish_try_stmt ();
4437 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4438 name as the class:
4440 struct <classname> {
4441 struct _objc_class *isa;
4443 }; */
4445 static void
4446 build_private_template (tree klass)
4448 if (!CLASS_STATIC_TEMPLATE (klass))
4450 tree record = objc_build_struct (klass,
4451 get_class_ivars (klass, false),
4452 CLASS_SUPER_NAME (klass));
4454 /* Set the TREE_USED bit for this struct, so that stab generator
4455 can emit stabs for this struct type. */
4456 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4457 TREE_USED (TYPE_STUB_DECL (record)) = 1;
4459 /* Copy the attributes from the class to the type. */
4460 if (TREE_DEPRECATED (klass))
4461 TREE_DEPRECATED (record) = 1;
4465 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4466 current class. */
4467 #ifdef OBJCPLUS
4468 static void
4469 objc_generate_cxx_ctor_or_dtor (bool dtor)
4471 tree fn, body, compound_stmt, ivar;
4473 /* - (id) .cxx_construct { ... return self; } */
4474 /* - (void) .cxx_construct { ... } */
4476 objc_start_method_definition
4477 (false /* is_class_method */,
4478 objc_build_method_signature (false /* is_class_method */,
4479 build_tree_list (NULL_TREE,
4480 dtor
4481 ? void_type_node
4482 : objc_object_type),
4483 get_identifier (dtor
4484 ? TAG_CXX_DESTRUCT
4485 : TAG_CXX_CONSTRUCT),
4486 make_node (TREE_LIST),
4487 false), NULL, NULL_TREE);
4488 body = begin_function_body ();
4489 compound_stmt = begin_compound_stmt (0);
4491 ivar = CLASS_IVARS (implementation_template);
4492 /* Destroy ivars in reverse order. */
4493 if (dtor)
4494 ivar = nreverse (copy_list (ivar));
4496 for (; ivar; ivar = TREE_CHAIN (ivar))
4498 if (TREE_CODE (ivar) == FIELD_DECL)
4500 tree type = TREE_TYPE (ivar);
4502 /* Call the ivar's default constructor or destructor. Do not
4503 call the destructor unless a corresponding constructor call
4504 has also been made (or is not needed). */
4505 if (MAYBE_CLASS_TYPE_P (type)
4506 && (dtor
4507 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4508 && (!TYPE_NEEDS_CONSTRUCTING (type)
4509 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4510 : (TYPE_NEEDS_CONSTRUCTING (type)
4511 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4512 finish_expr_stmt
4513 (build_special_member_call
4514 (build_ivar_reference (DECL_NAME (ivar)),
4515 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4516 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
4520 /* The constructor returns 'self'. */
4521 if (!dtor)
4522 finish_return_stmt (self_decl);
4524 finish_compound_stmt (compound_stmt);
4525 finish_function_body (body);
4526 fn = current_function_decl;
4527 finish_function ();
4528 objc_finish_method_definition (fn);
4531 /* The following routine will examine the current @interface for any
4532 non-POD C++ ivars requiring non-trivial construction and/or
4533 destruction, and then synthesize special '- .cxx_construct' and/or
4534 '- .cxx_destruct' methods which will run the appropriate
4535 construction or destruction code. Note that ivars inherited from
4536 super-classes are _not_ considered. */
4537 static void
4538 objc_generate_cxx_cdtors (void)
4540 bool need_ctor = false, need_dtor = false;
4541 tree ivar;
4543 /* Error case, due to possibly an extra @end. */
4544 if (!objc_implementation_context)
4545 return;
4547 /* We do not want to do this for categories, since they do not have
4548 their own ivars. */
4550 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4551 return;
4553 /* First, determine if we even need a constructor and/or destructor. */
4555 for (ivar = CLASS_IVARS (implementation_template); ivar;
4556 ivar = TREE_CHAIN (ivar))
4558 if (TREE_CODE (ivar) == FIELD_DECL)
4560 tree type = TREE_TYPE (ivar);
4562 if (MAYBE_CLASS_TYPE_P (type))
4564 if (TYPE_NEEDS_CONSTRUCTING (type)
4565 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4566 /* NB: If a default constructor is not available, we will not
4567 be able to initialize this ivar; the add_instance_variable()
4568 routine will already have warned about this. */
4569 need_ctor = true;
4571 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4572 && (!TYPE_NEEDS_CONSTRUCTING (type)
4573 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4574 /* NB: If a default constructor is not available, we will not
4575 call the destructor either, for symmetry. */
4576 need_dtor = true;
4581 /* Generate '- .cxx_construct' if needed. */
4583 if (need_ctor)
4584 objc_generate_cxx_ctor_or_dtor (false);
4586 /* Generate '- .cxx_destruct' if needed. */
4588 if (need_dtor)
4589 objc_generate_cxx_ctor_or_dtor (true);
4591 /* The 'imp_list' variable points at an imp_entry record for the current
4592 @implementation. Record the existence of '- .cxx_construct' and/or
4593 '- .cxx_destruct' methods therein; it will be included in the
4594 metadata for the class if the runtime needs it. */
4595 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
4597 #endif
4599 static void
4600 error_with_ivar (const char *message, tree decl)
4602 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4603 message, identifier_to_locale (gen_declaration (decl)));
4607 static void
4608 check_ivars (tree inter, tree imp)
4610 tree intdecls = CLASS_RAW_IVARS (inter);
4611 tree impdecls = CLASS_RAW_IVARS (imp);
4613 while (1)
4615 tree t1, t2;
4617 #ifdef OBJCPLUS
4618 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4619 intdecls = TREE_CHAIN (intdecls);
4620 #endif
4621 if (intdecls == 0 && impdecls == 0)
4622 break;
4623 if (intdecls == 0 || impdecls == 0)
4625 error ("inconsistent instance variable specification");
4626 break;
4629 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
4631 if (!comptypes (t1, t2)
4632 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4633 DECL_INITIAL (impdecls)))
4635 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4637 error_with_ivar ("conflicting instance variable type",
4638 impdecls);
4639 error_with_ivar ("previous declaration of",
4640 intdecls);
4642 else /* both the type and the name don't match */
4644 error ("inconsistent instance variable specification");
4645 break;
4649 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4651 error_with_ivar ("conflicting instance variable name",
4652 impdecls);
4653 error_with_ivar ("previous declaration of",
4654 intdecls);
4657 intdecls = DECL_CHAIN (intdecls);
4658 impdecls = DECL_CHAIN (impdecls);
4663 static void
4664 mark_referenced_methods (void)
4666 struct imp_entry *impent;
4667 tree chain;
4669 for (impent = imp_list; impent; impent = impent->next)
4671 chain = CLASS_CLS_METHODS (impent->imp_context);
4672 while (chain)
4674 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4675 chain = DECL_CHAIN (chain);
4678 chain = CLASS_NST_METHODS (impent->imp_context);
4679 while (chain)
4681 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
4682 chain = DECL_CHAIN (chain);
4687 /* If type is empty or only type qualifiers are present, add default
4688 type of id (otherwise grokdeclarator will default to int). */
4689 static inline tree
4690 adjust_type_for_id_default (tree type)
4692 if (!type)
4693 type = make_node (TREE_LIST);
4695 if (!TREE_VALUE (type))
4696 TREE_VALUE (type) = objc_object_type;
4697 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4698 && TYPED_OBJECT (TREE_VALUE (type)))
4699 error ("can not use an object as parameter to a method");
4701 return type;
4704 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4705 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4706 OBJC_METHOD_PARM_DECL ?)
4708 A KEYWORD_DECL is a tree representing the declaration of a
4709 parameter of an Objective-C method. It is produced when parsing a
4710 fragment of Objective-C method declaration of the form
4712 keyworddecl:
4713 selector ':' '(' typename ')' identifier
4715 For example, take the Objective-C method
4717 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4719 the two fragments "pathForResource:(NSString *)resource" and
4720 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4721 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4722 "pathForResource"), the 'arg_type' (eg, tree representing a
4723 NSString *), the 'arg_name' (eg identifier for "resource") and
4724 potentially some attributes (for example, a tree representing
4725 __attribute__ ((unused)) if such an attribute was attached to a
4726 certain parameter). You can access this information using the
4727 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4728 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4730 'key_name' is an identifier node (and is optional as you can omit
4731 it in Objective-C methods).
4732 'arg_type' is a tree list (and is optional too if no parameter type
4733 was specified).
4734 'arg_name' is an identifier node and is required.
4735 'attributes' is an optional tree containing parameter attributes. */
4736 tree
4737 objc_build_keyword_decl (tree key_name, tree arg_type,
4738 tree arg_name, tree attributes)
4740 tree keyword_decl;
4742 if (flag_objc1_only && attributes)
4743 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
4745 /* If no type is specified, default to "id". */
4746 arg_type = adjust_type_for_id_default (arg_type);
4748 keyword_decl = make_node (KEYWORD_DECL);
4750 TREE_TYPE (keyword_decl) = arg_type;
4751 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4752 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4753 DECL_ATTRIBUTES (keyword_decl) = attributes;
4755 return keyword_decl;
4758 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4759 static tree
4760 build_keyword_selector (tree selector)
4762 int len = 0;
4763 tree key_chain, key_name;
4764 char *buf;
4766 /* Scan the selector to see how much space we'll need. */
4767 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4769 switch (TREE_CODE (selector))
4771 case KEYWORD_DECL:
4772 key_name = KEYWORD_KEY_NAME (key_chain);
4773 break;
4774 case TREE_LIST:
4775 key_name = TREE_PURPOSE (key_chain);
4776 break;
4777 default:
4778 gcc_unreachable ();
4781 if (key_name)
4782 len += IDENTIFIER_LENGTH (key_name) + 1;
4783 else
4784 /* Just a ':' arg. */
4785 len++;
4788 buf = (char *) alloca (len + 1);
4789 /* Start the buffer out as an empty string. */
4790 buf[0] = '\0';
4792 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
4794 switch (TREE_CODE (selector))
4796 case KEYWORD_DECL:
4797 key_name = KEYWORD_KEY_NAME (key_chain);
4798 break;
4799 case TREE_LIST:
4800 key_name = TREE_PURPOSE (key_chain);
4801 /* The keyword decl chain will later be used as a function
4802 argument chain. Unhook the selector itself so as to not
4803 confuse other parts of the compiler. */
4804 TREE_PURPOSE (key_chain) = NULL_TREE;
4805 break;
4806 default:
4807 gcc_unreachable ();
4810 if (key_name)
4811 strcat (buf, IDENTIFIER_POINTER (key_name));
4812 strcat (buf, ":");
4815 return get_identifier_with_length (buf, len);
4818 /* Used for declarations and definitions. */
4820 static tree
4821 build_method_decl (enum tree_code code, tree ret_type, tree selector,
4822 tree add_args, bool ellipsis)
4824 tree method_decl;
4826 /* If no type is specified, default to "id". */
4827 ret_type = adjust_type_for_id_default (ret_type);
4829 /* Note how a method_decl has a TREE_TYPE which is not the function
4830 type of the function implementing the method, but only the return
4831 type of the method. We may want to change this, and store the
4832 entire function type in there (eg, it may be used to simplify
4833 dealing with attributes below). */
4834 method_decl = make_node (code);
4835 TREE_TYPE (method_decl) = ret_type;
4837 /* If we have a keyword selector, create an identifier_node that
4838 represents the full selector name (`:' included)... */
4839 if (TREE_CODE (selector) == KEYWORD_DECL)
4841 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4842 METHOD_SEL_ARGS (method_decl) = selector;
4843 METHOD_ADD_ARGS (method_decl) = add_args;
4844 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4846 else
4848 METHOD_SEL_NAME (method_decl) = selector;
4849 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4850 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4853 return method_decl;
4856 /* This routine processes objective-c method attributes. */
4858 static void
4859 objc_decl_method_attributes (tree *node, tree attributes, int flags)
4861 /* TODO: Replace the hackery below. An idea would be to store the
4862 full function type in the method declaration (for example in
4863 TREE_TYPE) and then expose ObjC method declarations to c-family
4864 and they could deal with them by simply treating them as
4865 functions. */
4867 /* Because of the dangers in the hackery below, we filter out any
4868 attribute that we do not know about. For the ones we know about,
4869 we know that they work with the hackery. For the other ones,
4870 there is no guarantee, so we have to filter them out. */
4871 tree filtered_attributes = NULL_TREE;
4873 if (attributes)
4875 tree attribute;
4876 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
4878 tree name = TREE_PURPOSE (attribute);
4880 if (is_attribute_p ("deprecated", name)
4881 || is_attribute_p ("sentinel", name)
4882 || is_attribute_p ("noreturn", name))
4884 /* An attribute that we support; add it to the filtered
4885 attributes. */
4886 filtered_attributes = chainon (filtered_attributes,
4887 copy_node (attribute));
4889 else if (is_attribute_p ("format", name))
4891 /* "format" is special because before adding it to the
4892 filtered attributes we need to adjust the specified
4893 format by adding the hidden function parameters for
4894 an Objective-C method (self, _cmd). */
4895 tree new_attribute = copy_node (attribute);
4897 /* Check the arguments specified with the attribute, and
4898 modify them adding 2 for the two hidden arguments.
4899 Note how this differs from C++; according to the
4900 specs, C++ does not do it so you have to add the +1
4901 yourself. For Objective-C, instead, the compiler
4902 adds the +2 for you. */
4904 /* The attribute arguments have not been checked yet, so
4905 we need to be careful as they could be missing or
4906 invalid. If anything looks wrong, we skip the
4907 process and the compiler will complain about it later
4908 when it validates the attribute. */
4909 /* Check that we have at least three arguments. */
4910 if (TREE_VALUE (new_attribute)
4911 && TREE_CHAIN (TREE_VALUE (new_attribute))
4912 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4914 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4915 tree third_argument = TREE_CHAIN (second_argument);
4916 tree number;
4918 /* This is the second argument, the "string-index",
4919 which specifies the index of the format string
4920 argument. Add 2. */
4921 number = TREE_VALUE (second_argument);
4922 if (number
4923 && TREE_CODE (number) == INTEGER_CST
4924 && !wi::eq_p (number, 0))
4925 TREE_VALUE (second_argument)
4926 = wide_int_to_tree (TREE_TYPE (number),
4927 wi::add (number, 2));
4929 /* This is the third argument, the "first-to-check",
4930 which specifies the index of the first argument to
4931 check. This could be 0, meaning it is not available,
4932 in which case we don't need to add 2. Add 2 if not
4933 0. */
4934 number = TREE_VALUE (third_argument);
4935 if (number
4936 && TREE_CODE (number) == INTEGER_CST
4937 && !wi::eq_p (number, 0))
4938 TREE_VALUE (third_argument)
4939 = wide_int_to_tree (TREE_TYPE (number),
4940 wi::add (number, 2));
4942 filtered_attributes = chainon (filtered_attributes,
4943 new_attribute);
4945 else if (is_attribute_p ("nonnull", name))
4947 /* We need to fixup all the argument indexes by adding 2
4948 for the two hidden arguments of an Objective-C method
4949 invocation, similat to what we do above for the
4950 "format" attribute. */
4951 /* FIXME: This works great in terms of implementing the
4952 functionality, but the warnings that are produced by
4953 nonnull do mention the argument index (while the
4954 format ones don't). For example, you could get
4955 "warning: null argument where non-null required
4956 (argument 3)". Now in that message, "argument 3"
4957 includes the 2 hidden arguments; it would be much
4958 more friendly to call it "argument 1", as that would
4959 be consistent with __attribute__ ((nonnnull (1))).
4960 To do this, we'd need to have the C family code that
4961 checks the arguments know about adding/removing 2 to
4962 the argument index ... or alternatively we could
4963 maybe store the "printable" argument index in
4964 addition to the actual argument index ? Some
4965 refactoring is needed to do this elegantly. */
4966 tree new_attribute = copy_node (attribute);
4967 tree argument = TREE_VALUE (attribute);
4968 while (argument != NULL_TREE)
4970 /* Get the value of the argument and add 2. */
4971 tree number = TREE_VALUE (argument);
4972 if (number && TREE_CODE (number) == INTEGER_CST
4973 && !wi::eq_p (number, 0))
4974 TREE_VALUE (argument)
4975 = wide_int_to_tree (TREE_TYPE (number),
4976 wi::add (number, 2));
4977 argument = TREE_CHAIN (argument);
4980 filtered_attributes = chainon (filtered_attributes,
4981 new_attribute);
4983 else
4984 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
4988 if (filtered_attributes)
4990 /* This hackery changes the TREE_TYPE of the ObjC method
4991 declaration to be a function type, so that decl_attributes
4992 will treat the ObjC method as if it was a function. Some
4993 attributes (sentinel, format) will be applied to the function
4994 type, changing it in place; so after calling decl_attributes,
4995 we extract the function type attributes and store them in
4996 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4997 deprecated) are applied directly to the method declaration
4998 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4999 is nothing to do. */
5000 tree saved_type = TREE_TYPE (*node);
5001 TREE_TYPE (*node)
5002 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
5003 METHOD_REF, 0);
5004 decl_attributes (node, filtered_attributes, flags);
5005 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
5006 TREE_TYPE (*node) = saved_type;
5010 bool
5011 objc_method_decl (enum tree_code opcode)
5013 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
5016 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
5017 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
5018 method or calling one. SUPER_FLAG indicates whether this is a send
5019 to super; this makes a difference for the NeXT calling sequence in
5020 which the lookup and the method call are done together. If METHOD is
5021 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5022 represented as varargs. */
5024 tree
5025 build_function_type_for_method (tree return_type, tree method,
5026 int context, bool super_flag)
5028 vec<tree, va_gc> *argtypes = make_tree_vector ();
5029 tree t, ftype;
5030 bool is_varargs = false;
5032 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
5034 /* No actual method prototype given; remaining args passed as varargs. */
5035 if (method == NULL_TREE)
5037 is_varargs = true;
5038 goto build_ftype;
5041 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
5043 tree arg_type = TREE_VALUE (TREE_TYPE (t));
5045 /* Decay argument types for the underlying C function as
5046 appropriate. */
5047 arg_type = objc_decay_parm_type (arg_type);
5049 vec_safe_push (argtypes, arg_type);
5052 if (METHOD_ADD_ARGS (method))
5054 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5055 t; t = TREE_CHAIN (t))
5057 tree arg_type = TREE_TYPE (TREE_VALUE (t));
5059 arg_type = objc_decay_parm_type (arg_type);
5061 vec_safe_push (argtypes, arg_type);
5064 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5065 is_varargs = true;
5068 build_ftype:
5069 if (is_varargs)
5070 ftype = build_varargs_function_type_vec (return_type, argtypes);
5071 else
5072 ftype = build_function_type_vec (return_type, argtypes);
5074 release_tree_vector (argtypes);
5075 return ftype;
5078 /* The 'method' argument is a tree; this tree could either be a single
5079 method, which is returned, or could be a TREE_VEC containing a list
5080 of methods. In that case, the first one is returned, and warnings
5081 are issued as appropriate. */
5082 static tree
5083 check_duplicates (tree method, int methods, int is_class)
5085 tree first_method;
5086 size_t i;
5088 if (method == NULL_TREE)
5089 return NULL_TREE;
5091 if (TREE_CODE (method) != TREE_VEC)
5092 return method;
5094 /* We have two or more methods with the same name but different
5095 types. */
5096 first_method = TREE_VEC_ELT (method, 0);
5098 /* But just how different are those types? If
5099 -Wno-strict-selector-match is specified, we shall not complain if
5100 the differences are solely among types with identical size and
5101 alignment. */
5102 if (!warn_strict_selector_match)
5104 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5105 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5106 goto issue_warning;
5108 return first_method;
5111 issue_warning:
5112 if (methods)
5114 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5116 warning_at (input_location, 0,
5117 "multiple methods named %<%c%E%> found",
5118 (is_class ? '+' : '-'),
5119 METHOD_SEL_NAME (first_method));
5120 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5121 (type ? '-' : '+'),
5122 identifier_to_locale (gen_method_decl (first_method)));
5124 else
5126 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5128 warning_at (input_location, 0,
5129 "multiple selectors named %<%c%E%> found",
5130 (is_class ? '+' : '-'),
5131 METHOD_SEL_NAME (first_method));
5132 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5133 (type ? '-' : '+'),
5134 identifier_to_locale (gen_method_decl (first_method)));
5137 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
5139 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5141 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5142 (type ? '-' : '+'),
5143 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5146 return first_method;
5149 /* If RECEIVER is a class reference, return the identifier node for
5150 the referenced class. RECEIVER is created by objc_get_class_reference,
5151 so we check the exact form created depending on which runtimes are
5152 used. */
5154 static tree
5155 receiver_is_class_object (tree receiver, int self, int super)
5157 tree exp, arg;
5159 /* The receiver is 'self' or 'super' in the context of a class method. */
5160 if (objc_method_context
5161 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5162 && (self || super))
5163 return (super
5164 ? CLASS_SUPER_NAME (implementation_template)
5165 : CLASS_NAME (implementation_template));
5167 /* The runtime might encapsulate things its own way. */
5168 exp = (*runtime.receiver_is_class_object) (receiver);
5169 if (exp)
5170 return exp;
5172 /* The receiver is a function call that returns an id. Check if
5173 it is a call to objc_getClass, if so, pick up the class name.
5175 This is required by the GNU runtime, which compiles
5177 [NSObject alloc]
5179 into
5181 [objc_get_class ("NSObject") alloc];
5183 and then, to check that the receiver responds to the +alloc
5184 method, needs to be able to determine that the objc_get_class()
5185 call returns the NSObject class and not just a generic Class
5186 pointer.
5188 But, traditionally this is enabled for all runtimes, not just the
5189 GNU one, which means that the compiler is smarter than you'd
5190 expect when dealing with objc_getClass(). For example, with the
5191 Apple runtime, in the code
5193 [objc_getClass ("NSObject") alloc];
5195 the compiler will recognize the objc_getClass() call as special
5196 (due to the code below) and so will know that +alloc is called on
5197 the 'NSObject' class, and can perform the corresponding checks.
5199 Programmers can disable this behaviour by casting the results of
5200 objc_getClass() to 'Class' (this may seem weird because
5201 objc_getClass() is already declared to return 'Class', but the
5202 compiler treats it as a special function). This may be useful if
5203 the class is never declared, and the compiler would complain
5204 about a missing @interface for it. Then, you can do
5206 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5208 to silence the warnings. */
5209 if (TREE_CODE (receiver) == CALL_EXPR
5210 && (exp = CALL_EXPR_FN (receiver))
5211 && TREE_CODE (exp) == ADDR_EXPR
5212 && (exp = TREE_OPERAND (exp, 0))
5213 && TREE_CODE (exp) == FUNCTION_DECL
5214 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5215 prototypes for objc_get_class(). Thankfully, they seem to share the
5216 same function type. */
5217 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5218 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5219 /* We have a call to objc_get_class/objc_getClass! */
5220 && (arg = CALL_EXPR_ARG (receiver, 0)))
5222 STRIP_NOPS (arg);
5223 if (TREE_CODE (arg) == ADDR_EXPR
5224 && (arg = TREE_OPERAND (arg, 0))
5225 && TREE_CODE (arg) == STRING_CST)
5226 /* Finally, we have the class name. */
5227 return get_identifier (TREE_STRING_POINTER (arg));
5229 return 0;
5232 /* If we are currently building a message expr, this holds
5233 the identifier of the selector of the message. This is
5234 used when printing warnings about argument mismatches. */
5236 static tree current_objc_message_selector = 0;
5238 tree
5239 objc_message_selector (void)
5241 return current_objc_message_selector;
5244 /* Construct an expression for sending a message.
5245 MESS has the object to send to in TREE_PURPOSE
5246 and the argument list (including selector) in TREE_VALUE.
5248 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5249 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5251 tree
5252 objc_build_message_expr (tree receiver, tree message_args)
5254 tree sel_name;
5255 #ifdef OBJCPLUS
5256 tree args = TREE_PURPOSE (message_args);
5257 #else
5258 tree args = message_args;
5259 #endif
5260 tree method_params = NULL_TREE;
5262 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5263 return error_mark_node;
5265 /* Obtain the full selector name. */
5266 switch (TREE_CODE (args))
5268 case IDENTIFIER_NODE:
5269 /* A unary selector. */
5270 sel_name = args;
5271 break;
5272 case TREE_LIST:
5273 sel_name = build_keyword_selector (args);
5274 break;
5275 default:
5276 gcc_unreachable ();
5279 /* Build the parameter list to give to the method. */
5280 if (TREE_CODE (args) == TREE_LIST)
5281 #ifdef OBJCPLUS
5282 method_params = chainon (args, TREE_VALUE (message_args));
5283 #else
5285 tree chain = args, prev = NULL_TREE;
5287 /* We have a keyword selector--check for comma expressions. */
5288 while (chain)
5290 tree element = TREE_VALUE (chain);
5292 /* We have a comma expression, must collapse... */
5293 if (TREE_CODE (element) == TREE_LIST)
5295 if (prev)
5296 TREE_CHAIN (prev) = element;
5297 else
5298 args = element;
5300 prev = chain;
5301 chain = TREE_CHAIN (chain);
5303 method_params = args;
5305 #endif
5307 #ifdef OBJCPLUS
5308 if (processing_template_decl)
5309 /* Must wait until template instantiation time. */
5310 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5311 sel_name, method_params);
5312 #endif
5314 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
5317 /* Look up method SEL_NAME that would be suitable for receiver
5318 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5319 nonzero), and report on any duplicates. */
5321 static tree
5322 lookup_method_in_hash_lists (tree sel_name, int is_class)
5324 tree method_prototype = OBJC_MAP_NOT_FOUND;
5326 if (!is_class)
5327 method_prototype = objc_map_get (instance_method_map, sel_name);
5329 if (method_prototype == OBJC_MAP_NOT_FOUND)
5331 method_prototype = objc_map_get (class_method_map, sel_name);
5332 is_class = 1;
5334 if (method_prototype == OBJC_MAP_NOT_FOUND)
5335 return NULL_TREE;
5338 return check_duplicates (method_prototype, 1, is_class);
5341 /* The 'objc_finish_message_expr' routine is called from within
5342 'objc_build_message_expr' for non-template functions. In the case of
5343 C++ template functions, it is called from 'build_expr_from_tree'
5344 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5346 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5347 if the method being used is deprecated. If it is not NULL, instead
5348 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5349 prototype that was used and is deprecated. This is useful for
5350 getter calls that are always generated when compiling dot-syntax
5351 expressions, even if they may not be used. In that case, we don't
5352 want the warning immediately; we produce it (if needed) at gimplify
5353 stage when we are sure that the deprecated getter is being
5354 used. */
5355 tree
5356 objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5357 tree *deprecated_method_prototype)
5359 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5360 tree retval, class_tree;
5361 int self, super, have_cast;
5363 /* We have used the receiver, so mark it as read. */
5364 mark_exp_read (receiver);
5366 /* Extract the receiver of the message, as well as its type
5367 (where the latter may take the form of a cast or be inferred
5368 from the implementation context). */
5369 rtype = receiver;
5370 while (TREE_CODE (rtype) == COMPOUND_EXPR
5371 || TREE_CODE (rtype) == MODIFY_EXPR
5372 || CONVERT_EXPR_P (rtype)
5373 || TREE_CODE (rtype) == COMPONENT_REF)
5374 rtype = TREE_OPERAND (rtype, 0);
5376 /* self is 1 if this is a message to self, 0 otherwise */
5377 self = (rtype == self_decl);
5379 /* super is 1 if this is a message to super, 0 otherwise. */
5380 super = (rtype == UOBJC_SUPER_decl);
5382 /* rtype is the type of the receiver. */
5383 rtype = TREE_TYPE (receiver);
5385 /* have_cast is 1 if the receiver is casted. */
5386 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5387 || (TREE_CODE (receiver) == COMPOUND_EXPR
5388 && !IS_SUPER (rtype)));
5390 /* If we are calling [super dealloc], reset our warning flag. */
5391 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5392 should_call_super_dealloc = 0;
5394 /* If the receiver is a class object, retrieve the corresponding
5395 @interface, if one exists. class_tree is the class name
5396 identifier, or NULL_TREE if this is not a class method or the
5397 class name could not be determined (as in the case "Class c; [c
5398 method];"). */
5399 class_tree = receiver_is_class_object (receiver, self, super);
5401 /* Now determine the receiver type (if an explicit cast has not been
5402 provided). */
5403 if (!have_cast)
5405 if (class_tree)
5407 /* We are here when we have no cast, and we have a class
5408 name. So, this is a plain method to a class object, as
5409 in [NSObject alloc]. Find the interface corresponding to
5410 the class name. */
5411 rtype = lookup_interface (class_tree);
5413 if (rtype == NULL_TREE)
5415 /* If 'rtype' is NULL_TREE at this point it means that
5416 we have seen no @interface corresponding to that
5417 class name, only a @class declaration (alternatively,
5418 this was a call such as [objc_getClass("SomeClass")
5419 alloc], where we've never seen the @interface of
5420 SomeClass). So, we have a class name (class_tree)
5421 but no actual details of the class methods. We won't
5422 be able to check that the class responds to the
5423 method, and we will have to guess the method
5424 prototype. Emit a warning, then keep going (this
5425 will use any method with a matching name, as if the
5426 receiver was of type 'Class'). */
5427 warning (0, "@interface of class %qE not found", class_tree);
5430 /* Handle `self' and `super'. */
5431 else if (super)
5433 if (!CLASS_SUPER_NAME (implementation_template))
5435 error ("no super class declared in @interface for %qE",
5436 CLASS_NAME (implementation_template));
5437 return error_mark_node;
5439 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
5441 else if (self)
5442 rtype = lookup_interface (CLASS_NAME (implementation_template));
5445 if (objc_is_id (rtype))
5447 /* The receiver is of type 'id' or 'Class' (with or without some
5448 protocols attached to it). */
5450 /* We set class_tree to the identifier for 'Class' if this is a
5451 class method, and to NULL_TREE if not. */
5452 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
5454 /* 'rprotos' is the list of protocols that the receiver
5455 supports. */
5456 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5457 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5458 : NULL_TREE);
5460 /* We have no information on the type, and we set it to
5461 NULL_TREE. */
5462 rtype = NULL_TREE;
5464 /* If there are any protocols, check that the method we are
5465 calling appears in the protocol list. If there are no
5466 protocols, this is a message to 'id' or 'Class' and we accept
5467 any method that exists. */
5468 if (rprotos)
5470 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5471 search in protocols themselves for the method
5472 prototype. */
5473 method_prototype
5474 = lookup_method_in_protocol_list (rprotos, sel_name,
5475 class_tree != NULL_TREE);
5477 /* If messaging 'Class <Proto>' but did not find a class
5478 method prototype, search for an instance method instead,
5479 and warn about having done so. */
5480 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5482 method_prototype
5483 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5485 if (method_prototype)
5486 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5487 sel_name, sel_name);
5491 else if (rtype)
5493 /* We have a receiver type which is more specific than 'id' or
5494 'Class'. */
5495 tree orig_rtype = rtype;
5497 if (TREE_CODE (rtype) == POINTER_TYPE)
5498 rtype = TREE_TYPE (rtype);
5499 /* Traverse typedef aliases */
5500 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5501 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5502 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5503 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5504 if (TYPED_OBJECT (rtype))
5506 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5507 rtype = TYPE_OBJC_INTERFACE (rtype);
5509 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5511 /* If we could not find an @interface declaration, we must
5512 have only seen a @class declaration; so, we cannot say
5513 anything more intelligent about which methods the
5514 receiver will understand. Note that this only happens
5515 for instance methods; for class methods to a class where
5516 we have only seen a @class declaration,
5517 lookup_interface() above would have set rtype to
5518 NULL_TREE. */
5519 if (rprotos)
5521 /* We could not find an @interface declaration, yet, if
5522 there are protocols attached to the type, we can
5523 still look up the method in the protocols. Ie, we
5524 are in the following case:
5526 @class MyClass;
5527 MyClass<MyProtocol> *x;
5528 [x method];
5530 If 'MyProtocol' has the method 'method', we can check
5531 and retrieve the method prototype. */
5532 method_prototype
5533 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5535 /* At this point, if we have found the method_prototype,
5536 we are quite happy. The details of the class are
5537 irrelevant. If we haven't found it, a warning will
5538 have been produced that the method could not be found
5539 in the protocol, and we won't produce further
5540 warnings (please note that this means that "@class
5541 MyClass; MyClass <MyProtocol> *x;" is exactly
5542 equivalent to "id <MyProtocol> x", which isn't too
5543 satisfactory but it's not easy to see how to do
5544 better). */
5546 else
5548 if (rtype)
5550 /* We could not find an @interface declaration, and
5551 there are no protocols attached to the receiver,
5552 so we can't complete the check that the receiver
5553 responds to the method, and we can't retrieve the
5554 method prototype. But, because the receiver has
5555 a well-specified class, the programmer did want
5556 this check to be performed. Emit a warning, then
5557 keep going as if it was an 'id'. To remove the
5558 warning, either include an @interface for the
5559 class, or cast the receiver to 'id'. Note that
5560 rtype is an IDENTIFIER_NODE at this point. */
5561 warning (0, "@interface of class %qE not found", rtype);
5565 rtype = NULL_TREE;
5567 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5568 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5570 /* We have a valid ObjC class name with an associated
5571 @interface. Look up the method name in the published
5572 @interface for the class (and its superclasses). */
5573 method_prototype
5574 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
5576 /* If the method was not found in the @interface, it may still
5577 exist locally as part of the @implementation. */
5578 if (!method_prototype && objc_implementation_context
5579 && CLASS_NAME (objc_implementation_context)
5580 == OBJC_TYPE_NAME (rtype))
5581 method_prototype
5582 = lookup_method
5583 ((class_tree
5584 ? CLASS_CLS_METHODS (objc_implementation_context)
5585 : CLASS_NST_METHODS (objc_implementation_context)),
5586 sel_name);
5588 /* If we haven't found a candidate method by now, try looking for
5589 it in the protocol list. */
5590 if (!method_prototype && rprotos)
5591 method_prototype
5592 = lookup_method_in_protocol_list (rprotos, sel_name,
5593 class_tree != NULL_TREE);
5595 else
5597 /* We have a type, but it's not an Objective-C type (!). */
5598 warning (0, "invalid receiver type %qs",
5599 identifier_to_locale (gen_type_name (orig_rtype)));
5600 /* After issuing the "invalid receiver" warning, perform method
5601 lookup as if we were messaging 'id'. */
5602 rtype = rprotos = NULL_TREE;
5605 /* Note that rtype could also be NULL_TREE. This happens if we are
5606 messaging a class by name, but the class was only
5607 forward-declared using @class. */
5609 /* For 'id' or 'Class' receivers, search in the global hash table as
5610 a last resort. For all receivers, warn if protocol searches have
5611 failed. */
5612 if (!method_prototype)
5614 if (rprotos)
5615 warning (0, "%<%c%E%> not found in protocol(s)",
5616 (class_tree ? '+' : '-'),
5617 sel_name);
5619 if (!rtype)
5620 method_prototype
5621 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5624 if (!method_prototype)
5626 static bool warn_missing_methods = false;
5628 if (rtype)
5629 warning (0, "%qE may not respond to %<%c%E%>",
5630 OBJC_TYPE_NAME (rtype),
5631 (class_tree ? '+' : '-'),
5632 sel_name);
5633 /* If we are messaging an 'id' or 'Class' object and made it here,
5634 then we have failed to find _any_ instance or class method,
5635 respectively. */
5636 else
5637 warning (0, "no %<%c%E%> method found",
5638 (class_tree ? '+' : '-'),
5639 sel_name);
5641 if (!warn_missing_methods)
5643 warning_at (input_location,
5644 0, "(Messages without a matching method signature");
5645 warning_at (input_location,
5646 0, "will be assumed to return %<id%> and accept");
5647 warning_at (input_location,
5648 0, "%<...%> as arguments.)");
5649 warn_missing_methods = true;
5652 else
5654 /* Warn if the method is deprecated, but not if the receiver is
5655 a generic 'id'. 'id' is used to cast an object to a generic
5656 object of an unspecified class; in that case, we'll use
5657 whatever method prototype we can find to get the method
5658 argument and return types, but it is not appropriate to
5659 produce deprecation warnings since we don't know the class
5660 that the object will be of at runtime. The @interface(s) for
5661 that class may not even be available to the compiler right
5662 now, and it is perfectly possible that the method is marked
5663 as non-deprecated in such @interface(s).
5665 In practice this makes sense since casting an object to 'id'
5666 is often used precisely to turn off warnings associated with
5667 the object being of a particular class. */
5668 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5670 if (deprecated_method_prototype)
5671 *deprecated_method_prototype = method_prototype;
5672 else
5673 warn_deprecated_use (method_prototype, NULL_TREE);
5677 /* Save the selector name for printing error messages. */
5678 current_objc_message_selector = sel_name;
5680 /* Build the method call.
5681 TODO: Get the location from somewhere that will work for delayed
5682 expansion. */
5684 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5685 receiver, rtype, sel_name,
5686 method_params, super);
5688 current_objc_message_selector = 0;
5690 return retval;
5694 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5695 expression. This variable will be initialized to global protocol_t meta-data
5696 pointer. */
5698 /* This function is called by the parser when (and only when) a
5699 @protocol() expression is found, in order to compile it. */
5700 tree
5701 objc_build_protocol_expr (tree protoname)
5703 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5704 /* definition_required */ false);
5706 if (!p)
5708 error ("cannot find protocol declaration for %qE", protoname);
5709 return error_mark_node;
5712 return (*runtime.get_protocol_reference) (input_location, p);
5715 /* This function is called by the parser when a @selector() expression
5716 is found, in order to compile it. It is only called by the parser
5717 and only to compile a @selector(). LOC is the location of the
5718 @selector. */
5719 tree
5720 objc_build_selector_expr (location_t loc, tree selnamelist)
5722 tree selname;
5724 /* Obtain the full selector name. */
5725 switch (TREE_CODE (selnamelist))
5727 case IDENTIFIER_NODE:
5728 /* A unary selector. */
5729 selname = selnamelist;
5730 break;
5731 case TREE_LIST:
5732 selname = build_keyword_selector (selnamelist);
5733 break;
5734 default:
5735 gcc_unreachable ();
5738 /* If we are required to check @selector() expressions as they
5739 are found, check that the selector has been declared. */
5740 if (warn_undeclared_selector)
5742 /* Look the selector up in the list of all known class and
5743 instance methods (up to this line) to check that the selector
5744 exists. */
5745 tree method;
5747 /* First try with instance methods. */
5748 method = objc_map_get (instance_method_map, selname);
5750 /* If not found, try with class methods. */
5751 if (method == OBJC_MAP_NOT_FOUND)
5753 method = objc_map_get (class_method_map, selname);
5755 /* If still not found, print out a warning. */
5756 if (method == OBJC_MAP_NOT_FOUND)
5757 warning (0, "undeclared selector %qE", selname);
5761 /* The runtimes do this differently, most particularly, GNU has typed
5762 selectors, whilst NeXT does not. */
5763 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
5766 static tree
5767 build_ivar_reference (tree id)
5769 tree base;
5770 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5772 /* Historically, a class method that produced objects (factory
5773 method) would assign `self' to the instance that it
5774 allocated. This would effectively turn the class method into
5775 an instance method. Following this assignment, the instance
5776 variables could be accessed. That practice, while safe,
5777 violates the simple rule that a class method should not refer
5778 to an instance variable. It's better to catch the cases
5779 where this is done unknowingly than to support the above
5780 paradigm. */
5781 warning (0, "instance variable %qE accessed in class method",
5782 id);
5783 self_decl = convert (objc_instance_type, self_decl); /* cast */
5786 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5787 return (*runtime.build_ivar_reference) (input_location, base, id);
5790 static void
5791 hash_init (void)
5793 instance_method_map = objc_map_alloc_ggc (1000);
5794 class_method_map = objc_map_alloc_ggc (1000);
5796 class_name_map = objc_map_alloc_ggc (200);
5797 alias_name_map = objc_map_alloc_ggc (200);
5799 /* Initialize the hash table used to hold the constant string objects. */
5800 string_htab = hash_table<objc_string_hasher>::create_ggc (31);
5803 /* Use the following to add a method to class_method_map or
5804 instance_method_map. It will add the method, keyed by the
5805 METHOD_SEL_NAME. If the method already exists, but with one or
5806 more different prototypes, it will store a TREE_VEC in the map,
5807 with the method prototypes in the vector. */
5808 static void
5809 insert_method_into_method_map (bool class_method, tree method)
5811 tree method_name = METHOD_SEL_NAME (method);
5812 tree existing_entry;
5813 objc_map_t map;
5815 if (class_method)
5816 map = class_method_map;
5817 else
5818 map = instance_method_map;
5820 /* Check if the method already exists in the map. */
5821 existing_entry = objc_map_get (map, method_name);
5823 /* If not, we simply add it to the map. */
5824 if (existing_entry == OBJC_MAP_NOT_FOUND)
5825 objc_map_put (map, method_name, method);
5826 else
5828 tree new_entry;
5830 /* If an entry already exists, it's more complicated. We'll
5831 have to check whether the method prototype is the same or
5832 not. */
5833 if (TREE_CODE (existing_entry) != TREE_VEC)
5835 /* If the method prototypes are the same, there is nothing
5836 to do. */
5837 if (comp_proto_with_proto (method, existing_entry, 1))
5838 return;
5840 /* If not, create a vector to store both the method already
5841 in the map, and the new one that we are adding. */
5842 new_entry = make_tree_vec (2);
5844 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5845 TREE_VEC_ELT (new_entry, 1) = method;
5847 else
5849 /* An entry already exists, and it's already a vector. This
5850 means that at least 2 different method prototypes were
5851 already found, and we're considering registering yet
5852 another one. */
5853 size_t i;
5855 /* Check all the existing prototypes. If any matches the
5856 one we need to add, there is nothing to do because it's
5857 already there. */
5858 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5859 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5860 return;
5862 /* Else, create a new, bigger vector and add the new method
5863 at the end of it. This is inefficient but extremely
5864 rare; in any sane program most methods have a single
5865 prototype, and very few, if any, will have more than
5866 2! */
5867 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5869 /* Copy the methods from the existing vector. */
5870 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
5871 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5873 /* Add the new method at the end. */
5874 TREE_VEC_ELT (new_entry, i) = method;
5877 /* Store the new vector in the map. */
5878 objc_map_put (map, method_name, new_entry);
5883 static tree
5884 lookup_method (tree mchain, tree method)
5886 tree key;
5888 if (TREE_CODE (method) == IDENTIFIER_NODE)
5889 key = method;
5890 else
5891 key = METHOD_SEL_NAME (method);
5893 while (mchain)
5895 if (METHOD_SEL_NAME (mchain) == key)
5896 return mchain;
5898 mchain = DECL_CHAIN (mchain);
5900 return NULL_TREE;
5903 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5904 method in INTERFACE, along with any categories and protocols
5905 attached thereto. If method is not found, and the
5906 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5907 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5908 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5909 be found in INTERFACE or any of its superclasses, look for an
5910 _instance_ method of the same name in the root class as a last
5911 resort. This behaviour can be turned off by using
5912 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5914 If a suitable method cannot be found, return NULL_TREE. */
5916 static tree
5917 lookup_method_static (tree interface, tree ident, int flags)
5919 tree meth = NULL_TREE, root_inter = NULL_TREE;
5920 tree inter = interface;
5921 int is_class = (flags & OBJC_LOOKUP_CLASS);
5922 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5923 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
5925 while (inter)
5927 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5928 tree category = inter;
5930 /* First, look up the method in the class itself. */
5931 if ((meth = lookup_method (chain, ident)))
5932 return meth;
5934 /* Failing that, look for the method in each category of the class. */
5935 while ((category = CLASS_CATEGORY_LIST (category)))
5937 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
5939 /* Check directly in each category. */
5940 if ((meth = lookup_method (chain, ident)))
5941 return meth;
5943 /* Failing that, check in each category's protocols. */
5944 if (CLASS_PROTOCOL_LIST (category))
5946 if ((meth = (lookup_method_in_protocol_list
5947 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5948 return meth;
5952 /* If not found in categories, check in protocols of the main class. */
5953 if (CLASS_PROTOCOL_LIST (inter))
5955 if ((meth = (lookup_method_in_protocol_list
5956 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5957 return meth;
5960 /* If we were instructed not to look in superclasses, don't. */
5961 if (no_superclasses)
5962 return NULL_TREE;
5964 /* Failing that, climb up the inheritance hierarchy. */
5965 root_inter = inter;
5966 inter = lookup_interface (CLASS_SUPER_NAME (inter));
5968 while (inter);
5970 if (is_class && !no_instance_methods_of_root_class)
5972 /* If no class (factory) method was found, check if an _instance_
5973 method of the same name exists in the root class. This is what
5974 the Objective-C runtime will do. */
5975 return lookup_method_static (root_inter, ident, 0);
5977 else
5979 /* If an instance method was not found, return 0. */
5980 return NULL_TREE;
5984 static tree
5985 objc_add_method (tree klass, tree method, int is_class, bool is_optional)
5987 tree existing_method = NULL_TREE;
5989 /* The first thing we do is look up the method in the list of
5990 methods already defined in the interface (or implementation). */
5991 if (is_class)
5992 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5993 else
5994 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
5996 /* In the case of protocols, we have a second list of methods to
5997 consider, the list of optional ones. */
5998 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6000 /* @required methods are added to the protocol's normal list.
6001 @optional methods are added to the protocol's OPTIONAL lists.
6002 Note that adding the methods to the optional lists disables
6003 checking that the methods are implemented by classes
6004 implementing the protocol, since these checks only use the
6005 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
6007 /* First of all, if the method to add is @optional, and we found
6008 it already existing as @required, emit an error. */
6009 if (is_optional && existing_method)
6011 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6012 (is_class ? '+' : '-'),
6013 METHOD_SEL_NAME (existing_method));
6014 inform (DECL_SOURCE_LOCATION (existing_method),
6015 "previous declaration of %<%c%E%> as %<@required%>",
6016 (is_class ? '+' : '-'),
6017 METHOD_SEL_NAME (existing_method));
6020 /* Now check the list of @optional methods if we didn't find the
6021 method in the @required list. */
6022 if (!existing_method)
6024 if (is_class)
6025 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6026 else
6027 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
6029 if (!is_optional && existing_method)
6031 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6032 (is_class ? '+' : '-'),
6033 METHOD_SEL_NAME (existing_method));
6034 inform (DECL_SOURCE_LOCATION (existing_method),
6035 "previous declaration of %<%c%E%> as %<@optional%>",
6036 (is_class ? '+' : '-'),
6037 METHOD_SEL_NAME (existing_method));
6042 /* If the method didn't exist already, add it. */
6043 if (!existing_method)
6045 if (is_optional)
6047 if (is_class)
6049 /* Put the method on the list in reverse order. */
6050 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6051 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6053 else
6055 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6056 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
6059 else
6061 if (is_class)
6063 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6064 CLASS_CLS_METHODS (klass) = method;
6066 else
6068 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6069 CLASS_NST_METHODS (klass) = method;
6073 else
6075 /* The method was already defined. Check that the types match
6076 for an @interface for a class or category, or for a
6077 @protocol. Give hard errors on methods with identical
6078 selectors but differing argument and/or return types. We do
6079 not do this for @implementations, because C/C++ will do it
6080 for us (i.e., there will be duplicate function definition
6081 errors). */
6082 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6083 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6084 /* Starting with GCC 4.6, we emit the same error for
6085 protocols too. The situation is identical to
6086 @interfaces as there is no possible meaningful reason
6087 for defining the same method with different signatures
6088 in the very same @protocol. If that was allowed,
6089 whenever the protocol is used (both at compile and run
6090 time) there wouldn't be any meaningful way to decide
6091 which of the two method signatures should be used. */
6092 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6093 && !comp_proto_with_proto (method, existing_method, 1))
6095 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6096 (is_class ? '+' : '-'),
6097 METHOD_SEL_NAME (existing_method));
6098 inform (DECL_SOURCE_LOCATION (existing_method),
6099 "previous declaration of %<%c%E%>",
6100 (is_class ? '+' : '-'),
6101 METHOD_SEL_NAME (existing_method));
6105 if (is_class)
6106 insert_method_into_method_map (true, method);
6107 else
6109 insert_method_into_method_map (false, method);
6111 /* Instance methods in root classes (and categories thereof)
6112 may act as class methods as a last resort. We also add
6113 instance methods listed in @protocol declarations to
6114 the class hash table, on the assumption that @protocols
6115 may be adopted by root classes or categories. */
6116 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6117 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6118 klass = lookup_interface (CLASS_NAME (klass));
6120 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6121 || !CLASS_SUPER_NAME (klass))
6122 insert_method_into_method_map (true, method);
6125 return method;
6128 static void
6129 add_category (tree klass, tree category)
6131 /* Put categories on list in reverse order. */
6132 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6134 if (cat)
6136 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6137 CLASS_NAME (klass),
6138 CLASS_SUPER_NAME (category));
6140 else
6142 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6143 CLASS_CATEGORY_LIST (klass) = category;
6147 #ifndef OBJCPLUS
6148 /* A flexible array member is a C99 extension where you can use
6149 "type[]" at the end of a struct to mean a variable-length array.
6151 In Objective-C, instance variables are fundamentally members of a
6152 struct, but the struct can always be extended by subclassing; hence
6153 we need to detect and forbid all instance variables declared using
6154 flexible array members.
6156 No check for this is needed in Objective-C++, since C++ does not
6157 have flexible array members. */
6159 /* Determine whether TYPE is a structure with a flexible array member,
6160 a union containing such a structure (possibly recursively) or an
6161 array of such structures or unions. These are all invalid as
6162 instance variable. */
6163 static bool
6164 flexible_array_type_p (tree type)
6166 tree x;
6167 switch (TREE_CODE (type))
6169 case RECORD_TYPE:
6170 x = TYPE_FIELDS (type);
6171 if (x == NULL_TREE)
6172 return false;
6173 while (DECL_CHAIN (x) != NULL_TREE)
6174 x = DECL_CHAIN (x);
6175 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6176 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6177 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6178 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6179 return true;
6180 return false;
6181 case UNION_TYPE:
6182 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6184 if (flexible_array_type_p (TREE_TYPE (x)))
6185 return true;
6187 return false;
6188 /* Note that we also check for arrays of something that uses a flexible array member. */
6189 case ARRAY_TYPE:
6190 if (flexible_array_type_p (TREE_TYPE (type)))
6191 return true;
6192 return false;
6193 default:
6194 return false;
6197 #endif
6199 /* Produce a printable version of an ivar name. This is only used
6200 inside add_instance_variable. */
6201 static const char *
6202 printable_ivar_name (tree field_decl)
6204 if (DECL_NAME (field_decl))
6205 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6206 else
6207 return _("<unnamed>");
6210 /* Called after parsing each instance variable declaration. Necessary to
6211 preserve typedefs and implement public/private...
6213 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6215 static tree
6216 add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
6217 tree field_decl)
6219 tree field_type = TREE_TYPE (field_decl);
6221 #ifdef OBJCPLUS
6222 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6224 error ("illegal reference type specified for instance variable %qs",
6225 printable_ivar_name (field_decl));
6226 /* Return class as is without adding this ivar. */
6227 return klass;
6229 #endif
6231 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6232 || TYPE_SIZE (field_type) == error_mark_node)
6233 /* 'type[0]' is allowed, but 'type[]' is not! */
6235 error ("instance variable %qs has unknown size",
6236 printable_ivar_name (field_decl));
6237 /* Return class as is without adding this ivar. */
6238 return klass;
6241 #ifndef OBJCPLUS
6242 /* Also, in C reject a struct with a flexible array member. Ie,
6244 struct A { int x; int[] y; };
6246 @interface X
6248 struct A instance_variable;
6250 @end
6252 is not valid because if the class is subclassed, we wouldn't be able
6253 to calculate the offset of the next instance variable. */
6254 if (flexible_array_type_p (field_type))
6256 error ("instance variable %qs uses flexible array member",
6257 printable_ivar_name (field_decl));
6258 /* Return class as is without adding this ivar. */
6259 return klass;
6261 #endif
6263 #ifdef OBJCPLUS
6264 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6265 need to either (1) warn the user about it or (2) generate suitable
6266 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6267 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6268 if (MAYBE_CLASS_TYPE_P (field_type)
6269 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6270 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6271 || TYPE_POLYMORPHIC_P (field_type)))
6273 tree type_name = OBJC_TYPE_NAME (field_type);
6275 if (flag_objc_call_cxx_cdtors)
6277 /* Since the ObjC runtime will be calling the constructors and
6278 destructors for us, the only thing we can't handle is the lack
6279 of a default constructor. */
6280 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6281 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6283 warning (0, "type %qE has no default constructor to call",
6284 type_name);
6286 /* If we cannot call a constructor, we should also avoid
6287 calling the destructor, for symmetry. */
6288 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6289 warning (0, "destructor for %qE shall not be run either",
6290 type_name);
6293 else
6295 static bool warn_cxx_ivars = false;
6297 if (TYPE_POLYMORPHIC_P (field_type))
6299 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6300 initialize them. */
6301 error ("type %qE has virtual member functions", type_name);
6302 error ("illegal aggregate type %qE specified "
6303 "for instance variable %qs",
6304 type_name, printable_ivar_name (field_decl));
6305 /* Return class as is without adding this ivar. */
6306 return klass;
6309 /* User-defined constructors and destructors are not known to Obj-C
6310 and hence will not be called. This may or may not be a problem. */
6311 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6312 warning (0, "type %qE has a user-defined constructor", type_name);
6313 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6314 warning (0, "type %qE has a user-defined destructor", type_name);
6316 if (!warn_cxx_ivars)
6318 warning (0, "C++ constructors and destructors will not "
6319 "be invoked for Objective-C fields");
6320 warn_cxx_ivars = true;
6324 #endif
6326 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6327 switch (visibility)
6329 case OBJC_IVAR_VIS_PROTECTED:
6330 TREE_PUBLIC (field_decl) = 0;
6331 TREE_PRIVATE (field_decl) = 0;
6332 TREE_PROTECTED (field_decl) = 1;
6333 break;
6335 case OBJC_IVAR_VIS_PACKAGE:
6336 /* TODO: Implement the package variant. */
6337 case OBJC_IVAR_VIS_PUBLIC:
6338 TREE_PUBLIC (field_decl) = 1;
6339 TREE_PRIVATE (field_decl) = 0;
6340 TREE_PROTECTED (field_decl) = 0;
6341 break;
6343 case OBJC_IVAR_VIS_PRIVATE:
6344 TREE_PUBLIC (field_decl) = 0;
6345 TREE_PRIVATE (field_decl) = 1;
6346 TREE_PROTECTED (field_decl) = 0;
6347 break;
6351 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6353 return klass;
6356 /* True if the ivar is private and we are not in its implementation. */
6358 static int
6359 is_private (tree decl)
6361 return (TREE_PRIVATE (decl)
6362 && ! is_ivar (CLASS_IVARS (implementation_template),
6363 DECL_NAME (decl)));
6366 /* Searches all the instance variables of 'klass' and of its
6367 superclasses for an instance variable whose name (identifier) is
6368 'ivar_name_ident'. Return the declaration (DECL) of the instance
6369 variable, if found, or NULL_TREE, if not found. */
6370 static inline tree
6371 ivar_of_class (tree klass, tree ivar_name_ident)
6373 /* First, look up the ivar in CLASS_RAW_IVARS. */
6374 tree decl_chain = CLASS_RAW_IVARS (klass);
6376 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6377 if (DECL_NAME (decl_chain) == ivar_name_ident)
6378 return decl_chain;
6380 /* If not found, search up the class hierarchy. */
6381 while (CLASS_SUPER_NAME (klass))
6383 klass = lookup_interface (CLASS_SUPER_NAME (klass));
6385 decl_chain = CLASS_RAW_IVARS (klass);
6387 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6388 if (DECL_NAME (decl_chain) == ivar_name_ident)
6389 return decl_chain;
6392 return NULL_TREE;
6395 /* We have an instance variable reference;, check to see if it is public. */
6398 objc_is_public (tree expr, tree identifier)
6400 tree basetype, decl;
6402 #ifdef OBJCPLUS
6403 if (processing_template_decl)
6404 return 1;
6405 #endif
6407 if (TREE_TYPE (expr) == error_mark_node)
6408 return 1;
6410 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6412 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6414 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6416 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6418 if (!klass)
6420 error ("cannot find interface declaration for %qE",
6421 OBJC_TYPE_NAME (basetype));
6422 return 0;
6425 if ((decl = ivar_of_class (klass, identifier)))
6427 if (TREE_PUBLIC (decl))
6428 return 1;
6430 /* Important difference between the Stepstone translator:
6431 all instance variables should be public within the context
6432 of the implementation. */
6433 if (objc_implementation_context
6434 && ((TREE_CODE (objc_implementation_context)
6435 == CLASS_IMPLEMENTATION_TYPE)
6436 || (TREE_CODE (objc_implementation_context)
6437 == CATEGORY_IMPLEMENTATION_TYPE)))
6439 tree curtype = TYPE_MAIN_VARIANT
6440 (CLASS_STATIC_TEMPLATE
6441 (implementation_template));
6443 if (basetype == curtype
6444 || DERIVED_FROM_P (basetype, curtype))
6446 int priv = is_private (decl);
6448 if (priv)
6449 error ("instance variable %qE is declared private",
6450 DECL_NAME (decl));
6452 return !priv;
6456 /* The 2.95.2 compiler sometimes allowed C functions to access
6457 non-@public ivars. We will let this slide for now... */
6458 if (!objc_method_context)
6460 warning (0, "instance variable %qE is %s; "
6461 "this will be a hard error in the future",
6462 identifier,
6463 TREE_PRIVATE (decl) ? "@private" : "@protected");
6464 return 1;
6467 error ("instance variable %qE is declared %s",
6468 identifier,
6469 TREE_PRIVATE (decl) ? "private" : "protected");
6470 return 0;
6475 return 1;
6478 /* Make sure all methods in CHAIN (a list of method declarations from
6479 an @interface or a @protocol) are in IMPLEMENTATION (the
6480 implementation context). This is used to check for example that
6481 all methods declared in an @interface were implemented in an
6482 @implementation.
6484 Some special methods (property setters/getters) are special and if
6485 they are not found in IMPLEMENTATION, we look them up in its
6486 superclasses. */
6488 static int
6489 check_methods (tree chain, tree implementation, int mtype)
6491 int first = 1;
6492 tree list;
6494 if (mtype == (int)'+')
6495 list = CLASS_CLS_METHODS (implementation);
6496 else
6497 list = CLASS_NST_METHODS (implementation);
6499 while (chain)
6501 /* If the method is associated with a dynamic property, then it
6502 is Ok not to have the method implementation, as it will be
6503 generated dynamically at runtime. To decide if the method is
6504 associated with a @dynamic property, we search the list of
6505 @synthesize and @dynamic for this implementation, and look
6506 for any @dynamic property with the same setter or getter name
6507 as this method. */
6508 tree x;
6509 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6510 if (PROPERTY_DYNAMIC (x)
6511 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6512 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6513 break;
6515 if (x != NULL_TREE)
6517 chain = TREE_CHAIN (chain); /* next method... */
6518 continue;
6521 if (!lookup_method (list, chain))
6523 /* If the method is a property setter/getter, we'll still
6524 allow it to be missing if it is implemented by
6525 'interface' or any of its superclasses. */
6526 tree property = METHOD_PROPERTY_CONTEXT (chain);
6527 if (property)
6529 /* Note that since this is a property getter/setter, it
6530 is obviously an instance method. */
6531 tree interface = NULL_TREE;
6533 /* For a category, first check the main class
6534 @interface. */
6535 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6537 interface = lookup_interface (CLASS_NAME (implementation));
6539 /* If the method is found in the main class, it's Ok. */
6540 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6542 chain = DECL_CHAIN (chain);
6543 continue;
6546 /* Else, get the superclass. */
6547 if (CLASS_SUPER_NAME (interface))
6548 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6549 else
6550 interface = NULL_TREE;
6553 /* Get the superclass for classes. */
6554 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6556 if (CLASS_SUPER_NAME (implementation))
6557 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6558 else
6559 interface = NULL_TREE;
6562 /* Now, interface is the superclass, if any; go check it. */
6563 if (interface)
6565 if (lookup_method_static (interface, chain, 0))
6567 chain = DECL_CHAIN (chain);
6568 continue;
6571 /* Else, fall through - warn. */
6573 if (first)
6575 switch (TREE_CODE (implementation))
6577 case CLASS_IMPLEMENTATION_TYPE:
6578 warning (0, "incomplete implementation of class %qE",
6579 CLASS_NAME (implementation));
6580 break;
6581 case CATEGORY_IMPLEMENTATION_TYPE:
6582 warning (0, "incomplete implementation of category %qE",
6583 CLASS_SUPER_NAME (implementation));
6584 break;
6585 default:
6586 gcc_unreachable ();
6588 first = 0;
6591 warning (0, "method definition for %<%c%E%> not found",
6592 mtype, METHOD_SEL_NAME (chain));
6595 chain = DECL_CHAIN (chain);
6598 return first;
6601 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6603 static int
6604 conforms_to_protocol (tree klass, tree protocol)
6606 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6608 tree p = CLASS_PROTOCOL_LIST (klass);
6609 while (p && TREE_VALUE (p) != protocol)
6610 p = TREE_CHAIN (p);
6612 if (!p)
6614 tree super = (CLASS_SUPER_NAME (klass)
6615 ? lookup_interface (CLASS_SUPER_NAME (klass))
6616 : NULL_TREE);
6617 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6618 if (!tmp)
6619 return 0;
6623 return 1;
6626 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6627 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6629 static int
6630 check_methods_accessible (tree chain, tree context, int mtype)
6632 int first = 1;
6633 tree list;
6634 tree base_context = context;
6636 while (chain)
6638 /* If the method is associated with a dynamic property, then it
6639 is Ok not to have the method implementation, as it will be
6640 generated dynamically at runtime. Search for any @dynamic
6641 property with the same setter or getter name as this
6642 method. TODO: Use a hashtable lookup. */
6643 tree x;
6644 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6645 if (PROPERTY_DYNAMIC (x)
6646 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6647 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6648 break;
6650 if (x != NULL_TREE)
6652 chain = TREE_CHAIN (chain); /* next method... */
6653 continue;
6656 context = base_context;
6657 while (context)
6659 if (mtype == '+')
6660 list = CLASS_CLS_METHODS (context);
6661 else
6662 list = CLASS_NST_METHODS (context);
6664 if (lookup_method (list, chain))
6665 break;
6667 switch (TREE_CODE (context))
6669 case CLASS_IMPLEMENTATION_TYPE:
6670 case CLASS_INTERFACE_TYPE:
6671 context = (CLASS_SUPER_NAME (context)
6672 ? lookup_interface (CLASS_SUPER_NAME (context))
6673 : NULL_TREE);
6674 break;
6675 case CATEGORY_IMPLEMENTATION_TYPE:
6676 case CATEGORY_INTERFACE_TYPE:
6677 context = (CLASS_NAME (context)
6678 ? lookup_interface (CLASS_NAME (context))
6679 : NULL_TREE);
6680 break;
6681 default:
6682 gcc_unreachable ();
6686 if (context == NULL_TREE)
6688 if (first)
6690 switch (TREE_CODE (objc_implementation_context))
6692 case CLASS_IMPLEMENTATION_TYPE:
6693 warning (0, "incomplete implementation of class %qE",
6694 CLASS_NAME (objc_implementation_context));
6695 break;
6696 case CATEGORY_IMPLEMENTATION_TYPE:
6697 warning (0, "incomplete implementation of category %qE",
6698 CLASS_SUPER_NAME (objc_implementation_context));
6699 break;
6700 default:
6701 gcc_unreachable ();
6703 first = 0;
6705 warning (0, "method definition for %<%c%E%> not found",
6706 mtype, METHOD_SEL_NAME (chain));
6709 chain = TREE_CHAIN (chain); /* next method... */
6711 return first;
6714 /* Check whether the current interface (accessible via
6715 'objc_implementation_context') actually implements protocol P, along
6716 with any protocols that P inherits. */
6718 static void
6719 check_protocol (tree p, const char *type, tree name)
6721 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
6723 int f1, f2;
6725 /* Ensure that all protocols have bodies! */
6726 if (warn_protocol)
6728 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6729 objc_implementation_context,
6730 '+');
6731 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6732 objc_implementation_context,
6733 '-');
6735 else
6737 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6738 objc_implementation_context,
6739 '+');
6740 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6741 objc_implementation_context,
6742 '-');
6745 if (!f1 || !f2)
6746 warning (0, "%s %qE does not fully implement the %qE protocol",
6747 type, name, PROTOCOL_NAME (p));
6750 /* Check protocols recursively. */
6751 if (PROTOCOL_LIST (p))
6753 tree subs = PROTOCOL_LIST (p);
6754 tree super_class =
6755 lookup_interface (CLASS_SUPER_NAME (implementation_template));
6757 while (subs)
6759 tree sub = TREE_VALUE (subs);
6761 /* If the superclass does not conform to the protocols
6762 inherited by P, then we must! */
6763 if (!super_class || !conforms_to_protocol (super_class, sub))
6764 check_protocol (sub, type, name);
6765 subs = TREE_CHAIN (subs);
6770 /* Check whether the current interface (accessible via
6771 'objc_implementation_context') actually implements the protocols listed
6772 in PROTO_LIST. */
6774 static void
6775 check_protocols (tree proto_list, const char *type, tree name)
6777 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
6779 tree p = TREE_VALUE (proto_list);
6781 check_protocol (p, type, name);
6785 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6786 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6787 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6788 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6789 SUPER_NAME is the name of the category. For a class extension,
6790 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6791 static tree
6792 start_class (enum tree_code code, tree class_name, tree super_name,
6793 tree protocol_list, tree attributes)
6795 tree klass = NULL_TREE;
6796 tree decl;
6798 #ifdef OBJCPLUS
6799 if (current_namespace != global_namespace)
6801 error ("Objective-C declarations may only appear in global scope");
6803 #endif /* OBJCPLUS */
6805 if (objc_implementation_context)
6807 warning (0, "%<@end%> missing in implementation context");
6808 finish_class (objc_implementation_context);
6809 objc_ivar_chain = NULL_TREE;
6810 objc_implementation_context = NULL_TREE;
6813 /* If this is a class extension, we'll be "reopening" the existing
6814 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6815 a new node. */
6816 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6818 klass = make_node (code);
6819 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6822 /* Check for existence of the super class, if one was specified. Note
6823 that we must have seen an @interface, not just a @class. If we
6824 are looking at a @compatibility_alias, traverse it first. */
6825 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6826 && super_name)
6828 tree super = objc_is_class_name (super_name);
6829 tree super_interface = NULL_TREE;
6831 if (super)
6832 super_interface = lookup_interface (super);
6834 if (!super_interface)
6836 error ("cannot find interface declaration for %qE, superclass of %qE",
6837 super ? super : super_name,
6838 class_name);
6839 super_name = NULL_TREE;
6841 else
6843 if (TREE_DEPRECATED (super_interface))
6844 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6845 super);
6846 super_name = super;
6850 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6852 CLASS_NAME (klass) = class_name;
6853 CLASS_SUPER_NAME (klass) = super_name;
6854 CLASS_CLS_METHODS (klass) = NULL_TREE;
6857 if (! objc_is_class_name (class_name)
6858 && (decl = lookup_name (class_name)))
6860 error ("%qE redeclared as different kind of symbol",
6861 class_name);
6862 error ("previous declaration of %q+D",
6863 decl);
6866 switch (code)
6868 case CLASS_IMPLEMENTATION_TYPE:
6870 tree chain;
6872 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6873 if (TREE_VALUE (chain) == class_name)
6875 error ("reimplementation of class %qE",
6876 class_name);
6877 /* TODO: error message saying where it was previously
6878 implemented. */
6879 break;
6881 if (chain == NULL_TREE)
6882 implemented_classes = tree_cons (NULL_TREE, class_name,
6883 implemented_classes);
6886 /* Reset for multiple classes per file. */
6887 method_slot = 0;
6889 objc_implementation_context = klass;
6891 /* Lookup the interface for this implementation. */
6893 if (!(implementation_template = lookup_interface (class_name)))
6895 warning (0, "cannot find interface declaration for %qE",
6896 class_name);
6897 add_interface (implementation_template = objc_implementation_context,
6898 class_name);
6901 /* If a super class has been specified in the implementation,
6902 insure it conforms to the one specified in the interface. */
6904 if (super_name
6905 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6907 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6908 error ("conflicting super class name %qE",
6909 super_name);
6910 if (previous_name)
6911 error ("previous declaration of %qE", previous_name);
6912 else
6913 error ("previous declaration");
6916 else if (! super_name)
6918 CLASS_SUPER_NAME (objc_implementation_context)
6919 = CLASS_SUPER_NAME (implementation_template);
6921 break;
6923 case CLASS_INTERFACE_TYPE:
6924 if (lookup_interface (class_name))
6925 #ifdef OBJCPLUS
6926 error ("duplicate interface declaration for class %qE", class_name);
6927 #else
6928 warning (0, "duplicate interface declaration for class %qE", class_name);
6929 #endif
6930 else
6931 add_interface (klass, class_name);
6933 if (protocol_list)
6934 CLASS_PROTOCOL_LIST (klass)
6935 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
6937 if (attributes)
6939 tree attribute;
6940 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6942 tree name = TREE_PURPOSE (attribute);
6944 /* TODO: Document what the objc_exception attribute is/does. */
6945 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6946 attributes. */
6947 if (is_attribute_p ("deprecated", name))
6948 TREE_DEPRECATED (klass) = 1;
6949 else if (is_attribute_p ("objc_exception", name))
6950 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6951 else
6952 /* Warn about and ignore all others for now, but store them. */
6953 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6955 TYPE_ATTRIBUTES (klass) = attributes;
6957 break;
6959 case CATEGORY_INTERFACE_TYPE:
6961 tree class_category_is_assoc_with;
6963 /* For a category, class_name is really the name of the class that
6964 the following set of methods will be associated with. We must
6965 find the interface so that can derive the objects template. */
6966 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6968 error ("cannot find interface declaration for %qE",
6969 class_name);
6970 exit (FATAL_EXIT_CODE);
6972 else
6974 if (TREE_DEPRECATED (class_category_is_assoc_with))
6975 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
6976 class_name);
6978 if (super_name == NULL_TREE)
6980 /* This is a class extension. Get the original
6981 interface, and continue working on it. */
6982 objc_in_class_extension = true;
6983 klass = class_category_is_assoc_with;
6985 if (protocol_list)
6987 /* Append protocols to the original protocol
6988 list. */
6989 CLASS_PROTOCOL_LIST (klass)
6990 = chainon (CLASS_PROTOCOL_LIST (klass),
6991 lookup_and_install_protocols
6992 (protocol_list,
6993 /* definition_required */ true));
6996 else
6998 add_category (class_category_is_assoc_with, klass);
7000 if (protocol_list)
7001 CLASS_PROTOCOL_LIST (klass)
7002 = lookup_and_install_protocols
7003 (protocol_list, /* definition_required */ true);
7007 break;
7009 case CATEGORY_IMPLEMENTATION_TYPE:
7010 /* Reset for multiple classes per file. */
7011 method_slot = 0;
7013 objc_implementation_context = klass;
7015 /* For a category, class_name is really the name of the class that
7016 the following set of methods will be associated with. We must
7017 find the interface so that can derive the objects template. */
7019 if (!(implementation_template = lookup_interface (class_name)))
7021 error ("cannot find interface declaration for %qE",
7022 class_name);
7023 exit (FATAL_EXIT_CODE);
7025 break;
7026 default:
7027 gcc_unreachable ();
7029 return klass;
7032 static tree
7033 continue_class (tree klass)
7035 switch (TREE_CODE (klass))
7037 case CLASS_IMPLEMENTATION_TYPE:
7038 case CATEGORY_IMPLEMENTATION_TYPE:
7040 struct imp_entry *imp_entry;
7042 /* Check consistency of the instance variables. */
7044 if (CLASS_RAW_IVARS (klass))
7045 check_ivars (implementation_template, klass);
7047 /* code generation */
7048 #ifdef OBJCPLUS
7049 push_lang_context (lang_name_c);
7050 #endif
7051 build_private_template (implementation_template);
7052 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7053 objc_instance_type = build_pointer_type (uprivate_record);
7055 imp_entry = ggc_alloc<struct imp_entry> ();
7057 imp_entry->next = imp_list;
7058 imp_entry->imp_context = klass;
7059 imp_entry->imp_template = implementation_template;
7060 ucls_super_ref = uucls_super_ref = NULL;
7061 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7063 imp_entry->class_decl = (*runtime.class_decl) (klass);
7064 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7066 else
7068 imp_entry->class_decl = (*runtime.category_decl) (klass);
7069 imp_entry->meta_decl = NULL;
7071 imp_entry->has_cxx_cdtors = 0;
7073 /* Append to front and increment count. */
7074 imp_list = imp_entry;
7075 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7076 imp_count++;
7077 else
7078 cat_count++;
7079 #ifdef OBJCPLUS
7080 pop_lang_context ();
7081 #endif /* OBJCPLUS */
7083 return get_class_ivars (implementation_template, true);
7084 break;
7086 case CLASS_INTERFACE_TYPE:
7088 if (objc_in_class_extension)
7089 return NULL_TREE;
7090 #ifdef OBJCPLUS
7091 push_lang_context (lang_name_c);
7092 #endif /* OBJCPLUS */
7093 objc_collecting_ivars = 1;
7094 build_private_template (klass);
7095 objc_collecting_ivars = 0;
7096 #ifdef OBJCPLUS
7097 pop_lang_context ();
7098 #endif /* OBJCPLUS */
7099 return NULL_TREE;
7100 break;
7102 default:
7103 return error_mark_node;
7107 /* This routine builds name of the setter synthesized function. */
7108 char *
7109 objc_build_property_setter_name (tree ident)
7111 /* TODO: Use alloca to allocate buffer of appropriate size. */
7112 static char string[BUFSIZE];
7113 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7114 string[3] = TOUPPER (string[3]);
7115 return string;
7118 /* This routine prepares the declarations of the property accessor
7119 helper functions (objc_getProperty(), etc) that are used when
7120 @synthesize is used.
7122 runtime-specific routines are built in the respective runtime
7123 initialize functions. */
7124 static void
7125 build_common_objc_property_accessor_helpers (void)
7127 tree type;
7129 /* Declare the following function:
7131 objc_getProperty (id self, SEL _cmd,
7132 ptrdiff_t offset, BOOL is_atomic); */
7133 type = build_function_type_list (objc_object_type,
7134 objc_object_type,
7135 objc_selector_type,
7136 ptrdiff_type_node,
7137 boolean_type_node,
7138 NULL_TREE);
7139 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7140 type, 0, NOT_BUILT_IN,
7141 NULL, NULL_TREE);
7142 TREE_NOTHROW (objc_getProperty_decl) = 0;
7144 /* Declare the following function:
7145 void
7146 objc_setProperty (id self, SEL _cmd,
7147 ptrdiff_t offset, id new_value,
7148 BOOL is_atomic, BOOL should_copy); */
7149 type = build_function_type_list (void_type_node,
7150 objc_object_type,
7151 objc_selector_type,
7152 ptrdiff_type_node,
7153 objc_object_type,
7154 boolean_type_node,
7155 boolean_type_node,
7156 NULL_TREE);
7157 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7158 type, 0, NOT_BUILT_IN,
7159 NULL, NULL_TREE);
7160 TREE_NOTHROW (objc_setProperty_decl) = 0;
7163 /* This looks up an ivar in a class (including superclasses). */
7164 static tree
7165 lookup_ivar (tree interface, tree instance_variable_name)
7167 while (interface)
7169 tree decl_chain;
7171 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7172 if (DECL_NAME (decl_chain) == instance_variable_name)
7173 return decl_chain;
7175 /* Not found. Search superclass if any. */
7176 if (CLASS_SUPER_NAME (interface))
7177 interface = lookup_interface (CLASS_SUPER_NAME (interface));
7180 return NULL_TREE;
7183 /* This routine synthesizes a 'getter' method. This is only called
7184 for @synthesize properties. */
7185 static void
7186 objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7188 location_t location = DECL_SOURCE_LOCATION (property);
7189 tree fn, decl;
7190 tree body;
7191 tree ret_val;
7193 /* If user has implemented a getter with same name then do nothing. */
7194 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7195 PROPERTY_GETTER_NAME (property)))
7196 return;
7198 /* Find declaration of the property getter in the interface (or
7199 superclass, or protocol). There must be one. */
7200 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
7202 /* If one not declared in the interface, this condition has already
7203 been reported as user error (because property was not declared in
7204 the interface). */
7205 if (!decl)
7206 return;
7208 /* Adapt the 'decl'. Use the source location of the @synthesize
7209 statement for error messages. */
7210 decl = copy_node (decl);
7211 DECL_SOURCE_LOCATION (decl) = location;
7213 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7214 NULL_TREE);
7215 body = c_begin_compound_stmt (true);
7217 /* Now we need to decide how we build the getter. There are three
7218 cases:
7220 for 'copy' or 'retain' properties we need to use the
7221 objc_getProperty() accessor helper which knows about retain and
7222 copy. It supports both 'nonatomic' and 'atomic' access.
7224 for 'nonatomic, assign' properties we can access the instance
7225 variable directly. 'nonatomic' means we don't have to use locks,
7226 and 'assign' means we don't have to worry about retain or copy.
7227 If you combine the two, it means we can just access the instance
7228 variable directly.
7230 for 'atomic, assign' properties we use objc_copyStruct() (for the
7231 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7232 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7234 case OBJC_PROPERTY_RETAIN:
7235 case OBJC_PROPERTY_COPY:
7237 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7238 tree cmd, ivar, offset, is_atomic;
7239 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7241 /* Find the ivar to compute the offset. */
7242 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7243 if (!ivar || is_private (ivar))
7245 /* This should never happen. */
7246 error_at (location,
7247 "can not find instance variable associated with property");
7248 ret_val = error_mark_node;
7249 break;
7251 offset = byte_position (ivar);
7253 if (PROPERTY_NONATOMIC (property))
7254 is_atomic = boolean_false_node;
7255 else
7256 is_atomic = boolean_true_node;
7258 ret_val = build_function_call
7259 (location,
7260 /* Function prototype. */
7261 objc_getProperty_decl,
7262 /* Parameters. */
7263 tree_cons /* self */
7264 (NULL_TREE, self_decl,
7265 tree_cons /* _cmd */
7266 (NULL_TREE, cmd,
7267 tree_cons /* offset */
7268 (NULL_TREE, offset,
7269 tree_cons /* is_atomic */
7270 (NULL_TREE, is_atomic, NULL_TREE)))));
7272 break;
7273 case OBJC_PROPERTY_ASSIGN:
7274 if (PROPERTY_NONATOMIC (property))
7276 /* We build "return self->PROPERTY_IVAR_NAME;" */
7277 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7278 break;
7280 else
7282 /* We build
7283 <property type> __objc_property_temp;
7284 objc_getPropertyStruct (&__objc_property_temp,
7285 &(self->PROPERTY_IVAR_NAME),
7286 sizeof (type of self->PROPERTY_IVAR_NAME),
7287 is_atomic,
7288 false)
7289 return __objc_property_temp;
7291 For the NeXT runtime, we need to use objc_copyStruct
7292 instead of objc_getPropertyStruct. */
7293 tree objc_property_temp_decl, function_decl, function_call;
7294 tree size_of, is_atomic;
7296 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7297 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7298 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7300 /* sizeof (ivar type). Since the ivar and the property have
7301 the same type, there is no need to lookup the ivar. */
7302 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7303 true /* is_sizeof */,
7304 false /* min_alignof */,
7305 false /* complain */);
7307 if (PROPERTY_NONATOMIC (property))
7308 is_atomic = boolean_false_node;
7309 else
7310 is_atomic = boolean_true_node;
7312 if (objc_copyStruct_decl)
7313 function_decl = objc_copyStruct_decl;
7314 else
7315 function_decl = objc_getPropertyStruct_decl;
7317 function_call = build_function_call
7318 (location,
7319 /* Function prototype. */
7320 function_decl,
7321 /* Parameters. */
7322 tree_cons /* &__objc_property_temp_decl */
7323 /* Warning: note that using build_fold_addr_expr_loc()
7324 here causes invalid code to be generated. */
7325 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7326 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7327 (NULL_TREE, build_fold_addr_expr_loc (location,
7328 objc_lookup_ivar
7329 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7330 tree_cons /* sizeof (PROPERTY_IVAR) */
7331 (NULL_TREE, size_of,
7332 tree_cons /* is_atomic */
7333 (NULL_TREE, is_atomic,
7334 /* TODO: This is currently ignored by the GNU
7335 runtime, but what about the next one ? */
7336 tree_cons /* has_strong */
7337 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7339 add_stmt (function_call);
7341 ret_val = objc_property_temp_decl;
7343 break;
7344 default:
7345 gcc_unreachable ();
7348 gcc_assert (ret_val);
7350 #ifdef OBJCPLUS
7351 finish_return_stmt (ret_val);
7352 #else
7353 c_finish_return (location, ret_val, NULL_TREE);
7354 #endif
7356 add_stmt (c_end_compound_stmt (location, body, true));
7357 fn = current_function_decl;
7358 #ifdef OBJCPLUS
7359 finish_function ();
7360 #endif
7361 objc_finish_method_definition (fn);
7364 /* This routine synthesizes a 'setter' method. */
7366 static void
7367 objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
7369 location_t location = DECL_SOURCE_LOCATION (property);
7370 tree fn, decl;
7371 tree body;
7372 tree new_value, statement;
7374 /* If user has implemented a setter with same name then do nothing. */
7375 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7376 PROPERTY_SETTER_NAME (property)))
7377 return;
7379 /* Find declaration of the property setter in the interface (or
7380 superclass, or protocol). There must be one. */
7381 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
7383 /* If one not declared in the interface, this condition has already
7384 been reported as user error (because property was not declared in
7385 the interface). */
7386 if (!decl)
7387 return;
7389 /* Adapt the 'decl'. Use the source location of the @synthesize
7390 statement for error messages. */
7391 decl = copy_node (decl);
7392 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
7394 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7395 NULL_TREE);
7397 body = c_begin_compound_stmt (true);
7399 /* The 'new_value' is the only argument to the method, which is the
7400 3rd argument of the function, after self and _cmd. We use twice
7401 TREE_CHAIN to move forward two arguments. */
7402 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7404 /* This would presumably happen if the user has specified a
7405 prototype for the setter that does not have an argument! */
7406 if (new_value == NULL_TREE)
7408 /* TODO: This should be caught much earlier than this. */
7409 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7410 /* Try to recover somehow. */
7411 new_value = error_mark_node;
7414 /* Now we need to decide how we build the setter. There are three
7415 cases:
7417 for 'copy' or 'retain' properties we need to use the
7418 objc_setProperty() accessor helper which knows about retain and
7419 copy. It supports both 'nonatomic' and 'atomic' access.
7421 for 'nonatomic, assign' properties we can access the instance
7422 variable directly. 'nonatomic' means we don't have to use locks,
7423 and 'assign' means we don't have to worry about retain or copy.
7424 If you combine the two, it means we can just access the instance
7425 variable directly.
7427 for 'atomic, assign' properties we use objc_copyStruct() (for the
7428 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7429 switch (PROPERTY_ASSIGN_SEMANTICS (property))
7431 case OBJC_PROPERTY_RETAIN:
7432 case OBJC_PROPERTY_COPY:
7434 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7435 tree cmd, ivar, offset, is_atomic, should_copy;
7436 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
7438 /* Find the ivar to compute the offset. */
7439 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7440 if (!ivar || is_private (ivar))
7442 error_at (location,
7443 "can not find instance variable associated with property");
7444 statement = error_mark_node;
7445 break;
7447 offset = byte_position (ivar);
7449 if (PROPERTY_NONATOMIC (property))
7450 is_atomic = boolean_false_node;
7451 else
7452 is_atomic = boolean_true_node;
7454 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7455 should_copy = boolean_true_node;
7456 else
7457 should_copy = boolean_false_node;
7459 statement = build_function_call
7460 (location,
7461 /* Function prototype. */
7462 objc_setProperty_decl,
7463 /* Parameters. */
7464 tree_cons /* self */
7465 (NULL_TREE, self_decl,
7466 tree_cons /* _cmd */
7467 (NULL_TREE, cmd,
7468 tree_cons /* offset */
7469 (NULL_TREE, offset,
7470 tree_cons /* new_value */
7471 (NULL_TREE, new_value,
7472 tree_cons /* is_atomic */
7473 (NULL_TREE, is_atomic,
7474 tree_cons /* should_copy */
7475 (NULL_TREE, should_copy, NULL_TREE)))))));
7477 break;
7478 case OBJC_PROPERTY_ASSIGN:
7479 if (PROPERTY_NONATOMIC (property))
7481 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7482 statement = build_modify_expr
7483 (location,
7484 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
7485 NULL_TREE, NOP_EXPR,
7486 location, new_value, NULL_TREE);
7487 break;
7489 else
7491 /* We build
7492 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7493 &new_value,
7494 sizeof (type of self->PROPERTY_IVAR_NAME),
7495 is_atomic,
7496 false)
7498 For the NeXT runtime, we need to use objc_copyStruct
7499 instead of objc_getPropertyStruct. */
7500 tree function_decl, size_of, is_atomic;
7502 /* sizeof (ivar type). Since the ivar and the property have
7503 the same type, there is no need to lookup the ivar. */
7504 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7505 true /* is_sizeof */,
7506 false /* min_alignof */,
7507 false /* complain */);
7509 if (PROPERTY_NONATOMIC (property))
7510 is_atomic = boolean_false_node;
7511 else
7512 is_atomic = boolean_true_node;
7514 if (objc_copyStruct_decl)
7515 function_decl = objc_copyStruct_decl;
7516 else
7517 function_decl = objc_setPropertyStruct_decl;
7519 statement = build_function_call
7520 (location,
7521 /* Function prototype. */
7522 function_decl,
7523 /* Parameters. */
7524 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
7525 (NULL_TREE, build_fold_addr_expr_loc (location,
7526 objc_lookup_ivar
7527 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7528 tree_cons /* &new_value */
7529 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7530 tree_cons /* sizeof (PROPERTY_IVAR) */
7531 (NULL_TREE, size_of,
7532 tree_cons /* is_atomic */
7533 (NULL_TREE, is_atomic,
7534 /* TODO: This is currently ignored by the GNU
7535 runtime, but what about the next one ? */
7536 tree_cons /* has_strong */
7537 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7539 break;
7540 default:
7541 gcc_unreachable ();
7543 gcc_assert (statement);
7545 add_stmt (statement);
7546 add_stmt (c_end_compound_stmt (location, body, true));
7547 fn = current_function_decl;
7548 #ifdef OBJCPLUS
7549 finish_function ();
7550 #endif
7551 objc_finish_method_definition (fn);
7554 /* This function is a sub-routine of objc_add_synthesize_declaration.
7555 It is called for each property to synthesize once we have
7556 determined that the context is Ok. */
7557 static void
7558 objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7559 tree property_name, tree ivar_name)
7561 /* Find the @property declaration. */
7562 tree property;
7563 tree x;
7565 /* Check that synthesize or dynamic has not already been used for
7566 the same property. */
7567 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7568 if (PROPERTY_NAME (property) == property_name)
7570 location_t original_location = DECL_SOURCE_LOCATION (property);
7572 if (PROPERTY_DYNAMIC (property))
7573 error_at (location, "property %qs already specified in %<@dynamic%>",
7574 IDENTIFIER_POINTER (property_name));
7575 else
7576 error_at (location, "property %qs already specified in %<@synthesize%>",
7577 IDENTIFIER_POINTER (property_name));
7579 if (original_location != UNKNOWN_LOCATION)
7580 inform (original_location, "originally specified here");
7581 return;
7584 /* Check that the property is declared in the interface. It could
7585 also be declared in a superclass or protocol. */
7586 property = lookup_property (interface, property_name);
7588 if (!property)
7590 error_at (location, "no declaration of property %qs found in the interface",
7591 IDENTIFIER_POINTER (property_name));
7592 return;
7594 else
7596 /* We have to copy the property, because we want to chain it to
7597 the implementation context, and we want to store the source
7598 location of the @synthesize, not of the original
7599 @property. */
7600 property = copy_node (property);
7601 DECL_SOURCE_LOCATION (property) = location;
7604 /* Determine PROPERTY_IVAR_NAME. */
7605 if (ivar_name == NULL_TREE)
7606 ivar_name = property_name;
7608 /* Check that the instance variable exists. You can only use an
7609 instance variable from the same class, not one from the
7610 superclass (this makes sense as it allows us to check that an
7611 instance variable is only used in one synthesized property). */
7613 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7614 tree type_of_ivar;
7615 if (!ivar)
7617 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7618 IDENTIFIER_POINTER (property_name));
7619 return;
7622 if (DECL_BIT_FIELD_TYPE (ivar))
7623 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7624 else
7625 type_of_ivar = TREE_TYPE (ivar);
7627 /* If the instance variable has a different C type, we throw an error ... */
7628 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7629 /* ... unless the property is readonly, in which case we allow
7630 the instance variable to be more specialized (this means we
7631 can generate the getter all right and it works). */
7632 && (!PROPERTY_READONLY (property)
7633 || !objc_compare_types (TREE_TYPE (property),
7634 type_of_ivar, -5, NULL_TREE)))
7636 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7638 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7639 IDENTIFIER_POINTER (property_name),
7640 IDENTIFIER_POINTER (ivar_name));
7642 if (original_location != UNKNOWN_LOCATION)
7643 inform (original_location, "originally specified here");
7646 /* If the instance variable is a bitfield, the property must be
7647 'assign', 'nonatomic' because the runtime getter/setter helper
7648 do not work with bitfield instance variables. */
7649 if (DECL_BIT_FIELD_TYPE (ivar))
7651 /* If there is an error, we return and not generate any
7652 getter/setter because trying to set up the runtime
7653 getter/setter helper calls with bitfields is at high risk
7654 of ICE. */
7656 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7658 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7660 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
7661 IDENTIFIER_POINTER (property_name),
7662 IDENTIFIER_POINTER (ivar_name));
7664 if (original_location != UNKNOWN_LOCATION)
7665 inform (original_location, "originally specified here");
7666 return;
7669 if (!PROPERTY_NONATOMIC (property))
7671 location_t original_location = DECL_SOURCE_LOCATION (ivar);
7673 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
7674 IDENTIFIER_POINTER (property_name),
7675 IDENTIFIER_POINTER (ivar_name));
7677 if (original_location != UNKNOWN_LOCATION)
7678 inform (original_location, "originally specified here");
7679 return;
7684 /* Check that no other property is using the same instance
7685 variable. */
7686 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7687 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7689 location_t original_location = DECL_SOURCE_LOCATION (x);
7691 error_at (location, "property %qs is using the same instance variable as property %qs",
7692 IDENTIFIER_POINTER (property_name),
7693 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
7695 if (original_location != UNKNOWN_LOCATION)
7696 inform (original_location, "originally specified here");
7698 /* We keep going on. This won't cause the compiler to fail;
7699 the failure would most likely be at runtime. */
7702 /* Note that a @synthesize (and only a @synthesize) always sets
7703 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7704 @synthesize by that. */
7705 PROPERTY_IVAR_NAME (property) = ivar_name;
7707 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7708 original declaration; they are always set (with the exception of
7709 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7711 /* Add the property to the list of properties for current implementation. */
7712 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7713 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7715 /* Note how we don't actually synthesize the getter/setter here; it
7716 would be very natural, but we may miss the fact that the user has
7717 implemented his own getter/setter later on in the @implementation
7718 (in which case we shouldn't generate getter/setter). We wait
7719 until we have parsed it all before generating the code. */
7722 /* This function is called by the parser after a @synthesize
7723 expression is parsed. 'location' is the location of the
7724 @synthesize expression, and 'property_and_ivar_list' is a chained
7725 list of the property and ivar names. */
7726 void
7727 objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7729 tree interface, chain;
7731 if (flag_objc1_only)
7732 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
7734 if (property_and_ivar_list == error_mark_node)
7735 return;
7737 if (!objc_implementation_context)
7739 /* We can get here only in Objective-C; the Objective-C++ parser
7740 detects the problem while parsing, outputs the error
7741 "misplaced '@synthesize' Objective-C++ construct" and skips
7742 the declaration. */
7743 error_at (location, "%<@synthesize%> not in @implementation context");
7744 return;
7747 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7749 error_at (location, "%<@synthesize%> can not be used in categories");
7750 return;
7753 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7754 if (!interface)
7756 /* I can't see how this could happen, but it is good as a safety check. */
7757 error_at (location,
7758 "%<@synthesize%> requires the @interface of the class to be available");
7759 return;
7762 /* Now, iterate over the properties and do each of them. */
7763 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7765 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
7766 TREE_PURPOSE (chain));
7770 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7771 is called for each property to mark as dynamic once we have
7772 determined that the context is Ok. */
7773 static void
7774 objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7775 tree property_name)
7777 /* Find the @property declaration. */
7778 tree property;
7780 /* Check that synthesize or dynamic has not already been used for
7781 the same property. */
7782 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7783 if (PROPERTY_NAME (property) == property_name)
7785 location_t original_location = DECL_SOURCE_LOCATION (property);
7787 if (PROPERTY_DYNAMIC (property))
7788 error_at (location, "property %qs already specified in %<@dynamic%>",
7789 IDENTIFIER_POINTER (property_name));
7790 else
7791 error_at (location, "property %qs already specified in %<@synthesize%>",
7792 IDENTIFIER_POINTER (property_name));
7794 if (original_location != UNKNOWN_LOCATION)
7795 inform (original_location, "originally specified here");
7796 return;
7799 /* Check that the property is declared in the interface. It could
7800 also be declared in a superclass or protocol. */
7801 property = lookup_property (interface, property_name);
7803 if (!property)
7805 error_at (location, "no declaration of property %qs found in the interface",
7806 IDENTIFIER_POINTER (property_name));
7807 return;
7809 else
7811 /* We have to copy the property, because we want to chain it to
7812 the implementation context, and we want to store the source
7813 location of the @synthesize, not of the original
7814 @property. */
7815 property = copy_node (property);
7816 DECL_SOURCE_LOCATION (property) = location;
7819 /* Note that a @dynamic (and only a @dynamic) always sets
7820 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7821 (actually, as explained above, PROPERTY_DECL generated by
7822 @property and associated with a @dynamic property are also marked
7823 as PROPERTY_DYNAMIC). */
7824 PROPERTY_DYNAMIC (property) = 1;
7826 /* Add the property to the list of properties for current implementation. */
7827 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7828 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7831 /* This function is called by the parser after a @dynamic expression
7832 is parsed. 'location' is the location of the @dynamic expression,
7833 and 'property_list' is a chained list of all the property
7834 names. */
7835 void
7836 objc_add_dynamic_declaration (location_t location, tree property_list)
7838 tree interface, chain;
7840 if (flag_objc1_only)
7841 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
7843 if (property_list == error_mark_node)
7844 return;
7846 if (!objc_implementation_context)
7848 /* We can get here only in Objective-C; the Objective-C++ parser
7849 detects the problem while parsing, outputs the error
7850 "misplaced '@dynamic' Objective-C++ construct" and skips the
7851 declaration. */
7852 error_at (location, "%<@dynamic%> not in @implementation context");
7853 return;
7856 /* @dynamic is allowed in categories. */
7857 switch (TREE_CODE (objc_implementation_context))
7859 case CLASS_IMPLEMENTATION_TYPE:
7860 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7861 break;
7862 case CATEGORY_IMPLEMENTATION_TYPE:
7863 interface = lookup_category (implementation_template,
7864 CLASS_SUPER_NAME (objc_implementation_context));
7865 break;
7866 default:
7867 gcc_unreachable ();
7870 if (!interface)
7872 /* I can't see how this could happen, but it is good as a safety check. */
7873 error_at (location,
7874 "%<@dynamic%> requires the @interface of the class to be available");
7875 return;
7878 /* Now, iterate over the properties and do each of them. */
7879 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
7881 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7885 /* Main routine to generate code/data for all the property information for
7886 current implementation (class or category). CLASS is the interface where
7887 ivars are declared. CLASS_METHODS is where methods are found which
7888 could be a class or a category depending on whether we are implementing
7889 property of a class or a category. */
7891 static void
7892 objc_gen_property_data (tree klass, tree class_methods)
7894 tree x;
7896 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7898 /* @dynamic property - nothing to check or synthesize. */
7899 if (PROPERTY_DYNAMIC (x))
7900 continue;
7902 /* @synthesize property - need to synthesize the accessors. */
7903 if (PROPERTY_IVAR_NAME (x))
7905 objc_synthesize_getter (klass, class_methods, x);
7907 if (PROPERTY_READONLY (x) == 0)
7908 objc_synthesize_setter (klass, class_methods, x);
7910 continue;
7913 gcc_unreachable ();
7917 /* This is called once we see the "@end" in an interface/implementation. */
7919 static void
7920 finish_class (tree klass)
7922 switch (TREE_CODE (klass))
7924 case CLASS_IMPLEMENTATION_TYPE:
7926 /* All metadata generation is done in runtime.generate_metadata(). */
7928 /* Generate what needed for property; setters, getters, etc. */
7929 objc_gen_property_data (implementation_template, implementation_template);
7931 if (implementation_template != objc_implementation_context)
7933 /* Ensure that all method listed in the interface contain bodies. */
7934 check_methods (CLASS_CLS_METHODS (implementation_template),
7935 objc_implementation_context, '+');
7936 check_methods (CLASS_NST_METHODS (implementation_template),
7937 objc_implementation_context, '-');
7939 if (CLASS_PROTOCOL_LIST (implementation_template))
7940 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7941 "class",
7942 CLASS_NAME (objc_implementation_context));
7944 break;
7946 case CATEGORY_IMPLEMENTATION_TYPE:
7948 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
7950 if (category)
7952 /* Generate what needed for property; setters, getters, etc. */
7953 objc_gen_property_data (implementation_template, category);
7955 /* Ensure all method listed in the interface contain bodies. */
7956 check_methods (CLASS_CLS_METHODS (category),
7957 objc_implementation_context, '+');
7958 check_methods (CLASS_NST_METHODS (category),
7959 objc_implementation_context, '-');
7961 if (CLASS_PROTOCOL_LIST (category))
7962 check_protocols (CLASS_PROTOCOL_LIST (category),
7963 "category",
7964 CLASS_SUPER_NAME (objc_implementation_context));
7966 break;
7968 case CLASS_INTERFACE_TYPE:
7969 case CATEGORY_INTERFACE_TYPE:
7970 case PROTOCOL_INTERFACE_TYPE:
7972 /* Process properties of the class. */
7973 tree x;
7974 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7976 /* Now we check that the appropriate getter is declared,
7977 and if not, we declare one ourselves. */
7978 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7979 PROPERTY_GETTER_NAME (x));
7981 if (getter_decl)
7983 /* TODO: Check that the declaration is consistent with the property. */
7986 else
7988 /* Generate an instance method declaration for the
7989 getter; for example "- (id) name;". In general it
7990 will be of the form
7991 -(type)property_getter_name; */
7992 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
7993 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7994 rettype, PROPERTY_GETTER_NAME (x),
7995 NULL_TREE, false);
7996 if (PROPERTY_OPTIONAL (x))
7997 objc_add_method (objc_interface_context, getter_decl, false, true);
7998 else
7999 objc_add_method (objc_interface_context, getter_decl, false, false);
8000 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
8001 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
8004 if (PROPERTY_READONLY (x) == 0)
8006 /* Now we check that the appropriate setter is declared,
8007 and if not, we declare on ourselves. */
8008 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
8009 PROPERTY_SETTER_NAME (x));
8011 if (setter_decl)
8013 /* TODO: Check that the declaration is consistent with the property. */
8016 else
8018 /* The setter name is something like 'setName:'.
8019 We need the substring 'setName' to build the
8020 method declaration due to how the declaration
8021 works. TODO: build_method_decl() will then
8022 generate back 'setName:' from 'setName'; it
8023 would be more efficient to hook into there. */
8024 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8025 size_t length = strlen (full_setter_name);
8026 char *setter_name = (char *) alloca (length);
8027 tree ret_type, selector, arg_type, arg_name;
8029 strcpy (setter_name, full_setter_name);
8030 setter_name[length - 1] = '\0';
8031 ret_type = build_tree_list (NULL_TREE, void_type_node);
8032 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8033 arg_name = get_identifier ("_value");
8034 selector = objc_build_keyword_decl (get_identifier (setter_name),
8035 arg_type, arg_name, NULL);
8036 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
8037 ret_type, selector,
8038 build_tree_list (NULL_TREE, NULL_TREE),
8039 false);
8040 if (PROPERTY_OPTIONAL (x))
8041 objc_add_method (objc_interface_context, setter_decl, false, true);
8042 else
8043 objc_add_method (objc_interface_context, setter_decl, false, false);
8044 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8045 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
8049 break;
8051 default:
8052 gcc_unreachable ();
8053 break;
8057 static tree
8058 add_protocol (tree protocol)
8060 /* Put protocol on list in reverse order. */
8061 TREE_CHAIN (protocol) = protocol_chain;
8062 protocol_chain = protocol;
8063 return protocol_chain;
8066 /* Check that a protocol is defined, and, recursively, that all
8067 protocols that this protocol conforms to are defined too. */
8068 static void
8069 check_that_protocol_is_defined (tree protocol)
8071 if (!PROTOCOL_DEFINED (protocol))
8072 warning (0, "definition of protocol %qE not found",
8073 PROTOCOL_NAME (protocol));
8075 /* If the protocol itself conforms to other protocols, check them
8076 too, recursively. */
8077 if (PROTOCOL_LIST (protocol))
8079 tree p;
8081 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8082 check_that_protocol_is_defined (TREE_VALUE (p));
8086 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8087 emitted if the protocol is deprecated. If 'definition_required' is
8088 true, a warning is emitted if a full @protocol definition has not
8089 been seen. */
8090 static tree
8091 lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
8093 tree chain;
8095 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8096 if (ident == PROTOCOL_NAME (chain))
8098 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8100 /* It would be nice to use warn_deprecated_use() here, but
8101 we are using TREE_CHAIN (which is supposed to be the
8102 TYPE_STUB_DECL for a TYPE) for something different. */
8103 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
8104 PROTOCOL_NAME (chain));
8107 if (definition_required)
8108 check_that_protocol_is_defined (chain);
8110 return chain;
8113 return NULL_TREE;
8116 /* This function forward declares the protocols named by NAMES. If
8117 they are already declared or defined, the function has no effect. */
8119 void
8120 objc_declare_protocol (tree name, tree attributes)
8122 bool deprecated = false;
8124 #ifdef OBJCPLUS
8125 if (current_namespace != global_namespace) {
8126 error ("Objective-C declarations may only appear in global scope");
8128 #endif /* OBJCPLUS */
8130 /* Determine if 'deprecated', the only attribute we recognize for
8131 protocols, was used. Ignore all other attributes. */
8132 if (attributes)
8134 tree attribute;
8135 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8137 tree name = TREE_PURPOSE (attribute);
8139 if (is_attribute_p ("deprecated", name))
8140 deprecated = true;
8141 else
8142 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8146 if (lookup_protocol (name, /* warn if deprecated */ false,
8147 /* definition_required */ false) == NULL_TREE)
8149 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
8151 TYPE_LANG_SLOT_1 (protocol)
8152 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8153 PROTOCOL_NAME (protocol) = name;
8154 PROTOCOL_LIST (protocol) = NULL_TREE;
8155 add_protocol (protocol);
8156 PROTOCOL_DEFINED (protocol) = 0;
8157 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8159 if (attributes)
8161 /* TODO: Do we need to store the attributes here ? */
8162 TYPE_ATTRIBUTES (protocol) = attributes;
8163 if (deprecated)
8164 TREE_DEPRECATED (protocol) = 1;
8169 static tree
8170 start_protocol (enum tree_code code, tree name, tree list, tree attributes)
8172 tree protocol;
8173 bool deprecated = false;
8175 #ifdef OBJCPLUS
8176 if (current_namespace != global_namespace) {
8177 error ("Objective-C declarations may only appear in global scope");
8179 #endif /* OBJCPLUS */
8181 /* Determine if 'deprecated', the only attribute we recognize for
8182 protocols, was used. Ignore all other attributes. */
8183 if (attributes)
8185 tree attribute;
8186 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8188 tree name = TREE_PURPOSE (attribute);
8190 if (is_attribute_p ("deprecated", name))
8191 deprecated = true;
8192 else
8193 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8197 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8198 /* definition_required */ false);
8200 if (!protocol)
8202 protocol = make_node (code);
8203 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8205 PROTOCOL_NAME (protocol) = name;
8206 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8207 add_protocol (protocol);
8208 PROTOCOL_DEFINED (protocol) = 1;
8209 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
8211 check_protocol_recursively (protocol, list);
8213 else if (! PROTOCOL_DEFINED (protocol))
8215 PROTOCOL_DEFINED (protocol) = 1;
8216 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8218 check_protocol_recursively (protocol, list);
8220 else
8222 warning (0, "duplicate declaration for protocol %qE",
8223 name);
8226 if (attributes)
8228 TYPE_ATTRIBUTES (protocol) = attributes;
8229 if (deprecated)
8230 TREE_DEPRECATED (protocol) = 1;
8233 return protocol;
8236 /* Decay array and function parameters into pointers. */
8238 static tree
8239 objc_decay_parm_type (tree type)
8241 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8242 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8243 ? TREE_TYPE (type)
8244 : type);
8246 return type;
8249 static GTY(()) tree objc_parmlist = NULL_TREE;
8251 /* Append PARM to a list of formal parameters of a method, making a necessary
8252 array-to-pointer adjustment along the way. */
8254 void
8255 objc_push_parm (tree parm)
8257 tree type;
8259 if (TREE_TYPE (parm) == error_mark_node)
8261 objc_parmlist = chainon (objc_parmlist, parm);
8262 return;
8265 /* Decay arrays and functions into pointers. */
8266 type = objc_decay_parm_type (TREE_TYPE (parm));
8268 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8269 built as well. */
8270 if (type != TREE_TYPE (parm))
8271 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
8273 DECL_ARG_TYPE (parm)
8274 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
8276 /* Record constancy and volatility. */
8277 c_apply_type_quals_to_decl
8278 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8279 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
8280 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
8281 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
8283 objc_parmlist = chainon (objc_parmlist, parm);
8286 /* Retrieve the formal parameter list constructed via preceding calls to
8287 objc_push_parm(). */
8289 #ifdef OBJCPLUS
8290 tree
8291 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8292 tree expr ATTRIBUTE_UNUSED)
8294 tree parm_info = objc_parmlist;
8295 objc_parmlist = NULL_TREE;
8297 return parm_info;
8299 #else
8300 struct c_arg_info *
8301 objc_get_parm_info (int have_ellipsis, tree expr)
8303 tree parm_info = objc_parmlist;
8304 struct c_arg_info *arg_info;
8305 /* The C front-end requires an elaborate song and dance at
8306 this point. */
8307 push_scope ();
8308 declare_parm_level ();
8309 while (parm_info)
8311 tree next = DECL_CHAIN (parm_info);
8313 DECL_CHAIN (parm_info) = NULL_TREE;
8314 parm_info = pushdecl (parm_info);
8315 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8316 parm_info = next;
8318 arg_info = get_parm_info (have_ellipsis, expr);
8319 pop_scope ();
8320 objc_parmlist = NULL_TREE;
8321 return arg_info;
8323 #endif
8325 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8326 method definitions. In the case of instance methods, we can be more
8327 specific as to the type of 'self'. */
8329 static void
8330 synth_self_and_ucmd_args (void)
8332 tree self_type;
8334 if (objc_method_context
8335 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8336 self_type = objc_instance_type;
8337 else
8338 /* Really a `struct objc_class *'. However, we allow people to
8339 assign to self, which changes its type midstream. */
8340 self_type = objc_object_type;
8342 /* id self; */
8343 objc_push_parm (build_decl (input_location,
8344 PARM_DECL, self_id, self_type));
8346 /* SEL _cmd; */
8347 objc_push_parm (build_decl (input_location,
8348 PARM_DECL, ucmd_id, objc_selector_type));
8351 /* Transform an Objective-C method definition into a static C function
8352 definition, synthesizing the first two arguments, "self" and "_cmd",
8353 in the process. EXPR is NULL or an expression that needs to be
8354 evaluated for the side effects of array size expressions in the
8355 parameters. */
8357 static void
8358 start_method_def (tree method, tree expr)
8360 tree parmlist;
8361 #ifdef OBJCPLUS
8362 tree parm_info;
8363 #else
8364 struct c_arg_info *parm_info;
8365 #endif
8366 int have_ellipsis = 0;
8368 /* If we are defining a "dealloc" method in a non-root class, we
8369 will need to check if a [super dealloc] is missing, and warn if
8370 it is. */
8371 if(CLASS_SUPER_NAME (objc_implementation_context)
8372 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8373 should_call_super_dealloc = 1;
8374 else
8375 should_call_super_dealloc = 0;
8377 /* Required to implement _msgSuper. */
8378 objc_method_context = method;
8379 UOBJC_SUPER_decl = NULL_TREE;
8381 /* Generate prototype declarations for arguments..."new-style". */
8382 synth_self_and_ucmd_args ();
8384 /* Generate argument declarations if a keyword_decl. */
8385 parmlist = METHOD_SEL_ARGS (method);
8386 while (parmlist)
8388 /* parmlist is a KEYWORD_DECL. */
8389 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8390 tree parm;
8392 parm = build_decl (input_location,
8393 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8394 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8395 objc_push_parm (parm);
8396 parmlist = DECL_CHAIN (parmlist);
8399 if (METHOD_ADD_ARGS (method))
8401 tree akey;
8403 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8404 akey; akey = TREE_CHAIN (akey))
8406 objc_push_parm (TREE_VALUE (akey));
8409 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8410 have_ellipsis = 1;
8413 parm_info = objc_get_parm_info (have_ellipsis, expr);
8415 really_start_method (objc_method_context, parm_info);
8418 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8419 overloading. */
8420 static int
8421 objc_types_are_equivalent (tree type1, tree type2)
8423 if (type1 == type2)
8424 return 1;
8426 /* Strip away indirections. */
8427 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8428 && (TREE_CODE (type1) == TREE_CODE (type2)))
8429 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8430 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8431 return 0;
8433 /* Compare the protocol lists. */
8434 type1 = (TYPE_HAS_OBJC_INFO (type1)
8435 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8436 : NULL_TREE);
8437 type2 = (TYPE_HAS_OBJC_INFO (type2)
8438 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8439 : NULL_TREE);
8441 /* If there are no protocols (most common case), the types are
8442 identical. */
8443 if (type1 == NULL_TREE && type2 == NULL_TREE)
8444 return 1;
8446 /* If one has protocols, and the other one hasn't, they are not
8447 identical. */
8448 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8449 || (type1 != NULL_TREE && type2 == NULL_TREE))
8450 return 0;
8451 else
8453 /* Else, both have protocols, and we need to do the full
8454 comparison. It is possible that either type1 or type2
8455 contain some duplicate protocols in the list, so we can't
8456 even just compare list_length as a first check. */
8457 tree t;
8459 for (t = type2; t; t = TREE_CHAIN (t))
8460 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8461 return 0;
8463 for (t = type1; t; t = TREE_CHAIN (t))
8464 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8465 return 0;
8467 return 1;
8471 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8473 static int
8474 objc_types_share_size_and_alignment (tree type1, tree type2)
8476 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8477 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8480 /* Return 1 if PROTO1 is equivalent to PROTO2
8481 for purposes of method overloading. Ordinarily, the type signatures
8482 should match up exactly, unless STRICT is zero, in which case we
8483 shall allow differences in which the size and alignment of a type
8484 is the same. */
8486 static int
8487 comp_proto_with_proto (tree proto1, tree proto2, int strict)
8489 tree type1, type2;
8491 /* The following test is needed in case there are hashing
8492 collisions. */
8493 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8494 return 0;
8496 /* Compare return types. */
8497 type1 = TREE_VALUE (TREE_TYPE (proto1));
8498 type2 = TREE_VALUE (TREE_TYPE (proto2));
8500 if (!objc_types_are_equivalent (type1, type2)
8501 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8502 return 0;
8504 /* Compare argument types. */
8506 /* The first argument (objc_object_type) is always the same, no need
8507 to compare. */
8509 /* The second argument (objc_selector_type) is always the same, no
8510 need to compare. */
8512 /* Compare the other arguments. */
8514 tree arg1, arg2;
8516 /* Compare METHOD_SEL_ARGS. */
8517 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8518 arg1 && arg2;
8519 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8521 type1 = TREE_VALUE (TREE_TYPE (arg1));
8522 type2 = TREE_VALUE (TREE_TYPE (arg2));
8524 /* FIXME: Do we need to decay argument types to compare them ? */
8525 type1 = objc_decay_parm_type (type1);
8526 type2 = objc_decay_parm_type (type2);
8528 if (!objc_types_are_equivalent (type1, type2)
8529 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8530 return 0;
8533 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8534 both NULL. */
8535 if (arg1 != arg2)
8536 return 0;
8538 /* Compare METHOD_ADD_ARGS. */
8539 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8540 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8541 return 0;
8543 if (METHOD_ADD_ARGS (proto1))
8545 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8546 arg1 && arg2;
8547 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8549 type1 = TREE_TYPE (TREE_VALUE (arg1));
8550 type2 = TREE_TYPE (TREE_VALUE (arg2));
8552 /* FIXME: Do we need to decay argument types to compare them ? */
8553 type1 = objc_decay_parm_type (type1);
8554 type2 = objc_decay_parm_type (type2);
8556 if (!objc_types_are_equivalent (type1, type2)
8557 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8558 return 0;
8562 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8563 both NULL. */
8564 if (arg1 != arg2)
8565 return 0;
8567 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8568 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8569 return 0;
8572 /* Success. */
8573 return 1;
8576 /* This routine returns true if TYPE is a valid objc object type,
8577 suitable for messaging; false otherwise. If 'accept_class' is
8578 'true', then a Class object is considered valid for messaging and
8579 'true' is returned if 'type' refers to a Class. If 'accept_class'
8580 is 'false', then a Class object is not considered valid for
8581 messaging and 'false' is returned in that case. */
8583 static bool
8584 objc_type_valid_for_messaging (tree type, bool accept_classes)
8586 if (!POINTER_TYPE_P (type))
8587 return false;
8589 /* Remove the pointer indirection; don't remove more than one
8590 otherwise we'd consider "NSObject **" a valid type for messaging,
8591 which it isn't. */
8592 type = TREE_TYPE (type);
8594 if (TREE_CODE (type) != RECORD_TYPE)
8595 return false;
8597 if (objc_is_object_id (type))
8598 return true;
8600 if (objc_is_class_id (type))
8601 return accept_classes;
8603 if (TYPE_HAS_OBJC_INFO (type))
8604 return true;
8606 return false;
8609 void
8610 objc_start_function (tree name, tree type, tree attrs,
8611 #ifdef OBJCPLUS
8612 tree params
8613 #else
8614 struct c_arg_info *params
8615 #endif
8618 tree fndecl = build_decl (input_location,
8619 FUNCTION_DECL, name, type);
8621 #ifdef OBJCPLUS
8622 DECL_ARGUMENTS (fndecl) = params;
8623 DECL_INITIAL (fndecl) = error_mark_node;
8624 DECL_EXTERNAL (fndecl) = 0;
8625 TREE_STATIC (fndecl) = 1;
8626 retrofit_lang_decl (fndecl);
8627 cplus_decl_attributes (&fndecl, attrs, 0);
8628 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8629 #else
8630 current_function_returns_value = 0; /* Assume, until we see it does. */
8631 current_function_returns_null = 0;
8632 decl_attributes (&fndecl, attrs, 0);
8633 announce_function (fndecl);
8634 DECL_INITIAL (fndecl) = error_mark_node;
8635 DECL_EXTERNAL (fndecl) = 0;
8636 TREE_STATIC (fndecl) = 1;
8637 current_function_decl = pushdecl (fndecl);
8638 push_scope ();
8639 declare_parm_level ();
8640 DECL_RESULT (current_function_decl)
8641 = build_decl (input_location,
8642 RESULT_DECL, NULL_TREE,
8643 TREE_TYPE (TREE_TYPE (current_function_decl)));
8644 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8645 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8646 start_fname_decls ();
8647 store_parm_decls_from (params);
8648 #endif
8650 TREE_USED (current_function_decl) = 1;
8653 /* - Generate an identifier for the function. the format is "_n_cls",
8654 where 1 <= n <= nMethods, and cls is the name the implementation we
8655 are processing.
8656 - Install the return type from the method declaration.
8657 - If we have a prototype, check for type consistency. */
8659 static void
8660 really_start_method (tree method,
8661 #ifdef OBJCPLUS
8662 tree parmlist
8663 #else
8664 struct c_arg_info *parmlist
8665 #endif
8668 tree ret_type, meth_type;
8669 tree method_id;
8670 const char *sel_name, *class_name, *cat_name;
8671 char *buf;
8673 /* Synth the storage class & assemble the return type. */
8674 ret_type = TREE_VALUE (TREE_TYPE (method));
8676 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8677 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8678 cat_name = ((TREE_CODE (objc_implementation_context)
8679 == CLASS_IMPLEMENTATION_TYPE)
8680 ? NULL
8681 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8682 method_slot++;
8684 /* Make sure this is big enough for any plausible method label. */
8685 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8686 + (cat_name ? strlen (cat_name) : 0));
8688 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8689 class_name, cat_name, sel_name, method_slot);
8691 method_id = get_identifier (buf);
8693 #ifdef OBJCPLUS
8694 /* Objective-C methods cannot be overloaded, so we don't need
8695 the type encoding appended. It looks bad anyway... */
8696 push_lang_context (lang_name_c);
8697 #endif
8699 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
8700 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
8702 /* Set self_decl from the first argument. */
8703 self_decl = DECL_ARGUMENTS (current_function_decl);
8705 /* Suppress unused warnings. */
8706 TREE_USED (self_decl) = 1;
8707 DECL_READ_P (self_decl) = 1;
8708 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8709 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8710 #ifdef OBJCPLUS
8711 pop_lang_context ();
8712 #endif
8714 METHOD_DEFINITION (method) = current_function_decl;
8716 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8718 if (implementation_template != objc_implementation_context)
8720 tree proto
8721 = lookup_method_static (implementation_template,
8722 METHOD_SEL_NAME (method),
8723 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8724 | OBJC_LOOKUP_NO_SUPER));
8726 if (proto)
8728 if (!comp_proto_with_proto (method, proto, 1))
8730 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
8732 warning_at (DECL_SOURCE_LOCATION (method), 0,
8733 "conflicting types for %<%c%s%>",
8734 (type ? '-' : '+'),
8735 identifier_to_locale (gen_method_decl (method)));
8736 inform (DECL_SOURCE_LOCATION (proto),
8737 "previous declaration of %<%c%s%>",
8738 (type ? '-' : '+'),
8739 identifier_to_locale (gen_method_decl (proto)));
8741 else
8743 /* If the method in the @interface was deprecated, mark
8744 the implemented method as deprecated too. It should
8745 never be used for messaging (when the deprecation
8746 warnings are produced), but just in case. */
8747 if (TREE_DEPRECATED (proto))
8748 TREE_DEPRECATED (method) = 1;
8750 /* If the method in the @interface was marked as
8751 'noreturn', mark the function implementing the method
8752 as 'noreturn' too. */
8753 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8756 else
8758 /* We have a method @implementation even though we did not
8759 see a corresponding @interface declaration (which is allowed
8760 by Objective-C rules). Go ahead and place the method in
8761 the @interface anyway, so that message dispatch lookups
8762 will see it. */
8763 tree interface = implementation_template;
8765 if (TREE_CODE (objc_implementation_context)
8766 == CATEGORY_IMPLEMENTATION_TYPE)
8767 interface = lookup_category
8768 (interface,
8769 CLASS_SUPER_NAME (objc_implementation_context));
8771 if (interface)
8772 objc_add_method (interface, copy_node (method),
8773 TREE_CODE (method) == CLASS_METHOD_DECL,
8774 /* is_optional= */ false);
8779 static void *UOBJC_SUPER_scope = 0;
8781 /* _n_Method (id self, SEL sel, ...)
8783 struct objc_super _S;
8784 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8785 } */
8787 static tree
8788 get_super_receiver (void)
8790 if (objc_method_context)
8792 tree super_expr, super_expr_list, class_expr;
8793 bool inst_meth;
8794 if (!UOBJC_SUPER_decl)
8796 UOBJC_SUPER_decl = build_decl (input_location,
8797 VAR_DECL, get_identifier (TAG_SUPER),
8798 objc_super_template);
8799 /* This prevents `unused variable' warnings when compiling with -Wall. */
8800 TREE_USED (UOBJC_SUPER_decl) = 1;
8801 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8802 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8803 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
8804 NULL_TREE);
8805 UOBJC_SUPER_scope = objc_get_current_scope ();
8808 /* Set receiver to self. */
8809 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8810 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8811 NOP_EXPR, input_location, self_decl,
8812 NULL_TREE);
8813 super_expr_list = super_expr;
8815 /* Set class to begin searching. */
8816 /* Get the ident for the superclass class field & build a ref to it.
8817 ??? maybe we should just name the field the same for all runtimes. */
8818 super_expr = (*runtime.super_superclassfield_ident) ();
8819 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
8821 gcc_assert (imp_list->imp_context == objc_implementation_context
8822 && imp_list->imp_template == implementation_template);
8823 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8825 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
8826 class_expr = (*runtime.get_class_super_ref) (input_location,
8827 imp_list, inst_meth);
8828 else
8829 /* We have a category. */
8831 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8832 tree super_class;
8834 /* Barf if super used in a category of a root object. */
8835 if (!super_name)
8837 error ("no super class declared in interface for %qE",
8838 CLASS_NAME (imp_list->imp_template));
8839 return error_mark_node;
8842 super_class = (*runtime.get_category_super_ref) (input_location,
8843 imp_list, inst_meth);
8844 class_expr = build_c_cast (input_location,
8845 TREE_TYPE (super_expr), super_class);
8848 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8849 NOP_EXPR,
8850 input_location, class_expr, NULL_TREE);
8852 super_expr_list = build_compound_expr (input_location,
8853 super_expr_list, super_expr);
8855 super_expr = build_unary_op (input_location,
8856 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8857 super_expr_list = build_compound_expr (input_location,
8858 super_expr_list, super_expr);
8860 return super_expr_list;
8862 else
8864 error ("[super ...] must appear in a method context");
8865 return error_mark_node;
8869 /* When exiting a scope, sever links to a 'super' declaration (if any)
8870 therein contained. */
8872 void
8873 objc_clear_super_receiver (void)
8875 if (objc_method_context
8876 && UOBJC_SUPER_scope == objc_get_current_scope ())
8878 UOBJC_SUPER_decl = 0;
8879 UOBJC_SUPER_scope = 0;
8883 void
8884 objc_finish_method_definition (tree fndecl)
8886 /* We cannot validly inline ObjC methods, at least not without a language
8887 extension to declare that a method need not be dynamically
8888 dispatched, so suppress all thoughts of doing so. */
8889 DECL_UNINLINABLE (fndecl) = 1;
8891 #ifndef OBJCPLUS
8892 /* The C++ front-end will have called finish_function() for us. */
8893 finish_function ();
8894 #endif
8896 METHOD_ENCODING (objc_method_context)
8897 = encode_method_prototype (objc_method_context);
8899 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8900 since the optimizer may find "may be used before set" errors. */
8901 objc_method_context = NULL_TREE;
8903 if (should_call_super_dealloc)
8904 warning (0, "method possibly missing a [super dealloc] call");
8907 /* Given a tree DECL node, produce a printable description of it in the given
8908 buffer, overwriting the buffer. */
8910 static char *
8911 gen_declaration (tree decl)
8913 errbuf[0] = '\0';
8915 if (DECL_P (decl))
8917 gen_type_name_0 (TREE_TYPE (decl));
8919 if (DECL_NAME (decl))
8921 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8922 strcat (errbuf, " ");
8924 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
8927 if (DECL_INITIAL (decl)
8928 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
8929 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8930 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
8933 return errbuf;
8936 /* Given a tree TYPE node, produce a printable description of it in the given
8937 buffer, overwriting the buffer. */
8939 static char *
8940 gen_type_name_0 (tree type)
8942 tree orig = type, proto;
8944 if (TYPE_P (type) && TYPE_NAME (type))
8945 type = TYPE_NAME (type);
8946 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
8948 tree inner = TREE_TYPE (type);
8950 while (TREE_CODE (inner) == ARRAY_TYPE)
8951 inner = TREE_TYPE (inner);
8953 gen_type_name_0 (inner);
8955 if (!POINTER_TYPE_P (inner))
8956 strcat (errbuf, " ");
8958 if (POINTER_TYPE_P (type))
8959 strcat (errbuf, "*");
8960 else
8961 while (type != inner)
8963 strcat (errbuf, "[");
8965 if (TYPE_DOMAIN (type))
8967 char sz[20];
8969 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8970 (TREE_INT_CST_LOW
8971 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8972 strcat (errbuf, sz);
8975 strcat (errbuf, "]");
8976 type = TREE_TYPE (type);
8979 goto exit_function;
8982 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8983 type = DECL_NAME (type);
8985 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
8986 ? IDENTIFIER_POINTER (type)
8987 : "");
8989 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8990 if (objc_is_id (orig))
8991 orig = TREE_TYPE (orig);
8993 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8995 if (proto)
8997 strcat (errbuf, " <");
8999 while (proto) {
9000 strcat (errbuf,
9001 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
9002 proto = TREE_CHAIN (proto);
9003 strcat (errbuf, proto ? ", " : ">");
9007 exit_function:
9008 return errbuf;
9011 static char *
9012 gen_type_name (tree type)
9014 errbuf[0] = '\0';
9016 return gen_type_name_0 (type);
9019 /* Given a method tree, put a printable description into the given
9020 buffer (overwriting) and return a pointer to the buffer. */
9022 static char *
9023 gen_method_decl (tree method)
9025 tree chain;
9027 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9028 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9029 strcat (errbuf, ")");
9030 chain = METHOD_SEL_ARGS (method);
9032 if (chain)
9034 /* We have a chain of keyword_decls. */
9037 if (KEYWORD_KEY_NAME (chain))
9038 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9040 strcat (errbuf, ":(");
9041 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9042 strcat (errbuf, ")");
9044 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9045 if ((chain = DECL_CHAIN (chain)))
9046 strcat (errbuf, " ");
9048 while (chain);
9050 if (METHOD_ADD_ARGS (method))
9052 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9054 /* Know we have a chain of parm_decls. */
9055 while (chain)
9057 strcat (errbuf, ", ");
9058 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9059 chain = TREE_CHAIN (chain);
9062 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9063 strcat (errbuf, ", ...");
9067 else
9068 /* We have a unary selector. */
9069 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9071 return errbuf;
9074 /* Debug info. */
9077 /* Dump an @interface declaration of the supplied class CHAIN to the
9078 supplied file FP. Used to implement the -gen-decls option (which
9079 prints out an @interface declaration of all classes compiled in
9080 this run); potentially useful for debugging the compiler too. */
9081 void
9082 dump_interface (FILE *fp, tree chain)
9084 /* FIXME: A heap overflow here whenever a method (or ivar)
9085 declaration is so long that it doesn't fit in the buffer. The
9086 code and all the related functions should be rewritten to avoid
9087 using fixed size buffers. */
9088 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9089 tree ivar_decls = CLASS_RAW_IVARS (chain);
9090 tree nst_methods = CLASS_NST_METHODS (chain);
9091 tree cls_methods = CLASS_CLS_METHODS (chain);
9093 fprintf (fp, "\n@interface %s", my_name);
9095 /* CLASS_SUPER_NAME is used to store the superclass name for
9096 classes, and the category name for categories. */
9097 if (CLASS_SUPER_NAME (chain))
9099 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
9101 switch (TREE_CODE (chain))
9103 case CATEGORY_IMPLEMENTATION_TYPE:
9104 case CATEGORY_INTERFACE_TYPE:
9105 fprintf (fp, " (%s)\n", name);
9106 break;
9107 default:
9108 fprintf (fp, " : %s\n", name);
9109 break;
9112 else
9113 fprintf (fp, "\n");
9115 /* FIXME - the following doesn't seem to work at the moment. */
9116 if (ivar_decls)
9118 fprintf (fp, "{\n");
9121 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9122 ivar_decls = TREE_CHAIN (ivar_decls);
9124 while (ivar_decls);
9125 fprintf (fp, "}\n");
9128 while (nst_methods)
9130 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9131 nst_methods = TREE_CHAIN (nst_methods);
9134 while (cls_methods)
9136 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9137 cls_methods = TREE_CHAIN (cls_methods);
9140 fprintf (fp, "@end\n");
9143 #if 0
9144 /* Produce the pretty printing for an Objective-C method. This is
9145 currently unused, but could be handy while reorganizing the pretty
9146 printing to be more robust. */
9147 static const char *
9148 objc_pretty_print_method (bool is_class_method,
9149 const char *class_name,
9150 const char *category_name,
9151 const char *selector)
9153 if (category_name)
9155 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
9156 + strlen (selector) + 7);
9158 if (is_class_method)
9159 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9160 else
9161 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
9163 return result;
9165 else
9167 char *result = XNEWVEC (char, strlen (class_name)
9168 + strlen (selector) + 5);
9170 if (is_class_method)
9171 sprintf (result, "+[%s %s]", class_name, selector);
9172 else
9173 sprintf (result, "-[%s %s]", class_name, selector);
9175 return result;
9178 #endif
9180 /* Demangle function for Objective-C. Attempt to demangle the
9181 function name associated with a method (eg, going from
9182 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9183 purpose of pretty printing or error messages. Return the demangled
9184 name, or NULL if the string is not an Objective-C mangled method
9185 name.
9187 Because of how the mangling is done, any method that has a '_' in
9188 its original name is at risk of being demangled incorrectly. In
9189 some cases there are multiple valid ways to demangle a method name
9190 and there is no way we can decide.
9192 TODO: objc_demangle() can't always get it right; the right way to
9193 get this correct for all method names would be to store the
9194 Objective-C method name somewhere in the function decl. Then,
9195 there is no demangling to do; we'd just pull the method name out of
9196 the decl. As an additional bonus, when printing error messages we
9197 could check for such a method name, and if we find it, we know the
9198 function is actually an Objective-C method and we could print error
9199 messages saying "In method '+[NSObject class]" instead of "In
9200 function '+[NSObject class]" as we do now. */
9201 static const char *
9202 objc_demangle (const char *mangled)
9204 char *demangled, *cp;
9206 /* First of all, if the name is too short it can't be an Objective-C
9207 mangled method name. */
9208 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9209 return NULL;
9211 /* If the name looks like an already demangled one, return it
9212 unchanged. This should only happen on Darwin, where method names
9213 are mangled differently into a pretty-print form (such as
9214 '+[NSObject class]', see darwin.h). In that case, demangling is
9215 a no-op, but we need to return the demangled name if it was an
9216 ObjC one, and return NULL if not. We should be safe as no C/C++
9217 function can start with "-[" or "+[". */
9218 if ((mangled[0] == '-' || mangled[0] == '+')
9219 && (mangled[1] == '['))
9220 return mangled;
9222 if (mangled[0] == '_' &&
9223 (mangled[1] == 'i' || mangled[1] == 'c') &&
9224 mangled[2] == '_')
9226 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9227 if (mangled[1] == 'i')
9228 *cp++ = '-'; /* for instance method */
9229 else
9230 *cp++ = '+'; /* for class method */
9231 *cp++ = '['; /* opening left brace */
9232 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9233 while (*cp && *cp == '_')
9234 cp++; /* skip any initial underbars in class name */
9235 cp = strchr(cp, '_'); /* find first non-initial underbar */
9236 if (cp == NULL)
9238 free(demangled); /* not mangled name */
9239 return NULL;
9241 if (cp[1] == '_') /* easy case: no category name */
9243 *cp++ = ' '; /* replace two '_' with one ' ' */
9244 strcpy(cp, mangled + (cp - demangled) + 2);
9246 else
9248 *cp++ = '('; /* less easy case: category name */
9249 cp = strchr(cp, '_');
9250 if (cp == 0)
9252 free(demangled); /* not mangled name */
9253 return NULL;
9255 *cp++ = ')';
9256 *cp++ = ' '; /* overwriting 1st char of method name... */
9257 strcpy(cp, mangled + (cp - demangled)); /* get it back */
9259 /* Now we have the method name. We need to generally replace
9260 '_' with ':' but trying to preserve '_' if it could only have
9261 been in the mangled string because it was already in the
9262 original name. In cases where it's ambiguous, we assume that
9263 any '_' originated from a ':'. */
9265 /* Initial '_'s in method name can't have been generating by
9266 converting ':'s. Skip them. */
9267 while (*cp && *cp == '_')
9268 cp++;
9270 /* If the method name does not end with '_', then it has no
9271 arguments and there was no replacement of ':'s with '_'s
9272 during mangling. Check for that case, and skip any
9273 replacement if so. This at least guarantees that methods
9274 with no arguments are always demangled correctly (unless the
9275 original name ends with '_'). */
9276 if (*(mangled + strlen (mangled) - 1) != '_')
9278 /* Skip to the end. */
9279 for (; *cp; cp++)
9282 else
9284 /* Replace remaining '_' with ':'. This may get it wrong if
9285 there were '_'s in the original name. In most cases it
9286 is impossible to disambiguate. */
9287 for (; *cp; cp++)
9288 if (*cp == '_')
9289 *cp = ':';
9291 *cp++ = ']'; /* closing right brace */
9292 *cp++ = 0; /* string terminator */
9293 return demangled;
9295 else
9296 return NULL; /* not an objc mangled name */
9299 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9300 specific decl, return the printable name for it. If not, return
9301 NULL. */
9302 const char *
9303 objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9305 switch (TREE_CODE (decl))
9307 case FUNCTION_DECL:
9308 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
9309 break;
9311 /* The following happens when we are printing a deprecation
9312 warning for a method. The warn_deprecation() will end up
9313 trying to print the decl for INSTANCE_METHOD_DECL or
9314 CLASS_METHOD_DECL. It would be nice to be able to print
9315 "-[NSObject autorelease] is deprecated", but to do that, we'd
9316 need to store the class and method name in the method decl,
9317 which we currently don't do. For now, just return the name
9318 of the method. We don't return NULL, because that may
9319 trigger further attempts to pretty-print the decl in C/C++,
9320 but they wouldn't know how to pretty-print it. */
9321 case INSTANCE_METHOD_DECL:
9322 case CLASS_METHOD_DECL:
9323 return IDENTIFIER_POINTER (DECL_NAME (decl));
9324 break;
9325 /* This happens when printing a deprecation warning for a
9326 property. We may want to consider some sort of pretty
9327 printing (eg, include the class name where it was declared
9328 ?). */
9329 case PROPERTY_DECL:
9330 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
9331 break;
9332 default:
9333 return NULL;
9334 break;
9338 /* Return a printable name for 'decl'. This first tries
9339 objc_maybe_printable_name(), and if that fails, it returns the name
9340 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9341 Objective-C; in Objective-C++, setting the hook is not enough
9342 because lots of C++ Front-End code calls cxx_printable_name,
9343 dump_decl and other C++ functions directly. So instead we have
9344 modified dump_decl to call objc_maybe_printable_name directly. */
9345 const char *
9346 objc_printable_name (tree decl, int v)
9348 const char *demangled_name = objc_maybe_printable_name (decl, v);
9350 if (demangled_name != NULL)
9351 return demangled_name;
9352 else
9353 return IDENTIFIER_POINTER (DECL_NAME (decl));
9356 /* Routine is called to issue diagnostic when reference to a private
9357 ivar is made and no other variable with same name is found in
9358 current scope. */
9359 bool
9360 objc_diagnose_private_ivar (tree id)
9362 tree ivar;
9363 if (!objc_method_context)
9364 return false;
9365 ivar = is_ivar (objc_ivar_chain, id);
9366 if (ivar && is_private (ivar))
9368 error ("instance variable %qs is declared private",
9369 IDENTIFIER_POINTER (id));
9370 return true;
9372 return false;
9375 /* Look up ID as an instance variable. OTHER contains the result of
9376 the C or C++ lookup, which we may want to use instead. */
9377 /* To use properties inside an instance method, use self.property. */
9378 tree
9379 objc_lookup_ivar (tree other, tree id)
9381 tree ivar;
9383 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9384 if (!objc_method_context)
9385 return other;
9387 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9388 /* We have a message to super. */
9389 return get_super_receiver ();
9391 /* In a class method, look up an instance variable only as a last
9392 resort. */
9393 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9394 && other && other != error_mark_node)
9395 return other;
9397 /* Don't look up the ivar if the user has explicitly advised against
9398 it with -fno-local-ivars. */
9400 if (!flag_local_ivars)
9401 return other;
9403 /* Look up the ivar, but do not use it if it is not accessible. */
9404 ivar = is_ivar (objc_ivar_chain, id);
9406 if (!ivar || is_private (ivar))
9407 return other;
9409 /* In an instance method, a local variable (or parameter) may hide the
9410 instance variable. */
9411 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9412 && other && other != error_mark_node
9413 #ifdef OBJCPLUS
9414 && CP_DECL_CONTEXT (other) != global_namespace)
9415 #else
9416 && !DECL_FILE_SCOPE_P (other))
9417 #endif
9419 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9420 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9421 "local declaration of %qE hides instance variable", id);
9424 return other;
9427 /* At this point, we are either in an instance method with no obscuring
9428 local definitions, or in a class method with no alternate definitions
9429 at all. */
9430 return build_ivar_reference (id);
9433 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9434 needs to be done if we are calling a function through a cast. */
9436 tree
9437 objc_rewrite_function_call (tree function, tree first_param)
9439 if (TREE_CODE (function) == NOP_EXPR
9440 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9441 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9442 == FUNCTION_DECL)
9444 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9445 TREE_OPERAND (function, 0),
9446 first_param, size_zero_node);
9449 return function;
9452 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9453 corresponding 'getter' function call. Note that we assume the
9454 PROPERTY_REF to be valid since we generated it while parsing. */
9455 static void
9456 objc_gimplify_property_ref (tree *expr_p)
9458 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9459 tree call_exp;
9461 if (getter == NULL_TREE)
9463 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9464 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9465 should be impossible for real properties, which always
9466 have a getter. */
9467 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9468 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9469 /* Try to recover from the error to prevent an ICE. We take
9470 zero and cast it to the type of the property. */
9471 *expr_p = convert (TREE_TYPE (property_decl),
9472 integer_zero_node);
9473 return;
9476 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9478 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9479 that is deprecated. */
9480 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9481 NULL_TREE);
9484 call_exp = getter;
9485 #ifdef OBJCPLUS
9486 /* In C++, a getter which returns an aggregate value results in a
9487 target_expr which initializes a temporary to the call
9488 expression. */
9489 if (TREE_CODE (getter) == TARGET_EXPR)
9491 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9492 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9493 call_exp = TREE_OPERAND (getter, 1);
9495 #endif
9496 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
9498 *expr_p = call_exp;
9501 /* This is called when "gimplifying" the trees. We need to gimplify
9502 the Objective-C/Objective-C++ specific trees, then hand over the
9503 process to C/C++. */
9505 objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9507 enum tree_code code = TREE_CODE (*expr_p);
9508 switch (code)
9510 /* Look for the special case of OBJC_TYPE_REF with the address
9511 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9512 or one of its cousins). */
9513 case OBJ_TYPE_REF:
9514 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9515 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9516 == FUNCTION_DECL)
9518 enum gimplify_status r0, r1;
9520 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9521 value of the OBJ_TYPE_REF, so force them to be emitted
9522 during subexpression evaluation rather than after the
9523 OBJ_TYPE_REF. This permits objc_msgSend calls in
9524 Objective C to use direct rather than indirect calls when
9525 the object expression has a postincrement. */
9526 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9527 is_gimple_val, fb_rvalue);
9528 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9529 is_gimple_val, fb_rvalue);
9531 return MIN (r0, r1);
9533 break;
9534 case PROPERTY_REF:
9535 objc_gimplify_property_ref (expr_p);
9536 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9537 break;
9538 default:
9539 break;
9542 #ifdef OBJCPLUS
9543 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
9544 #else
9545 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
9546 #endif
9549 /* --- FAST ENUMERATION --- */
9550 /* Begin code generation for fast enumeration (foreach) ... */
9552 /* Defines
9554 struct __objcFastEnumerationState
9556 unsigned long state;
9557 id *itemsPtr;
9558 unsigned long *mutationsPtr;
9559 unsigned long extra[5];
9562 Confusingly enough, NSFastEnumeration is then defined by libraries
9563 to be the same structure.
9566 static void
9567 build_fast_enumeration_state_template (void)
9569 tree decls, *chain = NULL;
9571 /* { */
9572 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
9573 (TAG_FAST_ENUMERATION_STATE));
9575 /* unsigned long state; */
9576 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
9578 /* id *itemsPtr; */
9579 add_field_decl (build_pointer_type (objc_object_type),
9580 "itemsPtr", &chain);
9582 /* unsigned long *mutationsPtr; */
9583 add_field_decl (build_pointer_type (long_unsigned_type_node),
9584 "mutationsPtr", &chain);
9586 /* unsigned long extra[5]; */
9587 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
9588 "extra", &chain);
9590 /* } */
9591 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9595 'objc_finish_foreach_loop()' generates the code for an Objective-C
9596 foreach loop. The 'location' argument is the location of the 'for'
9597 that starts the loop. The 'object_expression' is the expression of
9598 the 'object' that iterates; the 'collection_expression' is the
9599 expression of the collection that we iterate over (we need to make
9600 sure we evaluate this only once); the 'for_body' is the set of
9601 statements to be executed in each iteration; 'break_label' and
9602 'continue_label' are the break and continue labels which we need to
9603 emit since the <statements> may be jumping to 'break_label' (if they
9604 contain 'break') or to 'continue_label' (if they contain
9605 'continue').
9607 The syntax is
9609 for (<object expression> in <collection expression>)
9610 <statements>
9612 which is compiled into the following blurb:
9615 id __objc_foreach_collection;
9616 __objc_fast_enumeration_state __objc_foreach_enum_state;
9617 unsigned long __objc_foreach_batchsize;
9618 id __objc_foreach_items[16];
9619 __objc_foreach_collection = <collection expression>;
9620 __objc_foreach_enum_state = { 0 };
9621 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9623 if (__objc_foreach_batchsize == 0)
9624 <object expression> = nil;
9625 else
9627 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9628 next_batch:
9630 unsigned long __objc_foreach_index;
9631 __objc_foreach_index = 0;
9633 next_object:
9634 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9635 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9636 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9638 continue_label:
9639 __objc_foreach_index++;
9640 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9641 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9643 if (__objc_foreach_batchsize != 0) goto next_batch;
9644 <object expression> = nil;
9645 break_label:
9649 'statements' may contain a 'continue' or 'break' instruction, which
9650 the user expects to 'continue' or 'break' the entire foreach loop.
9651 We are provided the labels that 'break' and 'continue' jump to, so
9652 we place them where we want them to jump to when they pick them.
9654 Optimization TODO: we could cache the IMP of
9655 countByEnumeratingWithState:objects:count:.
9658 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9659 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9661 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9662 #include "tree-pretty-print.h"
9663 #endif
9665 void
9666 objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
9667 tree break_label, tree continue_label)
9669 /* A tree representing the __objcFastEnumerationState struct type,
9670 or NSFastEnumerationState struct, whatever we are using. */
9671 tree objc_fast_enumeration_state_type;
9673 /* The trees representing the declarations of each of the local variables. */
9674 tree objc_foreach_collection_decl;
9675 tree objc_foreach_enum_state_decl;
9676 tree objc_foreach_items_decl;
9677 tree objc_foreach_batchsize_decl;
9678 tree objc_foreach_mutations_pointer_decl;
9679 tree objc_foreach_index_decl;
9681 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9682 tree selector_name;
9684 /* A tree representing the local bind. */
9685 tree bind;
9687 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9688 tree first_if;
9690 /* A tree representing the 'else' part of 'first_if' */
9691 tree first_else;
9693 /* A tree representing the 'next_batch' label. */
9694 tree next_batch_label_decl;
9696 /* A tree representing the binding after the 'next_batch' label. */
9697 tree next_batch_bind;
9699 /* A tree representing the 'next_object' label. */
9700 tree next_object_label_decl;
9702 /* Temporary variables. */
9703 tree t;
9704 int i;
9706 if (flag_objc1_only)
9707 error_at (location, "fast enumeration is not available in Objective-C 1.0");
9709 if (object_expression == error_mark_node)
9710 return;
9712 if (collection_expression == error_mark_node)
9713 return;
9715 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
9717 error_at (location, "iterating variable in fast enumeration is not an object");
9718 return;
9721 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
9723 error_at (location, "collection in fast enumeration is not an object");
9724 return;
9727 /* TODO: Check that object_expression is either a variable
9728 declaration, or an lvalue. */
9730 /* This kludge is an idea from apple. We use the
9731 __objcFastEnumerationState struct implicitly defined by the
9732 compiler, unless a NSFastEnumerationState struct has been defined
9733 (by a Foundation library such as GNUstep Base) in which case, we
9734 use that one.
9736 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9738 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
9740 if (objc_NSFastEnumeration_type)
9742 /* TODO: We really need to check that
9743 objc_NSFastEnumeration_type is the same as ours! */
9744 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9746 /* If it's a typedef, use the original type. */
9747 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9748 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9749 else
9750 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
9755 /* { */
9756 /* Done by c-parser.c. */
9758 /* type object; */
9759 /* Done by c-parser.c. */
9761 /* Disable warnings that 'object' is unused. For example the code
9763 for (id object in collection)
9764 i++;
9766 which can be used to count how many objects there are in the
9767 collection is fine and should generate no warnings even if
9768 'object' is technically unused. */
9769 TREE_USED (object_expression) = 1;
9770 if (DECL_P (object_expression))
9771 DECL_READ_P (object_expression) = 1;
9773 /* id __objc_foreach_collection */
9774 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
9776 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9777 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9778 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
9780 /* id __objc_foreach_items[16]; */
9781 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9782 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
9784 /* unsigned long __objc_foreach_batchsize; */
9785 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9786 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9788 /* Generate the local variable binding. */
9789 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9790 SET_EXPR_LOCATION (bind, location);
9791 TREE_SIDE_EFFECTS (bind) = 1;
9793 /* __objc_foreach_collection = <collection expression>; */
9794 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9795 SET_EXPR_LOCATION (t, location);
9796 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9797 /* We have used 'collection_expression'. */
9798 mark_exp_read (collection_expression);
9800 /* __objc_foreach_enum_state.state = 0; */
9801 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9802 get_identifier ("state")),
9803 build_int_cst (long_unsigned_type_node, 0));
9804 SET_EXPR_LOCATION (t, location);
9805 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9807 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9808 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9809 get_identifier ("itemsPtr")),
9810 null_pointer_node);
9811 SET_EXPR_LOCATION (t, location);
9812 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9814 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9815 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
9816 get_identifier ("mutationsPtr")),
9817 null_pointer_node);
9818 SET_EXPR_LOCATION (t, location);
9819 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9821 /* __objc_foreach_enum_state.extra[0] = 0; */
9822 /* __objc_foreach_enum_state.extra[1] = 0; */
9823 /* __objc_foreach_enum_state.extra[2] = 0; */
9824 /* __objc_foreach_enum_state.extra[3] = 0; */
9825 /* __objc_foreach_enum_state.extra[4] = 0; */
9826 for (i = 0; i < 5 ; i++)
9828 t = build2 (MODIFY_EXPR, void_type_node,
9829 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9830 get_identifier ("extra")),
9831 build_int_cst (NULL_TREE, i)),
9832 build_int_cst (long_unsigned_type_node, 0));
9833 SET_EXPR_LOCATION (t, location);
9834 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9837 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9838 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9839 #ifdef OBJCPLUS
9840 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9841 /* Parameters. */
9842 tree_cons /* &__objc_foreach_enum_state */
9843 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9844 tree_cons /* __objc_foreach_items */
9845 (NULL_TREE, objc_foreach_items_decl,
9846 tree_cons /* 16 */
9847 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9848 #else
9849 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9851 struct c_expr array;
9852 array.value = objc_foreach_items_decl;
9853 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9854 /* Parameters. */
9855 tree_cons /* &__objc_foreach_enum_state */
9856 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9857 tree_cons /* __objc_foreach_items */
9858 (NULL_TREE, default_function_array_conversion (location, array).value,
9859 tree_cons /* 16 */
9860 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9862 #endif
9863 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9864 convert (long_unsigned_type_node, t));
9865 SET_EXPR_LOCATION (t, location);
9866 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9868 /* if (__objc_foreach_batchsize == 0) */
9869 first_if = build3 (COND_EXPR, void_type_node,
9870 /* Condition. */
9871 c_fully_fold
9872 (c_common_truthvalue_conversion
9873 (location,
9874 build_binary_op (location,
9875 EQ_EXPR,
9876 objc_foreach_batchsize_decl,
9877 build_int_cst (long_unsigned_type_node, 0), 1)),
9878 false, NULL),
9879 /* Then block (we fill it in later). */
9880 NULL_TREE,
9881 /* Else block (we fill it in later). */
9882 NULL_TREE);
9883 SET_EXPR_LOCATION (first_if, location);
9884 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
9886 /* then <object expression> = nil; */
9887 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9888 SET_EXPR_LOCATION (t, location);
9889 COND_EXPR_THEN (first_if) = t;
9891 /* Now we build the 'else' part of the if; once we finish building
9892 it, we attach it to first_if as the 'else' part. */
9894 /* else */
9895 /* { */
9897 /* unsigned long __objc_foreach_mutations_pointer; */
9898 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
9900 /* Generate the local variable binding. */
9901 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9902 SET_EXPR_LOCATION (first_else, location);
9903 TREE_SIDE_EFFECTS (first_else) = 1;
9905 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9906 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9907 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9908 get_identifier ("mutationsPtr")),
9909 RO_UNARY_STAR));
9910 SET_EXPR_LOCATION (t, location);
9911 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9913 /* next_batch: */
9914 next_batch_label_decl = create_artificial_label (location);
9915 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
9916 SET_EXPR_LOCATION (t, location);
9917 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
9919 /* { */
9921 /* unsigned long __objc_foreach_index; */
9922 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
9924 /* Generate the local variable binding. */
9925 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9926 SET_EXPR_LOCATION (next_batch_bind, location);
9927 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9928 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
9930 /* __objc_foreach_index = 0; */
9931 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9932 build_int_cst (long_unsigned_type_node, 0));
9933 SET_EXPR_LOCATION (t, location);
9934 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9936 /* next_object: */
9937 next_object_label_decl = create_artificial_label (location);
9938 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9939 SET_EXPR_LOCATION (t, location);
9940 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9942 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9943 t = build3 (COND_EXPR, void_type_node,
9944 /* Condition. */
9945 c_fully_fold
9946 (c_common_truthvalue_conversion
9947 (location,
9948 build_binary_op
9949 (location,
9950 NE_EXPR,
9951 objc_foreach_mutations_pointer_decl,
9952 build_indirect_ref (location,
9953 objc_build_component_ref (objc_foreach_enum_state_decl,
9954 get_identifier ("mutationsPtr")),
9955 RO_UNARY_STAR), 1)),
9956 false, NULL),
9957 /* Then block. */
9958 build_function_call (input_location,
9959 objc_enumeration_mutation_decl,
9960 tree_cons (NULL, collection_expression, NULL)),
9961 /* Else block. */
9962 NULL_TREE);
9963 SET_EXPR_LOCATION (t, location);
9964 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9966 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
9967 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9968 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
9969 get_identifier ("itemsPtr")),
9970 objc_foreach_index_decl));
9971 SET_EXPR_LOCATION (t, location);
9972 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9974 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9975 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
9977 /* continue_label: */
9978 if (continue_label)
9980 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9981 SET_EXPR_LOCATION (t, location);
9982 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9985 /* __objc_foreach_index++; */
9986 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9987 build_binary_op (location,
9988 PLUS_EXPR,
9989 objc_foreach_index_decl,
9990 build_int_cst (long_unsigned_type_node, 1), 1));
9991 SET_EXPR_LOCATION (t, location);
9992 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9994 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
9995 t = build3 (COND_EXPR, void_type_node,
9996 /* Condition. */
9997 c_fully_fold
9998 (c_common_truthvalue_conversion
9999 (location,
10000 build_binary_op (location,
10001 LT_EXPR,
10002 objc_foreach_index_decl,
10003 objc_foreach_batchsize_decl, 1)),
10004 false, NULL),
10005 /* Then block. */
10006 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
10007 /* Else block. */
10008 NULL_TREE);
10009 SET_EXPR_LOCATION (t, location);
10010 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10012 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
10013 #ifdef OBJCPLUS
10014 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10015 /* Parameters. */
10016 tree_cons /* &__objc_foreach_enum_state */
10017 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10018 tree_cons /* __objc_foreach_items */
10019 (NULL_TREE, objc_foreach_items_decl,
10020 tree_cons /* 16 */
10021 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10022 #else
10023 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10025 struct c_expr array;
10026 array.value = objc_foreach_items_decl;
10027 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10028 /* Parameters. */
10029 tree_cons /* &__objc_foreach_enum_state */
10030 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10031 tree_cons /* __objc_foreach_items */
10032 (NULL_TREE, default_function_array_conversion (location, array).value,
10033 tree_cons /* 16 */
10034 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10036 #endif
10037 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
10038 convert (long_unsigned_type_node, t));
10039 SET_EXPR_LOCATION (t, location);
10040 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
10042 /* } */
10044 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10045 t = build3 (COND_EXPR, void_type_node,
10046 /* Condition. */
10047 c_fully_fold
10048 (c_common_truthvalue_conversion
10049 (location,
10050 build_binary_op (location,
10051 NE_EXPR,
10052 objc_foreach_batchsize_decl,
10053 build_int_cst (long_unsigned_type_node, 0), 1)),
10054 false, NULL),
10055 /* Then block. */
10056 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10057 /* Else block. */
10058 NULL_TREE);
10059 SET_EXPR_LOCATION (t, location);
10060 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10062 /* <object expression> = nil; */
10063 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10064 SET_EXPR_LOCATION (t, location);
10065 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10067 /* break_label: */
10068 if (break_label)
10070 t = build1 (LABEL_EXPR, void_type_node, break_label);
10071 SET_EXPR_LOCATION (t, location);
10072 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10075 /* } */
10076 COND_EXPR_ELSE (first_if) = first_else;
10078 /* Do the whole thing. */
10079 add_stmt (bind);
10081 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10082 /* This will print to stderr the whole blurb generated by the
10083 compiler while compiling (assuming the compiler doesn't crash
10084 before getting here).
10086 debug_generic_stmt (bind);
10087 #endif
10089 /* } */
10090 /* Done by c-parser.c */
10093 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10094 /* Return true if we have an NxString object pointer. */
10096 bool
10097 objc_string_ref_type_p (tree strp)
10099 tree tmv;
10100 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10101 return false;
10103 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10104 tmv = OBJC_TYPE_NAME (tmv);
10105 return (tmv
10106 && TREE_CODE (tmv) == IDENTIFIER_NODE
10107 && IDENTIFIER_POINTER (tmv)
10108 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10111 /* At present the behavior of this is undefined and it does nothing. */
10112 void
10113 objc_check_format_arg (tree ARG_UNUSED (format_arg),
10114 tree ARG_UNUSED (args_list))
10118 void
10119 objc_common_init_ts (void)
10121 c_common_init_ts ();
10123 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10124 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10125 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10126 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10128 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10129 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10130 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10132 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10133 MARK_TS_TYPED (PROPERTY_REF);
10136 size_t
10137 objc_common_tree_size (enum tree_code code)
10139 switch (code)
10141 case CLASS_METHOD_DECL:
10142 case INSTANCE_METHOD_DECL:
10143 case KEYWORD_DECL:
10144 case PROPERTY_DECL:
10145 return sizeof (struct tree_decl_non_common);
10146 default:
10147 gcc_unreachable ();
10153 #include "gt-objc-objc-act.h"