1 /* Implement classes and message passing for Objective C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Steve Naroff.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "c-family/c-pragma.h"
39 #include "c-family/c-format.h"
41 #include "langhooks.h"
49 #include "c-family/c-target.h"
50 #include "diagnostic-core.h"
53 #include "tree-iterator.h"
55 #include "langhooks-def.h"
56 /* Different initialization, code gen and meta data generation for each
58 #include "objc-runtime-hooks.h"
59 /* Routines used mainly by the runtimes. */
60 #include "objc-runtime-shared-support.h"
61 /* For default_tree_printer (). */
62 #include "tree-pretty-print.h"
64 /* For enum gimplify_status */
67 /* For encode_method_prototype(). */
68 #include "objc-encoding.h"
70 static unsigned int should_call_super_dealloc
= 0;
72 /* When building Objective-C++, we are not linking against the C front-end
73 and so need to replicate the C tree-construction functions in some way. */
75 #define OBJCP_REMAP_FUNCTIONS
76 #include "objcp-decl.h"
79 /* This is the default way of generating a method name. */
80 /* This has the problem that "test_method:argument:" and
81 "test:method_argument:" will generate the same name
82 ("_i_Test__test_method_argument_" for an instance method of the
83 class "Test"), so you can't have them both in the same class!
84 Moreover, the demangling (going from
85 "_i_Test__test_method_argument" back to the original name) is
86 undefined because there are two correct ways of demangling the
88 #ifndef OBJC_GEN_METHOD_LABEL
89 #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
92 sprintf ((BUF), "_%s_%s_%s_%s", \
93 ((IS_INST) ? "i" : "c"), \
95 ((CAT_NAME)? (CAT_NAME) : ""), \
97 for (temp = (BUF); *temp; temp++) \
98 if (*temp == ':') *temp = '_'; \
102 /* These need specifying. */
103 #ifndef OBJC_FORWARDING_STACK_OFFSET
104 #define OBJC_FORWARDING_STACK_OFFSET 0
107 #ifndef OBJC_FORWARDING_MIN_OFFSET
108 #define OBJC_FORWARDING_MIN_OFFSET 0
111 /*** Private Interface (procedures) ***/
114 static void synth_module_prologue (void);
116 /* Code generation. */
118 static tree
start_class (enum tree_code
, tree
, tree
, tree
, tree
);
119 static tree
continue_class (tree
);
120 static void finish_class (tree
);
121 static void start_method_def (tree
, tree
);
123 static tree
start_protocol (enum tree_code
, tree
, tree
, tree
);
124 static tree
build_method_decl (enum tree_code
, tree
, tree
, tree
, bool);
125 static tree
objc_add_method (tree
, tree
, int, bool);
126 static tree
add_instance_variable (tree
, objc_ivar_visibility_kind
, tree
);
127 static tree
build_ivar_reference (tree
);
128 static tree
is_ivar (tree
, tree
);
130 /* We only need the following for ObjC; ObjC++ will use C++'s definition
131 of DERIVED_FROM_P. */
133 static bool objc_derived_from_p (tree
, tree
);
134 #define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
138 static void objc_gen_property_data (tree
, tree
);
139 static void objc_synthesize_getter (tree
, tree
, tree
);
140 static void objc_synthesize_setter (tree
, tree
, tree
);
141 static tree
lookup_property (tree
, tree
);
142 static tree
lookup_property_in_list (tree
, tree
);
143 static tree
lookup_property_in_protocol_list (tree
, tree
);
144 static void build_common_objc_property_accessor_helpers (void);
146 static void objc_xref_basetypes (tree
, tree
);
148 static tree
get_class_ivars (tree
, bool);
150 static void build_fast_enumeration_state_template (void);
153 static void objc_generate_cxx_cdtors (void);
157 static void objc_decl_method_attributes (tree
*, tree
, int);
158 static tree
build_keyword_selector (tree
);
160 /* Hash tables to manage the global pool of method prototypes. */
161 static void hash_init (void);
163 hash
*nst_method_hash_list
= 0;
164 hash
*cls_method_hash_list
= 0;
166 /* Hash tables to manage the global pool of class names. */
168 hash
*cls_name_hash_list
= 0;
169 hash
*als_name_hash_list
= 0;
171 hash
*ivar_offset_hash_list
= 0;
173 static void hash_class_name_enter (hash
*, tree
, tree
);
174 static hash
hash_class_name_lookup (hash
*, tree
);
176 static hash
hash_lookup (hash
*, tree
);
177 static tree
lookup_method (tree
, tree
);
178 static tree
lookup_method_static (tree
, tree
, int);
180 static tree
add_class (tree
, tree
);
181 static void add_category (tree
, tree
);
182 static inline tree
lookup_category (tree
, tree
);
186 static tree
lookup_protocol (tree
, bool, bool);
187 static tree
lookup_and_install_protocols (tree
, bool);
190 static void really_start_method (tree
, tree
);
192 static void really_start_method (tree
, struct c_arg_info
*);
194 static int comp_proto_with_proto (tree
, tree
, int);
195 static tree
objc_decay_parm_type (tree
);
197 /* Utilities for debugging and error diagnostics. */
199 static char *gen_type_name (tree
);
200 static char *gen_type_name_0 (tree
);
201 static char *gen_method_decl (tree
);
202 static char *gen_declaration (tree
);
204 /* Everything else. */
206 static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN
;
208 static void mark_referenced_methods (void);
209 static bool objc_type_valid_for_messaging (tree type
, bool allow_classes
);
210 static tree
check_duplicates (hash
, int, int);
212 /*** Private Interface (data) ***/
213 /* Flags for lookup_method_static(). */
215 /* Look for class methods. */
216 #define OBJC_LOOKUP_CLASS 1
217 /* Do not examine superclasses. */
218 #define OBJC_LOOKUP_NO_SUPER 2
219 /* Disable returning an instance method of a root class when a class
220 method can't be found. */
221 #define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
223 /* The OCTI_... enumeration itself is in objc/objc-act.h. */
224 tree objc_global_trees
[OCTI_MAX
];
226 struct imp_entry
*imp_list
= 0;
227 int imp_count
= 0; /* `@implementation' */
228 int cat_count
= 0; /* `@category' */
230 objc_ivar_visibility_kind objc_ivar_visibility
;
232 /* Use to generate method labels. */
233 static int method_slot
= 0;
235 /* Flag to say whether methods in a protocol are optional or
237 static bool objc_method_optional_flag
= false;
239 static int objc_collecting_ivars
= 0;
241 /* Flag that is set to 'true' while we are processing a class
242 extension. Since a class extension just "reopens" the main
243 @interface, this can be used to determine if we are in the main
244 @interface, or in a class extension. */
245 static bool objc_in_class_extension
= false;
247 static char *errbuf
; /* Buffer for error diagnostics */
249 /* An array of all the local variables in the current function that
250 need to be marked as volatile. */
251 VEC(tree
,gc
) *local_variables_to_volatilize
= NULL
;
253 /* Store all constructed constant strings in a hash table so that
254 they get uniqued properly. */
256 struct GTY(()) string_descriptor
{
257 /* The literal argument . */
260 /* The resulting constant string. */
264 static GTY((param_is (struct string_descriptor
))) htab_t string_htab
;
266 FILE *gen_declaration_file
;
268 /* Hooks for stuff that differs between runtimes. */
269 objc_runtime_hooks runtime
;
271 /* Create a temporary variable of type 'type'. If 'name' is set, uses
272 the specified name, else use no name. Returns the declaration of
273 the type. The 'name' is mostly useful for debugging.
276 objc_create_temporary_var (tree type
, const char *name
)
282 decl
= build_decl (input_location
,
283 VAR_DECL
, get_identifier (name
), type
);
287 decl
= build_decl (input_location
,
288 VAR_DECL
, NULL_TREE
, type
);
290 TREE_USED (decl
) = 1;
291 DECL_ARTIFICIAL (decl
) = 1;
292 DECL_IGNORED_P (decl
) = 1;
293 DECL_CONTEXT (decl
) = current_function_decl
;
298 /* Some platforms pass small structures through registers versus
299 through an invisible pointer. Determine at what size structure is
300 the transition point between the two possibilities. */
303 generate_struct_by_value_array (void)
308 int aggregate_in_mem
[32];
311 /* Presumably no platform passes 32 byte structures in a register. */
312 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
314 for (i
= 1; i
< 32; i
++)
319 /* Create an unnamed struct that has `i' character components */
320 type
= objc_start_struct (NULL_TREE
);
322 strcpy (buffer
, "c1");
323 decls
= add_field_decl (char_type_node
, buffer
, &chain
);
325 for (j
= 1; j
< i
; j
++)
327 sprintf (buffer
, "c%d", j
+ 1);
328 add_field_decl (char_type_node
, buffer
, &chain
);
330 objc_finish_struct (type
, decls
);
332 aggregate_in_mem
[i
] = aggregate_value_p (type
, 0);
333 if (!aggregate_in_mem
[i
])
337 /* We found some structures that are returned in registers instead of memory
338 so output the necessary data. */
341 for (i
= 31; i
>= 0; i
--)
342 if (!aggregate_in_mem
[i
])
344 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i
);
355 if (cxx_init () == false)
357 if (c_objc_common_init () == false)
361 /* print_struct_values is triggered by -print-runtime-info (used
362 when building libobjc, with an empty file as input). It does not
363 require any ObjC setup, and it never returns.
365 -fcompare-debug is used to check the compiler output; we are
366 executed twice, once with flag_compare_debug set, and once with
367 it not set. If the flag is used together with
368 -print-runtime-info, we want to print the runtime info only once,
369 else it would be output in duplicate. So we check
370 flag_compare_debug to output it in only one of the invocations.
372 As a side effect, this also that means -fcompare-debug
373 -print-runtime-info will run the compiler twice, and compare the
374 generated assembler file; the first time the compiler exits
375 immediately (producing no file), and the second time it compiles
376 an empty file. This checks, as a side effect, that compiling an
377 empty file produces no assembler output. */
378 if (print_struct_values
&& !flag_compare_debug
)
379 generate_struct_by_value_array ();
381 /* Set up stuff used by FE parser and all runtimes. */
382 errbuf
= XNEWVEC (char, 1024 * 10);
384 objc_encoding_init ();
385 /* ... and then check flags and set-up for the selected runtime ... */
386 if (flag_next_runtime
&& flag_objc_abi
>= 2)
387 ok
= objc_next_runtime_abi_02_init (&runtime
);
388 else if (flag_next_runtime
)
389 ok
= objc_next_runtime_abi_01_init (&runtime
);
391 ok
= objc_gnu_runtime_abi_01_init (&runtime
);
393 /* If that part of the setup failed - bail out immediately. */
397 /* Generate general types and push runtime-specific decls to file scope. */
398 synth_module_prologue ();
403 /* This is called automatically (at the very end of compilation) by
404 c_write_global_declarations and cp_write_global_declarations. */
406 objc_write_global_declarations (void)
408 mark_referenced_methods ();
410 /* A missing @end might not be detected by the parser. */
411 if (objc_implementation_context
)
413 warning (0, "%<@end%> missing in implementation context");
414 finish_class (objc_implementation_context
);
415 objc_ivar_chain
= NULL_TREE
;
416 objc_implementation_context
= NULL_TREE
;
424 /* Run through the selector hash tables and print a warning for any
425 selector which has multiple methods. */
427 for (slot
= 0; slot
< SIZEHASHTABLE
; slot
++)
429 for (hsh
= cls_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
430 check_duplicates (hsh
, 0, 1);
431 for (hsh
= nst_method_hash_list
[slot
]; hsh
; hsh
= hsh
->next
)
432 check_duplicates (hsh
, 0, 0);
436 /* TODO: consider an early exit here if either errorcount or sorrycount
437 is non-zero. Not only is it wasting time to generate the metadata,
438 it needlessly imposes need to re-check for things that are already
439 determined to be errors. */
441 /* Finalize Objective-C runtime data. No need to generate tables
442 and code if only checking syntax, or if generating a PCH file. */
443 if (!flag_syntax_only
&& !pch_file
)
445 location_t saved_location
;
447 /* If gen_declaration desired, open the output file. */
448 if (flag_gen_declaration
)
450 char * const dumpname
= concat (dump_base_name
, ".decl", NULL
);
451 gen_declaration_file
= fopen (dumpname
, "w");
452 if (gen_declaration_file
== 0)
453 fatal_error ("can%'t open %s: %m", dumpname
);
457 /* Set the input location to BUILTINS_LOCATION. This is good
458 for error messages, in case any is generated while producing
459 the metadata, but it also silences warnings that would be
460 produced when compiling with -Wpadded in case when padding is
461 automatically added to the built-in runtime data structure
462 declarations. We know about this padding, and it is fine; we
463 don't want users to see any warnings about it if they use
465 saved_location
= input_location
;
466 input_location
= BUILTINS_LOCATION
;
468 /* Compute and emit the meta-data tables for this runtime. */
469 (*runtime
.generate_metadata
) ();
471 /* Restore the original location, just in case it mattered. */
472 input_location
= saved_location
;
474 /* ... and then close any declaration file we opened. */
475 if (gen_declaration_file
)
476 fclose (gen_declaration_file
);
480 /* Return the first occurrence of a method declaration corresponding
481 to sel_name in rproto_list. Search rproto_list recursively.
482 If is_class is 0, search for instance methods, otherwise for class
485 lookup_method_in_protocol_list (tree rproto_list
, tree sel_name
,
490 for (rproto
= rproto_list
; rproto
; rproto
= TREE_CHAIN (rproto
))
492 p
= TREE_VALUE (rproto
);
495 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
497 /* First, search the @required protocol methods. */
499 m
= lookup_method (PROTOCOL_CLS_METHODS (p
), sel_name
);
501 m
= lookup_method (PROTOCOL_NST_METHODS (p
), sel_name
);
506 /* If still not found, search the @optional protocol methods. */
508 m
= lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p
), sel_name
);
510 m
= lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p
), sel_name
);
515 /* If still not found, search the attached protocols. */
516 if (PROTOCOL_LIST (p
))
517 m
= lookup_method_in_protocol_list (PROTOCOL_LIST (p
),
524 ; /* An identifier...if we could not find a protocol. */
532 lookup_protocol_in_reflist (tree rproto_list
, tree lproto
)
536 /* Make sure the protocol is supported by the object on the rhs. */
537 if (TREE_CODE (lproto
) == PROTOCOL_INTERFACE_TYPE
)
540 for (rproto
= rproto_list
; rproto
; rproto
= TREE_CHAIN (rproto
))
542 p
= TREE_VALUE (rproto
);
544 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
549 else if (PROTOCOL_LIST (p
))
550 fnd
= lookup_protocol_in_reflist (PROTOCOL_LIST (p
), lproto
);
559 ; /* An identifier...if we could not find a protocol. */
566 objc_start_class_interface (tree klass
, tree super_class
,
567 tree protos
, tree attributes
)
569 if (flag_objc1_only
&& attributes
)
570 error_at (input_location
, "class attributes are not available in Objective-C 1.0");
572 objc_interface_context
574 = start_class (CLASS_INTERFACE_TYPE
, klass
, super_class
, protos
, attributes
);
575 objc_ivar_visibility
= OBJC_IVAR_VIS_PROTECTED
;
579 objc_start_category_interface (tree klass
, tree categ
,
580 tree protos
, tree attributes
)
585 error_at (input_location
, "category attributes are not available in Objective-C 1.0");
587 warning_at (input_location
, OPT_Wattributes
,
588 "category attributes are not available in this version"
589 " of the compiler, (ignored)");
591 if (categ
== NULL_TREE
)
594 error_at (input_location
, "class extensions are not available in Objective-C 1.0");
597 /* Iterate over all the classes and categories implemented
598 up to now in this compilation unit. */
601 for (t
= imp_list
; t
; t
= t
->next
)
603 /* If we find a class @implementation with the same name
604 as the one we are extending, produce an error. */
605 if (TREE_CODE (t
->imp_context
) == CLASS_IMPLEMENTATION_TYPE
606 && IDENTIFIER_POINTER (CLASS_NAME (t
->imp_context
)) == IDENTIFIER_POINTER (klass
))
607 error_at (input_location
,
608 "class extension for class %qE declared after its %<@implementation%>",
613 objc_interface_context
614 = start_class (CATEGORY_INTERFACE_TYPE
, klass
, categ
, protos
, NULL_TREE
);
616 = continue_class (objc_interface_context
);
620 objc_start_protocol (tree name
, tree protos
, tree attributes
)
622 if (flag_objc1_only
&& attributes
)
623 error_at (input_location
, "protocol attributes are not available in Objective-C 1.0");
625 objc_interface_context
626 = start_protocol (PROTOCOL_INTERFACE_TYPE
, name
, protos
, attributes
);
627 objc_method_optional_flag
= false;
631 objc_continue_interface (void)
634 = continue_class (objc_interface_context
);
638 objc_finish_interface (void)
640 finish_class (objc_interface_context
);
641 objc_interface_context
= NULL_TREE
;
642 objc_method_optional_flag
= false;
643 objc_in_class_extension
= false;
647 objc_start_class_implementation (tree klass
, tree super_class
)
649 objc_implementation_context
651 = start_class (CLASS_IMPLEMENTATION_TYPE
, klass
, super_class
, NULL_TREE
,
653 objc_ivar_visibility
= OBJC_IVAR_VIS_PROTECTED
;
657 objc_start_category_implementation (tree klass
, tree categ
)
659 objc_implementation_context
660 = start_class (CATEGORY_IMPLEMENTATION_TYPE
, klass
, categ
, NULL_TREE
,
663 = continue_class (objc_implementation_context
);
667 objc_continue_implementation (void)
670 = continue_class (objc_implementation_context
);
674 objc_finish_implementation (void)
677 if (flag_objc_call_cxx_cdtors
)
678 objc_generate_cxx_cdtors ();
681 if (objc_implementation_context
)
683 finish_class (objc_implementation_context
);
684 objc_ivar_chain
= NULL_TREE
;
685 objc_implementation_context
= NULL_TREE
;
688 warning (0, "%<@end%> must appear in an @implementation context");
692 objc_set_visibility (objc_ivar_visibility_kind visibility
)
694 if (visibility
== OBJC_IVAR_VIS_PACKAGE
)
697 error ("%<@package%> is not available in Objective-C 1.0");
699 warning (0, "%<@package%> presently has the same effect as %<@public%>");
701 objc_ivar_visibility
= visibility
;
705 objc_set_method_opt (bool optional
)
710 error_at (input_location
, "%<@optional%> is not available in Objective-C 1.0");
712 error_at (input_location
, "%<@required%> is not available in Objective-C 1.0");
715 objc_method_optional_flag
= optional
;
716 if (!objc_interface_context
717 || TREE_CODE (objc_interface_context
) != PROTOCOL_INTERFACE_TYPE
)
720 error ("%<@optional%> is allowed in @protocol context only");
722 error ("%<@required%> is allowed in @protocol context only");
723 objc_method_optional_flag
= false;
727 /* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
730 lookup_property_in_list (tree chain
, tree property
)
733 for (x
= CLASS_PROPERTY_DECL (chain
); x
; x
= TREE_CHAIN (x
))
734 if (PROPERTY_NAME (x
) == property
)
739 /* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
740 static tree
lookup_property_in_protocol_list (tree rproto_list
, tree property
)
743 for (rproto
= rproto_list
; rproto
; rproto
= TREE_CHAIN (rproto
))
745 tree p
= TREE_VALUE (rproto
);
746 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
748 if ((x
= lookup_property_in_list (p
, property
)))
750 if (PROTOCOL_LIST (p
))
751 return lookup_property_in_protocol_list (PROTOCOL_LIST (p
), property
);
755 ; /* An identifier...if we could not find a protocol. */
761 /* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
762 chain of interface hierarchy. */
764 lookup_property (tree interface_type
, tree property
)
766 tree inter
= interface_type
;
770 if ((x
= lookup_property_in_list (inter
, property
)))
772 /* Failing that, look for the property in each category of the class. */
774 while ((category
= CLASS_CATEGORY_LIST (category
)))
776 if ((x
= lookup_property_in_list (category
, property
)))
779 /* When checking a category, also check the protocols
780 attached with the category itself. */
781 if (CLASS_PROTOCOL_LIST (category
)
782 && (x
= lookup_property_in_protocol_list
783 (CLASS_PROTOCOL_LIST (category
), property
)))
787 /* Failing to find in categories, look for property in protocol list. */
788 if (CLASS_PROTOCOL_LIST (inter
)
789 && (x
= lookup_property_in_protocol_list
790 (CLASS_PROTOCOL_LIST (inter
), property
)))
793 /* Failing that, climb up the inheritance hierarchy. */
794 inter
= lookup_interface (CLASS_SUPER_NAME (inter
));
799 /* This routine is called by the parser when a
800 @property... declaration is found. 'decl' is the declaration of
801 the property (type/identifier), and the other arguments represent
802 property attributes that may have been specified in the Objective-C
803 declaration. 'parsed_property_readonly' is 'true' if the attribute
804 'readonly' was specified, and 'false' if not; similarly for the
805 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
806 if the attribute 'getter' was not specified, and is the identifier
807 corresponding to the specified getter if it was; similarly for
808 'parsed_property_setter_ident'. */
810 objc_add_property_declaration (location_t location
, tree decl
,
811 bool parsed_property_readonly
, bool parsed_property_readwrite
,
812 bool parsed_property_assign
, bool parsed_property_retain
,
813 bool parsed_property_copy
, bool parsed_property_nonatomic
,
814 tree parsed_property_getter_ident
, tree parsed_property_setter_ident
)
818 /* 'property_readonly' and 'property_assign_semantics' are the final
819 attributes of the property after all parsed attributes have been
820 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
821 parsed_property_readonly = false and parsed_property_readwrite =
822 false, then property_readonly will be false because the default
824 bool property_readonly
= false;
825 objc_property_assign_semantics property_assign_semantics
= OBJC_PROPERTY_ASSIGN
;
826 bool property_extension_in_class_extension
= false;
829 error_at (input_location
, "%<@property%> is not available in Objective-C 1.0");
831 if (parsed_property_readonly
&& parsed_property_readwrite
)
833 error_at (location
, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
834 /* In case of conflicting attributes (here and below), after
835 producing an error, we pick one of the attributes and keep
837 property_readonly
= false;
841 if (parsed_property_readonly
)
842 property_readonly
= true;
844 if (parsed_property_readwrite
)
845 property_readonly
= false;
848 if (parsed_property_readonly
&& parsed_property_setter_ident
)
850 error_at (location
, "%<readonly%> attribute conflicts with %<setter%> attribute");
851 property_readonly
= false;
854 if (parsed_property_assign
&& parsed_property_retain
)
856 error_at (location
, "%<assign%> attribute conflicts with %<retain%> attribute");
857 property_assign_semantics
= OBJC_PROPERTY_RETAIN
;
859 else if (parsed_property_assign
&& parsed_property_copy
)
861 error_at (location
, "%<assign%> attribute conflicts with %<copy%> attribute");
862 property_assign_semantics
= OBJC_PROPERTY_COPY
;
864 else if (parsed_property_retain
&& parsed_property_copy
)
866 error_at (location
, "%<retain%> attribute conflicts with %<copy%> attribute");
867 property_assign_semantics
= OBJC_PROPERTY_COPY
;
871 if (parsed_property_assign
)
872 property_assign_semantics
= OBJC_PROPERTY_ASSIGN
;
874 if (parsed_property_retain
)
875 property_assign_semantics
= OBJC_PROPERTY_RETAIN
;
877 if (parsed_property_copy
)
878 property_assign_semantics
= OBJC_PROPERTY_COPY
;
881 if (!objc_interface_context
)
883 error_at (location
, "property declaration not in @interface or @protocol context");
887 /* At this point we know that we are either in an interface, a
888 category, or a protocol. */
890 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
891 something else, as that would confuse the checks below. */
892 if (TREE_CODE (decl
) != FIELD_DECL
)
894 error_at (location
, "invalid property declaration");
898 /* Do some spot-checks for the most obvious invalid types. */
900 if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
902 error_at (location
, "property can not be an array");
906 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
907 parsing, while the C/ObjC parser accepts it and gives us a
908 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
909 to check for a bitfield when doing ObjC. */
911 if (DECL_INITIAL (decl
))
913 /* A @property is not an actual variable, but it is a way to
914 describe a pair of accessor methods, so its type (which is
915 the type of the return value of the getter and the first
916 argument of the setter) can't be a bitfield (as return values
917 and arguments of functions can not be bitfields). The
918 underlying instance variable could be a bitfield, but that is
919 a different matter. */
920 error_at (location
, "property can not be a bit-field");
925 /* TODO: Check that the property type is an Objective-C object or a
928 /* Implement -Wproperty-assign-default (which is enabled by default). */
929 if (warn_property_assign_default
930 /* If garbage collection is not being used, then 'assign' is
931 valid for objects (and typically used for delegates) but it
932 is wrong in most cases (since most objects need to be
933 retained or copied in setters). Warn users when 'assign' is
935 && property_assign_semantics
== OBJC_PROPERTY_ASSIGN
936 /* Read-only properties are never assigned, so the assignment
937 semantics do not matter in that case. */
938 && !property_readonly
941 /* Please note that it would make sense to default to 'assign'
942 for non-{Objective-C objects}, and to 'retain' for
943 Objective-C objects. But that would break compatibility with
945 if (!parsed_property_assign
&& !parsed_property_retain
&& !parsed_property_copy
)
947 /* Use 'false' so we do not warn for Class objects. */
948 if (objc_type_valid_for_messaging (TREE_TYPE (decl
), false))
950 warning_at (location
,
952 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
955 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
960 if (property_assign_semantics
== OBJC_PROPERTY_RETAIN
961 && !objc_type_valid_for_messaging (TREE_TYPE (decl
), true))
962 error_at (location
, "%<retain%> attribute is only valid for Objective-C objects");
964 if (property_assign_semantics
== OBJC_PROPERTY_COPY
965 && !objc_type_valid_for_messaging (TREE_TYPE (decl
), true))
966 error_at (location
, "%<copy%> attribute is only valid for Objective-C objects");
968 /* Now determine the final property getter and setter names. They
969 will be stored in the PROPERTY_DECL, from which they'll always be
970 extracted and used. */
972 /* Adjust, or fill in, setter and getter names. We overwrite the
973 parsed_property_setter_ident and parsed_property_getter_ident
974 with the final setter and getter identifiers that will be
976 if (parsed_property_setter_ident
)
978 /* The setter should be terminated by ':', but the parser only
979 gives us an identifier without ':'. So, we need to add ':'
981 const char *parsed_setter
= IDENTIFIER_POINTER (parsed_property_setter_ident
);
982 size_t length
= strlen (parsed_setter
);
983 char *final_setter
= (char *)alloca (length
+ 2);
985 sprintf (final_setter
, "%s:", parsed_setter
);
986 parsed_property_setter_ident
= get_identifier (final_setter
);
990 if (!property_readonly
)
991 parsed_property_setter_ident
= get_identifier (objc_build_property_setter_name
995 if (!parsed_property_getter_ident
)
996 parsed_property_getter_ident
= DECL_NAME (decl
);
998 /* Check for duplicate property declarations. We first check the
999 immediate context for a property with the same name. Any such
1000 declarations are an error, unless this is a class extension and
1001 we are extending a property from readonly to readwrite. */
1002 for (x
= CLASS_PROPERTY_DECL (objc_interface_context
); x
; x
= TREE_CHAIN (x
))
1004 if (PROPERTY_NAME (x
) == DECL_NAME (decl
))
1006 if (objc_in_class_extension
1007 && property_readonly
== 0
1008 && PROPERTY_READONLY (x
) == 1)
1010 /* This is a class extension, and we are extending an
1011 existing readonly property to a readwrite one.
1013 property_extension_in_class_extension
= true;
1018 location_t original_location
= DECL_SOURCE_LOCATION (x
);
1020 error_at (location
, "redeclaration of property %qD", decl
);
1022 if (original_location
!= UNKNOWN_LOCATION
)
1023 inform (original_location
, "originally specified here");
1029 /* If x is not NULL_TREE, we must be in a class extension and we're
1030 extending a readonly property. In that case, no point in
1031 searching for another declaration. */
1034 /* We now need to check for existing property declarations (in
1035 the superclass, other categories or protocols) and check that
1036 the new declaration is not in conflict with existing
1039 /* Search for a previous, existing declaration of a property
1040 with the same name in superclasses, protocols etc. If one is
1041 found, it will be in the 'x' variable. */
1043 /* Note that, for simplicity, the following may search again the
1044 local context. That's Ok as nothing will be found (else we'd
1045 have thrown an error above); it's only a little inefficient,
1046 but the code is simpler. */
1047 switch (TREE_CODE (objc_interface_context
))
1049 case CLASS_INTERFACE_TYPE
:
1050 /* Look up the property in the current @interface (which
1051 will find nothing), then its protocols and categories and
1053 x
= lookup_property (objc_interface_context
, DECL_NAME (decl
));
1055 case CATEGORY_INTERFACE_TYPE
:
1056 /* Look up the property in the main @interface, then
1057 protocols and categories (one of them is ours, and will
1058 find nothing) and superclasses. */
1059 x
= lookup_property (lookup_interface (CLASS_NAME (objc_interface_context
)),
1062 case PROTOCOL_INTERFACE_TYPE
:
1063 /* Looks up the property in any protocols attached to the
1064 current protocol. */
1065 if (PROTOCOL_LIST (objc_interface_context
))
1067 x
= lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context
),
1078 /* An existing property was found; check that it has the same
1079 types, or it is compatible. */
1080 location_t original_location
= DECL_SOURCE_LOCATION (x
);
1082 if (PROPERTY_NONATOMIC (x
) != parsed_property_nonatomic
)
1084 warning_at (location
, 0,
1085 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl
);
1087 if (original_location
!= UNKNOWN_LOCATION
)
1088 inform (original_location
, "originally specified here");
1092 if (PROPERTY_GETTER_NAME (x
) != parsed_property_getter_ident
)
1094 warning_at (location
, 0,
1095 "'getter' attribute of property %qD conflicts with previous declaration", decl
);
1097 if (original_location
!= UNKNOWN_LOCATION
)
1098 inform (original_location
, "originally specified here");
1102 /* We can only compare the setter names if both the old and new property have a setter. */
1103 if (!property_readonly
&& !PROPERTY_READONLY(x
))
1105 if (PROPERTY_SETTER_NAME (x
) != parsed_property_setter_ident
)
1107 warning_at (location
, 0,
1108 "'setter' attribute of property %qD conflicts with previous declaration", decl
);
1110 if (original_location
!= UNKNOWN_LOCATION
)
1111 inform (original_location
, "originally specified here");
1116 if (PROPERTY_ASSIGN_SEMANTICS (x
) != property_assign_semantics
)
1118 warning_at (location
, 0,
1119 "assign semantics attributes of property %qD conflict with previous declaration", decl
);
1121 if (original_location
!= UNKNOWN_LOCATION
)
1122 inform (original_location
, "originally specified here");
1126 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1127 if (PROPERTY_READONLY (x
) == 0 && property_readonly
== 1)
1129 warning_at (location
, 0,
1130 "'readonly' attribute of property %qD conflicts with previous declaration", decl
);
1132 if (original_location
!= UNKNOWN_LOCATION
)
1133 inform (original_location
, "originally specified here");
1137 /* We now check that the new and old property declarations have
1138 the same types (or compatible one). In the Objective-C
1139 tradition of loose type checking, we do type-checking but
1140 only generate warnings (not errors) if they do not match.
1141 For non-readonly properties, the types must match exactly;
1142 for readonly properties, it is allowed to use a "more
1143 specialized" type in the new property declaration. Eg, the
1144 superclass has a getter returning (NSArray *) and the
1145 subclass a getter returning (NSMutableArray *). The object's
1146 getter returns an (NSMutableArray *); but if you cast the
1147 object to the superclass, which is allowed, you'd still
1148 expect the getter to return an (NSArray *), which works since
1149 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1150 objects belonging to the type of the new @property should be
1151 a subset of the set of objects belonging to the type of the
1152 old @property. This is what "specialization" means. And the
1153 reason it only applies to readonly properties is that for a
1154 readwrite property the setter would have the opposite
1155 requirement - ie that the superclass type is more specialized
1156 then the subclass one; hence the only way to satisfy both
1157 constraints is that the types match. */
1159 /* If the types are not the same in the C sense, we warn ... */
1160 if (!comptypes (TREE_TYPE (x
), TREE_TYPE (decl
))
1161 /* ... unless the property is readonly, in which case we
1162 allow a new, more specialized, declaration. */
1163 && (!property_readonly
1164 || !objc_compare_types (TREE_TYPE (x
),
1165 TREE_TYPE (decl
), -5, NULL_TREE
)))
1167 warning_at (location
, 0,
1168 "type of property %qD conflicts with previous declaration", decl
);
1169 if (original_location
!= UNKNOWN_LOCATION
)
1170 inform (original_location
, "originally specified here");
1174 /* If we are in a class extension and we're extending a readonly
1175 property in the main @interface, we'll just update the
1176 existing property with the readwrite flag and potentially the
1178 if (property_extension_in_class_extension
)
1180 PROPERTY_READONLY (x
) = 0;
1181 PROPERTY_SETTER_NAME (x
) = parsed_property_setter_ident
;
1186 /* Create a PROPERTY_DECL node. */
1187 property_decl
= make_node (PROPERTY_DECL
);
1189 /* Copy the basic information from the original decl. */
1190 TREE_TYPE (property_decl
) = TREE_TYPE (decl
);
1191 DECL_SOURCE_LOCATION (property_decl
) = DECL_SOURCE_LOCATION (decl
);
1192 TREE_DEPRECATED (property_decl
) = TREE_DEPRECATED (decl
);
1194 /* Add property-specific information. */
1195 PROPERTY_NAME (property_decl
) = DECL_NAME (decl
);
1196 PROPERTY_GETTER_NAME (property_decl
) = parsed_property_getter_ident
;
1197 PROPERTY_SETTER_NAME (property_decl
) = parsed_property_setter_ident
;
1198 PROPERTY_READONLY (property_decl
) = property_readonly
;
1199 PROPERTY_NONATOMIC (property_decl
) = parsed_property_nonatomic
;
1200 PROPERTY_ASSIGN_SEMANTICS (property_decl
) = property_assign_semantics
;
1201 PROPERTY_IVAR_NAME (property_decl
) = NULL_TREE
;
1202 PROPERTY_DYNAMIC (property_decl
) = 0;
1204 /* Remember the fact that the property was found in the @optional
1205 section in a @protocol, or not. */
1206 if (objc_method_optional_flag
)
1207 PROPERTY_OPTIONAL (property_decl
) = 1;
1209 PROPERTY_OPTIONAL (property_decl
) = 0;
1211 /* Note that PROPERTY_GETTER_NAME is always set for all
1212 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1213 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1214 with a getter or setter, we should get the PROPERTY_DECL and use
1215 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1218 /* Add the PROPERTY_DECL to the list of properties for the class. */
1219 TREE_CHAIN (property_decl
) = CLASS_PROPERTY_DECL (objc_interface_context
);
1220 CLASS_PROPERTY_DECL (objc_interface_context
) = property_decl
;
1223 /* This is a subroutine of objc_maybe_build_component_ref. Search the
1224 list of methods in the interface (and, failing that, the local list
1225 in the implementation, and failing that, the protocol list)
1226 provided for a 'setter' or 'getter' for 'component' with default
1227 names (ie, if 'component' is "name", then search for "name" and
1228 "setName:"). It is also possible to specify a different
1229 'getter_name' (this is used for @optional readonly properties). If
1230 any is found, then create an artificial property that uses them.
1231 Return NULL_TREE if 'getter' or 'setter' could not be found. */
1233 maybe_make_artificial_property_decl (tree interface
, tree implementation
,
1234 tree protocol_list
, tree component
, bool is_class
,
1237 tree setter_name
= get_identifier (objc_build_property_setter_name (component
));
1238 tree getter
= NULL_TREE
;
1239 tree setter
= NULL_TREE
;
1241 if (getter_name
== NULL_TREE
)
1242 getter_name
= component
;
1244 /* First, check the @interface and all superclasses. */
1249 /* Using instance methods of the root class as accessors is most
1250 likely unwanted and can be extremely confusing (and, most
1251 importantly, other Objective-C 2.0 compilers do not do it).
1254 flags
= OBJC_LOOKUP_CLASS
| OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS
;
1256 getter
= lookup_method_static (interface
, getter_name
, flags
);
1257 setter
= lookup_method_static (interface
, setter_name
, flags
);
1260 /* Second, check the local @implementation context. */
1261 if (!getter
&& !setter
)
1267 getter
= lookup_method (CLASS_CLS_METHODS (implementation
), getter_name
);
1268 setter
= lookup_method (CLASS_CLS_METHODS (implementation
), setter_name
);
1272 getter
= lookup_method (CLASS_NST_METHODS (implementation
), getter_name
);
1273 setter
= lookup_method (CLASS_NST_METHODS (implementation
), setter_name
);
1278 /* Try the protocol_list if we didn't find anything in the
1279 @interface and in the @implementation. */
1280 if (!getter
&& !setter
)
1282 getter
= lookup_method_in_protocol_list (protocol_list
, getter_name
, is_class
);
1283 setter
= lookup_method_in_protocol_list (protocol_list
, setter_name
, is_class
);
1286 /* There needs to be at least a getter or setter for this to be a
1287 valid 'object.component' syntax. */
1288 if (getter
|| setter
)
1290 /* Yes ... determine the type of the expression. */
1295 type
= TREE_VALUE (TREE_TYPE (getter
));
1297 type
= TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter
)));
1299 /* Create an artificial property declaration with the
1300 information we collected on the type and getter/setter
1302 property_decl
= make_node (PROPERTY_DECL
);
1304 TREE_TYPE (property_decl
) = type
;
1305 DECL_SOURCE_LOCATION (property_decl
) = input_location
;
1306 TREE_DEPRECATED (property_decl
) = 0;
1307 DECL_ARTIFICIAL (property_decl
) = 1;
1309 /* Add property-specific information. Note that one of
1310 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1311 non-existing method; this will generate an error when the
1312 expression is later compiled. At this stage we don't know if
1313 the getter or setter will be used, so we can't generate an
1315 PROPERTY_NAME (property_decl
) = component
;
1316 PROPERTY_GETTER_NAME (property_decl
) = getter_name
;
1317 PROPERTY_SETTER_NAME (property_decl
) = setter_name
;
1318 PROPERTY_READONLY (property_decl
) = 0;
1319 PROPERTY_NONATOMIC (property_decl
) = 0;
1320 PROPERTY_ASSIGN_SEMANTICS (property_decl
) = 0;
1321 PROPERTY_IVAR_NAME (property_decl
) = NULL_TREE
;
1322 PROPERTY_DYNAMIC (property_decl
) = 0;
1323 PROPERTY_OPTIONAL (property_decl
) = 0;
1326 PROPERTY_HAS_NO_GETTER (property_decl
) = 1;
1328 /* The following is currently unused, but it's nice to have
1329 there. We may use it if we need in the future. */
1331 PROPERTY_HAS_NO_SETTER (property_decl
) = 1;
1333 return property_decl
;
1339 /* This hook routine is invoked by the parser when an expression such
1340 as 'xxx.yyy' is parsed. We get a chance to process these
1341 expressions in a way that is specified to Objective-C (to implement
1342 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1343 If the expression is not an Objective-C specified expression, we
1344 should return NULL_TREE; else we return the expression.
1346 At the moment this only implements dot-syntax and properties (not
1347 non-fragile ivars yet), ie 'object.property' or 'object.component'
1348 where 'component' is not a declared property, but a valid getter or
1349 setter for it could be found. */
1351 objc_maybe_build_component_ref (tree object
, tree property_ident
)
1356 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1358 if (flag_objc1_only
)
1361 /* Try to determine if 'object' is an Objective-C object or not. If
1363 if (object
== NULL_TREE
|| object
== error_mark_node
1364 || (rtype
= TREE_TYPE (object
)) == NULL_TREE
)
1367 if (property_ident
== NULL_TREE
|| property_ident
== error_mark_node
1368 || TREE_CODE (property_ident
) != IDENTIFIER_NODE
)
1371 /* The following analysis of 'object' is similar to the one used for
1372 the 'receiver' of a method invocation. We need to determine what
1373 'object' is and find the appropriate property (either declared,
1374 or artificial) for it (in the same way as we need to find the
1375 appropriate method prototype for a method invocation). There are
1376 some simplifications here though: "object.property" is invalid if
1377 "object" has a type of "id" or "Class"; it must at least have a
1378 protocol attached to it, and "object" is never a class name as
1379 that is done by objc_build_class_component_ref. Finally, we
1380 don't know if this really is a dot-syntax expression, so we want
1381 to make a quick exit if it is not; for this reason, we try to
1382 postpone checks after determining that 'object' looks like an
1383 Objective-C object. */
1385 if (objc_is_id (rtype
))
1387 /* This is the case that the 'object' is of type 'id' or
1390 /* Check if at least it is of type 'id <Protocol>' or 'Class
1391 <Protocol>'; if so, look the property up in the
1393 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype
)))
1395 tree rprotos
= TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype
));
1399 /* No point looking up declared @properties if we are
1400 dealing with a class. Classes have no declared
1402 if (!IS_CLASS (rtype
))
1403 x
= lookup_property_in_protocol_list (rprotos
, property_ident
);
1407 /* Ok, no property. Maybe it was an
1408 object.component dot-syntax without a declared
1409 property (this is valid for classes too). Look
1410 for getter/setter methods and internally declare
1411 an artifical property based on them if found. */
1412 x
= maybe_make_artificial_property_decl (NULL_TREE
,
1419 else if (PROPERTY_OPTIONAL (x
) && PROPERTY_READONLY (x
))
1421 /* This is a special, complicated case. If the
1422 property is optional, and is read-only, then the
1423 property is always used for reading, but an
1424 eventual existing non-property setter can be used
1425 for writing. We create an artificial property
1426 decl copying the getter from the optional
1427 property, and looking up the setter in the
1429 x
= maybe_make_artificial_property_decl (NULL_TREE
,
1434 PROPERTY_GETTER_NAME (x
));
1438 else if (objc_method_context
)
1440 /* Else, if we are inside a method it could be the case of
1441 'super' or 'self'. */
1442 tree interface_type
= NULL_TREE
;
1444 while (TREE_CODE (t
) == COMPOUND_EXPR
1445 || TREE_CODE (t
) == MODIFY_EXPR
1446 || CONVERT_EXPR_P (t
)
1447 || TREE_CODE (t
) == COMPONENT_REF
)
1448 t
= TREE_OPERAND (t
, 0);
1450 if (t
== UOBJC_SUPER_decl
)
1451 interface_type
= lookup_interface (CLASS_SUPER_NAME (implementation_template
));
1452 else if (t
== self_decl
)
1453 interface_type
= lookup_interface (CLASS_NAME (implementation_template
));
1457 if (TREE_CODE (objc_method_context
) != CLASS_METHOD_DECL
)
1458 x
= lookup_property (interface_type
, property_ident
);
1462 /* Try the dot-syntax without a declared property.
1463 If this is an access to 'self', it is possible
1464 that they may refer to a setter/getter that is
1465 not declared in the interface, but exists locally
1466 in the implementation. In that case, get the
1467 implementation context and use it. */
1468 tree implementation
= NULL_TREE
;
1471 implementation
= objc_implementation_context
;
1473 x
= maybe_make_artificial_property_decl
1474 (interface_type
, implementation
, NULL_TREE
,
1476 (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
),
1479 else if (PROPERTY_OPTIONAL (x
) && PROPERTY_READONLY (x
))
1481 tree implementation
= NULL_TREE
;
1484 implementation
= objc_implementation_context
;
1486 x
= maybe_make_artificial_property_decl (interface_type
,
1491 PROPERTY_GETTER_NAME (x
));
1498 /* This is the case where we have more information on 'rtype'. */
1499 tree basetype
= TYPE_MAIN_VARIANT (rtype
);
1501 /* Skip the pointer - if none, it's not an Objective-C object or
1503 if (basetype
!= NULL_TREE
&& TREE_CODE (basetype
) == POINTER_TYPE
)
1504 basetype
= TREE_TYPE (basetype
);
1508 /* Traverse typedefs. */
1509 while (basetype
!= NULL_TREE
1510 && TREE_CODE (basetype
) == RECORD_TYPE
1511 && OBJC_TYPE_NAME (basetype
)
1512 && TREE_CODE (OBJC_TYPE_NAME (basetype
)) == TYPE_DECL
1513 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype
)))
1514 basetype
= DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype
));
1516 if (basetype
!= NULL_TREE
&& TYPED_OBJECT (basetype
))
1518 tree interface_type
= TYPE_OBJC_INTERFACE (basetype
);
1519 tree protocol_list
= TYPE_OBJC_PROTOCOL_LIST (basetype
);
1522 && (TREE_CODE (interface_type
) == CLASS_INTERFACE_TYPE
1523 || TREE_CODE (interface_type
) == CATEGORY_INTERFACE_TYPE
1524 || TREE_CODE (interface_type
) == PROTOCOL_INTERFACE_TYPE
))
1526 /* Not sure 'rtype' could ever be a class here! Just
1527 for safety we keep the checks. */
1528 if (!IS_CLASS (rtype
))
1530 x
= lookup_property (interface_type
, property_ident
);
1533 x
= lookup_property_in_protocol_list (protocol_list
,
1539 /* Try the dot-syntax without a declared property.
1540 If we are inside a method implementation, it is
1541 possible that they may refer to a setter/getter
1542 that is not declared in the interface, but exists
1543 locally in the implementation. In that case, get
1544 the implementation context and use it. */
1545 tree implementation
= NULL_TREE
;
1547 if (objc_implementation_context
1548 && CLASS_NAME (objc_implementation_context
)
1549 == OBJC_TYPE_NAME (interface_type
))
1550 implementation
= objc_implementation_context
;
1552 x
= maybe_make_artificial_property_decl (interface_type
,
1559 else if (PROPERTY_OPTIONAL (x
) && PROPERTY_READONLY (x
))
1561 tree implementation
= NULL_TREE
;
1563 if (objc_implementation_context
1564 && CLASS_NAME (objc_implementation_context
)
1565 == OBJC_TYPE_NAME (interface_type
))
1566 implementation
= objc_implementation_context
;
1568 x
= maybe_make_artificial_property_decl (interface_type
,
1573 PROPERTY_GETTER_NAME (x
));
1583 tree deprecated_method_prototype
= NULL_TREE
;
1585 /* We have an additional nasty problem here; if this
1586 PROPERTY_REF needs to become a 'getter', then the conversion
1587 from PROPERTY_REF into a getter call happens in gimplify,
1588 after the selector table has already been generated and when
1589 it is too late to add another selector to it. To work around
1590 the problem, we always create the getter call at this stage,
1591 which puts the selector in the table. Note that if the
1592 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1593 we have added a selector too many to the selector table.
1594 This is a little inefficient.
1596 Also note that method calls to 'self' and 'super' require the
1597 context (self_decl, UOBJS_SUPER_decl,
1598 objc_implementation_context etc) to be built correctly; this
1599 is yet another reason why building the call at the gimplify
1600 stage (when this context has been lost) is not very
1601 practical. If we build it at this stage, we know it will
1602 always be built correctly.
1604 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1605 property decl created to deal with a dotsyntax not really
1606 referring to an existing property) then do not try to build a
1607 call to the getter as there is no getter. */
1608 if (PROPERTY_HAS_NO_GETTER (x
))
1609 getter_call
= NULL_TREE
;
1611 getter_call
= objc_finish_message_expr
1612 (object
, PROPERTY_GETTER_NAME (x
), NULL_TREE
,
1613 /* Disable the immediate deprecation warning if the getter
1614 is deprecated, but record the fact that the getter is
1615 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1616 the method prototype. */
1617 &deprecated_method_prototype
);
1619 expression
= build4 (PROPERTY_REF
, TREE_TYPE(x
), object
, x
, getter_call
,
1620 deprecated_method_prototype
);
1621 SET_EXPR_LOCATION (expression
, input_location
);
1622 TREE_SIDE_EFFECTS (expression
) = 1;
1630 /* This hook routine is invoked by the parser when an expression such
1631 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1632 Objective-C 2.0 dot-syntax applied to classes, so we need to
1633 convert it into a setter/getter call on the class. */
1635 objc_build_class_component_ref (tree class_name
, tree property_ident
)
1640 if (flag_objc1_only
)
1641 error_at (input_location
, "the dot syntax is not available in Objective-C 1.0");
1643 if (class_name
== NULL_TREE
|| class_name
== error_mark_node
1644 || TREE_CODE (class_name
) != IDENTIFIER_NODE
)
1645 return error_mark_node
;
1647 if (property_ident
== NULL_TREE
|| property_ident
== error_mark_node
1648 || TREE_CODE (property_ident
) != IDENTIFIER_NODE
)
1651 object
= objc_get_class_reference (class_name
);
1654 /* We know that 'class_name' is an Objective-C class name as the
1655 parser won't call this function if it is not. This is only a
1656 double-check for safety. */
1657 error_at (input_location
, "could not find class %qE", class_name
);
1658 return error_mark_node
;
1661 rtype
= lookup_interface (class_name
);
1664 /* Again, this should never happen, but we do check. */
1665 error_at (input_location
, "could not find interface for class %qE", class_name
);
1666 return error_mark_node
;
1670 if (TREE_DEPRECATED (rtype
))
1671 warning (OPT_Wdeprecated_declarations
, "class %qE is deprecated", class_name
);
1674 x
= maybe_make_artificial_property_decl (rtype
, NULL_TREE
, NULL_TREE
,
1682 tree deprecated_method_prototype
= NULL_TREE
;
1684 if (PROPERTY_HAS_NO_GETTER (x
))
1685 getter_call
= NULL_TREE
;
1687 getter_call
= objc_finish_message_expr
1688 (object
, PROPERTY_GETTER_NAME (x
), NULL_TREE
,
1689 &deprecated_method_prototype
);
1691 expression
= build4 (PROPERTY_REF
, TREE_TYPE(x
), object
, x
, getter_call
,
1692 deprecated_method_prototype
);
1693 SET_EXPR_LOCATION (expression
, input_location
);
1694 TREE_SIDE_EFFECTS (expression
) = 1;
1700 error_at (input_location
, "could not find setter/getter for %qE in class %qE",
1701 property_ident
, class_name
);
1702 return error_mark_node
;
1710 /* This is used because we don't want to expose PROPERTY_REF to the
1711 C/C++ frontends. Maybe we should! */
1713 objc_is_property_ref (tree node
)
1715 if (node
&& TREE_CODE (node
) == PROPERTY_REF
)
1721 /* This function builds a setter call for a PROPERTY_REF (real, for a
1722 declared property, or artificial, for a dot-syntax accessor which
1723 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1724 (the caller must check this beforehand). 'rhs' is the value to
1725 assign to the property. A plain setter call is returned, or
1726 error_mark_node if the property is readonly. */
1729 objc_build_setter_call (tree lhs
, tree rhs
)
1731 tree object_expr
= PROPERTY_REF_OBJECT (lhs
);
1732 tree property_decl
= PROPERTY_REF_PROPERTY_DECL (lhs
);
1734 if (PROPERTY_READONLY (property_decl
))
1736 error ("readonly property can not be set");
1737 return error_mark_node
;
1741 tree setter_argument
= build_tree_list (NULL_TREE
, rhs
);
1744 /* TODO: Check that the setter return type is 'void'. */
1746 /* TODO: Decay arguments in C. */
1747 setter
= objc_finish_message_expr (object_expr
,
1748 PROPERTY_SETTER_NAME (property_decl
),
1749 setter_argument
, NULL
);
1753 /* Unreachable, but the compiler may not realize. */
1754 return error_mark_node
;
1757 /* This hook routine is called when a MODIFY_EXPR is being built. We
1758 check what is being modified; if it is a PROPERTY_REF, we need to
1759 generate a 'setter' function call for the property. If this is not
1760 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1761 on creating their MODIFY_EXPR.
1763 This is used for example if you write
1767 where 'count' is a property. The left-hand side creates a
1768 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1769 to assign something to it. We intercept that here, and generate a
1770 call to the 'setter' method instead. */
1772 objc_maybe_build_modify_expr (tree lhs
, tree rhs
)
1774 if (lhs
&& TREE_CODE (lhs
) == PROPERTY_REF
)
1776 /* Building a simple call to the setter method would work for cases such as
1780 but wouldn't work for cases such as
1782 count = object2.count = 1;
1784 to get these to work with very little effort, we build a
1785 compound statement which does the setter call (to set the
1786 property to 'rhs'), but which can also be evaluated returning
1787 the 'rhs'. If the 'rhs' has no side effects, we can simply
1788 evaluate it twice, building
1790 ([object setProperty: rhs]; rhs)
1792 If it has side effects, we put it in a temporary variable first,
1793 so we create the following:
1795 (temp = rhs; [object setProperty: temp]; temp)
1797 setter_argument is rhs in the first case, and temp in the second
1800 tree setter_argument
;
1802 /* s1, s2 and s3 are the tree statements that we need in the
1803 compound expression. */
1804 tree s1
, s2
, s3
, compound_expr
;
1806 if (TREE_SIDE_EFFECTS (rhs
))
1810 /* Declare __objc_property_temp in a local bind. */
1811 setter_argument
= objc_create_temporary_var (TREE_TYPE (rhs
), "__objc_property_temp");
1812 DECL_SOURCE_LOCATION (setter_argument
) = input_location
;
1813 bind
= build3 (BIND_EXPR
, void_type_node
, setter_argument
, NULL
, NULL
);
1814 SET_EXPR_LOCATION (bind
, input_location
);
1815 TREE_SIDE_EFFECTS (bind
) = 1;
1819 s1
= build_modify_expr (input_location
, setter_argument
, NULL_TREE
,
1821 input_location
, rhs
, NULL_TREE
);
1822 SET_EXPR_LOCATION (s1
, input_location
);
1827 setter_argument
= rhs
;
1831 /* Now build the compound statement. */
1833 /* s2: [object setProperty: x] */
1834 s2
= objc_build_setter_call (lhs
, setter_argument
);
1836 /* This happens if building the setter failed because the
1837 property is readonly. */
1838 if (s2
== error_mark_node
)
1839 return error_mark_node
;
1841 SET_EXPR_LOCATION (s2
, input_location
);
1844 s3
= convert (TREE_TYPE (lhs
), setter_argument
);
1846 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1849 compound_expr
= build_compound_expr (input_location
, build_compound_expr (input_location
, s1
, s2
), s3
);
1851 compound_expr
= build_compound_expr (input_location
, s2
, s3
);
1853 /* Without this, with -Wall you get a 'valued computed is not
1854 used' every time there is a "object.property = x" where the
1855 value of the resulting MODIFY_EXPR is not used. That is
1856 correct (maybe a more sophisticated implementation could
1857 avoid generating the compound expression if not needed), but
1858 we need to turn it off. */
1859 TREE_NO_WARNING (compound_expr
) = 1;
1860 return compound_expr
;
1866 /* This hook is called by the frontend when one of the four unary
1867 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1868 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1869 argument which is a PROPERTY_REF. For example, this happens if you have
1873 where 'count' is a property. We need to use the 'getter' and
1874 'setter' for the property in an appropriate way to build the
1875 appropriate expression. 'code' is the code for the expression (one
1876 of the four mentioned above); 'argument' is the PROPERTY_REF, and
1877 'increment' is how much we need to add or subtract. */
1879 objc_build_incr_expr_for_property_ref (location_t location
,
1880 enum tree_code code
,
1881 tree argument
, tree increment
)
1883 /* Here are the expressions that we want to build:
1885 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1886 (temp = [object property] +/- increment, [object setProperty: temp], temp)
1888 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1889 (temp = [object property], [object setProperty: temp +/- increment], temp) */
1891 tree temp_variable_decl
, bind
;
1892 /* s1, s2 and s3 are the tree statements that we need in the
1893 compound expression. */
1894 tree s1
, s2
, s3
, compound_expr
;
1897 if (!argument
|| TREE_CODE (argument
) != PROPERTY_REF
)
1898 return error_mark_node
;
1900 /* Declare __objc_property_temp in a local bind. */
1901 temp_variable_decl
= objc_create_temporary_var (TREE_TYPE (argument
), "__objc_property_temp");
1902 DECL_SOURCE_LOCATION (temp_variable_decl
) = location
;
1903 bind
= build3 (BIND_EXPR
, void_type_node
, temp_variable_decl
, NULL
, NULL
);
1904 SET_EXPR_LOCATION (bind
, location
);
1905 TREE_SIDE_EFFECTS (bind
) = 1;
1908 /* Now build the compound statement. */
1910 /* Note that the 'getter' is generated at gimplify time; at this
1911 time, we can simply put the property_ref (ie, argument) wherever
1912 we want the getter ultimately to be. */
1914 /* s1: __objc_property_temp = [object property] <+/- increment> */
1917 case PREINCREMENT_EXPR
:
1918 /* __objc_property_temp = [object property] + increment */
1919 s1
= build_modify_expr (location
, temp_variable_decl
, NULL_TREE
,
1921 location
, build2 (PLUS_EXPR
, TREE_TYPE (argument
),
1922 argument
, increment
), NULL_TREE
);
1924 case PREDECREMENT_EXPR
:
1925 /* __objc_property_temp = [object property] - increment */
1926 s1
= build_modify_expr (location
, temp_variable_decl
, NULL_TREE
,
1928 location
, build2 (MINUS_EXPR
, TREE_TYPE (argument
),
1929 argument
, increment
), NULL_TREE
);
1931 case POSTINCREMENT_EXPR
:
1932 case POSTDECREMENT_EXPR
:
1933 /* __objc_property_temp = [object property] */
1934 s1
= build_modify_expr (location
, temp_variable_decl
, NULL_TREE
,
1936 location
, argument
, NULL_TREE
);
1942 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1945 case PREINCREMENT_EXPR
:
1946 case PREDECREMENT_EXPR
:
1947 /* [object setProperty: __objc_property_temp] */
1948 s2
= objc_build_setter_call (argument
, temp_variable_decl
);
1950 case POSTINCREMENT_EXPR
:
1951 /* [object setProperty: __objc_property_temp + increment] */
1952 s2
= objc_build_setter_call (argument
,
1953 build2 (PLUS_EXPR
, TREE_TYPE (argument
),
1954 temp_variable_decl
, increment
));
1956 case POSTDECREMENT_EXPR
:
1957 /* [object setProperty: __objc_property_temp - increment] */
1958 s2
= objc_build_setter_call (argument
,
1959 build2 (MINUS_EXPR
, TREE_TYPE (argument
),
1960 temp_variable_decl
, increment
));
1966 /* This happens if building the setter failed because the property
1968 if (s2
== error_mark_node
)
1969 return error_mark_node
;
1971 SET_EXPR_LOCATION (s2
, location
);
1973 /* s3: __objc_property_temp */
1974 s3
= convert (TREE_TYPE (argument
), temp_variable_decl
);
1976 /* Now build the compound statement (s1, s2, s3) */
1977 compound_expr
= build_compound_expr (location
, build_compound_expr (location
, s1
, s2
), s3
);
1979 /* Prevent C++ from warning with -Wall that "right operand of comma
1980 operator has no effect". */
1981 TREE_NO_WARNING (compound_expr
) = 1;
1982 return compound_expr
;
1986 objc_build_method_signature (bool is_class_method
, tree rettype
, tree selector
,
1987 tree optparms
, bool ellipsis
)
1989 if (is_class_method
)
1990 return build_method_decl (CLASS_METHOD_DECL
, rettype
, selector
,
1991 optparms
, ellipsis
);
1993 return build_method_decl (INSTANCE_METHOD_DECL
, rettype
, selector
,
1994 optparms
, ellipsis
);
1998 objc_add_method_declaration (bool is_class_method
, tree decl
, tree attributes
)
2000 if (!objc_interface_context
)
2002 /* PS: At the moment, due to how the parser works, it should be
2003 impossible to get here. But it's good to have the check in
2004 case the parser changes.
2006 fatal_error ("method declaration not in @interface context");
2009 if (flag_objc1_only
&& attributes
)
2010 error_at (input_location
, "method attributes are not available in Objective-C 1.0");
2012 objc_decl_method_attributes (&decl
, attributes
, 0);
2013 objc_add_method (objc_interface_context
,
2016 objc_method_optional_flag
);
2019 /* Return 'true' if the method definition could be started, and
2020 'false' if not (because we are outside an @implementation context).
2021 EXPR is NULL or an expression that needs to be evaluated for the
2022 side effects of array size expressions in the parameters.
2025 objc_start_method_definition (bool is_class_method
, tree decl
, tree attributes
,
2028 if (!objc_implementation_context
)
2030 error ("method definition not in @implementation context");
2034 if (decl
!= NULL_TREE
&& METHOD_SEL_NAME (decl
) == error_mark_node
)
2038 /* Indicate no valid break/continue context by setting these variables
2039 to some non-null, non-label value. We'll notice and emit the proper
2040 error message in c_finish_bc_stmt. */
2041 c_break_label
= c_cont_label
= size_zero_node
;
2045 warning_at (input_location
, 0, "method attributes can not be specified in @implementation context");
2047 objc_decl_method_attributes (&decl
, attributes
, 0);
2049 objc_add_method (objc_implementation_context
,
2052 /* is optional */ false);
2053 start_method_def (decl
, expr
);
2058 objc_add_instance_variable (tree decl
)
2060 (void) add_instance_variable (objc_ivar_context
,
2061 objc_ivar_visibility
,
2065 /* Construct a C struct with same name as KLASS, a base struct with tag
2066 SUPER_NAME (if any), and FIELDS indicated. */
2069 objc_build_struct (tree klass
, tree fields
, tree super_name
)
2071 tree name
= CLASS_NAME (klass
);
2072 tree s
= objc_start_struct (name
);
2073 tree super
= (super_name
? xref_tag (RECORD_TYPE
, super_name
) : NULL_TREE
);
2075 VEC(tree
,heap
) *objc_info
= NULL
;
2080 /* Prepend a packed variant of the base class into the layout. This
2081 is necessary to preserve ObjC ABI compatibility. */
2082 tree base
= build_decl (input_location
,
2083 FIELD_DECL
, NULL_TREE
, super
);
2084 tree field
= TYPE_FIELDS (super
);
2086 while (field
&& DECL_CHAIN (field
)
2087 && TREE_CODE (DECL_CHAIN (field
)) == FIELD_DECL
)
2088 field
= DECL_CHAIN (field
);
2090 /* For ObjC ABI purposes, the "packed" size of a base class is
2091 the sum of the offset and the size (in bits) of the last field
2094 = (field
&& TREE_CODE (field
) == FIELD_DECL
2095 ? size_binop (PLUS_EXPR
,
2096 size_binop (PLUS_EXPR
,
2099 convert (bitsizetype
,
2100 DECL_FIELD_OFFSET (field
)),
2101 bitsize_int (BITS_PER_UNIT
)),
2102 DECL_FIELD_BIT_OFFSET (field
)),
2104 : bitsize_zero_node
);
2105 DECL_SIZE_UNIT (base
)
2106 = size_binop (FLOOR_DIV_EXPR
, convert (sizetype
, DECL_SIZE (base
)),
2107 size_int (BITS_PER_UNIT
));
2108 DECL_ARTIFICIAL (base
) = 1;
2109 DECL_ALIGN (base
) = 1;
2110 DECL_FIELD_CONTEXT (base
) = s
;
2112 DECL_FIELD_IS_BASE (base
) = 1;
2115 TREE_NO_WARNING (fields
) = 1; /* Suppress C++ ABI warnings -- we */
2116 #endif /* are following the ObjC ABI here. */
2117 DECL_CHAIN (base
) = fields
;
2121 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2122 information in all variants of this RECORD_TYPE to be destroyed
2123 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2124 for something else and then will change all variants to use the
2125 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2126 it for ObjC protocols and that such propagation will make all
2127 variants use the same objc_info), but it is therein that we store
2128 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2129 Hence, we must save the ObjC-specific information before calling
2130 finish_struct(), and then reinstate it afterwards. */
2132 for (t
= TYPE_MAIN_VARIANT (s
); t
; t
= TYPE_NEXT_VARIANT (t
))
2134 INIT_TYPE_OBJC_INFO (t
);
2135 VEC_safe_push (tree
, heap
, objc_info
, TYPE_OBJC_INFO (t
));
2138 s
= objc_finish_struct (s
, fields
);
2140 for (i
= 0, t
= TYPE_MAIN_VARIANT (s
); t
; t
= TYPE_NEXT_VARIANT (t
), i
++)
2142 /* We now want to restore the different TYPE_OBJC_INFO, but we
2143 have the additional problem that the C frontend doesn't just
2144 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2145 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2146 we need a different TYPE_OBJC_INFO for each (and
2147 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2148 make a copy of each TYPE_LANG_SPECIFIC before we modify
2150 if (TYPE_LANG_SPECIFIC (t
))
2152 /* Create a copy of TYPE_LANG_SPECIFIC. */
2153 struct lang_type
*old_lang_type
= TYPE_LANG_SPECIFIC (t
);
2154 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t
);
2155 memcpy (TYPE_LANG_SPECIFIC (t
), old_lang_type
,
2156 SIZEOF_OBJC_TYPE_LANG_SPECIFIC
);
2160 /* Just create a new one. */
2161 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t
);
2163 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2164 protocol information that may have been associated with the
2166 TYPE_OBJC_INFO (t
) = VEC_index (tree
, objc_info
, i
);
2167 /* Replace the IDENTIFIER_NODE with an actual @interface now
2169 TYPE_OBJC_INTERFACE (t
) = klass
;
2171 VEC_free (tree
, heap
, objc_info
);
2173 /* Use TYPE_BINFO structures to point at the super class, if any. */
2174 objc_xref_basetypes (s
, super
);
2176 /* Mark this struct as a class template. */
2177 CLASS_STATIC_TEMPLATE (klass
) = s
;
2182 /* Mark DECL as being 'volatile' for purposes of Darwin
2183 _setjmp()/_longjmp() exception handling. Called from
2184 objc_mark_locals_volatile(). */
2186 objc_volatilize_decl (tree decl
)
2188 /* Do not mess with variables that are 'static' or (already)
2190 if (!TREE_THIS_VOLATILE (decl
) && !TREE_STATIC (decl
)
2191 && (TREE_CODE (decl
) == VAR_DECL
2192 || TREE_CODE (decl
) == PARM_DECL
))
2194 if (local_variables_to_volatilize
== NULL
)
2195 local_variables_to_volatilize
= VEC_alloc (tree
, gc
, 8);
2197 VEC_safe_push (tree
, gc
, local_variables_to_volatilize
, decl
);
2201 /* Called when parsing of a function completes; if any local variables
2202 in the function were marked as variables to volatilize, change them
2203 to volatile. We do this at the end of the function when the
2204 warnings about discarding 'volatile' have already been produced.
2205 We are making the variables as volatile just to force the compiler
2206 to preserve them between setjmp/longjmp, but we don't want warnings
2207 for them as they aren't really volatile. */
2209 objc_finish_function (void)
2211 /* If there are any local variables to volatilize, volatilize them. */
2212 if (local_variables_to_volatilize
)
2216 FOR_EACH_VEC_ELT (tree
, local_variables_to_volatilize
, i
, decl
)
2218 tree t
= TREE_TYPE (decl
);
2220 t
= build_qualified_type (t
, TYPE_QUALS (t
) | TYPE_QUAL_VOLATILE
);
2221 TREE_TYPE (decl
) = t
;
2222 TREE_THIS_VOLATILE (decl
) = 1;
2223 TREE_SIDE_EFFECTS (decl
) = 1;
2224 DECL_REGISTER (decl
) = 0;
2226 C_DECL_REGISTER (decl
) = 0;
2230 /* Now we delete the vector. This sets it to NULL as well. */
2231 VEC_free (tree
, gc
, local_variables_to_volatilize
);
2235 /* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
2236 (including its categories and superclasses) or by object type TYP.
2237 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2240 objc_lookup_protocol (tree proto
, tree cls
, tree typ
, bool warn
)
2242 bool class_type
= (cls
!= NULL_TREE
);
2248 /* Check protocols adopted by the class and its categories. */
2249 for (c
= cls
; c
; c
= CLASS_CATEGORY_LIST (c
))
2251 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c
), proto
))
2255 /* Repeat for superclasses. */
2256 cls
= lookup_interface (CLASS_SUPER_NAME (cls
));
2259 /* Check for any protocols attached directly to the object type. */
2260 if (TYPE_HAS_OBJC_INFO (typ
))
2262 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ
), proto
))
2269 gen_type_name_0 (class_type
? typ
: TYPE_POINTER_TO (typ
));
2270 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2271 "implementing" a given protocol, since they do not have an
2274 warning (0, "class %qs does not implement the %qE protocol",
2275 identifier_to_locale (errbuf
), PROTOCOL_NAME (proto
));
2277 warning (0, "type %qs does not conform to the %qE protocol",
2278 identifier_to_locale (errbuf
), PROTOCOL_NAME (proto
));
2284 /* Check if class RCLS and instance struct type RTYP conform to at least the
2285 same protocols that LCLS and LTYP conform to. */
2288 objc_compare_protocols (tree lcls
, tree ltyp
, tree rcls
, tree rtyp
, bool warn
)
2291 bool have_lproto
= false;
2295 /* NB: We do _not_ look at categories defined for LCLS; these may or
2296 may not get loaded in, and therefore it is unreasonable to require
2297 that RCLS/RTYP must implement any of their protocols. */
2298 for (p
= CLASS_PROTOCOL_LIST (lcls
); p
; p
= TREE_CHAIN (p
))
2302 if (!objc_lookup_protocol (TREE_VALUE (p
), rcls
, rtyp
, warn
))
2306 /* Repeat for superclasses. */
2307 lcls
= lookup_interface (CLASS_SUPER_NAME (lcls
));
2310 /* Check for any protocols attached directly to the object type. */
2311 if (TYPE_HAS_OBJC_INFO (ltyp
))
2313 for (p
= TYPE_OBJC_PROTOCOL_LIST (ltyp
); p
; p
= TREE_CHAIN (p
))
2317 if (!objc_lookup_protocol (TREE_VALUE (p
), rcls
, rtyp
, warn
))
2322 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2323 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2324 away with simply checking for 'id' or 'Class' (!RCLS), since this
2325 routine will not get called in other cases. */
2326 return have_lproto
|| (rcls
!= NULL_TREE
);
2329 /* Given two types TYPE1 and TYPE2, return their least common ancestor.
2330 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2331 compatible by objc_compare_types() below. */
2334 objc_common_type (tree type1
, tree type2
)
2336 tree inner1
= TREE_TYPE (type1
), inner2
= TREE_TYPE (type2
);
2338 while (POINTER_TYPE_P (inner1
))
2340 inner1
= TREE_TYPE (inner1
);
2341 inner2
= TREE_TYPE (inner2
);
2344 /* If one type is derived from another, return the base type. */
2345 if (DERIVED_FROM_P (inner1
, inner2
))
2347 else if (DERIVED_FROM_P (inner2
, inner1
))
2350 /* If both types are 'Class', return 'Class'. */
2351 if (objc_is_class_id (inner1
) && objc_is_class_id (inner2
))
2352 return objc_class_type
;
2354 /* Otherwise, return 'id'. */
2355 return objc_object_type
;
2358 /* Determine if it is permissible to assign (if ARGNO is greater than -3)
2359 an instance of RTYP to an instance of LTYP or to compare the two
2360 (if ARGNO is equal to -3), per ObjC type system rules. Before
2361 returning 'true', this routine may issue warnings related to, e.g.,
2362 protocol conformance. When returning 'false', the routine must
2363 produce absolutely no warnings; the C or C++ front-end will do so
2364 instead, if needed. If either LTYP or RTYP is not an Objective-C
2365 type, the routine must return 'false'.
2367 The ARGNO parameter is encoded as follows:
2368 >= 1 Parameter number (CALLEE contains function being called);
2372 -3 Comparison (LTYP and RTYP may match in either direction);
2373 -4 Silent comparison (for C++ overload resolution);
2374 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2375 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
2376 so that each object of type RTYP is also of type LTYP). This is used
2377 when comparing property types. */
2380 objc_compare_types (tree ltyp
, tree rtyp
, int argno
, tree callee
)
2382 tree lcls
, rcls
, lproto
, rproto
;
2383 bool pointers_compatible
;
2385 /* We must be dealing with pointer types */
2386 if (!POINTER_TYPE_P (ltyp
) || !POINTER_TYPE_P (rtyp
))
2391 ltyp
= TREE_TYPE (ltyp
); /* Remove indirections. */
2392 rtyp
= TREE_TYPE (rtyp
);
2394 while (POINTER_TYPE_P (ltyp
) && POINTER_TYPE_P (rtyp
));
2396 /* We must also handle function pointers, since ObjC is a bit more
2397 lenient than C or C++ on this. */
2398 if (TREE_CODE (ltyp
) == FUNCTION_TYPE
&& TREE_CODE (rtyp
) == FUNCTION_TYPE
)
2400 function_args_iterator liter
, riter
;
2402 /* Return types must be covariant. */
2403 if (!comptypes (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
))
2404 && !objc_compare_types (TREE_TYPE (ltyp
), TREE_TYPE (rtyp
),
2408 /* Argument types must be contravariant. */
2409 function_args_iter_init (&liter
, ltyp
);
2410 function_args_iter_init (&riter
, rtyp
);
2414 ltyp
= function_args_iter_cond (&liter
);
2415 rtyp
= function_args_iter_cond (&riter
);
2417 /* If we've exhaused both lists simulateously, we're done. */
2418 if (ltyp
== NULL_TREE
&& rtyp
== NULL_TREE
)
2421 /* If one list is shorter than the other, they fail to match. */
2422 if (ltyp
== NULL_TREE
|| rtyp
== NULL_TREE
)
2425 if (!comptypes (rtyp
, ltyp
)
2426 && !objc_compare_types (rtyp
, ltyp
, argno
, callee
))
2429 function_args_iter_next (&liter
);
2430 function_args_iter_next (&riter
);
2436 /* Past this point, we are only interested in ObjC class instances,
2437 or 'id' or 'Class'. */
2438 if (TREE_CODE (ltyp
) != RECORD_TYPE
|| TREE_CODE (rtyp
) != RECORD_TYPE
)
2441 if (!objc_is_object_id (ltyp
) && !objc_is_class_id (ltyp
)
2442 && !TYPE_HAS_OBJC_INFO (ltyp
))
2445 if (!objc_is_object_id (rtyp
) && !objc_is_class_id (rtyp
)
2446 && !TYPE_HAS_OBJC_INFO (rtyp
))
2449 /* Past this point, we are committed to returning 'true' to the caller
2450 (unless performing a silent comparison; see below). However, we can
2451 still warn about type and/or protocol mismatches. */
2453 if (TYPE_HAS_OBJC_INFO (ltyp
))
2455 lcls
= TYPE_OBJC_INTERFACE (ltyp
);
2456 lproto
= TYPE_OBJC_PROTOCOL_LIST (ltyp
);
2459 lcls
= lproto
= NULL_TREE
;
2461 if (TYPE_HAS_OBJC_INFO (rtyp
))
2463 rcls
= TYPE_OBJC_INTERFACE (rtyp
);
2464 rproto
= TYPE_OBJC_PROTOCOL_LIST (rtyp
);
2467 rcls
= rproto
= NULL_TREE
;
2469 /* If we could not find an @interface declaration, we must have
2470 only seen a @class declaration; for purposes of type comparison,
2471 treat it as a stand-alone (root) class. */
2473 if (lcls
&& TREE_CODE (lcls
) == IDENTIFIER_NODE
)
2476 if (rcls
&& TREE_CODE (rcls
) == IDENTIFIER_NODE
)
2479 /* If either type is an unqualified 'id', we're done. This is because
2480 an 'id' can be assigned to or from any type with no warnings. */
2483 if ((!lproto
&& objc_is_object_id (ltyp
))
2484 || (!rproto
&& objc_is_object_id (rtyp
)))
2489 /* For property checks, though, an 'id' is considered the most
2490 general type of object, hence if you try to specialize an
2491 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2493 if (!lproto
&& objc_is_object_id (ltyp
))
2497 pointers_compatible
= (TYPE_MAIN_VARIANT (ltyp
) == TYPE_MAIN_VARIANT (rtyp
));
2499 /* If the underlying types are the same, and at most one of them has
2500 a protocol list, we do not need to issue any diagnostics. */
2501 if (pointers_compatible
&& (!lproto
|| !rproto
))
2504 /* If exactly one of the types is 'Class', issue a diagnostic; any
2505 exceptions of this rule have already been handled. */
2506 if (objc_is_class_id (ltyp
) ^ objc_is_class_id (rtyp
))
2507 pointers_compatible
= false;
2508 /* Otherwise, check for inheritance relations. */
2511 if (!pointers_compatible
)
2513 /* Again, if any of the two is an 'id', we're satisfied,
2514 unless we're comparing properties, in which case only an
2515 'id' on the left-hand side (old property) is good
2519 = (objc_is_object_id (ltyp
) || objc_is_object_id (rtyp
));
2521 pointers_compatible
= objc_is_object_id (ltyp
);
2524 if (!pointers_compatible
)
2525 pointers_compatible
= DERIVED_FROM_P (ltyp
, rtyp
);
2527 if (!pointers_compatible
&& (argno
== -3 || argno
== -4))
2528 pointers_compatible
= DERIVED_FROM_P (rtyp
, ltyp
);
2531 /* If the pointers match modulo protocols, check for protocol conformance
2533 if (pointers_compatible
)
2535 pointers_compatible
= objc_compare_protocols (lcls
, ltyp
, rcls
, rtyp
,
2538 if (!pointers_compatible
&& argno
== -3)
2539 pointers_compatible
= objc_compare_protocols (rcls
, rtyp
, lcls
, ltyp
,
2543 if (!pointers_compatible
)
2545 /* The two pointers are not exactly compatible. Issue a warning, unless
2546 we are performing a silent comparison, in which case return 'false'
2548 /* NB: For the time being, we shall make our warnings look like their
2549 C counterparts. In the future, we may wish to make them more
2558 warning (0, "comparison of distinct Objective-C types lacks a cast");
2562 warning (0, "initialization from distinct Objective-C type");
2566 warning (0, "assignment from distinct Objective-C type");
2570 warning (0, "distinct Objective-C type in return");
2574 warning (0, "passing argument %d of %qE from distinct "
2575 "Objective-C type", argno
, callee
);
2583 /* This routine is similar to objc_compare_types except that function-pointers are
2584 excluded. This is because, caller assumes that common types are of (id, Object*)
2585 variety and calls objc_common_type to obtain a common type. There is no commonolty
2586 between two function-pointers in this regard. */
2589 objc_have_common_type (tree ltyp
, tree rtyp
, int argno
, tree callee
)
2591 if (objc_compare_types (ltyp
, rtyp
, argno
, callee
))
2593 /* exclude function-pointer types. */
2596 ltyp
= TREE_TYPE (ltyp
); /* Remove indirections. */
2597 rtyp
= TREE_TYPE (rtyp
);
2599 while (POINTER_TYPE_P (ltyp
) && POINTER_TYPE_P (rtyp
));
2600 return !(TREE_CODE (ltyp
) == FUNCTION_TYPE
&& TREE_CODE (rtyp
) == FUNCTION_TYPE
);
2606 /* Determine if CHILD is derived from PARENT. The routine assumes that
2607 both parameters are RECORD_TYPEs, and is non-reflexive. */
2610 objc_derived_from_p (tree parent
, tree child
)
2612 parent
= TYPE_MAIN_VARIANT (parent
);
2614 for (child
= TYPE_MAIN_VARIANT (child
);
2615 TYPE_BINFO (child
) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child
));)
2617 child
= TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2618 (TYPE_BINFO (child
),
2621 if (child
== parent
)
2630 objc_build_component_ref (tree datum
, tree component
)
2632 /* If COMPONENT is NULL, the caller is referring to the anonymous
2633 base class field. */
2636 tree base
= TYPE_FIELDS (TREE_TYPE (datum
));
2638 return build3 (COMPONENT_REF
, TREE_TYPE (base
), datum
, base
, NULL_TREE
);
2641 /* The 'build_component_ref' routine has been removed from the C++
2642 front-end, but 'finish_class_member_access_expr' seems to be
2643 a worthy substitute. */
2645 return finish_class_member_access_expr (datum
, component
, false,
2646 tf_warning_or_error
);
2648 return build_component_ref (input_location
, datum
, component
);
2652 /* Recursively copy inheritance information rooted at BINFO. To do this,
2653 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
2656 objc_copy_binfo (tree binfo
)
2658 tree btype
= BINFO_TYPE (binfo
);
2659 tree binfo2
= make_tree_binfo (BINFO_N_BASE_BINFOS (binfo
));
2663 BINFO_TYPE (binfo2
) = btype
;
2664 BINFO_OFFSET (binfo2
) = BINFO_OFFSET (binfo
);
2665 BINFO_BASE_ACCESSES (binfo2
) = BINFO_BASE_ACCESSES (binfo
);
2667 /* Recursively copy base binfos of BINFO. */
2668 for (ix
= 0; BINFO_BASE_ITERATE (binfo
, ix
, base_binfo
); ix
++)
2670 tree base_binfo2
= objc_copy_binfo (base_binfo
);
2672 BINFO_INHERITANCE_CHAIN (base_binfo2
) = binfo2
;
2673 BINFO_BASE_APPEND (binfo2
, base_binfo2
);
2679 /* Record superclass information provided in BASETYPE for ObjC class REF.
2680 This is loosely based on cp/decl.c:xref_basetypes(). */
2683 objc_xref_basetypes (tree ref
, tree basetype
)
2685 tree binfo
= make_tree_binfo (basetype
? 1 : 0);
2687 TYPE_BINFO (ref
) = binfo
;
2688 BINFO_OFFSET (binfo
) = size_zero_node
;
2689 BINFO_TYPE (binfo
) = ref
;
2693 tree base_binfo
= objc_copy_binfo (TYPE_BINFO (basetype
));
2695 BINFO_INHERITANCE_CHAIN (base_binfo
) = binfo
;
2696 BINFO_BASE_ACCESSES (binfo
) = VEC_alloc (tree
, gc
, 1);
2697 BINFO_BASE_APPEND (binfo
, base_binfo
);
2698 BINFO_BASE_ACCESS_APPEND (binfo
, access_public_node
);
2702 /* Called from finish_decl. */
2705 objc_check_decl (tree decl
)
2707 tree type
= TREE_TYPE (decl
);
2709 if (TREE_CODE (type
) != RECORD_TYPE
)
2711 if (OBJC_TYPE_NAME (type
) && (type
= objc_is_class_name (OBJC_TYPE_NAME (type
))))
2712 error ("statically allocated instance of Objective-C class %qE",
2717 objc_check_global_decl (tree decl
)
2719 tree id
= DECL_NAME (decl
);
2720 if (objc_is_class_name (id
) && global_bindings_p())
2721 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id
));
2724 /* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2725 INTERFACE may either name an Objective-C class, or refer to the
2726 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2727 type, just return it unchanged. This function is often called when
2728 PROTOCOLS is NULL_TREE, in which case we simply look up the
2729 appropriate INTERFACE. */
2732 objc_get_protocol_qualified_type (tree interface
, tree protocols
)
2734 /* If INTERFACE is not provided, default to 'id'. */
2735 tree type
= (interface
? objc_is_id (interface
) : objc_object_type
);
2736 bool is_ptr
= (type
!= NULL_TREE
);
2740 type
= objc_is_class_name (interface
);
2744 /* If looking at a typedef, retrieve the precise type it
2746 if (TREE_CODE (interface
) == IDENTIFIER_NODE
)
2747 interface
= identifier_global_value (interface
);
2749 type
= ((interface
&& TREE_CODE (interface
) == TYPE_DECL
2750 && DECL_ORIGINAL_TYPE (interface
))
2751 ? DECL_ORIGINAL_TYPE (interface
)
2752 : xref_tag (RECORD_TYPE
, type
));
2756 /* This case happens when we are given an 'interface' which
2757 is not a valid class name. For example if a typedef was
2758 used, and 'interface' really is the identifier of the
2759 typedef, but when you resolve it you don't get an
2760 Objective-C class, but something else, such as 'int'.
2761 This is an error; protocols make no sense unless you use
2762 them with Objective-C objects. */
2763 error_at (input_location
, "only Objective-C object types can be qualified with a protocol");
2765 /* Try to recover. Ignore the invalid class name, and treat
2766 the object as an 'id' to silence further warnings about
2768 type
= objc_object_type
;
2775 type
= build_variant_type_copy (type
);
2777 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2781 tree orig_pointee_type
= TREE_TYPE (type
);
2782 TREE_TYPE (type
) = build_variant_type_copy (orig_pointee_type
);
2784 /* Set up the canonical type information. */
2785 TYPE_CANONICAL (type
)
2786 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type
));
2788 TYPE_POINTER_TO (TREE_TYPE (type
)) = type
;
2789 type
= TREE_TYPE (type
);
2792 /* Look up protocols and install in lang specific list. */
2793 DUP_TYPE_OBJC_INFO (type
, TYPE_MAIN_VARIANT (type
));
2794 TYPE_OBJC_PROTOCOL_LIST (type
) = lookup_and_install_protocols
2795 (protocols
, /* definition_required */ false);
2797 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2798 return the pointer to the new pointee variant. */
2800 type
= TYPE_POINTER_TO (type
);
2802 TYPE_OBJC_INTERFACE (type
)
2803 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type
));
2809 /* Check for circular dependencies in protocols. The arguments are
2810 PROTO, the protocol to check, and LIST, a list of protocol it
2814 check_protocol_recursively (tree proto
, tree list
)
2818 for (p
= list
; p
; p
= TREE_CHAIN (p
))
2820 tree pp
= TREE_VALUE (p
);
2822 if (TREE_CODE (pp
) == IDENTIFIER_NODE
)
2823 pp
= lookup_protocol (pp
, /* warn if deprecated */ false,
2824 /* definition_required */ false);
2827 fatal_error ("protocol %qE has circular dependency",
2828 PROTOCOL_NAME (pp
));
2830 check_protocol_recursively (proto
, PROTOCOL_LIST (pp
));
2834 /* Look up PROTOCOLS, and return a list of those that are found. If
2835 none are found, return NULL. Note that this function will emit a
2836 warning if a protocol is found and is deprecated. If
2837 'definition_required', then warn if the protocol is found but is
2838 not defined (ie, if we only saw a forward-declaration of the
2839 protocol (as in "@protocol NSObject;") not a real definition with
2840 the list of methods). */
2842 lookup_and_install_protocols (tree protocols
, bool definition_required
)
2845 tree return_value
= NULL_TREE
;
2847 if (protocols
== error_mark_node
)
2850 for (proto
= protocols
; proto
; proto
= TREE_CHAIN (proto
))
2852 tree ident
= TREE_VALUE (proto
);
2853 tree p
= lookup_protocol (ident
, /* warn_if_deprecated */ true,
2854 definition_required
);
2857 return_value
= chainon (return_value
,
2858 build_tree_list (NULL_TREE
, p
));
2859 else if (ident
!= error_mark_node
)
2860 error ("cannot find protocol declaration for %qE",
2864 return return_value
;
2868 build_common_objc_exception_stuff (void)
2870 tree noreturn_list
, nothrow_list
, temp_type
;
2872 noreturn_list
= tree_cons (get_identifier ("noreturn"), NULL
, NULL
);
2873 nothrow_list
= tree_cons (get_identifier ("nothrow"), NULL
, NULL
);
2875 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2876 /* void objc_sync_enter(id); */
2877 /* void objc_sync_exit(id); */
2878 temp_type
= build_function_type_list (void_type_node
,
2881 objc_exception_throw_decl
2882 = add_builtin_function (TAG_EXCEPTIONTHROW
, temp_type
, 0, NOT_BUILT_IN
, NULL
,
2884 /* Make sure that objc_exception_throw (id) claims that it may throw an
2886 TREE_NOTHROW (objc_exception_throw_decl
) = 0;
2888 objc_sync_enter_decl
2889 = add_builtin_function (TAG_SYNCENTER
, temp_type
, 0, NOT_BUILT_IN
,
2890 NULL
, nothrow_list
);
2893 = add_builtin_function (TAG_SYNCEXIT
, temp_type
, 0, NOT_BUILT_IN
,
2894 NULL
, nothrow_list
);
2897 /* Purpose: "play" parser, creating/installing representations
2898 of the declarations that are required by Objective-C.
2902 type_spec--------->sc_spec
2903 (tree_list) (tree_list)
2906 identifier_node identifier_node */
2909 synth_module_prologue (void)
2912 enum debug_info_type save_write_symbols
= write_symbols
;
2913 const struct gcc_debug_hooks
*const save_hooks
= debug_hooks
;
2915 /* Suppress outputting debug symbols, because
2916 dbxout_init hasn't been called yet. */
2917 write_symbols
= NO_DEBUG
;
2918 debug_hooks
= &do_nothing_debug_hooks
;
2921 push_lang_context (lang_name_c
); /* extern "C" */
2924 /* The following are also defined in <objc/objc.h> and friends. */
2926 objc_object_id
= get_identifier (TAG_OBJECT
);
2927 objc_class_id
= get_identifier (TAG_CLASS
);
2929 objc_object_reference
= xref_tag (RECORD_TYPE
, objc_object_id
);
2930 objc_class_reference
= xref_tag (RECORD_TYPE
, objc_class_id
);
2932 objc_object_type
= build_pointer_type (objc_object_reference
);
2933 objc_class_type
= build_pointer_type (objc_class_reference
);
2935 objc_object_name
= get_identifier (OBJECT_TYPEDEF_NAME
);
2936 objc_class_name
= get_identifier (CLASS_TYPEDEF_NAME
);
2938 /* Declare the 'id' and 'Class' typedefs. */
2939 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
2943 TREE_NO_WARNING (type
) = 1;
2945 type
= lang_hooks
.decls
.pushdecl (build_decl (input_location
,
2949 TREE_NO_WARNING (type
) = 1;
2951 /* Forward-declare '@interface Protocol'. */
2952 type
= get_identifier (PROTOCOL_OBJECT_CLASS_NAME
);
2953 objc_declare_class (type
);
2954 objc_protocol_type
= build_pointer_type (xref_tag (RECORD_TYPE
, type
));
2956 /* Declare receiver type used for dispatching messages to 'super'. */
2957 /* `struct objc_super *' */
2958 objc_super_type
= build_pointer_type (xref_tag (RECORD_TYPE
,
2959 get_identifier (TAG_SUPER
)));
2961 /* Declare pointers to method and ivar lists. */
2962 objc_method_list_ptr
= build_pointer_type
2963 (xref_tag (RECORD_TYPE
,
2964 get_identifier (UTAG_METHOD_LIST
)));
2965 objc_method_proto_list_ptr
2966 = build_pointer_type (xref_tag (RECORD_TYPE
,
2967 get_identifier (UTAG_METHOD_PROTOTYPE_LIST
)));
2968 objc_ivar_list_ptr
= build_pointer_type
2969 (xref_tag (RECORD_TYPE
,
2970 get_identifier (UTAG_IVAR_LIST
)));
2972 build_common_objc_exception_stuff ();
2974 /* Set-up runtime-specific templates, message and exception stuff. */
2975 (*runtime
.initialize
) ();
2977 /* Declare objc_getProperty, object_setProperty and other property
2978 accessor helpers. */
2979 build_common_objc_property_accessor_helpers ();
2981 /* Forward declare constant_string_id and constant_string_type. */
2982 if (!constant_string_class_name
)
2983 constant_string_class_name
= runtime
.default_constant_string_class_name
;
2984 constant_string_id
= get_identifier (constant_string_class_name
);
2985 objc_declare_class (constant_string_id
);
2987 /* Pre-build the following entities - for speed/convenience. */
2988 self_id
= get_identifier ("self");
2989 ucmd_id
= get_identifier ("_cmd");
2991 /* Declare struct _objc_fast_enumeration_state { ... }; */
2992 build_fast_enumeration_state_template ();
2994 /* void objc_enumeration_mutation (id) */
2995 type
= build_function_type_list (void_type_node
,
2996 objc_object_type
, NULL_TREE
);
2997 objc_enumeration_mutation_decl
2998 = add_builtin_function (TAG_ENUMERATION_MUTATION
, type
, 0, NOT_BUILT_IN
,
3000 TREE_NOTHROW (objc_enumeration_mutation_decl
) = 0;
3003 pop_lang_context ();
3006 write_symbols
= save_write_symbols
;
3007 debug_hooks
= save_hooks
;
3010 /* --- const strings --- */
3012 /* Ensure that the ivar list for NSConstantString/NXConstantString
3013 (or whatever was specified via `-fconstant-string-class')
3014 contains fields at least as large as the following three, so that
3015 the runtime can stomp on them with confidence:
3017 struct STRING_OBJECT_CLASS_NAME
3021 unsigned int length;
3025 check_string_class_template (void)
3027 tree field_decl
= objc_get_class_ivars (constant_string_id
);
3029 #define AT_LEAST_AS_LARGE_AS(F, T) \
3030 (F && TREE_CODE (F) == FIELD_DECL \
3031 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
3032 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
3034 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
3037 field_decl
= DECL_CHAIN (field_decl
);
3038 if (!AT_LEAST_AS_LARGE_AS (field_decl
, ptr_type_node
))
3041 field_decl
= DECL_CHAIN (field_decl
);
3042 return AT_LEAST_AS_LARGE_AS (field_decl
, unsigned_type_node
);
3044 #undef AT_LEAST_AS_LARGE_AS
3047 /* Avoid calling `check_string_class_template ()' more than once. */
3048 static GTY(()) int string_layout_checked
;
3050 /* Construct an internal string layout to be used as a template for
3051 creating NSConstantString/NXConstantString instances. */
3054 objc_build_internal_const_str_type (void)
3056 tree type
= (*lang_hooks
.types
.make_type
) (RECORD_TYPE
);
3057 tree fields
= build_decl (input_location
,
3058 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
3059 tree field
= build_decl (input_location
,
3060 FIELD_DECL
, NULL_TREE
, ptr_type_node
);
3062 DECL_CHAIN (field
) = fields
; fields
= field
;
3063 field
= build_decl (input_location
,
3064 FIELD_DECL
, NULL_TREE
, unsigned_type_node
);
3065 DECL_CHAIN (field
) = fields
; fields
= field
;
3066 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3068 finish_builtin_struct (type
, "__builtin_ObjCString",
3074 /* Custom build_string which sets TREE_TYPE! */
3077 my_build_string (int len
, const char *str
)
3079 return fix_string_type (build_string (len
, str
));
3082 /* Build a string with contents STR and length LEN and convert it to a
3086 my_build_string_pointer (int len
, const char *str
)
3088 tree string
= my_build_string (len
, str
);
3089 tree ptrtype
= build_pointer_type (TREE_TYPE (TREE_TYPE (string
)));
3090 return build1 (ADDR_EXPR
, ptrtype
, string
);
3094 string_hash (const void *ptr
)
3096 const_tree
const str
= ((const struct string_descriptor
*)ptr
)->literal
;
3097 const unsigned char *p
= (const unsigned char *) TREE_STRING_POINTER (str
);
3098 int i
, len
= TREE_STRING_LENGTH (str
);
3101 for (i
= 0; i
< len
; i
++)
3102 h
= ((h
* 613) + p
[i
]);
3108 string_eq (const void *ptr1
, const void *ptr2
)
3110 const_tree
const str1
= ((const struct string_descriptor
*)ptr1
)->literal
;
3111 const_tree
const str2
= ((const struct string_descriptor
*)ptr2
)->literal
;
3112 int len1
= TREE_STRING_LENGTH (str1
);
3114 return (len1
== TREE_STRING_LENGTH (str2
)
3115 && !memcmp (TREE_STRING_POINTER (str1
), TREE_STRING_POINTER (str2
),
3119 /* Given a chain of STRING_CST's, build a static instance of
3120 NXConstantString which points at the concatenation of those
3121 strings. We place the string object in the __string_objects
3122 section of the __OBJC segment. The Objective-C runtime will
3123 initialize the isa pointers of the string objects to point at the
3124 NXConstantString class object. */
3127 objc_build_string_object (tree string
)
3129 tree constant_string_class
;
3132 struct string_descriptor
*desc
, key
;
3135 /* Prep the string argument. */
3136 string
= fix_string_type (string
);
3137 TREE_SET_CODE (string
, STRING_CST
);
3138 length
= TREE_STRING_LENGTH (string
) - 1;
3140 /* The target may have different ideas on how to construct an ObjC string
3141 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
3142 constant CFString reference instead.
3143 At present, this is only supported for the NeXT runtime. */
3144 if (flag_next_runtime
3145 && targetcm
.objc_construct_string_object
)
3147 tree constructor
= (*targetcm
.objc_construct_string_object
) (string
);
3149 return build1 (NOP_EXPR
, objc_object_type
, constructor
);
3152 /* Check whether the string class being used actually exists and has the
3153 correct ivar layout. */
3154 if (!string_layout_checked
)
3156 string_layout_checked
= -1;
3157 constant_string_class
= lookup_interface (constant_string_id
);
3158 internal_const_str_type
= objc_build_internal_const_str_type ();
3160 if (!constant_string_class
3161 || !(constant_string_type
3162 = CLASS_STATIC_TEMPLATE (constant_string_class
)))
3163 error ("cannot find interface declaration for %qE",
3164 constant_string_id
);
3165 /* The NSConstantString/NXConstantString ivar layout is now known. */
3166 else if (!check_string_class_template ())
3167 error ("interface %qE does not have valid constant string layout",
3168 constant_string_id
);
3169 /* If the runtime can generate a literal reference to the string class,
3170 don't need to run a constructor. */
3171 else if (!(*runtime
.setup_const_string_class_decl
)())
3172 error ("cannot find reference tag for class %qE", constant_string_id
);
3175 string_layout_checked
= 1; /* Success! */
3176 add_class_reference (constant_string_id
);
3180 if (string_layout_checked
== -1)
3181 return error_mark_node
;
3183 /* Perhaps we already constructed a constant string just like this one? */
3184 key
.literal
= string
;
3185 loc
= htab_find_slot (string_htab
, &key
, INSERT
);
3186 desc
= (struct string_descriptor
*) *loc
;
3190 *loc
= desc
= ggc_alloc_string_descriptor ();
3191 desc
->literal
= string
;
3193 (*runtime
.build_const_string_constructor
) (input_location
, string
, length
);
3196 addr
= convert (build_pointer_type (constant_string_type
),
3197 build_unary_op (input_location
,
3198 ADDR_EXPR
, desc
->constructor
, 1));
3203 /* Build a static constant CONSTRUCTOR
3204 with type TYPE and elements ELTS. */
3207 objc_build_constructor (tree type
, VEC(constructor_elt
,gc
) *elts
)
3209 tree constructor
= build_constructor (type
, elts
);
3211 TREE_CONSTANT (constructor
) = 1;
3212 TREE_STATIC (constructor
) = 1;
3213 TREE_READONLY (constructor
) = 1;
3216 /* Adjust for impedance mismatch. We should figure out how to build
3217 CONSTRUCTORs that consistently please both the C and C++ gods. */
3218 if (!VEC_index (constructor_elt
, elts
, 0)->index
)
3219 TREE_TYPE (constructor
) = init_list_type_node
;
3225 /* Return the DECL of the string IDENT in the SECTION. */
3228 get_objc_string_decl (tree ident
, enum string_section section
)
3235 chain
= class_names_chain
;
3237 case meth_var_names
:
3238 chain
= meth_var_names_chain
;
3240 case meth_var_types
:
3241 chain
= meth_var_types_chain
;
3243 case prop_names_attr
:
3244 chain
= prop_names_attr_chain
;
3250 for (; chain
!= 0; chain
= TREE_CHAIN (chain
))
3251 if (TREE_VALUE (chain
) == ident
)
3252 return (TREE_PURPOSE (chain
));
3254 /* We didn't find the entry. */
3258 /* Create a class reference, but don't create a variable to reference
3262 add_class_reference (tree ident
)
3266 if ((chain
= cls_ref_chain
))
3271 if (ident
== TREE_VALUE (chain
))
3275 chain
= TREE_CHAIN (chain
);
3279 /* Append to the end of the list */
3280 TREE_CHAIN (tail
) = tree_cons (NULL_TREE
, ident
, NULL_TREE
);
3283 cls_ref_chain
= tree_cons (NULL_TREE
, ident
, NULL_TREE
);
3286 /* Get a class reference, creating it if necessary. Also create the
3287 reference variable. */
3289 objc_get_class_reference (tree ident
)
3291 tree orig_ident
= (DECL_P (ident
)
3294 ? OBJC_TYPE_NAME (ident
)
3296 bool local_scope
= false;
3299 if (processing_template_decl
)
3300 /* Must wait until template instantiation time. */
3301 return build_min_nt (CLASS_REFERENCE_EXPR
, ident
);
3304 if (TREE_CODE (ident
) == TYPE_DECL
)
3305 ident
= (DECL_ORIGINAL_TYPE (ident
)
3306 ? DECL_ORIGINAL_TYPE (ident
)
3307 : TREE_TYPE (ident
));
3311 && CP_TYPE_CONTEXT (ident
) != global_namespace
)
3315 if (local_scope
|| !(ident
= objc_is_class_name (ident
)))
3317 error ("%qE is not an Objective-C class name or alias",
3319 return error_mark_node
;
3322 return (*runtime
.get_class_reference
) (ident
);
3326 objc_declare_alias (tree alias_ident
, tree class_ident
)
3328 tree underlying_class
;
3331 if (current_namespace
!= global_namespace
) {
3332 error ("Objective-C declarations may only appear in global scope");
3334 #endif /* OBJCPLUS */
3336 if (!(underlying_class
= objc_is_class_name (class_ident
)))
3337 warning (0, "cannot find class %qE", class_ident
);
3338 else if (objc_is_class_name (alias_ident
))
3339 warning (0, "class %qE already exists", alias_ident
);
3342 /* Implement @compatibility_alias as a typedef. */
3344 push_lang_context (lang_name_c
); /* extern "C" */
3346 lang_hooks
.decls
.pushdecl (build_decl
3350 xref_tag (RECORD_TYPE
, underlying_class
)));
3352 pop_lang_context ();
3354 hash_class_name_enter (als_name_hash_list
, alias_ident
,
3360 objc_declare_class (tree identifier
)
3363 if (current_namespace
!= global_namespace
) {
3364 error ("Objective-C declarations may only appear in global scope");
3366 #endif /* OBJCPLUS */
3368 if (! objc_is_class_name (identifier
))
3370 tree record
= lookup_name (identifier
), type
= record
;
3374 if (TREE_CODE (record
) == TYPE_DECL
)
3375 type
= DECL_ORIGINAL_TYPE (record
)
3376 ? DECL_ORIGINAL_TYPE (record
)
3377 : TREE_TYPE (record
);
3379 if (!TYPE_HAS_OBJC_INFO (type
)
3380 || !TYPE_OBJC_INTERFACE (type
))
3382 error ("%qE redeclared as different kind of symbol",
3384 error ("previous declaration of %q+D",
3389 record
= xref_tag (RECORD_TYPE
, identifier
);
3390 INIT_TYPE_OBJC_INFO (record
);
3391 /* In the case of a @class declaration, we store the ident in
3392 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3393 we'll replace the ident with the interface. */
3394 TYPE_OBJC_INTERFACE (record
) = identifier
;
3395 hash_class_name_enter (cls_name_hash_list
, identifier
, NULL_TREE
);
3400 objc_is_class_name (tree ident
)
3404 if (ident
&& TREE_CODE (ident
) == IDENTIFIER_NODE
)
3406 tree t
= identifier_global_value (ident
);
3411 while (ident
&& TREE_CODE (ident
) == TYPE_DECL
&& DECL_ORIGINAL_TYPE (ident
))
3412 ident
= OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident
));
3414 if (ident
&& TREE_CODE (ident
) == RECORD_TYPE
)
3415 ident
= OBJC_TYPE_NAME (ident
);
3417 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3419 tree type
= TREE_TYPE (ident
);
3420 if (type
&& TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
3422 ident
= DECL_NAME (ident
);
3425 if (!ident
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3428 if (lookup_interface (ident
))
3431 target
= hash_class_name_lookup (cls_name_hash_list
, ident
);
3435 target
= hash_class_name_lookup (als_name_hash_list
, ident
);
3438 gcc_assert (target
->list
&& target
->list
->value
);
3439 return target
->list
->value
;
3445 /* Check whether TYPE is either 'id' or 'Class'. */
3448 objc_is_id (tree type
)
3450 if (type
&& TREE_CODE (type
) == IDENTIFIER_NODE
)
3452 tree t
= identifier_global_value (type
);
3457 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
3458 type
= TREE_TYPE (type
);
3460 /* NB: This function may be called before the ObjC front-end has
3461 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3462 return (objc_object_type
&& type
3463 && (IS_ID (type
) || IS_CLASS (type
) || IS_SUPER (type
))
3468 /* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3469 class instance. This is needed by other parts of the compiler to
3470 handle ObjC types gracefully. */
3473 objc_is_object_ptr (tree type
)
3477 type
= TYPE_MAIN_VARIANT (type
);
3478 if (!POINTER_TYPE_P (type
))
3481 ret
= objc_is_id (type
);
3483 ret
= objc_is_class_name (TREE_TYPE (type
));
3489 objc_is_gcable_type (tree type
, int or_strong_p
)
3495 if (objc_is_id (TYPE_MAIN_VARIANT (type
)))
3497 if (or_strong_p
&& lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type
)))
3499 if (TREE_CODE (type
) != POINTER_TYPE
&& TREE_CODE (type
) != INDIRECT_REF
)
3501 type
= TREE_TYPE (type
);
3502 if (TREE_CODE (type
) != RECORD_TYPE
)
3504 name
= TYPE_NAME (type
);
3505 return (objc_is_class_name (name
) != NULL_TREE
);
3509 objc_substitute_decl (tree expr
, tree oldexpr
, tree newexpr
)
3511 if (expr
== oldexpr
)
3514 switch (TREE_CODE (expr
))
3517 return objc_build_component_ref
3518 (objc_substitute_decl (TREE_OPERAND (expr
, 0),
3521 DECL_NAME (TREE_OPERAND (expr
, 1)));
3523 return build_array_ref (input_location
,
3524 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3527 TREE_OPERAND (expr
, 1));
3529 return build_indirect_ref (input_location
,
3530 objc_substitute_decl (TREE_OPERAND (expr
, 0),
3532 newexpr
), RO_ARROW
);
3539 objc_build_ivar_assignment (tree outervar
, tree lhs
, tree rhs
)
3542 /* The LHS parameter contains the expression 'outervar->memberspec';
3543 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3544 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3547 = objc_substitute_decl
3548 (lhs
, outervar
, convert (TREE_TYPE (outervar
), integer_zero_node
));
3550 = (flag_objc_direct_dispatch
3551 ? objc_assign_ivar_fast_decl
3552 : objc_assign_ivar_decl
);
3554 offs
= convert (integer_type_node
, build_unary_op (input_location
,
3555 ADDR_EXPR
, offs
, 0));
3557 func_params
= tree_cons (NULL_TREE
,
3558 convert (objc_object_type
, rhs
),
3559 tree_cons (NULL_TREE
, convert (objc_object_type
, outervar
),
3560 tree_cons (NULL_TREE
, offs
,
3563 assemble_external (func
);
3564 return build_function_call (input_location
, func
, func_params
);
3568 objc_build_global_assignment (tree lhs
, tree rhs
)
3570 tree func_params
= tree_cons (NULL_TREE
,
3571 convert (objc_object_type
, rhs
),
3572 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3573 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3576 assemble_external (objc_assign_global_decl
);
3577 return build_function_call (input_location
,
3578 objc_assign_global_decl
, func_params
);
3582 objc_build_strong_cast_assignment (tree lhs
, tree rhs
)
3584 tree func_params
= tree_cons (NULL_TREE
,
3585 convert (objc_object_type
, rhs
),
3586 tree_cons (NULL_TREE
, convert (build_pointer_type (objc_object_type
),
3587 build_unary_op (input_location
, ADDR_EXPR
, lhs
, 0)),
3590 assemble_external (objc_assign_strong_cast_decl
);
3591 return build_function_call (input_location
,
3592 objc_assign_strong_cast_decl
, func_params
);
3596 objc_is_gcable_p (tree expr
)
3598 return (TREE_CODE (expr
) == COMPONENT_REF
3599 ? objc_is_gcable_p (TREE_OPERAND (expr
, 1))
3600 : TREE_CODE (expr
) == ARRAY_REF
3601 ? (objc_is_gcable_p (TREE_TYPE (expr
))
3602 || objc_is_gcable_p (TREE_OPERAND (expr
, 0)))
3603 : TREE_CODE (expr
) == ARRAY_TYPE
3604 ? objc_is_gcable_p (TREE_TYPE (expr
))
3606 ? objc_is_gcable_type (expr
, 1)
3607 : (objc_is_gcable_p (TREE_TYPE (expr
))
3609 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr
)))));
3613 objc_is_ivar_reference_p (tree expr
)
3615 return (TREE_CODE (expr
) == ARRAY_REF
3616 ? objc_is_ivar_reference_p (TREE_OPERAND (expr
, 0))
3617 : TREE_CODE (expr
) == COMPONENT_REF
3618 ? TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
3623 objc_is_global_reference_p (tree expr
)
3625 return (TREE_CODE (expr
) == INDIRECT_REF
|| TREE_CODE (expr
) == PLUS_EXPR
3626 ? objc_is_global_reference_p (TREE_OPERAND (expr
, 0))
3628 ? (DECL_FILE_SCOPE_P (expr
) || TREE_STATIC (expr
))
3633 objc_generate_write_barrier (tree lhs
, enum tree_code modifycode
, tree rhs
)
3635 tree result
= NULL_TREE
, outer
;
3636 int strong_cast_p
= 0, outer_gc_p
= 0, indirect_p
= 0;
3638 /* This function is currently only used with the next runtime with
3639 garbage collection enabled (-fobjc-gc). */
3640 gcc_assert (flag_next_runtime
);
3642 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3643 will have been transformed to the form '*(type *)&expr'. */
3644 if (TREE_CODE (lhs
) == INDIRECT_REF
)
3646 outer
= TREE_OPERAND (lhs
, 0);
3648 while (!strong_cast_p
3649 && (CONVERT_EXPR_P (outer
)
3650 || TREE_CODE (outer
) == NON_LVALUE_EXPR
))
3652 tree lhstype
= TREE_TYPE (outer
);
3654 /* Descend down the cast chain, and record the first objc_gc
3656 if (POINTER_TYPE_P (lhstype
))
3659 = lookup_attribute ("objc_gc",
3660 TYPE_ATTRIBUTES (TREE_TYPE (lhstype
)));
3666 outer
= TREE_OPERAND (outer
, 0);
3670 /* If we have a __strong cast, it trumps all else. */
3673 if (modifycode
!= NOP_EXPR
)
3674 goto invalid_pointer_arithmetic
;
3676 if (warn_assign_intercept
)
3677 warning (0, "strong-cast assignment has been intercepted");
3679 result
= objc_build_strong_cast_assignment (lhs
, rhs
);
3684 /* the lhs must be of a suitable type, regardless of its underlying
3686 if (!objc_is_gcable_p (lhs
))
3692 && (TREE_CODE (outer
) == COMPONENT_REF
3693 || TREE_CODE (outer
) == ARRAY_REF
))
3694 outer
= TREE_OPERAND (outer
, 0);
3696 if (TREE_CODE (outer
) == INDIRECT_REF
)
3698 outer
= TREE_OPERAND (outer
, 0);
3702 outer_gc_p
= objc_is_gcable_p (outer
);
3704 /* Handle ivar assignments. */
3705 if (objc_is_ivar_reference_p (lhs
))
3707 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3708 doesn't cut it here), the best we can do here is suggest a cast. */
3709 if (!objc_is_gcable_type (TREE_TYPE (outer
), 0))
3711 /* We may still be able to use the global write barrier... */
3712 if (!indirect_p
&& objc_is_global_reference_p (outer
))
3713 goto global_reference
;
3716 if (modifycode
== NOP_EXPR
)
3718 if (warn_assign_intercept
)
3719 warning (0, "strong-cast may possibly be needed");
3725 if (modifycode
!= NOP_EXPR
)
3726 goto invalid_pointer_arithmetic
;
3728 if (warn_assign_intercept
)
3729 warning (0, "instance variable assignment has been intercepted");
3731 result
= objc_build_ivar_assignment (outer
, lhs
, rhs
);
3736 /* Likewise, intercept assignment to global/static variables if their type is
3738 if (objc_is_global_reference_p (outer
))
3744 if (modifycode
!= NOP_EXPR
)
3746 invalid_pointer_arithmetic
:
3748 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
3753 if (warn_assign_intercept
)
3754 warning (0, "global/static variable assignment has been intercepted");
3756 result
= objc_build_global_assignment (lhs
, rhs
);
3759 /* In all other cases, fall back to the normal mechanism. */
3764 struct GTY(()) interface_tuple
{
3769 static GTY ((param_is (struct interface_tuple
))) htab_t interface_htab
;
3772 hash_interface (const void *p
)
3774 const struct interface_tuple
*d
= (const struct interface_tuple
*) p
;
3775 return IDENTIFIER_HASH_VALUE (d
->id
);
3779 eq_interface (const void *p1
, const void *p2
)
3781 const struct interface_tuple
*d
= (const struct interface_tuple
*) p1
;
3786 lookup_interface (tree ident
)
3789 if (ident
&& TREE_CODE (ident
) == TYPE_DECL
)
3790 ident
= DECL_NAME (ident
);
3793 if (ident
== NULL_TREE
|| TREE_CODE (ident
) != IDENTIFIER_NODE
)
3797 struct interface_tuple
**slot
;
3802 slot
= (struct interface_tuple
**)
3803 htab_find_slot_with_hash (interface_htab
, ident
,
3804 IDENTIFIER_HASH_VALUE (ident
),
3807 i
= (*slot
)->class_name
;
3815 /* Implement @defs (<classname>) within struct bodies. */
3818 objc_get_class_ivars (tree class_name
)
3820 tree interface
= lookup_interface (class_name
);
3823 return get_class_ivars (interface
, true);
3825 error ("cannot find interface declaration for %qE",
3828 return error_mark_node
;
3832 /* Functions used by the hashtable for field duplicates in
3833 objc_detect_field_duplicates(). Ideally, we'd use a standard
3834 key-value dictionary hashtable , and store as keys the field names,
3835 and as values the actual declarations (used to print nice error
3836 messages with the locations). But, the hashtable we are using only
3837 allows us to store keys in the hashtable, without values (it looks
3838 more like a set). So, we store the DECLs, but define equality as
3839 DECLs having the same name, and hash as the hash of the name. */
3841 hash_instance_variable (const PTR p
)
3843 const_tree q
= (const_tree
)p
;
3844 return (hashval_t
) ((intptr_t)(DECL_NAME (q
)) >> 3);
3848 eq_instance_variable (const PTR p1
, const PTR p2
)
3850 const_tree a
= (const_tree
)p1
;
3851 const_tree b
= (const_tree
)p2
;
3852 return DECL_NAME (a
) == DECL_NAME (b
);
3855 /* Called when checking the variables in a struct. If we are not
3856 doing the ivars list inside an @interface context, then return
3857 false. Else, perform the check for duplicate ivars, then return
3858 true. The check for duplicates checks if an instance variable with
3859 the same name exists in the class or in a superclass. If
3860 'check_superclasses_only' is set to true, then it is assumed that
3861 checks for instance variables in the same class has already been
3862 performed (this is the case for ObjC++) and only the instance
3863 variables of superclasses are checked. */
3865 objc_detect_field_duplicates (bool check_superclasses_only
)
3867 if (!objc_collecting_ivars
|| !objc_interface_context
3868 || TREE_CODE (objc_interface_context
) != CLASS_INTERFACE_TYPE
)
3871 /* We have two ways of doing this check:
3873 "direct comparison": we iterate over the instance variables and
3874 compare them directly. This works great for small numbers of
3875 instance variables (such as 10 or 20), which are extremely common.
3876 But it will potentially take forever for the pathological case with
3877 a huge number (eg, 10k) of instance variables.
3879 "hashtable": we use a hashtable, which requires a single sweep
3880 through the list of instances variables. This is much slower for a
3881 small number of variables, and we only use it for large numbers.
3883 To decide which one to use, we need to get an idea of how many
3884 instance variables we have to compare. */
3886 unsigned int number_of_ivars_to_check
= 0;
3889 for (ivar
= CLASS_RAW_IVARS (objc_interface_context
);
3890 ivar
; ivar
= DECL_CHAIN (ivar
))
3892 /* Ignore anonymous ivars. */
3893 if (DECL_NAME (ivar
))
3894 number_of_ivars_to_check
++;
3898 /* Exit if there is nothing to do. */
3899 if (number_of_ivars_to_check
== 0)
3902 /* In case that there are only 1 or 2 instance variables to check,
3903 we always use direct comparison. If there are more, it is
3904 worth iterating over the instance variables in the superclass
3905 to count how many there are (note that this has the same cost
3906 as checking 1 instance variable by direct comparison, which is
3907 why we skip this check in the case of 1 or 2 ivars and just do
3908 the direct comparison) and then decide if it worth using a
3910 if (number_of_ivars_to_check
> 2)
3912 unsigned int number_of_superclass_ivars
= 0;
3915 for (interface
= lookup_interface (CLASS_SUPER_NAME (objc_interface_context
));
3916 interface
; interface
= lookup_interface (CLASS_SUPER_NAME (interface
)))
3919 for (ivar
= CLASS_RAW_IVARS (interface
);
3920 ivar
; ivar
= DECL_CHAIN (ivar
))
3921 number_of_superclass_ivars
++;
3925 /* We use a hashtable if we have over 10k comparisons. */
3926 if (number_of_ivars_to_check
* (number_of_superclass_ivars
3927 + (number_of_ivars_to_check
/ 2))
3930 /* First, build the hashtable by putting all the instance
3931 variables of superclasses in it. */
3932 htab_t htab
= htab_create (37, hash_instance_variable
,
3933 eq_instance_variable
, NULL
);
3935 for (interface
= lookup_interface (CLASS_SUPER_NAME
3936 (objc_interface_context
));
3937 interface
; interface
= lookup_interface
3938 (CLASS_SUPER_NAME (interface
)))
3941 for (ivar
= CLASS_RAW_IVARS (interface
); ivar
;
3942 ivar
= DECL_CHAIN (ivar
))
3944 if (DECL_NAME (ivar
) != NULL_TREE
)
3946 void **slot
= htab_find_slot (htab
, ivar
, INSERT
);
3947 /* Do not check for duplicate instance
3948 variables in superclasses. Errors have
3949 already been generated. */
3955 /* Now, we go through all the instance variables in the
3956 class, and check that they are not in the
3958 if (check_superclasses_only
)
3961 for (ivar
= CLASS_RAW_IVARS (objc_interface_context
); ivar
;
3962 ivar
= DECL_CHAIN (ivar
))
3964 if (DECL_NAME (ivar
) != NULL_TREE
)
3966 tree duplicate_ivar
= (tree
)(htab_find (htab
, ivar
));
3967 if (duplicate_ivar
!= HTAB_EMPTY_ENTRY
)
3969 error_at (DECL_SOURCE_LOCATION (ivar
),
3970 "duplicate instance variable %q+D",
3972 inform (DECL_SOURCE_LOCATION (duplicate_ivar
),
3973 "previous declaration of %q+D",
3975 /* FIXME: Do we need the following ? */
3976 /* DECL_NAME (ivar) = NULL_TREE; */
3983 /* If we're checking for duplicates in the class as
3984 well, we insert variables in the hashtable as we
3985 check them, so if a duplicate follows, it will be
3988 for (ivar
= CLASS_RAW_IVARS (objc_interface_context
); ivar
;
3989 ivar
= DECL_CHAIN (ivar
))
3991 if (DECL_NAME (ivar
) != NULL_TREE
)
3993 void **slot
= htab_find_slot (htab
, ivar
, INSERT
);
3996 tree duplicate_ivar
= (tree
)(*slot
);
3997 error_at (DECL_SOURCE_LOCATION (ivar
),
3998 "duplicate instance variable %q+D",
4000 inform (DECL_SOURCE_LOCATION (duplicate_ivar
),
4001 "previous declaration of %q+D",
4003 /* FIXME: Do we need the following ? */
4004 /* DECL_NAME (ivar) = NULL_TREE; */
4016 /* This is the "direct comparison" approach, which is used in most
4017 non-pathological cases. */
4019 /* Walk up to class hierarchy, starting with this class (this is
4020 the external loop, because lookup_interface() is expensive, and
4021 we want to do it few times). */
4022 tree interface
= objc_interface_context
;
4024 if (check_superclasses_only
)
4025 interface
= lookup_interface (CLASS_SUPER_NAME (interface
));
4027 for ( ; interface
; interface
= lookup_interface
4028 (CLASS_SUPER_NAME (interface
)))
4030 tree ivar_being_checked
;
4032 for (ivar_being_checked
= CLASS_RAW_IVARS (objc_interface_context
);
4034 ivar_being_checked
= DECL_CHAIN (ivar_being_checked
))
4038 /* Ignore anonymous ivars. */
4039 if (DECL_NAME (ivar_being_checked
) == NULL_TREE
)
4042 /* Note how we stop when we find the ivar we are checking
4043 (this can only happen in the main class, not
4044 superclasses), to avoid comparing things twice
4045 (otherwise, for each ivar, you'd compare A to B then B
4046 to A, and get duplicated error messages). */
4047 for (decl
= CLASS_RAW_IVARS (interface
);
4048 decl
&& decl
!= ivar_being_checked
;
4049 decl
= DECL_CHAIN (decl
))
4051 if (DECL_NAME (ivar_being_checked
) == DECL_NAME (decl
))
4053 error_at (DECL_SOURCE_LOCATION (ivar_being_checked
),
4054 "duplicate instance variable %q+D",
4055 ivar_being_checked
);
4056 inform (DECL_SOURCE_LOCATION (decl
),
4057 "previous declaration of %q+D",
4059 /* FIXME: Do we need the following ? */
4060 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4069 /* Used by: build_private_template, continue_class,
4070 and for @defs constructs. */
4073 get_class_ivars (tree interface
, bool inherited
)
4075 tree ivar_chain
= copy_list (CLASS_RAW_IVARS (interface
));
4077 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4078 by the current class (i.e., they do not include super-class ivars).
4079 However, the CLASS_IVARS list will be side-effected by a call to
4080 finish_struct(), which will fill in field offsets. */
4081 if (!CLASS_IVARS (interface
))
4082 CLASS_IVARS (interface
) = ivar_chain
;
4087 while (CLASS_SUPER_NAME (interface
))
4089 /* Prepend super-class ivars. */
4090 interface
= lookup_interface (CLASS_SUPER_NAME (interface
));
4091 ivar_chain
= chainon (copy_list (CLASS_RAW_IVARS (interface
)),
4099 objc_maybe_warn_exceptions (location_t loc
)
4101 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4102 For example, on Darwin, ObjC exceptions require a sufficiently
4103 recent version of the runtime, so the user must ask for them
4104 explicitly. On other platforms, at the moment -fobjc-exceptions
4105 triggers -fexceptions which again is required for exceptions to
4107 if (!flag_objc_exceptions
)
4109 /* Warn only once per compilation unit. */
4110 static bool warned
= false;
4114 error_at (loc
, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4120 static struct objc_try_context
*cur_try_context
;
4122 /* Called just after parsing the @try and its associated BODY. We now
4123 must prepare for the tricky bits -- handling the catches and finally. */
4126 objc_begin_try_stmt (location_t try_locus
, tree body
)
4128 struct objc_try_context
*c
= XCNEW (struct objc_try_context
);
4129 c
->outer
= cur_try_context
;
4131 c
->try_locus
= try_locus
;
4132 c
->end_try_locus
= input_location
;
4133 cur_try_context
= c
;
4135 /* Collect the list of local variables. We'll mark them as volatile
4136 at the end of compilation of this function to prevent them being
4137 clobbered by setjmp/longjmp. */
4138 if (flag_objc_sjlj_exceptions
)
4139 objc_mark_locals_volatile (NULL
);
4142 /* Called just after parsing "@catch (parm)". Open a binding level,
4143 enter DECL into the binding level, and initialize it. Leave the
4144 binding level open while the body of the compound statement is
4145 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4146 which we compile as "@catch(id tmp_variable)". */
4149 objc_begin_catch_clause (tree decl
)
4151 tree compound
, type
, t
;
4152 bool ellipsis
= false;
4154 /* Begin a new scope that the entire catch clause will live in. */
4155 compound
= c_begin_compound_stmt (true);
4157 /* Create the appropriate declaration for the argument. */
4158 if (decl
== error_mark_node
)
4159 type
= error_mark_node
;
4162 if (decl
== NULL_TREE
)
4164 /* If @catch(...) was specified, create a temporary variable of
4165 type 'id' and use it. */
4166 decl
= objc_create_temporary_var (objc_object_type
, "__objc_generic_catch_var");
4167 DECL_SOURCE_LOCATION (decl
) = input_location
;
4168 /* ... but allow the runtime to differentiate between ellipsis and the
4169 case of @catch (id xyz). */
4174 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4175 decl
= build_decl (input_location
,
4176 VAR_DECL
, DECL_NAME (decl
), TREE_TYPE (decl
));
4178 lang_hooks
.decls
.pushdecl (decl
);
4180 /* Mark the declaration as used so you never any warnings whether
4181 you use the exception argument or not. TODO: Implement a
4182 -Wunused-exception-parameter flag, which would cause warnings
4183 if exception parameter is not used. */
4184 TREE_USED (decl
) = 1;
4185 DECL_READ_P (decl
) = 1;
4187 type
= TREE_TYPE (decl
);
4190 /* Verify that the type of the catch is valid. It must be a pointer
4191 to an Objective-C class, or "id" (which is catch-all). */
4192 if (type
== error_mark_node
)
4194 ;/* Just keep going. */
4196 else if (!objc_type_valid_for_messaging (type
, false))
4198 error ("@catch parameter is not a known Objective-C class type");
4199 type
= error_mark_node
;
4201 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type
))
4202 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type
)))
4204 error ("@catch parameter can not be protocol-qualified");
4205 type
= error_mark_node
;
4207 else if (POINTER_TYPE_P (type
) && objc_is_object_id (TREE_TYPE (type
)))
4208 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4213 /* If 'type' was built using typedefs, we need to get rid of
4214 them and get a simple pointer to the class. */
4215 bool is_typedef
= false;
4216 tree x
= TYPE_MAIN_VARIANT (type
);
4218 /* Skip from the pointer to the pointee. */
4219 if (TREE_CODE (x
) == POINTER_TYPE
)
4222 /* Traverse typedef aliases */
4223 while (TREE_CODE (x
) == RECORD_TYPE
&& OBJC_TYPE_NAME (x
)
4224 && TREE_CODE (OBJC_TYPE_NAME (x
)) == TYPE_DECL
4225 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x
)))
4228 x
= DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x
));
4231 /* If it was a typedef, build a pointer to the final, original
4234 type
= build_pointer_type (x
);
4236 if (cur_try_context
->catch_list
)
4238 /* Examine previous @catch clauses and see if we've already
4239 caught the type in question. */
4240 tree_stmt_iterator i
= tsi_start (cur_try_context
->catch_list
);
4241 for (; !tsi_end_p (i
); tsi_next (&i
))
4243 tree stmt
= tsi_stmt (i
);
4244 t
= CATCH_TYPES (stmt
);
4245 if (t
== error_mark_node
)
4247 if (!t
|| DERIVED_FROM_P (TREE_TYPE (t
), TREE_TYPE (type
)))
4249 warning (0, "exception of type %<%T%> will be caught",
4251 warning_at (EXPR_LOCATION (stmt
), 0, " by earlier handler for %<%T%>",
4252 TREE_TYPE (t
? t
: objc_object_type
));
4259 t
= (*runtime
.begin_catch
) (&cur_try_context
, type
, decl
, compound
, ellipsis
);
4263 /* Called just after parsing the closing brace of a @catch clause. Close
4264 the open binding level, and record a CATCH_EXPR for it. */
4267 objc_finish_catch_clause (void)
4269 tree c
= cur_try_context
->current_catch
;
4270 cur_try_context
->current_catch
= NULL
;
4271 cur_try_context
->end_catch_locus
= input_location
;
4273 CATCH_BODY (c
) = c_end_compound_stmt (input_location
, CATCH_BODY (c
), 1);
4275 (*runtime
.finish_catch
) (&cur_try_context
, c
);
4278 /* Called after parsing a @finally clause and its associated BODY.
4279 Record the body for later placement. */
4282 objc_build_finally_clause (location_t finally_locus
, tree body
)
4284 cur_try_context
->finally_body
= body
;
4285 cur_try_context
->finally_locus
= finally_locus
;
4286 cur_try_context
->end_finally_locus
= input_location
;
4289 /* Called to finalize a @try construct. */
4292 objc_finish_try_stmt (void)
4294 struct objc_try_context
*c
= cur_try_context
;
4297 if (c
->catch_list
== NULL
&& c
->finally_body
== NULL
)
4298 error ("%<@try%> without %<@catch%> or %<@finally%>");
4300 stmt
= (*runtime
.finish_try_stmt
) (&cur_try_context
);
4303 cur_try_context
= c
->outer
;
4309 objc_build_throw_stmt (location_t loc
, tree throw_expr
)
4311 bool rethrown
= false;
4313 objc_maybe_warn_exceptions (loc
);
4315 /* Don't waste time trying to build something if we're already dead. */
4316 if (throw_expr
== error_mark_node
)
4317 return error_mark_node
;
4319 if (throw_expr
== NULL
)
4321 /* If we're not inside a @catch block, there is no "current
4322 exception" to be rethrown. */
4323 if (cur_try_context
== NULL
4324 || cur_try_context
->current_catch
== NULL
)
4326 error_at (loc
, "%<@throw%> (rethrow) used outside of a @catch block");
4327 return error_mark_node
;
4330 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4331 value that we get from the runtime. */
4332 throw_expr
= (*runtime
.build_exc_ptr
) (&cur_try_context
);
4337 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr
), true))
4339 error_at (loc
, "%<@throw%> argument is not an object");
4340 return error_mark_node
;
4344 return (*runtime
.build_throw_stmt
) (loc
, throw_expr
, rethrown
);
4348 objc_build_synchronized (location_t start_locus
, tree object_expr
, tree body
)
4350 /* object_expr should never be NULL; but in case it is, convert it to
4352 if (object_expr
== NULL
)
4353 object_expr
= error_mark_node
;
4355 /* Validate object_expr. If not valid, set it to error_mark_node. */
4356 if (object_expr
!= error_mark_node
)
4358 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr
), true))
4360 error_at (start_locus
, "%<@synchronized%> argument is not an object");
4361 object_expr
= error_mark_node
;
4365 if (object_expr
== error_mark_node
)
4367 /* If we found an error, we simply ignore the '@synchronized'.
4368 Compile the body so we can keep going with minimal
4370 return add_stmt (body
);
4377 /* objc_sync_enter (object_expr); */
4378 object_expr
= save_expr (object_expr
);
4379 args
= tree_cons (NULL
, object_expr
, NULL
);
4380 call
= build_function_call (input_location
,
4381 objc_sync_enter_decl
, args
);
4382 SET_EXPR_LOCATION (call
, start_locus
);
4385 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4386 it goes inside the @finalize() clause. */
4387 args
= tree_cons (NULL
, object_expr
, NULL
);
4388 call
= build_function_call (input_location
,
4389 objc_sync_exit_decl
, args
);
4390 SET_EXPR_LOCATION (call
, input_location
);
4392 /* @try { body; } */
4393 objc_begin_try_stmt (start_locus
, body
);
4395 /* @finally { objc_sync_exit (object_expr); } */
4396 objc_build_finally_clause (input_location
, call
);
4398 /* End of try statement. */
4399 return objc_finish_try_stmt ();
4403 /* Construct a C struct corresponding to ObjC class CLASS, with the same
4406 struct <classname> {
4407 struct _objc_class *isa;
4412 build_private_template (tree klass
)
4414 if (!CLASS_STATIC_TEMPLATE (klass
))
4416 tree record
= objc_build_struct (klass
,
4417 get_class_ivars (klass
, false),
4418 CLASS_SUPER_NAME (klass
));
4420 /* Set the TREE_USED bit for this struct, so that stab generator
4421 can emit stabs for this struct type. */
4422 if (flag_debug_only_used_symbols
&& TYPE_STUB_DECL (record
))
4423 TREE_USED (TYPE_STUB_DECL (record
)) = 1;
4425 /* Copy the attributes from the class to the type. */
4426 if (TREE_DEPRECATED (klass
))
4427 TREE_DEPRECATED (record
) = 1;
4431 /* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4435 objc_generate_cxx_ctor_or_dtor (bool dtor
)
4437 tree fn
, body
, compound_stmt
, ivar
;
4439 /* - (id) .cxx_construct { ... return self; } */
4440 /* - (void) .cxx_construct { ... } */
4442 objc_start_method_definition
4443 (false /* is_class_method */,
4444 objc_build_method_signature (false /* is_class_method */,
4445 build_tree_list (NULL_TREE
,
4448 : objc_object_type
),
4449 get_identifier (dtor
4451 : TAG_CXX_CONSTRUCT
),
4452 make_node (TREE_LIST
),
4453 false), NULL
, NULL_TREE
);
4454 body
= begin_function_body ();
4455 compound_stmt
= begin_compound_stmt (0);
4457 ivar
= CLASS_IVARS (implementation_template
);
4458 /* Destroy ivars in reverse order. */
4460 ivar
= nreverse (copy_list (ivar
));
4462 for (; ivar
; ivar
= TREE_CHAIN (ivar
))
4464 if (TREE_CODE (ivar
) == FIELD_DECL
)
4466 tree type
= TREE_TYPE (ivar
);
4468 /* Call the ivar's default constructor or destructor. Do not
4469 call the destructor unless a corresponding constructor call
4470 has also been made (or is not needed). */
4471 if (MAYBE_CLASS_TYPE_P (type
)
4473 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4474 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4475 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4476 : (TYPE_NEEDS_CONSTRUCTING (type
)
4477 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))))
4479 (build_special_member_call
4480 (build_ivar_reference (DECL_NAME (ivar
)),
4481 dtor
? complete_dtor_identifier
: complete_ctor_identifier
,
4482 NULL
, type
, LOOKUP_NORMAL
, tf_warning_or_error
));
4486 /* The constructor returns 'self'. */
4488 finish_return_stmt (self_decl
);
4490 finish_compound_stmt (compound_stmt
);
4491 finish_function_body (body
);
4492 fn
= current_function_decl
;
4494 objc_finish_method_definition (fn
);
4497 /* The following routine will examine the current @interface for any
4498 non-POD C++ ivars requiring non-trivial construction and/or
4499 destruction, and then synthesize special '- .cxx_construct' and/or
4500 '- .cxx_destruct' methods which will run the appropriate
4501 construction or destruction code. Note that ivars inherited from
4502 super-classes are _not_ considered. */
4504 objc_generate_cxx_cdtors (void)
4506 bool need_ctor
= false, need_dtor
= false;
4509 /* Error case, due to possibly an extra @end. */
4510 if (!objc_implementation_context
)
4513 /* We do not want to do this for categories, since they do not have
4516 if (TREE_CODE (objc_implementation_context
) != CLASS_IMPLEMENTATION_TYPE
)
4519 /* First, determine if we even need a constructor and/or destructor. */
4521 for (ivar
= CLASS_IVARS (implementation_template
); ivar
;
4522 ivar
= TREE_CHAIN (ivar
))
4524 if (TREE_CODE (ivar
) == FIELD_DECL
)
4526 tree type
= TREE_TYPE (ivar
);
4528 if (MAYBE_CLASS_TYPE_P (type
))
4530 if (TYPE_NEEDS_CONSTRUCTING (type
)
4531 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
4532 /* NB: If a default constructor is not available, we will not
4533 be able to initialize this ivar; the add_instance_variable()
4534 routine will already have warned about this. */
4537 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4538 && (!TYPE_NEEDS_CONSTRUCTING (type
)
4539 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
)))
4540 /* NB: If a default constructor is not available, we will not
4541 call the destructor either, for symmetry. */
4547 /* Generate '- .cxx_construct' if needed. */
4550 objc_generate_cxx_ctor_or_dtor (false);
4552 /* Generate '- .cxx_destruct' if needed. */
4555 objc_generate_cxx_ctor_or_dtor (true);
4557 /* The 'imp_list' variable points at an imp_entry record for the current
4558 @implementation. Record the existence of '- .cxx_construct' and/or
4559 '- .cxx_destruct' methods therein; it will be included in the
4560 metadata for the class if the runtime needs it. */
4561 imp_list
->has_cxx_cdtors
= (need_ctor
|| need_dtor
);
4566 error_with_ivar (const char *message
, tree decl
)
4568 error_at (DECL_SOURCE_LOCATION (decl
), "%s %qs",
4569 message
, identifier_to_locale (gen_declaration (decl
)));
4574 check_ivars (tree inter
, tree imp
)
4576 tree intdecls
= CLASS_RAW_IVARS (inter
);
4577 tree impdecls
= CLASS_RAW_IVARS (imp
);
4584 if (intdecls
&& TREE_CODE (intdecls
) == TYPE_DECL
)
4585 intdecls
= TREE_CHAIN (intdecls
);
4587 if (intdecls
== 0 && impdecls
== 0)
4589 if (intdecls
== 0 || impdecls
== 0)
4591 error ("inconsistent instance variable specification");
4595 t1
= TREE_TYPE (intdecls
); t2
= TREE_TYPE (impdecls
);
4597 if (!comptypes (t1
, t2
)
4598 || !tree_int_cst_equal (DECL_INITIAL (intdecls
),
4599 DECL_INITIAL (impdecls
)))
4601 if (DECL_NAME (intdecls
) == DECL_NAME (impdecls
))
4603 error_with_ivar ("conflicting instance variable type",
4605 error_with_ivar ("previous declaration of",
4608 else /* both the type and the name don't match */
4610 error ("inconsistent instance variable specification");
4615 else if (DECL_NAME (intdecls
) != DECL_NAME (impdecls
))
4617 error_with_ivar ("conflicting instance variable name",
4619 error_with_ivar ("previous declaration of",
4623 intdecls
= DECL_CHAIN (intdecls
);
4624 impdecls
= DECL_CHAIN (impdecls
);
4630 mark_referenced_methods (void)
4632 struct imp_entry
*impent
;
4635 for (impent
= imp_list
; impent
; impent
= impent
->next
)
4637 chain
= CLASS_CLS_METHODS (impent
->imp_context
);
4640 cgraph_mark_needed_node (
4641 cgraph_get_create_node (METHOD_DEFINITION (chain
)));
4642 chain
= DECL_CHAIN (chain
);
4645 chain
= CLASS_NST_METHODS (impent
->imp_context
);
4648 cgraph_mark_needed_node (
4649 cgraph_get_create_node (METHOD_DEFINITION (chain
)));
4650 chain
= DECL_CHAIN (chain
);
4655 /* If type is empty or only type qualifiers are present, add default
4656 type of id (otherwise grokdeclarator will default to int). */
4658 adjust_type_for_id_default (tree type
)
4661 type
= make_node (TREE_LIST
);
4663 if (!TREE_VALUE (type
))
4664 TREE_VALUE (type
) = objc_object_type
;
4665 else if (TREE_CODE (TREE_VALUE (type
)) == RECORD_TYPE
4666 && TYPED_OBJECT (TREE_VALUE (type
)))
4667 error ("can not use an object as parameter to a method");
4672 /* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4673 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4674 OBJC_METHOD_PARM_DECL ?)
4676 A KEYWORD_DECL is a tree representing the declaration of a
4677 parameter of an Objective-C method. It is produced when parsing a
4678 fragment of Objective-C method declaration of the form
4681 selector ':' '(' typename ')' identifier
4683 For example, take the Objective-C method
4685 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
4687 the two fragments "pathForResource:(NSString *)resource" and
4688 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4689 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4690 "pathForResource"), the 'arg_type' (eg, tree representing a
4691 NSString *), the 'arg_name' (eg identifier for "resource") and
4692 potentially some attributes (for example, a tree representing
4693 __attribute__ ((unused)) if such an attribute was attached to a
4694 certain parameter). You can access this information using the
4695 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4696 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
4698 'key_name' is an identifier node (and is optional as you can omit
4699 it in Objective-C methods).
4700 'arg_type' is a tree list (and is optional too if no parameter type
4702 'arg_name' is an identifier node and is required.
4703 'attributes' is an optional tree containing parameter attributes. */
4705 objc_build_keyword_decl (tree key_name
, tree arg_type
,
4706 tree arg_name
, tree attributes
)
4710 if (flag_objc1_only
&& attributes
)
4711 error_at (input_location
, "method argument attributes are not available in Objective-C 1.0");
4713 /* If no type is specified, default to "id". */
4714 arg_type
= adjust_type_for_id_default (arg_type
);
4716 keyword_decl
= make_node (KEYWORD_DECL
);
4718 TREE_TYPE (keyword_decl
) = arg_type
;
4719 KEYWORD_ARG_NAME (keyword_decl
) = arg_name
;
4720 KEYWORD_KEY_NAME (keyword_decl
) = key_name
;
4721 DECL_ATTRIBUTES (keyword_decl
) = attributes
;
4723 return keyword_decl
;
4726 /* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4728 build_keyword_selector (tree selector
)
4731 tree key_chain
, key_name
;
4734 /* Scan the selector to see how much space we'll need. */
4735 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
4737 switch (TREE_CODE (selector
))
4740 key_name
= KEYWORD_KEY_NAME (key_chain
);
4743 key_name
= TREE_PURPOSE (key_chain
);
4750 len
+= IDENTIFIER_LENGTH (key_name
) + 1;
4752 /* Just a ':' arg. */
4756 buf
= (char *) alloca (len
+ 1);
4757 /* Start the buffer out as an empty string. */
4760 for (key_chain
= selector
; key_chain
; key_chain
= TREE_CHAIN (key_chain
))
4762 switch (TREE_CODE (selector
))
4765 key_name
= KEYWORD_KEY_NAME (key_chain
);
4768 key_name
= TREE_PURPOSE (key_chain
);
4769 /* The keyword decl chain will later be used as a function
4770 argument chain. Unhook the selector itself so as to not
4771 confuse other parts of the compiler. */
4772 TREE_PURPOSE (key_chain
) = NULL_TREE
;
4779 strcat (buf
, IDENTIFIER_POINTER (key_name
));
4783 return get_identifier_with_length (buf
, len
);
4786 /* Used for declarations and definitions. */
4789 build_method_decl (enum tree_code code
, tree ret_type
, tree selector
,
4790 tree add_args
, bool ellipsis
)
4794 /* If no type is specified, default to "id". */
4795 ret_type
= adjust_type_for_id_default (ret_type
);
4797 /* Note how a method_decl has a TREE_TYPE which is not the function
4798 type of the function implementing the method, but only the return
4799 type of the method. We may want to change this, and store the
4800 entire function type in there (eg, it may be used to simplify
4801 dealing with attributes below). */
4802 method_decl
= make_node (code
);
4803 TREE_TYPE (method_decl
) = ret_type
;
4805 /* If we have a keyword selector, create an identifier_node that
4806 represents the full selector name (`:' included)... */
4807 if (TREE_CODE (selector
) == KEYWORD_DECL
)
4809 METHOD_SEL_NAME (method_decl
) = build_keyword_selector (selector
);
4810 METHOD_SEL_ARGS (method_decl
) = selector
;
4811 METHOD_ADD_ARGS (method_decl
) = add_args
;
4812 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl
) = ellipsis
;
4816 METHOD_SEL_NAME (method_decl
) = selector
;
4817 METHOD_SEL_ARGS (method_decl
) = NULL_TREE
;
4818 METHOD_ADD_ARGS (method_decl
) = NULL_TREE
;
4824 /* This routine processes objective-c method attributes. */
4827 objc_decl_method_attributes (tree
*node
, tree attributes
, int flags
)
4829 /* TODO: Replace the hackery below. An idea would be to store the
4830 full function type in the method declaration (for example in
4831 TREE_TYPE) and then expose ObjC method declarations to c-family
4832 and they could deal with them by simply treating them as
4835 /* Because of the dangers in the hackery below, we filter out any
4836 attribute that we do not know about. For the ones we know about,
4837 we know that they work with the hackery. For the other ones,
4838 there is no guarantee, so we have to filter them out. */
4839 tree filtered_attributes
= NULL_TREE
;
4844 for (attribute
= attributes
; attribute
; attribute
= TREE_CHAIN (attribute
))
4846 tree name
= TREE_PURPOSE (attribute
);
4848 if (is_attribute_p ("deprecated", name
)
4849 || is_attribute_p ("sentinel", name
)
4850 || is_attribute_p ("noreturn", name
))
4852 /* An attribute that we support; add it to the filtered
4854 filtered_attributes
= chainon (filtered_attributes
,
4855 copy_node (attribute
));
4857 else if (is_attribute_p ("format", name
))
4859 /* "format" is special because before adding it to the
4860 filtered attributes we need to adjust the specified
4861 format by adding the hidden function parameters for
4862 an Objective-C method (self, _cmd). */
4863 tree new_attribute
= copy_node (attribute
);
4865 /* Check the arguments specified with the attribute, and
4866 modify them adding 2 for the two hidden arguments.
4867 Note how this differs from C++; according to the
4868 specs, C++ does not do it so you have to add the +1
4869 yourself. For Objective-C, instead, the compiler
4870 adds the +2 for you. */
4872 /* The attribute arguments have not been checked yet, so
4873 we need to be careful as they could be missing or
4874 invalid. If anything looks wrong, we skip the
4875 process and the compiler will complain about it later
4876 when it validates the attribute. */
4877 /* Check that we have at least three arguments. */
4878 if (TREE_VALUE (new_attribute
)
4879 && TREE_CHAIN (TREE_VALUE (new_attribute
))
4880 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute
))))
4882 tree second_argument
= TREE_CHAIN (TREE_VALUE (new_attribute
));
4883 tree third_argument
= TREE_CHAIN (second_argument
);
4886 /* This is the second argument, the "string-index",
4887 which specifies the index of the format string
4889 number
= TREE_VALUE (second_argument
);
4891 && TREE_CODE (number
) == INTEGER_CST
4892 && TREE_INT_CST_HIGH (number
) == 0)
4894 TREE_VALUE (second_argument
)
4895 = build_int_cst (integer_type_node
,
4896 TREE_INT_CST_LOW (number
) + 2);
4899 /* This is the third argument, the "first-to-check",
4900 which specifies the index of the first argument to
4901 check. This could be 0, meaning it is not available,
4902 in which case we don't need to add 2. Add 2 if not
4904 number
= TREE_VALUE (third_argument
);
4906 && TREE_CODE (number
) == INTEGER_CST
4907 && TREE_INT_CST_HIGH (number
) == 0
4908 && TREE_INT_CST_LOW (number
) != 0)
4910 TREE_VALUE (third_argument
)
4911 = build_int_cst (integer_type_node
,
4912 TREE_INT_CST_LOW (number
) + 2);
4915 filtered_attributes
= chainon (filtered_attributes
,
4918 else if (is_attribute_p ("nonnull", name
))
4920 /* We need to fixup all the argument indexes by adding 2
4921 for the two hidden arguments of an Objective-C method
4922 invocation, similat to what we do above for the
4923 "format" attribute. */
4924 /* FIXME: This works great in terms of implementing the
4925 functionality, but the warnings that are produced by
4926 nonnull do mention the argument index (while the
4927 format ones don't). For example, you could get
4928 "warning: null argument where non-null required
4929 (argument 3)". Now in that message, "argument 3"
4930 includes the 2 hidden arguments; it would be much
4931 more friendly to call it "argument 1", as that would
4932 be consistent with __attribute__ ((nonnnull (1))).
4933 To do this, we'd need to have the C family code that
4934 checks the arguments know about adding/removing 2 to
4935 the argument index ... or alternatively we could
4936 maybe store the "printable" argument index in
4937 addition to the actual argument index ? Some
4938 refactoring is needed to do this elegantly. */
4939 tree new_attribute
= copy_node (attribute
);
4940 tree argument
= TREE_VALUE (attribute
);
4941 while (argument
!= NULL_TREE
)
4943 /* Get the value of the argument and add 2. */
4944 tree number
= TREE_VALUE (argument
);
4946 && TREE_CODE (number
) == INTEGER_CST
4947 && TREE_INT_CST_HIGH (number
) == 0
4948 && TREE_INT_CST_LOW (number
) != 0)
4950 TREE_VALUE (argument
)
4951 = build_int_cst (integer_type_node
,
4952 TREE_INT_CST_LOW (number
) + 2);
4954 argument
= TREE_CHAIN (argument
);
4957 filtered_attributes
= chainon (filtered_attributes
,
4961 warning (OPT_Wattributes
, "%qE attribute directive ignored", name
);
4965 if (filtered_attributes
)
4967 /* This hackery changes the TREE_TYPE of the ObjC method
4968 declaration to be a function type, so that decl_attributes
4969 will treat the ObjC method as if it was a function. Some
4970 attributes (sentinel, format) will be applied to the function
4971 type, changing it in place; so after calling decl_attributes,
4972 we extract the function type attributes and store them in
4973 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4974 deprecated) are applied directly to the method declaration
4975 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4976 is nothing to do. */
4977 tree saved_type
= TREE_TYPE (*node
);
4979 = build_function_type_for_method (TREE_VALUE (saved_type
), *node
,
4981 decl_attributes (node
, filtered_attributes
, flags
);
4982 METHOD_TYPE_ATTRIBUTES (*node
) = TYPE_ATTRIBUTES (TREE_TYPE (*node
));
4983 TREE_TYPE (*node
) = saved_type
;
4988 objc_method_decl (enum tree_code opcode
)
4990 return opcode
== INSTANCE_METHOD_DECL
|| opcode
== CLASS_METHOD_DECL
;
4993 /* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
4994 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
4995 method or calling one. SUPER_FLAG indicates whether this is a send
4996 to super; this makes a difference for the NeXT calling sequence in
4997 which the lookup and the method call are done together. If METHOD is
4998 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
4999 represented as varargs. */
5002 build_function_type_for_method (tree return_type
, tree method
,
5003 int context
, bool super_flag
)
5005 VEC(tree
,gc
) *argtypes
= make_tree_vector ();
5007 bool is_varargs
= false;
5009 (*runtime
.get_arg_type_list_base
) (&argtypes
, method
, context
, super_flag
);
5011 /* No actual method prototype given; remaining args passed as varargs. */
5012 if (method
== NULL_TREE
)
5018 for (t
= METHOD_SEL_ARGS (method
); t
; t
= DECL_CHAIN (t
))
5020 tree arg_type
= TREE_VALUE (TREE_TYPE (t
));
5022 /* Decay argument types for the underlying C function as
5024 arg_type
= objc_decay_parm_type (arg_type
);
5026 VEC_safe_push (tree
, gc
, argtypes
, arg_type
);
5029 if (METHOD_ADD_ARGS (method
))
5031 for (t
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
5032 t
; t
= TREE_CHAIN (t
))
5034 tree arg_type
= TREE_TYPE (TREE_VALUE (t
));
5036 arg_type
= objc_decay_parm_type (arg_type
);
5038 VEC_safe_push (tree
, gc
, argtypes
, arg_type
);
5041 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
5047 ftype
= build_varargs_function_type_vec (return_type
, argtypes
);
5049 ftype
= build_function_type_vec (return_type
, argtypes
);
5051 release_tree_vector (argtypes
);
5056 check_duplicates (hash hsh
, int methods
, int is_class
)
5058 tree meth
= NULL_TREE
;
5066 /* We have two or more methods with the same name but
5070 /* But just how different are those types? If
5071 -Wno-strict-selector-match is specified, we shall not
5072 complain if the differences are solely among types with
5073 identical size and alignment. */
5074 if (!warn_strict_selector_match
)
5076 for (loop
= hsh
->list
; loop
; loop
= loop
->next
)
5077 if (!comp_proto_with_proto (meth
, loop
->value
, 0))
5086 bool type
= TREE_CODE (meth
) == INSTANCE_METHOD_DECL
;
5088 warning_at (input_location
, 0,
5089 "multiple methods named %<%c%E%> found",
5090 (is_class
? '+' : '-'),
5091 METHOD_SEL_NAME (meth
));
5092 inform (DECL_SOURCE_LOCATION (meth
), "using %<%c%s%>",
5094 identifier_to_locale (gen_method_decl (meth
)));
5098 bool type
= TREE_CODE (meth
) == INSTANCE_METHOD_DECL
;
5100 warning_at (input_location
, 0,
5101 "multiple selectors named %<%c%E%> found",
5102 (is_class
? '+' : '-'),
5103 METHOD_SEL_NAME (meth
));
5104 inform (DECL_SOURCE_LOCATION (meth
), "found %<%c%s%>",
5106 identifier_to_locale (gen_method_decl (meth
)));
5109 for (loop
= hsh
->list
; loop
; loop
= loop
->next
)
5111 bool type
= TREE_CODE (loop
->value
) == INSTANCE_METHOD_DECL
;
5113 inform (DECL_SOURCE_LOCATION (loop
->value
), "also found %<%c%s%>",
5115 identifier_to_locale (gen_method_decl (loop
->value
)));
5122 /* If RECEIVER is a class reference, return the identifier node for
5123 the referenced class. RECEIVER is created by objc_get_class_reference,
5124 so we check the exact form created depending on which runtimes are
5128 receiver_is_class_object (tree receiver
, int self
, int super
)
5132 /* The receiver is 'self' or 'super' in the context of a class method. */
5133 if (objc_method_context
5134 && TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
5137 ? CLASS_SUPER_NAME (implementation_template
)
5138 : CLASS_NAME (implementation_template
));
5140 /* The runtime might encapsulate things its own way. */
5141 exp
= (*runtime
.receiver_is_class_object
) (receiver
);
5145 /* The receiver is a function call that returns an id. Check if
5146 it is a call to objc_getClass, if so, pick up the class name.
5148 This is required by the GNU runtime, which compiles
5154 [objc_get_class ("NSObject") alloc];
5156 and then, to check that the receiver responds to the +alloc
5157 method, needs to be able to determine that the objc_get_class()
5158 call returns the NSObject class and not just a generic Class
5161 But, traditionally this is enabled for all runtimes, not just the
5162 GNU one, which means that the compiler is smarter than you'd
5163 expect when dealing with objc_getClass(). For example, with the
5164 Apple runtime, in the code
5166 [objc_getClass ("NSObject") alloc];
5168 the compiler will recognize the objc_getClass() call as special
5169 (due to the code below) and so will know that +alloc is called on
5170 the 'NSObject' class, and can perform the corresponding checks.
5172 Programmers can disable this behaviour by casting the results of
5173 objc_getClass() to 'Class' (this may seem weird because
5174 objc_getClass() is already declared to return 'Class', but the
5175 compiler treats it as a special function). This may be useful if
5176 the class is never declared, and the compiler would complain
5177 about a missing @interface for it. Then, you can do
5179 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5181 to silence the warnings. */
5182 if (TREE_CODE (receiver
) == CALL_EXPR
5183 && (exp
= CALL_EXPR_FN (receiver
))
5184 && TREE_CODE (exp
) == ADDR_EXPR
5185 && (exp
= TREE_OPERAND (exp
, 0))
5186 && TREE_CODE (exp
) == FUNCTION_DECL
5187 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5188 prototypes for objc_get_class(). Thankfully, they seem to share the
5189 same function type. */
5190 && TREE_TYPE (exp
) == TREE_TYPE (objc_get_class_decl
)
5191 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp
)), runtime
.tag_getclass
)
5192 /* We have a call to objc_get_class/objc_getClass! */
5193 && (arg
= CALL_EXPR_ARG (receiver
, 0)))
5196 if (TREE_CODE (arg
) == ADDR_EXPR
5197 && (arg
= TREE_OPERAND (arg
, 0))
5198 && TREE_CODE (arg
) == STRING_CST
)
5199 /* Finally, we have the class name. */
5200 return get_identifier (TREE_STRING_POINTER (arg
));
5205 /* If we are currently building a message expr, this holds
5206 the identifier of the selector of the message. This is
5207 used when printing warnings about argument mismatches. */
5209 static tree current_objc_message_selector
= 0;
5212 objc_message_selector (void)
5214 return current_objc_message_selector
;
5217 /* Construct an expression for sending a message.
5218 MESS has the object to send to in TREE_PURPOSE
5219 and the argument list (including selector) in TREE_VALUE.
5221 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5222 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
5225 objc_build_message_expr (tree receiver
, tree message_args
)
5229 tree args
= TREE_PURPOSE (message_args
);
5231 tree args
= message_args
;
5233 tree method_params
= NULL_TREE
;
5235 if (TREE_CODE (receiver
) == ERROR_MARK
|| TREE_CODE (args
) == ERROR_MARK
)
5236 return error_mark_node
;
5238 /* Obtain the full selector name. */
5239 switch (TREE_CODE (args
))
5241 case IDENTIFIER_NODE
:
5242 /* A unary selector. */
5246 sel_name
= build_keyword_selector (args
);
5252 /* Build the parameter list to give to the method. */
5253 if (TREE_CODE (args
) == TREE_LIST
)
5255 method_params
= chainon (args
, TREE_VALUE (message_args
));
5258 tree chain
= args
, prev
= NULL_TREE
;
5260 /* We have a keyword selector--check for comma expressions. */
5263 tree element
= TREE_VALUE (chain
);
5265 /* We have a comma expression, must collapse... */
5266 if (TREE_CODE (element
) == TREE_LIST
)
5269 TREE_CHAIN (prev
) = element
;
5274 chain
= TREE_CHAIN (chain
);
5276 method_params
= args
;
5281 if (processing_template_decl
)
5282 /* Must wait until template instantiation time. */
5283 return build_min_nt (MESSAGE_SEND_EXPR
, receiver
, sel_name
,
5287 return objc_finish_message_expr (receiver
, sel_name
, method_params
, NULL
);
5290 /* Look up method SEL_NAME that would be suitable for receiver
5291 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5292 nonzero), and report on any duplicates. */
5295 lookup_method_in_hash_lists (tree sel_name
, int is_class
)
5297 hash method_prototype
= NULL
;
5300 method_prototype
= hash_lookup (nst_method_hash_list
,
5303 if (!method_prototype
)
5305 method_prototype
= hash_lookup (cls_method_hash_list
,
5310 return check_duplicates (method_prototype
, 1, is_class
);
5313 /* The 'objc_finish_message_expr' routine is called from within
5314 'objc_build_message_expr' for non-template functions. In the case of
5315 C++ template functions, it is called from 'build_expr_from_tree'
5316 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5318 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5319 if the method being used is deprecated. If it is not NULL, instead
5320 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5321 prototype that was used and is deprecated. This is useful for
5322 getter calls that are always generated when compiling dot-syntax
5323 expressions, even if they may not be used. In that case, we don't
5324 want the warning immediately; we produce it (if needed) at gimplify
5325 stage when we are sure that the deprecated getter is being
5328 objc_finish_message_expr (tree receiver
, tree sel_name
, tree method_params
,
5329 tree
*deprecated_method_prototype
)
5331 tree method_prototype
= NULL_TREE
, rprotos
= NULL_TREE
, rtype
;
5332 tree retval
, class_tree
;
5333 int self
, super
, have_cast
;
5335 /* We have used the receiver, so mark it as read. */
5336 mark_exp_read (receiver
);
5338 /* Extract the receiver of the message, as well as its type
5339 (where the latter may take the form of a cast or be inferred
5340 from the implementation context). */
5342 while (TREE_CODE (rtype
) == COMPOUND_EXPR
5343 || TREE_CODE (rtype
) == MODIFY_EXPR
5344 || CONVERT_EXPR_P (rtype
)
5345 || TREE_CODE (rtype
) == COMPONENT_REF
)
5346 rtype
= TREE_OPERAND (rtype
, 0);
5348 /* self is 1 if this is a message to self, 0 otherwise */
5349 self
= (rtype
== self_decl
);
5351 /* super is 1 if this is a message to super, 0 otherwise. */
5352 super
= (rtype
== UOBJC_SUPER_decl
);
5354 /* rtype is the type of the receiver. */
5355 rtype
= TREE_TYPE (receiver
);
5357 /* have_cast is 1 if the receiver is casted. */
5358 have_cast
= (TREE_CODE (receiver
) == NOP_EXPR
5359 || (TREE_CODE (receiver
) == COMPOUND_EXPR
5360 && !IS_SUPER (rtype
)));
5362 /* If we are calling [super dealloc], reset our warning flag. */
5363 if (super
&& !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name
)))
5364 should_call_super_dealloc
= 0;
5366 /* If the receiver is a class object, retrieve the corresponding
5367 @interface, if one exists. class_tree is the class name
5368 identifier, or NULL_TREE if this is not a class method or the
5369 class name could not be determined (as in the case "Class c; [c
5371 class_tree
= receiver_is_class_object (receiver
, self
, super
);
5373 /* Now determine the receiver type (if an explicit cast has not been
5379 /* We are here when we have no cast, and we have a class
5380 name. So, this is a plain method to a class object, as
5381 in [NSObject alloc]. Find the interface corresponding to
5383 rtype
= lookup_interface (class_tree
);
5385 if (rtype
== NULL_TREE
)
5387 /* If 'rtype' is NULL_TREE at this point it means that
5388 we have seen no @interface corresponding to that
5389 class name, only a @class declaration (alternatively,
5390 this was a call such as [objc_getClass("SomeClass")
5391 alloc], where we've never seen the @interface of
5392 SomeClass). So, we have a class name (class_tree)
5393 but no actual details of the class methods. We won't
5394 be able to check that the class responds to the
5395 method, and we will have to guess the method
5396 prototype. Emit a warning, then keep going (this
5397 will use any method with a matching name, as if the
5398 receiver was of type 'Class'). */
5399 warning (0, "@interface of class %qE not found", class_tree
);
5402 /* Handle `self' and `super'. */
5405 if (!CLASS_SUPER_NAME (implementation_template
))
5407 error ("no super class declared in @interface for %qE",
5408 CLASS_NAME (implementation_template
));
5409 return error_mark_node
;
5411 rtype
= lookup_interface (CLASS_SUPER_NAME (implementation_template
));
5414 rtype
= lookup_interface (CLASS_NAME (implementation_template
));
5417 if (objc_is_id (rtype
))
5419 /* The receiver is of type 'id' or 'Class' (with or without some
5420 protocols attached to it). */
5422 /* We set class_tree to the identifier for 'Class' if this is a
5423 class method, and to NULL_TREE if not. */
5424 class_tree
= (IS_CLASS (rtype
) ? objc_class_name
: NULL_TREE
);
5426 /* 'rprotos' is the list of protocols that the receiver
5428 rprotos
= (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype
))
5429 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype
))
5432 /* We have no information on the type, and we set it to
5436 /* If there are any protocols, check that the method we are
5437 calling appears in the protocol list. If there are no
5438 protocols, this is a message to 'id' or 'Class' and we accept
5439 any method that exists. */
5442 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5443 search in protocols themselves for the method
5446 = lookup_method_in_protocol_list (rprotos
, sel_name
,
5447 class_tree
!= NULL_TREE
);
5449 /* If messaging 'Class <Proto>' but did not find a class
5450 method prototype, search for an instance method instead,
5451 and warn about having done so. */
5452 if (!method_prototype
&& !rtype
&& class_tree
!= NULL_TREE
)
5455 = lookup_method_in_protocol_list (rprotos
, sel_name
, 0);
5457 if (method_prototype
)
5458 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5459 sel_name
, sel_name
);
5465 /* We have a receiver type which is more specific than 'id' or
5467 tree orig_rtype
= rtype
;
5469 if (TREE_CODE (rtype
) == POINTER_TYPE
)
5470 rtype
= TREE_TYPE (rtype
);
5471 /* Traverse typedef aliases */
5472 while (TREE_CODE (rtype
) == RECORD_TYPE
&& OBJC_TYPE_NAME (rtype
)
5473 && TREE_CODE (OBJC_TYPE_NAME (rtype
)) == TYPE_DECL
5474 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype
)))
5475 rtype
= DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype
));
5476 if (TYPED_OBJECT (rtype
))
5478 rprotos
= TYPE_OBJC_PROTOCOL_LIST (rtype
);
5479 rtype
= TYPE_OBJC_INTERFACE (rtype
);
5481 if (!rtype
|| TREE_CODE (rtype
) == IDENTIFIER_NODE
)
5483 /* If we could not find an @interface declaration, we must
5484 have only seen a @class declaration; so, we cannot say
5485 anything more intelligent about which methods the
5486 receiver will understand. Note that this only happens
5487 for instance methods; for class methods to a class where
5488 we have only seen a @class declaration,
5489 lookup_interface() above would have set rtype to
5493 /* We could not find an @interface declaration, yet, if
5494 there are protocols attached to the type, we can
5495 still look up the method in the protocols. Ie, we
5496 are in the following case:
5499 MyClass<MyProtocol> *x;
5502 If 'MyProtocol' has the method 'method', we can check
5503 and retrieve the method prototype. */
5505 = lookup_method_in_protocol_list (rprotos
, sel_name
, 0);
5507 /* At this point, if we have found the method_prototype,
5508 we are quite happy. The details of the class are
5509 irrelevant. If we haven't found it, a warning will
5510 have been produced that the method could not be found
5511 in the protocol, and we won't produce further
5512 warnings (please note that this means that "@class
5513 MyClass; MyClass <MyProtocol> *x;" is exactly
5514 equivalent to "id <MyProtocol> x", which isn't too
5515 satisfactory but it's not easy to see how to do
5522 /* We could not find an @interface declaration, and
5523 there are no protocols attached to the receiver,
5524 so we can't complete the check that the receiver
5525 responds to the method, and we can't retrieve the
5526 method prototype. But, because the receiver has
5527 a well-specified class, the programmer did want
5528 this check to be performed. Emit a warning, then
5529 keep going as if it was an 'id'. To remove the
5530 warning, either include an @interface for the
5531 class, or cast the receiver to 'id'. Note that
5532 rtype is an IDENTIFIER_NODE at this point. */
5533 warning (0, "@interface of class %qE not found", rtype
);
5539 else if (TREE_CODE (rtype
) == CLASS_INTERFACE_TYPE
5540 || TREE_CODE (rtype
) == CLASS_IMPLEMENTATION_TYPE
)
5542 /* We have a valid ObjC class name with an associated
5543 @interface. Look up the method name in the published
5544 @interface for the class (and its superclasses). */
5546 = lookup_method_static (rtype
, sel_name
, class_tree
!= NULL_TREE
);
5548 /* If the method was not found in the @interface, it may still
5549 exist locally as part of the @implementation. */
5550 if (!method_prototype
&& objc_implementation_context
5551 && CLASS_NAME (objc_implementation_context
)
5552 == OBJC_TYPE_NAME (rtype
))
5556 ? CLASS_CLS_METHODS (objc_implementation_context
)
5557 : CLASS_NST_METHODS (objc_implementation_context
)),
5560 /* If we haven't found a candidate method by now, try looking for
5561 it in the protocol list. */
5562 if (!method_prototype
&& rprotos
)
5564 = lookup_method_in_protocol_list (rprotos
, sel_name
,
5565 class_tree
!= NULL_TREE
);
5569 /* We have a type, but it's not an Objective-C type (!). */
5570 warning (0, "invalid receiver type %qs",
5571 identifier_to_locale (gen_type_name (orig_rtype
)));
5572 /* After issuing the "invalid receiver" warning, perform method
5573 lookup as if we were messaging 'id'. */
5574 rtype
= rprotos
= NULL_TREE
;
5577 /* Note that rtype could also be NULL_TREE. This happens if we are
5578 messaging a class by name, but the class was only
5579 forward-declared using @class. */
5581 /* For 'id' or 'Class' receivers, search in the global hash table as
5582 a last resort. For all receivers, warn if protocol searches have
5584 if (!method_prototype
)
5587 warning (0, "%<%c%E%> not found in protocol(s)",
5588 (class_tree
? '+' : '-'),
5593 = lookup_method_in_hash_lists (sel_name
, class_tree
!= NULL_TREE
);
5596 if (!method_prototype
)
5598 static bool warn_missing_methods
= false;
5601 warning (0, "%qE may not respond to %<%c%E%>",
5602 OBJC_TYPE_NAME (rtype
),
5603 (class_tree
? '+' : '-'),
5605 /* If we are messaging an 'id' or 'Class' object and made it here,
5606 then we have failed to find _any_ instance or class method,
5609 warning (0, "no %<%c%E%> method found",
5610 (class_tree
? '+' : '-'),
5613 if (!warn_missing_methods
)
5615 warning_at (input_location
,
5616 0, "(Messages without a matching method signature");
5617 warning_at (input_location
,
5618 0, "will be assumed to return %<id%> and accept");
5619 warning_at (input_location
,
5620 0, "%<...%> as arguments.)");
5621 warn_missing_methods
= true;
5626 /* Warn if the method is deprecated, but not if the receiver is
5627 a generic 'id'. 'id' is used to cast an object to a generic
5628 object of an unspecified class; in that case, we'll use
5629 whatever method prototype we can find to get the method
5630 argument and return types, but it is not appropriate to
5631 produce deprecation warnings since we don't know the class
5632 that the object will be of at runtime. The @interface(s) for
5633 that class may not even be available to the compiler right
5634 now, and it is perfectly possible that the method is marked
5635 as non-deprecated in such @interface(s).
5637 In practice this makes sense since casting an object to 'id'
5638 is often used precisely to turn off warnings associated with
5639 the object being of a particular class. */
5640 if (TREE_DEPRECATED (method_prototype
) && rtype
!= NULL_TREE
)
5642 if (deprecated_method_prototype
)
5643 *deprecated_method_prototype
= method_prototype
;
5645 warn_deprecated_use (method_prototype
, NULL_TREE
);
5649 /* Save the selector name for printing error messages. */
5650 current_objc_message_selector
= sel_name
;
5652 /* Build the method call.
5653 TODO: Get the location from somewhere that will work for delayed
5656 retval
= (*runtime
.build_objc_method_call
) (input_location
, method_prototype
,
5657 receiver
, rtype
, sel_name
,
5658 method_params
, super
);
5660 current_objc_message_selector
= 0;
5666 /* This routine creates a static variable used to implement @protocol(MyProtocol)
5667 expression. This variable will be initialized to global protocol_t meta-data
5670 /* This function is called by the parser when (and only when) a
5671 @protocol() expression is found, in order to compile it. */
5673 objc_build_protocol_expr (tree protoname
)
5675 tree p
= lookup_protocol (protoname
, /* warn if deprecated */ true,
5676 /* definition_required */ false);
5680 error ("cannot find protocol declaration for %qE", protoname
);
5681 return error_mark_node
;
5684 return (*runtime
.get_protocol_reference
) (input_location
, p
);
5687 /* This function is called by the parser when a @selector() expression
5688 is found, in order to compile it. It is only called by the parser
5689 and only to compile a @selector(). LOC is the location of the
5692 objc_build_selector_expr (location_t loc
, tree selnamelist
)
5696 /* Obtain the full selector name. */
5697 switch (TREE_CODE (selnamelist
))
5699 case IDENTIFIER_NODE
:
5700 /* A unary selector. */
5701 selname
= selnamelist
;
5704 selname
= build_keyword_selector (selnamelist
);
5710 /* If we are required to check @selector() expressions as they
5711 are found, check that the selector has been declared. */
5712 if (warn_undeclared_selector
)
5714 /* Look the selector up in the list of all known class and
5715 instance methods (up to this line) to check that the selector
5719 /* First try with instance methods. */
5720 hsh
= hash_lookup (nst_method_hash_list
, selname
);
5722 /* If not found, try with class methods. */
5725 hsh
= hash_lookup (cls_method_hash_list
, selname
);
5728 /* If still not found, print out a warning. */
5731 warning (0, "undeclared selector %qE", selname
);
5735 /* The runtimes do this differently, most particularly, GNU has typed
5736 selectors, whilst NeXT does not. */
5737 return (*runtime
.build_selector_reference
) (loc
, selname
, NULL_TREE
);
5741 build_ivar_reference (tree id
)
5744 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
)
5746 /* Historically, a class method that produced objects (factory
5747 method) would assign `self' to the instance that it
5748 allocated. This would effectively turn the class method into
5749 an instance method. Following this assignment, the instance
5750 variables could be accessed. That practice, while safe,
5751 violates the simple rule that a class method should not refer
5752 to an instance variable. It's better to catch the cases
5753 where this is done unknowingly than to support the above
5755 warning (0, "instance variable %qE accessed in class method",
5757 self_decl
= convert (objc_instance_type
, self_decl
); /* cast */
5760 base
= build_indirect_ref (input_location
, self_decl
, RO_ARROW
);
5761 return (*runtime
.build_ivar_reference
) (input_location
, base
, id
);
5764 /* Compute a hash value for a given method SEL_NAME. */
5767 hash_func (tree sel_name
)
5769 const unsigned char *s
5770 = (const unsigned char *)IDENTIFIER_POINTER (sel_name
);
5774 h
= h
* 67 + *s
++ - 113;
5781 nst_method_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
5782 cls_method_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
5784 cls_name_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
5785 als_name_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
5787 ivar_offset_hash_list
= ggc_alloc_cleared_vec_hash (SIZEHASHTABLE
);
5789 /* Initialize the hash table used to hold the constant string objects. */
5790 string_htab
= htab_create_ggc (31, string_hash
,
5794 /* This routine adds sel_name to the hash list. sel_name is a class or alias
5795 name for the class. If alias name, then value is its underlying class.
5796 If class, the value is NULL_TREE. */
5799 hash_class_name_enter (hash
*hashlist
, tree sel_name
, tree value
)
5802 int slot
= hash_func (sel_name
) % SIZEHASHTABLE
;
5804 obj
= ggc_alloc_hashed_entry ();
5805 if (value
!= NULL_TREE
)
5807 /* Save the underlying class for the 'alias' in the hash table */
5808 attr obj_attr
= ggc_alloc_hashed_attribute ();
5809 obj_attr
->value
= value
;
5810 obj
->list
= obj_attr
;
5814 obj
->next
= hashlist
[slot
];
5815 obj
->key
= sel_name
;
5817 hashlist
[slot
] = obj
; /* append to front */
5822 Searches in the hash table looking for a match for class or alias name.
5826 hash_class_name_lookup (hash
*hashlist
, tree sel_name
)
5830 target
= hashlist
[hash_func (sel_name
) % SIZEHASHTABLE
];
5834 if (sel_name
== target
->key
)
5837 target
= target
->next
;
5842 /* WARNING!!!! hash_enter is called with a method, and will peek
5843 inside to find its selector! But hash_lookup is given a selector
5844 directly, and looks for the selector that's inside the found
5845 entry's key (method) for comparison. */
5848 hash_enter (hash
*hashlist
, tree method
)
5851 int slot
= hash_func (METHOD_SEL_NAME (method
)) % SIZEHASHTABLE
;
5853 obj
= ggc_alloc_hashed_entry ();
5855 obj
->next
= hashlist
[slot
];
5858 hashlist
[slot
] = obj
; /* append to front */
5862 hash_lookup (hash
*hashlist
, tree sel_name
)
5866 target
= hashlist
[hash_func (sel_name
) % SIZEHASHTABLE
];
5870 if (sel_name
== METHOD_SEL_NAME (target
->key
))
5873 target
= target
->next
;
5879 hash_add_attr (hash entry
, tree value
)
5883 obj
= ggc_alloc_hashed_attribute ();
5884 obj
->next
= entry
->list
;
5887 entry
->list
= obj
; /* append to front */
5891 lookup_method (tree mchain
, tree method
)
5895 if (TREE_CODE (method
) == IDENTIFIER_NODE
)
5898 key
= METHOD_SEL_NAME (method
);
5902 if (METHOD_SEL_NAME (mchain
) == key
)
5905 mchain
= DECL_CHAIN (mchain
);
5910 /* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5911 method in INTERFACE, along with any categories and protocols
5912 attached thereto. If method is not found, and the
5913 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5914 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5915 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5916 be found in INTERFACE or any of its superclasses, look for an
5917 _instance_ method of the same name in the root class as a last
5918 resort. This behaviour can be turned off by using
5919 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
5921 If a suitable method cannot be found, return NULL_TREE. */
5924 lookup_method_static (tree interface
, tree ident
, int flags
)
5926 tree meth
= NULL_TREE
, root_inter
= NULL_TREE
;
5927 tree inter
= interface
;
5928 int is_class
= (flags
& OBJC_LOOKUP_CLASS
);
5929 int no_superclasses
= (flags
& OBJC_LOOKUP_NO_SUPER
);
5930 int no_instance_methods_of_root_class
= (flags
& OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS
);
5934 tree chain
= is_class
? CLASS_CLS_METHODS (inter
) : CLASS_NST_METHODS (inter
);
5935 tree category
= inter
;
5937 /* First, look up the method in the class itself. */
5938 if ((meth
= lookup_method (chain
, ident
)))
5941 /* Failing that, look for the method in each category of the class. */
5942 while ((category
= CLASS_CATEGORY_LIST (category
)))
5944 chain
= is_class
? CLASS_CLS_METHODS (category
) : CLASS_NST_METHODS (category
);
5946 /* Check directly in each category. */
5947 if ((meth
= lookup_method (chain
, ident
)))
5950 /* Failing that, check in each category's protocols. */
5951 if (CLASS_PROTOCOL_LIST (category
))
5953 if ((meth
= (lookup_method_in_protocol_list
5954 (CLASS_PROTOCOL_LIST (category
), ident
, is_class
))))
5959 /* If not found in categories, check in protocols of the main class. */
5960 if (CLASS_PROTOCOL_LIST (inter
))
5962 if ((meth
= (lookup_method_in_protocol_list
5963 (CLASS_PROTOCOL_LIST (inter
), ident
, is_class
))))
5967 /* If we were instructed not to look in superclasses, don't. */
5968 if (no_superclasses
)
5971 /* Failing that, climb up the inheritance hierarchy. */
5973 inter
= lookup_interface (CLASS_SUPER_NAME (inter
));
5977 if (is_class
&& !no_instance_methods_of_root_class
)
5979 /* If no class (factory) method was found, check if an _instance_
5980 method of the same name exists in the root class. This is what
5981 the Objective-C runtime will do. */
5982 return lookup_method_static (root_inter
, ident
, 0);
5986 /* If an instance method was not found, return 0. */
5991 /* Add the method to the hash list if it doesn't contain an identical
5995 add_method_to_hash_list (hash
*hash_list
, tree method
)
5999 if (!(hsh
= hash_lookup (hash_list
, METHOD_SEL_NAME (method
))))
6001 /* Install on a global chain. */
6002 hash_enter (hash_list
, method
);
6006 /* Check types against those; if different, add to a list. */
6008 int already_there
= comp_proto_with_proto (method
, hsh
->key
, 1);
6009 for (loop
= hsh
->list
; !already_there
&& loop
; loop
= loop
->next
)
6010 already_there
|= comp_proto_with_proto (method
, loop
->value
, 1);
6012 hash_add_attr (hsh
, method
);
6017 objc_add_method (tree klass
, tree method
, int is_class
, bool is_optional
)
6019 tree existing_method
= NULL_TREE
;
6021 /* The first thing we do is look up the method in the list of
6022 methods already defined in the interface (or implementation). */
6024 existing_method
= lookup_method (CLASS_CLS_METHODS (klass
), method
);
6026 existing_method
= lookup_method (CLASS_NST_METHODS (klass
), method
);
6028 /* In the case of protocols, we have a second list of methods to
6029 consider, the list of optional ones. */
6030 if (TREE_CODE (klass
) == PROTOCOL_INTERFACE_TYPE
)
6032 /* @required methods are added to the protocol's normal list.
6033 @optional methods are added to the protocol's OPTIONAL lists.
6034 Note that adding the methods to the optional lists disables
6035 checking that the methods are implemented by classes
6036 implementing the protocol, since these checks only use the
6037 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
6039 /* First of all, if the method to add is @optional, and we found
6040 it already existing as @required, emit an error. */
6041 if (is_optional
&& existing_method
)
6043 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6044 (is_class
? '+' : '-'),
6045 METHOD_SEL_NAME (existing_method
));
6046 inform (DECL_SOURCE_LOCATION (existing_method
),
6047 "previous declaration of %<%c%E%> as %<@required%>",
6048 (is_class
? '+' : '-'),
6049 METHOD_SEL_NAME (existing_method
));
6052 /* Now check the list of @optional methods if we didn't find the
6053 method in the @required list. */
6054 if (!existing_method
)
6057 existing_method
= lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass
), method
);
6059 existing_method
= lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass
), method
);
6061 if (!is_optional
&& existing_method
)
6063 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6064 (is_class
? '+' : '-'),
6065 METHOD_SEL_NAME (existing_method
));
6066 inform (DECL_SOURCE_LOCATION (existing_method
),
6067 "previous declaration of %<%c%E%> as %<@optional%>",
6068 (is_class
? '+' : '-'),
6069 METHOD_SEL_NAME (existing_method
));
6074 /* If the method didn't exist already, add it. */
6075 if (!existing_method
)
6081 /* Put the method on the list in reverse order. */
6082 TREE_CHAIN (method
) = PROTOCOL_OPTIONAL_CLS_METHODS (klass
);
6083 PROTOCOL_OPTIONAL_CLS_METHODS (klass
) = method
;
6087 TREE_CHAIN (method
) = PROTOCOL_OPTIONAL_NST_METHODS (klass
);
6088 PROTOCOL_OPTIONAL_NST_METHODS (klass
) = method
;
6095 DECL_CHAIN (method
) = CLASS_CLS_METHODS (klass
);
6096 CLASS_CLS_METHODS (klass
) = method
;
6100 DECL_CHAIN (method
) = CLASS_NST_METHODS (klass
);
6101 CLASS_NST_METHODS (klass
) = method
;
6107 /* The method was already defined. Check that the types match
6108 for an @interface for a class or category, or for a
6109 @protocol. Give hard errors on methods with identical
6110 selectors but differing argument and/or return types. We do
6111 not do this for @implementations, because C/C++ will do it
6112 for us (i.e., there will be duplicate function definition
6114 if ((TREE_CODE (klass
) == CLASS_INTERFACE_TYPE
6115 || TREE_CODE (klass
) == CATEGORY_INTERFACE_TYPE
6116 /* Starting with GCC 4.6, we emit the same error for
6117 protocols too. The situation is identical to
6118 @interfaces as there is no possible meaningful reason
6119 for defining the same method with different signatures
6120 in the very same @protocol. If that was allowed,
6121 whenever the protocol is used (both at compile and run
6122 time) there wouldn't be any meaningful way to decide
6123 which of the two method signatures should be used. */
6124 || TREE_CODE (klass
) == PROTOCOL_INTERFACE_TYPE
)
6125 && !comp_proto_with_proto (method
, existing_method
, 1))
6127 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6128 (is_class
? '+' : '-'),
6129 METHOD_SEL_NAME (existing_method
));
6130 inform (DECL_SOURCE_LOCATION (existing_method
),
6131 "previous declaration of %<%c%E%>",
6132 (is_class
? '+' : '-'),
6133 METHOD_SEL_NAME (existing_method
));
6138 add_method_to_hash_list (cls_method_hash_list
, method
);
6141 add_method_to_hash_list (nst_method_hash_list
, method
);
6143 /* Instance methods in root classes (and categories thereof)
6144 may act as class methods as a last resort. We also add
6145 instance methods listed in @protocol declarations to
6146 the class hash table, on the assumption that @protocols
6147 may be adopted by root classes or categories. */
6148 if (TREE_CODE (klass
) == CATEGORY_INTERFACE_TYPE
6149 || TREE_CODE (klass
) == CATEGORY_IMPLEMENTATION_TYPE
)
6150 klass
= lookup_interface (CLASS_NAME (klass
));
6152 if (TREE_CODE (klass
) == PROTOCOL_INTERFACE_TYPE
6153 || !CLASS_SUPER_NAME (klass
))
6154 add_method_to_hash_list (cls_method_hash_list
, method
);
6161 add_class (tree class_name
, tree name
)
6163 struct interface_tuple
**slot
;
6165 /* Put interfaces on list in reverse order. */
6166 TREE_CHAIN (class_name
) = interface_chain
;
6167 interface_chain
= class_name
;
6169 if (interface_htab
== NULL
)
6170 interface_htab
= htab_create_ggc (31, hash_interface
, eq_interface
, NULL
);
6171 slot
= (struct interface_tuple
**)
6172 htab_find_slot_with_hash (interface_htab
, name
,
6173 IDENTIFIER_HASH_VALUE (name
),
6177 *slot
= ggc_alloc_cleared_interface_tuple ();
6180 (*slot
)->class_name
= class_name
;
6182 return interface_chain
;
6186 add_category (tree klass
, tree category
)
6188 /* Put categories on list in reverse order. */
6189 tree cat
= lookup_category (klass
, CLASS_SUPER_NAME (category
));
6193 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6195 CLASS_SUPER_NAME (category
));
6199 CLASS_CATEGORY_LIST (category
) = CLASS_CATEGORY_LIST (klass
);
6200 CLASS_CATEGORY_LIST (klass
) = category
;
6205 /* A flexible array member is a C99 extension where you can use
6206 "type[]" at the end of a struct to mean a variable-length array.
6208 In Objective-C, instance variables are fundamentally members of a
6209 struct, but the struct can always be extended by subclassing; hence
6210 we need to detect and forbid all instance variables declared using
6211 flexible array members.
6213 No check for this is needed in Objective-C++, since C++ does not
6214 have flexible array members. */
6216 /* Determine whether TYPE is a structure with a flexible array member,
6217 a union containing such a structure (possibly recursively) or an
6218 array of such structures or unions. These are all invalid as
6219 instance variable. */
6221 flexible_array_type_p (tree type
)
6224 switch (TREE_CODE (type
))
6227 x
= TYPE_FIELDS (type
);
6230 while (DECL_CHAIN (x
) != NULL_TREE
)
6232 if (TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6233 && TYPE_SIZE (TREE_TYPE (x
)) == NULL_TREE
6234 && TYPE_DOMAIN (TREE_TYPE (x
)) != NULL_TREE
6235 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x
))) == NULL_TREE
)
6239 for (x
= TYPE_FIELDS (type
); x
!= NULL_TREE
; x
= DECL_CHAIN (x
))
6241 if (flexible_array_type_p (TREE_TYPE (x
)))
6245 /* Note that we also check for arrays of something that uses a flexible array member. */
6247 if (flexible_array_type_p (TREE_TYPE (type
)))
6256 /* Produce a printable version of an ivar name. This is only used
6257 inside add_instance_variable. */
6259 printable_ivar_name (tree field_decl
)
6261 if (DECL_NAME (field_decl
))
6262 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl
)));
6264 return _("<unnamed>");
6267 /* Called after parsing each instance variable declaration. Necessary to
6268 preserve typedefs and implement public/private...
6270 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6273 add_instance_variable (tree klass
, objc_ivar_visibility_kind visibility
,
6276 tree field_type
= TREE_TYPE (field_decl
);
6279 if (TREE_CODE (field_type
) == REFERENCE_TYPE
)
6281 error ("illegal reference type specified for instance variable %qs",
6282 printable_ivar_name (field_decl
));
6283 /* Return class as is without adding this ivar. */
6288 if (field_type
== error_mark_node
|| !TYPE_SIZE (field_type
)
6289 || TYPE_SIZE (field_type
) == error_mark_node
)
6290 /* 'type[0]' is allowed, but 'type[]' is not! */
6292 error ("instance variable %qs has unknown size",
6293 printable_ivar_name (field_decl
));
6294 /* Return class as is without adding this ivar. */
6299 /* Also, in C reject a struct with a flexible array member. Ie,
6301 struct A { int x; int[] y; };
6305 struct A instance_variable;
6309 is not valid because if the class is subclassed, we wouldn't be able
6310 to calculate the offset of the next instance variable. */
6311 if (flexible_array_type_p (field_type
))
6313 error ("instance variable %qs uses flexible array member",
6314 printable_ivar_name (field_decl
));
6315 /* Return class as is without adding this ivar. */
6321 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6322 need to either (1) warn the user about it or (2) generate suitable
6323 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6324 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6325 if (MAYBE_CLASS_TYPE_P (field_type
)
6326 && (TYPE_NEEDS_CONSTRUCTING (field_type
)
6327 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
)
6328 || TYPE_POLYMORPHIC_P (field_type
)))
6330 tree type_name
= OBJC_TYPE_NAME (field_type
);
6332 if (flag_objc_call_cxx_cdtors
)
6334 /* Since the ObjC runtime will be calling the constructors and
6335 destructors for us, the only thing we can't handle is the lack
6336 of a default constructor. */
6337 if (TYPE_NEEDS_CONSTRUCTING (field_type
)
6338 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type
))
6340 warning (0, "type %qE has no default constructor to call",
6343 /* If we cannot call a constructor, we should also avoid
6344 calling the destructor, for symmetry. */
6345 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
))
6346 warning (0, "destructor for %qE shall not be run either",
6352 static bool warn_cxx_ivars
= false;
6354 if (TYPE_POLYMORPHIC_P (field_type
))
6356 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6358 error ("type %qE has virtual member functions", type_name
);
6359 error ("illegal aggregate type %qE specified "
6360 "for instance variable %qs",
6361 type_name
, printable_ivar_name (field_decl
));
6362 /* Return class as is without adding this ivar. */
6366 /* User-defined constructors and destructors are not known to Obj-C
6367 and hence will not be called. This may or may not be a problem. */
6368 if (TYPE_NEEDS_CONSTRUCTING (field_type
))
6369 warning (0, "type %qE has a user-defined constructor", type_name
);
6370 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type
))
6371 warning (0, "type %qE has a user-defined destructor", type_name
);
6373 if (!warn_cxx_ivars
)
6375 warning (0, "C++ constructors and destructors will not "
6376 "be invoked for Objective-C fields");
6377 warn_cxx_ivars
= true;
6383 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6386 case OBJC_IVAR_VIS_PROTECTED
:
6387 TREE_PUBLIC (field_decl
) = 0;
6388 TREE_PRIVATE (field_decl
) = 0;
6389 TREE_PROTECTED (field_decl
) = 1;
6392 case OBJC_IVAR_VIS_PACKAGE
:
6393 /* TODO: Implement the package variant. */
6394 case OBJC_IVAR_VIS_PUBLIC
:
6395 TREE_PUBLIC (field_decl
) = 1;
6396 TREE_PRIVATE (field_decl
) = 0;
6397 TREE_PROTECTED (field_decl
) = 0;
6400 case OBJC_IVAR_VIS_PRIVATE
:
6401 TREE_PUBLIC (field_decl
) = 0;
6402 TREE_PRIVATE (field_decl
) = 1;
6403 TREE_PROTECTED (field_decl
) = 0;
6408 CLASS_RAW_IVARS (klass
) = chainon (CLASS_RAW_IVARS (klass
), field_decl
);
6413 /* True if the ivar is private and we are not in its implementation. */
6416 is_private (tree decl
)
6418 return (TREE_PRIVATE (decl
)
6419 && ! is_ivar (CLASS_IVARS (implementation_template
),
6423 /* Searches all the instance variables of 'klass' and of its
6424 superclasses for an instance variable whose name (identifier) is
6425 'ivar_name_ident'. Return the declaration (DECL) of the instance
6426 variable, if found, or NULL_TREE, if not found. */
6428 ivar_of_class (tree klass
, tree ivar_name_ident
)
6430 /* First, look up the ivar in CLASS_RAW_IVARS. */
6431 tree decl_chain
= CLASS_RAW_IVARS (klass
);
6433 for ( ; decl_chain
; decl_chain
= DECL_CHAIN (decl_chain
))
6434 if (DECL_NAME (decl_chain
) == ivar_name_ident
)
6437 /* If not found, search up the class hierarchy. */
6438 while (CLASS_SUPER_NAME (klass
))
6440 klass
= lookup_interface (CLASS_SUPER_NAME (klass
));
6442 decl_chain
= CLASS_RAW_IVARS (klass
);
6444 for ( ; decl_chain
; decl_chain
= DECL_CHAIN (decl_chain
))
6445 if (DECL_NAME (decl_chain
) == ivar_name_ident
)
6452 /* We have an instance variable reference;, check to see if it is public. */
6455 objc_is_public (tree expr
, tree identifier
)
6457 tree basetype
, decl
;
6460 if (processing_template_decl
)
6464 if (TREE_TYPE (expr
) == error_mark_node
)
6467 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (expr
));
6469 if (basetype
&& TREE_CODE (basetype
) == RECORD_TYPE
)
6471 if (TYPE_HAS_OBJC_INFO (basetype
) && TYPE_OBJC_INTERFACE (basetype
))
6473 tree klass
= lookup_interface (OBJC_TYPE_NAME (basetype
));
6477 error ("cannot find interface declaration for %qE",
6478 OBJC_TYPE_NAME (basetype
));
6482 if ((decl
= ivar_of_class (klass
, identifier
)))
6484 if (TREE_PUBLIC (decl
))
6487 /* Important difference between the Stepstone translator:
6488 all instance variables should be public within the context
6489 of the implementation. */
6490 if (objc_implementation_context
6491 && ((TREE_CODE (objc_implementation_context
)
6492 == CLASS_IMPLEMENTATION_TYPE
)
6493 || (TREE_CODE (objc_implementation_context
)
6494 == CATEGORY_IMPLEMENTATION_TYPE
)))
6496 tree curtype
= TYPE_MAIN_VARIANT
6497 (CLASS_STATIC_TEMPLATE
6498 (implementation_template
));
6500 if (basetype
== curtype
6501 || DERIVED_FROM_P (basetype
, curtype
))
6503 int priv
= is_private (decl
);
6506 error ("instance variable %qE is declared private",
6513 /* The 2.95.2 compiler sometimes allowed C functions to access
6514 non-@public ivars. We will let this slide for now... */
6515 if (!objc_method_context
)
6517 warning (0, "instance variable %qE is %s; "
6518 "this will be a hard error in the future",
6520 TREE_PRIVATE (decl
) ? "@private" : "@protected");
6524 error ("instance variable %qE is declared %s",
6526 TREE_PRIVATE (decl
) ? "private" : "protected");
6535 /* Make sure all methods in CHAIN (a list of method declarations from
6536 an @interface or a @protocol) are in IMPLEMENTATION (the
6537 implementation context). This is used to check for example that
6538 all methods declared in an @interface were implemented in an
6541 Some special methods (property setters/getters) are special and if
6542 they are not found in IMPLEMENTATION, we look them up in its
6546 check_methods (tree chain
, tree implementation
, int mtype
)
6551 if (mtype
== (int)'+')
6552 list
= CLASS_CLS_METHODS (implementation
);
6554 list
= CLASS_NST_METHODS (implementation
);
6558 /* If the method is associated with a dynamic property, then it
6559 is Ok not to have the method implementation, as it will be
6560 generated dynamically at runtime. To decide if the method is
6561 associated with a @dynamic property, we search the list of
6562 @synthesize and @dynamic for this implementation, and look
6563 for any @dynamic property with the same setter or getter name
6566 for (x
= IMPL_PROPERTY_DECL (implementation
); x
; x
= TREE_CHAIN (x
))
6567 if (PROPERTY_DYNAMIC (x
)
6568 && (PROPERTY_GETTER_NAME (x
) == METHOD_SEL_NAME (chain
)
6569 || PROPERTY_SETTER_NAME (x
) == METHOD_SEL_NAME (chain
)))
6574 chain
= TREE_CHAIN (chain
); /* next method... */
6578 if (!lookup_method (list
, chain
))
6580 /* If the method is a property setter/getter, we'll still
6581 allow it to be missing if it is implemented by
6582 'interface' or any of its superclasses. */
6583 tree property
= METHOD_PROPERTY_CONTEXT (chain
);
6586 /* Note that since this is a property getter/setter, it
6587 is obviously an instance method. */
6588 tree interface
= NULL_TREE
;
6590 /* For a category, first check the main class
6592 if (TREE_CODE (implementation
) == CATEGORY_IMPLEMENTATION_TYPE
)
6594 interface
= lookup_interface (CLASS_NAME (implementation
));
6596 /* If the method is found in the main class, it's Ok. */
6597 if (lookup_method (CLASS_NST_METHODS (interface
), chain
))
6599 chain
= DECL_CHAIN (chain
);
6603 /* Else, get the superclass. */
6604 if (CLASS_SUPER_NAME (interface
))
6605 interface
= lookup_interface (CLASS_SUPER_NAME (interface
));
6607 interface
= NULL_TREE
;
6610 /* Get the superclass for classes. */
6611 if (TREE_CODE (implementation
) == CLASS_IMPLEMENTATION_TYPE
)
6613 if (CLASS_SUPER_NAME (implementation
))
6614 interface
= lookup_interface (CLASS_SUPER_NAME (implementation
));
6616 interface
= NULL_TREE
;
6619 /* Now, interface is the superclass, if any; go check it. */
6622 if (lookup_method_static (interface
, chain
, 0))
6624 chain
= DECL_CHAIN (chain
);
6628 /* Else, fall through - warn. */
6632 switch (TREE_CODE (implementation
))
6634 case CLASS_IMPLEMENTATION_TYPE
:
6635 warning (0, "incomplete implementation of class %qE",
6636 CLASS_NAME (implementation
));
6638 case CATEGORY_IMPLEMENTATION_TYPE
:
6639 warning (0, "incomplete implementation of category %qE",
6640 CLASS_SUPER_NAME (implementation
));
6648 warning (0, "method definition for %<%c%E%> not found",
6649 mtype
, METHOD_SEL_NAME (chain
));
6652 chain
= DECL_CHAIN (chain
);
6658 /* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
6661 conforms_to_protocol (tree klass
, tree protocol
)
6663 if (TREE_CODE (protocol
) == PROTOCOL_INTERFACE_TYPE
)
6665 tree p
= CLASS_PROTOCOL_LIST (klass
);
6666 while (p
&& TREE_VALUE (p
) != protocol
)
6671 tree super
= (CLASS_SUPER_NAME (klass
)
6672 ? lookup_interface (CLASS_SUPER_NAME (klass
))
6674 int tmp
= super
? conforms_to_protocol (super
, protocol
) : 0;
6683 /* Make sure all methods in CHAIN are accessible as MTYPE methods in
6684 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6687 check_methods_accessible (tree chain
, tree context
, int mtype
)
6691 tree base_context
= context
;
6695 /* If the method is associated with a dynamic property, then it
6696 is Ok not to have the method implementation, as it will be
6697 generated dynamically at runtime. Search for any @dynamic
6698 property with the same setter or getter name as this
6699 method. TODO: Use a hashtable lookup. */
6701 for (x
= IMPL_PROPERTY_DECL (base_context
); x
; x
= TREE_CHAIN (x
))
6702 if (PROPERTY_DYNAMIC (x
)
6703 && (PROPERTY_GETTER_NAME (x
) == METHOD_SEL_NAME (chain
)
6704 || PROPERTY_SETTER_NAME (x
) == METHOD_SEL_NAME (chain
)))
6709 chain
= TREE_CHAIN (chain
); /* next method... */
6713 context
= base_context
;
6717 list
= CLASS_CLS_METHODS (context
);
6719 list
= CLASS_NST_METHODS (context
);
6721 if (lookup_method (list
, chain
))
6724 switch (TREE_CODE (context
))
6726 case CLASS_IMPLEMENTATION_TYPE
:
6727 case CLASS_INTERFACE_TYPE
:
6728 context
= (CLASS_SUPER_NAME (context
)
6729 ? lookup_interface (CLASS_SUPER_NAME (context
))
6732 case CATEGORY_IMPLEMENTATION_TYPE
:
6733 case CATEGORY_INTERFACE_TYPE
:
6734 context
= (CLASS_NAME (context
)
6735 ? lookup_interface (CLASS_NAME (context
))
6743 if (context
== NULL_TREE
)
6747 switch (TREE_CODE (objc_implementation_context
))
6749 case CLASS_IMPLEMENTATION_TYPE
:
6750 warning (0, "incomplete implementation of class %qE",
6751 CLASS_NAME (objc_implementation_context
));
6753 case CATEGORY_IMPLEMENTATION_TYPE
:
6754 warning (0, "incomplete implementation of category %qE",
6755 CLASS_SUPER_NAME (objc_implementation_context
));
6762 warning (0, "method definition for %<%c%E%> not found",
6763 mtype
, METHOD_SEL_NAME (chain
));
6766 chain
= TREE_CHAIN (chain
); /* next method... */
6771 /* Check whether the current interface (accessible via
6772 'objc_implementation_context') actually implements protocol P, along
6773 with any protocols that P inherits. */
6776 check_protocol (tree p
, const char *type
, tree name
)
6778 if (TREE_CODE (p
) == PROTOCOL_INTERFACE_TYPE
)
6782 /* Ensure that all protocols have bodies! */
6785 f1
= check_methods (PROTOCOL_CLS_METHODS (p
),
6786 objc_implementation_context
,
6788 f2
= check_methods (PROTOCOL_NST_METHODS (p
),
6789 objc_implementation_context
,
6794 f1
= check_methods_accessible (PROTOCOL_CLS_METHODS (p
),
6795 objc_implementation_context
,
6797 f2
= check_methods_accessible (PROTOCOL_NST_METHODS (p
),
6798 objc_implementation_context
,
6803 warning (0, "%s %qE does not fully implement the %qE protocol",
6804 type
, name
, PROTOCOL_NAME (p
));
6807 /* Check protocols recursively. */
6808 if (PROTOCOL_LIST (p
))
6810 tree subs
= PROTOCOL_LIST (p
);
6812 lookup_interface (CLASS_SUPER_NAME (implementation_template
));
6816 tree sub
= TREE_VALUE (subs
);
6818 /* If the superclass does not conform to the protocols
6819 inherited by P, then we must! */
6820 if (!super_class
|| !conforms_to_protocol (super_class
, sub
))
6821 check_protocol (sub
, type
, name
);
6822 subs
= TREE_CHAIN (subs
);
6827 /* Check whether the current interface (accessible via
6828 'objc_implementation_context') actually implements the protocols listed
6832 check_protocols (tree proto_list
, const char *type
, tree name
)
6834 for ( ; proto_list
; proto_list
= TREE_CHAIN (proto_list
))
6836 tree p
= TREE_VALUE (proto_list
);
6838 check_protocol (p
, type
, name
);
6842 /* Make sure that the class CLASS_NAME is defined CODE says which kind
6843 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6844 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6845 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6846 SUPER_NAME is the name of the category. For a class extension,
6847 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6849 start_class (enum tree_code code
, tree class_name
, tree super_name
,
6850 tree protocol_list
, tree attributes
)
6852 tree klass
= NULL_TREE
;
6856 if (current_namespace
!= global_namespace
)
6858 error ("Objective-C declarations may only appear in global scope");
6860 #endif /* OBJCPLUS */
6862 if (objc_implementation_context
)
6864 warning (0, "%<@end%> missing in implementation context");
6865 finish_class (objc_implementation_context
);
6866 objc_ivar_chain
= NULL_TREE
;
6867 objc_implementation_context
= NULL_TREE
;
6870 /* If this is a class extension, we'll be "reopening" the existing
6871 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6873 if (code
!= CATEGORY_INTERFACE_TYPE
|| super_name
!= NULL_TREE
)
6875 klass
= make_node (code
);
6876 TYPE_LANG_SLOT_1 (klass
) = make_tree_vec (CLASS_LANG_SLOT_ELTS
);
6879 /* Check for existence of the super class, if one was specified. Note
6880 that we must have seen an @interface, not just a @class. If we
6881 are looking at a @compatibility_alias, traverse it first. */
6882 if ((code
== CLASS_INTERFACE_TYPE
|| code
== CLASS_IMPLEMENTATION_TYPE
)
6885 tree super
= objc_is_class_name (super_name
);
6886 tree super_interface
= NULL_TREE
;
6889 super_interface
= lookup_interface (super
);
6891 if (!super_interface
)
6893 error ("cannot find interface declaration for %qE, superclass of %qE",
6894 super
? super
: super_name
,
6896 super_name
= NULL_TREE
;
6900 if (TREE_DEPRECATED (super_interface
))
6901 warning (OPT_Wdeprecated_declarations
, "class %qE is deprecated",
6907 if (code
!= CATEGORY_INTERFACE_TYPE
|| super_name
!= NULL_TREE
)
6909 CLASS_NAME (klass
) = class_name
;
6910 CLASS_SUPER_NAME (klass
) = super_name
;
6911 CLASS_CLS_METHODS (klass
) = NULL_TREE
;
6914 if (! objc_is_class_name (class_name
)
6915 && (decl
= lookup_name (class_name
)))
6917 error ("%qE redeclared as different kind of symbol",
6919 error ("previous declaration of %q+D",
6925 case CLASS_IMPLEMENTATION_TYPE
:
6929 for (chain
= implemented_classes
; chain
; chain
= TREE_CHAIN (chain
))
6930 if (TREE_VALUE (chain
) == class_name
)
6932 error ("reimplementation of class %qE",
6934 /* TODO: error message saying where it was previously
6938 if (chain
== NULL_TREE
)
6939 implemented_classes
= tree_cons (NULL_TREE
, class_name
,
6940 implemented_classes
);
6943 /* Reset for multiple classes per file. */
6946 objc_implementation_context
= klass
;
6948 /* Lookup the interface for this implementation. */
6950 if (!(implementation_template
= lookup_interface (class_name
)))
6952 warning (0, "cannot find interface declaration for %qE",
6954 add_class (implementation_template
= objc_implementation_context
,
6958 /* If a super class has been specified in the implementation,
6959 insure it conforms to the one specified in the interface. */
6962 && (super_name
!= CLASS_SUPER_NAME (implementation_template
)))
6964 tree previous_name
= CLASS_SUPER_NAME (implementation_template
);
6965 error ("conflicting super class name %qE",
6968 error ("previous declaration of %qE", previous_name
);
6970 error ("previous declaration");
6973 else if (! super_name
)
6975 CLASS_SUPER_NAME (objc_implementation_context
)
6976 = CLASS_SUPER_NAME (implementation_template
);
6980 case CLASS_INTERFACE_TYPE
:
6981 if (lookup_interface (class_name
))
6983 error ("duplicate interface declaration for class %qE", class_name
);
6985 warning (0, "duplicate interface declaration for class %qE", class_name
);
6988 add_class (klass
, class_name
);
6991 CLASS_PROTOCOL_LIST (klass
)
6992 = lookup_and_install_protocols (protocol_list
, /* definition_required */ true);
6997 for (attribute
= attributes
; attribute
; attribute
= TREE_CHAIN (attribute
))
6999 tree name
= TREE_PURPOSE (attribute
);
7001 /* TODO: Document what the objc_exception attribute is/does. */
7002 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
7004 if (is_attribute_p ("deprecated", name
))
7005 TREE_DEPRECATED (klass
) = 1;
7006 else if (is_attribute_p ("objc_exception", name
))
7007 CLASS_HAS_EXCEPTION_ATTR (klass
) = 1;
7009 /* Warn about and ignore all others for now, but store them. */
7010 warning (OPT_Wattributes
, "%qE attribute directive ignored", name
);
7012 TYPE_ATTRIBUTES (klass
) = attributes
;
7016 case CATEGORY_INTERFACE_TYPE
:
7018 tree class_category_is_assoc_with
;
7020 /* For a category, class_name is really the name of the class that
7021 the following set of methods will be associated with. We must
7022 find the interface so that can derive the objects template. */
7023 if (!(class_category_is_assoc_with
= lookup_interface (class_name
)))
7025 error ("cannot find interface declaration for %qE",
7027 exit (FATAL_EXIT_CODE
);
7031 if (TREE_DEPRECATED (class_category_is_assoc_with
))
7032 warning (OPT_Wdeprecated_declarations
, "class %qE is deprecated",
7035 if (super_name
== NULL_TREE
)
7037 /* This is a class extension. Get the original
7038 interface, and continue working on it. */
7039 objc_in_class_extension
= true;
7040 klass
= class_category_is_assoc_with
;
7044 /* Append protocols to the original protocol
7046 CLASS_PROTOCOL_LIST (klass
)
7047 = chainon (CLASS_PROTOCOL_LIST (klass
),
7048 lookup_and_install_protocols
7050 /* definition_required */ true));
7055 add_category (class_category_is_assoc_with
, klass
);
7058 CLASS_PROTOCOL_LIST (klass
)
7059 = lookup_and_install_protocols
7060 (protocol_list
, /* definition_required */ true);
7066 case CATEGORY_IMPLEMENTATION_TYPE
:
7067 /* Reset for multiple classes per file. */
7070 objc_implementation_context
= klass
;
7072 /* For a category, class_name is really the name of the class that
7073 the following set of methods will be associated with. We must
7074 find the interface so that can derive the objects template. */
7076 if (!(implementation_template
= lookup_interface (class_name
)))
7078 error ("cannot find interface declaration for %qE",
7080 exit (FATAL_EXIT_CODE
);
7090 continue_class (tree klass
)
7092 switch (TREE_CODE (klass
))
7094 case CLASS_IMPLEMENTATION_TYPE
:
7095 case CATEGORY_IMPLEMENTATION_TYPE
:
7097 struct imp_entry
*imp_entry
;
7099 /* Check consistency of the instance variables. */
7101 if (CLASS_RAW_IVARS (klass
))
7102 check_ivars (implementation_template
, klass
);
7104 /* code generation */
7106 push_lang_context (lang_name_c
);
7108 build_private_template (implementation_template
);
7109 uprivate_record
= CLASS_STATIC_TEMPLATE (implementation_template
);
7110 objc_instance_type
= build_pointer_type (uprivate_record
);
7112 imp_entry
= ggc_alloc_imp_entry ();
7114 imp_entry
->next
= imp_list
;
7115 imp_entry
->imp_context
= klass
;
7116 imp_entry
->imp_template
= implementation_template
;
7117 ucls_super_ref
= uucls_super_ref
= NULL
;
7118 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
)
7120 imp_entry
->class_decl
= (*runtime
.class_decl
) (klass
);
7121 imp_entry
->meta_decl
= (*runtime
.metaclass_decl
) (klass
);
7125 imp_entry
->class_decl
= (*runtime
.category_decl
) (klass
);
7126 imp_entry
->meta_decl
= NULL
;
7128 imp_entry
->has_cxx_cdtors
= 0;
7130 /* Append to front and increment count. */
7131 imp_list
= imp_entry
;
7132 if (TREE_CODE (klass
) == CLASS_IMPLEMENTATION_TYPE
)
7137 pop_lang_context ();
7138 #endif /* OBJCPLUS */
7140 return get_class_ivars (implementation_template
, true);
7143 case CLASS_INTERFACE_TYPE
:
7145 if (objc_in_class_extension
)
7148 push_lang_context (lang_name_c
);
7149 #endif /* OBJCPLUS */
7150 objc_collecting_ivars
= 1;
7151 build_private_template (klass
);
7152 objc_collecting_ivars
= 0;
7154 pop_lang_context ();
7155 #endif /* OBJCPLUS */
7160 return error_mark_node
;
7164 /* This routine builds name of the setter synthesized function. */
7166 objc_build_property_setter_name (tree ident
)
7168 /* TODO: Use alloca to allocate buffer of appropriate size. */
7169 static char string
[BUFSIZE
];
7170 sprintf (string
, "set%s:", IDENTIFIER_POINTER (ident
));
7171 string
[3] = TOUPPER (string
[3]);
7175 /* This routine prepares the declarations of the property accessor
7176 helper functions (objc_getProperty(), etc) that are used when
7177 @synthesize is used.
7179 runtime-specific routines are built in the respective runtime
7180 initialize functions. */
7182 build_common_objc_property_accessor_helpers (void)
7186 /* Declare the following function:
7188 objc_getProperty (id self, SEL _cmd,
7189 ptrdiff_t offset, BOOL is_atomic); */
7190 type
= build_function_type_list (objc_object_type
,
7196 objc_getProperty_decl
= add_builtin_function ("objc_getProperty",
7197 type
, 0, NOT_BUILT_IN
,
7199 TREE_NOTHROW (objc_getProperty_decl
) = 0;
7201 /* Declare the following function:
7203 objc_setProperty (id self, SEL _cmd,
7204 ptrdiff_t offset, id new_value,
7205 BOOL is_atomic, BOOL should_copy); */
7206 type
= build_function_type_list (void_type_node
,
7214 objc_setProperty_decl
= add_builtin_function ("objc_setProperty",
7215 type
, 0, NOT_BUILT_IN
,
7217 TREE_NOTHROW (objc_setProperty_decl
) = 0;
7220 /* This looks up an ivar in a class (including superclasses). */
7222 lookup_ivar (tree interface
, tree instance_variable_name
)
7228 for (decl_chain
= CLASS_IVARS (interface
); decl_chain
; decl_chain
= DECL_CHAIN (decl_chain
))
7229 if (DECL_NAME (decl_chain
) == instance_variable_name
)
7232 /* Not found. Search superclass if any. */
7233 if (CLASS_SUPER_NAME (interface
))
7234 interface
= lookup_interface (CLASS_SUPER_NAME (interface
));
7240 /* This routine synthesizes a 'getter' method. This is only called
7241 for @synthesize properties. */
7243 objc_synthesize_getter (tree klass
, tree class_methods ATTRIBUTE_UNUSED
, tree property
)
7245 location_t location
= DECL_SOURCE_LOCATION (property
);
7250 /* If user has implemented a getter with same name then do nothing. */
7251 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context
),
7252 PROPERTY_GETTER_NAME (property
)))
7255 /* Find declaration of the property getter in the interface (or
7256 superclass, or protocol). There must be one. */
7257 decl
= lookup_method_static (klass
, PROPERTY_GETTER_NAME (property
), 0);
7259 /* If one not declared in the interface, this condition has already
7260 been reported as user error (because property was not declared in
7265 /* Adapt the 'decl'. Use the source location of the @synthesize
7266 statement for error messages. */
7267 decl
= copy_node (decl
);
7268 DECL_SOURCE_LOCATION (decl
) = location
;
7270 objc_start_method_definition (false /* is_class_method */, decl
, NULL_TREE
,
7272 body
= c_begin_compound_stmt (true);
7274 /* Now we need to decide how we build the getter. There are three
7277 for 'copy' or 'retain' properties we need to use the
7278 objc_getProperty() accessor helper which knows about retain and
7279 copy. It supports both 'nonatomic' and 'atomic' access.
7281 for 'nonatomic, assign' properties we can access the instance
7282 variable directly. 'nonatomic' means we don't have to use locks,
7283 and 'assign' means we don't have to worry about retain or copy.
7284 If you combine the two, it means we can just access the instance
7287 for 'atomic, assign' properties we use objc_copyStruct() (for the
7288 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7289 switch (PROPERTY_ASSIGN_SEMANTICS (property
))
7291 case OBJC_PROPERTY_RETAIN
:
7292 case OBJC_PROPERTY_COPY
:
7294 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7295 tree cmd
, ivar
, offset
, is_atomic
;
7296 cmd
= TREE_CHAIN (DECL_ARGUMENTS (current_function_decl
));
7298 /* Find the ivar to compute the offset. */
7299 ivar
= lookup_ivar (klass
, PROPERTY_IVAR_NAME (property
));
7300 if (!ivar
|| is_private (ivar
))
7302 /* This should never happen. */
7304 "can not find instance variable associated with property");
7305 ret_val
= error_mark_node
;
7308 offset
= byte_position (ivar
);
7310 if (PROPERTY_NONATOMIC (property
))
7311 is_atomic
= boolean_false_node
;
7313 is_atomic
= boolean_true_node
;
7315 ret_val
= build_function_call
7317 /* Function prototype. */
7318 objc_getProperty_decl
,
7320 tree_cons
/* self */
7321 (NULL_TREE
, self_decl
,
7322 tree_cons
/* _cmd */
7324 tree_cons
/* offset */
7326 tree_cons
/* is_atomic */
7327 (NULL_TREE
, is_atomic
, NULL_TREE
)))));
7330 case OBJC_PROPERTY_ASSIGN
:
7331 if (PROPERTY_NONATOMIC (property
))
7333 /* We build "return self->PROPERTY_IVAR_NAME;" */
7334 ret_val
= objc_lookup_ivar (NULL_TREE
, PROPERTY_IVAR_NAME (property
));
7340 <property type> __objc_property_temp;
7341 objc_getPropertyStruct (&__objc_property_temp,
7342 &(self->PROPERTY_IVAR_NAME),
7343 sizeof (type of self->PROPERTY_IVAR_NAME),
7346 return __objc_property_temp;
7348 For the NeXT runtime, we need to use objc_copyStruct
7349 instead of objc_getPropertyStruct. */
7350 tree objc_property_temp_decl
, function_decl
, function_call
;
7351 tree size_of
, is_atomic
;
7353 objc_property_temp_decl
= objc_create_temporary_var (TREE_TYPE (property
), "__objc_property_temp");
7354 DECL_SOURCE_LOCATION (objc_property_temp_decl
) = location
;
7355 objc_property_temp_decl
= lang_hooks
.decls
.pushdecl (objc_property_temp_decl
);
7357 /* sizeof (ivar type). Since the ivar and the property have
7358 the same type, there is no need to lookup the ivar. */
7359 size_of
= c_sizeof_or_alignof_type (location
, TREE_TYPE (property
),
7360 true /* is_sizeof */,
7361 false /* complain */);
7363 if (PROPERTY_NONATOMIC (property
))
7364 is_atomic
= boolean_false_node
;
7366 is_atomic
= boolean_true_node
;
7368 if (objc_copyStruct_decl
)
7369 function_decl
= objc_copyStruct_decl
;
7371 function_decl
= objc_getPropertyStruct_decl
;
7373 function_call
= build_function_call
7375 /* Function prototype. */
7378 tree_cons
/* &__objc_property_temp_decl */
7379 /* Warning: note that using build_fold_addr_expr_loc()
7380 here causes invalid code to be generated. */
7381 (NULL_TREE
, build_unary_op (location
, ADDR_EXPR
, objc_property_temp_decl
, 0),
7382 tree_cons
/* &(self->PROPERTY_IVAR_NAME); */
7383 (NULL_TREE
, build_fold_addr_expr_loc (location
,
7385 (NULL_TREE
, PROPERTY_IVAR_NAME (property
))),
7386 tree_cons
/* sizeof (PROPERTY_IVAR) */
7387 (NULL_TREE
, size_of
,
7388 tree_cons
/* is_atomic */
7389 (NULL_TREE
, is_atomic
,
7390 /* TODO: This is currently ignored by the GNU
7391 runtime, but what about the next one ? */
7392 tree_cons
/* has_strong */
7393 (NULL_TREE
, boolean_true_node
, NULL_TREE
))))));
7395 add_stmt (function_call
);
7397 ret_val
= objc_property_temp_decl
;
7404 gcc_assert (ret_val
);
7407 finish_return_stmt (ret_val
);
7409 c_finish_return (location
, ret_val
, NULL_TREE
);
7412 add_stmt (c_end_compound_stmt (location
, body
, true));
7413 fn
= current_function_decl
;
7417 objc_finish_method_definition (fn
);
7420 /* This routine synthesizes a 'setter' method. */
7423 objc_synthesize_setter (tree klass
, tree class_methods ATTRIBUTE_UNUSED
, tree property
)
7425 location_t location
= DECL_SOURCE_LOCATION (property
);
7428 tree new_value
, statement
;
7430 /* If user has implemented a setter with same name then do nothing. */
7431 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context
),
7432 PROPERTY_SETTER_NAME (property
)))
7435 /* Find declaration of the property setter in the interface (or
7436 superclass, or protocol). There must be one. */
7437 decl
= lookup_method_static (klass
, PROPERTY_SETTER_NAME (property
), 0);
7439 /* If one not declared in the interface, this condition has already
7440 been reported as user error (because property was not declared in
7445 /* Adapt the 'decl'. Use the source location of the @synthesize
7446 statement for error messages. */
7447 decl
= copy_node (decl
);
7448 DECL_SOURCE_LOCATION (decl
) = DECL_SOURCE_LOCATION (property
);
7450 objc_start_method_definition (false /* is_class_method */, decl
, NULL_TREE
,
7453 body
= c_begin_compound_stmt (true);
7455 /* The 'new_value' is the only argument to the method, which is the
7456 3rd argument of the function, after self and _cmd. We use twice
7457 TREE_CHAIN to move forward two arguments. */
7458 new_value
= TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl
)));
7460 /* This would presumably happen if the user has specified a
7461 prototype for the setter that does not have an argument! */
7462 if (new_value
== NULL_TREE
)
7464 /* TODO: This should be caught much earlier than this. */
7465 error_at (DECL_SOURCE_LOCATION (decl
), "invalid setter, it must have one argument");
7466 /* Try to recover somehow. */
7467 new_value
= error_mark_node
;
7470 /* Now we need to decide how we build the setter. There are three
7473 for 'copy' or 'retain' properties we need to use the
7474 objc_setProperty() accessor helper which knows about retain and
7475 copy. It supports both 'nonatomic' and 'atomic' access.
7477 for 'nonatomic, assign' properties we can access the instance
7478 variable directly. 'nonatomic' means we don't have to use locks,
7479 and 'assign' means we don't have to worry about retain or copy.
7480 If you combine the two, it means we can just access the instance
7483 for 'atomic, assign' properties we use objc_copyStruct() (for the
7484 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7485 switch (PROPERTY_ASSIGN_SEMANTICS (property
))
7487 case OBJC_PROPERTY_RETAIN
:
7488 case OBJC_PROPERTY_COPY
:
7490 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7491 tree cmd
, ivar
, offset
, is_atomic
, should_copy
;
7492 cmd
= TREE_CHAIN (DECL_ARGUMENTS (current_function_decl
));
7494 /* Find the ivar to compute the offset. */
7495 ivar
= lookup_ivar (klass
, PROPERTY_IVAR_NAME (property
));
7496 if (!ivar
|| is_private (ivar
))
7499 "can not find instance variable associated with property");
7500 statement
= error_mark_node
;
7503 offset
= byte_position (ivar
);
7505 if (PROPERTY_NONATOMIC (property
))
7506 is_atomic
= boolean_false_node
;
7508 is_atomic
= boolean_true_node
;
7510 if (PROPERTY_ASSIGN_SEMANTICS (property
) == OBJC_PROPERTY_COPY
)
7511 should_copy
= boolean_true_node
;
7513 should_copy
= boolean_false_node
;
7515 statement
= build_function_call
7517 /* Function prototype. */
7518 objc_setProperty_decl
,
7520 tree_cons
/* self */
7521 (NULL_TREE
, self_decl
,
7522 tree_cons
/* _cmd */
7524 tree_cons
/* offset */
7526 tree_cons
/* new_value */
7527 (NULL_TREE
, new_value
,
7528 tree_cons
/* is_atomic */
7529 (NULL_TREE
, is_atomic
,
7530 tree_cons
/* should_copy */
7531 (NULL_TREE
, should_copy
, NULL_TREE
)))))));
7534 case OBJC_PROPERTY_ASSIGN
:
7535 if (PROPERTY_NONATOMIC (property
))
7537 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7538 statement
= build_modify_expr
7540 objc_lookup_ivar (NULL_TREE
, PROPERTY_IVAR_NAME (property
)),
7541 NULL_TREE
, NOP_EXPR
,
7542 location
, new_value
, NULL_TREE
);
7548 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7550 sizeof (type of self->PROPERTY_IVAR_NAME),
7554 For the NeXT runtime, we need to use objc_copyStruct
7555 instead of objc_getPropertyStruct. */
7556 tree function_decl
, size_of
, is_atomic
;
7558 /* sizeof (ivar type). Since the ivar and the property have
7559 the same type, there is no need to lookup the ivar. */
7560 size_of
= c_sizeof_or_alignof_type (location
, TREE_TYPE (property
),
7561 true /* is_sizeof */,
7562 false /* complain */);
7564 if (PROPERTY_NONATOMIC (property
))
7565 is_atomic
= boolean_false_node
;
7567 is_atomic
= boolean_true_node
;
7569 if (objc_copyStruct_decl
)
7570 function_decl
= objc_copyStruct_decl
;
7572 function_decl
= objc_setPropertyStruct_decl
;
7574 statement
= build_function_call
7576 /* Function prototype. */
7579 tree_cons
/* &(self->PROPERTY_IVAR_NAME); */
7580 (NULL_TREE
, build_fold_addr_expr_loc (location
,
7582 (NULL_TREE
, PROPERTY_IVAR_NAME (property
))),
7583 tree_cons
/* &new_value */
7584 (NULL_TREE
, build_fold_addr_expr_loc (location
, new_value
),
7585 tree_cons
/* sizeof (PROPERTY_IVAR) */
7586 (NULL_TREE
, size_of
,
7587 tree_cons
/* is_atomic */
7588 (NULL_TREE
, is_atomic
,
7589 /* TODO: This is currently ignored by the GNU
7590 runtime, but what about the next one ? */
7591 tree_cons
/* has_strong */
7592 (NULL_TREE
, boolean_true_node
, NULL_TREE
))))));
7598 gcc_assert (statement
);
7600 add_stmt (statement
);
7601 add_stmt (c_end_compound_stmt (location
, body
, true));
7602 fn
= current_function_decl
;
7606 objc_finish_method_definition (fn
);
7609 /* This function is a sub-routine of objc_add_synthesize_declaration.
7610 It is called for each property to synthesize once we have
7611 determined that the context is Ok. */
7613 objc_add_synthesize_declaration_for_property (location_t location
, tree interface
,
7614 tree property_name
, tree ivar_name
)
7616 /* Find the @property declaration. */
7620 /* Check that synthesize or dynamic has not already been used for
7621 the same property. */
7622 for (property
= IMPL_PROPERTY_DECL (objc_implementation_context
); property
; property
= TREE_CHAIN (property
))
7623 if (PROPERTY_NAME (property
) == property_name
)
7625 location_t original_location
= DECL_SOURCE_LOCATION (property
);
7627 if (PROPERTY_DYNAMIC (property
))
7628 error_at (location
, "property %qs already specified in %<@dynamic%>",
7629 IDENTIFIER_POINTER (property_name
));
7631 error_at (location
, "property %qs already specified in %<@synthesize%>",
7632 IDENTIFIER_POINTER (property_name
));
7634 if (original_location
!= UNKNOWN_LOCATION
)
7635 inform (original_location
, "originally specified here");
7639 /* Check that the property is declared in the interface. It could
7640 also be declared in a superclass or protocol. */
7641 property
= lookup_property (interface
, property_name
);
7645 error_at (location
, "no declaration of property %qs found in the interface",
7646 IDENTIFIER_POINTER (property_name
));
7651 /* We have to copy the property, because we want to chain it to
7652 the implementation context, and we want to store the source
7653 location of the @synthesize, not of the original
7655 property
= copy_node (property
);
7656 DECL_SOURCE_LOCATION (property
) = location
;
7659 /* Determine PROPERTY_IVAR_NAME. */
7660 if (ivar_name
== NULL_TREE
)
7661 ivar_name
= property_name
;
7663 /* Check that the instance variable exists. You can only use an
7664 instance variable from the same class, not one from the
7665 superclass (this makes sense as it allows us to check that an
7666 instance variable is only used in one synthesized property). */
7668 tree ivar
= is_ivar (CLASS_IVARS (interface
), ivar_name
);
7672 error_at (location
, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
7673 IDENTIFIER_POINTER (property_name
));
7677 if (DECL_BIT_FIELD_TYPE (ivar
))
7678 type_of_ivar
= DECL_BIT_FIELD_TYPE (ivar
);
7680 type_of_ivar
= TREE_TYPE (ivar
);
7682 /* If the instance variable has a different C type, we throw an error ... */
7683 if (!comptypes (TREE_TYPE (property
), type_of_ivar
)
7684 /* ... unless the property is readonly, in which case we allow
7685 the instance variable to be more specialized (this means we
7686 can generate the getter all right and it works). */
7687 && (!PROPERTY_READONLY (property
)
7688 || !objc_compare_types (TREE_TYPE (property
),
7689 type_of_ivar
, -5, NULL_TREE
)))
7691 location_t original_location
= DECL_SOURCE_LOCATION (ivar
);
7693 error_at (location
, "property %qs is using instance variable %qs of incompatible type",
7694 IDENTIFIER_POINTER (property_name
),
7695 IDENTIFIER_POINTER (ivar_name
));
7697 if (original_location
!= UNKNOWN_LOCATION
)
7698 inform (original_location
, "originally specified here");
7701 /* If the instance variable is a bitfield, the property must be
7702 'assign', 'nonatomic' because the runtime getter/setter helper
7703 do not work with bitfield instance variables. */
7704 if (DECL_BIT_FIELD_TYPE (ivar
))
7706 /* If there is an error, we return and not generate any
7707 getter/setter because trying to set up the runtime
7708 getter/setter helper calls with bitfields is at high risk
7711 if (PROPERTY_ASSIGN_SEMANTICS (property
) != OBJC_PROPERTY_ASSIGN
)
7713 location_t original_location
= DECL_SOURCE_LOCATION (ivar
);
7715 error_at (location
, "'assign' property %qs is using bit-field instance variable %qs",
7716 IDENTIFIER_POINTER (property_name
),
7717 IDENTIFIER_POINTER (ivar_name
));
7719 if (original_location
!= UNKNOWN_LOCATION
)
7720 inform (original_location
, "originally specified here");
7724 if (!PROPERTY_NONATOMIC (property
))
7726 location_t original_location
= DECL_SOURCE_LOCATION (ivar
);
7728 error_at (location
, "'atomic' property %qs is using bit-field instance variable %qs",
7729 IDENTIFIER_POINTER (property_name
),
7730 IDENTIFIER_POINTER (ivar_name
));
7732 if (original_location
!= UNKNOWN_LOCATION
)
7733 inform (original_location
, "originally specified here");
7739 /* Check that no other property is using the same instance
7741 for (x
= IMPL_PROPERTY_DECL (objc_implementation_context
); x
; x
= TREE_CHAIN (x
))
7742 if (PROPERTY_IVAR_NAME (x
) == ivar_name
)
7744 location_t original_location
= DECL_SOURCE_LOCATION (x
);
7746 error_at (location
, "property %qs is using the same instance variable as property %qs",
7747 IDENTIFIER_POINTER (property_name
),
7748 IDENTIFIER_POINTER (PROPERTY_NAME (x
)));
7750 if (original_location
!= UNKNOWN_LOCATION
)
7751 inform (original_location
, "originally specified here");
7753 /* We keep going on. This won't cause the compiler to fail;
7754 the failure would most likely be at runtime. */
7757 /* Note that a @synthesize (and only a @synthesize) always sets
7758 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7759 @synthesize by that. */
7760 PROPERTY_IVAR_NAME (property
) = ivar_name
;
7762 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7763 original declaration; they are always set (with the exception of
7764 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
7766 /* Add the property to the list of properties for current implementation. */
7767 TREE_CHAIN (property
) = IMPL_PROPERTY_DECL (objc_implementation_context
);
7768 IMPL_PROPERTY_DECL (objc_implementation_context
) = property
;
7770 /* Note how we don't actually synthesize the getter/setter here; it
7771 would be very natural, but we may miss the fact that the user has
7772 implemented his own getter/setter later on in the @implementation
7773 (in which case we shouldn't generate getter/setter). We wait
7774 until we have parsed it all before generating the code. */
7777 /* This function is called by the parser after a @synthesize
7778 expression is parsed. 'location' is the location of the
7779 @synthesize expression, and 'property_and_ivar_list' is a chained
7780 list of the property and ivar names. */
7782 objc_add_synthesize_declaration (location_t location
, tree property_and_ivar_list
)
7784 tree interface
, chain
;
7786 if (flag_objc1_only
)
7787 error_at (input_location
, "%<@synthesize%> is not available in Objective-C 1.0");
7789 if (property_and_ivar_list
== error_mark_node
)
7792 if (!objc_implementation_context
)
7794 /* We can get here only in Objective-C; the Objective-C++ parser
7795 detects the problem while parsing, outputs the error
7796 "misplaced '@synthesize' Objective-C++ construct" and skips
7798 error_at (location
, "%<@synthesize%> not in @implementation context");
7802 if (TREE_CODE (objc_implementation_context
) == CATEGORY_IMPLEMENTATION_TYPE
)
7804 error_at (location
, "%<@synthesize%> can not be used in categories");
7808 interface
= lookup_interface (CLASS_NAME (objc_implementation_context
));
7811 /* I can't see how this could happen, but it is good as a safety check. */
7813 "%<@synthesize%> requires the @interface of the class to be available");
7817 /* Now, iterate over the properties and do each of them. */
7818 for (chain
= property_and_ivar_list
; chain
; chain
= TREE_CHAIN (chain
))
7820 objc_add_synthesize_declaration_for_property (location
, interface
, TREE_VALUE (chain
),
7821 TREE_PURPOSE (chain
));
7825 /* This function is a sub-routine of objc_add_dynamic_declaration. It
7826 is called for each property to mark as dynamic once we have
7827 determined that the context is Ok. */
7829 objc_add_dynamic_declaration_for_property (location_t location
, tree interface
,
7832 /* Find the @property declaration. */
7835 /* Check that synthesize or dynamic has not already been used for
7836 the same property. */
7837 for (property
= IMPL_PROPERTY_DECL (objc_implementation_context
); property
; property
= TREE_CHAIN (property
))
7838 if (PROPERTY_NAME (property
) == property_name
)
7840 location_t original_location
= DECL_SOURCE_LOCATION (property
);
7842 if (PROPERTY_DYNAMIC (property
))
7843 error_at (location
, "property %qs already specified in %<@dynamic%>",
7844 IDENTIFIER_POINTER (property_name
));
7846 error_at (location
, "property %qs already specified in %<@synthesize%>",
7847 IDENTIFIER_POINTER (property_name
));
7849 if (original_location
!= UNKNOWN_LOCATION
)
7850 inform (original_location
, "originally specified here");
7854 /* Check that the property is declared in the interface. It could
7855 also be declared in a superclass or protocol. */
7856 property
= lookup_property (interface
, property_name
);
7860 error_at (location
, "no declaration of property %qs found in the interface",
7861 IDENTIFIER_POINTER (property_name
));
7866 /* We have to copy the property, because we want to chain it to
7867 the implementation context, and we want to store the source
7868 location of the @synthesize, not of the original
7870 property
= copy_node (property
);
7871 DECL_SOURCE_LOCATION (property
) = location
;
7874 /* Note that a @dynamic (and only a @dynamic) always sets
7875 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7876 (actually, as explained above, PROPERTY_DECL generated by
7877 @property and associated with a @dynamic property are also marked
7878 as PROPERTY_DYNAMIC). */
7879 PROPERTY_DYNAMIC (property
) = 1;
7881 /* Add the property to the list of properties for current implementation. */
7882 TREE_CHAIN (property
) = IMPL_PROPERTY_DECL (objc_implementation_context
);
7883 IMPL_PROPERTY_DECL (objc_implementation_context
) = property
;
7886 /* This function is called by the parser after a @dynamic expression
7887 is parsed. 'location' is the location of the @dynamic expression,
7888 and 'property_list' is a chained list of all the property
7891 objc_add_dynamic_declaration (location_t location
, tree property_list
)
7893 tree interface
, chain
;
7895 if (flag_objc1_only
)
7896 error_at (input_location
, "%<@dynamic%> is not available in Objective-C 1.0");
7898 if (property_list
== error_mark_node
)
7901 if (!objc_implementation_context
)
7903 /* We can get here only in Objective-C; the Objective-C++ parser
7904 detects the problem while parsing, outputs the error
7905 "misplaced '@dynamic' Objective-C++ construct" and skips the
7907 error_at (location
, "%<@dynamic%> not in @implementation context");
7911 /* @dynamic is allowed in categories. */
7912 switch (TREE_CODE (objc_implementation_context
))
7914 case CLASS_IMPLEMENTATION_TYPE
:
7915 interface
= lookup_interface (CLASS_NAME (objc_implementation_context
));
7917 case CATEGORY_IMPLEMENTATION_TYPE
:
7918 interface
= lookup_category (implementation_template
,
7919 CLASS_SUPER_NAME (objc_implementation_context
));
7927 /* I can't see how this could happen, but it is good as a safety check. */
7929 "%<@dynamic%> requires the @interface of the class to be available");
7933 /* Now, iterate over the properties and do each of them. */
7934 for (chain
= property_list
; chain
; chain
= TREE_CHAIN (chain
))
7936 objc_add_dynamic_declaration_for_property (location
, interface
, TREE_VALUE (chain
));
7940 /* Main routine to generate code/data for all the property information for
7941 current implementation (class or category). CLASS is the interface where
7942 ivars are declared. CLASS_METHODS is where methods are found which
7943 could be a class or a category depending on whether we are implementing
7944 property of a class or a category. */
7947 objc_gen_property_data (tree klass
, tree class_methods
)
7951 for (x
= IMPL_PROPERTY_DECL (objc_implementation_context
); x
; x
= TREE_CHAIN (x
))
7953 /* @dynamic property - nothing to check or synthesize. */
7954 if (PROPERTY_DYNAMIC (x
))
7957 /* @synthesize property - need to synthesize the accessors. */
7958 if (PROPERTY_IVAR_NAME (x
))
7960 objc_synthesize_getter (klass
, class_methods
, x
);
7962 if (PROPERTY_READONLY (x
) == 0)
7963 objc_synthesize_setter (klass
, class_methods
, x
);
7972 /* This is called once we see the "@end" in an interface/implementation. */
7975 finish_class (tree klass
)
7977 switch (TREE_CODE (klass
))
7979 case CLASS_IMPLEMENTATION_TYPE
:
7981 /* All metadata generation is done in runtime.generate_metadata(). */
7983 /* Generate what needed for property; setters, getters, etc. */
7984 objc_gen_property_data (implementation_template
, implementation_template
);
7986 if (implementation_template
!= objc_implementation_context
)
7988 /* Ensure that all method listed in the interface contain bodies. */
7989 check_methods (CLASS_CLS_METHODS (implementation_template
),
7990 objc_implementation_context
, '+');
7991 check_methods (CLASS_NST_METHODS (implementation_template
),
7992 objc_implementation_context
, '-');
7994 if (CLASS_PROTOCOL_LIST (implementation_template
))
7995 check_protocols (CLASS_PROTOCOL_LIST (implementation_template
),
7997 CLASS_NAME (objc_implementation_context
));
8001 case CATEGORY_IMPLEMENTATION_TYPE
:
8003 tree category
= lookup_category (implementation_template
, CLASS_SUPER_NAME (klass
));
8007 /* Generate what needed for property; setters, getters, etc. */
8008 objc_gen_property_data (implementation_template
, category
);
8010 /* Ensure all method listed in the interface contain bodies. */
8011 check_methods (CLASS_CLS_METHODS (category
),
8012 objc_implementation_context
, '+');
8013 check_methods (CLASS_NST_METHODS (category
),
8014 objc_implementation_context
, '-');
8016 if (CLASS_PROTOCOL_LIST (category
))
8017 check_protocols (CLASS_PROTOCOL_LIST (category
),
8019 CLASS_SUPER_NAME (objc_implementation_context
));
8023 case CLASS_INTERFACE_TYPE
:
8024 case CATEGORY_INTERFACE_TYPE
:
8025 case PROTOCOL_INTERFACE_TYPE
:
8027 /* Process properties of the class. */
8029 for (x
= CLASS_PROPERTY_DECL (objc_interface_context
); x
; x
= TREE_CHAIN (x
))
8031 /* Now we check that the appropriate getter is declared,
8032 and if not, we declare one ourselves. */
8033 tree getter_decl
= lookup_method (CLASS_NST_METHODS (klass
),
8034 PROPERTY_GETTER_NAME (x
));
8038 /* TODO: Check that the declaration is consistent with the property. */
8043 /* Generate an instance method declaration for the
8044 getter; for example "- (id) name;". In general it
8046 -(type)property_getter_name; */
8047 tree rettype
= build_tree_list (NULL_TREE
, TREE_TYPE (x
));
8048 getter_decl
= build_method_decl (INSTANCE_METHOD_DECL
,
8049 rettype
, PROPERTY_GETTER_NAME (x
),
8051 if (PROPERTY_OPTIONAL (x
))
8052 objc_add_method (objc_interface_context
, getter_decl
, false, true);
8054 objc_add_method (objc_interface_context
, getter_decl
, false, false);
8055 TREE_DEPRECATED (getter_decl
) = TREE_DEPRECATED (x
);
8056 METHOD_PROPERTY_CONTEXT (getter_decl
) = x
;
8059 if (PROPERTY_READONLY (x
) == 0)
8061 /* Now we check that the appropriate setter is declared,
8062 and if not, we declare on ourselves. */
8063 tree setter_decl
= lookup_method (CLASS_NST_METHODS (klass
),
8064 PROPERTY_SETTER_NAME (x
));
8068 /* TODO: Check that the declaration is consistent with the property. */
8073 /* The setter name is something like 'setName:'.
8074 We need the substring 'setName' to build the
8075 method declaration due to how the declaration
8076 works. TODO: build_method_decl() will then
8077 generate back 'setName:' from 'setName'; it
8078 would be more efficient to hook into there. */
8079 const char *full_setter_name
= IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x
));
8080 size_t length
= strlen (full_setter_name
);
8081 char *setter_name
= (char *) alloca (length
);
8082 tree ret_type
, selector
, arg_type
, arg_name
;
8084 strcpy (setter_name
, full_setter_name
);
8085 setter_name
[length
- 1] = '\0';
8086 ret_type
= build_tree_list (NULL_TREE
, void_type_node
);
8087 arg_type
= build_tree_list (NULL_TREE
, TREE_TYPE (x
));
8088 arg_name
= get_identifier ("_value");
8089 selector
= objc_build_keyword_decl (get_identifier (setter_name
),
8090 arg_type
, arg_name
, NULL
);
8091 setter_decl
= build_method_decl (INSTANCE_METHOD_DECL
,
8093 build_tree_list (NULL_TREE
, NULL_TREE
),
8095 if (PROPERTY_OPTIONAL (x
))
8096 objc_add_method (objc_interface_context
, setter_decl
, false, true);
8098 objc_add_method (objc_interface_context
, setter_decl
, false, false);
8099 TREE_DEPRECATED (setter_decl
) = TREE_DEPRECATED (x
);
8100 METHOD_PROPERTY_CONTEXT (setter_decl
) = x
;
8113 add_protocol (tree protocol
)
8115 /* Put protocol on list in reverse order. */
8116 TREE_CHAIN (protocol
) = protocol_chain
;
8117 protocol_chain
= protocol
;
8118 return protocol_chain
;
8121 /* Check that a protocol is defined, and, recursively, that all
8122 protocols that this protocol conforms to are defined too. */
8124 check_that_protocol_is_defined (tree protocol
)
8126 if (!PROTOCOL_DEFINED (protocol
))
8127 warning (0, "definition of protocol %qE not found",
8128 PROTOCOL_NAME (protocol
));
8130 /* If the protocol itself conforms to other protocols, check them
8131 too, recursively. */
8132 if (PROTOCOL_LIST (protocol
))
8136 for (p
= PROTOCOL_LIST (protocol
); p
; p
= TREE_CHAIN (p
))
8137 check_that_protocol_is_defined (TREE_VALUE (p
));
8141 /* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8142 emitted if the protocol is deprecated. If 'definition_required' is
8143 true, a warning is emitted if a full @protocol definition has not
8146 lookup_protocol (tree ident
, bool warn_if_deprecated
, bool definition_required
)
8150 for (chain
= protocol_chain
; chain
; chain
= TREE_CHAIN (chain
))
8151 if (ident
== PROTOCOL_NAME (chain
))
8153 if (warn_if_deprecated
&& TREE_DEPRECATED (chain
))
8155 /* It would be nice to use warn_deprecated_use() here, but
8156 we are using TREE_CHAIN (which is supposed to be the
8157 TYPE_STUB_DECL for a TYPE) for something different. */
8158 warning (OPT_Wdeprecated_declarations
, "protocol %qE is deprecated",
8159 PROTOCOL_NAME (chain
));
8162 if (definition_required
)
8163 check_that_protocol_is_defined (chain
);
8171 /* This function forward declares the protocols named by NAMES. If
8172 they are already declared or defined, the function has no effect. */
8175 objc_declare_protocol (tree name
, tree attributes
)
8177 bool deprecated
= false;
8180 if (current_namespace
!= global_namespace
) {
8181 error ("Objective-C declarations may only appear in global scope");
8183 #endif /* OBJCPLUS */
8185 /* Determine if 'deprecated', the only attribute we recognize for
8186 protocols, was used. Ignore all other attributes. */
8190 for (attribute
= attributes
; attribute
; attribute
= TREE_CHAIN (attribute
))
8192 tree name
= TREE_PURPOSE (attribute
);
8194 if (is_attribute_p ("deprecated", name
))
8197 warning (OPT_Wattributes
, "%qE attribute directive ignored", name
);
8201 if (lookup_protocol (name
, /* warn if deprecated */ false,
8202 /* definition_required */ false) == NULL_TREE
)
8204 tree protocol
= make_node (PROTOCOL_INTERFACE_TYPE
);
8206 TYPE_LANG_SLOT_1 (protocol
)
8207 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS
);
8208 PROTOCOL_NAME (protocol
) = name
;
8209 PROTOCOL_LIST (protocol
) = NULL_TREE
;
8210 add_protocol (protocol
);
8211 PROTOCOL_DEFINED (protocol
) = 0;
8212 PROTOCOL_FORWARD_DECL (protocol
) = NULL_TREE
;
8216 /* TODO: Do we need to store the attributes here ? */
8217 TYPE_ATTRIBUTES (protocol
) = attributes
;
8219 TREE_DEPRECATED (protocol
) = 1;
8225 start_protocol (enum tree_code code
, tree name
, tree list
, tree attributes
)
8228 bool deprecated
= false;
8231 if (current_namespace
!= global_namespace
) {
8232 error ("Objective-C declarations may only appear in global scope");
8234 #endif /* OBJCPLUS */
8236 /* Determine if 'deprecated', the only attribute we recognize for
8237 protocols, was used. Ignore all other attributes. */
8241 for (attribute
= attributes
; attribute
; attribute
= TREE_CHAIN (attribute
))
8243 tree name
= TREE_PURPOSE (attribute
);
8245 if (is_attribute_p ("deprecated", name
))
8248 warning (OPT_Wattributes
, "%qE attribute directive ignored", name
);
8252 protocol
= lookup_protocol (name
, /* warn_if_deprecated */ false,
8253 /* definition_required */ false);
8257 protocol
= make_node (code
);
8258 TYPE_LANG_SLOT_1 (protocol
) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS
);
8260 PROTOCOL_NAME (protocol
) = name
;
8261 PROTOCOL_LIST (protocol
) = lookup_and_install_protocols (list
, /* definition_required */ false);
8262 add_protocol (protocol
);
8263 PROTOCOL_DEFINED (protocol
) = 1;
8264 PROTOCOL_FORWARD_DECL (protocol
) = NULL_TREE
;
8266 check_protocol_recursively (protocol
, list
);
8268 else if (! PROTOCOL_DEFINED (protocol
))
8270 PROTOCOL_DEFINED (protocol
) = 1;
8271 PROTOCOL_LIST (protocol
) = lookup_and_install_protocols (list
, /* definition_required */ false);
8273 check_protocol_recursively (protocol
, list
);
8277 warning (0, "duplicate declaration for protocol %qE",
8283 TYPE_ATTRIBUTES (protocol
) = attributes
;
8285 TREE_DEPRECATED (protocol
) = 1;
8291 /* Decay array and function parameters into pointers. */
8294 objc_decay_parm_type (tree type
)
8296 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == FUNCTION_TYPE
)
8297 type
= build_pointer_type (TREE_CODE (type
) == ARRAY_TYPE
8304 static GTY(()) tree objc_parmlist
= NULL_TREE
;
8306 /* Append PARM to a list of formal parameters of a method, making a necessary
8307 array-to-pointer adjustment along the way. */
8310 objc_push_parm (tree parm
)
8314 if (TREE_TYPE (parm
) == error_mark_node
)
8316 objc_parmlist
= chainon (objc_parmlist
, parm
);
8320 /* Decay arrays and functions into pointers. */
8321 type
= objc_decay_parm_type (TREE_TYPE (parm
));
8323 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8325 if (type
!= TREE_TYPE (parm
))
8326 parm
= build_decl (input_location
, PARM_DECL
, DECL_NAME (parm
), type
);
8328 DECL_ARG_TYPE (parm
)
8329 = lang_hooks
.types
.type_promotes_to (TREE_TYPE (parm
));
8331 /* Record constancy and volatility. */
8332 c_apply_type_quals_to_decl
8333 ((TYPE_READONLY (TREE_TYPE (parm
)) ? TYPE_QUAL_CONST
: 0)
8334 | (TYPE_RESTRICT (TREE_TYPE (parm
)) ? TYPE_QUAL_RESTRICT
: 0)
8335 | (TYPE_VOLATILE (TREE_TYPE (parm
)) ? TYPE_QUAL_VOLATILE
: 0), parm
);
8337 objc_parmlist
= chainon (objc_parmlist
, parm
);
8340 /* Retrieve the formal parameter list constructed via preceding calls to
8341 objc_push_parm(). */
8345 objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED
,
8346 tree expr ATTRIBUTE_UNUSED
)
8348 tree parm_info
= objc_parmlist
;
8349 objc_parmlist
= NULL_TREE
;
8355 objc_get_parm_info (int have_ellipsis
, tree expr
)
8357 tree parm_info
= objc_parmlist
;
8358 struct c_arg_info
*arg_info
;
8359 /* The C front-end requires an elaborate song and dance at
8362 declare_parm_level ();
8365 tree next
= DECL_CHAIN (parm_info
);
8367 DECL_CHAIN (parm_info
) = NULL_TREE
;
8368 parm_info
= pushdecl (parm_info
);
8369 finish_decl (parm_info
, input_location
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
8372 arg_info
= get_parm_info (have_ellipsis
, expr
);
8374 objc_parmlist
= NULL_TREE
;
8379 /* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8380 method definitions. In the case of instance methods, we can be more
8381 specific as to the type of 'self'. */
8384 synth_self_and_ucmd_args (void)
8388 if (objc_method_context
8389 && TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
)
8390 self_type
= objc_instance_type
;
8392 /* Really a `struct objc_class *'. However, we allow people to
8393 assign to self, which changes its type midstream. */
8394 self_type
= objc_object_type
;
8397 objc_push_parm (build_decl (input_location
,
8398 PARM_DECL
, self_id
, self_type
));
8401 objc_push_parm (build_decl (input_location
,
8402 PARM_DECL
, ucmd_id
, objc_selector_type
));
8405 /* Transform an Objective-C method definition into a static C function
8406 definition, synthesizing the first two arguments, "self" and "_cmd",
8407 in the process. EXPR is NULL or an expression that needs to be
8408 evaluated for the side effects of array size expressions in the
8412 start_method_def (tree method
, tree expr
)
8418 struct c_arg_info
*parm_info
;
8420 int have_ellipsis
= 0;
8422 /* If we are defining a "dealloc" method in a non-root class, we
8423 will need to check if a [super dealloc] is missing, and warn if
8425 if(CLASS_SUPER_NAME (objc_implementation_context
)
8426 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method
))))
8427 should_call_super_dealloc
= 1;
8429 should_call_super_dealloc
= 0;
8431 /* Required to implement _msgSuper. */
8432 objc_method_context
= method
;
8433 UOBJC_SUPER_decl
= NULL_TREE
;
8435 /* Generate prototype declarations for arguments..."new-style". */
8436 synth_self_and_ucmd_args ();
8438 /* Generate argument declarations if a keyword_decl. */
8439 parmlist
= METHOD_SEL_ARGS (method
);
8442 /* parmlist is a KEYWORD_DECL. */
8443 tree type
= TREE_VALUE (TREE_TYPE (parmlist
));
8446 parm
= build_decl (input_location
,
8447 PARM_DECL
, KEYWORD_ARG_NAME (parmlist
), type
);
8448 decl_attributes (&parm
, DECL_ATTRIBUTES (parmlist
), 0);
8449 objc_push_parm (parm
);
8450 parmlist
= DECL_CHAIN (parmlist
);
8453 if (METHOD_ADD_ARGS (method
))
8457 for (akey
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
8458 akey
; akey
= TREE_CHAIN (akey
))
8460 objc_push_parm (TREE_VALUE (akey
));
8463 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
8467 parm_info
= objc_get_parm_info (have_ellipsis
, expr
);
8469 really_start_method (objc_method_context
, parm_info
);
8472 /* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8475 objc_types_are_equivalent (tree type1
, tree type2
)
8480 /* Strip away indirections. */
8481 while ((TREE_CODE (type1
) == ARRAY_TYPE
|| TREE_CODE (type1
) == POINTER_TYPE
)
8482 && (TREE_CODE (type1
) == TREE_CODE (type2
)))
8483 type1
= TREE_TYPE (type1
), type2
= TREE_TYPE (type2
);
8484 if (TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
8487 /* Compare the protocol lists. */
8488 type1
= (TYPE_HAS_OBJC_INFO (type1
)
8489 ? TYPE_OBJC_PROTOCOL_LIST (type1
)
8491 type2
= (TYPE_HAS_OBJC_INFO (type2
)
8492 ? TYPE_OBJC_PROTOCOL_LIST (type2
)
8495 /* If there are no protocols (most common case), the types are
8497 if (type1
== NULL_TREE
&& type2
== NULL_TREE
)
8500 /* If one has protocols, and the other one hasn't, they are not
8502 if ((type1
== NULL_TREE
&& type2
!= NULL_TREE
)
8503 || (type1
!= NULL_TREE
&& type2
== NULL_TREE
))
8507 /* Else, both have protocols, and we need to do the full
8508 comparison. It is possible that either type1 or type2
8509 contain some duplicate protocols in the list, so we can't
8510 even just compare list_length as a first check. */
8513 for (t
= type2
; t
; t
= TREE_CHAIN (t
))
8514 if (!lookup_protocol_in_reflist (type1
, TREE_VALUE (t
)))
8517 for (t
= type1
; t
; t
= TREE_CHAIN (t
))
8518 if (!lookup_protocol_in_reflist (type2
, TREE_VALUE (t
)))
8525 /* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
8528 objc_types_share_size_and_alignment (tree type1
, tree type2
)
8530 return (simple_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
8531 && TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
));
8534 /* Return 1 if PROTO1 is equivalent to PROTO2
8535 for purposes of method overloading. Ordinarily, the type signatures
8536 should match up exactly, unless STRICT is zero, in which case we
8537 shall allow differences in which the size and alignment of a type
8541 comp_proto_with_proto (tree proto1
, tree proto2
, int strict
)
8545 /* The following test is needed in case there are hashing
8547 if (METHOD_SEL_NAME (proto1
) != METHOD_SEL_NAME (proto2
))
8550 /* Compare return types. */
8551 type1
= TREE_VALUE (TREE_TYPE (proto1
));
8552 type2
= TREE_VALUE (TREE_TYPE (proto2
));
8554 if (!objc_types_are_equivalent (type1
, type2
)
8555 && (strict
|| !objc_types_share_size_and_alignment (type1
, type2
)))
8558 /* Compare argument types. */
8560 /* The first argument (objc_object_type) is always the same, no need
8563 /* The second argument (objc_selector_type) is always the same, no
8566 /* Compare the other arguments. */
8570 /* Compare METHOD_SEL_ARGS. */
8571 for (arg1
= METHOD_SEL_ARGS (proto1
), arg2
= METHOD_SEL_ARGS (proto2
);
8573 arg1
= DECL_CHAIN (arg1
), arg2
= DECL_CHAIN (arg2
))
8575 type1
= TREE_VALUE (TREE_TYPE (arg1
));
8576 type2
= TREE_VALUE (TREE_TYPE (arg2
));
8578 /* FIXME: Do we need to decay argument types to compare them ? */
8579 type1
= objc_decay_parm_type (type1
);
8580 type2
= objc_decay_parm_type (type2
);
8582 if (!objc_types_are_equivalent (type1
, type2
)
8583 && (strict
|| !objc_types_share_size_and_alignment (type1
, type2
)))
8587 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8592 /* Compare METHOD_ADD_ARGS. */
8593 if ((METHOD_ADD_ARGS (proto1
) && !METHOD_ADD_ARGS (proto2
))
8594 || (METHOD_ADD_ARGS (proto2
) && !METHOD_ADD_ARGS (proto1
)))
8597 if (METHOD_ADD_ARGS (proto1
))
8599 for (arg1
= TREE_CHAIN (METHOD_ADD_ARGS (proto1
)), arg2
= TREE_CHAIN (METHOD_ADD_ARGS (proto2
));
8601 arg1
= TREE_CHAIN (arg1
), arg2
= TREE_CHAIN (arg2
))
8603 type1
= TREE_TYPE (TREE_VALUE (arg1
));
8604 type2
= TREE_TYPE (TREE_VALUE (arg2
));
8606 /* FIXME: Do we need to decay argument types to compare them ? */
8607 type1
= objc_decay_parm_type (type1
);
8608 type2
= objc_decay_parm_type (type2
);
8610 if (!objc_types_are_equivalent (type1
, type2
)
8611 && (strict
|| !objc_types_share_size_and_alignment (type1
, type2
)))
8616 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8621 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8622 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1
) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2
))
8630 /* This routine returns true if TYPE is a valid objc object type,
8631 suitable for messaging; false otherwise. If 'accept_class' is
8632 'true', then a Class object is considered valid for messaging and
8633 'true' is returned if 'type' refers to a Class. If 'accept_class'
8634 is 'false', then a Class object is not considered valid for
8635 messaging and 'false' is returned in that case. */
8638 objc_type_valid_for_messaging (tree type
, bool accept_classes
)
8640 if (!POINTER_TYPE_P (type
))
8643 /* Remove the pointer indirection; don't remove more than one
8644 otherwise we'd consider "NSObject **" a valid type for messaging,
8646 type
= TREE_TYPE (type
);
8648 if (TREE_CODE (type
) != RECORD_TYPE
)
8651 if (objc_is_object_id (type
))
8654 if (objc_is_class_id (type
))
8655 return accept_classes
;
8657 if (TYPE_HAS_OBJC_INFO (type
))
8664 objc_start_function (tree name
, tree type
, tree attrs
,
8668 struct c_arg_info
*params
8672 tree fndecl
= build_decl (input_location
,
8673 FUNCTION_DECL
, name
, type
);
8676 DECL_ARGUMENTS (fndecl
) = params
;
8677 DECL_INITIAL (fndecl
) = error_mark_node
;
8678 DECL_EXTERNAL (fndecl
) = 0;
8679 TREE_STATIC (fndecl
) = 1;
8680 retrofit_lang_decl (fndecl
);
8681 cplus_decl_attributes (&fndecl
, attrs
, 0);
8682 start_preparsed_function (fndecl
, attrs
, /*flags=*/SF_DEFAULT
);
8684 current_function_returns_value
= 0; /* Assume, until we see it does. */
8685 current_function_returns_null
= 0;
8686 decl_attributes (&fndecl
, attrs
, 0);
8687 announce_function (fndecl
);
8688 DECL_INITIAL (fndecl
) = error_mark_node
;
8689 DECL_EXTERNAL (fndecl
) = 0;
8690 TREE_STATIC (fndecl
) = 1;
8691 current_function_decl
= pushdecl (fndecl
);
8693 declare_parm_level ();
8694 DECL_RESULT (current_function_decl
)
8695 = build_decl (input_location
,
8696 RESULT_DECL
, NULL_TREE
,
8697 TREE_TYPE (TREE_TYPE (current_function_decl
)));
8698 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl
)) = 1;
8699 DECL_IGNORED_P (DECL_RESULT (current_function_decl
)) = 1;
8700 start_fname_decls ();
8701 store_parm_decls_from (params
);
8704 TREE_USED (current_function_decl
) = 1;
8707 /* - Generate an identifier for the function. the format is "_n_cls",
8708 where 1 <= n <= nMethods, and cls is the name the implementation we
8710 - Install the return type from the method declaration.
8711 - If we have a prototype, check for type consistency. */
8714 really_start_method (tree method
,
8718 struct c_arg_info
*parmlist
8722 tree ret_type
, meth_type
;
8724 const char *sel_name
, *class_name
, *cat_name
;
8727 /* Synth the storage class & assemble the return type. */
8728 ret_type
= TREE_VALUE (TREE_TYPE (method
));
8730 sel_name
= IDENTIFIER_POINTER (METHOD_SEL_NAME (method
));
8731 class_name
= IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context
));
8732 cat_name
= ((TREE_CODE (objc_implementation_context
)
8733 == CLASS_IMPLEMENTATION_TYPE
)
8735 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context
)));
8738 /* Make sure this is big enough for any plausible method label. */
8739 buf
= (char *) alloca (50 + strlen (sel_name
) + strlen (class_name
)
8740 + (cat_name
? strlen (cat_name
) : 0));
8742 OBJC_GEN_METHOD_LABEL (buf
, TREE_CODE (method
) == INSTANCE_METHOD_DECL
,
8743 class_name
, cat_name
, sel_name
, method_slot
);
8745 method_id
= get_identifier (buf
);
8748 /* Objective-C methods cannot be overloaded, so we don't need
8749 the type encoding appended. It looks bad anyway... */
8750 push_lang_context (lang_name_c
);
8753 meth_type
= build_function_type_for_method (ret_type
, method
, METHOD_DEF
, 0);
8754 objc_start_function (method_id
, meth_type
, NULL_TREE
, parmlist
);
8756 /* Set self_decl from the first argument. */
8757 self_decl
= DECL_ARGUMENTS (current_function_decl
);
8759 /* Suppress unused warnings. */
8760 TREE_USED (self_decl
) = 1;
8761 DECL_READ_P (self_decl
) = 1;
8762 TREE_USED (DECL_CHAIN (self_decl
)) = 1;
8763 DECL_READ_P (DECL_CHAIN (self_decl
)) = 1;
8765 pop_lang_context ();
8768 METHOD_DEFINITION (method
) = current_function_decl
;
8770 /* Check consistency...start_function, pushdecl, duplicate_decls. */
8772 if (implementation_template
!= objc_implementation_context
)
8775 = lookup_method_static (implementation_template
,
8776 METHOD_SEL_NAME (method
),
8777 ((TREE_CODE (method
) == CLASS_METHOD_DECL
)
8778 | OBJC_LOOKUP_NO_SUPER
));
8782 if (!comp_proto_with_proto (method
, proto
, 1))
8784 bool type
= TREE_CODE (method
) == INSTANCE_METHOD_DECL
;
8786 warning_at (DECL_SOURCE_LOCATION (method
), 0,
8787 "conflicting types for %<%c%s%>",
8789 identifier_to_locale (gen_method_decl (method
)));
8790 inform (DECL_SOURCE_LOCATION (proto
),
8791 "previous declaration of %<%c%s%>",
8793 identifier_to_locale (gen_method_decl (proto
)));
8797 /* If the method in the @interface was deprecated, mark
8798 the implemented method as deprecated too. It should
8799 never be used for messaging (when the deprecation
8800 warnings are produced), but just in case. */
8801 if (TREE_DEPRECATED (proto
))
8802 TREE_DEPRECATED (method
) = 1;
8804 /* If the method in the @interface was marked as
8805 'noreturn', mark the function implementing the method
8806 as 'noreturn' too. */
8807 TREE_THIS_VOLATILE (current_function_decl
) = TREE_THIS_VOLATILE (proto
);
8812 /* We have a method @implementation even though we did not
8813 see a corresponding @interface declaration (which is allowed
8814 by Objective-C rules). Go ahead and place the method in
8815 the @interface anyway, so that message dispatch lookups
8817 tree interface
= implementation_template
;
8819 if (TREE_CODE (objc_implementation_context
)
8820 == CATEGORY_IMPLEMENTATION_TYPE
)
8821 interface
= lookup_category
8823 CLASS_SUPER_NAME (objc_implementation_context
));
8826 objc_add_method (interface
, copy_node (method
),
8827 TREE_CODE (method
) == CLASS_METHOD_DECL
,
8828 /* is_optional= */ false);
8833 static void *UOBJC_SUPER_scope
= 0;
8835 /* _n_Method (id self, SEL sel, ...)
8837 struct objc_super _S;
8838 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8842 get_super_receiver (void)
8844 if (objc_method_context
)
8846 tree super_expr
, super_expr_list
, class_expr
;
8848 if (!UOBJC_SUPER_decl
)
8850 UOBJC_SUPER_decl
= build_decl (input_location
,
8851 VAR_DECL
, get_identifier (TAG_SUPER
),
8852 objc_super_template
);
8853 /* This prevents `unused variable' warnings when compiling with -Wall. */
8854 TREE_USED (UOBJC_SUPER_decl
) = 1;
8855 DECL_READ_P (UOBJC_SUPER_decl
) = 1;
8856 lang_hooks
.decls
.pushdecl (UOBJC_SUPER_decl
);
8857 finish_decl (UOBJC_SUPER_decl
, input_location
, NULL_TREE
, NULL_TREE
,
8859 UOBJC_SUPER_scope
= objc_get_current_scope ();
8862 /* Set receiver to self. */
8863 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
, self_id
);
8864 super_expr
= build_modify_expr (input_location
, super_expr
, NULL_TREE
,
8865 NOP_EXPR
, input_location
, self_decl
,
8867 super_expr_list
= super_expr
;
8869 /* Set class to begin searching. */
8870 /* Get the ident for the superclass class field & build a ref to it.
8871 ??? maybe we should just name the field the same for all runtimes. */
8872 super_expr
= (*runtime
.super_superclassfield_ident
) ();
8873 super_expr
= objc_build_component_ref (UOBJC_SUPER_decl
, super_expr
);
8875 gcc_assert (imp_list
->imp_context
== objc_implementation_context
8876 && imp_list
->imp_template
== implementation_template
);
8877 inst_meth
= (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
);
8879 if (TREE_CODE (objc_implementation_context
) == CLASS_IMPLEMENTATION_TYPE
)
8880 class_expr
= (*runtime
.get_class_super_ref
) (input_location
,
8881 imp_list
, inst_meth
);
8883 /* We have a category. */
8885 tree super_name
= CLASS_SUPER_NAME (imp_list
->imp_template
);
8888 /* Barf if super used in a category of a root object. */
8891 error ("no super class declared in interface for %qE",
8892 CLASS_NAME (imp_list
->imp_template
));
8893 return error_mark_node
;
8896 super_class
= (*runtime
.get_category_super_ref
) (input_location
,
8897 imp_list
, inst_meth
);
8898 class_expr
= build_c_cast (input_location
,
8899 TREE_TYPE (super_expr
), super_class
);
8902 super_expr
= build_modify_expr (input_location
, super_expr
, NULL_TREE
,
8904 input_location
, class_expr
, NULL_TREE
);
8906 super_expr_list
= build_compound_expr (input_location
,
8907 super_expr_list
, super_expr
);
8909 super_expr
= build_unary_op (input_location
,
8910 ADDR_EXPR
, UOBJC_SUPER_decl
, 0);
8911 super_expr_list
= build_compound_expr (input_location
,
8912 super_expr_list
, super_expr
);
8914 return super_expr_list
;
8918 error ("[super ...] must appear in a method context");
8919 return error_mark_node
;
8923 /* When exiting a scope, sever links to a 'super' declaration (if any)
8924 therein contained. */
8927 objc_clear_super_receiver (void)
8929 if (objc_method_context
8930 && UOBJC_SUPER_scope
== objc_get_current_scope ())
8932 UOBJC_SUPER_decl
= 0;
8933 UOBJC_SUPER_scope
= 0;
8938 objc_finish_method_definition (tree fndecl
)
8940 /* We cannot validly inline ObjC methods, at least not without a language
8941 extension to declare that a method need not be dynamically
8942 dispatched, so suppress all thoughts of doing so. */
8943 DECL_UNINLINABLE (fndecl
) = 1;
8946 /* The C++ front-end will have called finish_function() for us. */
8950 METHOD_ENCODING (objc_method_context
)
8951 = encode_method_prototype (objc_method_context
);
8953 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8954 since the optimizer may find "may be used before set" errors. */
8955 objc_method_context
= NULL_TREE
;
8957 if (should_call_super_dealloc
)
8958 warning (0, "method possibly missing a [super dealloc] call");
8961 /* Given a tree DECL node, produce a printable description of it in the given
8962 buffer, overwriting the buffer. */
8965 gen_declaration (tree decl
)
8971 gen_type_name_0 (TREE_TYPE (decl
));
8973 if (DECL_NAME (decl
))
8975 if (!POINTER_TYPE_P (TREE_TYPE (decl
)))
8976 strcat (errbuf
, " ");
8978 strcat (errbuf
, IDENTIFIER_POINTER (DECL_NAME (decl
)));
8981 if (DECL_INITIAL (decl
)
8982 && TREE_CODE (DECL_INITIAL (decl
)) == INTEGER_CST
)
8983 sprintf (errbuf
+ strlen (errbuf
), ": " HOST_WIDE_INT_PRINT_DEC
,
8984 TREE_INT_CST_LOW (DECL_INITIAL (decl
)));
8990 /* Given a tree TYPE node, produce a printable description of it in the given
8991 buffer, overwriting the buffer. */
8994 gen_type_name_0 (tree type
)
8996 tree orig
= type
, proto
;
8998 if (TYPE_P (type
) && TYPE_NAME (type
))
8999 type
= TYPE_NAME (type
);
9000 else if (POINTER_TYPE_P (type
) || TREE_CODE (type
) == ARRAY_TYPE
)
9002 tree inner
= TREE_TYPE (type
);
9004 while (TREE_CODE (inner
) == ARRAY_TYPE
)
9005 inner
= TREE_TYPE (inner
);
9007 gen_type_name_0 (inner
);
9009 if (!POINTER_TYPE_P (inner
))
9010 strcat (errbuf
, " ");
9012 if (POINTER_TYPE_P (type
))
9013 strcat (errbuf
, "*");
9015 while (type
!= inner
)
9017 strcat (errbuf
, "[");
9019 if (TYPE_DOMAIN (type
))
9023 sprintf (sz
, HOST_WIDE_INT_PRINT_DEC
,
9025 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1));
9026 strcat (errbuf
, sz
);
9029 strcat (errbuf
, "]");
9030 type
= TREE_TYPE (type
);
9036 if (TREE_CODE (type
) == TYPE_DECL
&& DECL_NAME (type
))
9037 type
= DECL_NAME (type
);
9039 strcat (errbuf
, TREE_CODE (type
) == IDENTIFIER_NODE
9040 ? IDENTIFIER_POINTER (type
)
9043 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
9044 if (objc_is_id (orig
))
9045 orig
= TREE_TYPE (orig
);
9047 proto
= TYPE_HAS_OBJC_INFO (orig
) ? TYPE_OBJC_PROTOCOL_LIST (orig
) : NULL_TREE
;
9051 strcat (errbuf
, " <");
9055 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto
))));
9056 proto
= TREE_CHAIN (proto
);
9057 strcat (errbuf
, proto
? ", " : ">");
9066 gen_type_name (tree type
)
9070 return gen_type_name_0 (type
);
9073 /* Given a method tree, put a printable description into the given
9074 buffer (overwriting) and return a pointer to the buffer. */
9077 gen_method_decl (tree method
)
9081 strcpy (errbuf
, "("); /* NB: Do _not_ call strcat() here. */
9082 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method
)));
9083 strcat (errbuf
, ")");
9084 chain
= METHOD_SEL_ARGS (method
);
9088 /* We have a chain of keyword_decls. */
9091 if (KEYWORD_KEY_NAME (chain
))
9092 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain
)));
9094 strcat (errbuf
, ":(");
9095 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain
)));
9096 strcat (errbuf
, ")");
9098 strcat (errbuf
, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain
)));
9099 if ((chain
= DECL_CHAIN (chain
)))
9100 strcat (errbuf
, " ");
9104 if (METHOD_ADD_ARGS (method
))
9106 chain
= TREE_CHAIN (METHOD_ADD_ARGS (method
));
9108 /* Know we have a chain of parm_decls. */
9111 strcat (errbuf
, ", ");
9112 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain
)));
9113 chain
= TREE_CHAIN (chain
);
9116 if (METHOD_ADD_ARGS_ELLIPSIS_P (method
))
9117 strcat (errbuf
, ", ...");
9122 /* We have a unary selector. */
9123 strcat (errbuf
, IDENTIFIER_POINTER (METHOD_SEL_NAME (method
)));
9131 /* Dump an @interface declaration of the supplied class CHAIN to the
9132 supplied file FP. Used to implement the -gen-decls option (which
9133 prints out an @interface declaration of all classes compiled in
9134 this run); potentially useful for debugging the compiler too. */
9136 dump_interface (FILE *fp
, tree chain
)
9138 /* FIXME: A heap overflow here whenever a method (or ivar)
9139 declaration is so long that it doesn't fit in the buffer. The
9140 code and all the related functions should be rewritten to avoid
9141 using fixed size buffers. */
9142 const char *my_name
= IDENTIFIER_POINTER (CLASS_NAME (chain
));
9143 tree ivar_decls
= CLASS_RAW_IVARS (chain
);
9144 tree nst_methods
= CLASS_NST_METHODS (chain
);
9145 tree cls_methods
= CLASS_CLS_METHODS (chain
);
9147 fprintf (fp
, "\n@interface %s", my_name
);
9149 /* CLASS_SUPER_NAME is used to store the superclass name for
9150 classes, and the category name for categories. */
9151 if (CLASS_SUPER_NAME (chain
))
9153 const char *name
= IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain
));
9155 switch (TREE_CODE (chain
))
9157 case CATEGORY_IMPLEMENTATION_TYPE
:
9158 case CATEGORY_INTERFACE_TYPE
:
9159 fprintf (fp
, " (%s)\n", name
);
9162 fprintf (fp
, " : %s\n", name
);
9169 /* FIXME - the following doesn't seem to work at the moment. */
9172 fprintf (fp
, "{\n");
9175 fprintf (fp
, "\t%s;\n", gen_declaration (ivar_decls
));
9176 ivar_decls
= TREE_CHAIN (ivar_decls
);
9179 fprintf (fp
, "}\n");
9184 fprintf (fp
, "- %s;\n", gen_method_decl (nst_methods
));
9185 nst_methods
= TREE_CHAIN (nst_methods
);
9190 fprintf (fp
, "+ %s;\n", gen_method_decl (cls_methods
));
9191 cls_methods
= TREE_CHAIN (cls_methods
);
9194 fprintf (fp
, "@end\n");
9198 /* Produce the pretty printing for an Objective-C method. This is
9199 currently unused, but could be handy while reorganizing the pretty
9200 printing to be more robust. */
9202 objc_pretty_print_method (bool is_class_method
,
9203 const char *class_name
,
9204 const char *category_name
,
9205 const char *selector
)
9209 char *result
= XNEWVEC (char, strlen (class_name
) + strlen (category_name
)
9210 + strlen (selector
) + 7);
9212 if (is_class_method
)
9213 sprintf (result
, "+[%s(%s) %s]", class_name
, category_name
, selector
);
9215 sprintf (result
, "-[%s(%s) %s]", class_name
, category_name
, selector
);
9221 char *result
= XNEWVEC (char, strlen (class_name
)
9222 + strlen (selector
) + 5);
9224 if (is_class_method
)
9225 sprintf (result
, "+[%s %s]", class_name
, selector
);
9227 sprintf (result
, "-[%s %s]", class_name
, selector
);
9234 /* Demangle function for Objective-C. Attempt to demangle the
9235 function name associated with a method (eg, going from
9236 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9237 purpose of pretty printing or error messages. Return the demangled
9238 name, or NULL if the string is not an Objective-C mangled method
9241 Because of how the mangling is done, any method that has a '_' in
9242 its original name is at risk of being demangled incorrectly. In
9243 some cases there are multiple valid ways to demangle a method name
9244 and there is no way we can decide.
9246 TODO: objc_demangle() can't always get it right; the right way to
9247 get this correct for all method names would be to store the
9248 Objective-C method name somewhere in the function decl. Then,
9249 there is no demangling to do; we'd just pull the method name out of
9250 the decl. As an additional bonus, when printing error messages we
9251 could check for such a method name, and if we find it, we know the
9252 function is actually an Objective-C method and we could print error
9253 messages saying "In method '+[NSObject class]" instead of "In
9254 function '+[NSObject class]" as we do now. */
9256 objc_demangle (const char *mangled
)
9258 char *demangled
, *cp
;
9260 /* First of all, if the name is too short it can't be an Objective-C
9261 mangled method name. */
9262 if (mangled
[0] == '\0' || mangled
[1] == '\0' || mangled
[2] == '\0')
9265 /* If the name looks like an already demangled one, return it
9266 unchanged. This should only happen on Darwin, where method names
9267 are mangled differently into a pretty-print form (such as
9268 '+[NSObject class]', see darwin.h). In that case, demangling is
9269 a no-op, but we need to return the demangled name if it was an
9270 ObjC one, and return NULL if not. We should be safe as no C/C++
9271 function can start with "-[" or "+[". */
9272 if ((mangled
[0] == '-' || mangled
[0] == '+')
9273 && (mangled
[1] == '['))
9276 if (mangled
[0] == '_' &&
9277 (mangled
[1] == 'i' || mangled
[1] == 'c') &&
9280 cp
= demangled
= XNEWVEC (char, strlen(mangled
) + 2);
9281 if (mangled
[1] == 'i')
9282 *cp
++ = '-'; /* for instance method */
9284 *cp
++ = '+'; /* for class method */
9285 *cp
++ = '['; /* opening left brace */
9286 strcpy(cp
, mangled
+3); /* tack on the rest of the mangled name */
9287 while (*cp
&& *cp
== '_')
9288 cp
++; /* skip any initial underbars in class name */
9289 cp
= strchr(cp
, '_'); /* find first non-initial underbar */
9292 free(demangled
); /* not mangled name */
9295 if (cp
[1] == '_') /* easy case: no category name */
9297 *cp
++ = ' '; /* replace two '_' with one ' ' */
9298 strcpy(cp
, mangled
+ (cp
- demangled
) + 2);
9302 *cp
++ = '('; /* less easy case: category name */
9303 cp
= strchr(cp
, '_');
9306 free(demangled
); /* not mangled name */
9310 *cp
++ = ' '; /* overwriting 1st char of method name... */
9311 strcpy(cp
, mangled
+ (cp
- demangled
)); /* get it back */
9313 /* Now we have the method name. We need to generally replace
9314 '_' with ':' but trying to preserve '_' if it could only have
9315 been in the mangled string because it was already in the
9316 original name. In cases where it's ambiguous, we assume that
9317 any '_' originated from a ':'. */
9319 /* Initial '_'s in method name can't have been generating by
9320 converting ':'s. Skip them. */
9321 while (*cp
&& *cp
== '_')
9324 /* If the method name does not end with '_', then it has no
9325 arguments and there was no replacement of ':'s with '_'s
9326 during mangling. Check for that case, and skip any
9327 replacement if so. This at least guarantees that methods
9328 with no arguments are always demangled correctly (unless the
9329 original name ends with '_'). */
9330 if (*(mangled
+ strlen (mangled
) - 1) != '_')
9332 /* Skip to the end. */
9338 /* Replace remaining '_' with ':'. This may get it wrong if
9339 there were '_'s in the original name. In most cases it
9340 is impossible to disambiguate. */
9345 *cp
++ = ']'; /* closing right brace */
9346 *cp
++ = 0; /* string terminator */
9350 return NULL
; /* not an objc mangled name */
9353 /* Try to pretty-print a decl. If the 'decl' is an Objective-C
9354 specific decl, return the printable name for it. If not, return
9357 objc_maybe_printable_name (tree decl
, int v ATTRIBUTE_UNUSED
)
9359 switch (TREE_CODE (decl
))
9362 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl
)));
9365 /* The following happens when we are printing a deprecation
9366 warning for a method. The warn_deprecation() will end up
9367 trying to print the decl for INSTANCE_METHOD_DECL or
9368 CLASS_METHOD_DECL. It would be nice to be able to print
9369 "-[NSObject autorelease] is deprecated", but to do that, we'd
9370 need to store the class and method name in the method decl,
9371 which we currently don't do. For now, just return the name
9372 of the method. We don't return NULL, because that may
9373 trigger further attempts to pretty-print the decl in C/C++,
9374 but they wouldn't know how to pretty-print it. */
9375 case INSTANCE_METHOD_DECL
:
9376 case CLASS_METHOD_DECL
:
9377 return IDENTIFIER_POINTER (DECL_NAME (decl
));
9379 /* This happens when printing a deprecation warning for a
9380 property. We may want to consider some sort of pretty
9381 printing (eg, include the class name where it was declared
9384 return IDENTIFIER_POINTER (PROPERTY_NAME (decl
));
9392 /* Return a printable name for 'decl'. This first tries
9393 objc_maybe_printable_name(), and if that fails, it returns the name
9394 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9395 Objective-C; in Objective-C++, setting the hook is not enough
9396 because lots of C++ Front-End code calls cxx_printable_name,
9397 dump_decl and other C++ functions directly. So instead we have
9398 modified dump_decl to call objc_maybe_printable_name directly. */
9400 objc_printable_name (tree decl
, int v
)
9402 const char *demangled_name
= objc_maybe_printable_name (decl
, v
);
9404 if (demangled_name
!= NULL
)
9405 return demangled_name
;
9407 return IDENTIFIER_POINTER (DECL_NAME (decl
));
9410 /* Routine is called to issue diagnostic when reference to a private
9411 ivar is made and no other variable with same name is found in
9414 objc_diagnose_private_ivar (tree id
)
9417 if (!objc_method_context
)
9419 ivar
= is_ivar (objc_ivar_chain
, id
);
9420 if (ivar
&& is_private (ivar
))
9422 error ("instance variable %qs is declared private",
9423 IDENTIFIER_POINTER (id
));
9429 /* Look up ID as an instance variable. OTHER contains the result of
9430 the C or C++ lookup, which we may want to use instead. */
9431 /* To use properties inside an instance method, use self.property. */
9433 objc_lookup_ivar (tree other
, tree id
)
9437 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9438 if (!objc_method_context
)
9441 if (!strcmp (IDENTIFIER_POINTER (id
), "super"))
9442 /* We have a message to super. */
9443 return get_super_receiver ();
9445 /* In a class method, look up an instance variable only as a last
9447 if (TREE_CODE (objc_method_context
) == CLASS_METHOD_DECL
9448 && other
&& other
!= error_mark_node
)
9451 /* Look up the ivar, but do not use it if it is not accessible. */
9452 ivar
= is_ivar (objc_ivar_chain
, id
);
9454 if (!ivar
|| is_private (ivar
))
9457 /* In an instance method, a local variable (or parameter) may hide the
9458 instance variable. */
9459 if (TREE_CODE (objc_method_context
) == INSTANCE_METHOD_DECL
9460 && other
&& other
!= error_mark_node
9462 && CP_DECL_CONTEXT (other
) != global_namespace
)
9464 && !DECL_FILE_SCOPE_P (other
))
9467 warning (0, "local declaration of %qE hides instance variable", id
);
9472 /* At this point, we are either in an instance method with no obscuring
9473 local definitions, or in a class method with no alternate definitions
9475 return build_ivar_reference (id
);
9478 /* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9479 needs to be done if we are calling a function through a cast. */
9482 objc_rewrite_function_call (tree function
, tree first_param
)
9484 if (TREE_CODE (function
) == NOP_EXPR
9485 && TREE_CODE (TREE_OPERAND (function
, 0)) == ADDR_EXPR
9486 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function
, 0), 0))
9489 function
= build3 (OBJ_TYPE_REF
, TREE_TYPE (function
),
9490 TREE_OPERAND (function
, 0),
9491 first_param
, size_zero_node
);
9497 /* This is called to "gimplify" a PROPERTY_REF node. It builds the
9498 corresponding 'getter' function call. Note that we assume the
9499 PROPERTY_REF to be valid since we generated it while parsing. */
9501 objc_gimplify_property_ref (tree
*expr_p
)
9503 tree getter
= PROPERTY_REF_GETTER_CALL (*expr_p
);
9506 if (getter
== NULL_TREE
)
9508 tree property_decl
= PROPERTY_REF_PROPERTY_DECL (*expr_p
);
9509 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9510 should be impossible for real properties, which always
9512 error_at (EXPR_LOCATION (*expr_p
), "no %qs getter found",
9513 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl
)));
9514 /* Try to recover from the error to prevent an ICE. We take
9515 zero and cast it to the type of the property. */
9516 *expr_p
= convert (TREE_TYPE (property_decl
),
9521 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p
))
9523 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9524 that is deprecated. */
9525 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p
),
9531 /* In C++, a getter which returns an aggregate value results in a
9532 target_expr which initializes a temporary to the call
9534 if (TREE_CODE (getter
) == TARGET_EXPR
)
9536 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter
)));
9537 gcc_assert (TREE_CODE (TREE_OPERAND (getter
, 0)) == VAR_DECL
);
9538 call_exp
= TREE_OPERAND (getter
, 1);
9541 gcc_assert (TREE_CODE (call_exp
) == CALL_EXPR
);
9546 /* This is called when "gimplifying" the trees. We need to gimplify
9547 the Objective-C/Objective-C++ specific trees, then hand over the
9548 process to C/C++. */
9550 objc_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
9552 enum tree_code code
= TREE_CODE (*expr_p
);
9555 /* Look for the special case of OBJC_TYPE_REF with the address
9556 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9557 or one of its cousins). */
9559 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p
)) == ADDR_EXPR
9560 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p
), 0))
9563 enum gimplify_status r0
, r1
;
9565 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9566 value of the OBJ_TYPE_REF, so force them to be emitted
9567 during subexpression evaluation rather than after the
9568 OBJ_TYPE_REF. This permits objc_msgSend calls in
9569 Objective C to use direct rather than indirect calls when
9570 the object expression has a postincrement. */
9571 r0
= gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p
), pre_p
, NULL
,
9572 is_gimple_val
, fb_rvalue
);
9573 r1
= gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p
), pre_p
, post_p
,
9574 is_gimple_val
, fb_rvalue
);
9576 return MIN (r0
, r1
);
9580 objc_gimplify_property_ref (expr_p
);
9581 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9588 return (enum gimplify_status
) cp_gimplify_expr (expr_p
, pre_p
, post_p
);
9590 return (enum gimplify_status
) c_gimplify_expr (expr_p
, pre_p
, post_p
);
9594 /* --- FAST ENUMERATION --- */
9595 /* Begin code generation for fast enumeration (foreach) ... */
9599 struct __objcFastEnumerationState
9601 unsigned long state;
9603 unsigned long *mutationsPtr;
9604 unsigned long extra[5];
9607 Confusingly enough, NSFastEnumeration is then defined by libraries
9608 to be the same structure.
9612 build_fast_enumeration_state_template (void)
9614 tree decls
, *chain
= NULL
;
9617 objc_fast_enumeration_state_template
= objc_start_struct (get_identifier
9618 (TAG_FAST_ENUMERATION_STATE
));
9620 /* unsigned long state; */
9621 decls
= add_field_decl (long_unsigned_type_node
, "state", &chain
);
9624 add_field_decl (build_pointer_type (objc_object_type
),
9625 "itemsPtr", &chain
);
9627 /* unsigned long *mutationsPtr; */
9628 add_field_decl (build_pointer_type (long_unsigned_type_node
),
9629 "mutationsPtr", &chain
);
9631 /* unsigned long extra[5]; */
9632 add_field_decl (build_sized_array_type (long_unsigned_type_node
, 5),
9636 objc_finish_struct (objc_fast_enumeration_state_template
, decls
);
9640 'objc_finish_foreach_loop()' generates the code for an Objective-C
9641 foreach loop. The 'location' argument is the location of the 'for'
9642 that starts the loop. The 'object_expression' is the expression of
9643 the 'object' that iterates; the 'collection_expression' is the
9644 expression of the collection that we iterate over (we need to make
9645 sure we evaluate this only once); the 'for_body' is the set of
9646 statements to be executed in each iteration; 'break_label' and
9647 'continue_label' are the break and continue labels which we need to
9648 emit since the <statements> may be jumping to 'break_label' (if they
9649 contain 'break') or to 'continue_label' (if they contain
9654 for (<object expression> in <collection expression>)
9657 which is compiled into the following blurb:
9660 id __objc_foreach_collection;
9661 __objc_fast_enumeration_state __objc_foreach_enum_state;
9662 unsigned long __objc_foreach_batchsize;
9663 id __objc_foreach_items[16];
9664 __objc_foreach_collection = <collection expression>;
9665 __objc_foreach_enum_state = { 0 };
9666 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9668 if (__objc_foreach_batchsize == 0)
9669 <object expression> = nil;
9672 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9675 unsigned long __objc_foreach_index;
9676 __objc_foreach_index = 0;
9679 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9680 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9681 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
9684 __objc_foreach_index++;
9685 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9686 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9688 if (__objc_foreach_batchsize != 0) goto next_batch;
9689 <object expression> = nil;
9694 'statements' may contain a 'continue' or 'break' instruction, which
9695 the user expects to 'continue' or 'break' the entire foreach loop.
9696 We are provided the labels that 'break' and 'continue' jump to, so
9697 we place them where we want them to jump to when they pick them.
9699 Optimization TODO: we could cache the IMP of
9700 countByEnumeratingWithState:objects:count:.
9703 /* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9704 /* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
9706 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9707 #include "tree-pretty-print.h"
9711 objc_finish_foreach_loop (location_t location
, tree object_expression
, tree collection_expression
, tree for_body
,
9712 tree break_label
, tree continue_label
)
9714 /* A tree representing the __objcFastEnumerationState struct type,
9715 or NSFastEnumerationState struct, whatever we are using. */
9716 tree objc_fast_enumeration_state_type
;
9718 /* The trees representing the declarations of each of the local variables. */
9719 tree objc_foreach_collection_decl
;
9720 tree objc_foreach_enum_state_decl
;
9721 tree objc_foreach_items_decl
;
9722 tree objc_foreach_batchsize_decl
;
9723 tree objc_foreach_mutations_pointer_decl
;
9724 tree objc_foreach_index_decl
;
9726 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9729 /* A tree representing the local bind. */
9732 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9735 /* A tree representing the 'else' part of 'first_if' */
9738 /* A tree representing the 'next_batch' label. */
9739 tree next_batch_label_decl
;
9741 /* A tree representing the binding after the 'next_batch' label. */
9742 tree next_batch_bind
;
9744 /* A tree representing the 'next_object' label. */
9745 tree next_object_label_decl
;
9747 /* Temporary variables. */
9751 if (flag_objc1_only
)
9752 error_at (location
, "fast enumeration is not available in Objective-C 1.0");
9754 if (object_expression
== error_mark_node
)
9757 if (collection_expression
== error_mark_node
)
9760 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression
), true))
9762 error_at (location
, "iterating variable in fast enumeration is not an object");
9766 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression
), true))
9768 error_at (location
, "collection in fast enumeration is not an object");
9772 /* TODO: Check that object_expression is either a variable
9773 declaration, or an lvalue. */
9775 /* This kludge is an idea from apple. We use the
9776 __objcFastEnumerationState struct implicitly defined by the
9777 compiler, unless a NSFastEnumerationState struct has been defined
9778 (by a Foundation library such as GNUstep Base) in which case, we
9781 objc_fast_enumeration_state_type
= objc_fast_enumeration_state_template
;
9783 tree objc_NSFastEnumeration_type
= lookup_name (get_identifier ("NSFastEnumerationState"));
9785 if (objc_NSFastEnumeration_type
)
9787 /* TODO: We really need to check that
9788 objc_NSFastEnumeration_type is the same as ours! */
9789 if (TREE_CODE (objc_NSFastEnumeration_type
) == TYPE_DECL
)
9791 /* If it's a typedef, use the original type. */
9792 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type
))
9793 objc_fast_enumeration_state_type
= DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type
);
9795 objc_fast_enumeration_state_type
= TREE_TYPE (objc_NSFastEnumeration_type
);
9801 /* Done by c-parser.c. */
9804 /* Done by c-parser.c. */
9806 /* Disable warnings that 'object' is unused. For example the code
9808 for (id object in collection)
9811 which can be used to count how many objects there are in the
9812 collection is fine and should generate no warnings even if
9813 'object' is technically unused. */
9814 TREE_USED (object_expression
) = 1;
9815 if (DECL_P (object_expression
))
9816 DECL_READ_P (object_expression
) = 1;
9818 /* id __objc_foreach_collection */
9819 objc_foreach_collection_decl
= objc_create_temporary_var (objc_object_type
, "__objc_foreach_collection");
9821 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9822 objc_foreach_enum_state_decl
= objc_create_temporary_var (objc_fast_enumeration_state_type
, "__objc_foreach_enum_state");
9823 TREE_CHAIN (objc_foreach_enum_state_decl
) = objc_foreach_collection_decl
;
9825 /* id __objc_foreach_items[16]; */
9826 objc_foreach_items_decl
= objc_create_temporary_var (build_sized_array_type (objc_object_type
, 16), "__objc_foreach_items");
9827 TREE_CHAIN (objc_foreach_items_decl
) = objc_foreach_enum_state_decl
;
9829 /* unsigned long __objc_foreach_batchsize; */
9830 objc_foreach_batchsize_decl
= objc_create_temporary_var (long_unsigned_type_node
, "__objc_foreach_batchsize");
9831 TREE_CHAIN (objc_foreach_batchsize_decl
) = objc_foreach_items_decl
;
9833 /* Generate the local variable binding. */
9834 bind
= build3 (BIND_EXPR
, void_type_node
, objc_foreach_batchsize_decl
, NULL
, NULL
);
9835 SET_EXPR_LOCATION (bind
, location
);
9836 TREE_SIDE_EFFECTS (bind
) = 1;
9838 /* __objc_foreach_collection = <collection expression>; */
9839 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_foreach_collection_decl
, collection_expression
);
9840 SET_EXPR_LOCATION (t
, location
);
9841 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
9842 /* We have used 'collection_expression'. */
9843 mark_exp_read (collection_expression
);
9845 /* __objc_foreach_enum_state.state = 0; */
9846 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_build_component_ref (objc_foreach_enum_state_decl
,
9847 get_identifier ("state")),
9848 build_int_cst (long_unsigned_type_node
, 0));
9849 SET_EXPR_LOCATION (t
, location
);
9850 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
9852 /* __objc_foreach_enum_state.itemsPtr = NULL; */
9853 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_build_component_ref (objc_foreach_enum_state_decl
,
9854 get_identifier ("itemsPtr")),
9856 SET_EXPR_LOCATION (t
, location
);
9857 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
9859 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
9860 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_build_component_ref (objc_foreach_enum_state_decl
,
9861 get_identifier ("mutationsPtr")),
9863 SET_EXPR_LOCATION (t
, location
);
9864 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
9866 /* __objc_foreach_enum_state.extra[0] = 0; */
9867 /* __objc_foreach_enum_state.extra[1] = 0; */
9868 /* __objc_foreach_enum_state.extra[2] = 0; */
9869 /* __objc_foreach_enum_state.extra[3] = 0; */
9870 /* __objc_foreach_enum_state.extra[4] = 0; */
9871 for (i
= 0; i
< 5 ; i
++)
9873 t
= build2 (MODIFY_EXPR
, void_type_node
,
9874 build_array_ref (location
, objc_build_component_ref (objc_foreach_enum_state_decl
,
9875 get_identifier ("extra")),
9876 build_int_cst (NULL_TREE
, i
)),
9877 build_int_cst (long_unsigned_type_node
, 0));
9878 SET_EXPR_LOCATION (t
, location
);
9879 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
9882 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9883 selector_name
= get_identifier ("countByEnumeratingWithState:objects:count:");
9885 t
= objc_finish_message_expr (objc_foreach_collection_decl
, selector_name
,
9887 tree_cons
/* &__objc_foreach_enum_state */
9888 (NULL_TREE
, build_fold_addr_expr_loc (location
, objc_foreach_enum_state_decl
),
9889 tree_cons
/* __objc_foreach_items */
9890 (NULL_TREE
, objc_foreach_items_decl
,
9892 (NULL_TREE
, build_int_cst (NULL_TREE
, 16), NULL_TREE
))), NULL
);
9894 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9896 struct c_expr array
;
9897 array
.value
= objc_foreach_items_decl
;
9898 t
= objc_finish_message_expr (objc_foreach_collection_decl
, selector_name
,
9900 tree_cons
/* &__objc_foreach_enum_state */
9901 (NULL_TREE
, build_fold_addr_expr_loc (location
, objc_foreach_enum_state_decl
),
9902 tree_cons
/* __objc_foreach_items */
9903 (NULL_TREE
, default_function_array_conversion (location
, array
).value
,
9905 (NULL_TREE
, build_int_cst (NULL_TREE
, 16), NULL_TREE
))), NULL
);
9908 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_foreach_batchsize_decl
,
9909 convert (long_unsigned_type_node
, t
));
9910 SET_EXPR_LOCATION (t
, location
);
9911 append_to_statement_list (t
, &BIND_EXPR_BODY (bind
));
9913 /* if (__objc_foreach_batchsize == 0) */
9914 first_if
= build3 (COND_EXPR
, void_type_node
,
9917 (c_common_truthvalue_conversion
9919 build_binary_op (location
,
9921 objc_foreach_batchsize_decl
,
9922 build_int_cst (long_unsigned_type_node
, 0), 1)),
9924 /* Then block (we fill it in later). */
9926 /* Else block (we fill it in later). */
9928 SET_EXPR_LOCATION (first_if
, location
);
9929 append_to_statement_list (first_if
, &BIND_EXPR_BODY (bind
));
9931 /* then <object expression> = nil; */
9932 t
= build2 (MODIFY_EXPR
, void_type_node
, object_expression
, convert (objc_object_type
, null_pointer_node
));
9933 SET_EXPR_LOCATION (t
, location
);
9934 COND_EXPR_THEN (first_if
) = t
;
9936 /* Now we build the 'else' part of the if; once we finish building
9937 it, we attach it to first_if as the 'else' part. */
9942 /* unsigned long __objc_foreach_mutations_pointer; */
9943 objc_foreach_mutations_pointer_decl
= objc_create_temporary_var (long_unsigned_type_node
, "__objc_foreach_mutations_pointer");
9945 /* Generate the local variable binding. */
9946 first_else
= build3 (BIND_EXPR
, void_type_node
, objc_foreach_mutations_pointer_decl
, NULL
, NULL
);
9947 SET_EXPR_LOCATION (first_else
, location
);
9948 TREE_SIDE_EFFECTS (first_else
) = 1;
9950 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
9951 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_foreach_mutations_pointer_decl
,
9952 build_indirect_ref (location
, objc_build_component_ref (objc_foreach_enum_state_decl
,
9953 get_identifier ("mutationsPtr")),
9955 SET_EXPR_LOCATION (t
, location
);
9956 append_to_statement_list (t
, &BIND_EXPR_BODY (first_else
));
9959 next_batch_label_decl
= create_artificial_label (location
);
9960 t
= build1 (LABEL_EXPR
, void_type_node
, next_batch_label_decl
);
9961 SET_EXPR_LOCATION (t
, location
);
9962 append_to_statement_list (t
, &BIND_EXPR_BODY (first_else
));
9966 /* unsigned long __objc_foreach_index; */
9967 objc_foreach_index_decl
= objc_create_temporary_var (long_unsigned_type_node
, "__objc_foreach_index");
9969 /* Generate the local variable binding. */
9970 next_batch_bind
= build3 (BIND_EXPR
, void_type_node
, objc_foreach_index_decl
, NULL
, NULL
);
9971 SET_EXPR_LOCATION (next_batch_bind
, location
);
9972 TREE_SIDE_EFFECTS (next_batch_bind
) = 1;
9973 append_to_statement_list (next_batch_bind
, &BIND_EXPR_BODY (first_else
));
9975 /* __objc_foreach_index = 0; */
9976 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_foreach_index_decl
,
9977 build_int_cst (long_unsigned_type_node
, 0));
9978 SET_EXPR_LOCATION (t
, location
);
9979 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
9982 next_object_label_decl
= create_artificial_label (location
);
9983 t
= build1 (LABEL_EXPR
, void_type_node
, next_object_label_decl
);
9984 SET_EXPR_LOCATION (t
, location
);
9985 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
9987 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
9988 t
= build3 (COND_EXPR
, void_type_node
,
9991 (c_common_truthvalue_conversion
9996 objc_foreach_mutations_pointer_decl
,
9997 build_indirect_ref (location
,
9998 objc_build_component_ref (objc_foreach_enum_state_decl
,
9999 get_identifier ("mutationsPtr")),
10000 RO_UNARY_STAR
), 1)),
10003 build_function_call (input_location
,
10004 objc_enumeration_mutation_decl
,
10005 tree_cons (NULL
, collection_expression
, NULL
)),
10008 SET_EXPR_LOCATION (t
, location
);
10009 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
10011 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
10012 t
= build2 (MODIFY_EXPR
, void_type_node
, object_expression
,
10013 build_array_ref (location
, objc_build_component_ref (objc_foreach_enum_state_decl
,
10014 get_identifier ("itemsPtr")),
10015 objc_foreach_index_decl
));
10016 SET_EXPR_LOCATION (t
, location
);
10017 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
10019 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
10020 append_to_statement_list (for_body
, &BIND_EXPR_BODY (next_batch_bind
));
10022 /* continue_label: */
10023 if (continue_label
)
10025 t
= build1 (LABEL_EXPR
, void_type_node
, continue_label
);
10026 SET_EXPR_LOCATION (t
, location
);
10027 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
10030 /* __objc_foreach_index++; */
10031 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_foreach_index_decl
,
10032 build_binary_op (location
,
10034 objc_foreach_index_decl
,
10035 build_int_cst (long_unsigned_type_node
, 1), 1));
10036 SET_EXPR_LOCATION (t
, location
);
10037 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
10039 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
10040 t
= build3 (COND_EXPR
, void_type_node
,
10043 (c_common_truthvalue_conversion
10045 build_binary_op (location
,
10047 objc_foreach_index_decl
,
10048 objc_foreach_batchsize_decl
, 1)),
10051 build1 (GOTO_EXPR
, void_type_node
, next_object_label_decl
),
10054 SET_EXPR_LOCATION (t
, location
);
10055 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
10057 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
10059 t
= objc_finish_message_expr (objc_foreach_collection_decl
, selector_name
,
10061 tree_cons
/* &__objc_foreach_enum_state */
10062 (NULL_TREE
, build_fold_addr_expr_loc (location
, objc_foreach_enum_state_decl
),
10063 tree_cons
/* __objc_foreach_items */
10064 (NULL_TREE
, objc_foreach_items_decl
,
10066 (NULL_TREE
, build_int_cst (NULL_TREE
, 16), NULL_TREE
))), NULL
);
10068 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
10070 struct c_expr array
;
10071 array
.value
= objc_foreach_items_decl
;
10072 t
= objc_finish_message_expr (objc_foreach_collection_decl
, selector_name
,
10074 tree_cons
/* &__objc_foreach_enum_state */
10075 (NULL_TREE
, build_fold_addr_expr_loc (location
, objc_foreach_enum_state_decl
),
10076 tree_cons
/* __objc_foreach_items */
10077 (NULL_TREE
, default_function_array_conversion (location
, array
).value
,
10079 (NULL_TREE
, build_int_cst (NULL_TREE
, 16), NULL_TREE
))), NULL
);
10082 t
= build2 (MODIFY_EXPR
, void_type_node
, objc_foreach_batchsize_decl
,
10083 convert (long_unsigned_type_node
, t
));
10084 SET_EXPR_LOCATION (t
, location
);
10085 append_to_statement_list (t
, &BIND_EXPR_BODY (next_batch_bind
));
10089 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
10090 t
= build3 (COND_EXPR
, void_type_node
,
10093 (c_common_truthvalue_conversion
10095 build_binary_op (location
,
10097 objc_foreach_batchsize_decl
,
10098 build_int_cst (long_unsigned_type_node
, 0), 1)),
10101 build1 (GOTO_EXPR
, void_type_node
, next_batch_label_decl
),
10104 SET_EXPR_LOCATION (t
, location
);
10105 append_to_statement_list (t
, &BIND_EXPR_BODY (first_else
));
10107 /* <object expression> = nil; */
10108 t
= build2 (MODIFY_EXPR
, void_type_node
, object_expression
, convert (objc_object_type
, null_pointer_node
));
10109 SET_EXPR_LOCATION (t
, location
);
10110 append_to_statement_list (t
, &BIND_EXPR_BODY (first_else
));
10115 t
= build1 (LABEL_EXPR
, void_type_node
, break_label
);
10116 SET_EXPR_LOCATION (t
, location
);
10117 append_to_statement_list (t
, &BIND_EXPR_BODY (first_else
));
10121 COND_EXPR_ELSE (first_if
) = first_else
;
10123 /* Do the whole thing. */
10126 #ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
10127 /* This will print to stderr the whole blurb generated by the
10128 compiler while compiling (assuming the compiler doesn't crash
10129 before getting here).
10131 debug_generic_stmt (bind
);
10135 /* Done by c-parser.c */
10138 /* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10139 /* Return true if we have an NxString object pointer. */
10142 objc_string_ref_type_p (tree strp
)
10145 if (!strp
|| TREE_CODE (strp
) != POINTER_TYPE
)
10148 tmv
= TYPE_MAIN_VARIANT (TREE_TYPE (strp
));
10149 tmv
= OBJC_TYPE_NAME (tmv
);
10151 && TREE_CODE (tmv
) == IDENTIFIER_NODE
10152 && IDENTIFIER_POINTER (tmv
)
10153 && !strncmp (IDENTIFIER_POINTER (tmv
), "NSString", 8));
10156 /* At present the behavior of this is undefined and it does nothing. */
10158 objc_check_format_arg (tree
ARG_UNUSED (format_arg
),
10159 tree
ARG_UNUSED (args_list
))
10164 objc_common_init_ts (void)
10166 c_common_init_ts ();
10168 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL
);
10169 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL
);
10170 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL
);
10171 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL
);
10173 MARK_TS_COMMON (CLASS_INTERFACE_TYPE
);
10174 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE
);
10175 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE
);
10177 MARK_TS_TYPED (MESSAGE_SEND_EXPR
);
10178 MARK_TS_TYPED (PROPERTY_REF
);
10181 #include "gt-objc-objc-act.h"