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