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