]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/objc-lang.c
2003-10-06 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / objc-lang.c
CommitLineData
d2e6263c 1/* Objective-C language support routines for GDB, the GNU debugger.
b81654f1 2
de5ad195 3 Copyright 2002, 2003 Free Software Foundation, Inc.
b81654f1 4
437666f8
AC
5 Contributed by Apple Computer, Inc.
6 Written by Michael Snyder.
b81654f1 7
437666f8 8 This file is part of GDB.
b81654f1 9
437666f8
AC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
b81654f1
MS
24
25#include "defs.h"
26#include "symtab.h"
27#include "gdbtypes.h"
28#include "expression.h"
29#include "parser-defs.h"
30#include "language.h"
d2e6263c 31#include "c-lang.h"
b81654f1
MS
32#include "objc-lang.h"
33#include "complaints.h"
34#include "value.h"
35#include "symfile.h"
36#include "objfiles.h"
7248f48e 37#include "gdb_string.h" /* for strchr */
b81654f1
MS
38#include "target.h" /* for target_has_execution */
39#include "gdbcore.h"
40#include "gdbcmd.h"
41#include "frame.h"
42#include "gdb_regex.h"
43#include "regcache.h"
fe898f56 44#include "block.h"
04714b91 45#include "infcall.h"
4e45ca2e 46#include "valprint.h"
e8f3fcdd 47#include "gdb_assert.h"
b81654f1
MS
48
49#include <ctype.h>
50
51struct objc_object {
52 CORE_ADDR isa;
53};
54
55struct objc_class {
56 CORE_ADDR isa;
57 CORE_ADDR super_class;
58 CORE_ADDR name;
59 long version;
60 long info;
61 long instance_size;
62 CORE_ADDR ivars;
63 CORE_ADDR methods;
64 CORE_ADDR cache;
65 CORE_ADDR protocols;
66};
67
68struct objc_super {
69 CORE_ADDR receiver;
70 CORE_ADDR class;
71};
72
73struct objc_method {
74 CORE_ADDR name;
75 CORE_ADDR types;
76 CORE_ADDR imp;
77};
78
d2e6263c
MS
79/* Lookup a structure type named "struct NAME", visible in lexical
80 block BLOCK. If NOERR is nonzero, return zero if NAME is not
81 suitably defined. */
b81654f1
MS
82
83struct symbol *
84lookup_struct_typedef (char *name, struct block *block, int noerr)
85{
f86f5ca3 86 struct symbol *sym;
b81654f1 87
176620f1 88 sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0,
d2e6263c 89 (struct symtab **) NULL);
b81654f1
MS
90
91 if (sym == NULL)
92 {
93 if (noerr)
94 return 0;
95 else
96 error ("No struct type named %s.", name);
97 }
98 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
99 {
100 if (noerr)
101 return 0;
102 else
d2e6263c
MS
103 error ("This context has class, union or enum %s, not a struct.",
104 name);
b81654f1
MS
105 }
106 return sym;
107}
108
109CORE_ADDR
110lookup_objc_class (char *classname)
111{
112 struct value * function, *classval;
113
114 if (! target_has_execution)
115 {
d2e6263c 116 /* Can't call into inferior to lookup class. */
b81654f1
MS
117 return 0;
118 }
119
120 if (lookup_minimal_symbol("objc_lookUpClass", 0, 0))
121 function = find_function_in_inferior("objc_lookUpClass");
122 else if (lookup_minimal_symbol ("objc_lookup_class", 0, 0))
123 function = find_function_in_inferior("objc_lookup_class");
124 else
125 {
9d1127c5 126 complaint (&symfile_complaints, "no way to lookup Objective-C classes");
b81654f1
MS
127 return 0;
128 }
129
130 classval = value_string (classname, strlen (classname) + 1);
131 classval = value_coerce_array (classval);
132 return (CORE_ADDR) value_as_long (call_function_by_hand (function,
133 1, &classval));
134}
135
136int
137lookup_child_selector (char *selname)
138{
139 struct value * function, *selstring;
140
141 if (! target_has_execution)
142 {
d2e6263c 143 /* Can't call into inferior to lookup selector. */
b81654f1
MS
144 return 0;
145 }
146
147 if (lookup_minimal_symbol("sel_getUid", 0, 0))
148 function = find_function_in_inferior("sel_getUid");
149 else if (lookup_minimal_symbol ("sel_get_any_uid", 0, 0))
150 function = find_function_in_inferior("sel_get_any_uid");
151 else
152 {
9d1127c5 153 complaint (&symfile_complaints, "no way to lookup Objective-C selectors");
b81654f1
MS
154 return 0;
155 }
156
d2e6263c
MS
157 selstring = value_coerce_array (value_string (selname,
158 strlen (selname) + 1));
b81654f1
MS
159 return value_as_long (call_function_by_hand (function, 1, &selstring));
160}
161
162struct value *
163value_nsstring (char *ptr, int len)
164{
165 struct value *stringValue[3];
166 struct value *function, *nsstringValue;
167 struct symbol *sym;
168 struct type *type;
169
170 if (!target_has_execution)
d2e6263c 171 return 0; /* Can't call into inferior to create NSString. */
b81654f1 172
5e488a7b
AC
173 sym = lookup_struct_typedef("NSString", 0, 1);
174 if (sym == NULL)
175 sym = lookup_struct_typedef("NXString", 0, 1);
176 if (sym == NULL)
b81654f1
MS
177 type = lookup_pointer_type(builtin_type_void);
178 else
179 type = lookup_pointer_type(SYMBOL_TYPE (sym));
180
181 stringValue[2] = value_string(ptr, len);
182 stringValue[2] = value_coerce_array(stringValue[2]);
d2e6263c 183 /* _NSNewStringFromCString replaces "istr" after Lantern2A. */
b81654f1
MS
184 if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0))
185 {
186 function = find_function_in_inferior("_NSNewStringFromCString");
187 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
188 }
189 else if (lookup_minimal_symbol("istr", 0, 0))
190 {
191 function = find_function_in_inferior("istr");
192 nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
193 }
194 else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0))
195 {
196 function = find_function_in_inferior("+[NSString stringWithCString:]");
197 stringValue[0] = value_from_longest
198 (builtin_type_long, lookup_objc_class ("NSString"));
199 stringValue[1] = value_from_longest
200 (builtin_type_long, lookup_child_selector ("stringWithCString:"));
201 nsstringValue = call_function_by_hand(function, 3, &stringValue[0]);
202 }
203 else
204 error ("NSString: internal error -- no way to create new NSString");
205
206 VALUE_TYPE(nsstringValue) = type;
207 return nsstringValue;
208}
209
d2e6263c 210/* Objective-C name demangling. */
b81654f1
MS
211
212char *
9a3d7dfd 213objc_demangle (const char *mangled, int options)
b81654f1
MS
214{
215 char *demangled, *cp;
216
217 if (mangled[0] == '_' &&
218 (mangled[1] == 'i' || mangled[1] == 'c') &&
219 mangled[2] == '_')
220 {
221 cp = demangled = xmalloc(strlen(mangled) + 2);
222
223 if (mangled[1] == 'i')
224 *cp++ = '-'; /* for instance method */
225 else
226 *cp++ = '+'; /* for class method */
227
228 *cp++ = '['; /* opening left brace */
229 strcpy(cp, mangled+3); /* tack on the rest of the mangled name */
230
231 while (*cp && *cp == '_')
232 cp++; /* skip any initial underbars in class name */
233
7248f48e
AF
234 cp = strchr(cp, '_');
235 if (!cp) /* find first non-initial underbar */
b81654f1 236 {
7248f48e 237 xfree(demangled); /* not mangled name */
b81654f1
MS
238 return NULL;
239 }
240 if (cp[1] == '_') { /* easy case: no category name */
241 *cp++ = ' '; /* replace two '_' with one ' ' */
242 strcpy(cp, mangled + (cp - demangled) + 2);
243 }
244 else {
245 *cp++ = '('; /* less easy case: category name */
7248f48e
AF
246 cp = strchr(cp, '_');
247 if (!cp)
b81654f1 248 {
7248f48e 249 xfree(demangled); /* not mangled name */
b81654f1
MS
250 return NULL;
251 }
252 *cp++ = ')';
d2e6263c 253 *cp++ = ' '; /* overwriting 1st char of method name... */
b81654f1
MS
254 strcpy(cp, mangled + (cp - demangled)); /* get it back */
255 }
256
257 while (*cp && *cp == '_')
258 cp++; /* skip any initial underbars in method name */
259
260 for (; *cp; cp++)
261 if (*cp == '_')
262 *cp = ':'; /* replace remaining '_' with ':' */
263
264 *cp++ = ']'; /* closing right brace */
265 *cp++ = 0; /* string terminator */
266 return demangled;
267 }
268 else
d2e6263c 269 return NULL; /* Not an objc mangled name. */
b81654f1
MS
270}
271
d2e6263c
MS
272/* Print the character C on STREAM as part of the contents of a
273 literal string whose delimiter is QUOTER. Note that that format
274 for printing characters and strings is language specific. */
b81654f1
MS
275
276static void
f86f5ca3 277objc_emit_char (int c, struct ui_file *stream, int quoter)
b81654f1
MS
278{
279
d2e6263c 280 c &= 0xFF; /* Avoid sign bit follies. */
b81654f1
MS
281
282 if (PRINT_LITERAL_FORM (c))
283 {
284 if (c == '\\' || c == quoter)
285 {
286 fputs_filtered ("\\", stream);
287 }
288 fprintf_filtered (stream, "%c", c);
289 }
290 else
291 {
292 switch (c)
293 {
294 case '\n':
295 fputs_filtered ("\\n", stream);
296 break;
297 case '\b':
298 fputs_filtered ("\\b", stream);
299 break;
300 case '\t':
301 fputs_filtered ("\\t", stream);
302 break;
303 case '\f':
304 fputs_filtered ("\\f", stream);
305 break;
306 case '\r':
307 fputs_filtered ("\\r", stream);
308 break;
309 case '\033':
310 fputs_filtered ("\\e", stream);
311 break;
312 case '\007':
313 fputs_filtered ("\\a", stream);
314 break;
315 default:
316 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
317 break;
318 }
319 }
320}
321
322static void
323objc_printchar (int c, struct ui_file *stream)
324{
325 fputs_filtered ("'", stream);
326 objc_emit_char (c, stream, '\'');
327 fputs_filtered ("'", stream);
328}
329
d2e6263c
MS
330/* Print the character string STRING, printing at most LENGTH
331 characters. Printing stops early if the number hits print_max;
332 repeat counts are printed as appropriate. Print ellipses at the
333 end if we had to stop before printing LENGTH characters, or if
334 FORCE_ELLIPSES. */
b81654f1
MS
335
336static void
d2e6263c 337objc_printstr (struct ui_file *stream, char *string,
36e53c63 338 unsigned int length, int width, int force_ellipses)
b81654f1 339{
f86f5ca3 340 unsigned int i;
b81654f1
MS
341 unsigned int things_printed = 0;
342 int in_quotes = 0;
343 int need_comma = 0;
b81654f1
MS
344
345 /* If the string was not truncated due to `set print elements', and
d2e6263c
MS
346 the last byte of it is a null, we don't print that, in
347 traditional C style. */
b81654f1
MS
348 if ((!force_ellipses) && length > 0 && string[length-1] == '\0')
349 length--;
350
351 if (length == 0)
352 {
353 fputs_filtered ("\"\"", stream);
354 return;
355 }
356
357 for (i = 0; i < length && things_printed < print_max; ++i)
358 {
d2e6263c
MS
359 /* Position of the character we are examining to see whether it
360 is repeated. */
b81654f1
MS
361 unsigned int rep1;
362 /* Number of repetitions we have detected so far. */
363 unsigned int reps;
364
365 QUIT;
366
367 if (need_comma)
368 {
369 fputs_filtered (", ", stream);
370 need_comma = 0;
371 }
372
373 rep1 = i + 1;
374 reps = 1;
375 while (rep1 < length && string[rep1] == string[i])
376 {
377 ++rep1;
378 ++reps;
379 }
380
381 if (reps > repeat_count_threshold)
382 {
383 if (in_quotes)
384 {
385 if (inspect_it)
386 fputs_filtered ("\\\", ", stream);
387 else
388 fputs_filtered ("\", ", stream);
389 in_quotes = 0;
390 }
391 objc_printchar (string[i], stream);
392 fprintf_filtered (stream, " <repeats %u times>", reps);
393 i = rep1 - 1;
394 things_printed += repeat_count_threshold;
395 need_comma = 1;
396 }
397 else
398 {
399 if (!in_quotes)
400 {
401 if (inspect_it)
402 fputs_filtered ("\\\"", stream);
403 else
404 fputs_filtered ("\"", stream);
405 in_quotes = 1;
406 }
407 objc_emit_char (string[i], stream, '"');
408 ++things_printed;
409 }
410 }
411
412 /* Terminate the quotes if necessary. */
413 if (in_quotes)
414 {
415 if (inspect_it)
416 fputs_filtered ("\\\"", stream);
417 else
418 fputs_filtered ("\"", stream);
419 }
420
421 if (force_ellipses || i < length)
422 fputs_filtered ("...", stream);
423}
424
d2e6263c
MS
425/* Create a fundamental C type using default reasonable for the
426 current target.
427
428 Some object/debugging file formats (DWARF version 1, COFF, etc) do
429 not define fundamental types such as "int" or "double". Others
430 (stabs or DWARF version 2, etc) do define fundamental types. For
431 the formats which don't provide fundamental types, gdb can create
432 such types using this function.
433
434 FIXME: Some compilers distinguish explicitly signed integral types
435 (signed short, signed int, signed long) from "regular" integral
436 types (short, int, long) in the debugging information. There is
437 some disagreement as to how useful this feature is. In particular,
438 gcc does not support this. Also, only some debugging formats allow
439 the distinction to be passed on to a debugger. For now, we always
440 just use "short", "int", or "long" as the type name, for both the
441 implicit and explicitly signed types. This also makes life easier
442 for the gdb test suite since we don't have to account for the
443 differences in output depending upon what the compiler and
444 debugging format support. We will probably have to re-examine the
445 issue when gdb starts taking it's fundamental type information
446 directly from the debugging information supplied by the compiler.
447 fnf@cygnus.com */
b81654f1
MS
448
449static struct type *
450objc_create_fundamental_type (struct objfile *objfile, int typeid)
451{
f86f5ca3 452 struct type *type = NULL;
b81654f1
MS
453
454 switch (typeid)
455 {
456 default:
d2e6263c
MS
457 /* FIXME: For now, if we are asked to produce a type not in
458 this language, create the equivalent of a C integer type
459 with the name "<?type?>". When all the dust settles from
460 the type reconstruction work, this should probably become
461 an error. */
b81654f1
MS
462 type = init_type (TYPE_CODE_INT,
463 TARGET_INT_BIT / TARGET_CHAR_BIT,
464 0, "<?type?>", objfile);
465 warning ("internal error: no C/C++ fundamental type %d", typeid);
466 break;
467 case FT_VOID:
468 type = init_type (TYPE_CODE_VOID,
469 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
470 0, "void", objfile);
471 break;
472 case FT_CHAR:
473 type = init_type (TYPE_CODE_INT,
474 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
475 0, "char", objfile);
476 break;
477 case FT_SIGNED_CHAR:
478 type = init_type (TYPE_CODE_INT,
479 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
480 0, "signed char", objfile);
481 break;
482 case FT_UNSIGNED_CHAR:
483 type = init_type (TYPE_CODE_INT,
484 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
485 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
486 break;
487 case FT_SHORT:
488 type = init_type (TYPE_CODE_INT,
489 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
490 0, "short", objfile);
491 break;
492 case FT_SIGNED_SHORT:
493 type = init_type (TYPE_CODE_INT,
494 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
495 0, "short", objfile); /* FIXME-fnf */
496 break;
497 case FT_UNSIGNED_SHORT:
498 type = init_type (TYPE_CODE_INT,
499 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
500 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
501 break;
502 case FT_INTEGER:
503 type = init_type (TYPE_CODE_INT,
504 TARGET_INT_BIT / TARGET_CHAR_BIT,
505 0, "int", objfile);
506 break;
507 case FT_SIGNED_INTEGER:
508 type = init_type (TYPE_CODE_INT,
509 TARGET_INT_BIT / TARGET_CHAR_BIT,
510 0, "int", objfile); /* FIXME -fnf */
511 break;
512 case FT_UNSIGNED_INTEGER:
513 type = init_type (TYPE_CODE_INT,
514 TARGET_INT_BIT / TARGET_CHAR_BIT,
515 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
516 break;
517 case FT_LONG:
518 type = init_type (TYPE_CODE_INT,
519 TARGET_LONG_BIT / TARGET_CHAR_BIT,
520 0, "long", objfile);
521 break;
522 case FT_SIGNED_LONG:
523 type = init_type (TYPE_CODE_INT,
524 TARGET_LONG_BIT / TARGET_CHAR_BIT,
525 0, "long", objfile); /* FIXME -fnf */
526 break;
527 case FT_UNSIGNED_LONG:
528 type = init_type (TYPE_CODE_INT,
529 TARGET_LONG_BIT / TARGET_CHAR_BIT,
530 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
531 break;
532 case FT_LONG_LONG:
533 type = init_type (TYPE_CODE_INT,
534 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
535 0, "long long", objfile);
536 break;
537 case FT_SIGNED_LONG_LONG:
538 type = init_type (TYPE_CODE_INT,
539 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
540 0, "signed long long", objfile);
541 break;
542 case FT_UNSIGNED_LONG_LONG:
543 type = init_type (TYPE_CODE_INT,
544 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
545 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
546 break;
547 case FT_FLOAT:
548 type = init_type (TYPE_CODE_FLT,
549 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
550 0, "float", objfile);
551 break;
552 case FT_DBL_PREC_FLOAT:
553 type = init_type (TYPE_CODE_FLT,
554 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
555 0, "double", objfile);
556 break;
557 case FT_EXT_PREC_FLOAT:
558 type = init_type (TYPE_CODE_FLT,
559 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
560 0, "long double", objfile);
561 break;
562 }
563 return (type);
564}
565
f636b87d
AF
566/* Determine if we are currently in the Objective-C dispatch function.
567 If so, get the address of the method function that the dispatcher
568 would call and use that as the function to step into instead. Also
569 skip over the trampoline for the function (if any). This is better
570 for the user since they are only interested in stepping into the
571 method function anyway. */
572static CORE_ADDR
573objc_skip_trampoline (CORE_ADDR stop_pc)
574{
575 CORE_ADDR real_stop_pc;
576 CORE_ADDR method_stop_pc;
577
578 real_stop_pc = SKIP_TRAMPOLINE_CODE (stop_pc);
579
580 if (real_stop_pc != 0)
581 find_objc_msgcall (real_stop_pc, &method_stop_pc);
582 else
583 find_objc_msgcall (stop_pc, &method_stop_pc);
584
585 if (method_stop_pc)
586 {
587 real_stop_pc = SKIP_TRAMPOLINE_CODE (method_stop_pc);
588 if (real_stop_pc == 0)
589 real_stop_pc = method_stop_pc;
590 }
591
592 return real_stop_pc;
593}
594
b81654f1
MS
595
596/* Table mapping opcodes into strings for printing operators
597 and precedences of the operators. */
598
599static const struct op_print objc_op_print_tab[] =
600 {
601 {",", BINOP_COMMA, PREC_COMMA, 0},
602 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
603 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
604 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
605 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
606 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
607 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
608 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
609 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
610 {"<=", BINOP_LEQ, PREC_ORDER, 0},
611 {">=", BINOP_GEQ, PREC_ORDER, 0},
612 {">", BINOP_GTR, PREC_ORDER, 0},
613 {"<", BINOP_LESS, PREC_ORDER, 0},
614 {">>", BINOP_RSH, PREC_SHIFT, 0},
615 {"<<", BINOP_LSH, PREC_SHIFT, 0},
616 {"+", BINOP_ADD, PREC_ADD, 0},
617 {"-", BINOP_SUB, PREC_ADD, 0},
618 {"*", BINOP_MUL, PREC_MUL, 0},
619 {"/", BINOP_DIV, PREC_MUL, 0},
620 {"%", BINOP_REM, PREC_MUL, 0},
621 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
622 {"-", UNOP_NEG, PREC_PREFIX, 0},
623 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
624 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
625 {"*", UNOP_IND, PREC_PREFIX, 0},
626 {"&", UNOP_ADDR, PREC_PREFIX, 0},
627 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
628 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
629 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
e8f3fcdd 630 {NULL, OP_NULL, PREC_NULL, 0}
b81654f1
MS
631};
632
633struct type ** const (objc_builtin_types[]) =
634{
635 &builtin_type_int,
636 &builtin_type_long,
637 &builtin_type_short,
638 &builtin_type_char,
639 &builtin_type_float,
640 &builtin_type_double,
641 &builtin_type_void,
642 &builtin_type_long_long,
643 &builtin_type_signed_char,
644 &builtin_type_unsigned_char,
645 &builtin_type_unsigned_short,
646 &builtin_type_unsigned_int,
647 &builtin_type_unsigned_long,
648 &builtin_type_unsigned_long_long,
649 &builtin_type_long_double,
650 &builtin_type_complex,
651 &builtin_type_double_complex,
652 0
653};
654
655const struct language_defn objc_language_defn = {
d2e6263c 656 "objective-c", /* Language name */
b81654f1
MS
657 language_objc,
658 objc_builtin_types,
659 range_check_off,
660 type_check_off,
661 case_sensitive_on,
5f9769d1 662 &exp_descriptor_standard,
b81654f1
MS
663 objc_parse,
664 objc_error,
b81654f1
MS
665 objc_printchar, /* Print a character constant */
666 objc_printstr, /* Function to print string constant */
667 objc_emit_char,
668 objc_create_fundamental_type, /* Create fundamental type in this language */
669 c_print_type, /* Print a type using appropriate syntax */
670 c_val_print, /* Print a value using appropriate syntax */
671 c_value_print, /* Print a top-level value */
f636b87d 672 objc_skip_trampoline, /* Language specific skip_trampoline */
5f9a71c3
DC
673 value_of_this, /* value_of_this */
674 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
9a3d7dfd 675 objc_demangle, /* Language specific symbol demangler */
b81654f1
MS
676 {"", "", "", ""}, /* Binary format info */
677 {"0%lo", "0", "o", ""}, /* Octal format info */
678 {"%ld", "", "d", ""}, /* Decimal format info */
679 {"0x%lx", "0x", "x", ""}, /* Hex format info */
d2e6263c
MS
680 objc_op_print_tab, /* Expression operators for printing */
681 1, /* C-style arrays */
b81654f1
MS
682 0, /* String lower bound */
683 &builtin_type_char, /* Type of string elements */
684 LANG_MAGIC
685};
686
687/*
688 * ObjC:
d2e6263c 689 * Following functions help construct Objective-C message calls
b81654f1
MS
690 */
691
d2e6263c 692struct selname /* For parsing Objective-C. */
b81654f1
MS
693 {
694 struct selname *next;
695 char *msglist_sel;
696 int msglist_len;
697 };
698
699static int msglist_len;
700static struct selname *selname_chain;
701static char *msglist_sel;
702
703void
704start_msglist(void)
705{
f86f5ca3 706 struct selname *new =
b81654f1
MS
707 (struct selname *) xmalloc (sizeof (struct selname));
708
709 new->next = selname_chain;
710 new->msglist_len = msglist_len;
711 new->msglist_sel = msglist_sel;
712 msglist_len = 0;
713 msglist_sel = (char *)xmalloc(1);
714 *msglist_sel = 0;
715 selname_chain = new;
716}
717
718void
719add_msglist(struct stoken *str, int addcolon)
720{
721 char *s, *p;
722 int len, plen;
723
d2e6263c
MS
724 if (str == 0) { /* Unnamed arg, or... */
725 if (addcolon == 0) { /* variable number of args. */
b81654f1
MS
726 msglist_len++;
727 return;
728 }
729 p = "";
730 plen = 0;
731 } else {
732 p = str->ptr;
733 plen = str->length;
734 }
735 len = plen + strlen(msglist_sel) + 2;
736 s = (char *)xmalloc(len);
737 strcpy(s, msglist_sel);
738 strncat(s, p, plen);
7248f48e 739 xfree(msglist_sel);
b81654f1
MS
740 msglist_sel = s;
741 if (addcolon) {
742 s[len-2] = ':';
743 s[len-1] = 0;
744 msglist_len++;
745 } else
746 s[len-2] = '\0';
747}
748
749int
750end_msglist(void)
751{
f86f5ca3
PH
752 int val = msglist_len;
753 struct selname *sel = selname_chain;
754 char *p = msglist_sel;
b81654f1
MS
755 int selid;
756
757 selname_chain = sel->next;
758 msglist_len = sel->msglist_len;
759 msglist_sel = sel->msglist_sel;
760 selid = lookup_child_selector(p);
761 if (!selid)
762 error("Can't find selector \"%s\"", p);
763 write_exp_elt_longcst (selid);
7248f48e 764 xfree(p);
d2e6263c 765 write_exp_elt_longcst (val); /* Number of args */
7248f48e 766 xfree(sel);
b81654f1
MS
767
768 return val;
769}
770
771/*
772 * Function: specialcmp (char *a, char *b)
773 *
774 * Special strcmp: treats ']' and ' ' as end-of-string.
d2e6263c 775 * Used for qsorting lists of objc methods (either by class or selector).
b81654f1
MS
776 */
777
b9362cc7
AC
778static int
779specialcmp (char *a, char *b)
b81654f1
MS
780{
781 while (*a && *a != ' ' && *a != ']' && *b && *b != ' ' && *b != ']')
782 {
783 if (*a != *b)
784 return *a - *b;
785 a++, b++;
786 }
787 if (*a && *a != ' ' && *a != ']')
788 return 1; /* a is longer therefore greater */
789 if (*b && *b != ' ' && *b != ']')
790 return -1; /* a is shorter therefore lesser */
791 return 0; /* a and b are identical */
792}
793
794/*
36e53c63 795 * Function: compare_selectors (const void *, const void *)
b81654f1 796 *
d2e6263c
MS
797 * Comparison function for use with qsort. Arguments are symbols or
798 * msymbols Compares selector part of objc method name alphabetically.
b81654f1
MS
799 */
800
801static int
36e53c63 802compare_selectors (const void *a, const void *b)
b81654f1
MS
803{
804 char *aname, *bname;
805
de5ad195
DC
806 aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
807 bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
7248f48e 808 if (aname == NULL || bname == NULL)
b81654f1
MS
809 error ("internal: compare_selectors(1)");
810
7248f48e
AF
811 aname = strchr(aname, ' ');
812 bname = strchr(bname, ' ');
813 if (aname == NULL || bname == NULL)
b81654f1
MS
814 error ("internal: compare_selectors(2)");
815
816 return specialcmp (aname+1, bname+1);
817}
818
819/*
820 * Function: selectors_info (regexp, from_tty)
821 *
d2e6263c
MS
822 * Implements the "Info selectors" command. Takes an optional regexp
823 * arg. Lists all objective c selectors that match the regexp. Works
824 * by grepping thru all symbols for objective c methods. Output list
825 * is sorted and uniqued.
b81654f1
MS
826 */
827
828static void
829selectors_info (char *regexp, int from_tty)
830{
831 struct objfile *objfile;
832 struct minimal_symbol *msymbol;
833 char *name;
834 char *val;
835 int matches = 0;
836 int maxlen = 0;
837 int ix;
838 char myregexp[2048];
839 char asel[256];
840 struct symbol **sym_arr;
841 int plusminus = 0;
842
843 if (regexp == NULL)
d2e6263c 844 strcpy(myregexp, ".*]"); /* Null input, match all objc methods. */
b81654f1
MS
845 else
846 {
d2e6263c
MS
847 if (*regexp == '+' || *regexp == '-')
848 { /* User wants only class methods or only instance methods. */
b81654f1
MS
849 plusminus = *regexp++;
850 while (*regexp == ' ' || *regexp == '\t')
851 regexp++;
852 }
853 if (*regexp == '\0')
854 strcpy(myregexp, ".*]");
855 else
856 {
857 strcpy(myregexp, regexp);
858 if (myregexp[strlen(myregexp) - 1] == '$') /* end of selector */
859 myregexp[strlen(myregexp) - 1] = ']'; /* end of method name */
860 else
861 strcat(myregexp, ".*]");
862 }
863 }
864
865 if (regexp != NULL)
5e488a7b
AC
866 {
867 val = re_comp (myregexp);
868 if (val != 0)
869 error ("Invalid regexp (%s): %s", val, regexp);
870 }
b81654f1 871
d2e6263c 872 /* First time thru is JUST to get max length and count. */
b81654f1
MS
873 ALL_MSYMBOLS (objfile, msymbol)
874 {
875 QUIT;
36018d2e 876 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
877 if (name &&
878 (name[0] == '-' || name[0] == '+') &&
d2e6263c 879 name[1] == '[') /* Got a method name. */
b81654f1 880 {
d2e6263c 881 /* Filter for class/instance methods. */
b81654f1 882 if (plusminus && name[0] != plusminus)
d2e6263c
MS
883 continue;
884 /* Find selector part. */
885 name = (char *) strchr(name+2, ' ');
b81654f1
MS
886 if (regexp == NULL || re_exec(++name) != 0)
887 {
888 char *mystart = name;
889 char *myend = (char *) strchr(mystart, ']');
890
891 if (myend && (myend - mystart > maxlen))
d2e6263c 892 maxlen = myend - mystart; /* Get longest selector. */
b81654f1
MS
893 matches++;
894 }
895 }
896 }
897 if (matches)
898 {
899 printf_filtered ("Selectors matching \"%s\":\n\n",
900 regexp ? regexp : "*");
901
902 sym_arr = alloca (matches * sizeof (struct symbol *));
903 matches = 0;
904 ALL_MSYMBOLS (objfile, msymbol)
905 {
906 QUIT;
36018d2e 907 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
908 if (name &&
909 (name[0] == '-' || name[0] == '+') &&
d2e6263c 910 name[1] == '[') /* Got a method name. */
b81654f1 911 {
d2e6263c 912 /* Filter for class/instance methods. */
b81654f1 913 if (plusminus && name[0] != plusminus)
d2e6263c
MS
914 continue;
915 /* Find selector part. */
916 name = (char *) strchr(name+2, ' ');
b81654f1
MS
917 if (regexp == NULL || re_exec(++name) != 0)
918 sym_arr[matches++] = (struct symbol *) msymbol;
919 }
920 }
921
922 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
923 compare_selectors);
d2e6263c
MS
924 /* Prevent compare on first iteration. */
925 asel[0] = 0;
926 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
927 {
928 char *p = asel;
929
930 QUIT;
36018d2e 931 name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
b81654f1
MS
932 name = strchr (name, ' ') + 1;
933 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 934 continue; /* Seen this one already (not unique). */
b81654f1 935
d2e6263c
MS
936 /* Copy selector part. */
937 while (*name && *name != ']')
b81654f1
MS
938 *p++ = *name++;
939 *p++ = '\0';
d2e6263c
MS
940 /* Print in columns. */
941 puts_filtered_tabular(asel, maxlen + 1, 0);
b81654f1
MS
942 }
943 begin_line();
944 }
945 else
946 printf_filtered ("No selectors matching \"%s\"\n", regexp ? regexp : "*");
947}
948
949/*
36e53c63 950 * Function: compare_classes (const void *, const void *)
b81654f1 951 *
d2e6263c
MS
952 * Comparison function for use with qsort. Arguments are symbols or
953 * msymbols Compares class part of objc method name alphabetically.
b81654f1
MS
954 */
955
956static int
36e53c63 957compare_classes (const void *a, const void *b)
b81654f1
MS
958{
959 char *aname, *bname;
960
de5ad195
DC
961 aname = SYMBOL_PRINT_NAME (*(struct symbol **) a);
962 bname = SYMBOL_PRINT_NAME (*(struct symbol **) b);
7248f48e 963 if (aname == NULL || bname == NULL)
b81654f1
MS
964 error ("internal: compare_classes(1)");
965
966 return specialcmp (aname+1, bname+1);
967}
968
969/*
970 * Function: classes_info(regexp, from_tty)
971 *
972 * Implements the "info classes" command for objective c classes.
973 * Lists all objective c classes that match the optional regexp.
d2e6263c
MS
974 * Works by grepping thru the list of objective c methods. List will
975 * be sorted and uniqued (since one class may have many methods).
976 * BUGS: will not list a class that has no methods.
b81654f1
MS
977 */
978
979static void
980classes_info (char *regexp, int from_tty)
981{
982 struct objfile *objfile;
983 struct minimal_symbol *msymbol;
984 char *name;
985 char *val;
986 int matches = 0;
987 int maxlen = 0;
988 int ix;
989 char myregexp[2048];
990 char aclass[256];
991 struct symbol **sym_arr;
992
993 if (regexp == NULL)
d2e6263c 994 strcpy(myregexp, ".* "); /* Null input: match all objc classes. */
b81654f1
MS
995 else
996 {
997 strcpy(myregexp, regexp);
998 if (myregexp[strlen(myregexp) - 1] == '$')
d2e6263c 999 /* In the method name, the end of the class name is marked by ' '. */
b81654f1
MS
1000 myregexp[strlen(myregexp) - 1] = ' ';
1001 else
1002 strcat(myregexp, ".* ");
1003 }
1004
1005 if (regexp != NULL)
5e488a7b
AC
1006 {
1007 val = re_comp (myregexp);
1008 if (val != 0)
1009 error ("Invalid regexp (%s): %s", val, regexp);
1010 }
b81654f1 1011
d2e6263c 1012 /* First time thru is JUST to get max length and count. */
b81654f1
MS
1013 ALL_MSYMBOLS (objfile, msymbol)
1014 {
1015 QUIT;
36018d2e 1016 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
1017 if (name &&
1018 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1019 name[1] == '[') /* Got a method name. */
b81654f1
MS
1020 if (regexp == NULL || re_exec(name+2) != 0)
1021 {
d2e6263c
MS
1022 /* Compute length of classname part. */
1023 char *mystart = name + 2;
b81654f1
MS
1024 char *myend = (char *) strchr(mystart, ' ');
1025
1026 if (myend && (myend - mystart > maxlen))
1027 maxlen = myend - mystart;
1028 matches++;
1029 }
1030 }
1031 if (matches)
1032 {
1033 printf_filtered ("Classes matching \"%s\":\n\n",
1034 regexp ? regexp : "*");
1035 sym_arr = alloca (matches * sizeof (struct symbol *));
1036 matches = 0;
1037 ALL_MSYMBOLS (objfile, msymbol)
1038 {
1039 QUIT;
36018d2e 1040 name = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
1041 if (name &&
1042 (name[0] == '-' || name[0] == '+') &&
d2e6263c 1043 name[1] == '[') /* Got a method name. */
b81654f1
MS
1044 if (regexp == NULL || re_exec(name+2) != 0)
1045 sym_arr[matches++] = (struct symbol *) msymbol;
1046 }
1047
1048 qsort (sym_arr, matches, sizeof (struct minimal_symbol *),
1049 compare_classes);
d2e6263c
MS
1050 /* Prevent compare on first iteration. */
1051 aclass[0] = 0;
1052 for (ix = 0; ix < matches; ix++) /* Now do the output. */
b81654f1
MS
1053 {
1054 char *p = aclass;
1055
1056 QUIT;
36018d2e 1057 name = SYMBOL_NATURAL_NAME (sym_arr[ix]);
b81654f1
MS
1058 name += 2;
1059 if (p[0] && specialcmp(name, p) == 0)
d2e6263c 1060 continue; /* Seen this one already (not unique). */
b81654f1 1061
d2e6263c
MS
1062 /* Copy class part of method name. */
1063 while (*name && *name != ' ')
b81654f1
MS
1064 *p++ = *name++;
1065 *p++ = '\0';
d2e6263c
MS
1066 /* Print in columns. */
1067 puts_filtered_tabular(aclass, maxlen + 1, 0);
b81654f1
MS
1068 }
1069 begin_line();
1070 }
1071 else
1072 printf_filtered ("No classes matching \"%s\"\n", regexp ? regexp : "*");
1073}
1074
1075/*
1076 * Function: find_imps (char *selector, struct symbol **sym_arr)
1077 *
1078 * Input: a string representing a selector
1079 * a pointer to an array of symbol pointers
1080 * possibly a pointer to a symbol found by the caller.
1081 *
d2e6263c
MS
1082 * Output: number of methods that implement that selector. Side
1083 * effects: The array of symbol pointers is filled with matching syms.
b81654f1 1084 *
d2e6263c
MS
1085 * By analogy with function "find_methods" (symtab.c), builds a list
1086 * of symbols matching the ambiguous input, so that "decode_line_2"
1087 * (symtab.c) can list them and ask the user to choose one or more.
1088 * In this case the matches are objective c methods
1089 * ("implementations") matching an objective c selector.
b81654f1 1090 *
d2e6263c
MS
1091 * Note that it is possible for a normal (c-style) function to have
1092 * the same name as an objective c selector. To prevent the selector
1093 * from eclipsing the function, we allow the caller (decode_line_1) to
1094 * search for such a function first, and if it finds one, pass it in
1095 * to us. We will then integrate it into the list. We also search
1096 * for one here, among the minsyms.
b81654f1 1097 *
d2e6263c
MS
1098 * NOTE: if NUM_DEBUGGABLE is non-zero, the sym_arr will be divided
1099 * into two parts: debuggable (struct symbol) syms, and
1100 * non_debuggable (struct minimal_symbol) syms. The debuggable
1101 * ones will come first, before NUM_DEBUGGABLE (which will thus
1102 * be the index of the first non-debuggable one).
b81654f1
MS
1103 */
1104
1105/*
1106 * Function: total_number_of_imps (char *selector);
1107 *
1108 * Input: a string representing a selector
1109 * Output: number of methods that implement that selector.
1110 *
d2e6263c 1111 * By analogy with function "total_number_of_methods", this allows
b81654f1 1112 * decode_line_1 (symtab.c) to detect if there are objective c methods
d2e6263c
MS
1113 * matching the input, and to allocate an array of pointers to them
1114 * which can be manipulated by "decode_line_2" (also in symtab.c).
b81654f1
MS
1115 */
1116
1117char *
1118parse_selector (char *method, char **selector)
1119{
1120 char *s1 = NULL;
1121 char *s2 = NULL;
1122 int found_quote = 0;
1123
1124 char *nselector = NULL;
1125
e8f3fcdd 1126 gdb_assert (selector != NULL);
b81654f1
MS
1127
1128 s1 = method;
1129
1130 while (isspace (*s1))
1131 s1++;
1132 if (*s1 == '\'')
1133 {
1134 found_quote = 1;
1135 s1++;
1136 }
1137 while (isspace (*s1))
1138 s1++;
1139
1140 nselector = s1;
1141 s2 = s1;
1142
1143 for (;;) {
1144 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1145 *s1++ = *s2;
1146 else if (isspace (*s2))
1147 ;
1148 else if ((*s2 == '\0') || (*s2 == '\''))
1149 break;
1150 else
1151 return NULL;
1152 s2++;
1153 }
1154 *s1++ = '\0';
1155
1156 while (isspace (*s2))
1157 s2++;
1158 if (found_quote)
1159 {
1160 if (*s2 == '\'')
1161 s2++;
1162 while (isspace (*s2))
1163 s2++;
1164 }
1165
1166 if (selector != NULL)
1167 *selector = nselector;
1168
1169 return s2;
1170}
1171
1172char *
d2e6263c
MS
1173parse_method (char *method, char *type, char **class,
1174 char **category, char **selector)
b81654f1
MS
1175{
1176 char *s1 = NULL;
1177 char *s2 = NULL;
1178 int found_quote = 0;
1179
1180 char ntype = '\0';
1181 char *nclass = NULL;
1182 char *ncategory = NULL;
1183 char *nselector = NULL;
1184
e8f3fcdd
AC
1185 gdb_assert (type != NULL);
1186 gdb_assert (class != NULL);
1187 gdb_assert (category != NULL);
1188 gdb_assert (selector != NULL);
b81654f1
MS
1189
1190 s1 = method;
1191
1192 while (isspace (*s1))
1193 s1++;
1194 if (*s1 == '\'')
1195 {
1196 found_quote = 1;
1197 s1++;
1198 }
1199 while (isspace (*s1))
1200 s1++;
1201
1202 if ((s1[0] == '+') || (s1[0] == '-'))
1203 ntype = *s1++;
1204
1205 while (isspace (*s1))
1206 s1++;
1207
1208 if (*s1 != '[')
1209 return NULL;
1210 s1++;
1211
1212 nclass = s1;
1213 while (isalnum (*s1) || (*s1 == '_'))
1214 s1++;
1215
1216 s2 = s1;
1217 while (isspace (*s2))
1218 s2++;
1219
1220 if (*s2 == '(')
1221 {
1222 s2++;
1223 while (isspace (*s2))
1224 s2++;
1225 ncategory = s2;
1226 while (isalnum (*s2) || (*s2 == '_'))
1227 s2++;
1228 *s2++ = '\0';
1229 }
1230
d2e6263c 1231 /* Truncate the class name now that we're not using the open paren. */
b81654f1
MS
1232 *s1++ = '\0';
1233
1234 nselector = s2;
1235 s1 = s2;
1236
1237 for (;;) {
1238 if (isalnum (*s2) || (*s2 == '_') || (*s2 == ':'))
1239 *s1++ = *s2;
1240 else if (isspace (*s2))
1241 ;
1242 else if (*s2 == ']')
1243 break;
1244 else
1245 return NULL;
1246 s2++;
1247 }
1248 *s1++ = '\0';
1249 s2++;
1250
1251 while (isspace (*s2))
1252 s2++;
1253 if (found_quote)
1254 {
1255 if (*s2 != '\'')
1256 return NULL;
1257 s2++;
1258 while (isspace (*s2))
1259 s2++;
1260 }
1261
1262 if (type != NULL)
1263 *type = ntype;
1264 if (class != NULL)
1265 *class = nclass;
1266 if (category != NULL)
1267 *category = ncategory;
1268 if (selector != NULL)
1269 *selector = nselector;
1270
1271 return s2;
1272}
1273
2f9a90b4 1274static void
d2e6263c
MS
1275find_methods (struct symtab *symtab, char type,
1276 const char *class, const char *category,
1277 const char *selector, struct symbol **syms,
1278 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1279{
1280 struct objfile *objfile = NULL;
1281 struct minimal_symbol *msymbol = NULL;
1282 struct block *block = NULL;
1283 struct symbol *sym = NULL;
1284
1285 char *symname = NULL;
1286
1287 char ntype = '\0';
1288 char *nclass = NULL;
1289 char *ncategory = NULL;
1290 char *nselector = NULL;
1291
1292 unsigned int csym = 0;
1293 unsigned int cdebug = 0;
1294
1295 static char *tmp = NULL;
1296 static unsigned int tmplen = 0;
1297
e8f3fcdd
AC
1298 gdb_assert (nsym != NULL);
1299 gdb_assert (ndebug != NULL);
b81654f1
MS
1300
1301 if (symtab)
1302 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1303
1304 ALL_MSYMBOLS (objfile, msymbol)
1305 {
1306 QUIT;
1307
1308 if ((msymbol->type != mst_text) && (msymbol->type != mst_file_text))
d2e6263c 1309 /* Not a function or method. */
b81654f1
MS
1310 continue;
1311
1312 if (symtab)
8da065d5
DC
1313 if ((SYMBOL_VALUE_ADDRESS (msymbol) < BLOCK_START (block)) ||
1314 (SYMBOL_VALUE_ADDRESS (msymbol) >= BLOCK_END (block)))
d2e6263c 1315 /* Not in the specified symtab. */
b81654f1
MS
1316 continue;
1317
36018d2e 1318 symname = SYMBOL_NATURAL_NAME (msymbol);
b81654f1
MS
1319 if (symname == NULL)
1320 continue;
1321
1322 if ((symname[0] != '-' && symname[0] != '+') || (symname[1] != '['))
d2e6263c 1323 /* Not a method name. */
b81654f1
MS
1324 continue;
1325
1326 while ((strlen (symname) + 1) >= tmplen)
1327 {
1328 tmplen = (tmplen == 0) ? 1024 : tmplen * 2;
1329 tmp = xrealloc (tmp, tmplen);
1330 }
1331 strcpy (tmp, symname);
1332
1333 if (parse_method (tmp, &ntype, &nclass, &ncategory, &nselector) == NULL)
1334 continue;
1335
1336 if ((type != '\0') && (ntype != type))
1337 continue;
1338
d2e6263c
MS
1339 if ((class != NULL)
1340 && ((nclass == NULL) || (strcmp (class, nclass) != 0)))
b81654f1
MS
1341 continue;
1342
d2e6263c
MS
1343 if ((category != NULL) &&
1344 ((ncategory == NULL) || (strcmp (category, ncategory) != 0)))
b81654f1
MS
1345 continue;
1346
d2e6263c
MS
1347 if ((selector != NULL) &&
1348 ((nselector == NULL) || (strcmp (selector, nselector) != 0)))
b81654f1
MS
1349 continue;
1350
1351 sym = find_pc_function (SYMBOL_VALUE_ADDRESS (msymbol));
1352 if (sym != NULL)
1353 {
36018d2e 1354 const char *newsymname = SYMBOL_NATURAL_NAME (sym);
b81654f1 1355
b81654f1
MS
1356 if (strcmp (symname, newsymname) == 0)
1357 {
d2e6263c
MS
1358 /* Found a high-level method sym: swap it into the
1359 lower part of sym_arr (below num_debuggable). */
b81654f1
MS
1360 if (syms != NULL)
1361 {
1362 syms[csym] = syms[cdebug];
1363 syms[cdebug] = sym;
1364 }
1365 csym++;
1366 cdebug++;
1367 }
1368 else
1369 {
d2e6263c
MS
1370 warning (
1371"debugging symbol \"%s\" does not match minimal symbol (\"%s\"); ignoring",
b81654f1
MS
1372 newsymname, symname);
1373 if (syms != NULL)
1374 syms[csym] = (struct symbol *) msymbol;
1375 csym++;
1376 }
1377 }
1378 else
1379 {
d2e6263c 1380 /* Found a non-debuggable method symbol. */
b81654f1
MS
1381 if (syms != NULL)
1382 syms[csym] = (struct symbol *) msymbol;
1383 csym++;
1384 }
1385 }
1386
1387 if (nsym != NULL)
1388 *nsym = csym;
1389 if (ndebug != NULL)
1390 *ndebug = cdebug;
1391}
1392
1393char *find_imps (struct symtab *symtab, struct block *block,
d2e6263c
MS
1394 char *method, struct symbol **syms,
1395 unsigned int *nsym, unsigned int *ndebug)
b81654f1
MS
1396{
1397 char type = '\0';
1398 char *class = NULL;
1399 char *category = NULL;
1400 char *selector = NULL;
1401
1402 unsigned int csym = 0;
1403 unsigned int cdebug = 0;
1404
1405 unsigned int ncsym = 0;
1406 unsigned int ncdebug = 0;
1407
1408 char *buf = NULL;
1409 char *tmp = NULL;
1410
e8f3fcdd
AC
1411 gdb_assert (nsym != NULL);
1412 gdb_assert (ndebug != NULL);
b81654f1
MS
1413
1414 if (nsym != NULL)
1415 *nsym = 0;
1416 if (ndebug != NULL)
1417 *ndebug = 0;
1418
1419 buf = (char *) alloca (strlen (method) + 1);
1420 strcpy (buf, method);
1421 tmp = parse_method (buf, &type, &class, &category, &selector);
1422
1423 if (tmp == NULL) {
1424
b81654f1
MS
1425 struct symbol *sym = NULL;
1426 struct minimal_symbol *msym = NULL;
1427
1428 strcpy (buf, method);
1429 tmp = parse_selector (buf, &selector);
1430
1431 if (tmp == NULL)
1432 return NULL;
1433
cdef89d0 1434 sym = lookup_symbol (selector, block, VAR_DOMAIN, 0, NULL);
b81654f1
MS
1435 if (sym != NULL)
1436 {
1437 if (syms)
1438 syms[csym] = sym;
1439 csym++;
1440 cdebug++;
1441 }
1442
1443 if (sym == NULL)
1444 msym = lookup_minimal_symbol (selector, 0, 0);
1445
1446 if (msym != NULL)
1447 {
1448 if (syms)
36e53c63 1449 syms[csym] = (struct symbol *)msym;
b81654f1
MS
1450 csym++;
1451 }
1452 }
1453
1454 if (syms != NULL)
d2e6263c
MS
1455 find_methods (symtab, type, class, category, selector,
1456 syms + csym, &ncsym, &ncdebug);
b81654f1 1457 else
d2e6263c
MS
1458 find_methods (symtab, type, class, category, selector,
1459 NULL, &ncsym, &ncdebug);
b81654f1 1460
d2e6263c 1461 /* If we didn't find any methods, just return. */
b81654f1
MS
1462 if (ncsym == 0 && ncdebug == 0)
1463 return method;
1464
1465 /* Take debug symbols from the second batch of symbols and swap them
1466 * with debug symbols from the first batch. Repeat until either the
1467 * second section is out of debug symbols or the first section is
1468 * full of debug symbols. Either way we have all debug symbols
d2e6263c
MS
1469 * packed to the beginning of the buffer.
1470 */
b81654f1
MS
1471
1472 if (syms != NULL)
1473 {
1474 while ((cdebug < csym) && (ncdebug > 0))
1475 {
1476 struct symbol *s = NULL;
d2e6263c
MS
1477 /* First non-debugging symbol. */
1478 unsigned int i = cdebug;
1479 /* Last of second batch of debug symbols. */
1480 unsigned int j = csym + ncdebug - 1;
b81654f1
MS
1481
1482 s = syms[j];
1483 syms[j] = syms[i];
1484 syms[i] = s;
1485
d2e6263c
MS
1486 /* We've moved a symbol from the second debug section to the
1487 first one. */
b81654f1
MS
1488 cdebug++;
1489 ncdebug--;
1490 }
1491 }
1492
1493 csym += ncsym;
1494 cdebug += ncdebug;
1495
1496 if (nsym != NULL)
1497 *nsym = csym;
1498 if (ndebug != NULL)
1499 *ndebug = cdebug;
1500
1501 if (syms == NULL)
1502 return method + (tmp - buf);
1503
1504 if (csym > 1)
1505 {
d2e6263c 1506 /* Sort debuggable symbols. */
b81654f1 1507 if (cdebug > 1)
d2e6263c
MS
1508 qsort (syms, cdebug, sizeof (struct minimal_symbol *),
1509 compare_classes);
b81654f1 1510
d2e6263c 1511 /* Sort minimal_symbols. */
b81654f1 1512 if ((csym - cdebug) > 1)
d2e6263c
MS
1513 qsort (&syms[cdebug], csym - cdebug,
1514 sizeof (struct minimal_symbol *), compare_classes);
b81654f1 1515 }
d2e6263c
MS
1516 /* Terminate the sym_arr list. */
1517 syms[csym] = 0;
b81654f1
MS
1518
1519 return method + (tmp - buf);
1520}
1521
b9362cc7 1522static void
b81654f1
MS
1523print_object_command (char *args, int from_tty)
1524{
1525 struct value *object, *function, *description;
36e53c63 1526 CORE_ADDR string_addr, object_addr;
b81654f1
MS
1527 int i = 0;
1528 char c = -1;
1529
1530 if (!args || !*args)
d2e6263c
MS
1531 error (
1532"The 'print-object' command requires an argument (an Objective-C object)");
b81654f1
MS
1533
1534 {
1535 struct expression *expr = parse_expression (args);
f86f5ca3 1536 struct cleanup *old_chain =
d2e6263c 1537 make_cleanup (free_current_contents, &expr);
b81654f1
MS
1538 int pc = 0;
1539
5f9769d1
PH
1540 object = expr->language_defn->la_exp_desc->evaluate_exp
1541 (builtin_type_void_data_ptr, expr, &pc, EVAL_NORMAL);
b81654f1
MS
1542 do_cleanups (old_chain);
1543 }
1544
36e53c63
AF
1545 /* Validate the address for sanity. */
1546 object_addr = value_as_long (object);
1547 read_memory (object_addr, &c, 1);
1548
7248f48e 1549 function = find_function_in_inferior ("_NSPrintForDebugger");
36e53c63 1550 if (function == NULL)
b81654f1
MS
1551 error ("Unable to locate _NSPrintForDebugger in child process");
1552
1553 description = call_function_by_hand (function, 1, &object);
1554
7248f48e
AF
1555 string_addr = value_as_long (description);
1556 if (string_addr == 0)
b81654f1
MS
1557 error ("object returns null description");
1558
1559 read_memory (string_addr + i++, &c, 1);
1560 if (c != '\0')
1561 do
d2e6263c 1562 { /* Read and print characters up to EOS. */
b81654f1
MS
1563 QUIT;
1564 printf_filtered ("%c", c);
1565 read_memory (string_addr + i++, &c, 1);
1566 } while (c != 0);
1567 else
1568 printf_filtered("<object returns empty description>");
1569 printf_filtered ("\n");
1570}
1571
d2e6263c
MS
1572/* The data structure 'methcalls' is used to detect method calls (thru
1573 * ObjC runtime lib functions objc_msgSend, objc_msgSendSuper, etc.),
1574 * and ultimately find the method being called.
b81654f1
MS
1575 */
1576
1577struct objc_methcall {
1578 char *name;
d2e6263c 1579 /* Return instance method to be called. */
36e53c63 1580 int (*stop_at) (CORE_ADDR, CORE_ADDR *);
d2e6263c
MS
1581 /* Start of pc range corresponding to method invocation. */
1582 CORE_ADDR begin;
1583 /* End of pc range corresponding to method invocation. */
1584 CORE_ADDR end;
b81654f1
MS
1585};
1586
d2e6263c
MS
1587static int resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc);
1588static int resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
1589static int resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc);
1590static int resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc);
b81654f1
MS
1591
1592static struct objc_methcall methcalls[] = {
1593 { "_objc_msgSend", resolve_msgsend, 0, 0},
1594 { "_objc_msgSend_stret", resolve_msgsend_stret, 0, 0},
1595 { "_objc_msgSendSuper", resolve_msgsend_super, 0, 0},
1596 { "_objc_msgSendSuper_stret", resolve_msgsend_super_stret, 0, 0},
1597 { "_objc_getClass", NULL, 0, 0},
1598 { "_objc_getMetaClass", NULL, 0, 0}
1599};
1600
1601#define nmethcalls (sizeof (methcalls) / sizeof (methcalls[0]))
1602
d2e6263c
MS
1603/* The following function, "find_objc_msgsend", fills in the data
1604 * structure "objc_msgs" by finding the addresses of each of the
1605 * (currently four) functions that it holds (of which objc_msgSend is
1606 * the first). This must be called each time symbols are loaded, in
1607 * case the functions have moved for some reason.
b81654f1
MS
1608 */
1609
b9362cc7 1610static void
b81654f1
MS
1611find_objc_msgsend (void)
1612{
1613 unsigned int i;
1614 for (i = 0; i < nmethcalls; i++) {
1615
1616 struct minimal_symbol *func;
1617
d2e6263c 1618 /* Try both with and without underscore. */
b81654f1
MS
1619 func = lookup_minimal_symbol (methcalls[i].name, NULL, NULL);
1620 if ((func == NULL) && (methcalls[i].name[0] == '_')) {
1621 func = lookup_minimal_symbol (methcalls[i].name + 1, NULL, NULL);
1622 }
1623 if (func == NULL) {
1624 methcalls[i].begin = 0;
1625 methcalls[i].end = 0;
1626 continue;
1627 }
1628
1629 methcalls[i].begin = SYMBOL_VALUE_ADDRESS (func);
1630 do {
1631 methcalls[i].end = SYMBOL_VALUE_ADDRESS (++func);
1632 } while (methcalls[i].begin == methcalls[i].end);
1633 }
1634}
1635
1636/* find_objc_msgcall (replaces pc_off_limits)
1637 *
d2e6263c
MS
1638 * ALL that this function now does is to determine whether the input
1639 * address ("pc") is the address of one of the Objective-C message
b81654f1
MS
1640 * dispatch functions (mainly objc_msgSend or objc_msgSendSuper), and
1641 * if so, it returns the address of the method that will be called.
1642 *
1643 * The old function "pc_off_limits" used to do a lot of other things
d2e6263c 1644 * in addition, such as detecting shared library jump stubs and
b81654f1 1645 * returning the address of the shlib function that would be called.
d2e6263c
MS
1646 * That functionality has been moved into the SKIP_TRAMPOLINE_CODE and
1647 * IN_SOLIB_TRAMPOLINE macros, which are resolved in the target-
1648 * dependent modules.
b81654f1
MS
1649 */
1650
1651struct objc_submethod_helper_data {
36e53c63 1652 int (*f) (CORE_ADDR, CORE_ADDR *);
b81654f1
MS
1653 CORE_ADDR pc;
1654 CORE_ADDR *new_pc;
1655};
1656
b9362cc7 1657static int
7248f48e 1658find_objc_msgcall_submethod_helper (void * arg)
b81654f1 1659{
d2e6263c
MS
1660 struct objc_submethod_helper_data *s =
1661 (struct objc_submethod_helper_data *) arg;
1662
1663 if (s->f (s->pc, s->new_pc) == 0)
b81654f1 1664 return 1;
d2e6263c 1665 else
b81654f1 1666 return 0;
b81654f1
MS
1667}
1668
b9362cc7 1669static int
36e53c63 1670find_objc_msgcall_submethod (int (*f) (CORE_ADDR, CORE_ADDR *),
d2e6263c
MS
1671 CORE_ADDR pc,
1672 CORE_ADDR *new_pc)
b81654f1
MS
1673{
1674 struct objc_submethod_helper_data s;
1675
1676 s.f = f;
1677 s.pc = pc;
1678 s.new_pc = new_pc;
1679
1680 if (catch_errors (find_objc_msgcall_submethod_helper,
7248f48e 1681 (void *) &s,
d2e6263c
MS
1682 "Unable to determine target of Objective-C method call (ignoring):\n",
1683 RETURN_MASK_ALL) == 0)
b81654f1 1684 return 1;
d2e6263c 1685 else
b81654f1 1686 return 0;
b81654f1
MS
1687}
1688
1689int
1690find_objc_msgcall (CORE_ADDR pc, CORE_ADDR *new_pc)
1691{
1692 unsigned int i;
1693
1694 find_objc_msgsend ();
5e488a7b
AC
1695 if (new_pc != NULL)
1696 {
1697 *new_pc = 0;
1698 }
b81654f1 1699
d2e6263c
MS
1700 for (i = 0; i < nmethcalls; i++)
1701 if ((pc >= methcalls[i].begin) && (pc < methcalls[i].end))
1702 {
1703 if (methcalls[i].stop_at != NULL)
1704 return find_objc_msgcall_submethod (methcalls[i].stop_at,
1705 pc, new_pc);
1706 else
1707 return 0;
b81654f1 1708 }
d2e6263c 1709
b81654f1
MS
1710 return 0;
1711}
1712
a78f21af 1713extern initialize_file_ftype _initialize_objc_language; /* -Wmissing-prototypes */
b9362cc7 1714
b81654f1
MS
1715void
1716_initialize_objc_language (void)
1717{
1718 add_language (&objc_language_defn);
d2e6263c
MS
1719 add_info ("selectors", selectors_info, /* INFO SELECTORS command. */
1720 "All Objective-C selectors, or those matching REGEXP.");
1721 add_info ("classes", classes_info, /* INFO CLASSES command. */
1722 "All Objective-C classes, or those matching REGEXP.");
b81654f1 1723 add_com ("print-object", class_vars, print_object_command,
36e53c63 1724 "Ask an Objective-C object to print itself.");
b81654f1
MS
1725 add_com_alias ("po", "print-object", class_vars, 1);
1726}
1727
b81654f1
MS
1728static void
1729read_objc_method (CORE_ADDR addr, struct objc_method *method)
1730{
d2e6263c 1731 method->name = read_memory_unsigned_integer (addr + 0, 4);
b81654f1 1732 method->types = read_memory_unsigned_integer (addr + 4, 4);
d2e6263c 1733 method->imp = read_memory_unsigned_integer (addr + 8, 4);
b81654f1
MS
1734}
1735
1736static
1737unsigned long read_objc_methlist_nmethods (CORE_ADDR addr)
1738{
1739 return read_memory_unsigned_integer (addr + 4, 4);
1740}
1741
1742static void
1743read_objc_methlist_method (CORE_ADDR addr, unsigned long num,
1744 struct objc_method *method)
1745{
e8f3fcdd 1746 gdb_assert (num < read_objc_methlist_nmethods (addr));
b81654f1
MS
1747 read_objc_method (addr + 8 + (12 * num), method);
1748}
1749
1750static void
1751read_objc_object (CORE_ADDR addr, struct objc_object *object)
1752{
1753 object->isa = read_memory_unsigned_integer (addr, 4);
1754}
1755
1756static void
1757read_objc_super (CORE_ADDR addr, struct objc_super *super)
1758{
1759 super->receiver = read_memory_unsigned_integer (addr, 4);
1760 super->class = read_memory_unsigned_integer (addr + 4, 4);
1761};
1762
1763static void
1764read_objc_class (CORE_ADDR addr, struct objc_class *class)
1765{
1766 class->isa = read_memory_unsigned_integer (addr, 4);
1767 class->super_class = read_memory_unsigned_integer (addr + 4, 4);
1768 class->name = read_memory_unsigned_integer (addr + 8, 4);
1769 class->version = read_memory_unsigned_integer (addr + 12, 4);
1770 class->info = read_memory_unsigned_integer (addr + 16, 4);
1771 class->instance_size = read_memory_unsigned_integer (addr + 18, 4);
1772 class->ivars = read_memory_unsigned_integer (addr + 24, 4);
1773 class->methods = read_memory_unsigned_integer (addr + 28, 4);
1774 class->cache = read_memory_unsigned_integer (addr + 32, 4);
1775 class->protocols = read_memory_unsigned_integer (addr + 36, 4);
1776}
1777
b9362cc7 1778static CORE_ADDR
b81654f1
MS
1779find_implementation_from_class (CORE_ADDR class, CORE_ADDR sel)
1780{
1781 CORE_ADDR subclass = class;
1782
d2e6263c
MS
1783 while (subclass != 0)
1784 {
b81654f1 1785
d2e6263c
MS
1786 struct objc_class class_str;
1787 unsigned mlistnum = 0;
b81654f1 1788
d2e6263c 1789 read_objc_class (subclass, &class_str);
b81654f1 1790
d2e6263c
MS
1791 for (;;)
1792 {
1793 CORE_ADDR mlist;
1794 unsigned long nmethods;
1795 unsigned long i;
b81654f1 1796
d2e6263c
MS
1797 mlist = read_memory_unsigned_integer (class_str.methods +
1798 (4 * mlistnum), 4);
1799 if (mlist == 0)
1800 break;
b81654f1 1801
d2e6263c 1802 nmethods = read_objc_methlist_nmethods (mlist);
b81654f1 1803
d2e6263c
MS
1804 for (i = 0; i < nmethods; i++)
1805 {
1806 struct objc_method meth_str;
1807 read_objc_methlist_method (mlist, i, &meth_str);
b81654f1
MS
1808
1809#if 0
d2e6263c
MS
1810 fprintf (stderr,
1811 "checking method 0x%lx against selector 0x%lx\n",
1812 meth_str.name, sel);
b81654f1
MS
1813#endif
1814
d2e6263c 1815 if (meth_str.name == sel)
1abf022c
AF
1816 /* FIXME: hppa arch was doing a pointer dereference
1817 here. There needs to be a better way to do that. */
1818 return meth_str.imp;
d2e6263c
MS
1819 }
1820 mlistnum++;
b81654f1 1821 }
d2e6263c 1822 subclass = class_str.super_class;
b81654f1 1823 }
b81654f1
MS
1824
1825 return 0;
1826}
1827
b9362cc7 1828static CORE_ADDR
b81654f1
MS
1829find_implementation (CORE_ADDR object, CORE_ADDR sel)
1830{
1831 struct objc_object ostr;
1832
d2e6263c
MS
1833 if (object == 0)
1834 return 0;
b81654f1 1835 read_objc_object (object, &ostr);
d2e6263c
MS
1836 if (ostr.isa == 0)
1837 return 0;
b81654f1
MS
1838
1839 return find_implementation_from_class (ostr.isa, sel);
1840}
1841
a0273b2f
AF
1842#define OBJC_FETCH_POINTER_ARGUMENT(argi) \
1843 FETCH_POINTER_ARGUMENT (get_current_frame (), argi, builtin_type_void_func_ptr)
1844
b81654f1
MS
1845static int
1846resolve_msgsend (CORE_ADDR pc, CORE_ADDR *new_pc)
1847{
1848 CORE_ADDR object;
1849 CORE_ADDR sel;
1850 CORE_ADDR res;
1851
a0273b2f
AF
1852 object = OBJC_FETCH_POINTER_ARGUMENT (0);
1853 sel = OBJC_FETCH_POINTER_ARGUMENT (1);
b81654f1
MS
1854
1855 res = find_implementation (object, sel);
d2e6263c
MS
1856 if (new_pc != 0)
1857 *new_pc = res;
1858 if (res == 0)
1859 return 1;
b81654f1
MS
1860 return 0;
1861}
1862
1863static int
1864resolve_msgsend_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1865{
1866 CORE_ADDR object;
1867 CORE_ADDR sel;
1868 CORE_ADDR res;
1869
a0273b2f
AF
1870 object = OBJC_FETCH_POINTER_ARGUMENT (1);
1871 sel = OBJC_FETCH_POINTER_ARGUMENT (2);
b81654f1
MS
1872
1873 res = find_implementation (object, sel);
d2e6263c
MS
1874 if (new_pc != 0)
1875 *new_pc = res;
1876 if (res == 0)
1877 return 1;
b81654f1
MS
1878 return 0;
1879}
1880
1881static int
1882resolve_msgsend_super (CORE_ADDR pc, CORE_ADDR *new_pc)
1883{
1884 struct objc_super sstr;
1885
1886 CORE_ADDR super;
1887 CORE_ADDR sel;
1888 CORE_ADDR res;
1889
a0273b2f
AF
1890 super = OBJC_FETCH_POINTER_ARGUMENT (0);
1891 sel = OBJC_FETCH_POINTER_ARGUMENT (1);
b81654f1
MS
1892
1893 read_objc_super (super, &sstr);
d2e6263c
MS
1894 if (sstr.class == 0)
1895 return 0;
b81654f1
MS
1896
1897 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1898 if (new_pc != 0)
1899 *new_pc = res;
1900 if (res == 0)
1901 return 1;
b81654f1
MS
1902 return 0;
1903}
1904
1905static int
1906resolve_msgsend_super_stret (CORE_ADDR pc, CORE_ADDR *new_pc)
1907{
1908 struct objc_super sstr;
1909
1910 CORE_ADDR super;
1911 CORE_ADDR sel;
1912 CORE_ADDR res;
1913
a0273b2f
AF
1914 super = OBJC_FETCH_POINTER_ARGUMENT (1);
1915 sel = OBJC_FETCH_POINTER_ARGUMENT (2);
b81654f1
MS
1916
1917 read_objc_super (super, &sstr);
d2e6263c
MS
1918 if (sstr.class == 0)
1919 return 0;
b81654f1
MS
1920
1921 res = find_implementation_from_class (sstr.class, sel);
d2e6263c
MS
1922 if (new_pc != 0)
1923 *new_pc = res;
1924 if (res == 0)
1925 return 1;
b81654f1
MS
1926 return 0;
1927}