]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/objc/objc-act.c
hash-traits.h (ggc_hasher): Rename to...
[thirdparty/gcc.git] / gcc / objc / objc-act.c
CommitLineData
6c65299b 1/* Implement classes and message passing for Objective C.
5624e564 2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4cb8c14b 3 Contributed by Steve Naroff.
6c65299b 4
b9593599 5This file is part of GCC.
6c65299b 6
b9593599 7GCC is free software; you can redistribute it and/or modify
6c65299b 8it under the terms of the GNU General Public License as published by
2b7d992a 9the Free Software Foundation; either version 3, or (at your option)
6c65299b
RS
10any later version.
11
b9593599 12GCC is distributed in the hope that it will be useful,
6c65299b
RS
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
2b7d992a
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
6c65299b 21#include "config.h"
90fbb8c9 22#include "system.h"
4977bab6
ZW
23#include "coretypes.h"
24#include "tm.h"
40e23961
MC
25#include "alias.h"
26#include "symtab.h"
27#include "options.h"
6c65299b 28#include "tree.h"
40e23961 29#include "fold-const.h"
d8a2d370
DN
30#include "stringpool.h"
31#include "stor-layout.h"
32#include "attribs.h"
6408ef33
ZL
33
34#ifdef OBJCPLUS
d4a10d0a 35#include "cp/cp-tree.h"
6408ef33 36#else
d4a10d0a
SB
37#include "c/c-tree.h"
38#include "c/c-lang.h"
6408ef33
ZL
39#endif
40
39dabefd 41#include "c-family/c-common.h"
61d3ce20 42#include "c-family/c-objc.h"
39dabefd 43#include "c-family/c-pragma.h"
91ebb981 44#include "c-family/c-format.h"
6c65299b 45#include "flags.h"
e57e265b 46#include "langhooks.h"
e58232cd 47#include "objc-act.h"
3cc2dd4b 48#include "objc-map.h"
83685514 49#include "hard-reg-set.h"
e31c7eec 50#include "function.h"
d6f4ec51 51#include "toplev.h"
9099cc4f 52#include "debug.h"
acce4e77 53#include "c-family/c-target.h"
1da2ed5f 54#include "diagnostic-core.h"
f41c4af3 55#include "intl.h"
c582198b
AM
56#include "plugin-api.h"
57#include "ipa-ref.h"
0d446150 58#include "cgraph.h"
093c7153 59#include "tree-iterator.h"
3f16185f 60#include "langhooks-def.h"
d764a8e6
IS
61/* Different initialization, code gen and meta data generation for each
62 runtime. */
63#include "objc-runtime-hooks.h"
64/* Routines used mainly by the runtimes. */
65#include "objc-runtime-shared-support.h"
a1178b30
IS
66/* For default_tree_printer (). */
67#include "tree-pretty-print.h"
68
245763e3 69/* For enum gimplify_status */
2fb9a547 70#include "gimple-expr.h"
45b0be94 71#include "gimplify.h"
245763e3 72
f027ee7c
NP
73/* For encode_method_prototype(). */
74#include "objc-encoding.h"
75
38b9c8c3
ZL
76static unsigned int should_call_super_dealloc = 0;
77
6408ef33
ZL
78/* When building Objective-C++, we are not linking against the C front-end
79 and so need to replicate the C tree-construction functions in some way. */
80#ifdef OBJCPLUS
81#define OBJCP_REMAP_FUNCTIONS
82#include "objcp-decl.h"
83#endif /* OBJCPLUS */
cff559bc 84
187e4b0f 85/* This is the default way of generating a method name. */
b8a18805
NP
86/* This has the problem that "test_method:argument:" and
87 "test:method_argument:" will generate the same name
88 ("_i_Test__test_method_argument_" for an instance method of the
89 class "Test"), so you can't have them both in the same class!
90 Moreover, the demangling (going from
91 "_i_Test__test_method_argument" back to the original name) is
92 undefined because there are two correct ways of demangling the
93 name. */
187e4b0f 94#ifndef OBJC_GEN_METHOD_LABEL
b54b3fb0
RS
95#define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
96 do { \
97 char *temp; \
98 sprintf ((BUF), "_%s_%s_%s_%s", \
99 ((IS_INST) ? "i" : "c"), \
100 (CLASS_NAME), \
101 ((CAT_NAME)? (CAT_NAME) : ""), \
102 (SEL_NAME)); \
103 for (temp = (BUF); *temp; temp++) \
104 if (*temp == ':') *temp = '_'; \
187e4b0f 105 } while (0)
152551c6 106#endif
e31c7eec
TW
107
108/* These need specifying. */
109#ifndef OBJC_FORWARDING_STACK_OFFSET
110#define OBJC_FORWARDING_STACK_OFFSET 0
111#endif
112
113#ifndef OBJC_FORWARDING_MIN_OFFSET
114#define OBJC_FORWARDING_MIN_OFFSET 0
115#endif
d764a8e6 116
6c65299b
RS
117/*** Private Interface (procedures) ***/
118
d764a8e6
IS
119/* Init stuff. */
120static void synth_module_prologue (void);
e31c7eec 121
0f41302f 122/* Code generation. */
6c65299b 123
d4e72c58 124static tree start_class (enum tree_code, tree, tree, tree, tree);
6408ef33
ZL
125static tree continue_class (tree);
126static void finish_class (tree);
a04a722b 127static void start_method_def (tree, tree);
d764a8e6 128
6b192a09 129static tree start_protocol (enum tree_code, tree, tree, tree);
dbb74365 130static tree build_method_decl (enum tree_code, tree, tree, tree, bool);
92902b1b 131static tree objc_add_method (tree, tree, int, bool);
c37d8c30 132static tree add_instance_variable (tree, objc_ivar_visibility_kind, tree);
6408ef33
ZL
133static tree build_ivar_reference (tree);
134static tree is_ivar (tree, tree);
6408ef33 135
d11dd684
ZL
136/* We only need the following for ObjC; ObjC++ will use C++'s definition
137 of DERIVED_FROM_P. */
138#ifndef OBJCPLUS
139static bool objc_derived_from_p (tree, tree);
140#define DERIVED_FROM_P(PARENT, CHILD) objc_derived_from_p (PARENT, CHILD)
141#endif
668ea4b1
IS
142
143/* Property. */
668ea4b1
IS
144static void objc_gen_property_data (tree, tree);
145static void objc_synthesize_getter (tree, tree, tree);
668ea4b1 146static void objc_synthesize_setter (tree, tree, tree);
668ea4b1
IS
147static tree lookup_property (tree, tree);
148static tree lookup_property_in_list (tree, tree);
149static tree lookup_property_in_protocol_list (tree, tree);
d764a8e6 150static void build_common_objc_property_accessor_helpers (void);
668ea4b1 151
d11dd684
ZL
152static void objc_xref_basetypes (tree, tree);
153
d11dd684 154static tree get_class_ivars (tree, bool);
011d50d9 155
f05b9d93
NP
156static void build_fast_enumeration_state_template (void);
157
38b9c8c3
ZL
158#ifdef OBJCPLUS
159static void objc_generate_cxx_cdtors (void);
160#endif
011d50d9 161
a1178b30 162/* objc attribute */
944fb799 163static void objc_decl_method_attributes (tree*, tree, int);
a1178b30 164static tree build_keyword_selector (tree);
7cc273ad 165
d764a8e6 166static void hash_init (void);
6c65299b 167
3cc2dd4b
NP
168/* Hash tables to manage the global pool of method prototypes. Each
169 of these maps map a method name (selector) identifier to either a
170 single tree (for methods with a single method prototype) or a
171 TREE_VEC (for methods with multiple method prototypes). */
172static GTY(()) objc_map_t instance_method_map = 0;
173static GTY(()) objc_map_t class_method_map = 0;
e31c7eec 174
660820d1
NP
175/* Hash tables to manage the global pool of class names. */
176
3cc2dd4b
NP
177static GTY(()) objc_map_t class_name_map = 0;
178static GTY(()) objc_map_t alias_name_map = 0;
660820d1 179
011d50d9 180static tree lookup_method (tree, tree);
264fa2db 181static tree lookup_method_static (tree, tree, int);
e31c7eec 182
3cc2dd4b
NP
183static void interface_hash_init (void);
184static tree add_interface (tree, tree);
668ea4b1
IS
185static void add_category (tree, tree);
186static inline tree lookup_category (tree, tree);
187
6b192a09 188/* Protocols. */
e31c7eec 189
db0581ae
NP
190static tree lookup_protocol (tree, bool, bool);
191static tree lookup_and_install_protocols (tree, bool);
6c65299b 192
f8893e47 193#ifdef OBJCPLUS
011d50d9 194static void really_start_method (tree, tree);
f8893e47
JM
195#else
196static void really_start_method (tree, struct c_arg_info *);
197#endif
38b9c8c3 198static int comp_proto_with_proto (tree, tree, int);
26877873 199static tree objc_decay_parm_type (tree);
6c65299b 200
0f41302f 201/* Utilities for debugging and error diagnostics. */
6c65299b 202
6408ef33
ZL
203static char *gen_type_name (tree);
204static char *gen_type_name_0 (tree);
205static char *gen_method_decl (tree);
206static char *gen_declaration (tree);
e31c7eec 207
0f41302f 208/* Everything else. */
e31c7eec 209
d764a8e6
IS
210static void generate_struct_by_value_array (void) ATTRIBUTE_NORETURN;
211
011d50d9 212static void mark_referenced_methods (void);
9a179d01 213static bool objc_type_valid_for_messaging (tree type, bool allow_classes);
3cc2dd4b 214static tree check_duplicates (tree, int, int);
6c65299b
RS
215
216/*** Private Interface (data) ***/
38b9c8c3 217/* Flags for lookup_method_static(). */
a9625a91
NP
218
219/* Look for class methods. */
220#define OBJC_LOOKUP_CLASS 1
221/* Do not examine superclasses. */
222#define OBJC_LOOKUP_NO_SUPER 2
223/* Disable returning an instance method of a root class when a class
224 method can't be found. */
944fb799 225#define OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS 4
38b9c8c3 226
ad074d60 227/* The OCTI_... enumeration itself is in objc/objc-act.h. */
8607f1bc 228tree objc_global_trees[OCTI_MAX];
6c65299b 229
8607f1bc
ZL
230struct imp_entry *imp_list = 0;
231int imp_count = 0; /* `@implementation' */
232int cat_count = 0; /* `@category' */
233
dcaaa5a0 234objc_ivar_visibility_kind objc_ivar_visibility, objc_default_ivar_visibility;
6408ef33 235
264fa2db
ZL
236/* Use to generate method labels. */
237static int method_slot = 0;
6c65299b 238
92902b1b
IS
239/* Flag to say whether methods in a protocol are optional or
240 required. */
241static bool objc_method_optional_flag = false;
242
c0c24aa4
NP
243static int objc_collecting_ivars = 0;
244
ec3e9f82
NP
245/* Flag that is set to 'true' while we are processing a class
246 extension. Since a class extension just "reopens" the main
247 @interface, this can be used to determine if we are in the main
248 @interface, or in a class extension. */
249static bool objc_in_class_extension = false;
250
51900510 251static char *errbuf; /* Buffer for error diagnostics */
6c65299b 252
6c39e757
NP
253/* An array of all the local variables in the current function that
254 need to be marked as volatile. */
9771b263 255vec<tree, va_gc> *local_variables_to_volatilize = NULL;
6c39e757 256
6408ef33
ZL
257/* Store all constructed constant strings in a hash table so that
258 they get uniqued properly. */
259
2a22f99c 260struct GTY((for_user)) string_descriptor {
6408ef33
ZL
261 /* The literal argument . */
262 tree literal;
263
264 /* The resulting constant string. */
265 tree constructor;
266};
267
ca752f39 268struct objc_string_hasher : ggc_ptr_hash<string_descriptor>
2a22f99c
TS
269{
270 static hashval_t hash (string_descriptor *);
271 static bool equal (string_descriptor *, string_descriptor *);
272};
273
274static GTY(()) hash_table<objc_string_hasher> *string_htab;
6408ef33 275
6c65299b
RS
276FILE *gen_declaration_file;
277
d764a8e6
IS
278/* Hooks for stuff that differs between runtimes. */
279objc_runtime_hooks runtime;
3e78185c 280
925e8657
NP
281/* Create a temporary variable of type 'type'. If 'name' is set, uses
282 the specified name, else use no name. Returns the declaration of
283 the type. The 'name' is mostly useful for debugging.
284*/
d764a8e6 285tree
925e8657
NP
286objc_create_temporary_var (tree type, const char *name)
287{
288 tree decl;
289
290 if (name != NULL)
291 {
292 decl = build_decl (input_location,
293 VAR_DECL, get_identifier (name), type);
294 }
295 else
296 {
297 decl = build_decl (input_location,
298 VAR_DECL, NULL_TREE, type);
299 }
300 TREE_USED (decl) = 1;
301 DECL_ARTIFICIAL (decl) = 1;
302 DECL_IGNORED_P (decl) = 1;
303 DECL_CONTEXT (decl) = current_function_decl;
304
305 return decl;
306}
307
ad074d60
SS
308/* Some platforms pass small structures through registers versus
309 through an invisible pointer. Determine at what size structure is
310 the transition point between the two possibilities. */
7cc273ad 311
e36bf33a 312static void
011d50d9 313generate_struct_by_value_array (void)
7cc273ad
L
314{
315 tree type;
3e78185c 316 tree decls;
7cc273ad
L
317 int i, j;
318 int aggregate_in_mem[32];
319 int found = 0;
320
6da97b7b
NP
321 /* Presumably no platform passes 32 byte structures in a register. */
322 /* ??? As an example, m64/ppc/Darwin can pass up to 8*long+13*double
323 in registers. */
7cc273ad
L
324 for (i = 1; i < 32; i++)
325 {
326 char buffer[5];
3e78185c 327 tree *chain = NULL;
7cc273ad
L
328
329 /* Create an unnamed struct that has `i' character components */
24b97832 330 type = objc_start_struct (NULL_TREE);
7cc273ad
L
331
332 strcpy (buffer, "c1");
3e78185c 333 decls = add_field_decl (char_type_node, buffer, &chain);
7cc273ad
L
334
335 for (j = 1; j < i; j++)
336 {
337 sprintf (buffer, "c%d", j + 1);
3e78185c 338 add_field_decl (char_type_node, buffer, &chain);
7cc273ad 339 }
3e78185c 340 objc_finish_struct (type, decls);
011d50d9 341
61f71b34 342 aggregate_in_mem[i] = aggregate_value_p (type, 0);
7cc273ad
L
343 if (!aggregate_in_mem[i])
344 found = 1;
345 }
011d50d9 346
7cc273ad 347 /* We found some structures that are returned in registers instead of memory
ad074d60 348 so output the necessary data. */
7cc273ad
L
349 if (found)
350 {
351 for (i = 31; i >= 0; i--)
352 if (!aggregate_in_mem[i])
353 break;
2d66a8d3 354 printf ("#define OBJC_MAX_STRUCT_BY_VALUE %d\n", i);
7cc273ad 355 }
011d50d9 356
7cc273ad
L
357 exit (0);
358}
359
4bfec483 360bool
011d50d9 361objc_init (void)
6c65299b 362{
d764a8e6 363 bool ok;
6408ef33
ZL
364#ifdef OBJCPLUS
365 if (cxx_init () == false)
366#else
4bfec483 367 if (c_objc_common_init () == false)
6408ef33 368#endif
4bfec483 369 return false;
f5e99456 370
6da97b7b
NP
371 /* print_struct_values is triggered by -print-runtime-info (used
372 when building libobjc, with an empty file as input). It does not
373 require any ObjC setup, and it never returns.
374
375 -fcompare-debug is used to check the compiler output; we are
376 executed twice, once with flag_compare_debug set, and once with
377 it not set. If the flag is used together with
378 -print-runtime-info, we want to print the runtime info only once,
379 else it would be output in duplicate. So we check
380 flag_compare_debug to output it in only one of the invocations.
381
382 As a side effect, this also that means -fcompare-debug
383 -print-runtime-info will run the compiler twice, and compare the
384 generated assembler file; the first time the compiler exits
385 immediately (producing no file), and the second time it compiles
386 an empty file. This checks, as a side effect, that compiling an
387 empty file produces no assembler output. */
d764a8e6 388 if (print_struct_values && !flag_compare_debug)
d764a8e6 389 generate_struct_by_value_array ();
6c65299b 390
d764a8e6
IS
391 /* Set up stuff used by FE parser and all runtimes. */
392 errbuf = XNEWVEC (char, 1024 * 10);
3cc2dd4b 393 interface_hash_init ();
d764a8e6 394 hash_init ();
a8f18c40 395 objc_encoding_init ();
d764a8e6
IS
396 /* ... and then check flags and set-up for the selected runtime ... */
397 if (flag_next_runtime && flag_objc_abi >= 2)
398 ok = objc_next_runtime_abi_02_init (&runtime);
399 else if (flag_next_runtime)
400 ok = objc_next_runtime_abi_01_init (&runtime);
401 else
402 ok = objc_gnu_runtime_abi_01_init (&runtime);
7cc273ad 403
d764a8e6
IS
404 /* If that part of the setup failed - bail out immediately. */
405 if (!ok)
406 return false;
bcdb1106 407
dcaaa5a0
DP
408 /* Determine the default visibility for instance variables. */
409 switch (default_ivar_visibility)
410 {
411 case IVAR_VISIBILITY_PRIVATE:
412 objc_default_ivar_visibility = OBJC_IVAR_VIS_PRIVATE;
413 break;
414 case IVAR_VISIBILITY_PUBLIC:
415 objc_default_ivar_visibility = OBJC_IVAR_VIS_PUBLIC;
416 break;
417 case IVAR_VISIBILITY_PACKAGE:
418 objc_default_ivar_visibility = OBJC_IVAR_VIS_PACKAGE;
419 break;
420 default:
421 objc_default_ivar_visibility = OBJC_IVAR_VIS_PROTECTED;
422 }
423
d764a8e6
IS
424 /* Generate general types and push runtime-specific decls to file scope. */
425 synth_module_prologue ();
46270f14 426
4bfec483 427 return true;
8b0e9a72
AO
428}
429
d7438551 430/* This is called at the end of parsing by the C/C++ parsers. */
6c65299b 431void
977e30bc 432objc_write_global_declarations (void)
6c65299b 433{
0d446150 434 mark_referenced_methods ();
8b0e9a72 435
d764a8e6
IS
436 /* A missing @end might not be detected by the parser. */
437 if (objc_implementation_context)
438 {
439 warning (0, "%<@end%> missing in implementation context");
440 finish_class (objc_implementation_context);
441 objc_ivar_chain = NULL_TREE;
442 objc_implementation_context = NULL_TREE;
443 }
444
445 if (warn_selector)
446 {
3cc2dd4b 447 objc_map_iterator_t i;
d764a8e6 448
3cc2dd4b
NP
449 objc_map_iterator_initialize (class_method_map, &i);
450 while (objc_map_iterator_move_to_next (class_method_map, &i))
451 check_duplicates (objc_map_iterator_current_value (class_method_map, i), 0, 1);
d764a8e6 452
3cc2dd4b
NP
453 objc_map_iterator_initialize (instance_method_map, &i);
454 while (objc_map_iterator_move_to_next (instance_method_map, &i))
455 check_duplicates (objc_map_iterator_current_value (instance_method_map, i), 0, 0);
d764a8e6
IS
456 }
457
458 /* TODO: consider an early exit here if either errorcount or sorrycount
459 is non-zero. Not only is it wasting time to generate the metadata,
944fb799 460 it needlessly imposes need to re-check for things that are already
d764a8e6 461 determined to be errors. */
6c65299b 462
d764a8e6
IS
463 /* Finalize Objective-C runtime data. No need to generate tables
464 and code if only checking syntax, or if generating a PCH file. */
465 if (!flag_syntax_only && !pch_file)
466 {
6d549148
NP
467 location_t saved_location;
468
d764a8e6
IS
469 /* If gen_declaration desired, open the output file. */
470 if (flag_gen_declaration)
471 {
472 char * const dumpname = concat (dump_base_name, ".decl", NULL);
473 gen_declaration_file = fopen (dumpname, "w");
474 if (gen_declaration_file == 0)
40fecdd6 475 fatal_error (input_location, "can%'t open %s: %m", dumpname);
d764a8e6
IS
476 free (dumpname);
477 }
6d549148
NP
478
479 /* Set the input location to BUILTINS_LOCATION. This is good
480 for error messages, in case any is generated while producing
481 the metadata, but it also silences warnings that would be
482 produced when compiling with -Wpadded in case when padding is
483 automatically added to the built-in runtime data structure
484 declarations. We know about this padding, and it is fine; we
485 don't want users to see any warnings about it if they use
486 -Wpadded. */
487 saved_location = input_location;
488 input_location = BUILTINS_LOCATION;
489
d764a8e6
IS
490 /* Compute and emit the meta-data tables for this runtime. */
491 (*runtime.generate_metadata) ();
6d549148
NP
492
493 /* Restore the original location, just in case it mattered. */
494 input_location = saved_location;
495
d764a8e6
IS
496 /* ... and then close any declaration file we opened. */
497 if (gen_declaration_file)
498 fclose (gen_declaration_file);
499 }
6c65299b 500}
d764a8e6 501
5c234cd7
DA
502/* Return the first occurrence of a method declaration corresponding
503 to sel_name in rproto_list. Search rproto_list recursively.
504 If is_class is 0, search for instance methods, otherwise for class
505 methods. */
e31c7eec 506static tree
011d50d9 507lookup_method_in_protocol_list (tree rproto_list, tree sel_name,
5c234cd7 508 int is_class)
e31c7eec 509{
354d8ce1 510 tree rproto, p, m;
e31c7eec
TW
511
512 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
513 {
354d8ce1
NP
514 p = TREE_VALUE (rproto);
515 m = NULL_TREE;
e31c7eec
TW
516
517 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
518 {
354d8ce1
NP
519 /* First, search the @required protocol methods. */
520 if (is_class)
521 m = lookup_method (PROTOCOL_CLS_METHODS (p), sel_name);
522 else
523 m = lookup_method (PROTOCOL_NST_METHODS (p), sel_name);
524
525 if (m)
526 return m;
527
528 /* If still not found, search the @optional protocol methods. */
529 if (is_class)
530 m = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (p), sel_name);
531 else
532 m = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (p), sel_name);
533
534 if (m)
535 return m;
536
537 /* If still not found, search the attached protocols. */
538 if (PROTOCOL_LIST (p))
539 m = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
540 sel_name, is_class);
541 if (m)
542 return m;
e31c7eec
TW
543 }
544 else
51723711
KG
545 {
546 ; /* An identifier...if we could not find a protocol. */
547 }
e31c7eec 548 }
51900510 549
e31c7eec
TW
550 return 0;
551}
552
553static tree
011d50d9 554lookup_protocol_in_reflist (tree rproto_list, tree lproto)
e31c7eec 555{
ad074d60 556 tree rproto, p;
e31c7eec 557
ad074d60
SS
558 /* Make sure the protocol is supported by the object on the rhs. */
559 if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
560 {
561 tree fnd = 0;
562 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
563 {
564 p = TREE_VALUE (rproto);
e31c7eec 565
ad074d60
SS
566 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
567 {
568 if (lproto == p)
569 fnd = lproto;
e31c7eec 570
ad074d60
SS
571 else if (PROTOCOL_LIST (p))
572 fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
573 }
e31c7eec 574
ad074d60
SS
575 if (fnd)
576 return fnd;
577 }
578 }
579 else
580 {
581 ; /* An identifier...if we could not find a protocol. */
582 }
e31c7eec 583
ad074d60 584 return 0;
6c65299b
RS
585}
586
6408ef33 587void
c165dca7
IS
588objc_start_class_interface (tree klass, tree super_class,
589 tree protos, tree attributes)
6408ef33 590{
d4e72c58 591 if (flag_objc1_only && attributes)
944fb799 592 error_at (input_location, "class attributes are not available in Objective-C 1.0");
d4e72c58 593
6408ef33
ZL
594 objc_interface_context
595 = objc_ivar_context
d4e72c58 596 = start_class (CLASS_INTERFACE_TYPE, klass, super_class, protos, attributes);
dcaaa5a0 597 objc_ivar_visibility = objc_default_ivar_visibility;
6408ef33
ZL
598}
599
600void
c165dca7
IS
601objc_start_category_interface (tree klass, tree categ,
602 tree protos, tree attributes)
6408ef33 603{
c165dca7 604 if (attributes)
22d8d616
NP
605 {
606 if (flag_objc1_only)
607 error_at (input_location, "category attributes are not available in Objective-C 1.0");
608 else
944fb799 609 warning_at (input_location, OPT_Wattributes,
22d8d616
NP
610 "category attributes are not available in this version"
611 " of the compiler, (ignored)");
612 }
ec3e9f82
NP
613 if (categ == NULL_TREE)
614 {
615 if (flag_objc1_only)
616 error_at (input_location, "class extensions are not available in Objective-C 1.0");
35bce82a
NP
617 else
618 {
619 /* Iterate over all the classes and categories implemented
620 up to now in this compilation unit. */
621 struct imp_entry *t;
622
623 for (t = imp_list; t; t = t->next)
624 {
625 /* If we find a class @implementation with the same name
626 as the one we are extending, produce an error. */
627 if (TREE_CODE (t->imp_context) == CLASS_IMPLEMENTATION_TYPE
628 && IDENTIFIER_POINTER (CLASS_NAME (t->imp_context)) == IDENTIFIER_POINTER (klass))
944fb799 629 error_at (input_location,
35bce82a
NP
630 "class extension for class %qE declared after its %<@implementation%>",
631 klass);
632 }
633 }
ec3e9f82 634 }
6408ef33 635 objc_interface_context
d4e72c58 636 = start_class (CATEGORY_INTERFACE_TYPE, klass, categ, protos, NULL_TREE);
6408ef33
ZL
637 objc_ivar_chain
638 = continue_class (objc_interface_context);
639}
640
641void
c165dca7 642objc_start_protocol (tree name, tree protos, tree attributes)
6408ef33 643{
6b192a09 644 if (flag_objc1_only && attributes)
944fb799 645 error_at (input_location, "protocol attributes are not available in Objective-C 1.0");
6b192a09 646
6408ef33 647 objc_interface_context
6b192a09 648 = start_protocol (PROTOCOL_INTERFACE_TYPE, name, protos, attributes);
92902b1b 649 objc_method_optional_flag = false;
6408ef33
ZL
650}
651
652void
653objc_continue_interface (void)
654{
655 objc_ivar_chain
656 = continue_class (objc_interface_context);
657}
658
659void
660objc_finish_interface (void)
661{
662 finish_class (objc_interface_context);
663 objc_interface_context = NULL_TREE;
92902b1b 664 objc_method_optional_flag = false;
ec3e9f82 665 objc_in_class_extension = false;
6408ef33
ZL
666}
667
668void
5025f379 669objc_start_class_implementation (tree klass, tree super_class)
6408ef33
ZL
670{
671 objc_implementation_context
672 = objc_ivar_context
d4e72c58
NP
673 = start_class (CLASS_IMPLEMENTATION_TYPE, klass, super_class, NULL_TREE,
674 NULL_TREE);
dcaaa5a0 675 objc_ivar_visibility = objc_default_ivar_visibility;
6408ef33
ZL
676}
677
678void
5025f379 679objc_start_category_implementation (tree klass, tree categ)
6408ef33
ZL
680{
681 objc_implementation_context
d4e72c58
NP
682 = start_class (CATEGORY_IMPLEMENTATION_TYPE, klass, categ, NULL_TREE,
683 NULL_TREE);
6408ef33
ZL
684 objc_ivar_chain
685 = continue_class (objc_implementation_context);
686}
687
688void
689objc_continue_implementation (void)
690{
691 objc_ivar_chain
692 = continue_class (objc_implementation_context);
693}
694
695void
696objc_finish_implementation (void)
697{
38b9c8c3
ZL
698#ifdef OBJCPLUS
699 if (flag_objc_call_cxx_cdtors)
700 objc_generate_cxx_cdtors ();
701#endif
702
6408ef33
ZL
703 if (objc_implementation_context)
704 {
705 finish_class (objc_implementation_context);
706 objc_ivar_chain = NULL_TREE;
707 objc_implementation_context = NULL_TREE;
708 }
709 else
d4ee4d25 710 warning (0, "%<@end%> must appear in an @implementation context");
6408ef33
ZL
711}
712
713void
c37d8c30 714objc_set_visibility (objc_ivar_visibility_kind visibility)
6408ef33 715{
c37d8c30 716 if (visibility == OBJC_IVAR_VIS_PACKAGE)
22d8d616
NP
717 {
718 if (flag_objc1_only)
719 error ("%<@package%> is not available in Objective-C 1.0");
720 else
721 warning (0, "%<@package%> presently has the same effect as %<@public%>");
722 }
c37d8c30 723 objc_ivar_visibility = visibility;
6408ef33
ZL
724}
725
92902b1b
IS
726void
727objc_set_method_opt (bool optional)
728{
22d8d616 729 if (flag_objc1_only)
445eb4c6
NP
730 {
731 if (optional)
944fb799 732 error_at (input_location, "%<@optional%> is not available in Objective-C 1.0");
445eb4c6 733 else
944fb799 734 error_at (input_location, "%<@required%> is not available in Objective-C 1.0");
445eb4c6 735 }
22d8d616 736
92902b1b 737 objc_method_optional_flag = optional;
944fb799 738 if (!objc_interface_context
92902b1b
IS
739 || TREE_CODE (objc_interface_context) != PROTOCOL_INTERFACE_TYPE)
740 {
445eb4c6
NP
741 if (optional)
742 error ("%<@optional%> is allowed in @protocol context only");
743 else
744 error ("%<@required%> is allowed in @protocol context only");
92902b1b
IS
745 objc_method_optional_flag = false;
746 }
747}
748
4ca5d2a7
NP
749/* This routine looks for a given PROPERTY in a list of CLASS, CATEGORY, or
750 PROTOCOL. */
751static tree
752lookup_property_in_list (tree chain, tree property)
753{
754 tree x;
755 for (x = CLASS_PROPERTY_DECL (chain); x; x = TREE_CHAIN (x))
756 if (PROPERTY_NAME (x) == property)
757 return x;
758 return NULL_TREE;
759}
760
761/* This routine looks for a given PROPERTY in the tree chain of RPROTO_LIST. */
762static tree lookup_property_in_protocol_list (tree rproto_list, tree property)
763{
764 tree rproto, x;
765 for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
766 {
767 tree p = TREE_VALUE (rproto);
768 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
769 {
770 if ((x = lookup_property_in_list (p, property)))
771 return x;
772 if (PROTOCOL_LIST (p))
773 return lookup_property_in_protocol_list (PROTOCOL_LIST (p), property);
774 }
775 else
776 {
777 ; /* An identifier...if we could not find a protocol. */
778 }
779 }
780 return NULL_TREE;
781}
782
783/* This routine looks up the PROPERTY in current INTERFACE, its categories and up the
784 chain of interface hierarchy. */
785static tree
786lookup_property (tree interface_type, tree property)
787{
788 tree inter = interface_type;
789 while (inter)
790 {
791 tree x, category;
792 if ((x = lookup_property_in_list (inter, property)))
793 return x;
794 /* Failing that, look for the property in each category of the class. */
795 category = inter;
796 while ((category = CLASS_CATEGORY_LIST (category)))
797 {
798 if ((x = lookup_property_in_list (category, property)))
799 return x;
800
801 /* When checking a category, also check the protocols
802 attached with the category itself. */
803 if (CLASS_PROTOCOL_LIST (category)
804 && (x = lookup_property_in_protocol_list
805 (CLASS_PROTOCOL_LIST (category), property)))
806 return x;
807 }
808
809 /* Failing to find in categories, look for property in protocol list. */
944fb799 810 if (CLASS_PROTOCOL_LIST (inter)
4ca5d2a7
NP
811 && (x = lookup_property_in_protocol_list
812 (CLASS_PROTOCOL_LIST (inter), property)))
813 return x;
944fb799 814
4ca5d2a7
NP
815 /* Failing that, climb up the inheritance hierarchy. */
816 inter = lookup_interface (CLASS_SUPER_NAME (inter));
817 }
818 return inter;
819}
820
200290f2
NP
821/* This routine is called by the parser when a
822 @property... declaration is found. 'decl' is the declaration of
823 the property (type/identifier), and the other arguments represent
824 property attributes that may have been specified in the Objective-C
825 declaration. 'parsed_property_readonly' is 'true' if the attribute
826 'readonly' was specified, and 'false' if not; similarly for the
827 other bool parameters. 'parsed_property_getter_ident' is NULL_TREE
828 if the attribute 'getter' was not specified, and is the identifier
829 corresponding to the specified getter if it was; similarly for
830 'parsed_property_setter_ident'. */
668ea4b1 831void
200290f2
NP
832objc_add_property_declaration (location_t location, tree decl,
833 bool parsed_property_readonly, bool parsed_property_readwrite,
834 bool parsed_property_assign, bool parsed_property_retain,
835 bool parsed_property_copy, bool parsed_property_nonatomic,
46a88c12 836 tree parsed_property_getter_ident, tree parsed_property_setter_ident)
668ea4b1 837{
200290f2
NP
838 tree property_decl;
839 tree x;
46a88c12
NP
840 /* 'property_readonly' and 'property_assign_semantics' are the final
841 attributes of the property after all parsed attributes have been
842 considered (eg, if we parsed no 'readonly' and no 'readwrite', ie
843 parsed_property_readonly = false and parsed_property_readwrite =
844 false, then property_readonly will be false because the default
845 is readwrite). */
200290f2 846 bool property_readonly = false;
46a88c12 847 objc_property_assign_semantics property_assign_semantics = OBJC_PROPERTY_ASSIGN;
ec3e9f82 848 bool property_extension_in_class_extension = false;
200290f2 849
22d8d616
NP
850 if (flag_objc1_only)
851 error_at (input_location, "%<@property%> is not available in Objective-C 1.0");
852
200290f2 853 if (parsed_property_readonly && parsed_property_readwrite)
668ea4b1 854 {
200290f2
NP
855 error_at (location, "%<readonly%> attribute conflicts with %<readwrite%> attribute");
856 /* In case of conflicting attributes (here and below), after
857 producing an error, we pick one of the attributes and keep
858 going. */
859 property_readonly = false;
860 }
861 else
862 {
863 if (parsed_property_readonly)
668ea4b1 864 property_readonly = true;
944fb799 865
200290f2
NP
866 if (parsed_property_readwrite)
867 property_readonly = false;
668ea4b1 868 }
668ea4b1 869
200290f2
NP
870 if (parsed_property_readonly && parsed_property_setter_ident)
871 {
8926bd5d 872 error_at (location, "%<readonly%> attribute conflicts with %<setter%> attribute");
46a88c12 873 property_readonly = false;
200290f2 874 }
668ea4b1 875
200290f2
NP
876 if (parsed_property_assign && parsed_property_retain)
877 {
878 error_at (location, "%<assign%> attribute conflicts with %<retain%> attribute");
879 property_assign_semantics = OBJC_PROPERTY_RETAIN;
880 }
881 else if (parsed_property_assign && parsed_property_copy)
882 {
883 error_at (location, "%<assign%> attribute conflicts with %<copy%> attribute");
884 property_assign_semantics = OBJC_PROPERTY_COPY;
885 }
886 else if (parsed_property_retain && parsed_property_copy)
887 {
888 error_at (location, "%<retain%> attribute conflicts with %<copy%> attribute");
889 property_assign_semantics = OBJC_PROPERTY_COPY;
890 }
891 else
892 {
893 if (parsed_property_assign)
894 property_assign_semantics = OBJC_PROPERTY_ASSIGN;
668ea4b1 895
200290f2
NP
896 if (parsed_property_retain)
897 property_assign_semantics = OBJC_PROPERTY_RETAIN;
898
899 if (parsed_property_copy)
900 property_assign_semantics = OBJC_PROPERTY_COPY;
901 }
902
46a88c12 903 if (!objc_interface_context)
668ea4b1 904 {
46a88c12 905 error_at (location, "property declaration not in @interface or @protocol context");
668ea4b1
IS
906 return;
907 }
908
46a88c12
NP
909 /* At this point we know that we are either in an interface, a
910 category, or a protocol. */
911
8926bd5d
NP
912 /* We expect a FIELD_DECL from the parser. Make sure we didn't get
913 something else, as that would confuse the checks below. */
914 if (TREE_CODE (decl) != FIELD_DECL)
915 {
916 error_at (location, "invalid property declaration");
944fb799 917 return;
8926bd5d
NP
918 }
919
920 /* Do some spot-checks for the most obvious invalid types. */
921
922 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
46a88c12 923 {
8926bd5d 924 error_at (location, "property can not be an array");
46a88c12
NP
925 return;
926 }
668ea4b1 927
8926bd5d
NP
928 /* The C++/ObjC++ parser seems to reject the ':' for a bitfield when
929 parsing, while the C/ObjC parser accepts it and gives us a
930 FIELD_DECL with a DECL_INITIAL set. So we use the DECL_INITIAL
931 to check for a bitfield when doing ObjC. */
932#ifndef OBJCPLUS
933 if (DECL_INITIAL (decl))
934 {
935 /* A @property is not an actual variable, but it is a way to
936 describe a pair of accessor methods, so its type (which is
937 the type of the return value of the getter and the first
938 argument of the setter) can't be a bitfield (as return values
939 and arguments of functions can not be bitfields). The
940 underlying instance variable could be a bitfield, but that is
941 a different matter. */
942 error_at (location, "property can not be a bit-field");
944fb799 943 return;
8926bd5d
NP
944 }
945#endif
946
947 /* TODO: Check that the property type is an Objective-C object or a
948 "POD". */
46a88c12 949
9a179d01
NP
950 /* Implement -Wproperty-assign-default (which is enabled by default). */
951 if (warn_property_assign_default
46a88c12
NP
952 /* If garbage collection is not being used, then 'assign' is
953 valid for objects (and typically used for delegates) but it
954 is wrong in most cases (since most objects need to be
955 retained or copied in setters). Warn users when 'assign' is
956 used implicitly. */
9a179d01
NP
957 && property_assign_semantics == OBJC_PROPERTY_ASSIGN
958 /* Read-only properties are never assigned, so the assignment
959 semantics do not matter in that case. */
960 && !property_readonly
961 && !flag_objc_gc)
962 {
46a88c12
NP
963 /* Please note that it would make sense to default to 'assign'
964 for non-{Objective-C objects}, and to 'retain' for
965 Objective-C objects. But that would break compatibility with
966 other compilers. */
9a179d01 967 if (!parsed_property_assign && !parsed_property_retain && !parsed_property_copy)
668ea4b1 968 {
9a179d01
NP
969 /* Use 'false' so we do not warn for Class objects. */
970 if (objc_type_valid_for_messaging (TREE_TYPE (decl), false))
668ea4b1 971 {
944fb799 972 warning_at (location,
9a179d01 973 0,
944fb799 974 "object property %qD has no %<assign%>, %<retain%> or %<copy%> attribute; assuming %<assign%>",
9a179d01 975 decl);
944fb799 976 inform (location,
9a179d01 977 "%<assign%> can be unsafe for Objective-C objects; please state explicitly if you need it");
668ea4b1
IS
978 }
979 }
668ea4b1 980 }
944fb799 981
46a88c12 982 if (property_assign_semantics == OBJC_PROPERTY_RETAIN
9a179d01 983 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
46a88c12 984 error_at (location, "%<retain%> attribute is only valid for Objective-C objects");
944fb799 985
46a88c12 986 if (property_assign_semantics == OBJC_PROPERTY_COPY
9a179d01 987 && !objc_type_valid_for_messaging (TREE_TYPE (decl), true))
46a88c12 988 error_at (location, "%<copy%> attribute is only valid for Objective-C objects");
200290f2 989
4ca5d2a7
NP
990 /* Now determine the final property getter and setter names. They
991 will be stored in the PROPERTY_DECL, from which they'll always be
992 extracted and used. */
993
994 /* Adjust, or fill in, setter and getter names. We overwrite the
995 parsed_property_setter_ident and parsed_property_getter_ident
996 with the final setter and getter identifiers that will be
997 used. */
998 if (parsed_property_setter_ident)
999 {
1000 /* The setter should be terminated by ':', but the parser only
1001 gives us an identifier without ':'. So, we need to add ':'
1002 at the end. */
1003 const char *parsed_setter = IDENTIFIER_POINTER (parsed_property_setter_ident);
1004 size_t length = strlen (parsed_setter);
1005 char *final_setter = (char *)alloca (length + 2);
1006
1007 sprintf (final_setter, "%s:", parsed_setter);
1008 parsed_property_setter_ident = get_identifier (final_setter);
1009 }
1010 else
1011 {
1012 if (!property_readonly)
944fb799 1013 parsed_property_setter_ident = get_identifier (objc_build_property_setter_name
4ca5d2a7
NP
1014 (DECL_NAME (decl)));
1015 }
1016
1017 if (!parsed_property_getter_ident)
1018 parsed_property_getter_ident = DECL_NAME (decl);
1019
46a88c12 1020 /* Check for duplicate property declarations. We first check the
4ca5d2a7 1021 immediate context for a property with the same name. Any such
ec3e9f82
NP
1022 declarations are an error, unless this is a class extension and
1023 we are extending a property from readonly to readwrite. */
46a88c12
NP
1024 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
1025 {
1026 if (PROPERTY_NAME (x) == DECL_NAME (decl))
668ea4b1 1027 {
ec3e9f82
NP
1028 if (objc_in_class_extension
1029 && property_readonly == 0
1030 && PROPERTY_READONLY (x) == 1)
1031 {
1032 /* This is a class extension, and we are extending an
1033 existing readonly property to a readwrite one.
1034 That's fine. :-) */
1035 property_extension_in_class_extension = true;
1036 break;
1037 }
1038 else
1039 {
1040 location_t original_location = DECL_SOURCE_LOCATION (x);
944fb799 1041
ec3e9f82 1042 error_at (location, "redeclaration of property %qD", decl);
944fb799 1043
ec3e9f82
NP
1044 if (original_location != UNKNOWN_LOCATION)
1045 inform (original_location, "originally specified here");
1046 return;
1047 }
1048 }
668ea4b1 1049 }
46a88c12 1050
ec3e9f82
NP
1051 /* If x is not NULL_TREE, we must be in a class extension and we're
1052 extending a readonly property. In that case, no point in
1053 searching for another declaration. */
1054 if (x == NULL_TREE)
1055 {
1056 /* We now need to check for existing property declarations (in
1057 the superclass, other categories or protocols) and check that
1058 the new declaration is not in conflict with existing
1059 ones. */
4ca5d2a7 1060
ec3e9f82
NP
1061 /* Search for a previous, existing declaration of a property
1062 with the same name in superclasses, protocols etc. If one is
1063 found, it will be in the 'x' variable. */
4ca5d2a7 1064
ec3e9f82
NP
1065 /* Note that, for simplicity, the following may search again the
1066 local context. That's Ok as nothing will be found (else we'd
1067 have thrown an error above); it's only a little inefficient,
1068 but the code is simpler. */
1069 switch (TREE_CODE (objc_interface_context))
4ca5d2a7 1070 {
ec3e9f82
NP
1071 case CLASS_INTERFACE_TYPE:
1072 /* Look up the property in the current @interface (which
1073 will find nothing), then its protocols and categories and
1074 superclasses. */
1075 x = lookup_property (objc_interface_context, DECL_NAME (decl));
1076 break;
1077 case CATEGORY_INTERFACE_TYPE:
1078 /* Look up the property in the main @interface, then
1079 protocols and categories (one of them is ours, and will
1080 find nothing) and superclasses. */
1081 x = lookup_property (lookup_interface (CLASS_NAME (objc_interface_context)),
1082 DECL_NAME (decl));
1083 break;
1084 case PROTOCOL_INTERFACE_TYPE:
1085 /* Looks up the property in any protocols attached to the
1086 current protocol. */
1087 if (PROTOCOL_LIST (objc_interface_context))
1088 {
1089 x = lookup_property_in_protocol_list (PROTOCOL_LIST (objc_interface_context),
1090 DECL_NAME (decl));
1091 }
1092 break;
1093 default:
1094 gcc_unreachable ();
4ca5d2a7 1095 }
4ca5d2a7
NP
1096 }
1097
1098 if (x != NULL_TREE)
1099 {
1100 /* An existing property was found; check that it has the same
1101 types, or it is compatible. */
1102 location_t original_location = DECL_SOURCE_LOCATION (x);
2dd24dbd 1103
4ca5d2a7
NP
1104 if (PROPERTY_NONATOMIC (x) != parsed_property_nonatomic)
1105 {
8926bd5d
NP
1106 warning_at (location, 0,
1107 "'nonatomic' attribute of property %qD conflicts with previous declaration", decl);
944fb799 1108
4ca5d2a7
NP
1109 if (original_location != UNKNOWN_LOCATION)
1110 inform (original_location, "originally specified here");
1111 return;
1112 }
1113
1114 if (PROPERTY_GETTER_NAME (x) != parsed_property_getter_ident)
1115 {
8926bd5d
NP
1116 warning_at (location, 0,
1117 "'getter' attribute of property %qD conflicts with previous declaration", decl);
944fb799 1118
4ca5d2a7
NP
1119 if (original_location != UNKNOWN_LOCATION)
1120 inform (original_location, "originally specified here");
1121 return;
1122 }
1123
1124 /* We can only compare the setter names if both the old and new property have a setter. */
1125 if (!property_readonly && !PROPERTY_READONLY(x))
1126 {
1127 if (PROPERTY_SETTER_NAME (x) != parsed_property_setter_ident)
1128 {
8926bd5d
NP
1129 warning_at (location, 0,
1130 "'setter' attribute of property %qD conflicts with previous declaration", decl);
944fb799 1131
4ca5d2a7
NP
1132 if (original_location != UNKNOWN_LOCATION)
1133 inform (original_location, "originally specified here");
1134 return;
1135 }
1136 }
1137
1138 if (PROPERTY_ASSIGN_SEMANTICS (x) != property_assign_semantics)
1139 {
8926bd5d
NP
1140 warning_at (location, 0,
1141 "assign semantics attributes of property %qD conflict with previous declaration", decl);
944fb799 1142
4ca5d2a7
NP
1143 if (original_location != UNKNOWN_LOCATION)
1144 inform (original_location, "originally specified here");
1145 return;
1146 }
1147
1148 /* It's ok to have a readonly property that becomes a readwrite, but not vice versa. */
1149 if (PROPERTY_READONLY (x) == 0 && property_readonly == 1)
1150 {
8926bd5d
NP
1151 warning_at (location, 0,
1152 "'readonly' attribute of property %qD conflicts with previous declaration", decl);
944fb799 1153
4ca5d2a7
NP
1154 if (original_location != UNKNOWN_LOCATION)
1155 inform (original_location, "originally specified here");
1156 return;
1157 }
1158
10e34e6e
NP
1159 /* We now check that the new and old property declarations have
1160 the same types (or compatible one). In the Objective-C
1161 tradition of loose type checking, we do type-checking but
1162 only generate warnings (not errors) if they do not match.
1163 For non-readonly properties, the types must match exactly;
1164 for readonly properties, it is allowed to use a "more
1165 specialized" type in the new property declaration. Eg, the
1166 superclass has a getter returning (NSArray *) and the
1167 subclass a getter returning (NSMutableArray *). The object's
1168 getter returns an (NSMutableArray *); but if you cast the
1169 object to the superclass, which is allowed, you'd still
1170 expect the getter to return an (NSArray *), which works since
1171 an (NSMutableArray *) is an (NSArray *) too. So, the set of
1172 objects belonging to the type of the new @property should be
1173 a subset of the set of objects belonging to the type of the
1174 old @property. This is what "specialization" means. And the
1175 reason it only applies to readonly properties is that for a
1176 readwrite property the setter would have the opposite
1177 requirement - ie that the superclass type is more specialized
1178 then the subclass one; hence the only way to satisfy both
1179 constraints is that the types match. */
1180
1181 /* If the types are not the same in the C sense, we warn ... */
1182 if (!comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1183 /* ... unless the property is readonly, in which case we
1184 allow a new, more specialized, declaration. */
944fb799 1185 && (!property_readonly
10e34e6e
NP
1186 || !objc_compare_types (TREE_TYPE (x),
1187 TREE_TYPE (decl), -5, NULL_TREE)))
4ca5d2a7 1188 {
10e34e6e
NP
1189 warning_at (location, 0,
1190 "type of property %qD conflicts with previous declaration", decl);
1191 if (original_location != UNKNOWN_LOCATION)
1192 inform (original_location, "originally specified here");
1193 return;
4ca5d2a7 1194 }
ec3e9f82
NP
1195
1196 /* If we are in a class extension and we're extending a readonly
1197 property in the main @interface, we'll just update the
1198 existing property with the readwrite flag and potentially the
1199 new setter name. */
1200 if (property_extension_in_class_extension)
1201 {
1202 PROPERTY_READONLY (x) = 0;
1203 PROPERTY_SETTER_NAME (x) = parsed_property_setter_ident;
1204 return;
1205 }
4ca5d2a7 1206 }
46a88c12
NP
1207
1208 /* Create a PROPERTY_DECL node. */
1209 property_decl = make_node (PROPERTY_DECL);
1210
1211 /* Copy the basic information from the original decl. */
1212 TREE_TYPE (property_decl) = TREE_TYPE (decl);
1213 DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl);
1214 TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl);
944fb799 1215
46a88c12
NP
1216 /* Add property-specific information. */
1217 PROPERTY_NAME (property_decl) = DECL_NAME (decl);
1218 PROPERTY_GETTER_NAME (property_decl) = parsed_property_getter_ident;
1219 PROPERTY_SETTER_NAME (property_decl) = parsed_property_setter_ident;
1220 PROPERTY_READONLY (property_decl) = property_readonly;
1221 PROPERTY_NONATOMIC (property_decl) = parsed_property_nonatomic;
1222 PROPERTY_ASSIGN_SEMANTICS (property_decl) = property_assign_semantics;
1223 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1224 PROPERTY_DYNAMIC (property_decl) = 0;
1225
2dd24dbd
NP
1226 /* Remember the fact that the property was found in the @optional
1227 section in a @protocol, or not. */
1228 if (objc_method_optional_flag)
1229 PROPERTY_OPTIONAL (property_decl) = 1;
1230 else
1231 PROPERTY_OPTIONAL (property_decl) = 0;
1232
4ca5d2a7
NP
1233 /* Note that PROPERTY_GETTER_NAME is always set for all
1234 PROPERTY_DECLs, and PROPERTY_SETTER_NAME is always set for all
1235 PROPERTY_DECLs where PROPERTY_READONLY == 0. Any time we deal
1236 with a getter or setter, we should get the PROPERTY_DECL and use
1237 PROPERTY_GETTER_NAME and PROPERTY_SETTER_NAME to know the correct
1238 names. */
1239
46a88c12
NP
1240 /* Add the PROPERTY_DECL to the list of properties for the class. */
1241 TREE_CHAIN (property_decl) = CLASS_PROPERTY_DECL (objc_interface_context);
1242 CLASS_PROPERTY_DECL (objc_interface_context) = property_decl;
668ea4b1
IS
1243}
1244
43236c26 1245/* This is a subroutine of objc_maybe_build_component_ref. Search the
a9625a91
NP
1246 list of methods in the interface (and, failing that, the local list
1247 in the implementation, and failing that, the protocol list)
43236c26
NP
1248 provided for a 'setter' or 'getter' for 'component' with default
1249 names (ie, if 'component' is "name", then search for "name" and
2dd24dbd
NP
1250 "setName:"). It is also possible to specify a different
1251 'getter_name' (this is used for @optional readonly properties). If
1252 any is found, then create an artificial property that uses them.
1253 Return NULL_TREE if 'getter' or 'setter' could not be found. */
43236c26 1254static tree
944fb799 1255maybe_make_artificial_property_decl (tree interface, tree implementation,
2dd24dbd
NP
1256 tree protocol_list, tree component, bool is_class,
1257 tree getter_name)
43236c26 1258{
43236c26
NP
1259 tree setter_name = get_identifier (objc_build_property_setter_name (component));
1260 tree getter = NULL_TREE;
1261 tree setter = NULL_TREE;
1262
2dd24dbd
NP
1263 if (getter_name == NULL_TREE)
1264 getter_name = component;
1265
a9625a91 1266 /* First, check the @interface and all superclasses. */
43236c26
NP
1267 if (interface)
1268 {
1269 int flags = 0;
1270
a9625a91
NP
1271 /* Using instance methods of the root class as accessors is most
1272 likely unwanted and can be extremely confusing (and, most
1273 importantly, other Objective-C 2.0 compilers do not do it).
1274 Turn it off. */
43236c26 1275 if (is_class)
a9625a91 1276 flags = OBJC_LOOKUP_CLASS | OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS;
944fb799 1277
43236c26
NP
1278 getter = lookup_method_static (interface, getter_name, flags);
1279 setter = lookup_method_static (interface, setter_name, flags);
1280 }
1281
a9625a91
NP
1282 /* Second, check the local @implementation context. */
1283 if (!getter && !setter)
1284 {
1285 if (implementation)
1286 {
1287 if (is_class)
1288 {
1289 getter = lookup_method (CLASS_CLS_METHODS (implementation), getter_name);
1290 setter = lookup_method (CLASS_CLS_METHODS (implementation), setter_name);
1291 }
1292 else
1293 {
1294 getter = lookup_method (CLASS_NST_METHODS (implementation), getter_name);
944fb799 1295 setter = lookup_method (CLASS_NST_METHODS (implementation), setter_name);
a9625a91
NP
1296 }
1297 }
1298 }
1299
1300 /* Try the protocol_list if we didn't find anything in the
1301 @interface and in the @implementation. */
43236c26
NP
1302 if (!getter && !setter)
1303 {
1304 getter = lookup_method_in_protocol_list (protocol_list, getter_name, is_class);
1305 setter = lookup_method_in_protocol_list (protocol_list, setter_name, is_class);
1306 }
1307
1308 /* There needs to be at least a getter or setter for this to be a
1309 valid 'object.component' syntax. */
1310 if (getter || setter)
1311 {
1312 /* Yes ... determine the type of the expression. */
1313 tree property_decl;
1314 tree type;
944fb799 1315
43236c26
NP
1316 if (getter)
1317 type = TREE_VALUE (TREE_TYPE (getter));
1318 else
1319 type = TREE_VALUE (TREE_TYPE (METHOD_SEL_ARGS (setter)));
944fb799 1320
43236c26
NP
1321 /* Create an artificial property declaration with the
1322 information we collected on the type and getter/setter
1323 names. */
1324 property_decl = make_node (PROPERTY_DECL);
944fb799 1325
43236c26
NP
1326 TREE_TYPE (property_decl) = type;
1327 DECL_SOURCE_LOCATION (property_decl) = input_location;
1328 TREE_DEPRECATED (property_decl) = 0;
1329 DECL_ARTIFICIAL (property_decl) = 1;
b4f588c4 1330
43236c26
NP
1331 /* Add property-specific information. Note that one of
1332 PROPERTY_GETTER_NAME or PROPERTY_SETTER_NAME may refer to a
1333 non-existing method; this will generate an error when the
1334 expression is later compiled. At this stage we don't know if
1335 the getter or setter will be used, so we can't generate an
1336 error. */
1337 PROPERTY_NAME (property_decl) = component;
1338 PROPERTY_GETTER_NAME (property_decl) = getter_name;
1339 PROPERTY_SETTER_NAME (property_decl) = setter_name;
1340 PROPERTY_READONLY (property_decl) = 0;
1341 PROPERTY_NONATOMIC (property_decl) = 0;
1342 PROPERTY_ASSIGN_SEMANTICS (property_decl) = 0;
1343 PROPERTY_IVAR_NAME (property_decl) = NULL_TREE;
1344 PROPERTY_DYNAMIC (property_decl) = 0;
2dd24dbd 1345 PROPERTY_OPTIONAL (property_decl) = 0;
43236c26
NP
1346
1347 if (!getter)
1348 PROPERTY_HAS_NO_GETTER (property_decl) = 1;
1349
1350 /* The following is currently unused, but it's nice to have
1351 there. We may use it if we need in the future. */
1352 if (!setter)
1353 PROPERTY_HAS_NO_SETTER (property_decl) = 1;
1354
1355 return property_decl;
1356 }
1357
1358 return NULL_TREE;
1359}
668ea4b1 1360
46a88c12
NP
1361/* This hook routine is invoked by the parser when an expression such
1362 as 'xxx.yyy' is parsed. We get a chance to process these
1363 expressions in a way that is specified to Objective-C (to implement
43236c26
NP
1364 the Objective-C 2.0 dot-syntax, properties, or non-fragile ivars).
1365 If the expression is not an Objective-C specified expression, we
1366 should return NULL_TREE; else we return the expression.
1367
1368 At the moment this only implements dot-syntax and properties (not
1369 non-fragile ivars yet), ie 'object.property' or 'object.component'
1370 where 'component' is not a declared property, but a valid getter or
1371 setter for it could be found. */
668ea4b1 1372tree
46a88c12 1373objc_maybe_build_component_ref (tree object, tree property_ident)
668ea4b1
IS
1374{
1375 tree x = NULL_TREE;
1376 tree rtype;
1377
a9625a91
NP
1378 /* If we are in Objective-C 1.0 mode, dot-syntax and properties are
1379 not available. */
22d8d616
NP
1380 if (flag_objc1_only)
1381 return NULL_TREE;
1382
a9625a91
NP
1383 /* Try to determine if 'object' is an Objective-C object or not. If
1384 not, return. */
944fb799 1385 if (object == NULL_TREE || object == error_mark_node
46a88c12 1386 || (rtype = TREE_TYPE (object)) == NULL_TREE)
668ea4b1 1387 return NULL_TREE;
944fb799 1388
46a88c12
NP
1389 if (property_ident == NULL_TREE || property_ident == error_mark_node
1390 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
668ea4b1
IS
1391 return NULL_TREE;
1392
a9625a91
NP
1393 /* The following analysis of 'object' is similar to the one used for
1394 the 'receiver' of a method invocation. We need to determine what
1395 'object' is and find the appropriate property (either declared,
1396 or artificial) for it (in the same way as we need to find the
1397 appropriate method prototype for a method invocation). There are
1398 some simplifications here though: "object.property" is invalid if
1399 "object" has a type of "id" or "Class"; it must at least have a
1400 protocol attached to it, and "object" is never a class name as
1401 that is done by objc_build_class_component_ref. Finally, we
1402 don't know if this really is a dot-syntax expression, so we want
1403 to make a quick exit if it is not; for this reason, we try to
1404 postpone checks after determining that 'object' looks like an
1405 Objective-C object. */
1406
668ea4b1
IS
1407 if (objc_is_id (rtype))
1408 {
a9625a91
NP
1409 /* This is the case that the 'object' is of type 'id' or
1410 'Class'. */
43236c26 1411
a9625a91
NP
1412 /* Check if at least it is of type 'id <Protocol>' or 'Class
1413 <Protocol>'; if so, look the property up in the
1414 protocols. */
1415 if (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype)))
43236c26 1416 {
a9625a91 1417 tree rprotos = TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype));
944fb799 1418
a9625a91
NP
1419 if (rprotos)
1420 {
1421 /* No point looking up declared @properties if we are
1422 dealing with a class. Classes have no declared
1423 properties. */
1424 if (!IS_CLASS (rtype))
1425 x = lookup_property_in_protocol_list (rprotos, property_ident);
2dd24dbd 1426
a9625a91
NP
1427 if (x == NULL_TREE)
1428 {
1429 /* Ok, no property. Maybe it was an
1430 object.component dot-syntax without a declared
1431 property (this is valid for classes too). Look
1432 for getter/setter methods and internally declare
587ae1e3 1433 an artificial property based on them if found. */
a9625a91
NP
1434 x = maybe_make_artificial_property_decl (NULL_TREE,
1435 NULL_TREE,
944fb799 1436 rprotos,
a9625a91 1437 property_ident,
2dd24dbd
NP
1438 IS_CLASS (rtype),
1439 NULL_TREE);
1440 }
1441 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1442 {
1443 /* This is a special, complicated case. If the
1444 property is optional, and is read-only, then the
1445 property is always used for reading, but an
1446 eventual existing non-property setter can be used
1447 for writing. We create an artificial property
1448 decl copying the getter from the optional
1449 property, and looking up the setter in the
1450 interface. */
1451 x = maybe_make_artificial_property_decl (NULL_TREE,
1452 NULL_TREE,
1453 rprotos,
1454 property_ident,
1455 false,
944fb799 1456 PROPERTY_GETTER_NAME (x));
a9625a91
NP
1457 }
1458 }
1459 }
1460 else if (objc_method_context)
1461 {
1462 /* Else, if we are inside a method it could be the case of
1463 'super' or 'self'. */
1464 tree interface_type = NULL_TREE;
1465 tree t = object;
1466 while (TREE_CODE (t) == COMPOUND_EXPR
1467 || TREE_CODE (t) == MODIFY_EXPR
1468 || CONVERT_EXPR_P (t)
1469 || TREE_CODE (t) == COMPONENT_REF)
1470 t = TREE_OPERAND (t, 0);
944fb799
MS
1471
1472 if (t == UOBJC_SUPER_decl)
d402edc1 1473 interface_type = lookup_interface (CLASS_SUPER_NAME (implementation_template));
a9625a91
NP
1474 else if (t == self_decl)
1475 interface_type = lookup_interface (CLASS_NAME (implementation_template));
1476
a9625a91
NP
1477 if (interface_type)
1478 {
1479 if (TREE_CODE (objc_method_context) != CLASS_METHOD_DECL)
10e34e6e 1480 x = lookup_property (interface_type, property_ident);
944fb799 1481
a9625a91
NP
1482 if (x == NULL_TREE)
1483 {
1484 /* Try the dot-syntax without a declared property.
1485 If this is an access to 'self', it is possible
1486 that they may refer to a setter/getter that is
1487 not declared in the interface, but exists locally
1488 in the implementation. In that case, get the
1489 implementation context and use it. */
1490 tree implementation = NULL_TREE;
1491
1492 if (t == self_decl)
1493 implementation = objc_implementation_context;
944fb799
MS
1494
1495 x = maybe_make_artificial_property_decl
a9625a91
NP
1496 (interface_type, implementation, NULL_TREE,
1497 property_ident,
2dd24dbd
NP
1498 (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL),
1499 NULL_TREE);
1500 }
1501 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1502 {
1503 tree implementation = NULL_TREE;
944fb799 1504
2dd24dbd
NP
1505 if (t == self_decl)
1506 implementation = objc_implementation_context;
944fb799 1507
2dd24dbd
NP
1508 x = maybe_make_artificial_property_decl (interface_type,
1509 implementation,
1510 NULL_TREE,
1511 property_ident,
1512 false,
944fb799 1513 PROPERTY_GETTER_NAME (x));
a9625a91
NP
1514 }
1515 }
43236c26 1516 }
668ea4b1
IS
1517 }
1518 else
1519 {
a9625a91 1520 /* This is the case where we have more information on 'rtype'. */
668ea4b1
IS
1521 tree basetype = TYPE_MAIN_VARIANT (rtype);
1522
a9625a91
NP
1523 /* Skip the pointer - if none, it's not an Objective-C object or
1524 class. */
668ea4b1
IS
1525 if (basetype != NULL_TREE && TREE_CODE (basetype) == POINTER_TYPE)
1526 basetype = TREE_TYPE (basetype);
1527 else
1528 return NULL_TREE;
1529
a9625a91 1530 /* Traverse typedefs. */
668ea4b1 1531 while (basetype != NULL_TREE
944fb799 1532 && TREE_CODE (basetype) == RECORD_TYPE
668ea4b1
IS
1533 && OBJC_TYPE_NAME (basetype)
1534 && TREE_CODE (OBJC_TYPE_NAME (basetype)) == TYPE_DECL
1535 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype)))
1536 basetype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (basetype));
1537
1538 if (basetype != NULL_TREE && TYPED_OBJECT (basetype))
1539 {
1540 tree interface_type = TYPE_OBJC_INTERFACE (basetype);
46a88c12 1541 tree protocol_list = TYPE_OBJC_PROTOCOL_LIST (basetype);
43236c26 1542
944fb799 1543 if (interface_type
a9625a91
NP
1544 && (TREE_CODE (interface_type) == CLASS_INTERFACE_TYPE
1545 || TREE_CODE (interface_type) == CATEGORY_INTERFACE_TYPE
1546 || TREE_CODE (interface_type) == PROTOCOL_INTERFACE_TYPE))
43236c26 1547 {
a9625a91
NP
1548 /* Not sure 'rtype' could ever be a class here! Just
1549 for safety we keep the checks. */
1550 if (!IS_CLASS (rtype))
1551 {
1552 x = lookup_property (interface_type, property_ident);
944fb799 1553
a9625a91 1554 if (x == NULL_TREE)
944fb799 1555 x = lookup_property_in_protocol_list (protocol_list,
a9625a91
NP
1556 property_ident);
1557 }
944fb799 1558
a9625a91
NP
1559 if (x == NULL_TREE)
1560 {
1561 /* Try the dot-syntax without a declared property.
1562 If we are inside a method implementation, it is
1563 possible that they may refer to a setter/getter
1564 that is not declared in the interface, but exists
1565 locally in the implementation. In that case, get
1566 the implementation context and use it. */
1567 tree implementation = NULL_TREE;
1568
1569 if (objc_implementation_context
944fb799 1570 && CLASS_NAME (objc_implementation_context)
a9625a91
NP
1571 == OBJC_TYPE_NAME (interface_type))
1572 implementation = objc_implementation_context;
944fb799 1573
a9625a91
NP
1574 x = maybe_make_artificial_property_decl (interface_type,
1575 implementation,
944fb799 1576 protocol_list,
a9625a91 1577 property_ident,
2dd24dbd
NP
1578 IS_CLASS (rtype),
1579 NULL_TREE);
a9625a91 1580 }
2dd24dbd
NP
1581 else if (PROPERTY_OPTIONAL (x) && PROPERTY_READONLY (x))
1582 {
1583 tree implementation = NULL_TREE;
1584
1585 if (objc_implementation_context
944fb799 1586 && CLASS_NAME (objc_implementation_context)
2dd24dbd
NP
1587 == OBJC_TYPE_NAME (interface_type))
1588 implementation = objc_implementation_context;
944fb799 1589
2dd24dbd
NP
1590 x = maybe_make_artificial_property_decl (interface_type,
1591 implementation,
1592 protocol_list,
1593 property_ident,
1594 false,
944fb799
MS
1595 PROPERTY_GETTER_NAME (x));
1596 }
43236c26 1597 }
668ea4b1
IS
1598 }
1599 }
1600
1601 if (x)
1602 {
46a88c12 1603 tree expression;
a9625a91 1604 tree getter_call;
b4f588c4 1605 tree deprecated_method_prototype = NULL_TREE;
46a88c12
NP
1606
1607 /* We have an additional nasty problem here; if this
1608 PROPERTY_REF needs to become a 'getter', then the conversion
1609 from PROPERTY_REF into a getter call happens in gimplify,
a9625a91
NP
1610 after the selector table has already been generated and when
1611 it is too late to add another selector to it. To work around
1612 the problem, we always create the getter call at this stage,
1613 which puts the selector in the table. Note that if the
1614 PROPERTY_REF becomes a 'setter' instead of a 'getter', then
1615 we have added a selector too many to the selector table.
1616 This is a little inefficient.
1617
1618 Also note that method calls to 'self' and 'super' require the
1619 context (self_decl, UOBJS_SUPER_decl,
1620 objc_implementation_context etc) to be built correctly; this
1621 is yet another reason why building the call at the gimplify
1622 stage (when this context has been lost) is not very
1623 practical. If we build it at this stage, we know it will
1624 always be built correctly.
43236c26
NP
1625
1626 If the PROPERTY_HAS_NO_GETTER() (ie, it is an artificial
1627 property decl created to deal with a dotsyntax not really
1628 referring to an existing property) then do not try to build a
1629 call to the getter as there is no getter. */
a9625a91
NP
1630 if (PROPERTY_HAS_NO_GETTER (x))
1631 getter_call = NULL_TREE;
1632 else
b4f588c4
NP
1633 getter_call = objc_finish_message_expr
1634 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1635 /* Disable the immediate deprecation warning if the getter
1636 is deprecated, but record the fact that the getter is
1637 deprecated by setting PROPERTY_REF_DEPRECATED_GETTER to
1638 the method prototype. */
1639 &deprecated_method_prototype);
1640
1641 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1642 deprecated_method_prototype);
a9625a91
NP
1643 SET_EXPR_LOCATION (expression, input_location);
1644 TREE_SIDE_EFFECTS (expression) = 1;
944fb799 1645
46a88c12 1646 return expression;
668ea4b1 1647 }
668ea4b1 1648
46a88c12 1649 return NULL_TREE;
668ea4b1
IS
1650}
1651
bede2adc
NP
1652/* This hook routine is invoked by the parser when an expression such
1653 as 'xxx.yyy' is parsed, and 'xxx' is a class name. This is the
1654 Objective-C 2.0 dot-syntax applied to classes, so we need to
1655 convert it into a setter/getter call on the class. */
1656tree
1657objc_build_class_component_ref (tree class_name, tree property_ident)
1658{
1659 tree x = NULL_TREE;
1660 tree object, rtype;
944fb799 1661
bede2adc
NP
1662 if (flag_objc1_only)
1663 error_at (input_location, "the dot syntax is not available in Objective-C 1.0");
944fb799 1664
bede2adc
NP
1665 if (class_name == NULL_TREE || class_name == error_mark_node
1666 || TREE_CODE (class_name) != IDENTIFIER_NODE)
1667 return error_mark_node;
944fb799 1668
bede2adc
NP
1669 if (property_ident == NULL_TREE || property_ident == error_mark_node
1670 || TREE_CODE (property_ident) != IDENTIFIER_NODE)
1671 return NULL_TREE;
944fb799 1672
bede2adc
NP
1673 object = objc_get_class_reference (class_name);
1674 if (!object)
1675 {
1676 /* We know that 'class_name' is an Objective-C class name as the
1677 parser won't call this function if it is not. This is only a
1678 double-check for safety. */
944fb799 1679 error_at (input_location, "could not find class %qE", class_name);
bede2adc
NP
1680 return error_mark_node;
1681 }
1682
1683 rtype = lookup_interface (class_name);
1684 if (!rtype)
1685 {
1686 /* Again, this should never happen, but we do check. */
944fb799 1687 error_at (input_location, "could not find interface for class %qE", class_name);
bede2adc
NP
1688 return error_mark_node;
1689 }
d4e72c58
NP
1690 else
1691 {
1692 if (TREE_DEPRECATED (rtype))
944fb799 1693 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated", class_name);
d4e72c58 1694 }
bede2adc 1695
a9625a91 1696 x = maybe_make_artificial_property_decl (rtype, NULL_TREE, NULL_TREE,
bede2adc 1697 property_ident,
2dd24dbd 1698 true, NULL_TREE);
944fb799 1699
bede2adc
NP
1700 if (x)
1701 {
1702 tree expression;
a9625a91 1703 tree getter_call;
b4f588c4 1704 tree deprecated_method_prototype = NULL_TREE;
bede2adc 1705
a9625a91
NP
1706 if (PROPERTY_HAS_NO_GETTER (x))
1707 getter_call = NULL_TREE;
1708 else
b4f588c4
NP
1709 getter_call = objc_finish_message_expr
1710 (object, PROPERTY_GETTER_NAME (x), NULL_TREE,
1711 &deprecated_method_prototype);
bede2adc 1712
b4f588c4
NP
1713 expression = build4 (PROPERTY_REF, TREE_TYPE(x), object, x, getter_call,
1714 deprecated_method_prototype);
bede2adc
NP
1715 SET_EXPR_LOCATION (expression, input_location);
1716 TREE_SIDE_EFFECTS (expression) = 1;
a9625a91 1717
bede2adc
NP
1718 return expression;
1719 }
1720 else
1721 {
944fb799
MS
1722 error_at (input_location, "could not find setter/getter for %qE in class %qE",
1723 property_ident, class_name);
bede2adc
NP
1724 return error_mark_node;
1725 }
1726
1727 return NULL_TREE;
1728}
1729
1730
1731
46a88c12
NP
1732/* This is used because we don't want to expose PROPERTY_REF to the
1733 C/C++ frontends. Maybe we should! */
1734bool
1735objc_is_property_ref (tree node)
668ea4b1 1736{
46a88c12
NP
1737 if (node && TREE_CODE (node) == PROPERTY_REF)
1738 return true;
668ea4b1 1739 else
46a88c12 1740 return false;
668ea4b1
IS
1741}
1742
b6cfe8ac
NP
1743/* This function builds a setter call for a PROPERTY_REF (real, for a
1744 declared property, or artificial, for a dot-syntax accessor which
1745 is not corresponding to a property). 'lhs' must be a PROPERTY_REF
1746 (the caller must check this beforehand). 'rhs' is the value to
1747 assign to the property. A plain setter call is returned, or
1748 error_mark_node if the property is readonly. */
1749
1750static tree
1751objc_build_setter_call (tree lhs, tree rhs)
1752{
1753 tree object_expr = PROPERTY_REF_OBJECT (lhs);
1754 tree property_decl = PROPERTY_REF_PROPERTY_DECL (lhs);
944fb799 1755
b6cfe8ac
NP
1756 if (PROPERTY_READONLY (property_decl))
1757 {
944fb799 1758 error ("readonly property can not be set");
b6cfe8ac
NP
1759 return error_mark_node;
1760 }
1761 else
1762 {
1763 tree setter_argument = build_tree_list (NULL_TREE, rhs);
1764 tree setter;
944fb799 1765
b6cfe8ac
NP
1766 /* TODO: Check that the setter return type is 'void'. */
1767
1768 /* TODO: Decay arguments in C. */
944fb799 1769 setter = objc_finish_message_expr (object_expr,
b6cfe8ac 1770 PROPERTY_SETTER_NAME (property_decl),
b4f588c4 1771 setter_argument, NULL);
b6cfe8ac
NP
1772 return setter;
1773 }
1774
1775 /* Unreachable, but the compiler may not realize. */
1776 return error_mark_node;
1777}
1778
46a88c12
NP
1779/* This hook routine is called when a MODIFY_EXPR is being built. We
1780 check what is being modified; if it is a PROPERTY_REF, we need to
1781 generate a 'setter' function call for the property. If this is not
1782 a PROPERTY_REF, we return NULL_TREE and the C/C++ frontend will go
1783 on creating their MODIFY_EXPR.
668ea4b1 1784
46a88c12 1785 This is used for example if you write
668ea4b1 1786
46a88c12 1787 object.count = 1;
668ea4b1 1788
46a88c12
NP
1789 where 'count' is a property. The left-hand side creates a
1790 PROPERTY_REF, and then the compiler tries to generate a MODIFY_EXPR
1791 to assign something to it. We intercept that here, and generate a
1792 call to the 'setter' method instead. */
1793tree
1794objc_maybe_build_modify_expr (tree lhs, tree rhs)
668ea4b1 1795{
46a88c12 1796 if (lhs && TREE_CODE (lhs) == PROPERTY_REF)
668ea4b1 1797 {
b6cfe8ac 1798 /* Building a simple call to the setter method would work for cases such as
668ea4b1 1799
b6cfe8ac 1800 object.count = 1;
43236c26 1801
b6cfe8ac 1802 but wouldn't work for cases such as
43236c26 1803
b6cfe8ac
NP
1804 count = object2.count = 1;
1805
1806 to get these to work with very little effort, we build a
1807 compound statement which does the setter call (to set the
1808 property to 'rhs'), but which can also be evaluated returning
3890ce93
NP
1809 the 'rhs'. If the 'rhs' has no side effects, we can simply
1810 evaluate it twice, building
1811
1812 ([object setProperty: rhs]; rhs)
1813
1814 If it has side effects, we put it in a temporary variable first,
1815 so we create the following:
b6cfe8ac
NP
1816
1817 (temp = rhs; [object setProperty: temp]; temp)
3890ce93
NP
1818
1819 setter_argument is rhs in the first case, and temp in the second
1820 case.
b6cfe8ac 1821 */
3890ce93
NP
1822 tree setter_argument;
1823
b6cfe8ac
NP
1824 /* s1, s2 and s3 are the tree statements that we need in the
1825 compound expression. */
1826 tree s1, s2, s3, compound_expr;
3890ce93
NP
1827
1828 if (TREE_SIDE_EFFECTS (rhs))
1829 {
1830 tree bind;
944fb799 1831
3890ce93
NP
1832 /* Declare __objc_property_temp in a local bind. */
1833 setter_argument = objc_create_temporary_var (TREE_TYPE (rhs), "__objc_property_temp");
1834 DECL_SOURCE_LOCATION (setter_argument) = input_location;
1835 bind = build3 (BIND_EXPR, void_type_node, setter_argument, NULL, NULL);
1836 SET_EXPR_LOCATION (bind, input_location);
1837 TREE_SIDE_EFFECTS (bind) = 1;
1838 add_stmt (bind);
1839
1840 /* s1: x = rhs */
1841 s1 = build_modify_expr (input_location, setter_argument, NULL_TREE,
1842 NOP_EXPR,
1843 input_location, rhs, NULL_TREE);
1844 SET_EXPR_LOCATION (s1, input_location);
1845 }
1846 else
1847 {
1848 /* No s1. */
1849 setter_argument = rhs;
1850 s1 = NULL_TREE;
1851 }
944fb799 1852
b6cfe8ac 1853 /* Now build the compound statement. */
944fb799 1854
3890ce93
NP
1855 /* s2: [object setProperty: x] */
1856 s2 = objc_build_setter_call (lhs, setter_argument);
944fb799 1857
3890ce93
NP
1858 /* This happens if building the setter failed because the
1859 property is readonly. */
b6cfe8ac
NP
1860 if (s2 == error_mark_node)
1861 return error_mark_node;
1862
1863 SET_EXPR_LOCATION (s2, input_location);
944fb799 1864
3890ce93
NP
1865 /* s3: x */
1866 s3 = convert (TREE_TYPE (lhs), setter_argument);
b6cfe8ac 1867
3890ce93
NP
1868 /* Now build the compound statement (s1, s2, s3) or (s2, s3) as
1869 appropriate. */
1870 if (s1)
1871 compound_expr = build_compound_expr (input_location, build_compound_expr (input_location, s1, s2), s3);
1872 else
944fb799 1873 compound_expr = build_compound_expr (input_location, s2, s3);
b6cfe8ac
NP
1874
1875 /* Without this, with -Wall you get a 'valued computed is not
1876 used' every time there is a "object.property = x" where the
1877 value of the resulting MODIFY_EXPR is not used. That is
1878 correct (maybe a more sophisticated implementation could
1879 avoid generating the compound expression if not needed), but
1880 we need to turn it off. */
1881 TREE_NO_WARNING (compound_expr) = 1;
1882 return compound_expr;
46a88c12
NP
1883 }
1884 else
1885 return NULL_TREE;
668ea4b1
IS
1886}
1887
925e8657
NP
1888/* This hook is called by the frontend when one of the four unary
1889 expressions PREINCREMENT_EXPR, POSTINCREMENT_EXPR,
1890 PREDECREMENT_EXPR and POSTDECREMENT_EXPR is being built with an
1891 argument which is a PROPERTY_REF. For example, this happens if you have
1892
1893 object.count++;
1894
1895 where 'count' is a property. We need to use the 'getter' and
1896 'setter' for the property in an appropriate way to build the
1897 appropriate expression. 'code' is the code for the expression (one
1898 of the four mentioned above); 'argument' is the PROPERTY_REF, and
944fb799 1899 'increment' is how much we need to add or subtract. */
925e8657
NP
1900tree
1901objc_build_incr_expr_for_property_ref (location_t location,
944fb799 1902 enum tree_code code,
925e8657
NP
1903 tree argument, tree increment)
1904{
1905 /* Here are the expressions that we want to build:
1906
1907 For PREINCREMENT_EXPR / PREDECREMENT_EXPR:
1908 (temp = [object property] +/- increment, [object setProperty: temp], temp)
944fb799 1909
925e8657
NP
1910 For POSTINCREMENT_EXPR / POSTECREMENT_EXPR:
1911 (temp = [object property], [object setProperty: temp +/- increment], temp) */
944fb799 1912
925e8657
NP
1913 tree temp_variable_decl, bind;
1914 /* s1, s2 and s3 are the tree statements that we need in the
1915 compound expression. */
b6cfe8ac 1916 tree s1, s2, s3, compound_expr;
944fb799 1917
925e8657
NP
1918 /* Safety check. */
1919 if (!argument || TREE_CODE (argument) != PROPERTY_REF)
1920 return error_mark_node;
1921
1922 /* Declare __objc_property_temp in a local bind. */
1923 temp_variable_decl = objc_create_temporary_var (TREE_TYPE (argument), "__objc_property_temp");
1924 DECL_SOURCE_LOCATION (temp_variable_decl) = location;
1925 bind = build3 (BIND_EXPR, void_type_node, temp_variable_decl, NULL, NULL);
1926 SET_EXPR_LOCATION (bind, location);
1927 TREE_SIDE_EFFECTS (bind) = 1;
1928 add_stmt (bind);
944fb799 1929
925e8657 1930 /* Now build the compound statement. */
944fb799 1931
925e8657
NP
1932 /* Note that the 'getter' is generated at gimplify time; at this
1933 time, we can simply put the property_ref (ie, argument) wherever
1934 we want the getter ultimately to be. */
944fb799 1935
925e8657
NP
1936 /* s1: __objc_property_temp = [object property] <+/- increment> */
1937 switch (code)
1938 {
944fb799 1939 case PREINCREMENT_EXPR:
925e8657 1940 /* __objc_property_temp = [object property] + increment */
b6cfe8ac
NP
1941 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1942 NOP_EXPR,
944fb799 1943 location, build2 (PLUS_EXPR, TREE_TYPE (argument),
b6cfe8ac 1944 argument, increment), NULL_TREE);
925e8657
NP
1945 break;
1946 case PREDECREMENT_EXPR:
1947 /* __objc_property_temp = [object property] - increment */
b6cfe8ac
NP
1948 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1949 NOP_EXPR,
944fb799 1950 location, build2 (MINUS_EXPR, TREE_TYPE (argument),
b6cfe8ac 1951 argument, increment), NULL_TREE);
925e8657
NP
1952 break;
1953 case POSTINCREMENT_EXPR:
1954 case POSTDECREMENT_EXPR:
1955 /* __objc_property_temp = [object property] */
b6cfe8ac
NP
1956 s1 = build_modify_expr (location, temp_variable_decl, NULL_TREE,
1957 NOP_EXPR,
1958 location, argument, NULL_TREE);
925e8657
NP
1959 break;
1960 default:
1961 gcc_unreachable ();
1962 }
944fb799 1963
925e8657
NP
1964 /* s2: [object setProperty: __objc_property_temp <+/- increment>] */
1965 switch (code)
1966 {
944fb799 1967 case PREINCREMENT_EXPR:
925e8657
NP
1968 case PREDECREMENT_EXPR:
1969 /* [object setProperty: __objc_property_temp] */
b6cfe8ac 1970 s2 = objc_build_setter_call (argument, temp_variable_decl);
925e8657
NP
1971 break;
1972 case POSTINCREMENT_EXPR:
1973 /* [object setProperty: __objc_property_temp + increment] */
b6cfe8ac 1974 s2 = objc_build_setter_call (argument,
944fb799 1975 build2 (PLUS_EXPR, TREE_TYPE (argument),
b6cfe8ac 1976 temp_variable_decl, increment));
925e8657
NP
1977 break;
1978 case POSTDECREMENT_EXPR:
1979 /* [object setProperty: __objc_property_temp - increment] */
b6cfe8ac 1980 s2 = objc_build_setter_call (argument,
944fb799 1981 build2 (MINUS_EXPR, TREE_TYPE (argument),
b6cfe8ac 1982 temp_variable_decl, increment));
925e8657
NP
1983 break;
1984 default:
1985 gcc_unreachable ();
1986 }
1987
1988 /* This happens if building the setter failed because the property
1989 is readonly. */
1990 if (s2 == error_mark_node)
1991 return error_mark_node;
1992
944fb799
MS
1993 SET_EXPR_LOCATION (s2, location);
1994
925e8657 1995 /* s3: __objc_property_temp */
b6cfe8ac 1996 s3 = convert (TREE_TYPE (argument), temp_variable_decl);
944fb799 1997
925e8657 1998 /* Now build the compound statement (s1, s2, s3) */
b6cfe8ac
NP
1999 compound_expr = build_compound_expr (location, build_compound_expr (location, s1, s2), s3);
2000
2001 /* Prevent C++ from warning with -Wall that "right operand of comma
2002 operator has no effect". */
2003 TREE_NO_WARNING (compound_expr) = 1;
2004 return compound_expr;
925e8657
NP
2005}
2006
6408ef33 2007tree
249a82c4 2008objc_build_method_signature (bool is_class_method, tree rettype, tree selector,
dbb74365 2009 tree optparms, bool ellipsis)
6408ef33 2010{
249a82c4
NP
2011 if (is_class_method)
2012 return build_method_decl (CLASS_METHOD_DECL, rettype, selector,
2013 optparms, ellipsis);
2014 else
2015 return build_method_decl (INSTANCE_METHOD_DECL, rettype, selector,
2016 optparms, ellipsis);
6408ef33
ZL
2017}
2018
2019void
249a82c4 2020objc_add_method_declaration (bool is_class_method, tree decl, tree attributes)
6408ef33
ZL
2021{
2022 if (!objc_interface_context)
45547c7f
NP
2023 {
2024 /* PS: At the moment, due to how the parser works, it should be
2025 impossible to get here. But it's good to have the check in
2026 case the parser changes.
2027 */
40fecdd6
JM
2028 fatal_error (input_location,
2029 "method declaration not in @interface context");
45547c7f 2030 }
6408ef33 2031
22d8d616
NP
2032 if (flag_objc1_only && attributes)
2033 error_at (input_location, "method attributes are not available in Objective-C 1.0");
2034
a1178b30 2035 objc_decl_method_attributes (&decl, attributes, 0);
6408ef33
ZL
2036 objc_add_method (objc_interface_context,
2037 decl,
249a82c4 2038 is_class_method,
92902b1b 2039 objc_method_optional_flag);
6408ef33
ZL
2040}
2041
45547c7f
NP
2042/* Return 'true' if the method definition could be started, and
2043 'false' if not (because we are outside an @implementation context).
a04a722b
JM
2044 EXPR is NULL or an expression that needs to be evaluated for the
2045 side effects of array size expressions in the parameters.
45547c7f
NP
2046*/
2047bool
a04a722b
JM
2048objc_start_method_definition (bool is_class_method, tree decl, tree attributes,
2049 tree expr)
6408ef33
ZL
2050{
2051 if (!objc_implementation_context)
45547c7f
NP
2052 {
2053 error ("method definition not in @implementation context");
2054 return false;
2055 }
6408ef33 2056
a26d8862
NP
2057 if (decl != NULL_TREE && METHOD_SEL_NAME (decl) == error_mark_node)
2058 return false;
2059
0f185d6e
NP
2060#ifndef OBJCPLUS
2061 /* Indicate no valid break/continue context by setting these variables
2062 to some non-null, non-label value. We'll notice and emit the proper
2063 error message in c_finish_bc_stmt. */
2064 c_break_label = c_cont_label = size_zero_node;
2065#endif
2066
2debdb4f
NP
2067 if (attributes)
2068 warning_at (input_location, 0, "method attributes can not be specified in @implementation context");
2069 else
2070 objc_decl_method_attributes (&decl, attributes, 0);
2071
6408ef33
ZL
2072 objc_add_method (objc_implementation_context,
2073 decl,
249a82c4 2074 is_class_method,
92902b1b 2075 /* is optional */ false);
a04a722b 2076 start_method_def (decl, expr);
45547c7f 2077 return true;
6408ef33
ZL
2078}
2079
2080void
2081objc_add_instance_variable (tree decl)
2082{
2083 (void) add_instance_variable (objc_ivar_context,
c37d8c30 2084 objc_ivar_visibility,
6408ef33
ZL
2085 decl);
2086}
2087
5025f379 2088/* Construct a C struct with same name as KLASS, a base struct with tag
d11dd684 2089 SUPER_NAME (if any), and FIELDS indicated. */
3f16185f 2090
d11dd684 2091static tree
5025f379 2092objc_build_struct (tree klass, tree fields, tree super_name)
d11dd684 2093{
5025f379 2094 tree name = CLASS_NAME (klass);
24b97832 2095 tree s = objc_start_struct (name);
d11dd684 2096 tree super = (super_name ? xref_tag (RECORD_TYPE, super_name) : NULL_TREE);
3e78185c 2097 tree t;
6e1aa848 2098 vec<tree> objc_info = vNULL;
3e78185c 2099 int i;
d11dd684
ZL
2100
2101 if (super)
2102 {
2103 /* Prepend a packed variant of the base class into the layout. This
2104 is necessary to preserve ObjC ABI compatibility. */
c2255bc4
AH
2105 tree base = build_decl (input_location,
2106 FIELD_DECL, NULL_TREE, super);
d11dd684
ZL
2107 tree field = TYPE_FIELDS (super);
2108
910ad8de
NF
2109 while (field && DECL_CHAIN (field)
2110 && TREE_CODE (DECL_CHAIN (field)) == FIELD_DECL)
2111 field = DECL_CHAIN (field);
d11dd684 2112
39a13be5 2113 /* For ObjC ABI purposes, the "packed" size of a base class is
c64de75f
ZL
2114 the sum of the offset and the size (in bits) of the last field
2115 in the class. */
d11dd684
ZL
2116 DECL_SIZE (base)
2117 = (field && TREE_CODE (field) == FIELD_DECL
64ee9490 2118 ? size_binop (PLUS_EXPR,
d11dd684
ZL
2119 size_binop (PLUS_EXPR,
2120 size_binop
2121 (MULT_EXPR,
2122 convert (bitsizetype,
2123 DECL_FIELD_OFFSET (field)),
2124 bitsize_int (BITS_PER_UNIT)),
2125 DECL_FIELD_BIT_OFFSET (field)),
2126 DECL_SIZE (field))
2127 : bitsize_zero_node);
2128 DECL_SIZE_UNIT (base)
2129 = size_binop (FLOOR_DIV_EXPR, convert (sizetype, DECL_SIZE (base)),
2130 size_int (BITS_PER_UNIT));
2131 DECL_ARTIFICIAL (base) = 1;
2132 DECL_ALIGN (base) = 1;
2133 DECL_FIELD_CONTEXT (base) = s;
2134#ifdef OBJCPLUS
2135 DECL_FIELD_IS_BASE (base) = 1;
2136
2137 if (fields)
2138 TREE_NO_WARNING (fields) = 1; /* Suppress C++ ABI warnings -- we */
2139#endif /* are following the ObjC ABI here. */
910ad8de 2140 DECL_CHAIN (base) = fields;
d11dd684
ZL
2141 fields = base;
2142 }
2143
a693d3a8
NP
2144 /* NB: Calling finish_struct() may cause type TYPE_OBJC_INFO
2145 information in all variants of this RECORD_TYPE to be destroyed
2146 (this is because the C frontend manipulates TYPE_LANG_SPECIFIC
2147 for something else and then will change all variants to use the
2148 same resulting TYPE_LANG_SPECIFIC, ignoring the fact that we use
2149 it for ObjC protocols and that such propagation will make all
2150 variants use the same objc_info), but it is therein that we store
2151 protocol conformance info (e.g., 'NSObject <MyProtocol>').
2152 Hence, we must save the ObjC-specific information before calling
2aba33dd
ZL
2153 finish_struct(), and then reinstate it afterwards. */
2154
a693d3a8 2155 for (t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t))
76dcaf33 2156 {
a693d3a8 2157 INIT_TYPE_OBJC_INFO (t);
9771b263 2158 objc_info.safe_push (TYPE_OBJC_INFO (t));
76dcaf33 2159 }
2aba33dd 2160
24b97832 2161 s = objc_finish_struct (s, fields);
d11dd684 2162
a693d3a8 2163 for (i = 0, t = TYPE_MAIN_VARIANT (s); t; t = TYPE_NEXT_VARIANT (t), i++)
a0e71127 2164 {
a693d3a8
NP
2165 /* We now want to restore the different TYPE_OBJC_INFO, but we
2166 have the additional problem that the C frontend doesn't just
2167 copy TYPE_LANG_SPECIFIC from one variant to the other; it
2168 actually makes all of them the *same* TYPE_LANG_SPECIFIC. As
2169 we need a different TYPE_OBJC_INFO for each (and
2170 TYPE_OBJC_INFO is a field in TYPE_LANG_SPECIFIC), we need to
2171 make a copy of each TYPE_LANG_SPECIFIC before we modify
2172 TYPE_OBJC_INFO. */
2173 if (TYPE_LANG_SPECIFIC (t))
2174 {
2175 /* Create a copy of TYPE_LANG_SPECIFIC. */
2176 struct lang_type *old_lang_type = TYPE_LANG_SPECIFIC (t);
2177 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2178 memcpy (TYPE_LANG_SPECIFIC (t), old_lang_type,
2179 SIZEOF_OBJC_TYPE_LANG_SPECIFIC);
2180 }
2181 else
2182 {
2183 /* Just create a new one. */
2184 ALLOC_OBJC_TYPE_LANG_SPECIFIC (t);
2185 }
2186 /* Replace TYPE_OBJC_INFO with the saved one. This restores any
2187 protocol information that may have been associated with the
2188 type. */
9771b263 2189 TYPE_OBJC_INFO (t) = objc_info[i];
a693d3a8
NP
2190 /* Replace the IDENTIFIER_NODE with an actual @interface now
2191 that we have it. */
5025f379 2192 TYPE_OBJC_INTERFACE (t) = klass;
a0e71127 2193 }
9771b263 2194 objc_info.release ();
2aba33dd 2195
d11dd684
ZL
2196 /* Use TYPE_BINFO structures to point at the super class, if any. */
2197 objc_xref_basetypes (s, super);
2198
a0e71127 2199 /* Mark this struct as a class template. */
5025f379 2200 CLASS_STATIC_TEMPLATE (klass) = s;
a0e71127 2201
d11dd684
ZL
2202 return s;
2203}
2204
2205/* Mark DECL as being 'volatile' for purposes of Darwin
2206 _setjmp()/_longjmp() exception handling. Called from
2207 objc_mark_locals_volatile(). */
2208void
2209objc_volatilize_decl (tree decl)
2210{
2211 /* Do not mess with variables that are 'static' or (already)
2212 'volatile'. */
2213 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
2214 && (TREE_CODE (decl) == VAR_DECL
2215 || TREE_CODE (decl) == PARM_DECL))
2216 {
6c39e757 2217 if (local_variables_to_volatilize == NULL)
9771b263 2218 vec_alloc (local_variables_to_volatilize, 8);
d11dd684 2219
9771b263 2220 vec_safe_push (local_variables_to_volatilize, decl);
6c39e757
NP
2221 }
2222}
d11dd684 2223
6c39e757
NP
2224/* Called when parsing of a function completes; if any local variables
2225 in the function were marked as variables to volatilize, change them
2226 to volatile. We do this at the end of the function when the
2227 warnings about discarding 'volatile' have already been produced.
2228 We are making the variables as volatile just to force the compiler
2229 to preserve them between setjmp/longjmp, but we don't want warnings
2230 for them as they aren't really volatile. */
2231void
2232objc_finish_function (void)
2233{
2234 /* If there are any local variables to volatilize, volatilize them. */
2235 if (local_variables_to_volatilize)
2236 {
2237 int i;
2238 tree decl;
9771b263 2239 FOR_EACH_VEC_ELT (*local_variables_to_volatilize, i, decl)
6c39e757
NP
2240 {
2241 tree t = TREE_TYPE (decl);
2242
2243 t = build_qualified_type (t, TYPE_QUALS (t) | TYPE_QUAL_VOLATILE);
2244 TREE_TYPE (decl) = t;
2245 TREE_THIS_VOLATILE (decl) = 1;
2246 TREE_SIDE_EFFECTS (decl) = 1;
2247 DECL_REGISTER (decl) = 0;
d11dd684 2248#ifndef OBJCPLUS
6c39e757 2249 C_DECL_REGISTER (decl) = 0;
d11dd684 2250#endif
6c39e757
NP
2251 }
2252
2253 /* Now we delete the vector. This sets it to NULL as well. */
9771b263 2254 vec_free (local_variables_to_volatilize);
d11dd684
ZL
2255 }
2256}
2257
2258/* Check if protocol PROTO is adopted (directly or indirectly) by class CLS
c9819cb2 2259 (including its categories and superclasses) or by object type TYP.
d11dd684
ZL
2260 Issue a warning if PROTO is not adopted anywhere and WARN is set. */
2261
2262static bool
2263objc_lookup_protocol (tree proto, tree cls, tree typ, bool warn)
3f16185f 2264{
d11dd684 2265 bool class_type = (cls != NULL_TREE);
3f16185f 2266
d11dd684 2267 while (cls)
3f16185f 2268 {
d11dd684
ZL
2269 tree c;
2270
2271 /* Check protocols adopted by the class and its categories. */
2272 for (c = cls; c; c = CLASS_CATEGORY_LIST (c))
2273 {
2274 if (lookup_protocol_in_reflist (CLASS_PROTOCOL_LIST (c), proto))
2275 return true;
2276 }
2277
2278 /* Repeat for superclasses. */
2279 cls = lookup_interface (CLASS_SUPER_NAME (cls));
3f16185f 2280 }
d11dd684
ZL
2281
2282 /* Check for any protocols attached directly to the object type. */
2283 if (TYPE_HAS_OBJC_INFO (typ))
3f16185f 2284 {
d11dd684
ZL
2285 if (lookup_protocol_in_reflist (TYPE_OBJC_PROTOCOL_LIST (typ), proto))
2286 return true;
2287 }
2288
2289 if (warn)
2290 {
f41c4af3 2291 *errbuf = 0;
d11dd684 2292 gen_type_name_0 (class_type ? typ : TYPE_POINTER_TO (typ));
d11dd684
ZL
2293 /* NB: Types 'id' and 'Class' cannot reasonably be described as
2294 "implementing" a given protocol, since they do not have an
2295 implementation. */
f41c4af3
JM
2296 if (class_type)
2297 warning (0, "class %qs does not implement the %qE protocol",
2298 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
2299 else
2300 warning (0, "type %qs does not conform to the %qE protocol",
2301 identifier_to_locale (errbuf), PROTOCOL_NAME (proto));
3f16185f 2302 }
d11dd684
ZL
2303
2304 return false;
3f16185f
AM
2305}
2306
d11dd684
ZL
2307/* Check if class RCLS and instance struct type RTYP conform to at least the
2308 same protocols that LCLS and LTYP conform to. */
2309
2310static bool
2311objc_compare_protocols (tree lcls, tree ltyp, tree rcls, tree rtyp, bool warn)
2312{
2313 tree p;
2314 bool have_lproto = false;
2315
2316 while (lcls)
2317 {
2318 /* NB: We do _not_ look at categories defined for LCLS; these may or
2319 may not get loaded in, and therefore it is unreasonable to require
2320 that RCLS/RTYP must implement any of their protocols. */
2321 for (p = CLASS_PROTOCOL_LIST (lcls); p; p = TREE_CHAIN (p))
2322 {
2323 have_lproto = true;
3f16185f 2324
d11dd684
ZL
2325 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2326 return warn;
2327 }
1074d9d4 2328
d11dd684
ZL
2329 /* Repeat for superclasses. */
2330 lcls = lookup_interface (CLASS_SUPER_NAME (lcls));
2331 }
1074d9d4 2332
d11dd684
ZL
2333 /* Check for any protocols attached directly to the object type. */
2334 if (TYPE_HAS_OBJC_INFO (ltyp))
2335 {
2336 for (p = TYPE_OBJC_PROTOCOL_LIST (ltyp); p; p = TREE_CHAIN (p))
2337 {
2338 have_lproto = true;
392202b0 2339
d11dd684
ZL
2340 if (!objc_lookup_protocol (TREE_VALUE (p), rcls, rtyp, warn))
2341 return warn;
2342 }
2343 }
2344
2345 /* NB: If LTYP and LCLS have no protocols to search for, return 'true'
2346 vacuously, _unless_ RTYP is a protocol-qualified 'id'. We can get
2347 away with simply checking for 'id' or 'Class' (!RCLS), since this
2348 routine will not get called in other cases. */
2349 return have_lproto || (rcls != NULL_TREE);
2350}
2351
b581b85b
NP
2352/* Given two types TYPE1 and TYPE2, return their least common ancestor.
2353 Both TYPE1 and TYPE2 must be pointers, and already determined to be
2354 compatible by objc_compare_types() below. */
2355
2356tree
2357objc_common_type (tree type1, tree type2)
2358{
2359 tree inner1 = TREE_TYPE (type1), inner2 = TREE_TYPE (type2);
2360
2361 while (POINTER_TYPE_P (inner1))
2362 {
2363 inner1 = TREE_TYPE (inner1);
2364 inner2 = TREE_TYPE (inner2);
2365 }
2366
2367 /* If one type is derived from another, return the base type. */
2368 if (DERIVED_FROM_P (inner1, inner2))
2369 return type1;
2370 else if (DERIVED_FROM_P (inner2, inner1))
2371 return type2;
2372
2373 /* If both types are 'Class', return 'Class'. */
2374 if (objc_is_class_id (inner1) && objc_is_class_id (inner2))
2375 return objc_class_type;
2376
2377 /* Otherwise, return 'id'. */
2378 return objc_object_type;
2379}
2380
d11dd684
ZL
2381/* Determine if it is permissible to assign (if ARGNO is greater than -3)
2382 an instance of RTYP to an instance of LTYP or to compare the two
2383 (if ARGNO is equal to -3), per ObjC type system rules. Before
2384 returning 'true', this routine may issue warnings related to, e.g.,
2385 protocol conformance. When returning 'false', the routine must
2386 produce absolutely no warnings; the C or C++ front-end will do so
10e34e6e
NP
2387 instead, if needed. If either LTYP or RTYP is not an Objective-C
2388 type, the routine must return 'false'.
d11dd684
ZL
2389
2390 The ARGNO parameter is encoded as follows:
2391 >= 1 Parameter number (CALLEE contains function being called);
2392 0 Return value;
2393 -1 Assignment;
2394 -2 Initialization;
4c116505 2395 -3 Comparison (LTYP and RTYP may match in either direction);
10e34e6e 2396 -4 Silent comparison (for C++ overload resolution);
944fb799
MS
2397 -5 Silent "specialization" comparison for RTYP to be a "specialization"
2398 of LTYP (a specialization means that RTYP is LTYP plus some constraints,
10e34e6e
NP
2399 so that each object of type RTYP is also of type LTYP). This is used
2400 when comparing property types. */
d11dd684
ZL
2401
2402bool
2403objc_compare_types (tree ltyp, tree rtyp, int argno, tree callee)
6c65299b 2404{
d11dd684
ZL
2405 tree lcls, rcls, lproto, rproto;
2406 bool pointers_compatible;
2407
2408 /* We must be dealing with pointer types */
2409 if (!POINTER_TYPE_P (ltyp) || !POINTER_TYPE_P (rtyp))
2410 return false;
e31c7eec 2411
d11dd684 2412 do
e31c7eec 2413 {
d11dd684
ZL
2414 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2415 rtyp = TREE_TYPE (rtyp);
2416 }
2417 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
e31c7eec 2418
0f185d6e
NP
2419 /* We must also handle function pointers, since ObjC is a bit more
2420 lenient than C or C++ on this. */
2421 if (TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE)
2422 {
769b55b2
NF
2423 function_args_iterator liter, riter;
2424
0f185d6e
NP
2425 /* Return types must be covariant. */
2426 if (!comptypes (TREE_TYPE (ltyp), TREE_TYPE (rtyp))
2427 && !objc_compare_types (TREE_TYPE (ltyp), TREE_TYPE (rtyp),
2428 argno, callee))
2429 return false;
2430
2431 /* Argument types must be contravariant. */
769b55b2
NF
2432 function_args_iter_init (&liter, ltyp);
2433 function_args_iter_init (&riter, rtyp);
2434
2435 while (1)
0f185d6e 2436 {
769b55b2
NF
2437 ltyp = function_args_iter_cond (&liter);
2438 rtyp = function_args_iter_cond (&riter);
2439
2440 /* If we've exhaused both lists simulateously, we're done. */
2441 if (ltyp == NULL_TREE && rtyp == NULL_TREE)
2442 break;
2443
2444 /* If one list is shorter than the other, they fail to match. */
2445 if (ltyp == NULL_TREE || rtyp == NULL_TREE)
0f185d6e 2446 return false;
0f185d6e 2447
769b55b2
NF
2448 if (!comptypes (rtyp, ltyp)
2449 && !objc_compare_types (rtyp, ltyp, argno, callee))
2450 return false;
2451
2452 function_args_iter_next (&liter);
2453 function_args_iter_next (&riter);
2454 }
2455
2456 return true;
0f185d6e
NP
2457 }
2458
d11dd684
ZL
2459 /* Past this point, we are only interested in ObjC class instances,
2460 or 'id' or 'Class'. */
2461 if (TREE_CODE (ltyp) != RECORD_TYPE || TREE_CODE (rtyp) != RECORD_TYPE)
2462 return false;
e31c7eec 2463
d11dd684
ZL
2464 if (!objc_is_object_id (ltyp) && !objc_is_class_id (ltyp)
2465 && !TYPE_HAS_OBJC_INFO (ltyp))
2466 return false;
3a3589b4 2467
d11dd684
ZL
2468 if (!objc_is_object_id (rtyp) && !objc_is_class_id (rtyp)
2469 && !TYPE_HAS_OBJC_INFO (rtyp))
2470 return false;
011d50d9 2471
4c116505
NP
2472 /* Past this point, we are committed to returning 'true' to the caller
2473 (unless performing a silent comparison; see below). However, we can
2474 still warn about type and/or protocol mismatches. */
e31c7eec 2475
d11dd684
ZL
2476 if (TYPE_HAS_OBJC_INFO (ltyp))
2477 {
2478 lcls = TYPE_OBJC_INTERFACE (ltyp);
2479 lproto = TYPE_OBJC_PROTOCOL_LIST (ltyp);
2480 }
2481 else
2482 lcls = lproto = NULL_TREE;
e31c7eec 2483
d11dd684
ZL
2484 if (TYPE_HAS_OBJC_INFO (rtyp))
2485 {
2486 rcls = TYPE_OBJC_INTERFACE (rtyp);
2487 rproto = TYPE_OBJC_PROTOCOL_LIST (rtyp);
2488 }
2489 else
2490 rcls = rproto = NULL_TREE;
3a3589b4 2491
a0e71127
ZL
2492 /* If we could not find an @interface declaration, we must have
2493 only seen a @class declaration; for purposes of type comparison,
2494 treat it as a stand-alone (root) class. */
2495
2496 if (lcls && TREE_CODE (lcls) == IDENTIFIER_NODE)
2497 lcls = NULL_TREE;
2498
2499 if (rcls && TREE_CODE (rcls) == IDENTIFIER_NODE)
2500 rcls = NULL_TREE;
2501
10e34e6e
NP
2502 /* If either type is an unqualified 'id', we're done. This is because
2503 an 'id' can be assigned to or from any type with no warnings. */
2504 if (argno != -5)
2505 {
2506 if ((!lproto && objc_is_object_id (ltyp))
2507 || (!rproto && objc_is_object_id (rtyp)))
2508 return true;
2509 }
2510 else
2511 {
2512 /* For property checks, though, an 'id' is considered the most
2513 general type of object, hence if you try to specialize an
2514 'NSArray *' (ltyp) property with an 'id' (rtyp) one, we need
2515 to warn. */
2516 if (!lproto && objc_is_object_id (ltyp))
2517 return true;
2518 }
944fb799 2519
d11dd684 2520 pointers_compatible = (TYPE_MAIN_VARIANT (ltyp) == TYPE_MAIN_VARIANT (rtyp));
51900510 2521
d11dd684
ZL
2522 /* If the underlying types are the same, and at most one of them has
2523 a protocol list, we do not need to issue any diagnostics. */
2524 if (pointers_compatible && (!lproto || !rproto))
2525 return true;
3a3589b4 2526
d11dd684
ZL
2527 /* If exactly one of the types is 'Class', issue a diagnostic; any
2528 exceptions of this rule have already been handled. */
2529 if (objc_is_class_id (ltyp) ^ objc_is_class_id (rtyp))
2530 pointers_compatible = false;
2531 /* Otherwise, check for inheritance relations. */
2532 else
2533 {
2534 if (!pointers_compatible)
10e34e6e
NP
2535 {
2536 /* Again, if any of the two is an 'id', we're satisfied,
2537 unless we're comparing properties, in which case only an
2538 'id' on the left-hand side (old property) is good
2539 enough. */
2540 if (argno != -5)
2541 pointers_compatible
2542 = (objc_is_object_id (ltyp) || objc_is_object_id (rtyp));
2543 else
944fb799 2544 pointers_compatible = objc_is_object_id (ltyp);
10e34e6e 2545 }
d11dd684
ZL
2546
2547 if (!pointers_compatible)
2548 pointers_compatible = DERIVED_FROM_P (ltyp, rtyp);
2549
10e34e6e 2550 if (!pointers_compatible && (argno == -3 || argno == -4))
d11dd684
ZL
2551 pointers_compatible = DERIVED_FROM_P (rtyp, ltyp);
2552 }
2553
2554 /* If the pointers match modulo protocols, check for protocol conformance
2555 mismatches. */
2556 if (pointers_compatible)
2557 {
2558 pointers_compatible = objc_compare_protocols (lcls, ltyp, rcls, rtyp,
2559 argno != -3);
2560
2561 if (!pointers_compatible && argno == -3)
2562 pointers_compatible = objc_compare_protocols (rcls, rtyp, lcls, ltyp,
2563 argno != -3);
2564 }
2565
2566 if (!pointers_compatible)
2567 {
4c116505
NP
2568 /* The two pointers are not exactly compatible. Issue a warning, unless
2569 we are performing a silent comparison, in which case return 'false'
2570 instead. */
d11dd684
ZL
2571 /* NB: For the time being, we shall make our warnings look like their
2572 C counterparts. In the future, we may wish to make them more
2573 ObjC-specific. */
2574 switch (argno)
1074d9d4 2575 {
10e34e6e 2576 case -5:
4c116505
NP
2577 case -4:
2578 return false;
2579
d11dd684
ZL
2580 case -3:
2581 warning (0, "comparison of distinct Objective-C types lacks a cast");
2582 break;
2583
2584 case -2:
2585 warning (0, "initialization from distinct Objective-C type");
2586 break;
2587
2588 case -1:
2589 warning (0, "assignment from distinct Objective-C type");
2590 break;
2591
2592 case 0:
2593 warning (0, "distinct Objective-C type in return");
2594 break;
2595
2596 default:
2597 warning (0, "passing argument %d of %qE from distinct "
2598 "Objective-C type", argno, callee);
2599 break;
1074d9d4 2600 }
e31c7eec 2601 }
51900510 2602
d11dd684
ZL
2603 return true;
2604}
2605
b581b85b
NP
2606/* This routine is similar to objc_compare_types except that function-pointers are
2607 excluded. This is because, caller assumes that common types are of (id, Object*)
2608 variety and calls objc_common_type to obtain a common type. There is no commonolty
2609 between two function-pointers in this regard. */
2610
944fb799 2611bool
b581b85b
NP
2612objc_have_common_type (tree ltyp, tree rtyp, int argno, tree callee)
2613{
2614 if (objc_compare_types (ltyp, rtyp, argno, callee))
2615 {
2616 /* exclude function-pointer types. */
2617 do
2618 {
2619 ltyp = TREE_TYPE (ltyp); /* Remove indirections. */
2620 rtyp = TREE_TYPE (rtyp);
2621 }
2622 while (POINTER_TYPE_P (ltyp) && POINTER_TYPE_P (rtyp));
2623 return !(TREE_CODE (ltyp) == FUNCTION_TYPE && TREE_CODE (rtyp) == FUNCTION_TYPE);
2624 }
2625 return false;
2626}
2627
d11dd684
ZL
2628#ifndef OBJCPLUS
2629/* Determine if CHILD is derived from PARENT. The routine assumes that
2630 both parameters are RECORD_TYPEs, and is non-reflexive. */
2631
2632static bool
2633objc_derived_from_p (tree parent, tree child)
2634{
2635 parent = TYPE_MAIN_VARIANT (parent);
2636
2637 for (child = TYPE_MAIN_VARIANT (child);
2638 TYPE_BINFO (child) && BINFO_N_BASE_BINFOS (TYPE_BINFO (child));)
1074d9d4 2639 {
d11dd684
ZL
2640 child = TYPE_MAIN_VARIANT (BINFO_TYPE (BINFO_BASE_BINFO
2641 (TYPE_BINFO (child),
2642 0)));
2643
2644 if (child == parent)
2645 return true;
1074d9d4 2646 }
6c65299b 2647
d11dd684
ZL
2648 return false;
2649}
2650#endif
2651
d764a8e6 2652tree
d11dd684
ZL
2653objc_build_component_ref (tree datum, tree component)
2654{
2655 /* If COMPONENT is NULL, the caller is referring to the anonymous
2656 base class field. */
2657 if (!component)
2658 {
2659 tree base = TYPE_FIELDS (TREE_TYPE (datum));
6c65299b 2660
d11dd684
ZL
2661 return build3 (COMPONENT_REF, TREE_TYPE (base), datum, base, NULL_TREE);
2662 }
6c65299b 2663
d11dd684
ZL
2664 /* The 'build_component_ref' routine has been removed from the C++
2665 front-end, but 'finish_class_member_access_expr' seems to be
2666 a worthy substitute. */
2667#ifdef OBJCPLUS
525521b6
DG
2668 return finish_class_member_access_expr (datum, component, false,
2669 tf_warning_or_error);
d11dd684 2670#else
c2255bc4 2671 return build_component_ref (input_location, datum, component);
d11dd684
ZL
2672#endif
2673}
6c65299b 2674
d11dd684
ZL
2675/* Recursively copy inheritance information rooted at BINFO. To do this,
2676 we emulate the song and dance performed by cp/tree.c:copy_binfo(). */
f75963f5 2677
d11dd684
ZL
2678static tree
2679objc_copy_binfo (tree binfo)
2680{
2681 tree btype = BINFO_TYPE (binfo);
2682 tree binfo2 = make_tree_binfo (BINFO_N_BASE_BINFOS (binfo));
2683 tree base_binfo;
2684 int ix;
6c65299b 2685
d11dd684
ZL
2686 BINFO_TYPE (binfo2) = btype;
2687 BINFO_OFFSET (binfo2) = BINFO_OFFSET (binfo);
2688 BINFO_BASE_ACCESSES (binfo2) = BINFO_BASE_ACCESSES (binfo);
2689
2690 /* Recursively copy base binfos of BINFO. */
2691 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
6c65299b 2692 {
d11dd684
ZL
2693 tree base_binfo2 = objc_copy_binfo (base_binfo);
2694
2695 BINFO_INHERITANCE_CHAIN (base_binfo2) = binfo2;
2696 BINFO_BASE_APPEND (binfo2, base_binfo2);
2697 }
2698
2699 return binfo2;
2700}
2701
2702/* Record superclass information provided in BASETYPE for ObjC class REF.
2703 This is loosely based on cp/decl.c:xref_basetypes(). */
6c65299b 2704
d11dd684
ZL
2705static void
2706objc_xref_basetypes (tree ref, tree basetype)
2707{
ba429440 2708 tree variant;
d11dd684 2709 tree binfo = make_tree_binfo (basetype ? 1 : 0);
d11dd684
ZL
2710 TYPE_BINFO (ref) = binfo;
2711 BINFO_OFFSET (binfo) = size_zero_node;
2712 BINFO_TYPE (binfo) = ref;
e31c7eec 2713
ba429440
JH
2714 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
2715 for (variant = ref; variant; variant = TYPE_NEXT_VARIANT (variant))
2716 TYPE_BINFO (variant) = binfo;
2717
d11dd684
ZL
2718 if (basetype)
2719 {
2720 tree base_binfo = objc_copy_binfo (TYPE_BINFO (basetype));
e31c7eec 2721
d11dd684 2722 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
9771b263 2723 vec_alloc (BINFO_BASE_ACCESSES (binfo), 1);
d11dd684
ZL
2724 BINFO_BASE_APPEND (binfo, base_binfo);
2725 BINFO_BASE_ACCESS_APPEND (binfo, access_public_node);
6c65299b 2726 }
d11dd684
ZL
2727}
2728
264fa2db 2729/* Called from finish_decl. */
6c65299b 2730
e31c7eec 2731void
011d50d9 2732objc_check_decl (tree decl)
e31c7eec
TW
2733{
2734 tree type = TREE_TYPE (decl);
2735
264fa2db
ZL
2736 if (TREE_CODE (type) != RECORD_TYPE)
2737 return;
57a6af27 2738 if (OBJC_TYPE_NAME (type) && (type = objc_is_class_name (OBJC_TYPE_NAME (type))))
f41c4af3
JM
2739 error ("statically allocated instance of Objective-C class %qE",
2740 type);
e31c7eec
TW
2741}
2742
a6341d57
NP
2743void
2744objc_check_global_decl (tree decl)
2745{
2746 tree id = DECL_NAME (decl);
2747 if (objc_is_class_name (id) && global_bindings_p())
2748 error ("redeclaration of Objective-C class %qs", IDENTIFIER_POINTER (id));
2749}
2750
a693d3a8
NP
2751/* Construct a PROTOCOLS-qualified variant of INTERFACE, where
2752 INTERFACE may either name an Objective-C class, or refer to the
2753 special 'id' or 'Class' types. If INTERFACE is not a valid ObjC
2754 type, just return it unchanged. This function is often called when
2755 PROTOCOLS is NULL_TREE, in which case we simply look up the
2756 appropriate INTERFACE. */
6c65299b
RS
2757
2758tree
6408ef33 2759objc_get_protocol_qualified_type (tree interface, tree protocols)
e31c7eec 2760{
035e8f01
ZL
2761 /* If INTERFACE is not provided, default to 'id'. */
2762 tree type = (interface ? objc_is_id (interface) : objc_object_type);
2763 bool is_ptr = (type != NULL_TREE);
e31c7eec 2764
035e8f01 2765 if (!is_ptr)
e31c7eec 2766 {
6408ef33 2767 type = objc_is_class_name (interface);
e31c7eec 2768
6408ef33 2769 if (type)
0f185d6e
NP
2770 {
2771 /* If looking at a typedef, retrieve the precise type it
2772 describes. */
2773 if (TREE_CODE (interface) == IDENTIFIER_NODE)
2774 interface = identifier_global_value (interface);
2775
2776 type = ((interface && TREE_CODE (interface) == TYPE_DECL
2777 && DECL_ORIGINAL_TYPE (interface))
2778 ? DECL_ORIGINAL_TYPE (interface)
2779 : xref_tag (RECORD_TYPE, type));
2780 }
6408ef33 2781 else
5a2a6eb0
NP
2782 {
2783 /* This case happens when we are given an 'interface' which
2784 is not a valid class name. For example if a typedef was
2785 used, and 'interface' really is the identifier of the
2786 typedef, but when you resolve it you don't get an
2787 Objective-C class, but something else, such as 'int'.
2788 This is an error; protocols make no sense unless you use
2789 them with Objective-C objects. */
2790 error_at (input_location, "only Objective-C object types can be qualified with a protocol");
2791
2792 /* Try to recover. Ignore the invalid class name, and treat
2793 the object as an 'id' to silence further warnings about
2794 the class. */
2795 type = objc_object_type;
2796 is_ptr = true;
2797 }
6408ef33 2798 }
51900510 2799
e31c7eec
TW
2800 if (protocols)
2801 {
8c1d6d62 2802 type = build_variant_type_copy (type);
6408ef33 2803
035e8f01
ZL
2804 /* For pointers (i.e., 'id' or 'Class'), attach the protocol(s)
2805 to the pointee. */
2806 if (is_ptr)
2807 {
06d40de8
DG
2808 tree orig_pointee_type = TREE_TYPE (type);
2809 TREE_TYPE (type) = build_variant_type_copy (orig_pointee_type);
2810
2811 /* Set up the canonical type information. */
944fb799 2812 TYPE_CANONICAL (type)
06d40de8
DG
2813 = TYPE_CANONICAL (TYPE_POINTER_TO (orig_pointee_type));
2814
035e8f01
ZL
2815 TYPE_POINTER_TO (TREE_TYPE (type)) = type;
2816 type = TREE_TYPE (type);
2817 }
2818
2819 /* Look up protocols and install in lang specific list. */
2820 DUP_TYPE_OBJC_INFO (type, TYPE_MAIN_VARIANT (type));
db0581ae
NP
2821 TYPE_OBJC_PROTOCOL_LIST (type) = lookup_and_install_protocols
2822 (protocols, /* definition_required */ false);
035e8f01
ZL
2823
2824 /* For RECORD_TYPEs, point to the @interface; for 'id' and 'Class',
2825 return the pointer to the new pointee variant. */
2826 if (is_ptr)
2827 type = TYPE_POINTER_TO (type);
2828 else
2829 TYPE_OBJC_INTERFACE (type)
2830 = TYPE_OBJC_INTERFACE (TYPE_MAIN_VARIANT (type));
e31c7eec 2831 }
6408ef33 2832
e31c7eec
TW
2833 return type;
2834}
2835
f2e6e530
ZL
2836/* Check for circular dependencies in protocols. The arguments are
2837 PROTO, the protocol to check, and LIST, a list of protocol it
2838 conforms to. */
2839
011d50d9
AJ
2840static void
2841check_protocol_recursively (tree proto, tree list)
f2e6e530
ZL
2842{
2843 tree p;
2844
2845 for (p = list; p; p = TREE_CHAIN (p))
2846 {
2847 tree pp = TREE_VALUE (p);
2848
2849 if (TREE_CODE (pp) == IDENTIFIER_NODE)
db0581ae
NP
2850 pp = lookup_protocol (pp, /* warn if deprecated */ false,
2851 /* definition_required */ false);
f2e6e530
ZL
2852
2853 if (pp == proto)
40fecdd6 2854 fatal_error (input_location, "protocol %qE has circular dependency",
f41c4af3 2855 PROTOCOL_NAME (pp));
f2e6e530
ZL
2856 if (pp)
2857 check_protocol_recursively (proto, PROTOCOL_LIST (pp));
2858 }
2859}
2860
6b192a09
NP
2861/* Look up PROTOCOLS, and return a list of those that are found. If
2862 none are found, return NULL. Note that this function will emit a
db0581ae
NP
2863 warning if a protocol is found and is deprecated. If
2864 'definition_required', then warn if the protocol is found but is
2865 not defined (ie, if we only saw a forward-declaration of the
2866 protocol (as in "@protocol NSObject;") not a real definition with
2867 the list of methods). */
e31c7eec 2868static tree
db0581ae 2869lookup_and_install_protocols (tree protocols, bool definition_required)
6c65299b 2870{
e31c7eec 2871 tree proto;
264fa2db 2872 tree return_value = NULL_TREE;
e31c7eec 2873
da57d1b9
NP
2874 if (protocols == error_mark_node)
2875 return NULL;
2876
e31c7eec
TW
2877 for (proto = protocols; proto; proto = TREE_CHAIN (proto))
2878 {
2879 tree ident = TREE_VALUE (proto);
db0581ae
NP
2880 tree p = lookup_protocol (ident, /* warn_if_deprecated */ true,
2881 definition_required);
e31c7eec 2882
7eb314dc 2883 if (p)
264fa2db
ZL
2884 return_value = chainon (return_value,
2885 build_tree_list (NULL_TREE, p));
7eb314dc 2886 else if (ident != error_mark_node)
f41c4af3
JM
2887 error ("cannot find protocol declaration for %qE",
2888 ident);
e31c7eec 2889 }
51900510 2890
e31c7eec 2891 return return_value;
6c65299b
RS
2892}
2893
fa340504 2894static void
d764a8e6 2895build_common_objc_exception_stuff (void)
6408ef33 2896{
d764a8e6 2897 tree noreturn_list, nothrow_list, temp_type;
6408ef33 2898
d764a8e6
IS
2899 noreturn_list = tree_cons (get_identifier ("noreturn"), NULL, NULL);
2900 nothrow_list = tree_cons (get_identifier ("nothrow"), NULL, NULL);
6408ef33 2901
d764a8e6
IS
2902 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2903 /* void objc_sync_enter(id); */
2904 /* void objc_sync_exit(id); */
2905 temp_type = build_function_type_list (void_type_node,
2906 objc_object_type,
2907 NULL_TREE);
2908 objc_exception_throw_decl
2909 = add_builtin_function (TAG_EXCEPTIONTHROW, temp_type, 0, NOT_BUILT_IN, NULL,
2910 noreturn_list);
2911 /* Make sure that objc_exception_throw (id) claims that it may throw an
2912 exception. */
2913 TREE_NOTHROW (objc_exception_throw_decl) = 0;
6408ef33 2914
d764a8e6
IS
2915 objc_sync_enter_decl
2916 = add_builtin_function (TAG_SYNCENTER, temp_type, 0, NOT_BUILT_IN,
2917 NULL, nothrow_list);
6408ef33 2918
d764a8e6
IS
2919 objc_sync_exit_decl
2920 = add_builtin_function (TAG_SYNCEXIT, temp_type, 0, NOT_BUILT_IN,
2921 NULL, nothrow_list);
fa340504
SS
2922}
2923
51900510 2924/* Purpose: "play" parser, creating/installing representations
e31c7eec
TW
2925 of the declarations that are required by Objective-C.
2926
51900510 2927 Model:
e31c7eec 2928
011d50d9
AJ
2929 type_spec--------->sc_spec
2930 (tree_list) (tree_list)
2931 | |
2932 | |
2933 identifier_node identifier_node */
e31c7eec 2934
6c65299b 2935static void
011d50d9 2936synth_module_prologue (void)
6c65299b 2937{
6408ef33
ZL
2938 tree type;
2939 enum debug_info_type save_write_symbols = write_symbols;
2940 const struct gcc_debug_hooks *const save_hooks = debug_hooks;
2941
2942 /* Suppress outputting debug symbols, because
39a13be5 2943 dbxout_init hasn't been called yet. */
6408ef33
ZL
2944 write_symbols = NO_DEBUG;
2945 debug_hooks = &do_nothing_debug_hooks;
2946
2947#ifdef OBJCPLUS
2948 push_lang_context (lang_name_c); /* extern "C" */
2949#endif
2950
2951 /* The following are also defined in <objc/objc.h> and friends. */
6c65299b 2952
6c65299b 2953 objc_object_id = get_identifier (TAG_OBJECT);
6408ef33 2954 objc_class_id = get_identifier (TAG_CLASS);
6c65299b
RS
2955
2956 objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
6408ef33 2957 objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
64ee9490 2958
6408ef33
ZL
2959 objc_object_type = build_pointer_type (objc_object_reference);
2960 objc_class_type = build_pointer_type (objc_class_reference);
6c65299b 2961
6408ef33
ZL
2962 objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
2963 objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
6c65299b 2964
6408ef33 2965 /* Declare the 'id' and 'Class' typedefs. */
c2255bc4
AH
2966 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2967 TYPE_DECL,
6408ef33
ZL
2968 objc_object_name,
2969 objc_object_type));
df0ed6c5 2970 TREE_NO_WARNING (type) = 1;
d764a8e6 2971
c2255bc4
AH
2972 type = lang_hooks.decls.pushdecl (build_decl (input_location,
2973 TYPE_DECL,
6408ef33
ZL
2974 objc_class_name,
2975 objc_class_type));
df0ed6c5 2976 TREE_NO_WARNING (type) = 1;
6408ef33
ZL
2977
2978 /* Forward-declare '@interface Protocol'. */
6408ef33 2979 type = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
32dabdaf 2980 objc_declare_class (type);
d764a8e6 2981 objc_protocol_type = build_pointer_type (xref_tag (RECORD_TYPE, type));
6c65299b 2982
1a4a7065 2983 /* Declare receiver type used for dispatching messages to 'super'. */
264fa2db 2984 /* `struct objc_super *' */
5c234cd7
DA
2985 objc_super_type = build_pointer_type (xref_tag (RECORD_TYPE,
2986 get_identifier (TAG_SUPER)));
e31c7eec 2987
38b9c8c3
ZL
2988 /* Declare pointers to method and ivar lists. */
2989 objc_method_list_ptr = build_pointer_type
2990 (xref_tag (RECORD_TYPE,
2991 get_identifier (UTAG_METHOD_LIST)));
2992 objc_method_proto_list_ptr
2993 = build_pointer_type (xref_tag (RECORD_TYPE,
2994 get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
2995 objc_ivar_list_ptr = build_pointer_type
2996 (xref_tag (RECORD_TYPE,
2997 get_identifier (UTAG_IVAR_LIST)));
2998
d764a8e6 2999 build_common_objc_exception_stuff ();
6c65299b 3000
d764a8e6
IS
3001 /* Set-up runtime-specific templates, message and exception stuff. */
3002 (*runtime.initialize) ();
6408ef33 3003
8f07a2aa
NP
3004 /* Declare objc_getProperty, object_setProperty and other property
3005 accessor helpers. */
d764a8e6 3006 build_common_objc_property_accessor_helpers ();
e31c7eec 3007
e31c7eec 3008 /* Forward declare constant_string_id and constant_string_type. */
fda47030 3009 if (!constant_string_class_name)
d764a8e6 3010 constant_string_class_name = runtime.default_constant_string_class_name;
fda47030 3011 constant_string_id = get_identifier (constant_string_class_name);
32dabdaf 3012 objc_declare_class (constant_string_id);
264fa2db
ZL
3013
3014 /* Pre-build the following entities - for speed/convenience. */
3015 self_id = get_identifier ("self");
3016 ucmd_id = get_identifier ("_cmd");
6408ef33 3017
f05b9d93
NP
3018 /* Declare struct _objc_fast_enumeration_state { ... }; */
3019 build_fast_enumeration_state_template ();
944fb799 3020
f05b9d93 3021 /* void objc_enumeration_mutation (id) */
491add72
NF
3022 type = build_function_type_list (void_type_node,
3023 objc_object_type, NULL_TREE);
944fb799
MS
3024 objc_enumeration_mutation_decl
3025 = add_builtin_function (TAG_ENUMERATION_MUTATION, type, 0, NOT_BUILT_IN,
f05b9d93
NP
3026 NULL, NULL_TREE);
3027 TREE_NOTHROW (objc_enumeration_mutation_decl) = 0;
3028
6408ef33
ZL
3029#ifdef OBJCPLUS
3030 pop_lang_context ();
3031#endif
3032
3033 write_symbols = save_write_symbols;
3034 debug_hooks = save_hooks;
6c65299b
RS
3035}
3036
d764a8e6
IS
3037/* --- const strings --- */
3038
264fa2db
ZL
3039/* Ensure that the ivar list for NSConstantString/NXConstantString
3040 (or whatever was specified via `-fconstant-string-class')
3041 contains fields at least as large as the following three, so that
3042 the runtime can stomp on them with confidence:
c66c59d1 3043
011d50d9 3044 struct STRING_OBJECT_CLASS_NAME
c66c59d1
RH
3045 {
3046 Object isa;
3047 char *cString;
3048 unsigned int length;
3049 }; */
3050
264fa2db
ZL
3051static int
3052check_string_class_template (void)
c66c59d1 3053{
c64de75f 3054 tree field_decl = objc_get_class_ivars (constant_string_id);
c66c59d1 3055
264fa2db
ZL
3056#define AT_LEAST_AS_LARGE_AS(F, T) \
3057 (F && TREE_CODE (F) == FIELD_DECL \
c64de75f 3058 && (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (F))) \
264fa2db 3059 >= TREE_INT_CST_LOW (TYPE_SIZE (T))))
c66c59d1 3060
264fa2db
ZL
3061 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3062 return 0;
c66c59d1 3063
910ad8de 3064 field_decl = DECL_CHAIN (field_decl);
264fa2db
ZL
3065 if (!AT_LEAST_AS_LARGE_AS (field_decl, ptr_type_node))
3066 return 0;
3067
910ad8de 3068 field_decl = DECL_CHAIN (field_decl);
264fa2db 3069 return AT_LEAST_AS_LARGE_AS (field_decl, unsigned_type_node);
c66c59d1 3070
264fa2db 3071#undef AT_LEAST_AS_LARGE_AS
c66c59d1
RH
3072}
3073
264fa2db
ZL
3074/* Avoid calling `check_string_class_template ()' more than once. */
3075static GTY(()) int string_layout_checked;
3076
c64de75f
ZL
3077/* Construct an internal string layout to be used as a template for
3078 creating NSConstantString/NXConstantString instances. */
3079
3080static tree
3081objc_build_internal_const_str_type (void)
3082{
3083 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
c2255bc4
AH
3084 tree fields = build_decl (input_location,
3085 FIELD_DECL, NULL_TREE, ptr_type_node);
3086 tree field = build_decl (input_location,
3087 FIELD_DECL, NULL_TREE, ptr_type_node);
c64de75f 3088
910ad8de 3089 DECL_CHAIN (field) = fields; fields = field;
c2255bc4
AH
3090 field = build_decl (input_location,
3091 FIELD_DECL, NULL_TREE, unsigned_type_node);
910ad8de 3092 DECL_CHAIN (field) = fields; fields = field;
c64de75f
ZL
3093 /* NB: The finish_builtin_struct() routine expects FIELD_DECLs in
3094 reverse order! */
3095 finish_builtin_struct (type, "__builtin_ObjCString",
3096 fields, NULL_TREE);
3097
3098 return type;
3099}
3100
e31c7eec
TW
3101/* Custom build_string which sets TREE_TYPE! */
3102
d764a8e6 3103tree
011d50d9 3104my_build_string (int len, const char *str)
6c65299b 3105{
b84a3874 3106 return fix_string_type (build_string (len, str));
e31c7eec
TW
3107}
3108
46bdb9cf
JM
3109/* Build a string with contents STR and length LEN and convert it to a
3110 pointer. */
3111
d764a8e6 3112tree
46bdb9cf
JM
3113my_build_string_pointer (int len, const char *str)
3114{
3115 tree string = my_build_string (len, str);
3116 tree ptrtype = build_pointer_type (TREE_TYPE (TREE_TYPE (string)));
3117 return build1 (ADDR_EXPR, ptrtype, string);
3118}
6408ef33 3119
2a22f99c
TS
3120hashval_t
3121objc_string_hasher::hash (string_descriptor *ptr)
6408ef33 3122{
2a22f99c 3123 const_tree const str = ptr->literal;
6408ef33
ZL
3124 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3125 int i, len = TREE_STRING_LENGTH (str);
3126 hashval_t h = len;
3127
3128 for (i = 0; i < len; i++)
3129 h = ((h * 613) + p[i]);
3130
3131 return h;
3132}
3133
2a22f99c
TS
3134bool
3135objc_string_hasher::equal (string_descriptor *ptr1, string_descriptor *ptr2)
6408ef33 3136{
2a22f99c
TS
3137 const_tree const str1 = ptr1->literal;
3138 const_tree const str2 = ptr2->literal;
6408ef33
ZL
3139 int len1 = TREE_STRING_LENGTH (str1);
3140
3141 return (len1 == TREE_STRING_LENGTH (str2)
3142 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3143 len1));
3144}
3145
264fa2db
ZL
3146/* Given a chain of STRING_CST's, build a static instance of
3147 NXConstantString which points at the concatenation of those
3148 strings. We place the string object in the __string_objects
3149 section of the __OBJC segment. The Objective-C runtime will
3150 initialize the isa pointers of the string objects to point at the
3151 NXConstantString class object. */
e31c7eec
TW
3152
3153tree
57a6af27 3154objc_build_string_object (tree string)
e31c7eec 3155{
26705988 3156 tree constant_string_class;
e31c7eec 3157 int length;
d764a8e6 3158 tree addr;
6408ef33 3159 struct string_descriptor *desc, key;
e31c7eec 3160
f439d6ba
IS
3161 /* We should be passed a STRING_CST. */
3162 gcc_checking_assert (TREE_CODE (string) == STRING_CST);
e31c7eec
TW
3163 length = TREE_STRING_LENGTH (string) - 1;
3164
944fb799
MS
3165 /* The target may have different ideas on how to construct an ObjC string
3166 literal. On Darwin (Mac OS X), for example, we may wish to obtain a
26705988
IS
3167 constant CFString reference instead.
3168 At present, this is only supported for the NeXT runtime. */
944fb799 3169 if (flag_next_runtime
d764a8e6 3170 && targetcm.objc_construct_string_object)
26705988 3171 {
91ebb981 3172 tree constructor = (*targetcm.objc_construct_string_object) (string);
26705988
IS
3173 if (constructor)
3174 return build1 (NOP_EXPR, objc_object_type, constructor);
3175 }
3176
6408ef33
ZL
3177 /* Check whether the string class being used actually exists and has the
3178 correct ivar layout. */
264fa2db
ZL
3179 if (!string_layout_checked)
3180 {
6408ef33
ZL
3181 string_layout_checked = -1;
3182 constant_string_class = lookup_interface (constant_string_id);
c64de75f 3183 internal_const_str_type = objc_build_internal_const_str_type ();
6408ef33
ZL
3184
3185 if (!constant_string_class
3186 || !(constant_string_type
3187 = CLASS_STATIC_TEMPLATE (constant_string_class)))
f41c4af3
JM
3188 error ("cannot find interface declaration for %qE",
3189 constant_string_id);
6408ef33
ZL
3190 /* The NSConstantString/NXConstantString ivar layout is now known. */
3191 else if (!check_string_class_template ())
f41c4af3
JM
3192 error ("interface %qE does not have valid constant string layout",
3193 constant_string_id);
d764a8e6
IS
3194 /* If the runtime can generate a literal reference to the string class,
3195 don't need to run a constructor. */
3196 else if (!(*runtime.setup_const_string_class_decl)())
3197 error ("cannot find reference tag for class %qE", constant_string_id);
6408ef33 3198 else
fa340504 3199 {
6408ef33
ZL
3200 string_layout_checked = 1; /* Success! */
3201 add_class_reference (constant_string_id);
fa340504 3202 }
fa340504
SS
3203 }
3204
6408ef33
ZL
3205 if (string_layout_checked == -1)
3206 return error_mark_node;
89ef1046 3207
6408ef33
ZL
3208 /* Perhaps we already constructed a constant string just like this one? */
3209 key.literal = string;
2a22f99c
TS
3210 string_descriptor **loc = string_htab->find_slot (&key, INSERT);
3211 desc = *loc;
6408ef33
ZL
3212
3213 if (!desc)
51900510 3214 {
766090c2 3215 *loc = desc = ggc_alloc<string_descriptor> ();
6408ef33 3216 desc->literal = string;
944fb799 3217 desc->constructor =
d764a8e6 3218 (*runtime.build_const_string_constructor) (input_location, string, length);
51900510
RK
3219 }
3220
c64de75f 3221 addr = convert (build_pointer_type (constant_string_type),
c9f9eb5d
AH
3222 build_unary_op (input_location,
3223 ADDR_EXPR, desc->constructor, 1));
6408ef33
ZL
3224
3225 return addr;
4cb8c14b
RK
3226}
3227
89ef1046
RS
3228/* Build a static constant CONSTRUCTOR
3229 with type TYPE and elements ELTS. */
3230
d764a8e6 3231tree
9771b263 3232objc_build_constructor (tree type, vec<constructor_elt, va_gc> *elts)
89ef1046 3233{
076a7055 3234 tree constructor = build_constructor (type, elts);
ff1c8a1a 3235
e31c7eec
TW
3236 TREE_CONSTANT (constructor) = 1;
3237 TREE_STATIC (constructor) = 1;
3238 TREE_READONLY (constructor) = 1;
3239
264fa2db 3240#ifdef OBJCPLUS
6408ef33
ZL
3241 /* Adjust for impedance mismatch. We should figure out how to build
3242 CONSTRUCTORs that consistently please both the C and C++ gods. */
9771b263 3243 if (!(*elts)[0].index)
7ce841d2 3244 TREE_TYPE (constructor) = init_list_type_node;
264fa2db 3245#endif
6408ef33 3246
89ef1046 3247 return constructor;
6c65299b 3248}
af35aeb2 3249
d764a8e6 3250/* Return the DECL of the string IDENT in the SECTION. */
6c65299b 3251
d764a8e6
IS
3252tree
3253get_objc_string_decl (tree ident, enum string_section section)
6c65299b 3254{
d764a8e6 3255 tree chain;
6c65299b 3256
d764a8e6 3257 switch (section)
264fa2db 3258 {
d764a8e6
IS
3259 case class_names:
3260 chain = class_names_chain;
3261 break;
3262 case meth_var_names:
3263 chain = meth_var_names_chain;
3264 break;
3265 case meth_var_types:
3266 chain = meth_var_types_chain;
3267 break;
3268 case prop_names_attr:
3269 chain = prop_names_attr_chain;
3270 break;
3271 default:
3272 gcc_unreachable ();
264fa2db 3273 }
6c65299b 3274
d764a8e6
IS
3275 for (; chain != 0; chain = TREE_CHAIN (chain))
3276 if (TREE_VALUE (chain) == ident)
3277 return (TREE_PURPOSE (chain));
3278
3279 /* We didn't find the entry. */
3280 return NULL_TREE;
6c65299b
RS
3281}
3282
d764a8e6
IS
3283/* Create a class reference, but don't create a variable to reference
3284 it. */
af35aeb2 3285
d764a8e6
IS
3286void
3287add_class_reference (tree ident)
6c65299b 3288{
d764a8e6 3289 tree chain;
6c65299b 3290
d764a8e6 3291 if ((chain = cls_ref_chain))
e335b3ee 3292 {
d764a8e6
IS
3293 tree tail;
3294 do
3295 {
3296 if (ident == TREE_VALUE (chain))
3297 return;
e335b3ee 3298
d764a8e6
IS
3299 tail = chain;
3300 chain = TREE_CHAIN (chain);
3301 }
3302 while (chain);
e335b3ee 3303
d764a8e6
IS
3304 /* Append to the end of the list */
3305 TREE_CHAIN (tail) = tree_cons (NULL_TREE, ident, NULL_TREE);
3306 }
3307 else
3308 cls_ref_chain = tree_cons (NULL_TREE, ident, NULL_TREE);
6c65299b
RS
3309}
3310
d764a8e6
IS
3311/* Get a class reference, creating it if necessary. Also create the
3312 reference variable. */
3313tree
3314objc_get_class_reference (tree ident)
6c65299b 3315{
d764a8e6
IS
3316 tree orig_ident = (DECL_P (ident)
3317 ? DECL_NAME (ident)
3318 : TYPE_P (ident)
3319 ? OBJC_TYPE_NAME (ident)
3320 : ident);
3321 bool local_scope = false;
6c65299b 3322
d764a8e6
IS
3323#ifdef OBJCPLUS
3324 if (processing_template_decl)
3325 /* Must wait until template instantiation time. */
d7faa110 3326 return build_min_nt_loc (UNKNOWN_LOCATION, CLASS_REFERENCE_EXPR, ident);
d764a8e6 3327#endif
6c65299b 3328
d764a8e6
IS
3329 if (TREE_CODE (ident) == TYPE_DECL)
3330 ident = (DECL_ORIGINAL_TYPE (ident)
3331 ? DECL_ORIGINAL_TYPE (ident)
3332 : TREE_TYPE (ident));
6c65299b 3333
d764a8e6
IS
3334#ifdef OBJCPLUS
3335 if (TYPE_P (ident)
3336 && CP_TYPE_CONTEXT (ident) != global_namespace)
3337 local_scope = true;
3338#endif
6c65299b 3339
d764a8e6 3340 if (local_scope || !(ident = objc_is_class_name (ident)))
076a7055 3341 {
d764a8e6
IS
3342 error ("%qE is not an Objective-C class name or alias",
3343 orig_ident);
3344 return error_mark_node;
076a7055 3345 }
6c65299b 3346
d764a8e6
IS
3347 return (*runtime.get_class_reference) (ident);
3348}
6c65299b 3349
d764a8e6
IS
3350void
3351objc_declare_alias (tree alias_ident, tree class_ident)
3352{
3353 tree underlying_class;
6c65299b 3354
d764a8e6
IS
3355#ifdef OBJCPLUS
3356 if (current_namespace != global_namespace) {
3357 error ("Objective-C declarations may only appear in global scope");
3358 }
3359#endif /* OBJCPLUS */
6c65299b 3360
d764a8e6
IS
3361 if (!(underlying_class = objc_is_class_name (class_ident)))
3362 warning (0, "cannot find class %qE", class_ident);
3363 else if (objc_is_class_name (alias_ident))
3364 warning (0, "class %qE already exists", alias_ident);
3365 else
89ef1046 3366 {
d764a8e6
IS
3367 /* Implement @compatibility_alias as a typedef. */
3368#ifdef OBJCPLUS
3369 push_lang_context (lang_name_c); /* extern "C" */
3370#endif
3371 lang_hooks.decls.pushdecl (build_decl
3372 (input_location,
3373 TYPE_DECL,
3374 alias_ident,
3375 xref_tag (RECORD_TYPE, underlying_class)));
3376#ifdef OBJCPLUS
3377 pop_lang_context ();
3378#endif
3cc2dd4b 3379 objc_map_put (alias_name_map, alias_ident, underlying_class);
89ef1046 3380 }
6c65299b
RS
3381}
3382
d764a8e6 3383void
32dabdaf 3384objc_declare_class (tree identifier)
264fa2db 3385{
d764a8e6
IS
3386#ifdef OBJCPLUS
3387 if (current_namespace != global_namespace) {
3388 error ("Objective-C declarations may only appear in global scope");
3389 }
3390#endif /* OBJCPLUS */
6408ef33 3391
32dabdaf 3392 if (! objc_is_class_name (identifier))
d764a8e6 3393 {
32dabdaf 3394 tree record = lookup_name (identifier), type = record;
944fb799 3395
32dabdaf 3396 if (record)
d764a8e6 3397 {
32dabdaf
NP
3398 if (TREE_CODE (record) == TYPE_DECL)
3399 type = DECL_ORIGINAL_TYPE (record)
3400 ? DECL_ORIGINAL_TYPE (record)
3401 : TREE_TYPE (record);
944fb799 3402
32dabdaf
NP
3403 if (!TYPE_HAS_OBJC_INFO (type)
3404 || !TYPE_OBJC_INTERFACE (type))
d764a8e6 3405 {
32dabdaf
NP
3406 error ("%qE redeclared as different kind of symbol",
3407 identifier);
3408 error ("previous declaration of %q+D",
3409 record);
d764a8e6 3410 }
6c65299b 3411 }
944fb799 3412
32dabdaf
NP
3413 record = xref_tag (RECORD_TYPE, identifier);
3414 INIT_TYPE_OBJC_INFO (record);
3415 /* In the case of a @class declaration, we store the ident in
3416 the TYPE_OBJC_INTERFACE. If later an @interface is found,
3417 we'll replace the ident with the interface. */
3418 TYPE_OBJC_INTERFACE (record) = identifier;
3cc2dd4b 3419 objc_map_put (class_name_map, identifier, NULL_TREE);
6c65299b 3420 }
6c65299b
RS
3421}
3422
d764a8e6
IS
3423tree
3424objc_is_class_name (tree ident)
6c65299b 3425{
0d8a2528
NP
3426 if (ident && TREE_CODE (ident) == IDENTIFIER_NODE)
3427 {
3428 tree t = identifier_global_value (ident);
3429 if (t)
3430 ident = t;
3431 }
3432
d764a8e6
IS
3433 while (ident && TREE_CODE (ident) == TYPE_DECL && DECL_ORIGINAL_TYPE (ident))
3434 ident = OBJC_TYPE_NAME (DECL_ORIGINAL_TYPE (ident));
6408ef33 3435
d764a8e6
IS
3436 if (ident && TREE_CODE (ident) == RECORD_TYPE)
3437 ident = OBJC_TYPE_NAME (ident);
6408ef33 3438#ifdef OBJCPLUS
d764a8e6
IS
3439 if (ident && TREE_CODE (ident) == TYPE_DECL)
3440 {
3441 tree type = TREE_TYPE (ident);
3442 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
3443 return NULL_TREE;
3444 ident = DECL_NAME (ident);
3445 }
6408ef33 3446#endif
d764a8e6
IS
3447 if (!ident || TREE_CODE (ident) != IDENTIFIER_NODE)
3448 return NULL_TREE;
6c65299b 3449
d764a8e6
IS
3450 if (lookup_interface (ident))
3451 return ident;
6c65299b 3452
3cc2dd4b
NP
3453 {
3454 tree target;
6c65299b 3455
3cc2dd4b
NP
3456 target = objc_map_get (class_name_map, ident);
3457 if (target != OBJC_MAP_NOT_FOUND)
3458 return ident;
3459
3460 target = objc_map_get (alias_name_map, ident);
3461 if (target != OBJC_MAP_NOT_FOUND)
3462 return target;
3463 }
6408ef33 3464
d764a8e6 3465 return 0;
6408ef33
ZL
3466}
3467
d764a8e6 3468/* Check whether TYPE is either 'id' or 'Class'. */
6c65299b 3469
d764a8e6
IS
3470tree
3471objc_is_id (tree type)
6408ef33 3472{
0d8a2528
NP
3473 if (type && TREE_CODE (type) == IDENTIFIER_NODE)
3474 {
3475 tree t = identifier_global_value (type);
3476 if (t)
3477 type = t;
3478 }
6c65299b 3479
d764a8e6
IS
3480 if (type && TREE_CODE (type) == TYPE_DECL)
3481 type = TREE_TYPE (type);
6c65299b 3482
d764a8e6
IS
3483 /* NB: This function may be called before the ObjC front-end has
3484 been initialized, in which case OBJC_OBJECT_TYPE will (still) be NULL. */
3485 return (objc_object_type && type
3486 && (IS_ID (type) || IS_CLASS (type) || IS_SUPER (type))
3487 ? type
3488 : NULL_TREE);
6408ef33 3489}
e31c7eec 3490
d764a8e6
IS
3491/* Check whether TYPE is either 'id', 'Class', or a pointer to an ObjC
3492 class instance. This is needed by other parts of the compiler to
3493 handle ObjC types gracefully. */
6408ef33 3494
d764a8e6
IS
3495tree
3496objc_is_object_ptr (tree type)
6408ef33 3497{
d764a8e6 3498 tree ret;
6408ef33 3499
d764a8e6
IS
3500 type = TYPE_MAIN_VARIANT (type);
3501 if (!POINTER_TYPE_P (type))
3502 return 0;
6408ef33 3503
d764a8e6
IS
3504 ret = objc_is_id (type);
3505 if (!ret)
3506 ret = objc_is_class_name (TREE_TYPE (type));
6408ef33 3507
d764a8e6 3508 return ret;
6c65299b 3509}
51900510 3510
d764a8e6
IS
3511static int
3512objc_is_gcable_type (tree type, int or_strong_p)
4cb8c14b 3513{
d764a8e6 3514 tree name;
4cb8c14b 3515
d764a8e6
IS
3516 if (!TYPE_P (type))
3517 return 0;
3518 if (objc_is_id (TYPE_MAIN_VARIANT (type)))
3519 return 1;
3520 if (or_strong_p && lookup_attribute ("objc_gc", TYPE_ATTRIBUTES (type)))
3521 return 1;
3522 if (TREE_CODE (type) != POINTER_TYPE && TREE_CODE (type) != INDIRECT_REF)
3523 return 0;
3524 type = TREE_TYPE (type);
3525 if (TREE_CODE (type) != RECORD_TYPE)
3526 return 0;
3527 name = TYPE_NAME (type);
3528 return (objc_is_class_name (name) != NULL_TREE);
4cb8c14b
RK
3529}
3530
d764a8e6
IS
3531static tree
3532objc_substitute_decl (tree expr, tree oldexpr, tree newexpr)
4cb8c14b 3533{
d764a8e6
IS
3534 if (expr == oldexpr)
3535 return newexpr;
4cb8c14b 3536
d764a8e6 3537 switch (TREE_CODE (expr))
4cb8c14b 3538 {
d764a8e6
IS
3539 case COMPONENT_REF:
3540 return objc_build_component_ref
3541 (objc_substitute_decl (TREE_OPERAND (expr, 0),
3542 oldexpr,
3543 newexpr),
3544 DECL_NAME (TREE_OPERAND (expr, 1)));
3545 case ARRAY_REF:
3546 return build_array_ref (input_location,
3547 objc_substitute_decl (TREE_OPERAND (expr, 0),
3548 oldexpr,
3549 newexpr),
3550 TREE_OPERAND (expr, 1));
3551 case INDIRECT_REF:
3552 return build_indirect_ref (input_location,
3553 objc_substitute_decl (TREE_OPERAND (expr, 0),
3554 oldexpr,
3555 newexpr), RO_ARROW);
3556 default:
3557 return expr;
4cb8c14b 3558 }
4cb8c14b
RK
3559}
3560
6c65299b 3561static tree
d764a8e6 3562objc_build_ivar_assignment (tree outervar, tree lhs, tree rhs)
6c65299b 3563{
d764a8e6
IS
3564 tree func_params;
3565 /* The LHS parameter contains the expression 'outervar->memberspec';
3566 we need to transform it into '&((typeof(outervar) *) 0)->memberspec',
3567 where memberspec may be arbitrarily complex (e.g., 'g->f.d[2].g[3]').
3568 */
3569 tree offs
3570 = objc_substitute_decl
3571 (lhs, outervar, convert (TREE_TYPE (outervar), integer_zero_node));
3572 tree func
3573 = (flag_objc_direct_dispatch
3574 ? objc_assign_ivar_fast_decl
3575 : objc_assign_ivar_decl);
6c65299b 3576
d764a8e6
IS
3577 offs = convert (integer_type_node, build_unary_op (input_location,
3578 ADDR_EXPR, offs, 0));
3579 offs = fold (offs);
3580 func_params = tree_cons (NULL_TREE,
3581 convert (objc_object_type, rhs),
3582 tree_cons (NULL_TREE, convert (objc_object_type, outervar),
3583 tree_cons (NULL_TREE, offs,
3584 NULL_TREE)));
6c65299b 3585
d764a8e6 3586 return build_function_call (input_location, func, func_params);
6408ef33 3587}
6c65299b 3588
d764a8e6
IS
3589static tree
3590objc_build_global_assignment (tree lhs, tree rhs)
6408ef33 3591{
d764a8e6
IS
3592 tree func_params = tree_cons (NULL_TREE,
3593 convert (objc_object_type, rhs),
3594 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3595 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3596 NULL_TREE));
e31c7eec 3597
944fb799 3598 return build_function_call (input_location,
d764a8e6 3599 objc_assign_global_decl, func_params);
6c65299b 3600}
e31c7eec 3601
e31c7eec 3602static tree
d764a8e6 3603objc_build_strong_cast_assignment (tree lhs, tree rhs)
e31c7eec 3604{
d764a8e6
IS
3605 tree func_params = tree_cons (NULL_TREE,
3606 convert (objc_object_type, rhs),
3607 tree_cons (NULL_TREE, convert (build_pointer_type (objc_object_type),
3608 build_unary_op (input_location, ADDR_EXPR, lhs, 0)),
3609 NULL_TREE));
3610
d764a8e6
IS
3611 return build_function_call (input_location,
3612 objc_assign_strong_cast_decl, func_params);
e31c7eec
TW
3613}
3614
d764a8e6
IS
3615static int
3616objc_is_gcable_p (tree expr)
6c65299b 3617{
d764a8e6
IS
3618 return (TREE_CODE (expr) == COMPONENT_REF
3619 ? objc_is_gcable_p (TREE_OPERAND (expr, 1))
3620 : TREE_CODE (expr) == ARRAY_REF
3621 ? (objc_is_gcable_p (TREE_TYPE (expr))
3622 || objc_is_gcable_p (TREE_OPERAND (expr, 0)))
3623 : TREE_CODE (expr) == ARRAY_TYPE
3624 ? objc_is_gcable_p (TREE_TYPE (expr))
3625 : TYPE_P (expr)
3626 ? objc_is_gcable_type (expr, 1)
3627 : (objc_is_gcable_p (TREE_TYPE (expr))
3628 || (DECL_P (expr)
3629 && lookup_attribute ("objc_gc", DECL_ATTRIBUTES (expr)))));
058bfe53
IS
3630}
3631
d764a8e6
IS
3632static int
3633objc_is_ivar_reference_p (tree expr)
058bfe53 3634{
d764a8e6
IS
3635 return (TREE_CODE (expr) == ARRAY_REF
3636 ? objc_is_ivar_reference_p (TREE_OPERAND (expr, 0))
3637 : TREE_CODE (expr) == COMPONENT_REF
3638 ? TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL
3639 : 0);
058bfe53
IS
3640}
3641
d764a8e6
IS
3642static int
3643objc_is_global_reference_p (tree expr)
058bfe53 3644{
d764a8e6
IS
3645 return (TREE_CODE (expr) == INDIRECT_REF || TREE_CODE (expr) == PLUS_EXPR
3646 ? objc_is_global_reference_p (TREE_OPERAND (expr, 0))
3647 : DECL_P (expr)
3648 ? (DECL_FILE_SCOPE_P (expr) || TREE_STATIC (expr))
3649 : 0);
3650}
058bfe53 3651
d764a8e6
IS
3652tree
3653objc_generate_write_barrier (tree lhs, enum tree_code modifycode, tree rhs)
3654{
3655 tree result = NULL_TREE, outer;
3656 int strong_cast_p = 0, outer_gc_p = 0, indirect_p = 0;
058bfe53 3657
d764a8e6
IS
3658 /* This function is currently only used with the next runtime with
3659 garbage collection enabled (-fobjc-gc). */
3660 gcc_assert (flag_next_runtime);
058bfe53 3661
d764a8e6
IS
3662 /* See if we have any lhs casts, and strip them out. NB: The lvalue casts
3663 will have been transformed to the form '*(type *)&expr'. */
3664 if (TREE_CODE (lhs) == INDIRECT_REF)
3665 {
3666 outer = TREE_OPERAND (lhs, 0);
058bfe53 3667
d764a8e6
IS
3668 while (!strong_cast_p
3669 && (CONVERT_EXPR_P (outer)
3670 || TREE_CODE (outer) == NON_LVALUE_EXPR))
5d0f30f7 3671 {
d764a8e6 3672 tree lhstype = TREE_TYPE (outer);
011d50d9 3673
d764a8e6
IS
3674 /* Descend down the cast chain, and record the first objc_gc
3675 attribute found. */
3676 if (POINTER_TYPE_P (lhstype))
3677 {
3678 tree attr
3679 = lookup_attribute ("objc_gc",
3680 TYPE_ATTRIBUTES (TREE_TYPE (lhstype)));
6c65299b 3681
d764a8e6
IS
3682 if (attr)
3683 strong_cast_p = 1;
3684 }
076a7055 3685
d764a8e6 3686 outer = TREE_OPERAND (outer, 0);
076a7055 3687 }
e31c7eec 3688 }
e31c7eec 3689
d764a8e6
IS
3690 /* If we have a __strong cast, it trumps all else. */
3691 if (strong_cast_p)
5d0f30f7 3692 {
d764a8e6
IS
3693 if (modifycode != NOP_EXPR)
3694 goto invalid_pointer_arithmetic;
e31c7eec 3695
d764a8e6
IS
3696 if (warn_assign_intercept)
3697 warning (0, "strong-cast assignment has been intercepted");
5d0f30f7 3698
d764a8e6 3699 result = objc_build_strong_cast_assignment (lhs, rhs);
51900510 3700
d764a8e6 3701 goto exit_point;
5d0f30f7
KKT
3702 }
3703
d764a8e6
IS
3704 /* the lhs must be of a suitable type, regardless of its underlying
3705 structure. */
3706 if (!objc_is_gcable_p (lhs))
3707 goto exit_point;
5d0f30f7 3708
d764a8e6 3709 outer = lhs;
5d0f30f7 3710
d764a8e6
IS
3711 while (outer
3712 && (TREE_CODE (outer) == COMPONENT_REF
3713 || TREE_CODE (outer) == ARRAY_REF))
3714 outer = TREE_OPERAND (outer, 0);
e31c7eec 3715
d764a8e6 3716 if (TREE_CODE (outer) == INDIRECT_REF)
e31c7eec 3717 {
d764a8e6
IS
3718 outer = TREE_OPERAND (outer, 0);
3719 indirect_p = 1;
e31c7eec
TW
3720 }
3721
d764a8e6 3722 outer_gc_p = objc_is_gcable_p (outer);
e31c7eec 3723
d764a8e6
IS
3724 /* Handle ivar assignments. */
3725 if (objc_is_ivar_reference_p (lhs))
3726 {
3727 /* if the struct to the left of the ivar is not an Objective-C object (__strong
3728 doesn't cut it here), the best we can do here is suggest a cast. */
3729 if (!objc_is_gcable_type (TREE_TYPE (outer), 0))
3730 {
3731 /* We may still be able to use the global write barrier... */
3732 if (!indirect_p && objc_is_global_reference_p (outer))
3733 goto global_reference;
264fa2db 3734
d764a8e6
IS
3735 suggest_cast:
3736 if (modifycode == NOP_EXPR)
3737 {
3738 if (warn_assign_intercept)
3739 warning (0, "strong-cast may possibly be needed");
3740 }
e31c7eec 3741
d764a8e6
IS
3742 goto exit_point;
3743 }
e31c7eec 3744
d764a8e6
IS
3745 if (modifycode != NOP_EXPR)
3746 goto invalid_pointer_arithmetic;
6c65299b 3747
d764a8e6
IS
3748 if (warn_assign_intercept)
3749 warning (0, "instance variable assignment has been intercepted");
e31c7eec 3750
d764a8e6 3751 result = objc_build_ivar_assignment (outer, lhs, rhs);
6c65299b 3752
d764a8e6
IS
3753 goto exit_point;
3754 }
3755
3756 /* Likewise, intercept assignment to global/static variables if their type is
3757 GC-marked. */
3758 if (objc_is_global_reference_p (outer))
6c65299b 3759 {
d764a8e6
IS
3760 if (indirect_p)
3761 goto suggest_cast;
6c65299b 3762
d764a8e6
IS
3763 global_reference:
3764 if (modifycode != NOP_EXPR)
3765 {
3766 invalid_pointer_arithmetic:
3767 if (outer_gc_p)
3768 warning (0, "pointer arithmetic for garbage-collected objects not allowed");
6c65299b 3769
d764a8e6
IS
3770 goto exit_point;
3771 }
3772
3773 if (warn_assign_intercept)
3774 warning (0, "global/static variable assignment has been intercepted");
3775
3776 result = objc_build_global_assignment (lhs, rhs);
6c65299b 3777 }
d764a8e6
IS
3778
3779 /* In all other cases, fall back to the normal mechanism. */
3780 exit_point:
3781 return result;
6c65299b
RS
3782}
3783
3cc2dd4b
NP
3784/* Implementation of the table mapping a class name (as an identifier)
3785 to a class node. The two public functions for it are
3786 lookup_interface() and add_interface(). add_interface() is only
3787 used in this file, so we can make it static. */
264fa2db 3788
3cc2dd4b 3789static GTY(()) objc_map_t interface_map;
d11dd684 3790
3cc2dd4b
NP
3791static void
3792interface_hash_init (void)
d764a8e6 3793{
3cc2dd4b 3794 interface_map = objc_map_alloc_ggc (200);
d764a8e6
IS
3795}
3796
3cc2dd4b
NP
3797static tree
3798add_interface (tree class_name, tree name)
d764a8e6 3799{
3cc2dd4b
NP
3800 /* Put interfaces on list in reverse order. */
3801 TREE_CHAIN (class_name) = interface_chain;
3802 interface_chain = class_name;
3803
3804 /* Add it to the map. */
3805 objc_map_put (interface_map, name, class_name);
3806
3807 return interface_chain;
d764a8e6 3808}
38b9c8c3 3809
d764a8e6
IS
3810tree
3811lookup_interface (tree ident)
3812{
d11dd684 3813#ifdef OBJCPLUS
d764a8e6
IS
3814 if (ident && TREE_CODE (ident) == TYPE_DECL)
3815 ident = DECL_NAME (ident);
264fa2db 3816#endif
264fa2db 3817
d764a8e6
IS
3818 if (ident == NULL_TREE || TREE_CODE (ident) != IDENTIFIER_NODE)
3819 return NULL_TREE;
264fa2db 3820
d764a8e6 3821 {
3cc2dd4b 3822 tree interface = objc_map_get (interface_map, ident);
6c65299b 3823
3cc2dd4b
NP
3824 if (interface == OBJC_MAP_NOT_FOUND)
3825 return NULL_TREE;
3826 else
3827 return interface;
d764a8e6
IS
3828 }
3829}
51900510 3830
0dc33c3c
NP
3831
3832
d764a8e6 3833/* Implement @defs (<classname>) within struct bodies. */
6c65299b 3834
d764a8e6
IS
3835tree
3836objc_get_class_ivars (tree class_name)
3837{
3838 tree interface = lookup_interface (class_name);
6c65299b 3839
d764a8e6
IS
3840 if (interface)
3841 return get_class_ivars (interface, true);
7651f8f7 3842
d764a8e6
IS
3843 error ("cannot find interface declaration for %qE",
3844 class_name);
6c65299b 3845
d764a8e6 3846 return error_mark_node;
e31c7eec
TW
3847}
3848
0dc33c3c
NP
3849
3850/* Functions used by the hashtable for field duplicates in
3851 objc_detect_field_duplicates(). Ideally, we'd use a standard
3852 key-value dictionary hashtable , and store as keys the field names,
3853 and as values the actual declarations (used to print nice error
3854 messages with the locations). But, the hashtable we are using only
3855 allows us to store keys in the hashtable, without values (it looks
3856 more like a set). So, we store the DECLs, but define equality as
3857 DECLs having the same name, and hash as the hash of the name. */
703c8606 3858
8d67ee55 3859struct decl_name_hash : nofree_ptr_hash <tree_node>
703c8606 3860{
67f58944
TS
3861 static inline hashval_t hash (const tree_node *);
3862 static inline bool equal (const tree_node *, const tree_node *);
703c8606
LC
3863};
3864
3865inline hashval_t
67f58944 3866decl_name_hash::hash (const tree_node *q)
0dc33c3c 3867{
0dc33c3c
NP
3868 return (hashval_t) ((intptr_t)(DECL_NAME (q)) >> 3);
3869}
3870
703c8606 3871inline bool
67f58944 3872decl_name_hash::equal (const tree_node *a, const tree_node *b)
0dc33c3c 3873{
0dc33c3c
NP
3874 return DECL_NAME (a) == DECL_NAME (b);
3875}
3876
d764a8e6 3877/* Called when checking the variables in a struct. If we are not
0dc33c3c
NP
3878 doing the ivars list inside an @interface context, then return
3879 false. Else, perform the check for duplicate ivars, then return
3880 true. The check for duplicates checks if an instance variable with
3881 the same name exists in the class or in a superclass. If
3882 'check_superclasses_only' is set to true, then it is assumed that
3883 checks for instance variables in the same class has already been
3884 performed (this is the case for ObjC++) and only the instance
3885 variables of superclasses are checked. */
3886bool
3887objc_detect_field_duplicates (bool check_superclasses_only)
d764a8e6 3888{
944fb799 3889 if (!objc_collecting_ivars || !objc_interface_context
0dc33c3c
NP
3890 || TREE_CODE (objc_interface_context) != CLASS_INTERFACE_TYPE)
3891 return false;
3892
3893 /* We have two ways of doing this check:
944fb799 3894
0dc33c3c
NP
3895 "direct comparison": we iterate over the instance variables and
3896 compare them directly. This works great for small numbers of
3897 instance variables (such as 10 or 20), which are extremely common.
3898 But it will potentially take forever for the pathological case with
3899 a huge number (eg, 10k) of instance variables.
944fb799 3900
0dc33c3c
NP
3901 "hashtable": we use a hashtable, which requires a single sweep
3902 through the list of instances variables. This is much slower for a
3903 small number of variables, and we only use it for large numbers.
3904
3905 To decide which one to use, we need to get an idea of how many
3906 instance variables we have to compare. */
3907 {
3908 unsigned int number_of_ivars_to_check = 0;
3909 {
3910 tree ivar;
3911 for (ivar = CLASS_RAW_IVARS (objc_interface_context);
3912 ivar; ivar = DECL_CHAIN (ivar))
3913 {
3914 /* Ignore anonymous ivars. */
3915 if (DECL_NAME (ivar))
3916 number_of_ivars_to_check++;
3917 }
3918 }
d764a8e6 3919
0dc33c3c
NP
3920 /* Exit if there is nothing to do. */
3921 if (number_of_ivars_to_check == 0)
3922 return true;
944fb799 3923
0dc33c3c
NP
3924 /* In case that there are only 1 or 2 instance variables to check,
3925 we always use direct comparison. If there are more, it is
3926 worth iterating over the instance variables in the superclass
3927 to count how many there are (note that this has the same cost
3928 as checking 1 instance variable by direct comparison, which is
3929 why we skip this check in the case of 1 or 2 ivars and just do
3930 the direct comparison) and then decide if it worth using a
3931 hashtable. */
3932 if (number_of_ivars_to_check > 2)
3933 {
3934 unsigned int number_of_superclass_ivars = 0;
3935 {
3936 tree interface;
3937 for (interface = lookup_interface (CLASS_SUPER_NAME (objc_interface_context));
3938 interface; interface = lookup_interface (CLASS_SUPER_NAME (interface)))
3939 {
3940 tree ivar;
3941 for (ivar = CLASS_RAW_IVARS (interface);
3942 ivar; ivar = DECL_CHAIN (ivar))
3943 number_of_superclass_ivars++;
3944 }
3945 }
3946
3947 /* We use a hashtable if we have over 10k comparisons. */
944fb799 3948 if (number_of_ivars_to_check * (number_of_superclass_ivars
0dc33c3c
NP
3949 + (number_of_ivars_to_check / 2))
3950 > 10000)
3951 {
3952 /* First, build the hashtable by putting all the instance
3953 variables of superclasses in it. */
c203e8a7 3954 hash_table<decl_name_hash> htab (37);
0dc33c3c
NP
3955 tree interface;
3956 for (interface = lookup_interface (CLASS_SUPER_NAME
3957 (objc_interface_context));
3958 interface; interface = lookup_interface
3959 (CLASS_SUPER_NAME (interface)))
3960 {
3961 tree ivar;
3962 for (ivar = CLASS_RAW_IVARS (interface); ivar;
3963 ivar = DECL_CHAIN (ivar))
3964 {
3965 if (DECL_NAME (ivar) != NULL_TREE)
3966 {
703c8606 3967 tree_node **slot = htab.find_slot (ivar, INSERT);
0dc33c3c
NP
3968 /* Do not check for duplicate instance
3969 variables in superclasses. Errors have
3970 already been generated. */
3971 *slot = ivar;
3972 }
3973 }
3974 }
944fb799 3975
0dc33c3c
NP
3976 /* Now, we go through all the instance variables in the
3977 class, and check that they are not in the
3978 hashtable. */
3979 if (check_superclasses_only)
3980 {
3981 tree ivar;
3982 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
3983 ivar = DECL_CHAIN (ivar))
3984 {
3985 if (DECL_NAME (ivar) != NULL_TREE)
3986 {
703c8606 3987 tree duplicate_ivar = htab.find (ivar);
0dc33c3c
NP
3988 if (duplicate_ivar != HTAB_EMPTY_ENTRY)
3989 {
3990 error_at (DECL_SOURCE_LOCATION (ivar),
3991 "duplicate instance variable %q+D",
3992 ivar);
3993 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
3994 "previous declaration of %q+D",
3995 duplicate_ivar);
3996 /* FIXME: Do we need the following ? */
3997 /* DECL_NAME (ivar) = NULL_TREE; */
3998 }
3999 }
4000 }
4001 }
4002 else
4003 {
4004 /* If we're checking for duplicates in the class as
4005 well, we insert variables in the hashtable as we
4006 check them, so if a duplicate follows, it will be
4007 caught. */
4008 tree ivar;
4009 for (ivar = CLASS_RAW_IVARS (objc_interface_context); ivar;
4010 ivar = DECL_CHAIN (ivar))
4011 {
4012 if (DECL_NAME (ivar) != NULL_TREE)
4013 {
703c8606 4014 tree_node **slot = htab.find_slot (ivar, INSERT);
0dc33c3c
NP
4015 if (*slot)
4016 {
4017 tree duplicate_ivar = (tree)(*slot);
4018 error_at (DECL_SOURCE_LOCATION (ivar),
4019 "duplicate instance variable %q+D",
4020 ivar);
4021 inform (DECL_SOURCE_LOCATION (duplicate_ivar),
4022 "previous declaration of %q+D",
4023 duplicate_ivar);
4024 /* FIXME: Do we need the following ? */
4025 /* DECL_NAME (ivar) = NULL_TREE; */
4026 }
4027 *slot = ivar;
4028 }
4029 }
4030 }
0dc33c3c
NP
4031 return true;
4032 }
4033 }
4034 }
944fb799 4035
0dc33c3c
NP
4036 /* This is the "direct comparison" approach, which is used in most
4037 non-pathological cases. */
4038 {
4039 /* Walk up to class hierarchy, starting with this class (this is
4040 the external loop, because lookup_interface() is expensive, and
4041 we want to do it few times). */
4042 tree interface = objc_interface_context;
4043
4044 if (check_superclasses_only)
4045 interface = lookup_interface (CLASS_SUPER_NAME (interface));
944fb799 4046
0dc33c3c
NP
4047 for ( ; interface; interface = lookup_interface
4048 (CLASS_SUPER_NAME (interface)))
4049 {
4050 tree ivar_being_checked;
4051
4052 for (ivar_being_checked = CLASS_RAW_IVARS (objc_interface_context);
4053 ivar_being_checked;
4054 ivar_being_checked = DECL_CHAIN (ivar_being_checked))
4055 {
4056 tree decl;
944fb799 4057
0dc33c3c
NP
4058 /* Ignore anonymous ivars. */
4059 if (DECL_NAME (ivar_being_checked) == NULL_TREE)
4060 continue;
4061
4062 /* Note how we stop when we find the ivar we are checking
4063 (this can only happen in the main class, not
4064 superclasses), to avoid comparing things twice
4065 (otherwise, for each ivar, you'd compare A to B then B
4066 to A, and get duplicated error messages). */
4067 for (decl = CLASS_RAW_IVARS (interface);
4068 decl && decl != ivar_being_checked;
4069 decl = DECL_CHAIN (decl))
4070 {
4071 if (DECL_NAME (ivar_being_checked) == DECL_NAME (decl))
4072 {
4073 error_at (DECL_SOURCE_LOCATION (ivar_being_checked),
4074 "duplicate instance variable %q+D",
4075 ivar_being_checked);
4076 inform (DECL_SOURCE_LOCATION (decl),
4077 "previous declaration of %q+D",
4078 decl);
4079 /* FIXME: Do we need the following ? */
4080 /* DECL_NAME (ivar_being_checked) = NULL_TREE; */
4081 }
4082 }
4083 }
4084 }
4085 }
4086 return true;
d764a8e6 4087}
e31c7eec 4088
d764a8e6
IS
4089/* Used by: build_private_template, continue_class,
4090 and for @defs constructs. */
058bfe53 4091
e31c7eec 4092static tree
d764a8e6 4093get_class_ivars (tree interface, bool inherited)
e31c7eec 4094{
d764a8e6
IS
4095 tree ivar_chain = copy_list (CLASS_RAW_IVARS (interface));
4096
4097 /* Both CLASS_RAW_IVARS and CLASS_IVARS contain a list of ivars declared
4098 by the current class (i.e., they do not include super-class ivars).
4099 However, the CLASS_IVARS list will be side-effected by a call to
4100 finish_struct(), which will fill in field offsets. */
4101 if (!CLASS_IVARS (interface))
4102 CLASS_IVARS (interface) = ivar_chain;
4103
4104 if (!inherited)
4105 return ivar_chain;
4106
4107 while (CLASS_SUPER_NAME (interface))
058bfe53 4108 {
d764a8e6
IS
4109 /* Prepend super-class ivars. */
4110 interface = lookup_interface (CLASS_SUPER_NAME (interface));
4111 ivar_chain = chainon (copy_list (CLASS_RAW_IVARS (interface)),
4112 ivar_chain);
058bfe53 4113 }
e31c7eec 4114
d764a8e6
IS
4115 return ivar_chain;
4116}
4117
4118void
4119objc_maybe_warn_exceptions (location_t loc)
4120{
4121 /* -fobjc-exceptions is required to enable Objective-C exceptions.
4122 For example, on Darwin, ObjC exceptions require a sufficiently
4123 recent version of the runtime, so the user must ask for them
4124 explicitly. On other platforms, at the moment -fobjc-exceptions
4125 triggers -fexceptions which again is required for exceptions to
4126 work. */
4127 if (!flag_objc_exceptions)
e31c7eec 4128 {
d764a8e6
IS
4129 /* Warn only once per compilation unit. */
4130 static bool warned = false;
e31c7eec 4131
d764a8e6
IS
4132 if (!warned)
4133 {
4134 error_at (loc, "%<-fobjc-exceptions%> is required to enable Objective-C exception syntax");
4135 warned = true;
4136 }
6c65299b 4137 }
d764a8e6
IS
4138}
4139
4140static struct objc_try_context *cur_try_context;
e31c7eec 4141
d764a8e6
IS
4142/* Called just after parsing the @try and its associated BODY. We now
4143 must prepare for the tricky bits -- handling the catches and finally. */
e31c7eec 4144
d764a8e6
IS
4145void
4146objc_begin_try_stmt (location_t try_locus, tree body)
4147{
4148 struct objc_try_context *c = XCNEW (struct objc_try_context);
4149 c->outer = cur_try_context;
4150 c->try_body = body;
4151 c->try_locus = try_locus;
4152 c->end_try_locus = input_location;
4153 cur_try_context = c;
e31c7eec 4154
d764a8e6
IS
4155 /* Collect the list of local variables. We'll mark them as volatile
4156 at the end of compilation of this function to prevent them being
4157 clobbered by setjmp/longjmp. */
4158 if (flag_objc_sjlj_exceptions)
4159 objc_mark_locals_volatile (NULL);
e31c7eec
TW
4160}
4161
d764a8e6
IS
4162/* Called just after parsing "@catch (parm)". Open a binding level,
4163 enter DECL into the binding level, and initialize it. Leave the
4164 binding level open while the body of the compound statement is
4165 parsed. If DECL is NULL_TREE, then we are compiling "@catch(...)"
4166 which we compile as "@catch(id tmp_variable)". */
4167
e31c7eec 4168void
d764a8e6 4169objc_begin_catch_clause (tree decl)
e31c7eec 4170{
d764a8e6
IS
4171 tree compound, type, t;
4172 bool ellipsis = false;
264fa2db 4173
d764a8e6
IS
4174 /* Begin a new scope that the entire catch clause will live in. */
4175 compound = c_begin_compound_stmt (true);
264fa2db 4176
d764a8e6
IS
4177 /* Create the appropriate declaration for the argument. */
4178 if (decl == error_mark_node)
4179 type = error_mark_node;
4180 else
4181 {
4182 if (decl == NULL_TREE)
4183 {
4184 /* If @catch(...) was specified, create a temporary variable of
4185 type 'id' and use it. */
4186 decl = objc_create_temporary_var (objc_object_type, "__objc_generic_catch_var");
4187 DECL_SOURCE_LOCATION (decl) = input_location;
4188 /* ... but allow the runtime to differentiate between ellipsis and the
4189 case of @catch (id xyz). */
4190 ellipsis = true;
4191 }
4192 else
4193 {
4194 /* The parser passed in a PARM_DECL, but what we really want is a VAR_DECL. */
4195 decl = build_decl (input_location,
4196 VAR_DECL, DECL_NAME (decl), TREE_TYPE (decl));
4197 }
4198 lang_hooks.decls.pushdecl (decl);
e31c7eec 4199
d764a8e6
IS
4200 /* Mark the declaration as used so you never any warnings whether
4201 you use the exception argument or not. TODO: Implement a
4202 -Wunused-exception-parameter flag, which would cause warnings
4203 if exception parameter is not used. */
4204 TREE_USED (decl) = 1;
4205 DECL_READ_P (decl) = 1;
e31c7eec 4206
d764a8e6
IS
4207 type = TREE_TYPE (decl);
4208 }
e31c7eec 4209
d764a8e6
IS
4210 /* Verify that the type of the catch is valid. It must be a pointer
4211 to an Objective-C class, or "id" (which is catch-all). */
4212 if (type == error_mark_node)
4213 {
4214 ;/* Just keep going. */
4215 }
4216 else if (!objc_type_valid_for_messaging (type, false))
4217 {
4218 error ("@catch parameter is not a known Objective-C class type");
4219 type = error_mark_node;
4220 }
4221 else if (TYPE_HAS_OBJC_INFO (TREE_TYPE (type))
4222 && TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (type)))
4223 {
4224 error ("@catch parameter can not be protocol-qualified");
944fb799 4225 type = error_mark_node;
d764a8e6
IS
4226 }
4227 else if (POINTER_TYPE_P (type) && objc_is_object_id (TREE_TYPE (type)))
4228 /* @catch (id xyz) or @catch (...) but we note this for runtimes that
4229 identify 'id'. */
4230 ;
4231 else
4232 {
4233 /* If 'type' was built using typedefs, we need to get rid of
4234 them and get a simple pointer to the class. */
4235 bool is_typedef = false;
4236 tree x = TYPE_MAIN_VARIANT (type);
944fb799 4237
d764a8e6
IS
4238 /* Skip from the pointer to the pointee. */
4239 if (TREE_CODE (x) == POINTER_TYPE)
4240 x = TREE_TYPE (x);
944fb799 4241
d764a8e6
IS
4242 /* Traverse typedef aliases */
4243 while (TREE_CODE (x) == RECORD_TYPE && OBJC_TYPE_NAME (x)
4244 && TREE_CODE (OBJC_TYPE_NAME (x)) == TYPE_DECL
4245 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x)))
e31c7eec 4246 {
d764a8e6
IS
4247 is_typedef = true;
4248 x = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (x));
4249 }
035e8f01 4250
d764a8e6
IS
4251 /* If it was a typedef, build a pointer to the final, original
4252 class. */
4253 if (is_typedef)
4254 type = build_pointer_type (x);
035e8f01 4255
d764a8e6
IS
4256 if (cur_try_context->catch_list)
4257 {
4258 /* Examine previous @catch clauses and see if we've already
4259 caught the type in question. */
4260 tree_stmt_iterator i = tsi_start (cur_try_context->catch_list);
4261 for (; !tsi_end_p (i); tsi_next (&i))
4262 {
4263 tree stmt = tsi_stmt (i);
4264 t = CATCH_TYPES (stmt);
4265 if (t == error_mark_node)
4266 continue;
4267 if (!t || DERIVED_FROM_P (TREE_TYPE (t), TREE_TYPE (type)))
035e8f01 4268 {
d764a8e6
IS
4269 warning (0, "exception of type %<%T%> will be caught",
4270 TREE_TYPE (type));
4271 warning_at (EXPR_LOCATION (stmt), 0, " by earlier handler for %<%T%>",
4272 TREE_TYPE (t ? t : objc_object_type));
4273 break;
035e8f01 4274 }
264fa2db 4275 }
e31c7eec
TW
4276 }
4277 }
6c65299b 4278
d764a8e6
IS
4279 t = (*runtime.begin_catch) (&cur_try_context, type, decl, compound, ellipsis);
4280 add_stmt (t);
4281}
6c65299b 4282
d764a8e6
IS
4283/* Called just after parsing the closing brace of a @catch clause. Close
4284 the open binding level, and record a CATCH_EXPR for it. */
264fa2db 4285
d764a8e6
IS
4286void
4287objc_finish_catch_clause (void)
4288{
4289 tree c = cur_try_context->current_catch;
4290 cur_try_context->current_catch = NULL;
4291 cur_try_context->end_catch_locus = input_location;
264fa2db 4292
d764a8e6 4293 CATCH_BODY (c) = c_end_compound_stmt (input_location, CATCH_BODY (c), 1);
6c65299b 4294
d764a8e6
IS
4295 (*runtime.finish_catch) (&cur_try_context, c);
4296}
6c65299b 4297
d764a8e6
IS
4298/* Called after parsing a @finally clause and its associated BODY.
4299 Record the body for later placement. */
6c65299b 4300
d764a8e6
IS
4301void
4302objc_build_finally_clause (location_t finally_locus, tree body)
4303{
4304 cur_try_context->finally_body = body;
4305 cur_try_context->finally_locus = finally_locus;
4306 cur_try_context->end_finally_locus = input_location;
370ce32a
ZL
4307}
4308
d764a8e6 4309/* Called to finalize a @try construct. */
6408ef33
ZL
4310
4311tree
d764a8e6 4312objc_finish_try_stmt (void)
6408ef33 4313{
d764a8e6
IS
4314 struct objc_try_context *c = cur_try_context;
4315 tree stmt;
6408ef33 4316
d764a8e6
IS
4317 if (c->catch_list == NULL && c->finally_body == NULL)
4318 error ("%<@try%> without %<@catch%> or %<@finally%>");
6408ef33 4319
d764a8e6
IS
4320 stmt = (*runtime.finish_try_stmt) (&cur_try_context);
4321 add_stmt (stmt);
6408ef33 4322
d764a8e6
IS
4323 cur_try_context = c->outer;
4324 free (c);
4325 return stmt;
4326}
264fa2db 4327
370ce32a 4328tree
d764a8e6 4329objc_build_throw_stmt (location_t loc, tree throw_expr)
370ce32a 4330{
d764a8e6 4331 bool rethrown = false;
6408ef33 4332
d764a8e6 4333 objc_maybe_warn_exceptions (loc);
6408ef33 4334
d764a8e6
IS
4335 /* Don't waste time trying to build something if we're already dead. */
4336 if (throw_expr == error_mark_node)
4337 return error_mark_node;
6408ef33 4338
d764a8e6
IS
4339 if (throw_expr == NULL)
4340 {
4341 /* If we're not inside a @catch block, there is no "current
4342 exception" to be rethrown. */
4343 if (cur_try_context == NULL
4344 || cur_try_context->current_catch == NULL)
4345 {
4346 error_at (loc, "%<@throw%> (rethrow) used outside of a @catch block");
4347 return error_mark_node;
4348 }
4349
4350 /* Otherwise the object is still sitting in the EXC_PTR_EXPR
4351 value that we get from the runtime. */
4352 throw_expr = (*runtime.build_exc_ptr) (&cur_try_context);
4353 rethrown = true;
4354 }
4355 else
4356 {
4357 if (!objc_type_valid_for_messaging (TREE_TYPE (throw_expr), true))
4358 {
4359 error_at (loc, "%<@throw%> argument is not an object");
4360 return error_mark_node;
4361 }
4362 }
4363
4364 return (*runtime.build_throw_stmt) (loc, throw_expr, rethrown);
6c65299b
RS
4365}
4366
d764a8e6
IS
4367tree
4368objc_build_synchronized (location_t start_locus, tree object_expr, tree body)
38b9c8c3 4369{
d764a8e6
IS
4370 /* object_expr should never be NULL; but in case it is, convert it to
4371 error_mark_node. */
4372 if (object_expr == NULL)
4373 object_expr = error_mark_node;
38b9c8c3 4374
d764a8e6
IS
4375 /* Validate object_expr. If not valid, set it to error_mark_node. */
4376 if (object_expr != error_mark_node)
38b9c8c3 4377 {
d764a8e6
IS
4378 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expr), true))
4379 {
4380 error_at (start_locus, "%<@synchronized%> argument is not an object");
4381 object_expr = error_mark_node;
4382 }
38b9c8c3 4383 }
944fb799 4384
d764a8e6
IS
4385 if (object_expr == error_mark_node)
4386 {
4387 /* If we found an error, we simply ignore the '@synchronized'.
4388 Compile the body so we can keep going with minimal
4389 casualties. */
4390 return add_stmt (body);
4391 }
4392 else
4393 {
4394 tree call;
4395 tree args;
38b9c8c3 4396
944fb799 4397 /* objc_sync_enter (object_expr); */
d764a8e6
IS
4398 object_expr = save_expr (object_expr);
4399 args = tree_cons (NULL, object_expr, NULL);
4400 call = build_function_call (input_location,
4401 objc_sync_enter_decl, args);
4402 SET_EXPR_LOCATION (call, start_locus);
4403 add_stmt (call);
38b9c8c3 4404
d764a8e6
IS
4405 /* Build "objc_sync_exit (object_expr);" but do not add it yet;
4406 it goes inside the @finalize() clause. */
4407 args = tree_cons (NULL, object_expr, NULL);
4408 call = build_function_call (input_location,
4409 objc_sync_exit_decl, args);
4410 SET_EXPR_LOCATION (call, input_location);
38b9c8c3 4411
d764a8e6
IS
4412 /* @try { body; } */
4413 objc_begin_try_stmt (start_locus, body);
944fb799 4414
d764a8e6
IS
4415 /* @finally { objc_sync_exit (object_expr); } */
4416 objc_build_finally_clause (input_location, call);
944fb799 4417
d764a8e6
IS
4418 /* End of try statement. */
4419 return objc_finish_try_stmt ();
4420 }
38b9c8c3
ZL
4421}
4422
d764a8e6
IS
4423/* Construct a C struct corresponding to ObjC class CLASS, with the same
4424 name as the class:
38b9c8c3 4425
d764a8e6
IS
4426 struct <classname> {
4427 struct _objc_class *isa;
4428 ...
4429 }; */
38b9c8c3 4430
d764a8e6
IS
4431static void
4432build_private_template (tree klass)
38b9c8c3 4433{
d764a8e6
IS
4434 if (!CLASS_STATIC_TEMPLATE (klass))
4435 {
4436 tree record = objc_build_struct (klass,
4437 get_class_ivars (klass, false),
4438 CLASS_SUPER_NAME (klass));
38b9c8c3 4439
d764a8e6
IS
4440 /* Set the TREE_USED bit for this struct, so that stab generator
4441 can emit stabs for this struct type. */
4442 if (flag_debug_only_used_symbols && TYPE_STUB_DECL (record))
4443 TREE_USED (TYPE_STUB_DECL (record)) = 1;
38b9c8c3 4444
d764a8e6
IS
4445 /* Copy the attributes from the class to the type. */
4446 if (TREE_DEPRECATED (klass))
4447 TREE_DEPRECATED (record) = 1;
d764a8e6 4448 }
38b9c8c3
ZL
4449}
4450
d764a8e6
IS
4451/* Generate either '- .cxx_construct' or '- .cxx_destruct' for the
4452 current class. */
4453#ifdef OBJCPLUS
4454static void
4455objc_generate_cxx_ctor_or_dtor (bool dtor)
4456{
4457 tree fn, body, compound_stmt, ivar;
38b9c8c3 4458
d764a8e6
IS
4459 /* - (id) .cxx_construct { ... return self; } */
4460 /* - (void) .cxx_construct { ... } */
38b9c8c3 4461
d764a8e6
IS
4462 objc_start_method_definition
4463 (false /* is_class_method */,
4464 objc_build_method_signature (false /* is_class_method */,
4465 build_tree_list (NULL_TREE,
4466 dtor
4467 ? void_type_node
4468 : objc_object_type),
4469 get_identifier (dtor
4470 ? TAG_CXX_DESTRUCT
4471 : TAG_CXX_CONSTRUCT),
4472 make_node (TREE_LIST),
a04a722b 4473 false), NULL, NULL_TREE);
d764a8e6
IS
4474 body = begin_function_body ();
4475 compound_stmt = begin_compound_stmt (0);
38b9c8c3 4476
d764a8e6
IS
4477 ivar = CLASS_IVARS (implementation_template);
4478 /* Destroy ivars in reverse order. */
4479 if (dtor)
4480 ivar = nreverse (copy_list (ivar));
64ee9490 4481
d764a8e6 4482 for (; ivar; ivar = TREE_CHAIN (ivar))
38b9c8c3 4483 {
d764a8e6 4484 if (TREE_CODE (ivar) == FIELD_DECL)
38b9c8c3 4485 {
d764a8e6 4486 tree type = TREE_TYPE (ivar);
38b9c8c3 4487
d764a8e6
IS
4488 /* Call the ivar's default constructor or destructor. Do not
4489 call the destructor unless a corresponding constructor call
4490 has also been made (or is not needed). */
4491 if (MAYBE_CLASS_TYPE_P (type)
4492 && (dtor
4493 ? (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4494 && (!TYPE_NEEDS_CONSTRUCTING (type)
4495 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4496 : (TYPE_NEEDS_CONSTRUCTING (type)
4497 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))))
4498 finish_expr_stmt
4499 (build_special_member_call
4500 (build_ivar_reference (DECL_NAME (ivar)),
4501 dtor ? complete_dtor_identifier : complete_ctor_identifier,
4502 NULL, type, LOOKUP_NORMAL, tf_warning_or_error));
38b9c8c3 4503 }
d764a8e6 4504 }
38b9c8c3 4505
d764a8e6
IS
4506 /* The constructor returns 'self'. */
4507 if (!dtor)
4508 finish_return_stmt (self_decl);
38b9c8c3 4509
d764a8e6
IS
4510 finish_compound_stmt (compound_stmt);
4511 finish_function_body (body);
4512 fn = current_function_decl;
4513 finish_function ();
4514 objc_finish_method_definition (fn);
4515}
38b9c8c3 4516
d764a8e6
IS
4517/* The following routine will examine the current @interface for any
4518 non-POD C++ ivars requiring non-trivial construction and/or
4519 destruction, and then synthesize special '- .cxx_construct' and/or
4520 '- .cxx_destruct' methods which will run the appropriate
4521 construction or destruction code. Note that ivars inherited from
4522 super-classes are _not_ considered. */
4523static void
4524objc_generate_cxx_cdtors (void)
4525{
4526 bool need_ctor = false, need_dtor = false;
4527 tree ivar;
38b9c8c3 4528
d764a8e6
IS
4529 /* Error case, due to possibly an extra @end. */
4530 if (!objc_implementation_context)
4531 return;
38b9c8c3 4532
d764a8e6
IS
4533 /* We do not want to do this for categories, since they do not have
4534 their own ivars. */
38b9c8c3 4535
d764a8e6
IS
4536 if (TREE_CODE (objc_implementation_context) != CLASS_IMPLEMENTATION_TYPE)
4537 return;
38b9c8c3 4538
d764a8e6 4539 /* First, determine if we even need a constructor and/or destructor. */
38b9c8c3 4540
d764a8e6
IS
4541 for (ivar = CLASS_IVARS (implementation_template); ivar;
4542 ivar = TREE_CHAIN (ivar))
4543 {
4544 if (TREE_CODE (ivar) == FIELD_DECL)
4545 {
4546 tree type = TREE_TYPE (ivar);
38b9c8c3 4547
d764a8e6
IS
4548 if (MAYBE_CLASS_TYPE_P (type))
4549 {
4550 if (TYPE_NEEDS_CONSTRUCTING (type)
4551 && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
4552 /* NB: If a default constructor is not available, we will not
4553 be able to initialize this ivar; the add_instance_variable()
4554 routine will already have warned about this. */
4555 need_ctor = true;
4556
4557 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
4558 && (!TYPE_NEEDS_CONSTRUCTING (type)
4559 || TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4560 /* NB: If a default constructor is not available, we will not
4561 call the destructor either, for symmetry. */
4562 need_dtor = true;
4563 }
4564 }
38b9c8c3
ZL
4565 }
4566
d764a8e6 4567 /* Generate '- .cxx_construct' if needed. */
38b9c8c3 4568
d764a8e6
IS
4569 if (need_ctor)
4570 objc_generate_cxx_ctor_or_dtor (false);
38b9c8c3 4571
d764a8e6 4572 /* Generate '- .cxx_destruct' if needed. */
55cb0169 4573
d764a8e6
IS
4574 if (need_dtor)
4575 objc_generate_cxx_ctor_or_dtor (true);
4576
4577 /* The 'imp_list' variable points at an imp_entry record for the current
4578 @implementation. Record the existence of '- .cxx_construct' and/or
4579 '- .cxx_destruct' methods therein; it will be included in the
4580 metadata for the class if the runtime needs it. */
4581 imp_list->has_cxx_cdtors = (need_ctor || need_dtor);
38b9c8c3 4582}
d764a8e6 4583#endif
38b9c8c3 4584
d764a8e6
IS
4585static void
4586error_with_ivar (const char *message, tree decl)
38b9c8c3 4587{
d764a8e6
IS
4588 error_at (DECL_SOURCE_LOCATION (decl), "%s %qs",
4589 message, identifier_to_locale (gen_declaration (decl)));
4590
38b9c8c3
ZL
4591}
4592
d764a8e6
IS
4593static void
4594check_ivars (tree inter, tree imp)
38b9c8c3 4595{
d764a8e6
IS
4596 tree intdecls = CLASS_RAW_IVARS (inter);
4597 tree impdecls = CLASS_RAW_IVARS (imp);
4598
4599 while (1)
4600 {
4601 tree t1, t2;
4602
264fa2db 4603#ifdef OBJCPLUS
d764a8e6
IS
4604 if (intdecls && TREE_CODE (intdecls) == TYPE_DECL)
4605 intdecls = TREE_CHAIN (intdecls);
264fa2db 4606#endif
d764a8e6
IS
4607 if (intdecls == 0 && impdecls == 0)
4608 break;
4609 if (intdecls == 0 || impdecls == 0)
4610 {
4611 error ("inconsistent instance variable specification");
4612 break;
4613 }
38b9c8c3 4614
d764a8e6 4615 t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
38b9c8c3 4616
d764a8e6
IS
4617 if (!comptypes (t1, t2)
4618 || !tree_int_cst_equal (DECL_INITIAL (intdecls),
4619 DECL_INITIAL (impdecls)))
4620 {
4621 if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
4622 {
4623 error_with_ivar ("conflicting instance variable type",
4624 impdecls);
4625 error_with_ivar ("previous declaration of",
4626 intdecls);
4627 }
4628 else /* both the type and the name don't match */
4629 {
4630 error ("inconsistent instance variable specification");
4631 break;
4632 }
4633 }
264fa2db 4634
d764a8e6
IS
4635 else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
4636 {
4637 error_with_ivar ("conflicting instance variable name",
4638 impdecls);
4639 error_with_ivar ("previous declaration of",
4640 intdecls);
4641 }
264fa2db 4642
d764a8e6
IS
4643 intdecls = DECL_CHAIN (intdecls);
4644 impdecls = DECL_CHAIN (impdecls);
4645 }
264fa2db
ZL
4646}
4647
d764a8e6
IS
4648
4649static void
4650mark_referenced_methods (void)
c0c24aa4 4651{
d764a8e6
IS
4652 struct imp_entry *impent;
4653 tree chain;
c0c24aa4 4654
d764a8e6
IS
4655 for (impent = imp_list; impent; impent = impent->next)
4656 {
4657 chain = CLASS_CLS_METHODS (impent->imp_context);
4658 while (chain)
4659 {
d52f5295 4660 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
d764a8e6
IS
4661 chain = DECL_CHAIN (chain);
4662 }
c0c24aa4 4663
d764a8e6
IS
4664 chain = CLASS_NST_METHODS (impent->imp_context);
4665 while (chain)
4666 {
d52f5295 4667 cgraph_node::get_create (METHOD_DEFINITION (chain))->mark_force_output ();
d764a8e6
IS
4668 chain = DECL_CHAIN (chain);
4669 }
4670 }
4671}
e31c7eec 4672
d764a8e6
IS
4673/* If type is empty or only type qualifiers are present, add default
4674 type of id (otherwise grokdeclarator will default to int). */
4675static inline tree
4676adjust_type_for_id_default (tree type)
e31c7eec 4677{
d764a8e6
IS
4678 if (!type)
4679 type = make_node (TREE_LIST);
d11dd684 4680
d764a8e6
IS
4681 if (!TREE_VALUE (type))
4682 TREE_VALUE (type) = objc_object_type;
4683 else if (TREE_CODE (TREE_VALUE (type)) == RECORD_TYPE
4684 && TYPED_OBJECT (TREE_VALUE (type)))
4685 error ("can not use an object as parameter to a method");
264fa2db 4686
d764a8e6 4687 return type;
264fa2db
ZL
4688}
4689
d764a8e6
IS
4690/* Return a KEYWORD_DECL built using the specified key_name, arg_type,
4691 arg_name and attributes. (TODO: Rename KEYWORD_DECL to
4692 OBJC_METHOD_PARM_DECL ?)
264fa2db 4693
d764a8e6
IS
4694 A KEYWORD_DECL is a tree representing the declaration of a
4695 parameter of an Objective-C method. It is produced when parsing a
4696 fragment of Objective-C method declaration of the form
264fa2db 4697
d764a8e6
IS
4698 keyworddecl:
4699 selector ':' '(' typename ')' identifier
264fa2db 4700
d764a8e6 4701 For example, take the Objective-C method
264fa2db 4702
944fb799 4703 -(NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;
264fa2db 4704
d764a8e6
IS
4705 the two fragments "pathForResource:(NSString *)resource" and
4706 "ofType:(NSString *)type" will generate a KEYWORD_DECL each. The
4707 KEYWORD_DECL stores the 'key_name' (eg, identifier for
4708 "pathForResource"), the 'arg_type' (eg, tree representing a
4709 NSString *), the 'arg_name' (eg identifier for "resource") and
4710 potentially some attributes (for example, a tree representing
4711 __attribute__ ((unused)) if such an attribute was attached to a
4712 certain parameter). You can access this information using the
4713 TREE_TYPE (for arg_type), KEYWORD_ARG_NAME (for arg_name),
4714 KEYWORD_KEY_NAME (for key_name), DECL_ATTRIBUTES (for attributes).
264fa2db 4715
d764a8e6
IS
4716 'key_name' is an identifier node (and is optional as you can omit
4717 it in Objective-C methods).
4718 'arg_type' is a tree list (and is optional too if no parameter type
4719 was specified).
4720 'arg_name' is an identifier node and is required.
4721 'attributes' is an optional tree containing parameter attributes. */
4722tree
944fb799 4723objc_build_keyword_decl (tree key_name, tree arg_type,
d764a8e6
IS
4724 tree arg_name, tree attributes)
4725{
4726 tree keyword_decl;
264fa2db 4727
d764a8e6
IS
4728 if (flag_objc1_only && attributes)
4729 error_at (input_location, "method argument attributes are not available in Objective-C 1.0");
264fa2db 4730
d764a8e6
IS
4731 /* If no type is specified, default to "id". */
4732 arg_type = adjust_type_for_id_default (arg_type);
325c3691 4733
d764a8e6 4734 keyword_decl = make_node (KEYWORD_DECL);
f9417da1 4735
d764a8e6
IS
4736 TREE_TYPE (keyword_decl) = arg_type;
4737 KEYWORD_ARG_NAME (keyword_decl) = arg_name;
4738 KEYWORD_KEY_NAME (keyword_decl) = key_name;
4739 DECL_ATTRIBUTES (keyword_decl) = attributes;
264fa2db 4740
d764a8e6 4741 return keyword_decl;
264fa2db 4742}
093c7153 4743
d764a8e6
IS
4744/* Given a chain of keyword_decl's, synthesize the full keyword selector. */
4745static tree
4746build_keyword_selector (tree selector)
264fa2db 4747{
d764a8e6
IS
4748 int len = 0;
4749 tree key_chain, key_name;
4750 char *buf;
264fa2db 4751
d764a8e6
IS
4752 /* Scan the selector to see how much space we'll need. */
4753 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
5287cfd5 4754 {
d764a8e6 4755 switch (TREE_CODE (selector))
46270f14 4756 {
d764a8e6
IS
4757 case KEYWORD_DECL:
4758 key_name = KEYWORD_KEY_NAME (key_chain);
4759 break;
4760 case TREE_LIST:
4761 key_name = TREE_PURPOSE (key_chain);
4762 break;
4763 default:
4764 gcc_unreachable ();
46270f14 4765 }
d764a8e6
IS
4766
4767 if (key_name)
4768 len += IDENTIFIER_LENGTH (key_name) + 1;
4769 else
4770 /* Just a ':' arg. */
4771 len++;
46270f14 4772 }
5287cfd5 4773
d764a8e6
IS
4774 buf = (char *) alloca (len + 1);
4775 /* Start the buffer out as an empty string. */
4776 buf[0] = '\0';
093c7153 4777
d764a8e6 4778 for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
093c7153 4779 {
d764a8e6 4780 switch (TREE_CODE (selector))
093c7153 4781 {
d764a8e6
IS
4782 case KEYWORD_DECL:
4783 key_name = KEYWORD_KEY_NAME (key_chain);
4784 break;
4785 case TREE_LIST:
4786 key_name = TREE_PURPOSE (key_chain);
4787 /* The keyword decl chain will later be used as a function
4788 argument chain. Unhook the selector itself so as to not
4789 confuse other parts of the compiler. */
4790 TREE_PURPOSE (key_chain) = NULL_TREE;
4791 break;
4792 default:
4793 gcc_unreachable ();
093c7153 4794 }
264fa2db 4795
d764a8e6
IS
4796 if (key_name)
4797 strcat (buf, IDENTIFIER_POINTER (key_name));
4798 strcat (buf, ":");
4799 }
264fa2db 4800
1328049a 4801 return get_identifier_with_length (buf, len);
264fa2db
ZL
4802}
4803
d764a8e6 4804/* Used for declarations and definitions. */
093c7153
RH
4805
4806static tree
d764a8e6
IS
4807build_method_decl (enum tree_code code, tree ret_type, tree selector,
4808 tree add_args, bool ellipsis)
264fa2db 4809{
d764a8e6 4810 tree method_decl;
264fa2db 4811
d764a8e6
IS
4812 /* If no type is specified, default to "id". */
4813 ret_type = adjust_type_for_id_default (ret_type);
264fa2db 4814
d764a8e6
IS
4815 /* Note how a method_decl has a TREE_TYPE which is not the function
4816 type of the function implementing the method, but only the return
4817 type of the method. We may want to change this, and store the
4818 entire function type in there (eg, it may be used to simplify
4819 dealing with attributes below). */
4820 method_decl = make_node (code);
4821 TREE_TYPE (method_decl) = ret_type;
264fa2db 4822
d764a8e6
IS
4823 /* If we have a keyword selector, create an identifier_node that
4824 represents the full selector name (`:' included)... */
4825 if (TREE_CODE (selector) == KEYWORD_DECL)
4826 {
4827 METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
4828 METHOD_SEL_ARGS (method_decl) = selector;
4829 METHOD_ADD_ARGS (method_decl) = add_args;
4830 METHOD_ADD_ARGS_ELLIPSIS_P (method_decl) = ellipsis;
4831 }
4832 else
4833 {
4834 METHOD_SEL_NAME (method_decl) = selector;
4835 METHOD_SEL_ARGS (method_decl) = NULL_TREE;
4836 METHOD_ADD_ARGS (method_decl) = NULL_TREE;
4837 }
264fa2db 4838
d764a8e6 4839 return method_decl;
264fa2db
ZL
4840}
4841
d764a8e6 4842/* This routine processes objective-c method attributes. */
64ee9490 4843
d764a8e6
IS
4844static void
4845objc_decl_method_attributes (tree *node, tree attributes, int flags)
264fa2db 4846{
d764a8e6
IS
4847 /* TODO: Replace the hackery below. An idea would be to store the
4848 full function type in the method declaration (for example in
4849 TREE_TYPE) and then expose ObjC method declarations to c-family
4850 and they could deal with them by simply treating them as
4851 functions. */
264fa2db 4852
d764a8e6
IS
4853 /* Because of the dangers in the hackery below, we filter out any
4854 attribute that we do not know about. For the ones we know about,
4855 we know that they work with the hackery. For the other ones,
4856 there is no guarantee, so we have to filter them out. */
4857 tree filtered_attributes = NULL_TREE;
264fa2db 4858
d764a8e6 4859 if (attributes)
264fa2db 4860 {
d764a8e6
IS
4861 tree attribute;
4862 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
093c7153 4863 {
d764a8e6 4864 tree name = TREE_PURPOSE (attribute);
944fb799 4865
d764a8e6
IS
4866 if (is_attribute_p ("deprecated", name)
4867 || is_attribute_p ("sentinel", name)
4868 || is_attribute_p ("noreturn", name))
093c7153 4869 {
d764a8e6
IS
4870 /* An attribute that we support; add it to the filtered
4871 attributes. */
944fb799 4872 filtered_attributes = chainon (filtered_attributes,
d764a8e6 4873 copy_node (attribute));
093c7153 4874 }
d764a8e6
IS
4875 else if (is_attribute_p ("format", name))
4876 {
4877 /* "format" is special because before adding it to the
4878 filtered attributes we need to adjust the specified
4879 format by adding the hidden function parameters for
4880 an Objective-C method (self, _cmd). */
4881 tree new_attribute = copy_node (attribute);
4882
4883 /* Check the arguments specified with the attribute, and
4884 modify them adding 2 for the two hidden arguments.
4885 Note how this differs from C++; according to the
4886 specs, C++ does not do it so you have to add the +1
4887 yourself. For Objective-C, instead, the compiler
4888 adds the +2 for you. */
4889
4890 /* The attribute arguments have not been checked yet, so
4891 we need to be careful as they could be missing or
4892 invalid. If anything looks wrong, we skip the
4893 process and the compiler will complain about it later
4894 when it validates the attribute. */
4895 /* Check that we have at least three arguments. */
4896 if (TREE_VALUE (new_attribute)
4897 && TREE_CHAIN (TREE_VALUE (new_attribute))
4898 && TREE_CHAIN (TREE_CHAIN (TREE_VALUE (new_attribute))))
4899 {
4900 tree second_argument = TREE_CHAIN (TREE_VALUE (new_attribute));
4901 tree third_argument = TREE_CHAIN (second_argument);
4902 tree number;
264fa2db 4903
d764a8e6
IS
4904 /* This is the second argument, the "string-index",
4905 which specifies the index of the format string
4906 argument. Add 2. */
4907 number = TREE_VALUE (second_argument);
4908 if (number
4909 && TREE_CODE (number) == INTEGER_CST
807e902e
KZ
4910 && !wi::eq_p (number, 0))
4911 TREE_VALUE (second_argument)
4912 = wide_int_to_tree (TREE_TYPE (number),
4913 wi::add (number, 2));
944fb799 4914
d764a8e6
IS
4915 /* This is the third argument, the "first-to-check",
4916 which specifies the index of the first argument to
4917 check. This could be 0, meaning it is not available,
4918 in which case we don't need to add 2. Add 2 if not
4919 0. */
4920 number = TREE_VALUE (third_argument);
4921 if (number
4922 && TREE_CODE (number) == INTEGER_CST
807e902e
KZ
4923 && !wi::eq_p (number, 0))
4924 TREE_VALUE (third_argument)
4925 = wide_int_to_tree (TREE_TYPE (number),
4926 wi::add (number, 2));
d764a8e6
IS
4927 }
4928 filtered_attributes = chainon (filtered_attributes,
4929 new_attribute);
4930 }
98ab0248
NP
4931 else if (is_attribute_p ("nonnull", name))
4932 {
4933 /* We need to fixup all the argument indexes by adding 2
4934 for the two hidden arguments of an Objective-C method
4935 invocation, similat to what we do above for the
4936 "format" attribute. */
4937 /* FIXME: This works great in terms of implementing the
4938 functionality, but the warnings that are produced by
4939 nonnull do mention the argument index (while the
4940 format ones don't). For example, you could get
4941 "warning: null argument where non-null required
4942 (argument 3)". Now in that message, "argument 3"
4943 includes the 2 hidden arguments; it would be much
4944 more friendly to call it "argument 1", as that would
4945 be consistent with __attribute__ ((nonnnull (1))).
4946 To do this, we'd need to have the C family code that
4947 checks the arguments know about adding/removing 2 to
4948 the argument index ... or alternatively we could
4949 maybe store the "printable" argument index in
4950 addition to the actual argument index ? Some
4951 refactoring is needed to do this elegantly. */
4952 tree new_attribute = copy_node (attribute);
4953 tree argument = TREE_VALUE (attribute);
4954 while (argument != NULL_TREE)
4955 {
4956 /* Get the value of the argument and add 2. */
4957 tree number = TREE_VALUE (argument);
807e902e
KZ
4958 if (number && TREE_CODE (number) == INTEGER_CST
4959 && !wi::eq_p (number, 0))
4960 TREE_VALUE (argument)
4961 = wide_int_to_tree (TREE_TYPE (number),
4962 wi::add (number, 2));
98ab0248
NP
4963 argument = TREE_CHAIN (argument);
4964 }
4965
4966 filtered_attributes = chainon (filtered_attributes,
4967 new_attribute);
4968 }
d764a8e6
IS
4969 else
4970 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
093c7153
RH
4971 }
4972 }
264fa2db 4973
d764a8e6 4974 if (filtered_attributes)
264fa2db 4975 {
d764a8e6
IS
4976 /* This hackery changes the TREE_TYPE of the ObjC method
4977 declaration to be a function type, so that decl_attributes
4978 will treat the ObjC method as if it was a function. Some
4979 attributes (sentinel, format) will be applied to the function
4980 type, changing it in place; so after calling decl_attributes,
4981 we extract the function type attributes and store them in
4982 METHOD_TYPE_ATTRIBUTES. Some other attributes (noreturn,
4983 deprecated) are applied directly to the method declaration
4984 (by setting TREE_DEPRECATED and TREE_THIS_VOLATILE) so there
4985 is nothing to do. */
4986 tree saved_type = TREE_TYPE (*node);
6174da1b
NF
4987 TREE_TYPE (*node)
4988 = build_function_type_for_method (TREE_VALUE (saved_type), *node,
4989 METHOD_REF, 0);
d764a8e6
IS
4990 decl_attributes (node, filtered_attributes, flags);
4991 METHOD_TYPE_ATTRIBUTES (*node) = TYPE_ATTRIBUTES (TREE_TYPE (*node));
4992 TREE_TYPE (*node) = saved_type;
264fa2db 4993 }
264fa2db
ZL
4994}
4995
944fb799 4996bool
d764a8e6
IS
4997objc_method_decl (enum tree_code opcode)
4998{
4999 return opcode == INSTANCE_METHOD_DECL || opcode == CLASS_METHOD_DECL;
5000}
264fa2db 5001
6174da1b
NF
5002/* Return a function type for METHOD with RETURN_TYPE. CONTEXT is
5003 either METHOD_DEF or METHOD_REF, indicating whether we are defining a
5004 method or calling one. SUPER_FLAG indicates whether this is a send
5005 to super; this makes a difference for the NeXT calling sequence in
5006 which the lookup and the method call are done together. If METHOD is
5007 NULL, user-defined arguments (i.e., beyond self and _cmd) shall be
5008 represented as varargs. */
264fa2db 5009
d764a8e6 5010tree
6174da1b
NF
5011build_function_type_for_method (tree return_type, tree method,
5012 int context, bool super_flag)
093c7153 5013{
9771b263 5014 vec<tree, va_gc> *argtypes = make_tree_vector ();
6174da1b
NF
5015 tree t, ftype;
5016 bool is_varargs = false;
093c7153 5017
6174da1b 5018 (*runtime.get_arg_type_list_base) (&argtypes, method, context, super_flag);
093c7153 5019
6174da1b
NF
5020 /* No actual method prototype given; remaining args passed as varargs. */
5021 if (method == NULL_TREE)
5022 {
5023 is_varargs = true;
5024 goto build_ftype;
5025 }
093c7153 5026
6174da1b 5027 for (t = METHOD_SEL_ARGS (method); t; t = DECL_CHAIN (t))
d764a8e6 5028 {
6174da1b 5029 tree arg_type = TREE_VALUE (TREE_TYPE (t));
093c7153 5030
6174da1b
NF
5031 /* Decay argument types for the underlying C function as
5032 appropriate. */
d764a8e6 5033 arg_type = objc_decay_parm_type (arg_type);
093c7153 5034
9771b263 5035 vec_safe_push (argtypes, arg_type);
d764a8e6 5036 }
093c7153 5037
6174da1b 5038 if (METHOD_ADD_ARGS (method))
264fa2db 5039 {
6174da1b
NF
5040 for (t = TREE_CHAIN (METHOD_ADD_ARGS (method));
5041 t; t = TREE_CHAIN (t))
d764a8e6 5042 {
6174da1b 5043 tree arg_type = TREE_TYPE (TREE_VALUE (t));
d764a8e6
IS
5044
5045 arg_type = objc_decay_parm_type (arg_type);
093c7153 5046
9771b263 5047 vec_safe_push (argtypes, arg_type);
d764a8e6 5048 }
093c7153 5049
6174da1b
NF
5050 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
5051 is_varargs = true;
264fa2db 5052 }
6174da1b
NF
5053
5054 build_ftype:
5055 if (is_varargs)
5056 ftype = build_varargs_function_type_vec (return_type, argtypes);
093c7153 5057 else
6174da1b 5058 ftype = build_function_type_vec (return_type, argtypes);
264fa2db 5059
6174da1b
NF
5060 release_tree_vector (argtypes);
5061 return ftype;
d764a8e6 5062}
264fa2db 5063
3cc2dd4b
NP
5064/* The 'method' argument is a tree; this tree could either be a single
5065 method, which is returned, or could be a TREE_VEC containing a list
5066 of methods. In that case, the first one is returned, and warnings
5067 are issued as appropriate. */
d764a8e6 5068static tree
3cc2dd4b 5069check_duplicates (tree method, int methods, int is_class)
093c7153 5070{
3cc2dd4b
NP
5071 tree first_method;
5072 size_t i;
264fa2db 5073
3cc2dd4b
NP
5074 if (method == NULL_TREE)
5075 return NULL_TREE;
d764a8e6 5076
3cc2dd4b
NP
5077 if (TREE_CODE (method) != TREE_VEC)
5078 return method;
5079
5080 /* We have two or more methods with the same name but different
5081 types. */
5082 first_method = TREE_VEC_ELT (method, 0);
5083
5084 /* But just how different are those types? If
5085 -Wno-strict-selector-match is specified, we shall not complain if
5086 the differences are solely among types with identical size and
5087 alignment. */
5088 if (!warn_strict_selector_match)
5089 {
5b6b2942 5090 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
3cc2dd4b
NP
5091 if (!comp_proto_with_proto (first_method, TREE_VEC_ELT (method, i), 0))
5092 goto issue_warning;
5093
5094 return first_method;
093c7153 5095 }
3cc2dd4b
NP
5096
5097 issue_warning:
5098 if (methods)
5099 {
5100 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5101
5102 warning_at (input_location, 0,
5103 "multiple methods named %<%c%E%> found",
5104 (is_class ? '+' : '-'),
5105 METHOD_SEL_NAME (first_method));
5106 inform (DECL_SOURCE_LOCATION (first_method), "using %<%c%s%>",
5107 (type ? '-' : '+'),
5108 identifier_to_locale (gen_method_decl (first_method)));
5109 }
5110 else
5111 {
5112 bool type = TREE_CODE (first_method) == INSTANCE_METHOD_DECL;
5113
5114 warning_at (input_location, 0,
5115 "multiple selectors named %<%c%E%> found",
5116 (is_class ? '+' : '-'),
5117 METHOD_SEL_NAME (first_method));
5118 inform (DECL_SOURCE_LOCATION (first_method), "found %<%c%s%>",
5119 (type ? '-' : '+'),
5120 identifier_to_locale (gen_method_decl (first_method)));
5121 }
5122
5b6b2942 5123 for (i = 0; i < (size_t) TREE_VEC_LENGTH (method); i++)
3cc2dd4b
NP
5124 {
5125 bool type = TREE_CODE (TREE_VEC_ELT (method, i)) == INSTANCE_METHOD_DECL;
5126
5127 inform (DECL_SOURCE_LOCATION (TREE_VEC_ELT (method, i)), "also found %<%c%s%>",
5128 (type ? '-' : '+'),
5129 identifier_to_locale (gen_method_decl (TREE_VEC_ELT (method, i))));
5130 }
5131
5132 return first_method;
093c7153 5133}
264fa2db 5134
d764a8e6
IS
5135/* If RECEIVER is a class reference, return the identifier node for
5136 the referenced class. RECEIVER is created by objc_get_class_reference,
5137 so we check the exact form created depending on which runtimes are
5138 used. */
264fa2db 5139
d764a8e6
IS
5140static tree
5141receiver_is_class_object (tree receiver, int self, int super)
093c7153 5142{
d764a8e6 5143 tree exp, arg;
264fa2db 5144
d764a8e6
IS
5145 /* The receiver is 'self' or 'super' in the context of a class method. */
5146 if (objc_method_context
5147 && TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
5148 && (self || super))
5149 return (super
5150 ? CLASS_SUPER_NAME (implementation_template)
5151 : CLASS_NAME (implementation_template));
5152
5153 /* The runtime might encapsulate things its own way. */
5154 exp = (*runtime.receiver_is_class_object) (receiver);
5155 if (exp)
5156 return exp;
5157
5158 /* The receiver is a function call that returns an id. Check if
419b55d0
NP
5159 it is a call to objc_getClass, if so, pick up the class name.
5160
5161 This is required by the GNU runtime, which compiles
5162
5163 [NSObject alloc]
5164
5165 into
5166
5167 [objc_get_class ("NSObject") alloc];
5168
5169 and then, to check that the receiver responds to the +alloc
5170 method, needs to be able to determine that the objc_get_class()
5171 call returns the NSObject class and not just a generic Class
5172 pointer.
5173
5174 But, traditionally this is enabled for all runtimes, not just the
5175 GNU one, which means that the compiler is smarter than you'd
5176 expect when dealing with objc_getClass(). For example, with the
5177 Apple runtime, in the code
5178
5179 [objc_getClass ("NSObject") alloc];
5180
5181 the compiler will recognize the objc_getClass() call as special
5182 (due to the code below) and so will know that +alloc is called on
5183 the 'NSObject' class, and can perform the corresponding checks.
5184
5185 Programmers can disable this behaviour by casting the results of
5186 objc_getClass() to 'Class' (this may seem weird because
5187 objc_getClass() is already declared to return 'Class', but the
5188 compiler treats it as a special function). This may be useful if
5189 the class is never declared, and the compiler would complain
5190 about a missing @interface for it. Then, you can do
5191
5192 [(Class)objc_getClass ("MyClassNeverDeclared") alloc];
5193
5194 to silence the warnings. */
d764a8e6
IS
5195 if (TREE_CODE (receiver) == CALL_EXPR
5196 && (exp = CALL_EXPR_FN (receiver))
5197 && TREE_CODE (exp) == ADDR_EXPR
5198 && (exp = TREE_OPERAND (exp, 0))
5199 && TREE_CODE (exp) == FUNCTION_DECL
5200 /* For some reason, we sometimes wind up with multiple FUNCTION_DECL
5201 prototypes for objc_get_class(). Thankfully, they seem to share the
5202 same function type. */
5203 && TREE_TYPE (exp) == TREE_TYPE (objc_get_class_decl)
5204 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (exp)), runtime.tag_getclass)
5205 /* We have a call to objc_get_class/objc_getClass! */
5206 && (arg = CALL_EXPR_ARG (receiver, 0)))
5207 {
5208 STRIP_NOPS (arg);
5209 if (TREE_CODE (arg) == ADDR_EXPR
5210 && (arg = TREE_OPERAND (arg, 0))
5211 && TREE_CODE (arg) == STRING_CST)
5212 /* Finally, we have the class name. */
5213 return get_identifier (TREE_STRING_POINTER (arg));
5214 }
5215 return 0;
264fa2db
ZL
5216}
5217
d764a8e6
IS
5218/* If we are currently building a message expr, this holds
5219 the identifier of the selector of the message. This is
5220 used when printing warnings about argument mismatches. */
264fa2db 5221
d764a8e6
IS
5222static tree current_objc_message_selector = 0;
5223
5224tree
5225objc_message_selector (void)
093c7153 5226{
d764a8e6 5227 return current_objc_message_selector;
093c7153 5228}
264fa2db 5229
d764a8e6
IS
5230/* Construct an expression for sending a message.
5231 MESS has the object to send to in TREE_PURPOSE
5232 and the argument list (including selector) in TREE_VALUE.
5233
5234 (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
5235 (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...); */
264fa2db 5236
38b9c8c3 5237tree
eb345401 5238objc_build_message_expr (tree receiver, tree message_args)
093c7153 5239{
d764a8e6
IS
5240 tree sel_name;
5241#ifdef OBJCPLUS
eb345401 5242 tree args = TREE_PURPOSE (message_args);
d764a8e6 5243#else
eb345401 5244 tree args = message_args;
d764a8e6
IS
5245#endif
5246 tree method_params = NULL_TREE;
093c7153 5247
d764a8e6
IS
5248 if (TREE_CODE (receiver) == ERROR_MARK || TREE_CODE (args) == ERROR_MARK)
5249 return error_mark_node;
264fa2db 5250
d764a8e6
IS
5251 /* Obtain the full selector name. */
5252 switch (TREE_CODE (args))
093c7153 5253 {
d764a8e6
IS
5254 case IDENTIFIER_NODE:
5255 /* A unary selector. */
5256 sel_name = args;
5257 break;
5258 case TREE_LIST:
5259 sel_name = build_keyword_selector (args);
5260 break;
5261 default:
5262 gcc_unreachable ();
093c7153 5263 }
d764a8e6
IS
5264
5265 /* Build the parameter list to give to the method. */
5266 if (TREE_CODE (args) == TREE_LIST)
5267#ifdef OBJCPLUS
eb345401 5268 method_params = chainon (args, TREE_VALUE (message_args));
d764a8e6 5269#else
093c7153 5270 {
d764a8e6
IS
5271 tree chain = args, prev = NULL_TREE;
5272
5273 /* We have a keyword selector--check for comma expressions. */
5274 while (chain)
093c7153 5275 {
d764a8e6
IS
5276 tree element = TREE_VALUE (chain);
5277
5278 /* We have a comma expression, must collapse... */
5279 if (TREE_CODE (element) == TREE_LIST)
5280 {
5281 if (prev)
5282 TREE_CHAIN (prev) = element;
5283 else
5284 args = element;
5285 }
5286 prev = chain;
5287 chain = TREE_CHAIN (chain);
5288 }
5289 method_params = args;
093c7153 5290 }
d764a8e6 5291#endif
093c7153 5292
d764a8e6
IS
5293#ifdef OBJCPLUS
5294 if (processing_template_decl)
5295 /* Must wait until template instantiation time. */
d7faa110
PC
5296 return build_min_nt_loc (UNKNOWN_LOCATION, MESSAGE_SEND_EXPR, receiver,
5297 sel_name, method_params);
d764a8e6
IS
5298#endif
5299
5300 return objc_finish_message_expr (receiver, sel_name, method_params, NULL);
264fa2db
ZL
5301}
5302
d764a8e6
IS
5303/* Look up method SEL_NAME that would be suitable for receiver
5304 of type 'id' (if IS_CLASS is zero) or 'Class' (if IS_CLASS is
5305 nonzero), and report on any duplicates. */
506e2710 5306
d764a8e6
IS
5307static tree
5308lookup_method_in_hash_lists (tree sel_name, int is_class)
5309{
3cc2dd4b 5310 tree method_prototype = OBJC_MAP_NOT_FOUND;
264fa2db 5311
d764a8e6 5312 if (!is_class)
3cc2dd4b
NP
5313 method_prototype = objc_map_get (instance_method_map, sel_name);
5314
5315 if (method_prototype == OBJC_MAP_NOT_FOUND)
6347cf31 5316 {
3cc2dd4b 5317 method_prototype = objc_map_get (class_method_map, sel_name);
d764a8e6 5318 is_class = 1;
3cc2dd4b
NP
5319
5320 if (method_prototype == OBJC_MAP_NOT_FOUND)
5321 return NULL_TREE;
6347cf31 5322 }
093c7153 5323
d764a8e6 5324 return check_duplicates (method_prototype, 1, is_class);
264fa2db
ZL
5325}
5326
d764a8e6
IS
5327/* The 'objc_finish_message_expr' routine is called from within
5328 'objc_build_message_expr' for non-template functions. In the case of
5329 C++ template functions, it is called from 'build_expr_from_tree'
5330 (in decl2.c) after RECEIVER and METHOD_PARAMS have been expanded.
5331
5332 If the DEPRECATED_METHOD_PROTOTYPE argument is NULL, then we warn
5333 if the method being used is deprecated. If it is not NULL, instead
5334 of deprecating, we set *DEPRECATED_METHOD_PROTOTYPE to the method
5335 prototype that was used and is deprecated. This is useful for
5336 getter calls that are always generated when compiling dot-syntax
5337 expressions, even if they may not be used. In that case, we don't
5338 want the warning immediately; we produce it (if needed) at gimplify
5339 stage when we are sure that the deprecated getter is being
5340 used. */
38b9c8c3 5341tree
d764a8e6
IS
5342objc_finish_message_expr (tree receiver, tree sel_name, tree method_params,
5343 tree *deprecated_method_prototype)
37153b1e 5344{
d764a8e6
IS
5345 tree method_prototype = NULL_TREE, rprotos = NULL_TREE, rtype;
5346 tree retval, class_tree;
5347 int self, super, have_cast;
37153b1e 5348
d764a8e6
IS
5349 /* We have used the receiver, so mark it as read. */
5350 mark_exp_read (receiver);
5351
5352 /* Extract the receiver of the message, as well as its type
5353 (where the latter may take the form of a cast or be inferred
5354 from the implementation context). */
5355 rtype = receiver;
5356 while (TREE_CODE (rtype) == COMPOUND_EXPR
bf79cedb
NP
5357 || TREE_CODE (rtype) == MODIFY_EXPR
5358 || CONVERT_EXPR_P (rtype)
5359 || TREE_CODE (rtype) == COMPONENT_REF)
d764a8e6
IS
5360 rtype = TREE_OPERAND (rtype, 0);
5361
bf79cedb 5362 /* self is 1 if this is a message to self, 0 otherwise */
d764a8e6 5363 self = (rtype == self_decl);
bf79cedb
NP
5364
5365 /* super is 1 if this is a message to super, 0 otherwise. */
d764a8e6 5366 super = (rtype == UOBJC_SUPER_decl);
bf79cedb
NP
5367
5368 /* rtype is the type of the receiver. */
d764a8e6
IS
5369 rtype = TREE_TYPE (receiver);
5370
bf79cedb 5371 /* have_cast is 1 if the receiver is casted. */
d764a8e6
IS
5372 have_cast = (TREE_CODE (receiver) == NOP_EXPR
5373 || (TREE_CODE (receiver) == COMPOUND_EXPR
5374 && !IS_SUPER (rtype)));
5375
5376 /* If we are calling [super dealloc], reset our warning flag. */
5377 if (super && !strcmp ("dealloc", IDENTIFIER_POINTER (sel_name)))
5378 should_call_super_dealloc = 0;
5379
5380 /* If the receiver is a class object, retrieve the corresponding
bf79cedb
NP
5381 @interface, if one exists. class_tree is the class name
5382 identifier, or NULL_TREE if this is not a class method or the
5383 class name could not be determined (as in the case "Class c; [c
5384 method];"). */
d764a8e6
IS
5385 class_tree = receiver_is_class_object (receiver, self, super);
5386
5387 /* Now determine the receiver type (if an explicit cast has not been
5388 provided). */
5389 if (!have_cast)
37153b1e 5390 {
d764a8e6 5391 if (class_tree)
bf79cedb
NP
5392 {
5393 /* We are here when we have no cast, and we have a class
5394 name. So, this is a plain method to a class object, as
5395 in [NSObject alloc]. Find the interface corresponding to
5396 the class name. */
5397 rtype = lookup_interface (class_tree);
5398
5399 if (rtype == NULL_TREE)
5400 {
5401 /* If 'rtype' is NULL_TREE at this point it means that
5402 we have seen no @interface corresponding to that
419b55d0
NP
5403 class name, only a @class declaration (alternatively,
5404 this was a call such as [objc_getClass("SomeClass")
5405 alloc], where we've never seen the @interface of
5406 SomeClass). So, we have a class name (class_tree)
5407 but no actual details of the class methods. We won't
5408 be able to check that the class responds to the
5409 method, and we will have to guess the method
5410 prototype. Emit a warning, then keep going (this
5411 will use any method with a matching name, as if the
5412 receiver was of type 'Class'). */
bf79cedb
NP
5413 warning (0, "@interface of class %qE not found", class_tree);
5414 }
5415 }
d764a8e6
IS
5416 /* Handle `self' and `super'. */
5417 else if (super)
37153b1e 5418 {
d764a8e6
IS
5419 if (!CLASS_SUPER_NAME (implementation_template))
5420 {
5421 error ("no super class declared in @interface for %qE",
5422 CLASS_NAME (implementation_template));
5423 return error_mark_node;
5424 }
5425 rtype = lookup_interface (CLASS_SUPER_NAME (implementation_template));
37153b1e 5426 }
d764a8e6
IS
5427 else if (self)
5428 rtype = lookup_interface (CLASS_NAME (implementation_template));
37153b1e 5429 }
d764a8e6 5430
d764a8e6 5431 if (objc_is_id (rtype))
37153b1e 5432 {
bf79cedb
NP
5433 /* The receiver is of type 'id' or 'Class' (with or without some
5434 protocols attached to it). */
5435
5436 /* We set class_tree to the identifier for 'Class' if this is a
5437 class method, and to NULL_TREE if not. */
d764a8e6 5438 class_tree = (IS_CLASS (rtype) ? objc_class_name : NULL_TREE);
bf79cedb
NP
5439
5440 /* 'rprotos' is the list of protocols that the receiver
5441 supports. */
d764a8e6
IS
5442 rprotos = (TYPE_HAS_OBJC_INFO (TREE_TYPE (rtype))
5443 ? TYPE_OBJC_PROTOCOL_LIST (TREE_TYPE (rtype))
5444 : NULL_TREE);
bf79cedb
NP
5445
5446 /* We have no information on the type, and we set it to
5447 NULL_TREE. */
d764a8e6
IS
5448 rtype = NULL_TREE;
5449
bf79cedb
NP
5450 /* If there are any protocols, check that the method we are
5451 calling appears in the protocol list. If there are no
5452 protocols, this is a message to 'id' or 'Class' and we accept
5453 any method that exists. */
d764a8e6
IS
5454 if (rprotos)
5455 {
bf79cedb
NP
5456 /* If messaging 'id <Protos>' or 'Class <Proto>', first
5457 search in protocols themselves for the method
5458 prototype. */
d764a8e6
IS
5459 method_prototype
5460 = lookup_method_in_protocol_list (rprotos, sel_name,
5461 class_tree != NULL_TREE);
5462
bf79cedb
NP
5463 /* If messaging 'Class <Proto>' but did not find a class
5464 method prototype, search for an instance method instead,
5465 and warn about having done so. */
d764a8e6
IS
5466 if (!method_prototype && !rtype && class_tree != NULL_TREE)
5467 {
5468 method_prototype
5469 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5470
5471 if (method_prototype)
5472 warning (0, "found %<-%E%> instead of %<+%E%> in protocol(s)",
5473 sel_name, sel_name);
5474 }
5475 }
37153b1e 5476 }
d764a8e6 5477 else if (rtype)
37153b1e 5478 {
bf79cedb
NP
5479 /* We have a receiver type which is more specific than 'id' or
5480 'Class'. */
d764a8e6 5481 tree orig_rtype = rtype;
37153b1e 5482
d764a8e6
IS
5483 if (TREE_CODE (rtype) == POINTER_TYPE)
5484 rtype = TREE_TYPE (rtype);
5485 /* Traverse typedef aliases */
5486 while (TREE_CODE (rtype) == RECORD_TYPE && OBJC_TYPE_NAME (rtype)
5487 && TREE_CODE (OBJC_TYPE_NAME (rtype)) == TYPE_DECL
5488 && DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype)))
5489 rtype = DECL_ORIGINAL_TYPE (OBJC_TYPE_NAME (rtype));
5490 if (TYPED_OBJECT (rtype))
5491 {
5492 rprotos = TYPE_OBJC_PROTOCOL_LIST (rtype);
5493 rtype = TYPE_OBJC_INTERFACE (rtype);
5494 }
d764a8e6
IS
5495 if (!rtype || TREE_CODE (rtype) == IDENTIFIER_NODE)
5496 {
bf79cedb
NP
5497 /* If we could not find an @interface declaration, we must
5498 have only seen a @class declaration; so, we cannot say
5499 anything more intelligent about which methods the
5500 receiver will understand. Note that this only happens
5501 for instance methods; for class methods to a class where
5502 we have only seen a @class declaration,
5503 lookup_interface() above would have set rtype to
5504 NULL_TREE. */
5505 if (rprotos)
5506 {
5507 /* We could not find an @interface declaration, yet, if
5508 there are protocols attached to the type, we can
5509 still look up the method in the protocols. Ie, we
5510 are in the following case:
5511
5512 @class MyClass;
5513 MyClass<MyProtocol> *x;
5514 [x method];
5515
5516 If 'MyProtocol' has the method 'method', we can check
5517 and retrieve the method prototype. */
5518 method_prototype
5519 = lookup_method_in_protocol_list (rprotos, sel_name, 0);
5520
5521 /* At this point, if we have found the method_prototype,
5522 we are quite happy. The details of the class are
5523 irrelevant. If we haven't found it, a warning will
5524 have been produced that the method could not be found
5525 in the protocol, and we won't produce further
5526 warnings (please note that this means that "@class
5527 MyClass; MyClass <MyProtocol> *x;" is exactly
5528 equivalent to "id <MyProtocol> x", which isn't too
5529 satisfactory but it's not easy to see how to do
5530 better). */
5531 }
5532 else
5533 {
5534 if (rtype)
5535 {
5536 /* We could not find an @interface declaration, and
5537 there are no protocols attached to the receiver,
5538 so we can't complete the check that the receiver
5539 responds to the method, and we can't retrieve the
5540 method prototype. But, because the receiver has
5541 a well-specified class, the programmer did want
5542 this check to be performed. Emit a warning, then
5543 keep going as if it was an 'id'. To remove the
5544 warning, either include an @interface for the
5545 class, or cast the receiver to 'id'. Note that
5546 rtype is an IDENTIFIER_NODE at this point. */
5547 warning (0, "@interface of class %qE not found", rtype);
5548 }
5549 }
5550
d764a8e6 5551 rtype = NULL_TREE;
d764a8e6
IS
5552 }
5553 else if (TREE_CODE (rtype) == CLASS_INTERFACE_TYPE
5554 || TREE_CODE (rtype) == CLASS_IMPLEMENTATION_TYPE)
5555 {
bf79cedb
NP
5556 /* We have a valid ObjC class name with an associated
5557 @interface. Look up the method name in the published
5558 @interface for the class (and its superclasses). */
d764a8e6
IS
5559 method_prototype
5560 = lookup_method_static (rtype, sel_name, class_tree != NULL_TREE);
37153b1e 5561
d764a8e6
IS
5562 /* If the method was not found in the @interface, it may still
5563 exist locally as part of the @implementation. */
5564 if (!method_prototype && objc_implementation_context
5565 && CLASS_NAME (objc_implementation_context)
5566 == OBJC_TYPE_NAME (rtype))
5567 method_prototype
5568 = lookup_method
5569 ((class_tree
5570 ? CLASS_CLS_METHODS (objc_implementation_context)
5571 : CLASS_NST_METHODS (objc_implementation_context)),
5572 sel_name);
37153b1e 5573
d764a8e6
IS
5574 /* If we haven't found a candidate method by now, try looking for
5575 it in the protocol list. */
5576 if (!method_prototype && rprotos)
5577 method_prototype
5578 = lookup_method_in_protocol_list (rprotos, sel_name,
5579 class_tree != NULL_TREE);
5580 }
5581 else
5582 {
bf79cedb 5583 /* We have a type, but it's not an Objective-C type (!). */
d764a8e6
IS
5584 warning (0, "invalid receiver type %qs",
5585 identifier_to_locale (gen_type_name (orig_rtype)));
5586 /* After issuing the "invalid receiver" warning, perform method
5587 lookup as if we were messaging 'id'. */
5588 rtype = rprotos = NULL_TREE;
5589 }
37153b1e 5590 }
bf79cedb
NP
5591 /* Note that rtype could also be NULL_TREE. This happens if we are
5592 messaging a class by name, but the class was only
5593 forward-declared using @class. */
264fa2db 5594
bf79cedb
NP
5595 /* For 'id' or 'Class' receivers, search in the global hash table as
5596 a last resort. For all receivers, warn if protocol searches have
5597 failed. */
d764a8e6
IS
5598 if (!method_prototype)
5599 {
5600 if (rprotos)
5601 warning (0, "%<%c%E%> not found in protocol(s)",
5602 (class_tree ? '+' : '-'),
5603 sel_name);
264fa2db 5604
d764a8e6
IS
5605 if (!rtype)
5606 method_prototype
5607 = lookup_method_in_hash_lists (sel_name, class_tree != NULL_TREE);
5608 }
264fa2db 5609
944fb799 5610 if (!method_prototype)
d764a8e6
IS
5611 {
5612 static bool warn_missing_methods = false;
264fa2db 5613
d764a8e6
IS
5614 if (rtype)
5615 warning (0, "%qE may not respond to %<%c%E%>",
5616 OBJC_TYPE_NAME (rtype),
5617 (class_tree ? '+' : '-'),
5618 sel_name);
5619 /* If we are messaging an 'id' or 'Class' object and made it here,
5620 then we have failed to find _any_ instance or class method,
5621 respectively. */
5622 else
5623 warning (0, "no %<%c%E%> method found",
5624 (class_tree ? '+' : '-'),
5625 sel_name);
38b9c8c3 5626
d764a8e6
IS
5627 if (!warn_missing_methods)
5628 {
944fb799 5629 warning_at (input_location,
d764a8e6 5630 0, "(Messages without a matching method signature");
944fb799 5631 warning_at (input_location,
d764a8e6 5632 0, "will be assumed to return %<id%> and accept");
944fb799 5633 warning_at (input_location,
d764a8e6
IS
5634 0, "%<...%> as arguments.)");
5635 warn_missing_methods = true;
5636 }
5637 }
5638 else
5639 {
5640 /* Warn if the method is deprecated, but not if the receiver is
5641 a generic 'id'. 'id' is used to cast an object to a generic
5642 object of an unspecified class; in that case, we'll use
5643 whatever method prototype we can find to get the method
5644 argument and return types, but it is not appropriate to
5645 produce deprecation warnings since we don't know the class
5646 that the object will be of at runtime. The @interface(s) for
5647 that class may not even be available to the compiler right
5648 now, and it is perfectly possible that the method is marked
5649 as non-deprecated in such @interface(s).
e31c7eec 5650
d764a8e6
IS
5651 In practice this makes sense since casting an object to 'id'
5652 is often used precisely to turn off warnings associated with
5653 the object being of a particular class. */
5654 if (TREE_DEPRECATED (method_prototype) && rtype != NULL_TREE)
5655 {
5656 if (deprecated_method_prototype)
5657 *deprecated_method_prototype = method_prototype;
5658 else
5659 warn_deprecated_use (method_prototype, NULL_TREE);
5660 }
5661 }
093c7153 5662
d764a8e6
IS
5663 /* Save the selector name for printing error messages. */
5664 current_objc_message_selector = sel_name;
093c7153 5665
d764a8e6 5666 /* Build the method call.
944fb799 5667 TODO: Get the location from somewhere that will work for delayed
d764a8e6 5668 expansion. */
944fb799 5669
d764a8e6
IS
5670 retval = (*runtime.build_objc_method_call) (input_location, method_prototype,
5671 receiver, rtype, sel_name,
5672 method_params, super);
093c7153 5673
d764a8e6 5674 current_objc_message_selector = 0;
093c7153 5675
d764a8e6
IS
5676 return retval;
5677}
5678\f
e31c7eec 5679
944fb799 5680/* This routine creates a static variable used to implement @protocol(MyProtocol)
d764a8e6
IS
5681 expression. This variable will be initialized to global protocol_t meta-data
5682 pointer. */
e31c7eec 5683
d764a8e6
IS
5684/* This function is called by the parser when (and only when) a
5685 @protocol() expression is found, in order to compile it. */
5686tree
5687objc_build_protocol_expr (tree protoname)
5688{
5689 tree p = lookup_protocol (protoname, /* warn if deprecated */ true,
5690 /* definition_required */ false);
d4e72c58 5691
d764a8e6
IS
5692 if (!p)
5693 {
5694 error ("cannot find protocol declaration for %qE", protoname);
5695 return error_mark_node;
e31c7eec 5696 }
e31c7eec 5697
d764a8e6
IS
5698 return (*runtime.get_protocol_reference) (input_location, p);
5699}
e31c7eec 5700
d764a8e6
IS
5701/* This function is called by the parser when a @selector() expression
5702 is found, in order to compile it. It is only called by the parser
5703 and only to compile a @selector(). LOC is the location of the
5704 @selector. */
5705tree
5706objc_build_selector_expr (location_t loc, tree selnamelist)
e31c7eec 5707{
d764a8e6 5708 tree selname;
e31c7eec 5709
d764a8e6
IS
5710 /* Obtain the full selector name. */
5711 switch (TREE_CODE (selnamelist))
5712 {
5713 case IDENTIFIER_NODE:
5714 /* A unary selector. */
5715 selname = selnamelist;
5716 break;
5717 case TREE_LIST:
5718 selname = build_keyword_selector (selnamelist);
5719 break;
5720 default:
5721 gcc_unreachable ();
5722 }
e31c7eec 5723
d764a8e6
IS
5724 /* If we are required to check @selector() expressions as they
5725 are found, check that the selector has been declared. */
5726 if (warn_undeclared_selector)
5727 {
5728 /* Look the selector up in the list of all known class and
5729 instance methods (up to this line) to check that the selector
5730 exists. */
3cc2dd4b 5731 tree method;
e31c7eec 5732
d764a8e6 5733 /* First try with instance methods. */
3cc2dd4b 5734 method = objc_map_get (instance_method_map, selname);
e31c7eec 5735
d764a8e6 5736 /* If not found, try with class methods. */
3cc2dd4b 5737 if (method == OBJC_MAP_NOT_FOUND)
d764a8e6 5738 {
3cc2dd4b 5739 method = objc_map_get (class_method_map, selname);
e31c7eec 5740
3cc2dd4b
NP
5741 /* If still not found, print out a warning. */
5742 if (method == OBJC_MAP_NOT_FOUND)
5743 warning (0, "undeclared selector %qE", selname);
d764a8e6
IS
5744 }
5745 }
e31c7eec 5746
d764a8e6
IS
5747 /* The runtimes do this differently, most particularly, GNU has typed
5748 selectors, whilst NeXT does not. */
5749 return (*runtime.build_selector_reference) (loc, selname, NULL_TREE);
e31c7eec
TW
5750}
5751
d764a8e6
IS
5752static tree
5753build_ivar_reference (tree id)
5754{
5755 tree base;
5756 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL)
5757 {
5758 /* Historically, a class method that produced objects (factory
5759 method) would assign `self' to the instance that it
5760 allocated. This would effectively turn the class method into
5761 an instance method. Following this assignment, the instance
5762 variables could be accessed. That practice, while safe,
5763 violates the simple rule that a class method should not refer
5764 to an instance variable. It's better to catch the cases
5765 where this is done unknowingly than to support the above
5766 paradigm. */
5767 warning (0, "instance variable %qE accessed in class method",
5768 id);
5769 self_decl = convert (objc_instance_type, self_decl); /* cast */
e31c7eec 5770 }
e31c7eec 5771
d764a8e6
IS
5772 base = build_indirect_ref (input_location, self_decl, RO_ARROW);
5773 return (*runtime.build_ivar_reference) (input_location, base, id);
e31c7eec
TW
5774}
5775
d764a8e6
IS
5776static void
5777hash_init (void)
5778{
3cc2dd4b
NP
5779 instance_method_map = objc_map_alloc_ggc (1000);
5780 class_method_map = objc_map_alloc_ggc (1000);
e31c7eec 5781
3cc2dd4b
NP
5782 class_name_map = objc_map_alloc_ggc (200);
5783 alias_name_map = objc_map_alloc_ggc (200);
e31c7eec 5784
d764a8e6 5785 /* Initialize the hash table used to hold the constant string objects. */
2a22f99c 5786 string_htab = hash_table<objc_string_hasher>::create_ggc (31);
e31c7eec
TW
5787}
5788
3cc2dd4b
NP
5789/* Use the following to add a method to class_method_map or
5790 instance_method_map. It will add the method, keyed by the
5791 METHOD_SEL_NAME. If the method already exists, but with one or
5792 more different prototypes, it will store a TREE_VEC in the map,
5793 with the method prototypes in the vector. */
d764a8e6 5794static void
3cc2dd4b 5795insert_method_into_method_map (bool class_method, tree method)
d764a8e6 5796{
3cc2dd4b
NP
5797 tree method_name = METHOD_SEL_NAME (method);
5798 tree existing_entry;
5799 objc_map_t map;
e31c7eec 5800
3cc2dd4b
NP
5801 if (class_method)
5802 map = class_method_map;
d764a8e6 5803 else
3cc2dd4b 5804 map = instance_method_map;
e31c7eec 5805
3cc2dd4b
NP
5806 /* Check if the method already exists in the map. */
5807 existing_entry = objc_map_get (map, method_name);
d764a8e6 5808
3cc2dd4b
NP
5809 /* If not, we simply add it to the map. */
5810 if (existing_entry == OBJC_MAP_NOT_FOUND)
5811 objc_map_put (map, method_name, method);
5812 else
d764a8e6 5813 {
3cc2dd4b
NP
5814 tree new_entry;
5815
5816 /* If an entry already exists, it's more complicated. We'll
5817 have to check whether the method prototype is the same or
5818 not. */
5819 if (TREE_CODE (existing_entry) != TREE_VEC)
5820 {
5821 /* If the method prototypes are the same, there is nothing
5822 to do. */
5823 if (comp_proto_with_proto (method, existing_entry, 1))
5824 return;
d764a8e6 5825
3cc2dd4b
NP
5826 /* If not, create a vector to store both the method already
5827 in the map, and the new one that we are adding. */
5828 new_entry = make_tree_vec (2);
5829
5830 TREE_VEC_ELT (new_entry, 0) = existing_entry;
5831 TREE_VEC_ELT (new_entry, 1) = method;
5832 }
5833 else
5834 {
5835 /* An entry already exists, and it's already a vector. This
5836 means that at least 2 different method prototypes were
5837 already found, and we're considering registering yet
5838 another one. */
5839 size_t i;
5840
5841 /* Check all the existing prototypes. If any matches the
5842 one we need to add, there is nothing to do because it's
5843 already there. */
5b6b2942 5844 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
3cc2dd4b
NP
5845 if (comp_proto_with_proto (method, TREE_VEC_ELT (existing_entry, i), 1))
5846 return;
d764a8e6 5847
3cc2dd4b
NP
5848 /* Else, create a new, bigger vector and add the new method
5849 at the end of it. This is inefficient but extremely
5850 rare; in any sane program most methods have a single
5851 prototype, and very few, if any, will have more than
5852 2! */
5853 new_entry = make_tree_vec (TREE_VEC_LENGTH (existing_entry) + 1);
5854
5855 /* Copy the methods from the existing vector. */
5b6b2942 5856 for (i = 0; i < (size_t) TREE_VEC_LENGTH (existing_entry); i++)
3cc2dd4b
NP
5857 TREE_VEC_ELT (new_entry, i) = TREE_VEC_ELT (existing_entry, i);
5858
5859 /* Add the new method at the end. */
5860 TREE_VEC_ELT (new_entry, i) = method;
5861 }
e31c7eec 5862
3cc2dd4b
NP
5863 /* Store the new vector in the map. */
5864 objc_map_put (map, method_name, new_entry);
e31c7eec 5865 }
d764a8e6 5866}
e31c7eec 5867
d764a8e6
IS
5868\f
5869static tree
5870lookup_method (tree mchain, tree method)
5871{
5872 tree key;
e31c7eec 5873
d764a8e6
IS
5874 if (TREE_CODE (method) == IDENTIFIER_NODE)
5875 key = method;
5876 else
5877 key = METHOD_SEL_NAME (method);
e31c7eec 5878
d764a8e6
IS
5879 while (mchain)
5880 {
5881 if (METHOD_SEL_NAME (mchain) == key)
5882 return mchain;
011d50d9 5883
d764a8e6 5884 mchain = DECL_CHAIN (mchain);
e31c7eec 5885 }
d764a8e6 5886 return NULL_TREE;
e31c7eec
TW
5887}
5888
d764a8e6
IS
5889/* Look up a class (if OBJC_LOOKUP_CLASS is set in FLAGS) or instance
5890 method in INTERFACE, along with any categories and protocols
5891 attached thereto. If method is not found, and the
5892 OBJC_LOOKUP_NO_SUPER is _not_ set in FLAGS, recursively examine the
5893 INTERFACE's superclass. If OBJC_LOOKUP_CLASS is set,
5894 OBJC_LOOKUP_NO_SUPER is clear, and no suitable class method could
5895 be found in INTERFACE or any of its superclasses, look for an
5896 _instance_ method of the same name in the root class as a last
5897 resort. This behaviour can be turned off by using
5898 OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS.
e31c7eec 5899
d764a8e6 5900 If a suitable method cannot be found, return NULL_TREE. */
e31c7eec 5901
d764a8e6
IS
5902static tree
5903lookup_method_static (tree interface, tree ident, int flags)
5904{
5905 tree meth = NULL_TREE, root_inter = NULL_TREE;
5906 tree inter = interface;
5907 int is_class = (flags & OBJC_LOOKUP_CLASS);
5908 int no_superclasses = (flags & OBJC_LOOKUP_NO_SUPER);
5909 int no_instance_methods_of_root_class = (flags & OBJC_LOOKUP_NO_INSTANCE_METHODS_OF_ROOT_CLASS);
e31c7eec 5910
d764a8e6
IS
5911 while (inter)
5912 {
5913 tree chain = is_class ? CLASS_CLS_METHODS (inter) : CLASS_NST_METHODS (inter);
5914 tree category = inter;
e31c7eec 5915
d764a8e6
IS
5916 /* First, look up the method in the class itself. */
5917 if ((meth = lookup_method (chain, ident)))
5918 return meth;
e31c7eec 5919
d764a8e6
IS
5920 /* Failing that, look for the method in each category of the class. */
5921 while ((category = CLASS_CATEGORY_LIST (category)))
5922 {
5923 chain = is_class ? CLASS_CLS_METHODS (category) : CLASS_NST_METHODS (category);
e31c7eec 5924
d764a8e6
IS
5925 /* Check directly in each category. */
5926 if ((meth = lookup_method (chain, ident)))
5927 return meth;
e31c7eec 5928
d764a8e6
IS
5929 /* Failing that, check in each category's protocols. */
5930 if (CLASS_PROTOCOL_LIST (category))
5931 {
5932 if ((meth = (lookup_method_in_protocol_list
5933 (CLASS_PROTOCOL_LIST (category), ident, is_class))))
5934 return meth;
5935 }
5936 }
e31c7eec 5937
d764a8e6
IS
5938 /* If not found in categories, check in protocols of the main class. */
5939 if (CLASS_PROTOCOL_LIST (inter))
5940 {
5941 if ((meth = (lookup_method_in_protocol_list
5942 (CLASS_PROTOCOL_LIST (inter), ident, is_class))))
5943 return meth;
5944 }
e31c7eec 5945
d764a8e6
IS
5946 /* If we were instructed not to look in superclasses, don't. */
5947 if (no_superclasses)
5948 return NULL_TREE;
89ef1046 5949
d764a8e6
IS
5950 /* Failing that, climb up the inheritance hierarchy. */
5951 root_inter = inter;
5952 inter = lookup_interface (CLASS_SUPER_NAME (inter));
e31c7eec 5953 }
d764a8e6 5954 while (inter);
e31c7eec 5955
d764a8e6 5956 if (is_class && !no_instance_methods_of_root_class)
e31c7eec 5957 {
d764a8e6
IS
5958 /* If no class (factory) method was found, check if an _instance_
5959 method of the same name exists in the root class. This is what
5960 the Objective-C runtime will do. */
5961 return lookup_method_static (root_inter, ident, 0);
e31c7eec
TW
5962 }
5963 else
d764a8e6 5964 {
944fb799 5965 /* If an instance method was not found, return 0. */
d764a8e6
IS
5966 return NULL_TREE;
5967 }
e31c7eec
TW
5968}
5969
d764a8e6
IS
5970static tree
5971objc_add_method (tree klass, tree method, int is_class, bool is_optional)
38b9c8c3 5972{
d764a8e6 5973 tree existing_method = NULL_TREE;
38b9c8c3 5974
d764a8e6
IS
5975 /* The first thing we do is look up the method in the list of
5976 methods already defined in the interface (or implementation). */
5977 if (is_class)
5978 existing_method = lookup_method (CLASS_CLS_METHODS (klass), method);
5979 else
5980 existing_method = lookup_method (CLASS_NST_METHODS (klass), method);
38b9c8c3 5981
d764a8e6
IS
5982 /* In the case of protocols, we have a second list of methods to
5983 consider, the list of optional ones. */
5984 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
38b9c8c3 5985 {
d764a8e6
IS
5986 /* @required methods are added to the protocol's normal list.
5987 @optional methods are added to the protocol's OPTIONAL lists.
5988 Note that adding the methods to the optional lists disables
5989 checking that the methods are implemented by classes
5990 implementing the protocol, since these checks only use the
5991 CLASS_CLS_METHODS and CLASS_NST_METHODS. */
38b9c8c3 5992
d764a8e6
IS
5993 /* First of all, if the method to add is @optional, and we found
5994 it already existing as @required, emit an error. */
5995 if (is_optional && existing_method)
5996 {
5997 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
5998 (is_class ? '+' : '-'),
5999 METHOD_SEL_NAME (existing_method));
6000 inform (DECL_SOURCE_LOCATION (existing_method),
6001 "previous declaration of %<%c%E%> as %<@required%>",
6002 (is_class ? '+' : '-'),
6003 METHOD_SEL_NAME (existing_method));
38b9c8c3 6004 }
38b9c8c3 6005
d764a8e6
IS
6006 /* Now check the list of @optional methods if we didn't find the
6007 method in the @required list. */
6008 if (!existing_method)
38b9c8c3 6009 {
d764a8e6
IS
6010 if (is_class)
6011 existing_method = lookup_method (PROTOCOL_OPTIONAL_CLS_METHODS (klass), method);
6012 else
6013 existing_method = lookup_method (PROTOCOL_OPTIONAL_NST_METHODS (klass), method);
944fb799 6014
d764a8e6 6015 if (!is_optional && existing_method)
38b9c8c3 6016 {
d764a8e6
IS
6017 error ("method %<%c%E%> declared %<@optional%> and %<@required%> at the same time",
6018 (is_class ? '+' : '-'),
6019 METHOD_SEL_NAME (existing_method));
6020 inform (DECL_SOURCE_LOCATION (existing_method),
6021 "previous declaration of %<%c%E%> as %<@optional%>",
6022 (is_class ? '+' : '-'),
6023 METHOD_SEL_NAME (existing_method));
38b9c8c3
ZL
6024 }
6025 }
6026 }
6027
d764a8e6
IS
6028 /* If the method didn't exist already, add it. */
6029 if (!existing_method)
6c65299b 6030 {
d764a8e6 6031 if (is_optional)
e31c7eec 6032 {
d764a8e6
IS
6033 if (is_class)
6034 {
6035 /* Put the method on the list in reverse order. */
6036 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_CLS_METHODS (klass);
6037 PROTOCOL_OPTIONAL_CLS_METHODS (klass) = method;
6038 }
6039 else
5d0f30f7 6040 {
d764a8e6
IS
6041 TREE_CHAIN (method) = PROTOCOL_OPTIONAL_NST_METHODS (klass);
6042 PROTOCOL_OPTIONAL_NST_METHODS (klass) = method;
5d0f30f7 6043 }
e31c7eec 6044 }
d764a8e6 6045 else
e31c7eec 6046 {
d764a8e6
IS
6047 if (is_class)
6048 {
6049 DECL_CHAIN (method) = CLASS_CLS_METHODS (klass);
6050 CLASS_CLS_METHODS (klass) = method;
6051 }
6052 else
5d0f30f7 6053 {
d764a8e6
IS
6054 DECL_CHAIN (method) = CLASS_NST_METHODS (klass);
6055 CLASS_NST_METHODS (klass) = method;
5d0f30f7 6056 }
e31c7eec 6057 }
6c65299b 6058 }
e31c7eec 6059 else
6c65299b 6060 {
d764a8e6
IS
6061 /* The method was already defined. Check that the types match
6062 for an @interface for a class or category, or for a
6063 @protocol. Give hard errors on methods with identical
6064 selectors but differing argument and/or return types. We do
6065 not do this for @implementations, because C/C++ will do it
6066 for us (i.e., there will be duplicate function definition
6067 errors). */
6068 if ((TREE_CODE (klass) == CLASS_INTERFACE_TYPE
6069 || TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6070 /* Starting with GCC 4.6, we emit the same error for
6071 protocols too. The situation is identical to
6072 @interfaces as there is no possible meaningful reason
6073 for defining the same method with different signatures
6074 in the very same @protocol. If that was allowed,
6075 whenever the protocol is used (both at compile and run
6076 time) there wouldn't be any meaningful way to decide
6077 which of the two method signatures should be used. */
6078 || TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE)
6079 && !comp_proto_with_proto (method, existing_method, 1))
6080 {
6081 error ("duplicate declaration of method %<%c%E%> with conflicting types",
6082 (is_class ? '+' : '-'),
6083 METHOD_SEL_NAME (existing_method));
6084 inform (DECL_SOURCE_LOCATION (existing_method),
6085 "previous declaration of %<%c%E%>",
6086 (is_class ? '+' : '-'),
6087 METHOD_SEL_NAME (existing_method));
6088 }
6c65299b 6089 }
51900510 6090
d764a8e6 6091 if (is_class)
3cc2dd4b 6092 insert_method_into_method_map (true, method);
6c65299b
RS
6093 else
6094 {
3cc2dd4b 6095 insert_method_into_method_map (false, method);
d764a8e6
IS
6096
6097 /* Instance methods in root classes (and categories thereof)
6098 may act as class methods as a last resort. We also add
6099 instance methods listed in @protocol declarations to
6100 the class hash table, on the assumption that @protocols
6101 may be adopted by root classes or categories. */
6102 if (TREE_CODE (klass) == CATEGORY_INTERFACE_TYPE
6103 || TREE_CODE (klass) == CATEGORY_IMPLEMENTATION_TYPE)
6104 klass = lookup_interface (CLASS_NAME (klass));
6105
6106 if (TREE_CODE (klass) == PROTOCOL_INTERFACE_TYPE
6107 || !CLASS_SUPER_NAME (klass))
3cc2dd4b 6108 insert_method_into_method_map (true, method);
6c65299b 6109 }
51900510 6110
d764a8e6 6111 return method;
6c65299b
RS
6112}
6113
5d0f30f7 6114static void
d764a8e6 6115add_category (tree klass, tree category)
5d0f30f7 6116{
d764a8e6
IS
6117 /* Put categories on list in reverse order. */
6118 tree cat = lookup_category (klass, CLASS_SUPER_NAME (category));
6c65299b 6119
d764a8e6 6120 if (cat)
e31c7eec 6121 {
d764a8e6
IS
6122 warning (0, "duplicate interface declaration for category %<%E(%E)%>",
6123 CLASS_NAME (klass),
6124 CLASS_SUPER_NAME (category));
e31c7eec
TW
6125 }
6126 else
6127 {
d764a8e6
IS
6128 CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (klass);
6129 CLASS_CATEGORY_LIST (klass) = category;
264fa2db 6130 }
6c65299b
RS
6131}
6132
4597541a
NP
6133#ifndef OBJCPLUS
6134/* A flexible array member is a C99 extension where you can use
6135 "type[]" at the end of a struct to mean a variable-length array.
6136
6137 In Objective-C, instance variables are fundamentally members of a
6138 struct, but the struct can always be extended by subclassing; hence
6139 we need to detect and forbid all instance variables declared using
6140 flexible array members.
6141
6142 No check for this is needed in Objective-C++, since C++ does not
6143 have flexible array members. */
6144
6145/* Determine whether TYPE is a structure with a flexible array member,
6146 a union containing such a structure (possibly recursively) or an
6147 array of such structures or unions. These are all invalid as
6148 instance variable. */
6149static bool
6150flexible_array_type_p (tree type)
6151{
6152 tree x;
6153 switch (TREE_CODE (type))
6154 {
6155 case RECORD_TYPE:
6156 x = TYPE_FIELDS (type);
6157 if (x == NULL_TREE)
6158 return false;
6159 while (DECL_CHAIN (x) != NULL_TREE)
6160 x = DECL_CHAIN (x);
6161 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6162 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
6163 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
6164 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
6165 return true;
6166 return false;
6167 case UNION_TYPE:
6168 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
6169 {
6170 if (flexible_array_type_p (TREE_TYPE (x)))
6171 return true;
6172 }
6173 return false;
6174 /* Note that we also check for arrays of something that uses a flexible array member. */
6175 case ARRAY_TYPE:
6176 if (flexible_array_type_p (TREE_TYPE (type)))
6177 return true;
6178 return false;
6179 default:
6180 return false;
6181 }
6182}
6183#endif
6184
cf3caeaf
NP
6185/* Produce a printable version of an ivar name. This is only used
6186 inside add_instance_variable. */
6187static const char *
6188printable_ivar_name (tree field_decl)
6189{
6190 if (DECL_NAME (field_decl))
6191 return identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6192 else
6193 return _("<unnamed>");
6194}
6195
d764a8e6
IS
6196/* Called after parsing each instance variable declaration. Necessary to
6197 preserve typedefs and implement public/private...
6c65299b 6198
d764a8e6 6199 VISIBILITY is 1 for public, 0 for protected, and 2 for private. */
6c65299b 6200
d764a8e6 6201static tree
944fb799 6202add_instance_variable (tree klass, objc_ivar_visibility_kind visibility,
d764a8e6 6203 tree field_decl)
6c65299b 6204{
d764a8e6 6205 tree field_type = TREE_TYPE (field_decl);
6c65299b 6206
d764a8e6
IS
6207#ifdef OBJCPLUS
6208 if (TREE_CODE (field_type) == REFERENCE_TYPE)
6209 {
6210 error ("illegal reference type specified for instance variable %qs",
cf3caeaf 6211 printable_ivar_name (field_decl));
d764a8e6
IS
6212 /* Return class as is without adding this ivar. */
6213 return klass;
6214 }
6215#endif
6c65299b 6216
d764a8e6
IS
6217 if (field_type == error_mark_node || !TYPE_SIZE (field_type)
6218 || TYPE_SIZE (field_type) == error_mark_node)
6219 /* 'type[0]' is allowed, but 'type[]' is not! */
6c65299b 6220 {
cf3caeaf
NP
6221 error ("instance variable %qs has unknown size",
6222 printable_ivar_name (field_decl));
d764a8e6
IS
6223 /* Return class as is without adding this ivar. */
6224 return klass;
6225 }
6c65299b 6226
4597541a
NP
6227#ifndef OBJCPLUS
6228 /* Also, in C reject a struct with a flexible array member. Ie,
6229
6230 struct A { int x; int[] y; };
6231
6232 @interface X
6233 {
6234 struct A instance_variable;
6235 }
6236 @end
6237
6238 is not valid because if the class is subclassed, we wouldn't be able
6239 to calculate the offset of the next instance variable. */
6240 if (flexible_array_type_p (field_type))
6241 {
cf3caeaf
NP
6242 error ("instance variable %qs uses flexible array member",
6243 printable_ivar_name (field_decl));
4597541a 6244 /* Return class as is without adding this ivar. */
944fb799 6245 return klass;
4597541a
NP
6246 }
6247#endif
6248
264fa2db 6249#ifdef OBJCPLUS
d764a8e6
IS
6250 /* Check if the ivar being added has a non-POD C++ type. If so, we will
6251 need to either (1) warn the user about it or (2) generate suitable
6252 constructor/destructor call from '- .cxx_construct' or '- .cxx_destruct'
6253 methods (if '-fobjc-call-cxx-cdtors' was specified). */
6254 if (MAYBE_CLASS_TYPE_P (field_type)
6255 && (TYPE_NEEDS_CONSTRUCTING (field_type)
6256 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type)
6257 || TYPE_POLYMORPHIC_P (field_type)))
6258 {
6259 tree type_name = OBJC_TYPE_NAME (field_type);
51900510 6260
d764a8e6
IS
6261 if (flag_objc_call_cxx_cdtors)
6262 {
6263 /* Since the ObjC runtime will be calling the constructors and
6264 destructors for us, the only thing we can't handle is the lack
6265 of a default constructor. */
6266 if (TYPE_NEEDS_CONSTRUCTING (field_type)
6267 && !TYPE_HAS_DEFAULT_CONSTRUCTOR (field_type))
6268 {
6269 warning (0, "type %qE has no default constructor to call",
6270 type_name);
6c65299b 6271
d764a8e6
IS
6272 /* If we cannot call a constructor, we should also avoid
6273 calling the destructor, for symmetry. */
6274 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6275 warning (0, "destructor for %qE shall not be run either",
6276 type_name);
6277 }
6278 }
6279 else
6c65299b 6280 {
d764a8e6
IS
6281 static bool warn_cxx_ivars = false;
6282
6283 if (TYPE_POLYMORPHIC_P (field_type))
6c65299b 6284 {
d764a8e6
IS
6285 /* Vtable pointers are Real Bad(tm), since Obj-C cannot
6286 initialize them. */
6287 error ("type %qE has virtual member functions", type_name);
6288 error ("illegal aggregate type %qE specified "
6289 "for instance variable %qs",
cf3caeaf 6290 type_name, printable_ivar_name (field_decl));
d764a8e6
IS
6291 /* Return class as is without adding this ivar. */
6292 return klass;
6c65299b 6293 }
d764a8e6
IS
6294
6295 /* User-defined constructors and destructors are not known to Obj-C
6296 and hence will not be called. This may or may not be a problem. */
6297 if (TYPE_NEEDS_CONSTRUCTING (field_type))
6298 warning (0, "type %qE has a user-defined constructor", type_name);
6299 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (field_type))
6300 warning (0, "type %qE has a user-defined destructor", type_name);
6301
6302 if (!warn_cxx_ivars)
6c65299b 6303 {
d764a8e6
IS
6304 warning (0, "C++ constructors and destructors will not "
6305 "be invoked for Objective-C fields");
6306 warn_cxx_ivars = true;
6c65299b
RS
6307 }
6308 }
6c65299b 6309 }
d764a8e6 6310#endif
6c65299b 6311
d764a8e6
IS
6312 /* Overload the public attribute, it is not used for FIELD_DECLs. */
6313 switch (visibility)
6314 {
6315 case OBJC_IVAR_VIS_PROTECTED:
6316 TREE_PUBLIC (field_decl) = 0;
6317 TREE_PRIVATE (field_decl) = 0;
6318 TREE_PROTECTED (field_decl) = 1;
6319 break;
6c65299b 6320
d764a8e6
IS
6321 case OBJC_IVAR_VIS_PACKAGE:
6322 /* TODO: Implement the package variant. */
6323 case OBJC_IVAR_VIS_PUBLIC:
6324 TREE_PUBLIC (field_decl) = 1;
6325 TREE_PRIVATE (field_decl) = 0;
6326 TREE_PROTECTED (field_decl) = 0;
6327 break;
6c65299b 6328
d764a8e6
IS
6329 case OBJC_IVAR_VIS_PRIVATE:
6330 TREE_PUBLIC (field_decl) = 0;
6331 TREE_PRIVATE (field_decl) = 1;
6332 TREE_PROTECTED (field_decl) = 0;
6333 break;
6c65299b 6334
d764a8e6 6335 }
6c65299b 6336
d764a8e6 6337 CLASS_RAW_IVARS (klass) = chainon (CLASS_RAW_IVARS (klass), field_decl);
6c65299b 6338
d764a8e6 6339 return klass;
6c65299b
RS
6340}
6341
d764a8e6 6342/* True if the ivar is private and we are not in its implementation. */
e31c7eec 6343
d764a8e6
IS
6344static int
6345is_private (tree decl)
6c65299b 6346{
d764a8e6
IS
6347 return (TREE_PRIVATE (decl)
6348 && ! is_ivar (CLASS_IVARS (implementation_template),
6349 DECL_NAME (decl)));
6c65299b
RS
6350}
6351
5678a5a3
NP
6352/* Searches all the instance variables of 'klass' and of its
6353 superclasses for an instance variable whose name (identifier) is
6354 'ivar_name_ident'. Return the declaration (DECL) of the instance
6355 variable, if found, or NULL_TREE, if not found. */
6356static inline tree
6357ivar_of_class (tree klass, tree ivar_name_ident)
6358{
6359 /* First, look up the ivar in CLASS_RAW_IVARS. */
6360 tree decl_chain = CLASS_RAW_IVARS (klass);
6361
6362 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6363 if (DECL_NAME (decl_chain) == ivar_name_ident)
6364 return decl_chain;
6365
6366 /* If not found, search up the class hierarchy. */
6367 while (CLASS_SUPER_NAME (klass))
6368 {
6369 klass = lookup_interface (CLASS_SUPER_NAME (klass));
944fb799 6370
5678a5a3 6371 decl_chain = CLASS_RAW_IVARS (klass);
944fb799 6372
5678a5a3
NP
6373 for ( ; decl_chain; decl_chain = DECL_CHAIN (decl_chain))
6374 if (DECL_NAME (decl_chain) == ivar_name_ident)
6375 return decl_chain;
6376 }
6377
6378 return NULL_TREE;
6379}
6380
d764a8e6 6381/* We have an instance variable reference;, check to see if it is public. */
e31c7eec 6382
d764a8e6
IS
6383int
6384objc_is_public (tree expr, tree identifier)
6c65299b 6385{
d764a8e6 6386 tree basetype, decl;
6c65299b 6387
d764a8e6
IS
6388#ifdef OBJCPLUS
6389 if (processing_template_decl)
6390 return 1;
6391#endif
6c65299b 6392
d764a8e6
IS
6393 if (TREE_TYPE (expr) == error_mark_node)
6394 return 1;
6c65299b 6395
d764a8e6 6396 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6c65299b 6397
d764a8e6
IS
6398 if (basetype && TREE_CODE (basetype) == RECORD_TYPE)
6399 {
6400 if (TYPE_HAS_OBJC_INFO (basetype) && TYPE_OBJC_INTERFACE (basetype))
6401 {
6402 tree klass = lookup_interface (OBJC_TYPE_NAME (basetype));
6c65299b 6403
d764a8e6
IS
6404 if (!klass)
6405 {
6406 error ("cannot find interface declaration for %qE",
6407 OBJC_TYPE_NAME (basetype));
6408 return 0;
6409 }
6c65299b 6410
5678a5a3 6411 if ((decl = ivar_of_class (klass, identifier)))
d764a8e6
IS
6412 {
6413 if (TREE_PUBLIC (decl))
6414 return 1;
6c65299b 6415
d764a8e6
IS
6416 /* Important difference between the Stepstone translator:
6417 all instance variables should be public within the context
6418 of the implementation. */
6419 if (objc_implementation_context
6420 && ((TREE_CODE (objc_implementation_context)
6421 == CLASS_IMPLEMENTATION_TYPE)
6422 || (TREE_CODE (objc_implementation_context)
6423 == CATEGORY_IMPLEMENTATION_TYPE)))
6424 {
6425 tree curtype = TYPE_MAIN_VARIANT
6426 (CLASS_STATIC_TEMPLATE
6427 (implementation_template));
6c65299b 6428
d764a8e6
IS
6429 if (basetype == curtype
6430 || DERIVED_FROM_P (basetype, curtype))
6431 {
6432 int priv = is_private (decl);
6c65299b 6433
d764a8e6
IS
6434 if (priv)
6435 error ("instance variable %qE is declared private",
6436 DECL_NAME (decl));
6c65299b 6437
d764a8e6
IS
6438 return !priv;
6439 }
6440 }
6c65299b 6441
d764a8e6
IS
6442 /* The 2.95.2 compiler sometimes allowed C functions to access
6443 non-@public ivars. We will let this slide for now... */
6444 if (!objc_method_context)
6445 {
6446 warning (0, "instance variable %qE is %s; "
6447 "this will be a hard error in the future",
6448 identifier,
6449 TREE_PRIVATE (decl) ? "@private" : "@protected");
6450 return 1;
6451 }
6c65299b 6452
d764a8e6
IS
6453 error ("instance variable %qE is declared %s",
6454 identifier,
6455 TREE_PRIVATE (decl) ? "private" : "protected");
6456 return 0;
6457 }
6458 }
6459 }
6c65299b 6460
d764a8e6 6461 return 1;
6c65299b 6462}
d764a8e6
IS
6463\f
6464/* Make sure all methods in CHAIN (a list of method declarations from
6465 an @interface or a @protocol) are in IMPLEMENTATION (the
6466 implementation context). This is used to check for example that
6467 all methods declared in an @interface were implemented in an
6468 @implementation.
6c65299b 6469
d764a8e6
IS
6470 Some special methods (property setters/getters) are special and if
6471 they are not found in IMPLEMENTATION, we look them up in its
6472 superclasses. */
96e5d745 6473
264fa2db 6474static int
d764a8e6 6475check_methods (tree chain, tree implementation, int mtype)
6c65299b 6476{
d764a8e6
IS
6477 int first = 1;
6478 tree list;
6c65299b 6479
d764a8e6
IS
6480 if (mtype == (int)'+')
6481 list = CLASS_CLS_METHODS (implementation);
6c65299b 6482 else
d764a8e6 6483 list = CLASS_NST_METHODS (implementation);
6c65299b 6484
d764a8e6 6485 while (chain)
6c65299b 6486 {
d764a8e6
IS
6487 /* If the method is associated with a dynamic property, then it
6488 is Ok not to have the method implementation, as it will be
6489 generated dynamically at runtime. To decide if the method is
6490 associated with a @dynamic property, we search the list of
6491 @synthesize and @dynamic for this implementation, and look
6492 for any @dynamic property with the same setter or getter name
6493 as this method. */
6494 tree x;
6495 for (x = IMPL_PROPERTY_DECL (implementation); x; x = TREE_CHAIN (x))
6496 if (PROPERTY_DYNAMIC (x)
6497 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6498 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6499 break;
944fb799 6500
d764a8e6
IS
6501 if (x != NULL_TREE)
6502 {
6503 chain = TREE_CHAIN (chain); /* next method... */
6504 continue;
6505 }
6c65299b 6506
d764a8e6
IS
6507 if (!lookup_method (list, chain))
6508 {
6509 /* If the method is a property setter/getter, we'll still
6510 allow it to be missing if it is implemented by
6511 'interface' or any of its superclasses. */
6512 tree property = METHOD_PROPERTY_CONTEXT (chain);
6513 if (property)
6514 {
6515 /* Note that since this is a property getter/setter, it
6516 is obviously an instance method. */
6517 tree interface = NULL_TREE;
6c65299b 6518
d764a8e6
IS
6519 /* For a category, first check the main class
6520 @interface. */
6521 if (TREE_CODE (implementation) == CATEGORY_IMPLEMENTATION_TYPE)
6522 {
6523 interface = lookup_interface (CLASS_NAME (implementation));
30c0e2df 6524
d764a8e6
IS
6525 /* If the method is found in the main class, it's Ok. */
6526 if (lookup_method (CLASS_NST_METHODS (interface), chain))
6527 {
6528 chain = DECL_CHAIN (chain);
944fb799 6529 continue;
d764a8e6 6530 }
6c65299b 6531
d764a8e6
IS
6532 /* Else, get the superclass. */
6533 if (CLASS_SUPER_NAME (interface))
6534 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6535 else
6536 interface = NULL_TREE;
6537 }
51a56a7d 6538
d764a8e6
IS
6539 /* Get the superclass for classes. */
6540 if (TREE_CODE (implementation) == CLASS_IMPLEMENTATION_TYPE)
6541 {
6542 if (CLASS_SUPER_NAME (implementation))
6543 interface = lookup_interface (CLASS_SUPER_NAME (implementation));
6544 else
6545 interface = NULL_TREE;
6546 }
6c65299b 6547
d764a8e6
IS
6548 /* Now, interface is the superclass, if any; go check it. */
6549 if (interface)
6550 {
6551 if (lookup_method_static (interface, chain, 0))
6552 {
6553 chain = DECL_CHAIN (chain);
6554 continue;
6555 }
6556 }
6557 /* Else, fall through - warn. */
6558 }
6559 if (first)
6560 {
6561 switch (TREE_CODE (implementation))
6562 {
6563 case CLASS_IMPLEMENTATION_TYPE:
6564 warning (0, "incomplete implementation of class %qE",
6565 CLASS_NAME (implementation));
6566 break;
6567 case CATEGORY_IMPLEMENTATION_TYPE:
6568 warning (0, "incomplete implementation of category %qE",
6569 CLASS_SUPER_NAME (implementation));
6570 break;
6571 default:
6572 gcc_unreachable ();
6573 }
6574 first = 0;
6575 }
30c0e2df 6576
d764a8e6
IS
6577 warning (0, "method definition for %<%c%E%> not found",
6578 mtype, METHOD_SEL_NAME (chain));
6579 }
6c65299b 6580
d764a8e6 6581 chain = DECL_CHAIN (chain);
6c65299b 6582 }
6c65299b 6583
d764a8e6 6584 return first;
6c65299b
RS
6585}
6586
d764a8e6 6587/* Check if KLASS, or its superclasses, explicitly conforms to PROTOCOL. */
e31c7eec 6588
d764a8e6
IS
6589static int
6590conforms_to_protocol (tree klass, tree protocol)
6c65299b 6591{
d764a8e6
IS
6592 if (TREE_CODE (protocol) == PROTOCOL_INTERFACE_TYPE)
6593 {
6594 tree p = CLASS_PROTOCOL_LIST (klass);
6595 while (p && TREE_VALUE (p) != protocol)
6596 p = TREE_CHAIN (p);
6c65299b 6597
d764a8e6
IS
6598 if (!p)
6599 {
6600 tree super = (CLASS_SUPER_NAME (klass)
6601 ? lookup_interface (CLASS_SUPER_NAME (klass))
6602 : NULL_TREE);
6603 int tmp = super ? conforms_to_protocol (super, protocol) : 0;
6604 if (!tmp)
6605 return 0;
6606 }
6607 }
6c65299b 6608
d764a8e6 6609 return 1;
6c65299b
RS
6610}
6611
d764a8e6
IS
6612/* Make sure all methods in CHAIN are accessible as MTYPE methods in
6613 CONTEXT. This is one of two mechanisms to check protocol integrity. */
6c65299b 6614
d764a8e6
IS
6615static int
6616check_methods_accessible (tree chain, tree context, int mtype)
0d446150 6617{
d764a8e6
IS
6618 int first = 1;
6619 tree list;
6620 tree base_context = context;
0d446150 6621
d764a8e6 6622 while (chain)
0d446150 6623 {
d764a8e6
IS
6624 /* If the method is associated with a dynamic property, then it
6625 is Ok not to have the method implementation, as it will be
6626 generated dynamically at runtime. Search for any @dynamic
6627 property with the same setter or getter name as this
6628 method. TODO: Use a hashtable lookup. */
6629 tree x;
6630 for (x = IMPL_PROPERTY_DECL (base_context); x; x = TREE_CHAIN (x))
6631 if (PROPERTY_DYNAMIC (x)
6632 && (PROPERTY_GETTER_NAME (x) == METHOD_SEL_NAME (chain)
6633 || PROPERTY_SETTER_NAME (x) == METHOD_SEL_NAME (chain)))
6634 break;
944fb799 6635
d764a8e6 6636 if (x != NULL_TREE)
0d446150 6637 {
d764a8e6
IS
6638 chain = TREE_CHAIN (chain); /* next method... */
6639 continue;
944fb799 6640 }
8dafba3c 6641
d764a8e6
IS
6642 context = base_context;
6643 while (context)
0d446150 6644 {
d764a8e6
IS
6645 if (mtype == '+')
6646 list = CLASS_CLS_METHODS (context);
6647 else
6648 list = CLASS_NST_METHODS (context);
e31c7eec 6649
d764a8e6
IS
6650 if (lookup_method (list, chain))
6651 break;
6c65299b 6652
d764a8e6
IS
6653 switch (TREE_CODE (context))
6654 {
6655 case CLASS_IMPLEMENTATION_TYPE:
6656 case CLASS_INTERFACE_TYPE:
6657 context = (CLASS_SUPER_NAME (context)
6658 ? lookup_interface (CLASS_SUPER_NAME (context))
6659 : NULL_TREE);
6660 break;
6661 case CATEGORY_IMPLEMENTATION_TYPE:
6662 case CATEGORY_INTERFACE_TYPE:
6663 context = (CLASS_NAME (context)
6664 ? lookup_interface (CLASS_NAME (context))
6665 : NULL_TREE);
6666 break;
6667 default:
6668 gcc_unreachable ();
6669 }
6670 }
30c0e2df 6671
d764a8e6
IS
6672 if (context == NULL_TREE)
6673 {
6674 if (first)
6675 {
6676 switch (TREE_CODE (objc_implementation_context))
6677 {
6678 case CLASS_IMPLEMENTATION_TYPE:
6679 warning (0, "incomplete implementation of class %qE",
6680 CLASS_NAME (objc_implementation_context));
6681 break;
6682 case CATEGORY_IMPLEMENTATION_TYPE:
6683 warning (0, "incomplete implementation of category %qE",
6684 CLASS_SUPER_NAME (objc_implementation_context));
6685 break;
6686 default:
6687 gcc_unreachable ();
6688 }
6689 first = 0;
6690 }
6691 warning (0, "method definition for %<%c%E%> not found",
6692 mtype, METHOD_SEL_NAME (chain));
6693 }
6c65299b 6694
d764a8e6 6695 chain = TREE_CHAIN (chain); /* next method... */
6c65299b 6696 }
d764a8e6 6697 return first;
e31c7eec
TW
6698}
6699
d764a8e6
IS
6700/* Check whether the current interface (accessible via
6701 'objc_implementation_context') actually implements protocol P, along
6702 with any protocols that P inherits. */
e31c7eec 6703
d764a8e6
IS
6704static void
6705check_protocol (tree p, const char *type, tree name)
6706{
6707 if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
50ea5861 6708 {
d764a8e6 6709 int f1, f2;
e31c7eec 6710
d764a8e6
IS
6711 /* Ensure that all protocols have bodies! */
6712 if (warn_protocol)
6713 {
6714 f1 = check_methods (PROTOCOL_CLS_METHODS (p),
6715 objc_implementation_context,
6716 '+');
6717 f2 = check_methods (PROTOCOL_NST_METHODS (p),
6718 objc_implementation_context,
6719 '-');
6720 }
6721 else
6722 {
6723 f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
6724 objc_implementation_context,
6725 '+');
6726 f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
6727 objc_implementation_context,
6728 '-');
6729 }
e31c7eec 6730
d764a8e6
IS
6731 if (!f1 || !f2)
6732 warning (0, "%s %qE does not fully implement the %qE protocol",
6733 type, name, PROTOCOL_NAME (p));
6734 }
e31c7eec 6735
d764a8e6
IS
6736 /* Check protocols recursively. */
6737 if (PROTOCOL_LIST (p))
e31c7eec 6738 {
d764a8e6
IS
6739 tree subs = PROTOCOL_LIST (p);
6740 tree super_class =
6741 lookup_interface (CLASS_SUPER_NAME (implementation_template));
e31c7eec 6742
d764a8e6 6743 while (subs)
e31c7eec 6744 {
d764a8e6
IS
6745 tree sub = TREE_VALUE (subs);
6746
6747 /* If the superclass does not conform to the protocols
6748 inherited by P, then we must! */
6749 if (!super_class || !conforms_to_protocol (super_class, sub))
6750 check_protocol (sub, type, name);
6751 subs = TREE_CHAIN (subs);
e31c7eec
TW
6752 }
6753 }
d764a8e6 6754}
e31c7eec 6755
d764a8e6
IS
6756/* Check whether the current interface (accessible via
6757 'objc_implementation_context') actually implements the protocols listed
6758 in PROTO_LIST. */
e31c7eec 6759
d764a8e6
IS
6760static void
6761check_protocols (tree proto_list, const char *type, tree name)
6762{
6763 for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
50ea5861 6764 {
d764a8e6 6765 tree p = TREE_VALUE (proto_list);
e31c7eec 6766
d764a8e6
IS
6767 check_protocol (p, type, name);
6768 }
6c65299b 6769}
d764a8e6
IS
6770\f
6771/* Make sure that the class CLASS_NAME is defined CODE says which kind
6772 of thing CLASS_NAME ought to be. It can be CLASS_INTERFACE_TYPE,
6773 CLASS_IMPLEMENTATION_TYPE, CATEGORY_INTERFACE_TYPE, or
6774 CATEGORY_IMPLEMENTATION_TYPE. For a CATEGORY_INTERFACE_TYPE,
6775 SUPER_NAME is the name of the category. For a class extension,
6776 CODE is CATEGORY_INTERFACE_TYPE and SUPER_NAME is NULL_TREE. */
6c65299b 6777static tree
d764a8e6
IS
6778start_class (enum tree_code code, tree class_name, tree super_name,
6779 tree protocol_list, tree attributes)
6c65299b 6780{
d764a8e6
IS
6781 tree klass = NULL_TREE;
6782 tree decl;
6c65299b 6783
d764a8e6
IS
6784#ifdef OBJCPLUS
6785 if (current_namespace != global_namespace)
6c65299b 6786 {
d764a8e6 6787 error ("Objective-C declarations may only appear in global scope");
6c65299b 6788 }
d764a8e6
IS
6789#endif /* OBJCPLUS */
6790
6791 if (objc_implementation_context)
6c65299b 6792 {
d764a8e6
IS
6793 warning (0, "%<@end%> missing in implementation context");
6794 finish_class (objc_implementation_context);
6795 objc_ivar_chain = NULL_TREE;
6796 objc_implementation_context = NULL_TREE;
6c65299b 6797 }
e31c7eec 6798
d764a8e6
IS
6799 /* If this is a class extension, we'll be "reopening" the existing
6800 CLASS_INTERFACE_TYPE, so in that case there is no need to create
6801 a new node. */
6802 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6408ef33 6803 {
d764a8e6
IS
6804 klass = make_node (code);
6805 TYPE_LANG_SLOT_1 (klass) = make_tree_vec (CLASS_LANG_SLOT_ELTS);
6806 }
6c65299b 6807
d764a8e6
IS
6808 /* Check for existence of the super class, if one was specified. Note
6809 that we must have seen an @interface, not just a @class. If we
6810 are looking at a @compatibility_alias, traverse it first. */
6811 if ((code == CLASS_INTERFACE_TYPE || code == CLASS_IMPLEMENTATION_TYPE)
6812 && super_name)
6c65299b 6813 {
d764a8e6
IS
6814 tree super = objc_is_class_name (super_name);
6815 tree super_interface = NULL_TREE;
6816
6817 if (super)
6818 super_interface = lookup_interface (super);
944fb799 6819
d764a8e6
IS
6820 if (!super_interface)
6821 {
6822 error ("cannot find interface declaration for %qE, superclass of %qE",
6823 super ? super : super_name,
6824 class_name);
6825 super_name = NULL_TREE;
6826 }
6827 else
6828 {
6829 if (TREE_DEPRECATED (super_interface))
944fb799 6830 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
d764a8e6
IS
6831 super);
6832 super_name = super;
6833 }
6c65299b
RS
6834 }
6835
d764a8e6 6836 if (code != CATEGORY_INTERFACE_TYPE || super_name != NULL_TREE)
6c65299b 6837 {
d764a8e6
IS
6838 CLASS_NAME (klass) = class_name;
6839 CLASS_SUPER_NAME (klass) = super_name;
6840 CLASS_CLS_METHODS (klass) = NULL_TREE;
6c65299b
RS
6841 }
6842
d764a8e6
IS
6843 if (! objc_is_class_name (class_name)
6844 && (decl = lookup_name (class_name)))
e31c7eec 6845 {
d764a8e6
IS
6846 error ("%qE redeclared as different kind of symbol",
6847 class_name);
6848 error ("previous declaration of %q+D",
6849 decl);
e31c7eec
TW
6850 }
6851
d764a8e6 6852 switch (code)
6408ef33 6853 {
d764a8e6
IS
6854 case CLASS_IMPLEMENTATION_TYPE:
6855 {
6856 tree chain;
944fb799 6857
d764a8e6
IS
6858 for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
6859 if (TREE_VALUE (chain) == class_name)
6860 {
6861 error ("reimplementation of class %qE",
6862 class_name);
6863 /* TODO: error message saying where it was previously
6864 implemented. */
6865 break;
6866 }
6867 if (chain == NULL_TREE)
6868 implemented_classes = tree_cons (NULL_TREE, class_name,
6869 implemented_classes);
6870 }
6c65299b 6871
d764a8e6
IS
6872 /* Reset for multiple classes per file. */
6873 method_slot = 0;
264fa2db 6874
d764a8e6 6875 objc_implementation_context = klass;
d8bb17c8 6876
d764a8e6 6877 /* Lookup the interface for this implementation. */
6c65299b 6878
d764a8e6
IS
6879 if (!(implementation_template = lookup_interface (class_name)))
6880 {
6881 warning (0, "cannot find interface declaration for %qE",
6882 class_name);
3cc2dd4b
NP
6883 add_interface (implementation_template = objc_implementation_context,
6884 class_name);
d764a8e6 6885 }
e31c7eec 6886
d764a8e6
IS
6887 /* If a super class has been specified in the implementation,
6888 insure it conforms to the one specified in the interface. */
6c65299b 6889
d764a8e6
IS
6890 if (super_name
6891 && (super_name != CLASS_SUPER_NAME (implementation_template)))
6c65299b 6892 {
d764a8e6
IS
6893 tree previous_name = CLASS_SUPER_NAME (implementation_template);
6894 error ("conflicting super class name %qE",
6895 super_name);
6896 if (previous_name)
6897 error ("previous declaration of %qE", previous_name);
6c65299b 6898 else
d764a8e6 6899 error ("previous declaration");
6c65299b 6900 }
6c65299b 6901
d764a8e6
IS
6902 else if (! super_name)
6903 {
6904 CLASS_SUPER_NAME (objc_implementation_context)
6905 = CLASS_SUPER_NAME (implementation_template);
6906 }
6907 break;
6c65299b 6908
d764a8e6
IS
6909 case CLASS_INTERFACE_TYPE:
6910 if (lookup_interface (class_name))
6911#ifdef OBJCPLUS
6912 error ("duplicate interface declaration for class %qE", class_name);
6913#else
6914 warning (0, "duplicate interface declaration for class %qE", class_name);
6915#endif
6916 else
3cc2dd4b 6917 add_interface (klass, class_name);
944fb799 6918
d764a8e6
IS
6919 if (protocol_list)
6920 CLASS_PROTOCOL_LIST (klass)
6921 = lookup_and_install_protocols (protocol_list, /* definition_required */ true);
e31c7eec 6922
d764a8e6
IS
6923 if (attributes)
6924 {
6925 tree attribute;
6926 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
6927 {
6928 tree name = TREE_PURPOSE (attribute);
944fb799 6929
d4c433f9 6930 /* TODO: Document what the objc_exception attribute is/does. */
d764a8e6
IS
6931 /* We handle the 'deprecated' and (undocumented) 'objc_exception'
6932 attributes. */
6933 if (is_attribute_p ("deprecated", name))
6934 TREE_DEPRECATED (klass) = 1;
6935 else if (is_attribute_p ("objc_exception", name))
6936 CLASS_HAS_EXCEPTION_ATTR (klass) = 1;
6937 else
6938 /* Warn about and ignore all others for now, but store them. */
6939 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
6940 }
6941 TYPE_ATTRIBUTES (klass) = attributes;
6942 }
944fb799 6943 break;
6c65299b 6944
d764a8e6
IS
6945 case CATEGORY_INTERFACE_TYPE:
6946 {
6947 tree class_category_is_assoc_with;
944fb799 6948
d764a8e6
IS
6949 /* For a category, class_name is really the name of the class that
6950 the following set of methods will be associated with. We must
6951 find the interface so that can derive the objects template. */
6952 if (!(class_category_is_assoc_with = lookup_interface (class_name)))
6953 {
6954 error ("cannot find interface declaration for %qE",
6955 class_name);
6956 exit (FATAL_EXIT_CODE);
6957 }
6958 else
6959 {
6960 if (TREE_DEPRECATED (class_category_is_assoc_with))
944fb799 6961 warning (OPT_Wdeprecated_declarations, "class %qE is deprecated",
d764a8e6 6962 class_name);
6c65299b 6963
d764a8e6
IS
6964 if (super_name == NULL_TREE)
6965 {
6966 /* This is a class extension. Get the original
6967 interface, and continue working on it. */
6968 objc_in_class_extension = true;
6969 klass = class_category_is_assoc_with;
6c65299b 6970
d764a8e6
IS
6971 if (protocol_list)
6972 {
6973 /* Append protocols to the original protocol
6974 list. */
6975 CLASS_PROTOCOL_LIST (klass)
6976 = chainon (CLASS_PROTOCOL_LIST (klass),
6977 lookup_and_install_protocols
6978 (protocol_list,
6979 /* definition_required */ true));
6980 }
6981 }
6982 else
6983 {
6984 add_category (class_category_is_assoc_with, klass);
944fb799 6985
d764a8e6
IS
6986 if (protocol_list)
6987 CLASS_PROTOCOL_LIST (klass)
6988 = lookup_and_install_protocols
6989 (protocol_list, /* definition_required */ true);
6990 }
6991 }
6992 }
6993 break;
944fb799 6994
d764a8e6
IS
6995 case CATEGORY_IMPLEMENTATION_TYPE:
6996 /* Reset for multiple classes per file. */
6997 method_slot = 0;
6c65299b 6998
d764a8e6 6999 objc_implementation_context = klass;
6c65299b 7000
d764a8e6
IS
7001 /* For a category, class_name is really the name of the class that
7002 the following set of methods will be associated with. We must
7003 find the interface so that can derive the objects template. */
6c65299b 7004
d764a8e6
IS
7005 if (!(implementation_template = lookup_interface (class_name)))
7006 {
7007 error ("cannot find interface declaration for %qE",
7008 class_name);
7009 exit (FATAL_EXIT_CODE);
7010 }
7011 break;
7012 default:
7013 gcc_unreachable ();
7014 }
7015 return klass;
6c65299b
RS
7016}
7017
d764a8e6
IS
7018static tree
7019continue_class (tree klass)
6c65299b 7020{
d764a8e6 7021 switch (TREE_CODE (klass))
6e3f0724 7022 {
50ea5861 7023 case CLASS_IMPLEMENTATION_TYPE:
50ea5861 7024 case CATEGORY_IMPLEMENTATION_TYPE:
50ea5861 7025 {
d764a8e6
IS
7026 struct imp_entry *imp_entry;
7027
7028 /* Check consistency of the instance variables. */
7029
7030 if (CLASS_RAW_IVARS (klass))
7031 check_ivars (implementation_template, klass);
944fb799 7032
d764a8e6
IS
7033 /* code generation */
7034#ifdef OBJCPLUS
7035 push_lang_context (lang_name_c);
7036#endif
7037 build_private_template (implementation_template);
7038 uprivate_record = CLASS_STATIC_TEMPLATE (implementation_template);
7039 objc_instance_type = build_pointer_type (uprivate_record);
7040
766090c2 7041 imp_entry = ggc_alloc<struct imp_entry> ();
d764a8e6
IS
7042
7043 imp_entry->next = imp_list;
7044 imp_entry->imp_context = klass;
7045 imp_entry->imp_template = implementation_template;
7046 ucls_super_ref = uucls_super_ref = NULL;
7047 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7048 {
7049 imp_entry->class_decl = (*runtime.class_decl) (klass);
7050 imp_entry->meta_decl = (*runtime.metaclass_decl) (klass);
7051 }
7052 else
7053 {
7054 imp_entry->class_decl = (*runtime.category_decl) (klass);
7055 imp_entry->meta_decl = NULL;
7056 }
7057 imp_entry->has_cxx_cdtors = 0;
7058
7059 /* Append to front and increment count. */
7060 imp_list = imp_entry;
7061 if (TREE_CODE (klass) == CLASS_IMPLEMENTATION_TYPE)
7062 imp_count++;
7063 else
7064 cat_count++;
7065#ifdef OBJCPLUS
7066 pop_lang_context ();
7067#endif /* OBJCPLUS */
944fb799 7068
d764a8e6 7069 return get_class_ivars (implementation_template, true);
50ea5861 7070 break;
d764a8e6
IS
7071 }
7072 case CLASS_INTERFACE_TYPE:
50ea5861 7073 {
d764a8e6
IS
7074 if (objc_in_class_extension)
7075 return NULL_TREE;
7076#ifdef OBJCPLUS
7077 push_lang_context (lang_name_c);
7078#endif /* OBJCPLUS */
7079 objc_collecting_ivars = 1;
7080 build_private_template (klass);
7081 objc_collecting_ivars = 0;
7082#ifdef OBJCPLUS
7083 pop_lang_context ();
7084#endif /* OBJCPLUS */
7085 return NULL_TREE;
50ea5861
NP
7086 break;
7087 }
7088 default:
d764a8e6 7089 return error_mark_node;
6e3f0724 7090 }
e31c7eec
TW
7091}
7092
d764a8e6 7093/* This routine builds name of the setter synthesized function. */
f027ee7c 7094char *
d764a8e6 7095objc_build_property_setter_name (tree ident)
e31c7eec 7096{
d764a8e6
IS
7097 /* TODO: Use alloca to allocate buffer of appropriate size. */
7098 static char string[BUFSIZE];
7099 sprintf (string, "set%s:", IDENTIFIER_POINTER (ident));
7100 string[3] = TOUPPER (string[3]);
7101 return string;
e31c7eec
TW
7102}
7103
d764a8e6
IS
7104/* This routine prepares the declarations of the property accessor
7105 helper functions (objc_getProperty(), etc) that are used when
944fb799
MS
7106 @synthesize is used.
7107
7108 runtime-specific routines are built in the respective runtime
d764a8e6 7109 initialize functions. */
944fb799 7110static void
d764a8e6
IS
7111build_common_objc_property_accessor_helpers (void)
7112{
7113 tree type;
011d50d9 7114
d764a8e6
IS
7115 /* Declare the following function:
7116 id
944fb799 7117 objc_getProperty (id self, SEL _cmd,
d764a8e6
IS
7118 ptrdiff_t offset, BOOL is_atomic); */
7119 type = build_function_type_list (objc_object_type,
7120 objc_object_type,
7121 objc_selector_type,
7122 ptrdiff_type_node,
7123 boolean_type_node,
7124 NULL_TREE);
7125 objc_getProperty_decl = add_builtin_function ("objc_getProperty",
7126 type, 0, NOT_BUILT_IN,
7127 NULL, NULL_TREE);
7128 TREE_NOTHROW (objc_getProperty_decl) = 0;
944fb799 7129
d764a8e6
IS
7130 /* Declare the following function:
7131 void
944fb799
MS
7132 objc_setProperty (id self, SEL _cmd,
7133 ptrdiff_t offset, id new_value,
d764a8e6
IS
7134 BOOL is_atomic, BOOL should_copy); */
7135 type = build_function_type_list (void_type_node,
7136 objc_object_type,
7137 objc_selector_type,
7138 ptrdiff_type_node,
7139 objc_object_type,
7140 boolean_type_node,
7141 boolean_type_node,
7142 NULL_TREE);
7143 objc_setProperty_decl = add_builtin_function ("objc_setProperty",
7144 type, 0, NOT_BUILT_IN,
7145 NULL, NULL_TREE);
7146 TREE_NOTHROW (objc_setProperty_decl) = 0;
6c65299b
RS
7147}
7148
d764a8e6 7149/* This looks up an ivar in a class (including superclasses). */
6c65299b 7150static tree
d764a8e6 7151lookup_ivar (tree interface, tree instance_variable_name)
6c65299b 7152{
d764a8e6 7153 while (interface)
6c65299b 7154 {
d764a8e6 7155 tree decl_chain;
944fb799 7156
d764a8e6
IS
7157 for (decl_chain = CLASS_IVARS (interface); decl_chain; decl_chain = DECL_CHAIN (decl_chain))
7158 if (DECL_NAME (decl_chain) == instance_variable_name)
7159 return decl_chain;
944fb799 7160
d764a8e6
IS
7161 /* Not found. Search superclass if any. */
7162 if (CLASS_SUPER_NAME (interface))
7163 interface = lookup_interface (CLASS_SUPER_NAME (interface));
6c65299b 7164 }
944fb799 7165
d764a8e6 7166 return NULL_TREE;
6c65299b
RS
7167}
7168
d764a8e6
IS
7169/* This routine synthesizes a 'getter' method. This is only called
7170 for @synthesize properties. */
7171static void
7172objc_synthesize_getter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
6c65299b 7173{
d764a8e6
IS
7174 location_t location = DECL_SOURCE_LOCATION (property);
7175 tree fn, decl;
7176 tree body;
7177 tree ret_val;
6c65299b 7178
d764a8e6
IS
7179 /* If user has implemented a getter with same name then do nothing. */
7180 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7181 PROPERTY_GETTER_NAME (property)))
7182 return;
6c65299b 7183
d764a8e6
IS
7184 /* Find declaration of the property getter in the interface (or
7185 superclass, or protocol). There must be one. */
7186 decl = lookup_method_static (klass, PROPERTY_GETTER_NAME (property), 0);
6c65299b 7187
d764a8e6
IS
7188 /* If one not declared in the interface, this condition has already
7189 been reported as user error (because property was not declared in
7190 the interface). */
7191 if (!decl)
7192 return;
6c65299b 7193
d764a8e6
IS
7194 /* Adapt the 'decl'. Use the source location of the @synthesize
7195 statement for error messages. */
7196 decl = copy_node (decl);
7197 DECL_SOURCE_LOCATION (decl) = location;
6c65299b 7198
a04a722b
JM
7199 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7200 NULL_TREE);
d764a8e6 7201 body = c_begin_compound_stmt (true);
e31c7eec 7202
d764a8e6
IS
7203 /* Now we need to decide how we build the getter. There are three
7204 cases:
a1178b30 7205
d764a8e6
IS
7206 for 'copy' or 'retain' properties we need to use the
7207 objc_getProperty() accessor helper which knows about retain and
7208 copy. It supports both 'nonatomic' and 'atomic' access.
2debdb4f 7209
d764a8e6
IS
7210 for 'nonatomic, assign' properties we can access the instance
7211 variable directly. 'nonatomic' means we don't have to use locks,
7212 and 'assign' means we don't have to worry about retain or copy.
7213 If you combine the two, it means we can just access the instance
7214 variable directly.
2debdb4f 7215
d764a8e6
IS
7216 for 'atomic, assign' properties we use objc_copyStruct() (for the
7217 next runtime) or objc_getPropertyStruct() (for the GNU runtime). */
7218 switch (PROPERTY_ASSIGN_SEMANTICS (property))
a1178b30 7219 {
d764a8e6
IS
7220 case OBJC_PROPERTY_RETAIN:
7221 case OBJC_PROPERTY_COPY:
7222 {
7223 /* We build "return objc_getProperty (self, _cmd, offset, is_atomic);" */
7224 tree cmd, ivar, offset, is_atomic;
7225 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
2debdb4f 7226
d764a8e6
IS
7227 /* Find the ivar to compute the offset. */
7228 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7229 if (!ivar || is_private (ivar))
7230 {
7231 /* This should never happen. */
7232 error_at (location,
7233 "can not find instance variable associated with property");
7234 ret_val = error_mark_node;
7235 break;
7236 }
7237 offset = byte_position (ivar);
2debdb4f 7238
d764a8e6
IS
7239 if (PROPERTY_NONATOMIC (property))
7240 is_atomic = boolean_false_node;
7241 else
7242 is_atomic = boolean_true_node;
7243
7244 ret_val = build_function_call
7245 (location,
7246 /* Function prototype. */
7247 objc_getProperty_decl,
7248 /* Parameters. */
7249 tree_cons /* self */
7250 (NULL_TREE, self_decl,
7251 tree_cons /* _cmd */
7252 (NULL_TREE, cmd,
7253 tree_cons /* offset */
7254 (NULL_TREE, offset,
7255 tree_cons /* is_atomic */
7256 (NULL_TREE, is_atomic, NULL_TREE)))));
7257 }
7258 break;
944fb799 7259 case OBJC_PROPERTY_ASSIGN:
d764a8e6
IS
7260 if (PROPERTY_NONATOMIC (property))
7261 {
7262 /* We build "return self->PROPERTY_IVAR_NAME;" */
7263 ret_val = objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property));
7264 break;
7265 }
7266 else
7267 {
7268 /* We build
7269 <property type> __objc_property_temp;
7270 objc_getPropertyStruct (&__objc_property_temp,
7271 &(self->PROPERTY_IVAR_NAME),
7272 sizeof (type of self->PROPERTY_IVAR_NAME),
7273 is_atomic,
7274 false)
7275 return __objc_property_temp;
7276
7277 For the NeXT runtime, we need to use objc_copyStruct
7278 instead of objc_getPropertyStruct. */
7279 tree objc_property_temp_decl, function_decl, function_call;
7280 tree size_of, is_atomic;
7281
7282 objc_property_temp_decl = objc_create_temporary_var (TREE_TYPE (property), "__objc_property_temp");
7283 DECL_SOURCE_LOCATION (objc_property_temp_decl) = location;
7284 objc_property_temp_decl = lang_hooks.decls.pushdecl (objc_property_temp_decl);
7285
7286 /* sizeof (ivar type). Since the ivar and the property have
7287 the same type, there is no need to lookup the ivar. */
7288 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7289 true /* is_sizeof */,
296674db 7290 false /* min_alignof */,
d764a8e6 7291 false /* complain */);
944fb799 7292
d764a8e6
IS
7293 if (PROPERTY_NONATOMIC (property))
7294 is_atomic = boolean_false_node;
2debdb4f 7295 else
d764a8e6 7296 is_atomic = boolean_true_node;
944fb799 7297
d764a8e6
IS
7298 if (objc_copyStruct_decl)
7299 function_decl = objc_copyStruct_decl;
7300 else
7301 function_decl = objc_getPropertyStruct_decl;
7302
7303 function_call = build_function_call
7304 (location,
7305 /* Function prototype. */
7306 function_decl,
7307 /* Parameters. */
7308 tree_cons /* &__objc_property_temp_decl */
7309 /* Warning: note that using build_fold_addr_expr_loc()
7310 here causes invalid code to be generated. */
7311 (NULL_TREE, build_unary_op (location, ADDR_EXPR, objc_property_temp_decl, 0),
7312 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
944fb799
MS
7313 (NULL_TREE, build_fold_addr_expr_loc (location,
7314 objc_lookup_ivar
d764a8e6
IS
7315 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7316 tree_cons /* sizeof (PROPERTY_IVAR) */
7317 (NULL_TREE, size_of,
7318 tree_cons /* is_atomic */
7319 (NULL_TREE, is_atomic,
7320 /* TODO: This is currently ignored by the GNU
7321 runtime, but what about the next one ? */
7322 tree_cons /* has_strong */
7323 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7324
7325 add_stmt (function_call);
7326
7327 ret_val = objc_property_temp_decl;
2debdb4f 7328 }
d764a8e6
IS
7329 break;
7330 default:
7331 gcc_unreachable ();
2debdb4f
NP
7332 }
7333
d764a8e6 7334 gcc_assert (ret_val);
a1178b30 7335
d764a8e6
IS
7336#ifdef OBJCPLUS
7337 finish_return_stmt (ret_val);
7338#else
7339 c_finish_return (location, ret_val, NULL_TREE);
7340#endif
7341
7342 add_stmt (c_end_compound_stmt (location, body, true));
7343 fn = current_function_decl;
7344#ifdef OBJCPLUS
7345 finish_function ();
7346#endif
7347 objc_finish_method_definition (fn);
a1178b30
IS
7348}
7349
d764a8e6 7350/* This routine synthesizes a 'setter' method. */
af35aeb2 7351
d764a8e6
IS
7352static void
7353objc_synthesize_setter (tree klass, tree class_methods ATTRIBUTE_UNUSED, tree property)
6c65299b 7354{
d764a8e6
IS
7355 location_t location = DECL_SOURCE_LOCATION (property);
7356 tree fn, decl;
7357 tree body;
7358 tree new_value, statement;
6c65299b 7359
d764a8e6
IS
7360 /* If user has implemented a setter with same name then do nothing. */
7361 if (lookup_method (CLASS_NST_METHODS (objc_implementation_context),
7362 PROPERTY_SETTER_NAME (property)))
7363 return;
6c65299b 7364
d764a8e6
IS
7365 /* Find declaration of the property setter in the interface (or
7366 superclass, or protocol). There must be one. */
7367 decl = lookup_method_static (klass, PROPERTY_SETTER_NAME (property), 0);
6c65299b 7368
d764a8e6
IS
7369 /* If one not declared in the interface, this condition has already
7370 been reported as user error (because property was not declared in
7371 the interface). */
7372 if (!decl)
7373 return;
264fa2db 7374
d764a8e6
IS
7375 /* Adapt the 'decl'. Use the source location of the @synthesize
7376 statement for error messages. */
7377 decl = copy_node (decl);
7378 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (property);
6408ef33 7379
a04a722b
JM
7380 objc_start_method_definition (false /* is_class_method */, decl, NULL_TREE,
7381 NULL_TREE);
9eb892ec 7382
d764a8e6 7383 body = c_begin_compound_stmt (true);
6c65299b 7384
d764a8e6
IS
7385 /* The 'new_value' is the only argument to the method, which is the
7386 3rd argument of the function, after self and _cmd. We use twice
7387 TREE_CHAIN to move forward two arguments. */
7388 new_value = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)));
7389
7390 /* This would presumably happen if the user has specified a
7391 prototype for the setter that does not have an argument! */
7392 if (new_value == NULL_TREE)
6c65299b 7393 {
d764a8e6
IS
7394 /* TODO: This should be caught much earlier than this. */
7395 error_at (DECL_SOURCE_LOCATION (decl), "invalid setter, it must have one argument");
7396 /* Try to recover somehow. */
7397 new_value = error_mark_node;
7398 }
6408ef33 7399
d764a8e6
IS
7400 /* Now we need to decide how we build the setter. There are three
7401 cases:
26877873 7402
d764a8e6
IS
7403 for 'copy' or 'retain' properties we need to use the
7404 objc_setProperty() accessor helper which knows about retain and
7405 copy. It supports both 'nonatomic' and 'atomic' access.
6408ef33 7406
d764a8e6
IS
7407 for 'nonatomic, assign' properties we can access the instance
7408 variable directly. 'nonatomic' means we don't have to use locks,
7409 and 'assign' means we don't have to worry about retain or copy.
7410 If you combine the two, it means we can just access the instance
7411 variable directly.
6c65299b 7412
d764a8e6
IS
7413 for 'atomic, assign' properties we use objc_copyStruct() (for the
7414 next runtime) or objc_setPropertyStruct() (for the GNU runtime). */
7415 switch (PROPERTY_ASSIGN_SEMANTICS (property))
6c65299b 7416 {
d764a8e6
IS
7417 case OBJC_PROPERTY_RETAIN:
7418 case OBJC_PROPERTY_COPY:
7419 {
7420 /* We build "objc_setProperty (self, _cmd, new_value, offset, is_atomic, should_copy);" */
7421 tree cmd, ivar, offset, is_atomic, should_copy;
7422 cmd = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
6c65299b 7423
d764a8e6
IS
7424 /* Find the ivar to compute the offset. */
7425 ivar = lookup_ivar (klass, PROPERTY_IVAR_NAME (property));
7426 if (!ivar || is_private (ivar))
7427 {
7428 error_at (location,
7429 "can not find instance variable associated with property");
7430 statement = error_mark_node;
7431 break;
7432 }
7433 offset = byte_position (ivar);
6c65299b 7434
d764a8e6
IS
7435 if (PROPERTY_NONATOMIC (property))
7436 is_atomic = boolean_false_node;
7437 else
7438 is_atomic = boolean_true_node;
944fb799 7439
d764a8e6
IS
7440 if (PROPERTY_ASSIGN_SEMANTICS (property) == OBJC_PROPERTY_COPY)
7441 should_copy = boolean_true_node;
7442 else
7443 should_copy = boolean_false_node;
38b9c8c3 7444
d764a8e6
IS
7445 statement = build_function_call
7446 (location,
7447 /* Function prototype. */
7448 objc_setProperty_decl,
7449 /* Parameters. */
7450 tree_cons /* self */
7451 (NULL_TREE, self_decl,
7452 tree_cons /* _cmd */
7453 (NULL_TREE, cmd,
7454 tree_cons /* offset */
7455 (NULL_TREE, offset,
7456 tree_cons /* new_value */
7457 (NULL_TREE, new_value,
7458 tree_cons /* is_atomic */
944fb799 7459 (NULL_TREE, is_atomic,
d764a8e6
IS
7460 tree_cons /* should_copy */
7461 (NULL_TREE, should_copy, NULL_TREE)))))));
7462 }
7463 break;
944fb799 7464 case OBJC_PROPERTY_ASSIGN:
d764a8e6
IS
7465 if (PROPERTY_NONATOMIC (property))
7466 {
7467 /* We build "self->PROPERTY_IVAR_NAME = new_value;" */
7468 statement = build_modify_expr
7469 (location,
7470 objc_lookup_ivar (NULL_TREE, PROPERTY_IVAR_NAME (property)),
944fb799 7471 NULL_TREE, NOP_EXPR,
d764a8e6
IS
7472 location, new_value, NULL_TREE);
7473 break;
7474 }
7475 else
7476 {
7477 /* We build
7478 objc_setPropertyStruct (&(self->PROPERTY_IVAR_NAME),
7479 &new_value,
7480 sizeof (type of self->PROPERTY_IVAR_NAME),
7481 is_atomic,
7482 false)
38b9c8c3 7483
d764a8e6
IS
7484 For the NeXT runtime, we need to use objc_copyStruct
7485 instead of objc_getPropertyStruct. */
7486 tree function_decl, size_of, is_atomic;
52daca75 7487
d764a8e6
IS
7488 /* sizeof (ivar type). Since the ivar and the property have
7489 the same type, there is no need to lookup the ivar. */
7490 size_of = c_sizeof_or_alignof_type (location, TREE_TYPE (property),
7491 true /* is_sizeof */,
296674db 7492 false /* min_alignof */,
d764a8e6 7493 false /* complain */);
944fb799 7494
d764a8e6
IS
7495 if (PROPERTY_NONATOMIC (property))
7496 is_atomic = boolean_false_node;
52daca75 7497 else
d764a8e6 7498 is_atomic = boolean_true_node;
944fb799 7499
d764a8e6
IS
7500 if (objc_copyStruct_decl)
7501 function_decl = objc_copyStruct_decl;
7502 else
7503 function_decl = objc_setPropertyStruct_decl;
52daca75 7504
944fb799 7505 statement = build_function_call
d764a8e6
IS
7506 (location,
7507 /* Function prototype. */
7508 function_decl,
7509 /* Parameters. */
7510 tree_cons /* &(self->PROPERTY_IVAR_NAME); */
944fb799
MS
7511 (NULL_TREE, build_fold_addr_expr_loc (location,
7512 objc_lookup_ivar
d764a8e6
IS
7513 (NULL_TREE, PROPERTY_IVAR_NAME (property))),
7514 tree_cons /* &new_value */
7515 (NULL_TREE, build_fold_addr_expr_loc (location, new_value),
7516 tree_cons /* sizeof (PROPERTY_IVAR) */
7517 (NULL_TREE, size_of,
7518 tree_cons /* is_atomic */
7519 (NULL_TREE, is_atomic,
7520 /* TODO: This is currently ignored by the GNU
7521 runtime, but what about the next one ? */
7522 tree_cons /* has_strong */
7523 (NULL_TREE, boolean_true_node, NULL_TREE))))));
7524 }
7525 break;
7526 default:
7527 gcc_unreachable ();
6c65299b 7528 }
d764a8e6 7529 gcc_assert (statement);
6c65299b 7530
944fb799 7531 add_stmt (statement);
d764a8e6
IS
7532 add_stmt (c_end_compound_stmt (location, body, true));
7533 fn = current_function_decl;
7534#ifdef OBJCPLUS
7535 finish_function ();
7536#endif
7537 objc_finish_method_definition (fn);
7538}
59ce3fc8 7539
d764a8e6
IS
7540/* This function is a sub-routine of objc_add_synthesize_declaration.
7541 It is called for each property to synthesize once we have
7542 determined that the context is Ok. */
7543static void
7544objc_add_synthesize_declaration_for_property (location_t location, tree interface,
7545 tree property_name, tree ivar_name)
6c65299b 7546{
d764a8e6
IS
7547 /* Find the @property declaration. */
7548 tree property;
7549 tree x;
f2e6e530 7550
d764a8e6
IS
7551 /* Check that synthesize or dynamic has not already been used for
7552 the same property. */
7553 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7554 if (PROPERTY_NAME (property) == property_name)
7555 {
7556 location_t original_location = DECL_SOURCE_LOCATION (property);
944fb799 7557
d764a8e6 7558 if (PROPERTY_DYNAMIC (property))
944fb799 7559 error_at (location, "property %qs already specified in %<@dynamic%>",
d764a8e6
IS
7560 IDENTIFIER_POINTER (property_name));
7561 else
944fb799 7562 error_at (location, "property %qs already specified in %<@synthesize%>",
d764a8e6 7563 IDENTIFIER_POINTER (property_name));
944fb799 7564
d764a8e6
IS
7565 if (original_location != UNKNOWN_LOCATION)
7566 inform (original_location, "originally specified here");
7567 return;
7568 }
011d50d9 7569
d764a8e6
IS
7570 /* Check that the property is declared in the interface. It could
7571 also be declared in a superclass or protocol. */
7572 property = lookup_property (interface, property_name);
264fa2db 7573
d764a8e6 7574 if (!property)
264fa2db 7575 {
944fb799 7576 error_at (location, "no declaration of property %qs found in the interface",
d764a8e6
IS
7577 IDENTIFIER_POINTER (property_name));
7578 return;
7579 }
7580 else
7581 {
7582 /* We have to copy the property, because we want to chain it to
7583 the implementation context, and we want to store the source
7584 location of the @synthesize, not of the original
7585 @property. */
7586 property = copy_node (property);
7587 DECL_SOURCE_LOCATION (property) = location;
6c65299b 7588 }
6c65299b 7589
d764a8e6
IS
7590 /* Determine PROPERTY_IVAR_NAME. */
7591 if (ivar_name == NULL_TREE)
7592 ivar_name = property_name;
6c65299b 7593
d764a8e6
IS
7594 /* Check that the instance variable exists. You can only use an
7595 instance variable from the same class, not one from the
7596 superclass (this makes sense as it allows us to check that an
7597 instance variable is only used in one synthesized property). */
7598 {
7599 tree ivar = is_ivar (CLASS_IVARS (interface), ivar_name);
7600 tree type_of_ivar;
7601 if (!ivar)
7602 {
944fb799 7603 error_at (location, "ivar %qs used by %<@synthesize%> declaration must be an existing ivar",
d764a8e6
IS
7604 IDENTIFIER_POINTER (property_name));
7605 return;
7606 }
6c65299b 7607
d764a8e6
IS
7608 if (DECL_BIT_FIELD_TYPE (ivar))
7609 type_of_ivar = DECL_BIT_FIELD_TYPE (ivar);
7610 else
7611 type_of_ivar = TREE_TYPE (ivar);
944fb799 7612
d764a8e6
IS
7613 /* If the instance variable has a different C type, we throw an error ... */
7614 if (!comptypes (TREE_TYPE (property), type_of_ivar)
7615 /* ... unless the property is readonly, in which case we allow
7616 the instance variable to be more specialized (this means we
7617 can generate the getter all right and it works). */
7618 && (!PROPERTY_READONLY (property)
7619 || !objc_compare_types (TREE_TYPE (property),
7620 type_of_ivar, -5, NULL_TREE)))
7621 {
7622 location_t original_location = DECL_SOURCE_LOCATION (ivar);
944fb799 7623
d764a8e6
IS
7624 error_at (location, "property %qs is using instance variable %qs of incompatible type",
7625 IDENTIFIER_POINTER (property_name),
7626 IDENTIFIER_POINTER (ivar_name));
944fb799 7627
d764a8e6
IS
7628 if (original_location != UNKNOWN_LOCATION)
7629 inform (original_location, "originally specified here");
7630 }
e31c7eec 7631
d764a8e6
IS
7632 /* If the instance variable is a bitfield, the property must be
7633 'assign', 'nonatomic' because the runtime getter/setter helper
7634 do not work with bitfield instance variables. */
7635 if (DECL_BIT_FIELD_TYPE (ivar))
7636 {
7637 /* If there is an error, we return and not generate any
7638 getter/setter because trying to set up the runtime
7639 getter/setter helper calls with bitfields is at high risk
7640 of ICE. */
6c65299b 7641
d764a8e6
IS
7642 if (PROPERTY_ASSIGN_SEMANTICS (property) != OBJC_PROPERTY_ASSIGN)
7643 {
7644 location_t original_location = DECL_SOURCE_LOCATION (ivar);
944fb799 7645
d764a8e6
IS
7646 error_at (location, "'assign' property %qs is using bit-field instance variable %qs",
7647 IDENTIFIER_POINTER (property_name),
7648 IDENTIFIER_POINTER (ivar_name));
944fb799 7649
d764a8e6
IS
7650 if (original_location != UNKNOWN_LOCATION)
7651 inform (original_location, "originally specified here");
7652 return;
7653 }
6c65299b 7654
d764a8e6
IS
7655 if (!PROPERTY_NONATOMIC (property))
7656 {
7657 location_t original_location = DECL_SOURCE_LOCATION (ivar);
944fb799 7658
d764a8e6
IS
7659 error_at (location, "'atomic' property %qs is using bit-field instance variable %qs",
7660 IDENTIFIER_POINTER (property_name),
7661 IDENTIFIER_POINTER (ivar_name));
944fb799 7662
d764a8e6
IS
7663 if (original_location != UNKNOWN_LOCATION)
7664 inform (original_location, "originally specified here");
7665 return;
7666 }
7667 }
7668 }
6c65299b 7669
d764a8e6
IS
7670 /* Check that no other property is using the same instance
7671 variable. */
7672 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7673 if (PROPERTY_IVAR_NAME (x) == ivar_name)
7674 {
7675 location_t original_location = DECL_SOURCE_LOCATION (x);
944fb799 7676
d764a8e6
IS
7677 error_at (location, "property %qs is using the same instance variable as property %qs",
7678 IDENTIFIER_POINTER (property_name),
7679 IDENTIFIER_POINTER (PROPERTY_NAME (x)));
944fb799 7680
d764a8e6
IS
7681 if (original_location != UNKNOWN_LOCATION)
7682 inform (original_location, "originally specified here");
944fb799 7683
d764a8e6
IS
7684 /* We keep going on. This won't cause the compiler to fail;
7685 the failure would most likely be at runtime. */
7686 }
6c65299b 7687
d764a8e6
IS
7688 /* Note that a @synthesize (and only a @synthesize) always sets
7689 PROPERTY_IVAR_NAME to a non-NULL_TREE. You can recognize a
7690 @synthesize by that. */
7691 PROPERTY_IVAR_NAME (property) = ivar_name;
944fb799 7692
d764a8e6
IS
7693 /* PROPERTY_SETTER_NAME and PROPERTY_GETTER_NAME are copied from the
7694 original declaration; they are always set (with the exception of
7695 PROPERTY_SETTER_NAME not being set if PROPERTY_READONLY == 1). */
6c65299b 7696
d764a8e6
IS
7697 /* Add the property to the list of properties for current implementation. */
7698 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7699 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
6c65299b 7700
d764a8e6
IS
7701 /* Note how we don't actually synthesize the getter/setter here; it
7702 would be very natural, but we may miss the fact that the user has
7703 implemented his own getter/setter later on in the @implementation
7704 (in which case we shouldn't generate getter/setter). We wait
7705 until we have parsed it all before generating the code. */
7706}
6c65299b 7707
d764a8e6
IS
7708/* This function is called by the parser after a @synthesize
7709 expression is parsed. 'location' is the location of the
7710 @synthesize expression, and 'property_and_ivar_list' is a chained
7711 list of the property and ivar names. */
7712void
7713objc_add_synthesize_declaration (location_t location, tree property_and_ivar_list)
7714{
7715 tree interface, chain;
264fa2db 7716
d764a8e6
IS
7717 if (flag_objc1_only)
7718 error_at (input_location, "%<@synthesize%> is not available in Objective-C 1.0");
8607f1bc 7719
d764a8e6
IS
7720 if (property_and_ivar_list == error_mark_node)
7721 return;
81ff723b 7722
d764a8e6
IS
7723 if (!objc_implementation_context)
7724 {
7725 /* We can get here only in Objective-C; the Objective-C++ parser
7726 detects the problem while parsing, outputs the error
7727 "misplaced '@synthesize' Objective-C++ construct" and skips
7728 the declaration. */
7729 error_at (location, "%<@synthesize%> not in @implementation context");
7730 return;
7731 }
81ff723b 7732
d764a8e6
IS
7733 if (TREE_CODE (objc_implementation_context) == CATEGORY_IMPLEMENTATION_TYPE)
7734 {
7735 error_at (location, "%<@synthesize%> can not be used in categories");
7736 return;
7737 }
64ee9490 7738
d764a8e6
IS
7739 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7740 if (!interface)
81ff723b 7741 {
d764a8e6 7742 /* I can't see how this could happen, but it is good as a safety check. */
944fb799 7743 error_at (location,
d764a8e6
IS
7744 "%<@synthesize%> requires the @interface of the class to be available");
7745 return;
81ff723b 7746 }
264fa2db 7747
d764a8e6
IS
7748 /* Now, iterate over the properties and do each of them. */
7749 for (chain = property_and_ivar_list; chain; chain = TREE_CHAIN (chain))
7750 {
944fb799 7751 objc_add_synthesize_declaration_for_property (location, interface, TREE_VALUE (chain),
d764a8e6
IS
7752 TREE_PURPOSE (chain));
7753 }
264fa2db
ZL
7754}
7755
d764a8e6
IS
7756/* This function is a sub-routine of objc_add_dynamic_declaration. It
7757 is called for each property to mark as dynamic once we have
7758 determined that the context is Ok. */
7759static void
7760objc_add_dynamic_declaration_for_property (location_t location, tree interface,
7761 tree property_name)
011d50d9 7762{
d764a8e6
IS
7763 /* Find the @property declaration. */
7764 tree property;
668ea4b1 7765
d764a8e6
IS
7766 /* Check that synthesize or dynamic has not already been used for
7767 the same property. */
7768 for (property = IMPL_PROPERTY_DECL (objc_implementation_context); property; property = TREE_CHAIN (property))
7769 if (PROPERTY_NAME (property) == property_name)
7770 {
7771 location_t original_location = DECL_SOURCE_LOCATION (property);
944fb799 7772
d764a8e6 7773 if (PROPERTY_DYNAMIC (property))
944fb799 7774 error_at (location, "property %qs already specified in %<@dynamic%>",
d764a8e6
IS
7775 IDENTIFIER_POINTER (property_name));
7776 else
7777 error_at (location, "property %qs already specified in %<@synthesize%>",
7778 IDENTIFIER_POINTER (property_name));
264fa2db 7779
d764a8e6
IS
7780 if (original_location != UNKNOWN_LOCATION)
7781 inform (original_location, "originally specified here");
7782 return;
7783 }
38b9c8c3 7784
d764a8e6
IS
7785 /* Check that the property is declared in the interface. It could
7786 also be declared in a superclass or protocol. */
7787 property = lookup_property (interface, property_name);
264fa2db 7788
d764a8e6 7789 if (!property)
264fa2db 7790 {
d764a8e6
IS
7791 error_at (location, "no declaration of property %qs found in the interface",
7792 IDENTIFIER_POINTER (property_name));
7793 return;
8607f1bc 7794 }
d764a8e6 7795 else
6c65299b 7796 {
d764a8e6
IS
7797 /* We have to copy the property, because we want to chain it to
7798 the implementation context, and we want to store the source
7799 location of the @synthesize, not of the original
7800 @property. */
7801 property = copy_node (property);
7802 DECL_SOURCE_LOCATION (property) = location;
7803 }
e31c7eec 7804
d764a8e6
IS
7805 /* Note that a @dynamic (and only a @dynamic) always sets
7806 PROPERTY_DYNAMIC to 1. You can recognize a @dynamic by that.
7807 (actually, as explained above, PROPERTY_DECL generated by
7808 @property and associated with a @dynamic property are also marked
7809 as PROPERTY_DYNAMIC). */
7810 PROPERTY_DYNAMIC (property) = 1;
3a3589b4 7811
d764a8e6
IS
7812 /* Add the property to the list of properties for current implementation. */
7813 TREE_CHAIN (property) = IMPL_PROPERTY_DECL (objc_implementation_context);
7814 IMPL_PROPERTY_DECL (objc_implementation_context) = property;
7815}
3a3589b4 7816
d764a8e6
IS
7817/* This function is called by the parser after a @dynamic expression
7818 is parsed. 'location' is the location of the @dynamic expression,
7819 and 'property_list' is a chained list of all the property
7820 names. */
7821void
7822objc_add_dynamic_declaration (location_t location, tree property_list)
7823{
7824 tree interface, chain;
264fa2db 7825
d764a8e6
IS
7826 if (flag_objc1_only)
7827 error_at (input_location, "%<@dynamic%> is not available in Objective-C 1.0");
e31c7eec 7828
d764a8e6
IS
7829 if (property_list == error_mark_node)
7830 return;
264fa2db 7831
d764a8e6
IS
7832 if (!objc_implementation_context)
7833 {
7834 /* We can get here only in Objective-C; the Objective-C++ parser
7835 detects the problem while parsing, outputs the error
7836 "misplaced '@dynamic' Objective-C++ construct" and skips the
7837 declaration. */
7838 error_at (location, "%<@dynamic%> not in @implementation context");
7839 return;
64ee9490 7840 }
e31c7eec 7841
d764a8e6
IS
7842 /* @dynamic is allowed in categories. */
7843 switch (TREE_CODE (objc_implementation_context))
3a3589b4 7844 {
d764a8e6
IS
7845 case CLASS_IMPLEMENTATION_TYPE:
7846 interface = lookup_interface (CLASS_NAME (objc_implementation_context));
7847 break;
7848 case CATEGORY_IMPLEMENTATION_TYPE:
944fb799 7849 interface = lookup_category (implementation_template,
d764a8e6
IS
7850 CLASS_SUPER_NAME (objc_implementation_context));
7851 break;
7852 default:
7853 gcc_unreachable ();
7854 }
3a3589b4 7855
d764a8e6
IS
7856 if (!interface)
7857 {
7858 /* I can't see how this could happen, but it is good as a safety check. */
7859 error_at (location,
7860 "%<@dynamic%> requires the @interface of the class to be available");
7861 return;
3a3589b4
ZL
7862 }
7863
d764a8e6
IS
7864 /* Now, iterate over the properties and do each of them. */
7865 for (chain = property_list; chain; chain = TREE_CHAIN (chain))
6c65299b 7866 {
d764a8e6
IS
7867 objc_add_dynamic_declaration_for_property (location, interface, TREE_VALUE (chain));
7868 }
7869}
011d50d9 7870
944fb799 7871/* Main routine to generate code/data for all the property information for
d764a8e6
IS
7872 current implementation (class or category). CLASS is the interface where
7873 ivars are declared. CLASS_METHODS is where methods are found which
7874 could be a class or a category depending on whether we are implementing
7875 property of a class or a category. */
3a3589b4 7876
d764a8e6
IS
7877static void
7878objc_gen_property_data (tree klass, tree class_methods)
7879{
7880 tree x;
2debdb4f 7881
d764a8e6
IS
7882 for (x = IMPL_PROPERTY_DECL (objc_implementation_context); x; x = TREE_CHAIN (x))
7883 {
7884 /* @dynamic property - nothing to check or synthesize. */
7885 if (PROPERTY_DYNAMIC (x))
7886 continue;
944fb799 7887
d764a8e6
IS
7888 /* @synthesize property - need to synthesize the accessors. */
7889 if (PROPERTY_IVAR_NAME (x))
b4f588c4 7890 {
d764a8e6 7891 objc_synthesize_getter (klass, class_methods, x);
944fb799 7892
d764a8e6
IS
7893 if (PROPERTY_READONLY (x) == 0)
7894 objc_synthesize_setter (klass, class_methods, x);
7895
7896 continue;
b4f588c4 7897 }
2debdb4f 7898
d764a8e6
IS
7899 gcc_unreachable ();
7900 }
7901}
6c65299b 7902
d764a8e6 7903/* This is called once we see the "@end" in an interface/implementation. */
6c65299b 7904
d764a8e6
IS
7905static void
7906finish_class (tree klass)
7907{
7908 switch (TREE_CODE (klass))
7909 {
7910 case CLASS_IMPLEMENTATION_TYPE:
7911 {
7912 /* All metadata generation is done in runtime.generate_metadata(). */
944fb799 7913
d764a8e6
IS
7914 /* Generate what needed for property; setters, getters, etc. */
7915 objc_gen_property_data (implementation_template, implementation_template);
5d0f30f7 7916
d764a8e6
IS
7917 if (implementation_template != objc_implementation_context)
7918 {
7919 /* Ensure that all method listed in the interface contain bodies. */
7920 check_methods (CLASS_CLS_METHODS (implementation_template),
7921 objc_implementation_context, '+');
7922 check_methods (CLASS_NST_METHODS (implementation_template),
7923 objc_implementation_context, '-');
5d0f30f7 7924
d764a8e6
IS
7925 if (CLASS_PROTOCOL_LIST (implementation_template))
7926 check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
7927 "class",
7928 CLASS_NAME (objc_implementation_context));
7929 }
7930 break;
7931 }
7932 case CATEGORY_IMPLEMENTATION_TYPE:
7933 {
7934 tree category = lookup_category (implementation_template, CLASS_SUPER_NAME (klass));
944fb799 7935
d764a8e6
IS
7936 if (category)
7937 {
7938 /* Generate what needed for property; setters, getters, etc. */
7939 objc_gen_property_data (implementation_template, category);
6c65299b 7940
d764a8e6
IS
7941 /* Ensure all method listed in the interface contain bodies. */
7942 check_methods (CLASS_CLS_METHODS (category),
7943 objc_implementation_context, '+');
7944 check_methods (CLASS_NST_METHODS (category),
7945 objc_implementation_context, '-');
944fb799 7946
d764a8e6
IS
7947 if (CLASS_PROTOCOL_LIST (category))
7948 check_protocols (CLASS_PROTOCOL_LIST (category),
7949 "category",
7950 CLASS_SUPER_NAME (objc_implementation_context));
7951 }
7952 break;
7953 }
7954 case CLASS_INTERFACE_TYPE:
7955 case CATEGORY_INTERFACE_TYPE:
7956 case PROTOCOL_INTERFACE_TYPE:
7957 {
7958 /* Process properties of the class. */
7959 tree x;
7960 for (x = CLASS_PROPERTY_DECL (objc_interface_context); x; x = TREE_CHAIN (x))
7961 {
7962 /* Now we check that the appropriate getter is declared,
7963 and if not, we declare one ourselves. */
7964 tree getter_decl = lookup_method (CLASS_NST_METHODS (klass),
7965 PROPERTY_GETTER_NAME (x));
944fb799 7966
d764a8e6
IS
7967 if (getter_decl)
7968 {
7969 /* TODO: Check that the declaration is consistent with the property. */
7970 ;
7971 }
7972 else
7973 {
7974 /* Generate an instance method declaration for the
7975 getter; for example "- (id) name;". In general it
7976 will be of the form
7977 -(type)property_getter_name; */
7978 tree rettype = build_tree_list (NULL_TREE, TREE_TYPE (x));
944fb799
MS
7979 getter_decl = build_method_decl (INSTANCE_METHOD_DECL,
7980 rettype, PROPERTY_GETTER_NAME (x),
d764a8e6
IS
7981 NULL_TREE, false);
7982 if (PROPERTY_OPTIONAL (x))
7983 objc_add_method (objc_interface_context, getter_decl, false, true);
7984 else
7985 objc_add_method (objc_interface_context, getter_decl, false, false);
7986 TREE_DEPRECATED (getter_decl) = TREE_DEPRECATED (x);
7987 METHOD_PROPERTY_CONTEXT (getter_decl) = x;
7988 }
6c65299b 7989
d764a8e6
IS
7990 if (PROPERTY_READONLY (x) == 0)
7991 {
7992 /* Now we check that the appropriate setter is declared,
7993 and if not, we declare on ourselves. */
944fb799 7994 tree setter_decl = lookup_method (CLASS_NST_METHODS (klass),
d764a8e6 7995 PROPERTY_SETTER_NAME (x));
944fb799 7996
d764a8e6
IS
7997 if (setter_decl)
7998 {
7999 /* TODO: Check that the declaration is consistent with the property. */
8000 ;
8001 }
8002 else
8003 {
8004 /* The setter name is something like 'setName:'.
8005 We need the substring 'setName' to build the
8006 method declaration due to how the declaration
8007 works. TODO: build_method_decl() will then
8008 generate back 'setName:' from 'setName'; it
8009 would be more efficient to hook into there. */
8010 const char *full_setter_name = IDENTIFIER_POINTER (PROPERTY_SETTER_NAME (x));
8011 size_t length = strlen (full_setter_name);
8012 char *setter_name = (char *) alloca (length);
8013 tree ret_type, selector, arg_type, arg_name;
944fb799 8014
d764a8e6
IS
8015 strcpy (setter_name, full_setter_name);
8016 setter_name[length - 1] = '\0';
8017 ret_type = build_tree_list (NULL_TREE, void_type_node);
8018 arg_type = build_tree_list (NULL_TREE, TREE_TYPE (x));
8019 arg_name = get_identifier ("_value");
8020 selector = objc_build_keyword_decl (get_identifier (setter_name),
8021 arg_type, arg_name, NULL);
944fb799 8022 setter_decl = build_method_decl (INSTANCE_METHOD_DECL,
d764a8e6
IS
8023 ret_type, selector,
8024 build_tree_list (NULL_TREE, NULL_TREE),
8025 false);
8026 if (PROPERTY_OPTIONAL (x))
8027 objc_add_method (objc_interface_context, setter_decl, false, true);
8028 else
8029 objc_add_method (objc_interface_context, setter_decl, false, false);
8030 TREE_DEPRECATED (setter_decl) = TREE_DEPRECATED (x);
8031 METHOD_PROPERTY_CONTEXT (setter_decl) = x;
944fb799 8032 }
d764a8e6
IS
8033 }
8034 }
8035 break;
8036 }
8037 default:
8038 gcc_unreachable ();
8039 break;
8040 }
6c65299b 8041}
6c65299b
RS
8042
8043static tree
d764a8e6
IS
8044add_protocol (tree protocol)
8045{
8046 /* Put protocol on list in reverse order. */
8047 TREE_CHAIN (protocol) = protocol_chain;
8048 protocol_chain = protocol;
8049 return protocol_chain;
8050}
7427ba36 8051
d764a8e6
IS
8052/* Check that a protocol is defined, and, recursively, that all
8053 protocols that this protocol conforms to are defined too. */
8054static void
8055check_that_protocol_is_defined (tree protocol)
8056{
8057 if (!PROTOCOL_DEFINED (protocol))
8058 warning (0, "definition of protocol %qE not found",
8059 PROTOCOL_NAME (protocol));
8060
8061 /* If the protocol itself conforms to other protocols, check them
8062 too, recursively. */
8063 if (PROTOCOL_LIST (protocol))
6c65299b 8064 {
d764a8e6 8065 tree p;
7427ba36 8066
d764a8e6
IS
8067 for (p = PROTOCOL_LIST (protocol); p; p = TREE_CHAIN (p))
8068 check_that_protocol_is_defined (TREE_VALUE (p));
7427ba36 8069 }
e31c7eec 8070}
7427ba36 8071
d764a8e6
IS
8072/* Looks up a protocol. If 'warn_if_deprecated' is true, a warning is
8073 emitted if the protocol is deprecated. If 'definition_required' is
8074 true, a warning is emitted if a full @protocol definition has not
8075 been seen. */
8076static tree
8077lookup_protocol (tree ident, bool warn_if_deprecated, bool definition_required)
e31c7eec 8078{
d764a8e6 8079 tree chain;
e31c7eec 8080
d764a8e6
IS
8081 for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
8082 if (ident == PROTOCOL_NAME (chain))
8083 {
8084 if (warn_if_deprecated && TREE_DEPRECATED (chain))
8085 {
8086 /* It would be nice to use warn_deprecated_use() here, but
8087 we are using TREE_CHAIN (which is supposed to be the
8088 TYPE_STUB_DECL for a TYPE) for something different. */
944fb799 8089 warning (OPT_Wdeprecated_declarations, "protocol %qE is deprecated",
d764a8e6
IS
8090 PROTOCOL_NAME (chain));
8091 }
e31c7eec 8092
d764a8e6
IS
8093 if (definition_required)
8094 check_that_protocol_is_defined (chain);
e31c7eec 8095
d764a8e6
IS
8096 return chain;
8097 }
e31c7eec 8098
d764a8e6
IS
8099 return NULL_TREE;
8100}
8101
8102/* This function forward declares the protocols named by NAMES. If
8103 they are already declared or defined, the function has no effect. */
8104
8105void
c59633d9 8106objc_declare_protocol (tree name, tree attributes)
e31c7eec 8107{
d764a8e6 8108 bool deprecated = false;
e31c7eec 8109
d764a8e6
IS
8110#ifdef OBJCPLUS
8111 if (current_namespace != global_namespace) {
8112 error ("Objective-C declarations may only appear in global scope");
8113 }
8114#endif /* OBJCPLUS */
8115
8116 /* Determine if 'deprecated', the only attribute we recognize for
8117 protocols, was used. Ignore all other attributes. */
8118 if (attributes)
e31c7eec 8119 {
d764a8e6
IS
8120 tree attribute;
8121 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
8122 {
8123 tree name = TREE_PURPOSE (attribute);
944fb799 8124
d764a8e6
IS
8125 if (is_attribute_p ("deprecated", name))
8126 deprecated = true;
8127 else
8128 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
8129 }
6c65299b 8130 }
e31c7eec 8131
c59633d9
NP
8132 if (lookup_protocol (name, /* warn if deprecated */ false,
8133 /* definition_required */ false) == NULL_TREE)
d764a8e6 8134 {
c59633d9 8135 tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
944fb799 8136
c59633d9
NP
8137 TYPE_LANG_SLOT_1 (protocol)
8138 = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
8139 PROTOCOL_NAME (protocol) = name;
8140 PROTOCOL_LIST (protocol) = NULL_TREE;
8141 add_protocol (protocol);
8142 PROTOCOL_DEFINED (protocol) = 0;
8143 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
944fb799 8144
c59633d9 8145 if (attributes)
5873a4c1 8146 {
c59633d9
NP
8147 /* TODO: Do we need to store the attributes here ? */
8148 TYPE_ATTRIBUTES (protocol) = attributes;
8149 if (deprecated)
8150 TREE_DEPRECATED (protocol) = 1;
d764a8e6 8151 }
5873a4c1 8152 }
6c65299b 8153}
e31c7eec 8154
d764a8e6
IS
8155static tree
8156start_protocol (enum tree_code code, tree name, tree list, tree attributes)
6c65299b 8157{
d764a8e6
IS
8158 tree protocol;
8159 bool deprecated = false;
6c65299b 8160
d764a8e6
IS
8161#ifdef OBJCPLUS
8162 if (current_namespace != global_namespace) {
8163 error ("Objective-C declarations may only appear in global scope");
8164 }
8165#endif /* OBJCPLUS */
6c65299b 8166
d764a8e6
IS
8167 /* Determine if 'deprecated', the only attribute we recognize for
8168 protocols, was used. Ignore all other attributes. */
8169 if (attributes)
ece4ce85 8170 {
d764a8e6
IS
8171 tree attribute;
8172 for (attribute = attributes; attribute; attribute = TREE_CHAIN (attribute))
ece4ce85 8173 {
d764a8e6 8174 tree name = TREE_PURPOSE (attribute);
944fb799 8175
d764a8e6
IS
8176 if (is_attribute_p ("deprecated", name))
8177 deprecated = true;
8178 else
8179 warning (OPT_Wattributes, "%qE attribute directive ignored", name);
ece4ce85
NP
8180 }
8181 }
011d50d9 8182
d764a8e6
IS
8183 protocol = lookup_protocol (name, /* warn_if_deprecated */ false,
8184 /* definition_required */ false);
ece4ce85 8185
d764a8e6
IS
8186 if (!protocol)
8187 {
8188 protocol = make_node (code);
8189 TYPE_LANG_SLOT_1 (protocol) = make_tree_vec (PROTOCOL_LANG_SLOT_ELTS);
6c65299b 8190
d764a8e6
IS
8191 PROTOCOL_NAME (protocol) = name;
8192 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
8193 add_protocol (protocol);
8194 PROTOCOL_DEFINED (protocol) = 1;
8195 PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
9dee6758 8196
d764a8e6
IS
8197 check_protocol_recursively (protocol, list);
8198 }
8199 else if (! PROTOCOL_DEFINED (protocol))
8200 {
8201 PROTOCOL_DEFINED (protocol) = 1;
8202 PROTOCOL_LIST (protocol) = lookup_and_install_protocols (list, /* definition_required */ false);
6c65299b 8203
d764a8e6
IS
8204 check_protocol_recursively (protocol, list);
8205 }
8206 else
8207 {
8208 warning (0, "duplicate declaration for protocol %qE",
8209 name);
8210 }
6c65299b 8211
d764a8e6 8212 if (attributes)
a878dab7 8213 {
d764a8e6
IS
8214 TYPE_ATTRIBUTES (protocol) = attributes;
8215 if (deprecated)
8216 TREE_DEPRECATED (protocol) = 1;
a878dab7 8217 }
6c65299b 8218
d764a8e6 8219 return protocol;
6c65299b 8220}
011d50d9 8221
d764a8e6 8222/* Decay array and function parameters into pointers. */
011d50d9 8223
d764a8e6
IS
8224static tree
8225objc_decay_parm_type (tree type)
6c65299b 8226{
d764a8e6
IS
8227 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE)
8228 type = build_pointer_type (TREE_CODE (type) == ARRAY_TYPE
8229 ? TREE_TYPE (type)
8230 : type);
660820d1 8231
d764a8e6 8232 return type;
6c65299b
RS
8233}
8234
d764a8e6 8235static GTY(()) tree objc_parmlist = NULL_TREE;
660820d1 8236
d764a8e6
IS
8237/* Append PARM to a list of formal parameters of a method, making a necessary
8238 array-to-pointer adjustment along the way. */
8239
8240void
8241objc_push_parm (tree parm)
660820d1 8242{
d764a8e6 8243 tree type;
660820d1 8244
d764a8e6 8245 if (TREE_TYPE (parm) == error_mark_node)
660820d1 8246 {
d764a8e6
IS
8247 objc_parmlist = chainon (objc_parmlist, parm);
8248 return;
660820d1 8249 }
660820d1 8250
d764a8e6
IS
8251 /* Decay arrays and functions into pointers. */
8252 type = objc_decay_parm_type (TREE_TYPE (parm));
660820d1 8253
d764a8e6
IS
8254 /* If the parameter type has been decayed, a new PARM_DECL needs to be
8255 built as well. */
8256 if (type != TREE_TYPE (parm))
8257 parm = build_decl (input_location, PARM_DECL, DECL_NAME (parm), type);
660820d1 8258
d764a8e6
IS
8259 DECL_ARG_TYPE (parm)
8260 = lang_hooks.types.type_promotes_to (TREE_TYPE (parm));
660820d1 8261
d764a8e6
IS
8262 /* Record constancy and volatility. */
8263 c_apply_type_quals_to_decl
8264 ((TYPE_READONLY (TREE_TYPE (parm)) ? TYPE_QUAL_CONST : 0)
8265 | (TYPE_RESTRICT (TREE_TYPE (parm)) ? TYPE_QUAL_RESTRICT : 0)
267bac10 8266 | (TYPE_ATOMIC (TREE_TYPE (parm)) ? TYPE_QUAL_ATOMIC : 0)
d764a8e6 8267 | (TYPE_VOLATILE (TREE_TYPE (parm)) ? TYPE_QUAL_VOLATILE : 0), parm);
660820d1 8268
d764a8e6 8269 objc_parmlist = chainon (objc_parmlist, parm);
660820d1
NP
8270}
8271
d764a8e6
IS
8272/* Retrieve the formal parameter list constructed via preceding calls to
8273 objc_push_parm(). */
4ce693cd 8274
d764a8e6
IS
8275#ifdef OBJCPLUS
8276tree
a04a722b
JM
8277objc_get_parm_info (int have_ellipsis ATTRIBUTE_UNUSED,
8278 tree expr ATTRIBUTE_UNUSED)
6c65299b 8279{
d764a8e6
IS
8280 tree parm_info = objc_parmlist;
8281 objc_parmlist = NULL_TREE;
6c65299b 8282
d764a8e6 8283 return parm_info;
6c65299b 8284}
d764a8e6
IS
8285#else
8286struct c_arg_info *
a04a722b 8287objc_get_parm_info (int have_ellipsis, tree expr)
6c65299b 8288{
d764a8e6
IS
8289 tree parm_info = objc_parmlist;
8290 struct c_arg_info *arg_info;
8291 /* The C front-end requires an elaborate song and dance at
8292 this point. */
8293 push_scope ();
8294 declare_parm_level ();
8295 while (parm_info)
6c65299b 8296 {
d764a8e6 8297 tree next = DECL_CHAIN (parm_info);
6c65299b 8298
d764a8e6
IS
8299 DECL_CHAIN (parm_info) = NULL_TREE;
8300 parm_info = pushdecl (parm_info);
8301 finish_decl (parm_info, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
8302 parm_info = next;
6c65299b 8303 }
a04a722b 8304 arg_info = get_parm_info (have_ellipsis, expr);
d764a8e6
IS
8305 pop_scope ();
8306 objc_parmlist = NULL_TREE;
8307 return arg_info;
6c65299b 8308}
d764a8e6 8309#endif
6c65299b 8310
d764a8e6
IS
8311/* Synthesize the formal parameters 'id self' and 'SEL _cmd' needed for ObjC
8312 method definitions. In the case of instance methods, we can be more
8313 specific as to the type of 'self'. */
6c65299b 8314
d764a8e6
IS
8315static void
8316synth_self_and_ucmd_args (void)
6c65299b 8317{
d764a8e6 8318 tree self_type;
6c65299b 8319
d764a8e6
IS
8320 if (objc_method_context
8321 && TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL)
8322 self_type = objc_instance_type;
6c65299b 8323 else
d764a8e6
IS
8324 /* Really a `struct objc_class *'. However, we allow people to
8325 assign to self, which changes its type midstream. */
8326 self_type = objc_object_type;
6c65299b 8327
d764a8e6
IS
8328 /* id self; */
8329 objc_push_parm (build_decl (input_location,
8330 PARM_DECL, self_id, self_type));
ece4ce85 8331
d764a8e6
IS
8332 /* SEL _cmd; */
8333 objc_push_parm (build_decl (input_location,
8334 PARM_DECL, ucmd_id, objc_selector_type));
6c65299b
RS
8335}
8336
d764a8e6
IS
8337/* Transform an Objective-C method definition into a static C function
8338 definition, synthesizing the first two arguments, "self" and "_cmd",
a04a722b
JM
8339 in the process. EXPR is NULL or an expression that needs to be
8340 evaluated for the side effects of array size expressions in the
8341 parameters. */
64ee9490 8342
d764a8e6 8343static void
a04a722b 8344start_method_def (tree method, tree expr)
6c65299b 8345{
d764a8e6
IS
8346 tree parmlist;
8347#ifdef OBJCPLUS
8348 tree parm_info;
8349#else
8350 struct c_arg_info *parm_info;
8351#endif
8352 int have_ellipsis = 0;
6c65299b 8353
d764a8e6
IS
8354 /* If we are defining a "dealloc" method in a non-root class, we
8355 will need to check if a [super dealloc] is missing, and warn if
8356 it is. */
8357 if(CLASS_SUPER_NAME (objc_implementation_context)
8358 && !strcmp ("dealloc", IDENTIFIER_POINTER (METHOD_SEL_NAME (method))))
8359 should_call_super_dealloc = 1;
8360 else
8361 should_call_super_dealloc = 0;
264fa2db 8362
d764a8e6
IS
8363 /* Required to implement _msgSuper. */
8364 objc_method_context = method;
8365 UOBJC_SUPER_decl = NULL_TREE;
6c65299b 8366
d764a8e6
IS
8367 /* Generate prototype declarations for arguments..."new-style". */
8368 synth_self_and_ucmd_args ();
e31c7eec 8369
d764a8e6
IS
8370 /* Generate argument declarations if a keyword_decl. */
8371 parmlist = METHOD_SEL_ARGS (method);
8372 while (parmlist)
8373 {
8374 /* parmlist is a KEYWORD_DECL. */
8375 tree type = TREE_VALUE (TREE_TYPE (parmlist));
8376 tree parm;
264fa2db 8377
d764a8e6
IS
8378 parm = build_decl (input_location,
8379 PARM_DECL, KEYWORD_ARG_NAME (parmlist), type);
8380 decl_attributes (&parm, DECL_ATTRIBUTES (parmlist), 0);
8381 objc_push_parm (parm);
8382 parmlist = DECL_CHAIN (parmlist);
8383 }
6c65299b 8384
d764a8e6
IS
8385 if (METHOD_ADD_ARGS (method))
8386 {
8387 tree akey;
8388
8389 for (akey = TREE_CHAIN (METHOD_ADD_ARGS (method));
8390 akey; akey = TREE_CHAIN (akey))
e31c7eec 8391 {
d764a8e6 8392 objc_push_parm (TREE_VALUE (akey));
e31c7eec
TW
8393 }
8394
d764a8e6
IS
8395 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
8396 have_ellipsis = 1;
6c65299b 8397 }
6c65299b 8398
a04a722b 8399 parm_info = objc_get_parm_info (have_ellipsis, expr);
6c65299b 8400
d764a8e6
IS
8401 really_start_method (objc_method_context, parm_info);
8402}
96e5d745 8403
d764a8e6
IS
8404/* Return 1 if TYPE1 is equivalent to TYPE2 for purposes of method
8405 overloading. */
8406static int
8407objc_types_are_equivalent (tree type1, tree type2)
81ff723b 8408{
d764a8e6
IS
8409 if (type1 == type2)
8410 return 1;
81ff723b 8411
d764a8e6
IS
8412 /* Strip away indirections. */
8413 while ((TREE_CODE (type1) == ARRAY_TYPE || TREE_CODE (type1) == POINTER_TYPE)
8414 && (TREE_CODE (type1) == TREE_CODE (type2)))
8415 type1 = TREE_TYPE (type1), type2 = TREE_TYPE (type2);
8416 if (TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
8417 return 0;
81ff723b 8418
d764a8e6
IS
8419 /* Compare the protocol lists. */
8420 type1 = (TYPE_HAS_OBJC_INFO (type1)
8421 ? TYPE_OBJC_PROTOCOL_LIST (type1)
8422 : NULL_TREE);
8423 type2 = (TYPE_HAS_OBJC_INFO (type2)
8424 ? TYPE_OBJC_PROTOCOL_LIST (type2)
8425 : NULL_TREE);
223706ad 8426
d764a8e6
IS
8427 /* If there are no protocols (most common case), the types are
8428 identical. */
8429 if (type1 == NULL_TREE && type2 == NULL_TREE)
8430 return 1;
944fb799 8431
d764a8e6
IS
8432 /* If one has protocols, and the other one hasn't, they are not
8433 identical. */
8434 if ((type1 == NULL_TREE && type2 != NULL_TREE)
8435 || (type1 != NULL_TREE && type2 == NULL_TREE))
8436 return 0;
223706ad 8437 else
223706ad 8438 {
d764a8e6
IS
8439 /* Else, both have protocols, and we need to do the full
8440 comparison. It is possible that either type1 or type2
8441 contain some duplicate protocols in the list, so we can't
8442 even just compare list_length as a first check. */
8443 tree t;
223706ad 8444
d764a8e6
IS
8445 for (t = type2; t; t = TREE_CHAIN (t))
8446 if (!lookup_protocol_in_reflist (type1, TREE_VALUE (t)))
8447 return 0;
944fb799 8448
d764a8e6
IS
8449 for (t = type1; t; t = TREE_CHAIN (t))
8450 if (!lookup_protocol_in_reflist (type2, TREE_VALUE (t)))
8451 return 0;
944fb799 8452
d764a8e6 8453 return 1;
223706ad 8454 }
d764a8e6 8455}
223706ad 8456
d764a8e6 8457/* Return 1 if TYPE1 has the same size and alignment as TYPE2. */
6c65299b 8458
d764a8e6
IS
8459static int
8460objc_types_share_size_and_alignment (tree type1, tree type2)
8461{
8462 return (simple_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
8463 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2));
8464}
81ff723b 8465
d764a8e6
IS
8466/* Return 1 if PROTO1 is equivalent to PROTO2
8467 for purposes of method overloading. Ordinarily, the type signatures
8468 should match up exactly, unless STRICT is zero, in which case we
8469 shall allow differences in which the size and alignment of a type
8470 is the same. */
81ff723b 8471
d764a8e6
IS
8472static int
8473comp_proto_with_proto (tree proto1, tree proto2, int strict)
8474{
dc2dc512
NP
8475 tree type1, type2;
8476
d764a8e6
IS
8477 /* The following test is needed in case there are hashing
8478 collisions. */
8479 if (METHOD_SEL_NAME (proto1) != METHOD_SEL_NAME (proto2))
8480 return 0;
81ff723b 8481
d764a8e6
IS
8482 /* Compare return types. */
8483 type1 = TREE_VALUE (TREE_TYPE (proto1));
8484 type2 = TREE_VALUE (TREE_TYPE (proto2));
38b9c8c3 8485
d764a8e6
IS
8486 if (!objc_types_are_equivalent (type1, type2)
8487 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8488 return 0;
8489
8490 /* Compare argument types. */
38b9c8c3 8491
dc2dc512
NP
8492 /* The first argument (objc_object_type) is always the same, no need
8493 to compare. */
8494
8495 /* The second argument (objc_selector_type) is always the same, no
8496 need to compare. */
8497
8498 /* Compare the other arguments. */
8499 {
8500 tree arg1, arg2;
8501
8502 /* Compare METHOD_SEL_ARGS. */
8503 for (arg1 = METHOD_SEL_ARGS (proto1), arg2 = METHOD_SEL_ARGS (proto2);
8504 arg1 && arg2;
8505 arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
8506 {
8507 type1 = TREE_VALUE (TREE_TYPE (arg1));
8508 type2 = TREE_VALUE (TREE_TYPE (arg2));
944fb799 8509
dc2dc512
NP
8510 /* FIXME: Do we need to decay argument types to compare them ? */
8511 type1 = objc_decay_parm_type (type1);
8512 type2 = objc_decay_parm_type (type2);
944fb799 8513
dc2dc512
NP
8514 if (!objc_types_are_equivalent (type1, type2)
8515 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8516 return 0;
8517 }
944fb799 8518
dc2dc512
NP
8519 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8520 both NULL. */
8521 if (arg1 != arg2)
8522 return 0;
8523
8524 /* Compare METHOD_ADD_ARGS. */
8525 if ((METHOD_ADD_ARGS (proto1) && !METHOD_ADD_ARGS (proto2))
8526 || (METHOD_ADD_ARGS (proto2) && !METHOD_ADD_ARGS (proto1)))
8527 return 0;
8528
8529 if (METHOD_ADD_ARGS (proto1))
8530 {
8531 for (arg1 = TREE_CHAIN (METHOD_ADD_ARGS (proto1)), arg2 = TREE_CHAIN (METHOD_ADD_ARGS (proto2));
8532 arg1 && arg2;
8533 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
8534 {
8535 type1 = TREE_TYPE (TREE_VALUE (arg1));
8536 type2 = TREE_TYPE (TREE_VALUE (arg2));
944fb799 8537
dc2dc512
NP
8538 /* FIXME: Do we need to decay argument types to compare them ? */
8539 type1 = objc_decay_parm_type (type1);
8540 type2 = objc_decay_parm_type (type2);
944fb799 8541
dc2dc512
NP
8542 if (!objc_types_are_equivalent (type1, type2)
8543 && (strict || !objc_types_share_size_and_alignment (type1, type2)))
8544 return 0;
8545 }
8546 }
944fb799 8547
dc2dc512
NP
8548 /* The loop ends when arg1 or arg2 are NULL. Make sure they are
8549 both NULL. */
8550 if (arg1 != arg2)
8551 return 0;
8552
8553 /* Compare METHOD_ADD_ARGS_ELLIPSIS_P. */
8554 if (METHOD_ADD_ARGS_ELLIPSIS_P (proto1) != METHOD_ADD_ARGS_ELLIPSIS_P (proto2))
8555 return 0;
8556 }
8557
8558 /* Success. */
8559 return 1;
6c65299b
RS
8560}
8561
d764a8e6
IS
8562/* This routine returns true if TYPE is a valid objc object type,
8563 suitable for messaging; false otherwise. If 'accept_class' is
8564 'true', then a Class object is considered valid for messaging and
8565 'true' is returned if 'type' refers to a Class. If 'accept_class'
8566 is 'false', then a Class object is not considered valid for
8567 messaging and 'false' is returned in that case. */
8568
8569static bool
8570objc_type_valid_for_messaging (tree type, bool accept_classes)
6c65299b 8571{
d764a8e6
IS
8572 if (!POINTER_TYPE_P (type))
8573 return false;
51900510 8574
d764a8e6
IS
8575 /* Remove the pointer indirection; don't remove more than one
8576 otherwise we'd consider "NSObject **" a valid type for messaging,
8577 which it isn't. */
8578 type = TREE_TYPE (type);
6c65299b 8579
d764a8e6
IS
8580 if (TREE_CODE (type) != RECORD_TYPE)
8581 return false;
e31c7eec 8582
d764a8e6
IS
8583 if (objc_is_object_id (type))
8584 return true;
e31c7eec 8585
d764a8e6
IS
8586 if (objc_is_class_id (type))
8587 return accept_classes;
6c65299b 8588
d764a8e6
IS
8589 if (TYPE_HAS_OBJC_INFO (type))
8590 return true;
6c65299b 8591
d764a8e6
IS
8592 return false;
8593}
264fa2db 8594
d764a8e6
IS
8595void
8596objc_start_function (tree name, tree type, tree attrs,
8597#ifdef OBJCPLUS
8598 tree params
8599#else
8600 struct c_arg_info *params
8601#endif
8602 )
8603{
8604 tree fndecl = build_decl (input_location,
8605 FUNCTION_DECL, name, type);
6c65299b 8606
d764a8e6
IS
8607#ifdef OBJCPLUS
8608 DECL_ARGUMENTS (fndecl) = params;
8609 DECL_INITIAL (fndecl) = error_mark_node;
8610 DECL_EXTERNAL (fndecl) = 0;
8611 TREE_STATIC (fndecl) = 1;
8612 retrofit_lang_decl (fndecl);
8613 cplus_decl_attributes (&fndecl, attrs, 0);
8614 start_preparsed_function (fndecl, attrs, /*flags=*/SF_DEFAULT);
8615#else
8616 current_function_returns_value = 0; /* Assume, until we see it does. */
8617 current_function_returns_null = 0;
8618 decl_attributes (&fndecl, attrs, 0);
8619 announce_function (fndecl);
8620 DECL_INITIAL (fndecl) = error_mark_node;
8621 DECL_EXTERNAL (fndecl) = 0;
8622 TREE_STATIC (fndecl) = 1;
8623 current_function_decl = pushdecl (fndecl);
8624 push_scope ();
8625 declare_parm_level ();
8626 DECL_RESULT (current_function_decl)
8627 = build_decl (input_location,
8628 RESULT_DECL, NULL_TREE,
8629 TREE_TYPE (TREE_TYPE (current_function_decl)));
8630 DECL_ARTIFICIAL (DECL_RESULT (current_function_decl)) = 1;
8631 DECL_IGNORED_P (DECL_RESULT (current_function_decl)) = 1;
8632 start_fname_decls ();
8633 store_parm_decls_from (params);
8634#endif
6408ef33 8635
d764a8e6 8636 TREE_USED (current_function_decl) = 1;
6c65299b 8637}
8f07a2aa 8638
d764a8e6
IS
8639/* - Generate an identifier for the function. the format is "_n_cls",
8640 where 1 <= n <= nMethods, and cls is the name the implementation we
8641 are processing.
8642 - Install the return type from the method declaration.
8643 - If we have a prototype, check for type consistency. */
8644
8645static void
8646really_start_method (tree method,
8647#ifdef OBJCPLUS
8648 tree parmlist
8649#else
8650 struct c_arg_info *parmlist
8651#endif
8652 )
6c65299b 8653{
d764a8e6
IS
8654 tree ret_type, meth_type;
8655 tree method_id;
8656 const char *sel_name, *class_name, *cat_name;
8657 char *buf;
6c65299b 8658
d764a8e6
IS
8659 /* Synth the storage class & assemble the return type. */
8660 ret_type = TREE_VALUE (TREE_TYPE (method));
e31c7eec 8661
d764a8e6
IS
8662 sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
8663 class_name = IDENTIFIER_POINTER (CLASS_NAME (objc_implementation_context));
8664 cat_name = ((TREE_CODE (objc_implementation_context)
8665 == CLASS_IMPLEMENTATION_TYPE)
8666 ? NULL
8667 : IDENTIFIER_POINTER (CLASS_SUPER_NAME (objc_implementation_context)));
8668 method_slot++;
e31c7eec 8669
d764a8e6
IS
8670 /* Make sure this is big enough for any plausible method label. */
8671 buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
8672 + (cat_name ? strlen (cat_name) : 0));
6c65299b 8673
d764a8e6
IS
8674 OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
8675 class_name, cat_name, sel_name, method_slot);
8676
8677 method_id = get_identifier (buf);
d11dd684
ZL
8678
8679#ifdef OBJCPLUS
d764a8e6
IS
8680 /* Objective-C methods cannot be overloaded, so we don't need
8681 the type encoding appended. It looks bad anyway... */
8682 push_lang_context (lang_name_c);
d11dd684
ZL
8683#endif
8684
6174da1b 8685 meth_type = build_function_type_for_method (ret_type, method, METHOD_DEF, 0);
d764a8e6 8686 objc_start_function (method_id, meth_type, NULL_TREE, parmlist);
b2072208 8687
d764a8e6
IS
8688 /* Set self_decl from the first argument. */
8689 self_decl = DECL_ARGUMENTS (current_function_decl);
6c65299b 8690
d764a8e6
IS
8691 /* Suppress unused warnings. */
8692 TREE_USED (self_decl) = 1;
8693 DECL_READ_P (self_decl) = 1;
8694 TREE_USED (DECL_CHAIN (self_decl)) = 1;
8695 DECL_READ_P (DECL_CHAIN (self_decl)) = 1;
8696#ifdef OBJCPLUS
8697 pop_lang_context ();
8698#endif
38b9c8c3 8699
d764a8e6 8700 METHOD_DEFINITION (method) = current_function_decl;
6c65299b 8701
d764a8e6 8702 /* Check consistency...start_function, pushdecl, duplicate_decls. */
6c65299b 8703
d764a8e6
IS
8704 if (implementation_template != objc_implementation_context)
8705 {
8706 tree proto
8707 = lookup_method_static (implementation_template,
8708 METHOD_SEL_NAME (method),
8709 ((TREE_CODE (method) == CLASS_METHOD_DECL)
8710 | OBJC_LOOKUP_NO_SUPER));
61c627ed 8711
d764a8e6
IS
8712 if (proto)
8713 {
8714 if (!comp_proto_with_proto (method, proto, 1))
8715 {
8716 bool type = TREE_CODE (method) == INSTANCE_METHOD_DECL;
d11dd684 8717
d764a8e6
IS
8718 warning_at (DECL_SOURCE_LOCATION (method), 0,
8719 "conflicting types for %<%c%s%>",
8720 (type ? '-' : '+'),
8721 identifier_to_locale (gen_method_decl (method)));
8722 inform (DECL_SOURCE_LOCATION (proto),
8723 "previous declaration of %<%c%s%>",
8724 (type ? '-' : '+'),
8725 identifier_to_locale (gen_method_decl (proto)));
8726 }
8727 else
8728 {
8729 /* If the method in the @interface was deprecated, mark
8730 the implemented method as deprecated too. It should
8731 never be used for messaging (when the deprecation
8732 warnings are produced), but just in case. */
8733 if (TREE_DEPRECATED (proto))
8734 TREE_DEPRECATED (method) = 1;
d11dd684 8735
d764a8e6
IS
8736 /* If the method in the @interface was marked as
8737 'noreturn', mark the function implementing the method
8738 as 'noreturn' too. */
8739 TREE_THIS_VOLATILE (current_function_decl) = TREE_THIS_VOLATILE (proto);
8740 }
8741 }
8742 else
8743 {
8744 /* We have a method @implementation even though we did not
8745 see a corresponding @interface declaration (which is allowed
8746 by Objective-C rules). Go ahead and place the method in
8747 the @interface anyway, so that message dispatch lookups
8748 will see it. */
8749 tree interface = implementation_template;
e31c7eec 8750
d764a8e6
IS
8751 if (TREE_CODE (objc_implementation_context)
8752 == CATEGORY_IMPLEMENTATION_TYPE)
8753 interface = lookup_category
8754 (interface,
8755 CLASS_SUPER_NAME (objc_implementation_context));
64ee9490 8756
d764a8e6
IS
8757 if (interface)
8758 objc_add_method (interface, copy_node (method),
944fb799 8759 TREE_CODE (method) == CLASS_METHOD_DECL,
d764a8e6 8760 /* is_optional= */ false);
6c65299b 8761 }
6c65299b 8762 }
6c65299b 8763}
4ca5d2a7 8764
d764a8e6 8765static void *UOBJC_SUPER_scope = 0;
6c65299b 8766
d764a8e6
IS
8767/* _n_Method (id self, SEL sel, ...)
8768 {
8769 struct objc_super _S;
8770 _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
8771 } */
8772
8773static tree
8774get_super_receiver (void)
6c65299b 8775{
d764a8e6
IS
8776 if (objc_method_context)
8777 {
8778 tree super_expr, super_expr_list, class_expr;
8779 bool inst_meth;
8780 if (!UOBJC_SUPER_decl)
8781 {
8782 UOBJC_SUPER_decl = build_decl (input_location,
8783 VAR_DECL, get_identifier (TAG_SUPER),
8784 objc_super_template);
8785 /* This prevents `unused variable' warnings when compiling with -Wall. */
8786 TREE_USED (UOBJC_SUPER_decl) = 1;
8787 DECL_READ_P (UOBJC_SUPER_decl) = 1;
8788 lang_hooks.decls.pushdecl (UOBJC_SUPER_decl);
8789 finish_decl (UOBJC_SUPER_decl, input_location, NULL_TREE, NULL_TREE,
944fb799 8790 NULL_TREE);
d764a8e6
IS
8791 UOBJC_SUPER_scope = objc_get_current_scope ();
8792 }
4ca5d2a7 8793
d764a8e6
IS
8794 /* Set receiver to self. */
8795 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, self_id);
8796 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
8797 NOP_EXPR, input_location, self_decl,
8798 NULL_TREE);
8799 super_expr_list = super_expr;
6c65299b 8800
d764a8e6
IS
8801 /* Set class to begin searching. */
8802 /* Get the ident for the superclass class field & build a ref to it.
8803 ??? maybe we should just name the field the same for all runtimes. */
8804 super_expr = (*runtime.super_superclassfield_ident) ();
8805 super_expr = objc_build_component_ref (UOBJC_SUPER_decl, super_expr);
46a88c12 8806
d764a8e6
IS
8807 gcc_assert (imp_list->imp_context == objc_implementation_context
8808 && imp_list->imp_template == implementation_template);
8809 inst_meth = (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL);
8810
8811 if (TREE_CODE (objc_implementation_context) == CLASS_IMPLEMENTATION_TYPE)
944fb799 8812 class_expr = (*runtime.get_class_super_ref) (input_location,
d764a8e6
IS
8813 imp_list, inst_meth);
8814 else
8815 /* We have a category. */
6c65299b 8816 {
d764a8e6
IS
8817 tree super_name = CLASS_SUPER_NAME (imp_list->imp_template);
8818 tree super_class;
8819
8820 /* Barf if super used in a category of a root object. */
8821 if (!super_name)
4ca5d2a7 8822 {
d764a8e6
IS
8823 error ("no super class declared in interface for %qE",
8824 CLASS_NAME (imp_list->imp_template));
8825 return error_mark_node;
8826 }
4ca5d2a7 8827
944fb799 8828 super_class = (*runtime.get_category_super_ref) (input_location,
d764a8e6 8829 imp_list, inst_meth);
944fb799 8830 class_expr = build_c_cast (input_location,
d764a8e6
IS
8831 TREE_TYPE (super_expr), super_class);
8832 }
4ca5d2a7 8833
944fb799 8834 super_expr = build_modify_expr (input_location, super_expr, NULL_TREE,
d764a8e6
IS
8835 NOP_EXPR,
8836 input_location, class_expr, NULL_TREE);
4ca5d2a7 8837
944fb799 8838 super_expr_list = build_compound_expr (input_location,
d764a8e6 8839 super_expr_list, super_expr);
4ca5d2a7 8840
944fb799 8841 super_expr = build_unary_op (input_location,
d764a8e6
IS
8842 ADDR_EXPR, UOBJC_SUPER_decl, 0);
8843 super_expr_list = build_compound_expr (input_location,
8844 super_expr_list, super_expr);
4ca5d2a7 8845
d764a8e6
IS
8846 return super_expr_list;
8847 }
8848 else
8849 {
8850 error ("[super ...] must appear in a method context");
8851 return error_mark_node;
6c65299b 8852 }
e31c7eec
TW
8853}
8854
d764a8e6
IS
8855/* When exiting a scope, sever links to a 'super' declaration (if any)
8856 therein contained. */
f2e6e530 8857
d764a8e6
IS
8858void
8859objc_clear_super_receiver (void)
e31c7eec 8860{
d764a8e6 8861 if (objc_method_context
d4c433f9
NP
8862 && UOBJC_SUPER_scope == objc_get_current_scope ())
8863 {
8864 UOBJC_SUPER_decl = 0;
8865 UOBJC_SUPER_scope = 0;
8866 }
d764a8e6 8867}
51900510 8868
d764a8e6
IS
8869void
8870objc_finish_method_definition (tree fndecl)
8871{
8872 /* We cannot validly inline ObjC methods, at least not without a language
8873 extension to declare that a method need not be dynamically
8874 dispatched, so suppress all thoughts of doing so. */
8875 DECL_UNINLINABLE (fndecl) = 1;
51900510 8876
d764a8e6
IS
8877#ifndef OBJCPLUS
8878 /* The C++ front-end will have called finish_function() for us. */
8879 finish_function ();
8880#endif
8881
8882 METHOD_ENCODING (objc_method_context)
8883 = encode_method_prototype (objc_method_context);
8884
8885 /* Required to implement _msgSuper. This must be done AFTER finish_function,
8886 since the optimizer may find "may be used before set" errors. */
8887 objc_method_context = NULL_TREE;
8888
8889 if (should_call_super_dealloc)
8890 warning (0, "method possibly missing a [super dealloc] call");
e31c7eec
TW
8891}
8892
d764a8e6
IS
8893/* Given a tree DECL node, produce a printable description of it in the given
8894 buffer, overwriting the buffer. */
143dc646 8895
d764a8e6
IS
8896static char *
8897gen_declaration (tree decl)
143dc646 8898{
d764a8e6 8899 errbuf[0] = '\0';
143dc646 8900
d764a8e6 8901 if (DECL_P (decl))
143dc646 8902 {
d764a8e6 8903 gen_type_name_0 (TREE_TYPE (decl));
46a88c12 8904
d764a8e6 8905 if (DECL_NAME (decl))
143dc646 8906 {
d764a8e6
IS
8907 if (!POINTER_TYPE_P (TREE_TYPE (decl)))
8908 strcat (errbuf, " ");
143dc646 8909
d764a8e6 8910 strcat (errbuf, IDENTIFIER_POINTER (DECL_NAME (decl)));
143dc646
KKT
8911 }
8912
d764a8e6
IS
8913 if (DECL_INITIAL (decl)
8914 && TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST)
8915 sprintf (errbuf + strlen (errbuf), ": " HOST_WIDE_INT_PRINT_DEC,
8916 TREE_INT_CST_LOW (DECL_INITIAL (decl)));
143dc646 8917 }
d764a8e6
IS
8918
8919 return errbuf;
143dc646
KKT
8920}
8921
d764a8e6
IS
8922/* Given a tree TYPE node, produce a printable description of it in the given
8923 buffer, overwriting the buffer. */
011d50d9 8924
d764a8e6
IS
8925static char *
8926gen_type_name_0 (tree type)
e31c7eec 8927{
d764a8e6
IS
8928 tree orig = type, proto;
8929
8930 if (TYPE_P (type) && TYPE_NAME (type))
8931 type = TYPE_NAME (type);
8932 else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
e31c7eec 8933 {
d764a8e6 8934 tree inner = TREE_TYPE (type);
e31c7eec 8935
d764a8e6
IS
8936 while (TREE_CODE (inner) == ARRAY_TYPE)
8937 inner = TREE_TYPE (inner);
e31c7eec 8938
d764a8e6 8939 gen_type_name_0 (inner);
011d50d9 8940
d764a8e6
IS
8941 if (!POINTER_TYPE_P (inner))
8942 strcat (errbuf, " ");
ad074d60 8943
d764a8e6
IS
8944 if (POINTER_TYPE_P (type))
8945 strcat (errbuf, "*");
8946 else
8947 while (type != inner)
8948 {
8949 strcat (errbuf, "[");
f2e6e530 8950
d764a8e6
IS
8951 if (TYPE_DOMAIN (type))
8952 {
8953 char sz[20];
011d50d9 8954
d764a8e6
IS
8955 sprintf (sz, HOST_WIDE_INT_PRINT_DEC,
8956 (TREE_INT_CST_LOW
8957 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1));
8958 strcat (errbuf, sz);
8959 }
011d50d9 8960
d764a8e6
IS
8961 strcat (errbuf, "]");
8962 type = TREE_TYPE (type);
8963 }
f2e6e530 8964
d764a8e6 8965 goto exit_function;
f2e6e530 8966 }
6c65299b 8967
d764a8e6
IS
8968 if (TREE_CODE (type) == TYPE_DECL && DECL_NAME (type))
8969 type = DECL_NAME (type);
264fa2db 8970
d764a8e6 8971 strcat (errbuf, TREE_CODE (type) == IDENTIFIER_NODE
944fb799 8972 ? IDENTIFIER_POINTER (type)
d764a8e6
IS
8973 : "");
8974
8975 /* For 'id' and 'Class', adopted protocols are stored in the pointee. */
8976 if (objc_is_id (orig))
8977 orig = TREE_TYPE (orig);
8978
8979 proto = TYPE_HAS_OBJC_INFO (orig) ? TYPE_OBJC_PROTOCOL_LIST (orig) : NULL_TREE;
8980
8981 if (proto)
e804281e 8982 {
d764a8e6
IS
8983 strcat (errbuf, " <");
8984
8985 while (proto) {
8986 strcat (errbuf,
8987 IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (proto))));
8988 proto = TREE_CHAIN (proto);
8989 strcat (errbuf, proto ? ", " : ">");
8990 }
e804281e
SS
8991 }
8992
d764a8e6
IS
8993 exit_function:
8994 return errbuf;
8995}
8996
8997static char *
8998gen_type_name (tree type)
8999{
9000 errbuf[0] = '\0';
9001
9002 return gen_type_name_0 (type);
9003}
9004
9005/* Given a method tree, put a printable description into the given
9006 buffer (overwriting) and return a pointer to the buffer. */
9007
9008static char *
9009gen_method_decl (tree method)
9010{
9011 tree chain;
9012
9013 strcpy (errbuf, "("); /* NB: Do _not_ call strcat() here. */
9014 gen_type_name_0 (TREE_VALUE (TREE_TYPE (method)));
9015 strcat (errbuf, ")");
9016 chain = METHOD_SEL_ARGS (method);
9017
9018 if (chain)
ec3e9f82 9019 {
d764a8e6
IS
9020 /* We have a chain of keyword_decls. */
9021 do
9022 {
9023 if (KEYWORD_KEY_NAME (chain))
9024 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
9025
9026 strcat (errbuf, ":(");
9027 gen_type_name_0 (TREE_VALUE (TREE_TYPE (chain)));
9028 strcat (errbuf, ")");
9029
9030 strcat (errbuf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
9031 if ((chain = DECL_CHAIN (chain)))
9032 strcat (errbuf, " ");
9033 }
9034 while (chain);
9035
9036 if (METHOD_ADD_ARGS (method))
9037 {
9038 chain = TREE_CHAIN (METHOD_ADD_ARGS (method));
9039
9040 /* Know we have a chain of parm_decls. */
9041 while (chain)
9042 {
9043 strcat (errbuf, ", ");
9044 gen_type_name_0 (TREE_TYPE (TREE_VALUE (chain)));
9045 chain = TREE_CHAIN (chain);
9046 }
9047
9048 if (METHOD_ADD_ARGS_ELLIPSIS_P (method))
9049 strcat (errbuf, ", ...");
9050 }
ec3e9f82 9051 }
6c65299b 9052
d764a8e6
IS
9053 else
9054 /* We have a unary selector. */
9055 strcat (errbuf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
9056
9057 return errbuf;
9058}
9059\f
9060/* Debug info. */
9061
9062
9063/* Dump an @interface declaration of the supplied class CHAIN to the
9064 supplied file FP. Used to implement the -gen-decls option (which
9065 prints out an @interface declaration of all classes compiled in
9066 this run); potentially useful for debugging the compiler too. */
9067void
9068dump_interface (FILE *fp, tree chain)
9069{
9070 /* FIXME: A heap overflow here whenever a method (or ivar)
9071 declaration is so long that it doesn't fit in the buffer. The
9072 code and all the related functions should be rewritten to avoid
9073 using fixed size buffers. */
9074 const char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
9075 tree ivar_decls = CLASS_RAW_IVARS (chain);
9076 tree nst_methods = CLASS_NST_METHODS (chain);
9077 tree cls_methods = CLASS_CLS_METHODS (chain);
9078
9079 fprintf (fp, "\n@interface %s", my_name);
9080
9081 /* CLASS_SUPER_NAME is used to store the superclass name for
9082 classes, and the category name for categories. */
9083 if (CLASS_SUPER_NAME (chain))
6408ef33 9084 {
d764a8e6 9085 const char *name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
38b9c8c3 9086
d764a8e6 9087 switch (TREE_CODE (chain))
38b9c8c3 9088 {
d764a8e6
IS
9089 case CATEGORY_IMPLEMENTATION_TYPE:
9090 case CATEGORY_INTERFACE_TYPE:
9091 fprintf (fp, " (%s)\n", name);
9092 break;
9093 default:
9094 fprintf (fp, " : %s\n", name);
9095 break;
38b9c8c3 9096 }
d764a8e6
IS
9097 }
9098 else
9099 fprintf (fp, "\n");
9100
9101 /* FIXME - the following doesn't seem to work at the moment. */
9102 if (ivar_decls)
9103 {
9104 fprintf (fp, "{\n");
9105 do
d4e72c58 9106 {
d764a8e6
IS
9107 fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls));
9108 ivar_decls = TREE_CHAIN (ivar_decls);
d4e72c58 9109 }
d764a8e6
IS
9110 while (ivar_decls);
9111 fprintf (fp, "}\n");
6408ef33
ZL
9112 }
9113
d764a8e6 9114 while (nst_methods)
ec3e9f82 9115 {
d764a8e6
IS
9116 fprintf (fp, "- %s;\n", gen_method_decl (nst_methods));
9117 nst_methods = TREE_CHAIN (nst_methods);
ec3e9f82 9118 }
6c65299b 9119
d764a8e6 9120 while (cls_methods)
e31c7eec 9121 {
d764a8e6
IS
9122 fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods));
9123 cls_methods = TREE_CHAIN (cls_methods);
e31c7eec
TW
9124 }
9125
d764a8e6
IS
9126 fprintf (fp, "@end\n");
9127}
e31c7eec 9128
d764a8e6
IS
9129#if 0
9130/* Produce the pretty printing for an Objective-C method. This is
9131 currently unused, but could be handy while reorganizing the pretty
9132 printing to be more robust. */
9133static const char *
9134objc_pretty_print_method (bool is_class_method,
9135 const char *class_name,
9136 const char *category_name,
9137 const char *selector)
9138{
9139 if (category_name)
9140 {
944fb799 9141 char *result = XNEWVEC (char, strlen (class_name) + strlen (category_name)
d764a8e6 9142 + strlen (selector) + 7);
6c65299b 9143
d764a8e6
IS
9144 if (is_class_method)
9145 sprintf (result, "+[%s(%s) %s]", class_name, category_name, selector);
9146 else
9147 sprintf (result, "-[%s(%s) %s]", class_name, category_name, selector);
6c65299b 9148
d764a8e6
IS
9149 return result;
9150 }
9151 else
9152 {
9153 char *result = XNEWVEC (char, strlen (class_name)
9154 + strlen (selector) + 5);
6c65299b 9155
d764a8e6
IS
9156 if (is_class_method)
9157 sprintf (result, "+[%s %s]", class_name, selector);
9158 else
9159 sprintf (result, "-[%s %s]", class_name, selector);
6c65299b 9160
944fb799 9161 return result;
d764a8e6
IS
9162 }
9163}
9164#endif
6c65299b 9165
d764a8e6
IS
9166/* Demangle function for Objective-C. Attempt to demangle the
9167 function name associated with a method (eg, going from
9168 "_i_NSObject__class" to "-[NSObject class]"); usually for the
9169 purpose of pretty printing or error messages. Return the demangled
9170 name, or NULL if the string is not an Objective-C mangled method
9171 name.
9172
9173 Because of how the mangling is done, any method that has a '_' in
9174 its original name is at risk of being demangled incorrectly. In
9175 some cases there are multiple valid ways to demangle a method name
9176 and there is no way we can decide.
9177
9178 TODO: objc_demangle() can't always get it right; the right way to
9179 get this correct for all method names would be to store the
9180 Objective-C method name somewhere in the function decl. Then,
9181 there is no demangling to do; we'd just pull the method name out of
9182 the decl. As an additional bonus, when printing error messages we
9183 could check for such a method name, and if we find it, we know the
9184 function is actually an Objective-C method and we could print error
9185 messages saying "In method '+[NSObject class]" instead of "In
9186 function '+[NSObject class]" as we do now. */
9187static const char *
9188objc_demangle (const char *mangled)
9189{
9190 char *demangled, *cp;
9191
9192 /* First of all, if the name is too short it can't be an Objective-C
9193 mangled method name. */
9194 if (mangled[0] == '\0' || mangled[1] == '\0' || mangled[2] == '\0')
9195 return NULL;
9196
9197 /* If the name looks like an already demangled one, return it
9198 unchanged. This should only happen on Darwin, where method names
9199 are mangled differently into a pretty-print form (such as
9200 '+[NSObject class]', see darwin.h). In that case, demangling is
9201 a no-op, but we need to return the demangled name if it was an
9202 ObjC one, and return NULL if not. We should be safe as no C/C++
9203 function can start with "-[" or "+[". */
9204 if ((mangled[0] == '-' || mangled[0] == '+')
9205 && (mangled[1] == '['))
9206 return mangled;
9207
9208 if (mangled[0] == '_' &&
9209 (mangled[1] == 'i' || mangled[1] == 'c') &&
9210 mangled[2] == '_')
9211 {
9212 cp = demangled = XNEWVEC (char, strlen(mangled) + 2);
9213 if (mangled[1] == 'i')
9214 *cp++ = '-'; /* for instance method */
9215 else
9216 *cp++ = '+'; /* for class method */
9217 *cp++ = '['; /* opening left brace */
9218 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
9219 while (*cp && *cp == '_')
9220 cp++; /* skip any initial underbars in class name */
9221 cp = strchr(cp, '_'); /* find first non-initial underbar */
9222 if (cp == NULL)
50ea5861 9223 {
d764a8e6
IS
9224 free(demangled); /* not mangled name */
9225 return NULL;
50ea5861 9226 }
d764a8e6 9227 if (cp[1] == '_') /* easy case: no category name */
143dc646 9228 {
d764a8e6
IS
9229 *cp++ = ' '; /* replace two '_' with one ' ' */
9230 strcpy(cp, mangled + (cp - demangled) + 2);
143dc646 9231 }
6c65299b 9232 else
d4e72c58 9233 {
d764a8e6
IS
9234 *cp++ = '('; /* less easy case: category name */
9235 cp = strchr(cp, '_');
9236 if (cp == 0)
d4e72c58 9237 {
d764a8e6
IS
9238 free(demangled); /* not mangled name */
9239 return NULL;
d4e72c58 9240 }
d764a8e6
IS
9241 *cp++ = ')';
9242 *cp++ = ' '; /* overwriting 1st char of method name... */
9243 strcpy(cp, mangled + (cp - demangled)); /* get it back */
d4e72c58 9244 }
d764a8e6
IS
9245 /* Now we have the method name. We need to generally replace
9246 '_' with ':' but trying to preserve '_' if it could only have
9247 been in the mangled string because it was already in the
9248 original name. In cases where it's ambiguous, we assume that
9249 any '_' originated from a ':'. */
51900510 9250
d764a8e6
IS
9251 /* Initial '_'s in method name can't have been generating by
9252 converting ':'s. Skip them. */
9253 while (*cp && *cp == '_')
9254 cp++;
d4e72c58 9255
d764a8e6
IS
9256 /* If the method name does not end with '_', then it has no
9257 arguments and there was no replacement of ':'s with '_'s
9258 during mangling. Check for that case, and skip any
9259 replacement if so. This at least guarantees that methods
9260 with no arguments are always demangled correctly (unless the
9261 original name ends with '_'). */
9262 if (*(mangled + strlen (mangled) - 1) != '_')
9263 {
9264 /* Skip to the end. */
9265 for (; *cp; cp++)
9266 ;
9267 }
9268 else
9269 {
9270 /* Replace remaining '_' with ':'. This may get it wrong if
9271 there were '_'s in the original name. In most cases it
9272 is impossible to disambiguate. */
9273 for (; *cp; cp++)
9274 if (*cp == '_')
944fb799 9275 *cp = ':';
d764a8e6
IS
9276 }
9277 *cp++ = ']'; /* closing right brace */
9278 *cp++ = 0; /* string terminator */
9279 return demangled;
9280 }
9281 else
9282 return NULL; /* not an objc mangled name */
9283}
ec3e9f82 9284
d764a8e6
IS
9285/* Try to pretty-print a decl. If the 'decl' is an Objective-C
9286 specific decl, return the printable name for it. If not, return
9287 NULL. */
9288const char *
9289objc_maybe_printable_name (tree decl, int v ATTRIBUTE_UNUSED)
9290{
9291 switch (TREE_CODE (decl))
9292 {
9293 case FUNCTION_DECL:
9294 return objc_demangle (IDENTIFIER_POINTER (DECL_NAME (decl)));
50ea5861 9295 break;
6c65299b 9296
d764a8e6
IS
9297 /* The following happens when we are printing a deprecation
9298 warning for a method. The warn_deprecation() will end up
9299 trying to print the decl for INSTANCE_METHOD_DECL or
9300 CLASS_METHOD_DECL. It would be nice to be able to print
9301 "-[NSObject autorelease] is deprecated", but to do that, we'd
9302 need to store the class and method name in the method decl,
9303 which we currently don't do. For now, just return the name
9304 of the method. We don't return NULL, because that may
9305 trigger further attempts to pretty-print the decl in C/C++,
9306 but they wouldn't know how to pretty-print it. */
9307 case INSTANCE_METHOD_DECL:
9308 case CLASS_METHOD_DECL:
9309 return IDENTIFIER_POINTER (DECL_NAME (decl));
9310 break;
9311 /* This happens when printing a deprecation warning for a
9312 property. We may want to consider some sort of pretty
9313 printing (eg, include the class name where it was declared
9314 ?). */
9315 case PROPERTY_DECL:
9316 return IDENTIFIER_POINTER (PROPERTY_NAME (decl));
50ea5861
NP
9317 break;
9318 default:
d764a8e6
IS
9319 return NULL;
9320 break;
6c65299b 9321 }
6c65299b
RS
9322}
9323
d764a8e6
IS
9324/* Return a printable name for 'decl'. This first tries
9325 objc_maybe_printable_name(), and if that fails, it returns the name
9326 in the decl. This is used as LANG_HOOKS_DECL_PRINTABLE_NAME for
9327 Objective-C; in Objective-C++, setting the hook is not enough
9328 because lots of C++ Front-End code calls cxx_printable_name,
9329 dump_decl and other C++ functions directly. So instead we have
9330 modified dump_decl to call objc_maybe_printable_name directly. */
9331const char *
9332objc_printable_name (tree decl, int v)
9333{
9334 const char *demangled_name = objc_maybe_printable_name (decl, v);
6c65299b 9335
d764a8e6
IS
9336 if (demangled_name != NULL)
9337 return demangled_name;
9338 else
9339 return IDENTIFIER_POINTER (DECL_NAME (decl));
9340}
6c65299b 9341
944fb799
MS
9342/* Routine is called to issue diagnostic when reference to a private
9343 ivar is made and no other variable with same name is found in
d764a8e6
IS
9344 current scope. */
9345bool
9346objc_diagnose_private_ivar (tree id)
9347{
9348 tree ivar;
9349 if (!objc_method_context)
9350 return false;
9351 ivar = is_ivar (objc_ivar_chain, id);
9352 if (ivar && is_private (ivar))
9353 {
944fb799 9354 error ("instance variable %qs is declared private",
d764a8e6
IS
9355 IDENTIFIER_POINTER (id));
9356 return true;
9357 }
9358 return false;
9359}
6c65299b 9360
d764a8e6
IS
9361/* Look up ID as an instance variable. OTHER contains the result of
9362 the C or C++ lookup, which we may want to use instead. */
9363/* To use properties inside an instance method, use self.property. */
9364tree
9365objc_lookup_ivar (tree other, tree id)
9366{
9367 tree ivar;
6c65299b 9368
d764a8e6
IS
9369 /* If we are not inside of an ObjC method, ivar lookup makes no sense. */
9370 if (!objc_method_context)
9371 return other;
6c65299b 9372
d764a8e6
IS
9373 if (!strcmp (IDENTIFIER_POINTER (id), "super"))
9374 /* We have a message to super. */
9375 return get_super_receiver ();
6c65299b 9376
d764a8e6
IS
9377 /* In a class method, look up an instance variable only as a last
9378 resort. */
9379 if (TREE_CODE (objc_method_context) == CLASS_METHOD_DECL
9380 && other && other != error_mark_node)
9381 return other;
9382
dcaaa5a0
DP
9383 /* Don't look up the ivar if the user has explicitly advised against
9384 it with -fno-local-ivars. */
9385
9386 if (!flag_local_ivars)
9387 return other;
9388
d764a8e6
IS
9389 /* Look up the ivar, but do not use it if it is not accessible. */
9390 ivar = is_ivar (objc_ivar_chain, id);
944fb799 9391
d764a8e6
IS
9392 if (!ivar || is_private (ivar))
9393 return other;
9394
9395 /* In an instance method, a local variable (or parameter) may hide the
9396 instance variable. */
9397 if (TREE_CODE (objc_method_context) == INSTANCE_METHOD_DECL
9398 && other && other != error_mark_node
6408ef33 9399#ifdef OBJCPLUS
d764a8e6
IS
9400 && CP_DECL_CONTEXT (other) != global_namespace)
9401#else
9402 && !DECL_FILE_SCOPE_P (other))
9403#endif
9404 {
dcaaa5a0
DP
9405 if (warn_shadow_ivar == 1 || (warn_shadow && warn_shadow_ivar != 0)) {
9406 warning (warn_shadow_ivar ? OPT_Wshadow_ivar : OPT_Wshadow,
9407 "local declaration of %qE hides instance variable", id);
9408 }
9409
d764a8e6 9410 return other;
6c65299b 9411 }
6c65299b 9412
d764a8e6
IS
9413 /* At this point, we are either in an instance method with no obscuring
9414 local definitions, or in a class method with no alternate definitions
9415 at all. */
9416 return build_ivar_reference (id);
668ea4b1
IS
9417}
9418
d764a8e6
IS
9419/* Possibly rewrite a function CALL into an OBJ_TYPE_REF expression. This
9420 needs to be done if we are calling a function through a cast. */
9421
9422tree
9423objc_rewrite_function_call (tree function, tree first_param)
8f07a2aa 9424{
d764a8e6
IS
9425 if (TREE_CODE (function) == NOP_EXPR
9426 && TREE_CODE (TREE_OPERAND (function, 0)) == ADDR_EXPR
9427 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (function, 0), 0))
9428 == FUNCTION_DECL)
9429 {
9430 function = build3 (OBJ_TYPE_REF, TREE_TYPE (function),
9431 TREE_OPERAND (function, 0),
9432 first_param, size_zero_node);
9433 }
8f07a2aa 9434
d764a8e6
IS
9435 return function;
9436}
8f07a2aa 9437
d764a8e6
IS
9438/* This is called to "gimplify" a PROPERTY_REF node. It builds the
9439 corresponding 'getter' function call. Note that we assume the
9440 PROPERTY_REF to be valid since we generated it while parsing. */
9441static void
9442objc_gimplify_property_ref (tree *expr_p)
9443{
9444 tree getter = PROPERTY_REF_GETTER_CALL (*expr_p);
9445 tree call_exp;
8f07a2aa 9446
d764a8e6 9447 if (getter == NULL_TREE)
8f07a2aa 9448 {
d764a8e6
IS
9449 tree property_decl = PROPERTY_REF_PROPERTY_DECL (*expr_p);
9450 /* This can happen if DECL_ARTIFICIAL (*expr_p), but
9451 should be impossible for real properties, which always
9452 have a getter. */
9453 error_at (EXPR_LOCATION (*expr_p), "no %qs getter found",
9454 IDENTIFIER_POINTER (PROPERTY_NAME (property_decl)));
9455 /* Try to recover from the error to prevent an ICE. We take
9456 zero and cast it to the type of the property. */
9457 *expr_p = convert (TREE_TYPE (property_decl),
9458 integer_zero_node);
9459 return;
8f07a2aa 9460 }
8f07a2aa 9461
d764a8e6
IS
9462 if (PROPERTY_REF_DEPRECATED_GETTER (*expr_p))
9463 {
9464 /* PROPERTY_REF_DEPRECATED_GETTER contains the method prototype
9465 that is deprecated. */
9466 warn_deprecated_use (PROPERTY_REF_DEPRECATED_GETTER (*expr_p),
9467 NULL_TREE);
8f07a2aa 9468 }
8f07a2aa 9469
d764a8e6
IS
9470 call_exp = getter;
9471#ifdef OBJCPLUS
9472 /* In C++, a getter which returns an aggregate value results in a
9473 target_expr which initializes a temporary to the call
9474 expression. */
9475 if (TREE_CODE (getter) == TARGET_EXPR)
8f07a2aa 9476 {
d764a8e6
IS
9477 gcc_assert (MAYBE_CLASS_TYPE_P (TREE_TYPE (getter)));
9478 gcc_assert (TREE_CODE (TREE_OPERAND (getter, 0)) == VAR_DECL);
9479 call_exp = TREE_OPERAND (getter, 1);
8f07a2aa 9480 }
d764a8e6
IS
9481#endif
9482 gcc_assert (TREE_CODE (call_exp) == CALL_EXPR);
944fb799 9483
d764a8e6 9484 *expr_p = call_exp;
8f07a2aa
NP
9485}
9486
d764a8e6
IS
9487/* This is called when "gimplifying" the trees. We need to gimplify
9488 the Objective-C/Objective-C++ specific trees, then hand over the
9489 process to C/C++. */
9490int
9491objc_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
668ea4b1 9492{
d764a8e6
IS
9493 enum tree_code code = TREE_CODE (*expr_p);
9494 switch (code)
8f07a2aa 9495 {
d764a8e6
IS
9496 /* Look for the special case of OBJC_TYPE_REF with the address
9497 of a function in OBJ_TYPE_REF_EXPR (presumably objc_msgSend
9498 or one of its cousins). */
9499 case OBJ_TYPE_REF:
9500 if (TREE_CODE (OBJ_TYPE_REF_EXPR (*expr_p)) == ADDR_EXPR
9501 && TREE_CODE (TREE_OPERAND (OBJ_TYPE_REF_EXPR (*expr_p), 0))
9502 == FUNCTION_DECL)
8f07a2aa 9503 {
d764a8e6 9504 enum gimplify_status r0, r1;
8f07a2aa 9505
d764a8e6
IS
9506 /* Postincrements in OBJ_TYPE_REF_OBJECT don't affect the
9507 value of the OBJ_TYPE_REF, so force them to be emitted
9508 during subexpression evaluation rather than after the
9509 OBJ_TYPE_REF. This permits objc_msgSend calls in
9510 Objective C to use direct rather than indirect calls when
9511 the object expression has a postincrement. */
9512 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, NULL,
9513 is_gimple_val, fb_rvalue);
9514 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
9515 is_gimple_val, fb_rvalue);
944fb799 9516
d764a8e6 9517 return MIN (r0, r1);
8f07a2aa
NP
9518 }
9519 break;
d764a8e6
IS
9520 case PROPERTY_REF:
9521 objc_gimplify_property_ref (expr_p);
9522 /* Do not return yet; let C/C++ gimplify the resulting expression. */
9523 break;
8f07a2aa 9524 default:
d764a8e6 9525 break;
8f07a2aa 9526 }
46a88c12 9527
668ea4b1 9528#ifdef OBJCPLUS
d764a8e6 9529 return (enum gimplify_status) cp_gimplify_expr (expr_p, pre_p, post_p);
668ea4b1 9530#else
d764a8e6 9531 return (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
668ea4b1 9532#endif
668ea4b1
IS
9533}
9534
d764a8e6
IS
9535/* --- FAST ENUMERATION --- */
9536/* Begin code generation for fast enumeration (foreach) ... */
668ea4b1 9537
d764a8e6 9538/* Defines
668ea4b1 9539
d764a8e6
IS
9540 struct __objcFastEnumerationState
9541 {
9542 unsigned long state;
9543 id *itemsPtr;
9544 unsigned long *mutationsPtr;
9545 unsigned long extra[5];
9546 };
668ea4b1 9547
d764a8e6 9548 Confusingly enough, NSFastEnumeration is then defined by libraries
944fb799 9549 to be the same structure.
d764a8e6 9550*/
668ea4b1 9551
d764a8e6
IS
9552static void
9553build_fast_enumeration_state_template (void)
9554{
9555 tree decls, *chain = NULL;
46a88c12 9556
d764a8e6 9557 /* { */
944fb799 9558 objc_fast_enumeration_state_template = objc_start_struct (get_identifier
d764a8e6 9559 (TAG_FAST_ENUMERATION_STATE));
668ea4b1 9560
d764a8e6
IS
9561 /* unsigned long state; */
9562 decls = add_field_decl (long_unsigned_type_node, "state", &chain);
46a88c12 9563
d764a8e6 9564 /* id *itemsPtr; */
944fb799 9565 add_field_decl (build_pointer_type (objc_object_type),
d764a8e6 9566 "itemsPtr", &chain);
46a88c12 9567
d764a8e6 9568 /* unsigned long *mutationsPtr; */
944fb799 9569 add_field_decl (build_pointer_type (long_unsigned_type_node),
d764a8e6 9570 "mutationsPtr", &chain);
8f07a2aa 9571
d764a8e6 9572 /* unsigned long extra[5]; */
944fb799 9573 add_field_decl (build_sized_array_type (long_unsigned_type_node, 5),
d764a8e6 9574 "extra", &chain);
8f07a2aa 9575
d764a8e6
IS
9576 /* } */
9577 objc_finish_struct (objc_fast_enumeration_state_template, decls);
9578}
8f07a2aa 9579
d764a8e6
IS
9580/*
9581 'objc_finish_foreach_loop()' generates the code for an Objective-C
9582 foreach loop. The 'location' argument is the location of the 'for'
9583 that starts the loop. The 'object_expression' is the expression of
9584 the 'object' that iterates; the 'collection_expression' is the
9585 expression of the collection that we iterate over (we need to make
9586 sure we evaluate this only once); the 'for_body' is the set of
9587 statements to be executed in each iteration; 'break_label' and
9588 'continue_label' are the break and continue labels which we need to
9589 emit since the <statements> may be jumping to 'break_label' (if they
9590 contain 'break') or to 'continue_label' (if they contain
9591 'continue').
8f07a2aa 9592
d764a8e6 9593 The syntax is
944fb799 9594
d764a8e6
IS
9595 for (<object expression> in <collection expression>)
9596 <statements>
8f07a2aa 9597
d764a8e6
IS
9598 which is compiled into the following blurb:
9599
9600 {
9601 id __objc_foreach_collection;
9602 __objc_fast_enumeration_state __objc_foreach_enum_state;
9603 unsigned long __objc_foreach_batchsize;
9604 id __objc_foreach_items[16];
9605 __objc_foreach_collection = <collection expression>;
9606 __objc_foreach_enum_state = { 0 };
9607 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
944fb799 9608
d764a8e6
IS
9609 if (__objc_foreach_batchsize == 0)
9610 <object expression> = nil;
9611 else
9612 {
9613 unsigned long __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr;
9614 next_batch:
8f07a2aa 9615 {
d764a8e6
IS
9616 unsigned long __objc_foreach_index;
9617 __objc_foreach_index = 0;
8f07a2aa 9618
d764a8e6
IS
9619 next_object:
9620 if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>);
9621 <object expression> = enumState.itemsPtr[__objc_foreach_index];
9622 <statements> [PS: inside <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label]
8f07a2aa 9623
d764a8e6
IS
9624 continue_label:
9625 __objc_foreach_index++;
9626 if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object;
9627 __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16];
9628 }
9629 if (__objc_foreach_batchsize != 0) goto next_batch;
9630 <object expression> = nil;
9631 break_label:
8f07a2aa 9632 }
d764a8e6 9633 }
8f07a2aa 9634
d764a8e6
IS
9635 'statements' may contain a 'continue' or 'break' instruction, which
9636 the user expects to 'continue' or 'break' the entire foreach loop.
9637 We are provided the labels that 'break' and 'continue' jump to, so
9638 we place them where we want them to jump to when they pick them.
944fb799 9639
d764a8e6
IS
9640 Optimization TODO: we could cache the IMP of
9641 countByEnumeratingWithState:objects:count:.
9642*/
8f07a2aa 9643
d764a8e6
IS
9644/* If you need to debug objc_finish_foreach_loop(), uncomment the following line. */
9645/* #define DEBUG_OBJC_FINISH_FOREACH_LOOP 1 */
8f07a2aa 9646
d764a8e6
IS
9647#ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
9648#include "tree-pretty-print.h"
668ea4b1 9649#endif
668ea4b1 9650
d764a8e6 9651void
944fb799 9652objc_finish_foreach_loop (location_t location, tree object_expression, tree collection_expression, tree for_body,
d764a8e6 9653 tree break_label, tree continue_label)
46a88c12 9654{
d764a8e6
IS
9655 /* A tree representing the __objcFastEnumerationState struct type,
9656 or NSFastEnumerationState struct, whatever we are using. */
9657 tree objc_fast_enumeration_state_type;
46a88c12 9658
d764a8e6
IS
9659 /* The trees representing the declarations of each of the local variables. */
9660 tree objc_foreach_collection_decl;
9661 tree objc_foreach_enum_state_decl;
9662 tree objc_foreach_items_decl;
9663 tree objc_foreach_batchsize_decl;
9664 tree objc_foreach_mutations_pointer_decl;
9665 tree objc_foreach_index_decl;
46a88c12 9666
d764a8e6
IS
9667 /* A tree representing the selector countByEnumeratingWithState:objects:count:. */
9668 tree selector_name;
46a88c12 9669
d764a8e6
IS
9670 /* A tree representing the local bind. */
9671 tree bind;
46a88c12 9672
d764a8e6
IS
9673 /* A tree representing the external 'if (__objc_foreach_batchsize)' */
9674 tree first_if;
46a88c12 9675
d764a8e6
IS
9676 /* A tree representing the 'else' part of 'first_if' */
9677 tree first_else;
46a88c12 9678
d764a8e6
IS
9679 /* A tree representing the 'next_batch' label. */
9680 tree next_batch_label_decl;
46a88c12 9681
d764a8e6
IS
9682 /* A tree representing the binding after the 'next_batch' label. */
9683 tree next_batch_bind;
46a88c12 9684
d764a8e6
IS
9685 /* A tree representing the 'next_object' label. */
9686 tree next_object_label_decl;
46a88c12 9687
d764a8e6
IS
9688 /* Temporary variables. */
9689 tree t;
9690 int i;
46a88c12 9691
22d8d616 9692 if (flag_objc1_only)
d764a8e6 9693 error_at (location, "fast enumeration is not available in Objective-C 1.0");
22d8d616 9694
d764a8e6 9695 if (object_expression == error_mark_node)
da57d1b9
NP
9696 return;
9697
d764a8e6
IS
9698 if (collection_expression == error_mark_node)
9699 return;
46a88c12 9700
d764a8e6 9701 if (!objc_type_valid_for_messaging (TREE_TYPE (object_expression), true))
46a88c12 9702 {
d764a8e6 9703 error_at (location, "iterating variable in fast enumeration is not an object");
46a88c12
NP
9704 return;
9705 }
9706
d764a8e6 9707 if (!objc_type_valid_for_messaging (TREE_TYPE (collection_expression), true))
46a88c12 9708 {
d764a8e6 9709 error_at (location, "collection in fast enumeration is not an object");
46a88c12
NP
9710 return;
9711 }
9712
d764a8e6
IS
9713 /* TODO: Check that object_expression is either a variable
9714 declaration, or an lvalue. */
46a88c12 9715
d764a8e6
IS
9716 /* This kludge is an idea from apple. We use the
9717 __objcFastEnumerationState struct implicitly defined by the
9718 compiler, unless a NSFastEnumerationState struct has been defined
9719 (by a Foundation library such as GNUstep Base) in which case, we
9720 use that one.
9721 */
9722 objc_fast_enumeration_state_type = objc_fast_enumeration_state_template;
9723 {
9724 tree objc_NSFastEnumeration_type = lookup_name (get_identifier ("NSFastEnumerationState"));
46a88c12 9725
d764a8e6 9726 if (objc_NSFastEnumeration_type)
46a88c12 9727 {
d764a8e6
IS
9728 /* TODO: We really need to check that
9729 objc_NSFastEnumeration_type is the same as ours! */
9730 if (TREE_CODE (objc_NSFastEnumeration_type) == TYPE_DECL)
9731 {
9732 /* If it's a typedef, use the original type. */
9733 if (DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type))
9734 objc_fast_enumeration_state_type = DECL_ORIGINAL_TYPE (objc_NSFastEnumeration_type);
9735 else
944fb799 9736 objc_fast_enumeration_state_type = TREE_TYPE (objc_NSFastEnumeration_type);
d764a8e6 9737 }
46a88c12 9738 }
d764a8e6 9739 }
46a88c12 9740
d764a8e6
IS
9741 /* { */
9742 /* Done by c-parser.c. */
46a88c12 9743
d764a8e6
IS
9744 /* type object; */
9745 /* Done by c-parser.c. */
da57d1b9 9746
d764a8e6 9747 /* Disable warnings that 'object' is unused. For example the code
46a88c12 9748
d764a8e6
IS
9749 for (id object in collection)
9750 i++;
da57d1b9 9751
d764a8e6
IS
9752 which can be used to count how many objects there are in the
9753 collection is fine and should generate no warnings even if
9754 'object' is technically unused. */
9755 TREE_USED (object_expression) = 1;
9756 if (DECL_P (object_expression))
9757 DECL_READ_P (object_expression) = 1;
46a88c12 9758
d764a8e6
IS
9759 /* id __objc_foreach_collection */
9760 objc_foreach_collection_decl = objc_create_temporary_var (objc_object_type, "__objc_foreach_collection");
22d8d616 9761
d764a8e6
IS
9762 /* __objcFastEnumerationState __objc_foreach_enum_state; */
9763 objc_foreach_enum_state_decl = objc_create_temporary_var (objc_fast_enumeration_state_type, "__objc_foreach_enum_state");
9764 TREE_CHAIN (objc_foreach_enum_state_decl) = objc_foreach_collection_decl;
da57d1b9 9765
d764a8e6
IS
9766 /* id __objc_foreach_items[16]; */
9767 objc_foreach_items_decl = objc_create_temporary_var (build_sized_array_type (objc_object_type, 16), "__objc_foreach_items");
9768 TREE_CHAIN (objc_foreach_items_decl) = objc_foreach_enum_state_decl;
da57d1b9 9769
d764a8e6
IS
9770 /* unsigned long __objc_foreach_batchsize; */
9771 objc_foreach_batchsize_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_batchsize");
9772 TREE_CHAIN (objc_foreach_batchsize_decl) = objc_foreach_items_decl;
9a179d01 9773
d764a8e6
IS
9774 /* Generate the local variable binding. */
9775 bind = build3 (BIND_EXPR, void_type_node, objc_foreach_batchsize_decl, NULL, NULL);
9776 SET_EXPR_LOCATION (bind, location);
9777 TREE_SIDE_EFFECTS (bind) = 1;
944fb799 9778
d764a8e6
IS
9779 /* __objc_foreach_collection = <collection expression>; */
9780 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_collection_decl, collection_expression);
9781 SET_EXPR_LOCATION (t, location);
9782 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9783 /* We have used 'collection_expression'. */
9784 mark_exp_read (collection_expression);
46a88c12 9785
d764a8e6 9786 /* __objc_foreach_enum_state.state = 0; */
944fb799 9787 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
d764a8e6
IS
9788 get_identifier ("state")),
9789 build_int_cst (long_unsigned_type_node, 0));
9790 SET_EXPR_LOCATION (t, location);
9791 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9792
9793 /* __objc_foreach_enum_state.itemsPtr = NULL; */
944fb799 9794 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
d764a8e6
IS
9795 get_identifier ("itemsPtr")),
9796 null_pointer_node);
9797 SET_EXPR_LOCATION (t, location);
9798 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9799
9800 /* __objc_foreach_enum_state.mutationsPtr = NULL; */
944fb799 9801 t = build2 (MODIFY_EXPR, void_type_node, objc_build_component_ref (objc_foreach_enum_state_decl,
d764a8e6
IS
9802 get_identifier ("mutationsPtr")),
9803 null_pointer_node);
9804 SET_EXPR_LOCATION (t, location);
9805 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
9806
9807 /* __objc_foreach_enum_state.extra[0] = 0; */
9808 /* __objc_foreach_enum_state.extra[1] = 0; */
9809 /* __objc_foreach_enum_state.extra[2] = 0; */
9810 /* __objc_foreach_enum_state.extra[3] = 0; */
9811 /* __objc_foreach_enum_state.extra[4] = 0; */
9812 for (i = 0; i < 5 ; i++)
46a88c12 9813 {
d764a8e6 9814 t = build2 (MODIFY_EXPR, void_type_node,
944fb799 9815 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
d764a8e6
IS
9816 get_identifier ("extra")),
9817 build_int_cst (NULL_TREE, i)),
9818 build_int_cst (long_unsigned_type_node, 0));
9819 SET_EXPR_LOCATION (t, location);
9820 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
46a88c12 9821 }
944fb799 9822
d764a8e6
IS
9823 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9824 selector_name = get_identifier ("countByEnumeratingWithState:objects:count:");
9825#ifdef OBJCPLUS
9826 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9827 /* Parameters. */
9828 tree_cons /* &__objc_foreach_enum_state */
9829 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9830 tree_cons /* __objc_foreach_items */
9831 (NULL_TREE, objc_foreach_items_decl,
9832 tree_cons /* 16 */
9833 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9834#else
9835 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
9836 {
9837 struct c_expr array;
9838 array.value = objc_foreach_items_decl;
9839 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
9840 /* Parameters. */
9841 tree_cons /* &__objc_foreach_enum_state */
9842 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
9843 tree_cons /* __objc_foreach_items */
9844 (NULL_TREE, default_function_array_conversion (location, array).value,
9845 tree_cons /* 16 */
9846 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
9847 }
9848#endif
9849 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
9850 convert (long_unsigned_type_node, t));
9851 SET_EXPR_LOCATION (t, location);
9852 append_to_statement_list (t, &BIND_EXPR_BODY (bind));
da57d1b9 9853
d764a8e6 9854 /* if (__objc_foreach_batchsize == 0) */
944fb799 9855 first_if = build3 (COND_EXPR, void_type_node,
d764a8e6 9856 /* Condition. */
944fb799
MS
9857 c_fully_fold
9858 (c_common_truthvalue_conversion
9859 (location,
d764a8e6 9860 build_binary_op (location,
944fb799 9861 EQ_EXPR,
d764a8e6
IS
9862 objc_foreach_batchsize_decl,
9863 build_int_cst (long_unsigned_type_node, 0), 1)),
9864 false, NULL),
9865 /* Then block (we fill it in later). */
9866 NULL_TREE,
9867 /* Else block (we fill it in later). */
9868 NULL_TREE);
9869 SET_EXPR_LOCATION (first_if, location);
9870 append_to_statement_list (first_if, &BIND_EXPR_BODY (bind));
668ea4b1 9871
d764a8e6
IS
9872 /* then <object expression> = nil; */
9873 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
9874 SET_EXPR_LOCATION (t, location);
9875 COND_EXPR_THEN (first_if) = t;
668ea4b1 9876
d764a8e6
IS
9877 /* Now we build the 'else' part of the if; once we finish building
9878 it, we attach it to first_if as the 'else' part. */
46a88c12 9879
d764a8e6
IS
9880 /* else */
9881 /* { */
46a88c12 9882
d764a8e6
IS
9883 /* unsigned long __objc_foreach_mutations_pointer; */
9884 objc_foreach_mutations_pointer_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_mutations_pointer");
668ea4b1 9885
d764a8e6
IS
9886 /* Generate the local variable binding. */
9887 first_else = build3 (BIND_EXPR, void_type_node, objc_foreach_mutations_pointer_decl, NULL, NULL);
9888 SET_EXPR_LOCATION (first_else, location);
9889 TREE_SIDE_EFFECTS (first_else) = 1;
e31c7eec 9890
d764a8e6 9891 /* __objc_foreach_mutations_pointer = *__objc_foreach_enum_state.mutationsPtr; */
944fb799
MS
9892 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_mutations_pointer_decl,
9893 build_indirect_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
d764a8e6
IS
9894 get_identifier ("mutationsPtr")),
9895 RO_UNARY_STAR));
9896 SET_EXPR_LOCATION (t, location);
9897 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
668ea4b1 9898
d764a8e6
IS
9899 /* next_batch: */
9900 next_batch_label_decl = create_artificial_label (location);
944fb799 9901 t = build1 (LABEL_EXPR, void_type_node, next_batch_label_decl);
d764a8e6
IS
9902 SET_EXPR_LOCATION (t, location);
9903 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
944fb799 9904
d764a8e6 9905 /* { */
50ea5861 9906
d764a8e6
IS
9907 /* unsigned long __objc_foreach_index; */
9908 objc_foreach_index_decl = objc_create_temporary_var (long_unsigned_type_node, "__objc_foreach_index");
4ca5d2a7 9909
d764a8e6
IS
9910 /* Generate the local variable binding. */
9911 next_batch_bind = build3 (BIND_EXPR, void_type_node, objc_foreach_index_decl, NULL, NULL);
9912 SET_EXPR_LOCATION (next_batch_bind, location);
9913 TREE_SIDE_EFFECTS (next_batch_bind) = 1;
9914 append_to_statement_list (next_batch_bind, &BIND_EXPR_BODY (first_else));
f05b9d93 9915
d764a8e6
IS
9916 /* __objc_foreach_index = 0; */
9917 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
9918 build_int_cst (long_unsigned_type_node, 0));
9919 SET_EXPR_LOCATION (t, location);
9920 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
f05b9d93 9921
d764a8e6
IS
9922 /* next_object: */
9923 next_object_label_decl = create_artificial_label (location);
9924 t = build1 (LABEL_EXPR, void_type_node, next_object_label_decl);
9925 SET_EXPR_LOCATION (t, location);
9926 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
f05b9d93 9927
d764a8e6 9928 /* if (__objc_foreach_mutation_pointer != *__objc_foreach_enum_state.mutationsPtr) objc_enumeration_mutation (<collection expression>); */
944fb799 9929 t = build3 (COND_EXPR, void_type_node,
d764a8e6 9930 /* Condition. */
944fb799
MS
9931 c_fully_fold
9932 (c_common_truthvalue_conversion
9933 (location,
9934 build_binary_op
d764a8e6 9935 (location,
944fb799 9936 NE_EXPR,
d764a8e6 9937 objc_foreach_mutations_pointer_decl,
944fb799
MS
9938 build_indirect_ref (location,
9939 objc_build_component_ref (objc_foreach_enum_state_decl,
d764a8e6
IS
9940 get_identifier ("mutationsPtr")),
9941 RO_UNARY_STAR), 1)),
9942 false, NULL),
9943 /* Then block. */
9944 build_function_call (input_location,
9945 objc_enumeration_mutation_decl,
9946 tree_cons (NULL, collection_expression, NULL)),
9947 /* Else block. */
9948 NULL_TREE);
9949 SET_EXPR_LOCATION (t, location);
9950 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
f05b9d93 9951
d764a8e6 9952 /* <object expression> = enumState.itemsPtr[__objc_foreach_index]; */
944fb799
MS
9953 t = build2 (MODIFY_EXPR, void_type_node, object_expression,
9954 build_array_ref (location, objc_build_component_ref (objc_foreach_enum_state_decl,
d764a8e6
IS
9955 get_identifier ("itemsPtr")),
9956 objc_foreach_index_decl));
9957 SET_EXPR_LOCATION (t, location);
9958 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
f05b9d93 9959
d764a8e6
IS
9960 /* <statements> [PS: in <statments>, 'break' jumps to break_label and 'continue' jumps to continue_label] */
9961 append_to_statement_list (for_body, &BIND_EXPR_BODY (next_batch_bind));
f05b9d93 9962
d764a8e6
IS
9963 /* continue_label: */
9964 if (continue_label)
9965 {
9966 t = build1 (LABEL_EXPR, void_type_node, continue_label);
9967 SET_EXPR_LOCATION (t, location);
9968 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
9969 }
f05b9d93 9970
d764a8e6 9971 /* __objc_foreach_index++; */
944fb799 9972 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_index_decl,
d764a8e6
IS
9973 build_binary_op (location,
9974 PLUS_EXPR,
9975 objc_foreach_index_decl,
9976 build_int_cst (long_unsigned_type_node, 1), 1));
9977 SET_EXPR_LOCATION (t, location);
9978 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
f05b9d93 9979
d764a8e6 9980 /* if (__objc_foreach_index < __objc_foreach_batchsize) goto next_object; */
944fb799 9981 t = build3 (COND_EXPR, void_type_node,
d764a8e6 9982 /* Condition. */
944fb799
MS
9983 c_fully_fold
9984 (c_common_truthvalue_conversion
9985 (location,
d764a8e6 9986 build_binary_op (location,
944fb799 9987 LT_EXPR,
d764a8e6
IS
9988 objc_foreach_index_decl,
9989 objc_foreach_batchsize_decl, 1)),
9990 false, NULL),
9991 /* Then block. */
9992 build1 (GOTO_EXPR, void_type_node, next_object_label_decl),
9993 /* Else block. */
9994 NULL_TREE);
9995 SET_EXPR_LOCATION (t, location);
9996 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
944fb799 9997
d764a8e6
IS
9998 /* __objc_foreach_batchsize = [__objc_foreach_collection countByEnumeratingWithState: &__objc_foreach_enum_state objects: __objc_foreach_items count: 16]; */
9999#ifdef OBJCPLUS
10000 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10001 /* Parameters. */
10002 tree_cons /* &__objc_foreach_enum_state */
10003 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10004 tree_cons /* __objc_foreach_items */
10005 (NULL_TREE, objc_foreach_items_decl,
10006 tree_cons /* 16 */
10007 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10008#else
10009 /* In C, we need to decay the __objc_foreach_items array that we are passing. */
f05b9d93 10010 {
d764a8e6
IS
10011 struct c_expr array;
10012 array.value = objc_foreach_items_decl;
10013 t = objc_finish_message_expr (objc_foreach_collection_decl, selector_name,
10014 /* Parameters. */
10015 tree_cons /* &__objc_foreach_enum_state */
10016 (NULL_TREE, build_fold_addr_expr_loc (location, objc_foreach_enum_state_decl),
10017 tree_cons /* __objc_foreach_items */
10018 (NULL_TREE, default_function_array_conversion (location, array).value,
10019 tree_cons /* 16 */
10020 (NULL_TREE, build_int_cst (NULL_TREE, 16), NULL_TREE))), NULL);
10021 }
10022#endif
944fb799 10023 t = build2 (MODIFY_EXPR, void_type_node, objc_foreach_batchsize_decl,
d764a8e6
IS
10024 convert (long_unsigned_type_node, t));
10025 SET_EXPR_LOCATION (t, location);
10026 append_to_statement_list (t, &BIND_EXPR_BODY (next_batch_bind));
f05b9d93 10027
d764a8e6 10028 /* } */
f05b9d93 10029
d764a8e6 10030 /* if (__objc_foreach_batchsize != 0) goto next_batch; */
944fb799 10031 t = build3 (COND_EXPR, void_type_node,
d764a8e6 10032 /* Condition. */
944fb799
MS
10033 c_fully_fold
10034 (c_common_truthvalue_conversion
10035 (location,
d764a8e6 10036 build_binary_op (location,
944fb799 10037 NE_EXPR,
d764a8e6
IS
10038 objc_foreach_batchsize_decl,
10039 build_int_cst (long_unsigned_type_node, 0), 1)),
10040 false, NULL),
10041 /* Then block. */
10042 build1 (GOTO_EXPR, void_type_node, next_batch_label_decl),
10043 /* Else block. */
10044 NULL_TREE);
10045 SET_EXPR_LOCATION (t, location);
10046 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
f05b9d93 10047
d764a8e6
IS
10048 /* <object expression> = nil; */
10049 t = build2 (MODIFY_EXPR, void_type_node, object_expression, convert (objc_object_type, null_pointer_node));
10050 SET_EXPR_LOCATION (t, location);
10051 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10052
10053 /* break_label: */
10054 if (break_label)
10055 {
10056 t = build1 (LABEL_EXPR, void_type_node, break_label);
10057 SET_EXPR_LOCATION (t, location);
10058 append_to_statement_list (t, &BIND_EXPR_BODY (first_else));
10059 }
f05b9d93 10060
d764a8e6
IS
10061 /* } */
10062 COND_EXPR_ELSE (first_if) = first_else;
10063
10064 /* Do the whole thing. */
10065 add_stmt (bind);
f05b9d93
NP
10066
10067#ifdef DEBUG_OBJC_FINISH_FOREACH_LOOP
d764a8e6
IS
10068 /* This will print to stderr the whole blurb generated by the
10069 compiler while compiling (assuming the compiler doesn't crash
10070 before getting here).
10071 */
10072 debug_generic_stmt (bind);
f05b9d93
NP
10073#endif
10074
d764a8e6
IS
10075 /* } */
10076 /* Done by c-parser.c */
10077}
10078
10079/* --- SUPPORT FOR FORMAT ARG CHECKING --- */
10080/* Return true if we have an NxString object pointer. */
10081
10082bool
10083objc_string_ref_type_p (tree strp)
10084{
10085 tree tmv;
10086 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
10087 return false;
10088
10089 tmv = TYPE_MAIN_VARIANT (TREE_TYPE (strp));
10090 tmv = OBJC_TYPE_NAME (tmv);
10091 return (tmv
944fb799
MS
10092 && TREE_CODE (tmv) == IDENTIFIER_NODE
10093 && IDENTIFIER_POINTER (tmv)
d764a8e6
IS
10094 && !strncmp (IDENTIFIER_POINTER (tmv), "NSString", 8));
10095}
10096
10097/* At present the behavior of this is undefined and it does nothing. */
f05b9d93 10098void
944fb799 10099objc_check_format_arg (tree ARG_UNUSED (format_arg),
d764a8e6 10100 tree ARG_UNUSED (args_list))
f05b9d93 10101{
d764a8e6 10102}
f05b9d93 10103
81f653d6
NF
10104void
10105objc_common_init_ts (void)
10106{
10107 c_common_init_ts ();
10108
10109 MARK_TS_DECL_NON_COMMON (CLASS_METHOD_DECL);
10110 MARK_TS_DECL_NON_COMMON (INSTANCE_METHOD_DECL);
10111 MARK_TS_DECL_NON_COMMON (KEYWORD_DECL);
10112 MARK_TS_DECL_NON_COMMON (PROPERTY_DECL);
10113
10114 MARK_TS_COMMON (CLASS_INTERFACE_TYPE);
10115 MARK_TS_COMMON (PROTOCOL_INTERFACE_TYPE);
10116 MARK_TS_COMMON (CLASS_IMPLEMENTATION_TYPE);
10117
10118 MARK_TS_TYPED (MESSAGE_SEND_EXPR);
10119 MARK_TS_TYPED (PROPERTY_REF);
10120}
10121
ad115a3c
JH
10122size_t
10123objc_common_tree_size (enum tree_code code)
10124{
10125 switch (code)
10126 {
10127 case CLASS_METHOD_DECL:
10128 case INSTANCE_METHOD_DECL:
10129 case KEYWORD_DECL:
10130 case PROPERTY_DECL:
10131 return sizeof (struct tree_decl_non_common);
10132 default:
10133 gcc_unreachable ();
10134
10135 }
10136}
10137
10138
8643e92d 10139#include "gt-objc-objc-act.h"