]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/jv-lang.c
* Makefile.in (c_lang.o, jv_lang.o, language.o): Add $(demangle_h).
[thirdparty/binutils-gdb.git] / gdb / jv-lang.c
1 /* Java language support routines for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "gdbtypes.h"
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdb_string.h"
32 #include "value.h"
33 #include "c-lang.h"
34 #include "jv-lang.h"
35 #include "gdbcore.h"
36 #include "block.h"
37 #include "demangle.h"
38 #include <ctype.h>
39
40 struct type *java_int_type;
41 struct type *java_byte_type;
42 struct type *java_short_type;
43 struct type *java_long_type;
44 struct type *java_boolean_type;
45 struct type *java_char_type;
46 struct type *java_float_type;
47 struct type *java_double_type;
48 struct type *java_void_type;
49
50 /* Local functions */
51
52 extern void _initialize_java_language (void);
53
54 static int java_demangled_signature_length (char *);
55 static void java_demangled_signature_copy (char *, char *);
56
57 static struct symtab *get_java_class_symtab (void);
58 static char *get_java_utf8_name (struct obstack *obstack, struct value *name);
59 static int java_class_is_primitive (struct value *clas);
60 static struct type *java_lookup_type (char *signature);
61 static struct value *java_value_string (char *ptr, int len);
62
63 static void java_emit_char (int c, struct ui_file * stream, int quoter);
64
65 /* This objfile contains symtabs that have been dynamically created
66 to record dynamically loaded Java classes and dynamically
67 compiled java methods. */
68
69 static struct objfile *dynamics_objfile = NULL;
70
71 static struct type *java_link_class_type (struct type *, struct value *);
72
73 /* FIXME: carlton/2003-02-04: This is the main or only caller of
74 allocate_objfile with first argument NULL; as a result, this code
75 breaks every so often. Somebody should write a test case that
76 exercises GDB in various ways (e.g. something involving loading a
77 dynamic library) after this code has been called. */
78
79 static struct objfile *
80 get_dynamics_objfile (void)
81 {
82 if (dynamics_objfile == NULL)
83 {
84 dynamics_objfile = allocate_objfile (NULL, 0);
85 }
86 return dynamics_objfile;
87 }
88
89 #if 1
90 /* symtab contains classes read from the inferior. */
91
92 static struct symtab *class_symtab = NULL;
93
94 /* Maximum number of class in class_symtab before relocation is needed. */
95
96 static int class_symtab_space;
97
98 static struct symtab *
99 get_java_class_symtab (void)
100 {
101 if (class_symtab == NULL)
102 {
103 struct objfile *objfile = get_dynamics_objfile ();
104 struct blockvector *bv;
105 struct block *bl;
106 class_symtab = allocate_symtab ("<java-classes>", objfile);
107 class_symtab->language = language_java;
108 bv = (struct blockvector *)
109 obstack_alloc (&objfile->symbol_obstack, sizeof (struct blockvector));
110 BLOCKVECTOR_NBLOCKS (bv) = 1;
111 BLOCKVECTOR (class_symtab) = bv;
112
113 /* Allocate dummy STATIC_BLOCK. */
114 bl = (struct block *)
115 obstack_alloc (&objfile->symbol_obstack, sizeof (struct block));
116 BLOCK_NSYMS (bl) = 0;
117 BLOCK_HASHTABLE (bl) = 0;
118 BLOCK_START (bl) = 0;
119 BLOCK_END (bl) = 0;
120 BLOCK_FUNCTION (bl) = NULL;
121 BLOCK_SUPERBLOCK (bl) = NULL;
122 BLOCK_GCC_COMPILED (bl) = 0;
123 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
124
125 /* Allocate GLOBAL_BLOCK. This has to be relocatable. */
126 class_symtab_space = 128;
127 bl = xmmalloc (objfile->md,
128 sizeof (struct block)
129 + ((class_symtab_space - 1) * sizeof (struct symbol *)));
130 *bl = *BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
131 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
132 class_symtab->free_ptr = (char *) bl;
133 }
134 return class_symtab;
135 }
136
137 static void
138 add_class_symtab_symbol (struct symbol *sym)
139 {
140 struct symtab *symtab = get_java_class_symtab ();
141 struct blockvector *bv = BLOCKVECTOR (symtab);
142 struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
143 if (BLOCK_NSYMS (bl) >= class_symtab_space)
144 {
145 /* Need to re-allocate. */
146 class_symtab_space *= 2;
147 bl = xmrealloc (symtab->objfile->md, bl,
148 sizeof (struct block)
149 + ((class_symtab_space - 1) * sizeof (struct symbol *)));
150 class_symtab->free_ptr = (char *) bl;
151 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
152 }
153
154 BLOCK_SYM (bl, BLOCK_NSYMS (bl)) = sym;
155 BLOCK_NSYMS (bl) = BLOCK_NSYMS (bl) + 1;
156 }
157
158 static struct symbol *add_class_symbol (struct type *type, CORE_ADDR addr);
159
160 static struct symbol *
161 add_class_symbol (struct type *type, CORE_ADDR addr)
162 {
163 struct symbol *sym;
164 sym = (struct symbol *)
165 obstack_alloc (&dynamics_objfile->symbol_obstack, sizeof (struct symbol));
166 memset (sym, 0, sizeof (struct symbol));
167 SYMBOL_LANGUAGE (sym) = language_java;
168 DEPRECATED_SYMBOL_NAME (sym) = TYPE_TAG_NAME (type);
169 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
170 /* SYMBOL_VALUE (sym) = valu; */
171 SYMBOL_TYPE (sym) = type;
172 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
173 SYMBOL_VALUE_ADDRESS (sym) = addr;
174 return sym;
175 }
176 #endif
177
178 struct type *
179 java_lookup_class (char *name)
180 {
181 struct symbol *sym;
182 sym = lookup_symbol (name, expression_context_block, STRUCT_NAMESPACE,
183 (int *) 0, (struct symtab **) NULL);
184 if (sym != NULL)
185 return SYMBOL_TYPE (sym);
186 #if 0
187 CORE_ADDR addr;
188 if (called from parser)
189 {
190 call lookup_class (or similar) in inferior;
191 if not
192 found:
193 return NULL;
194 addr = found in inferior;
195 }
196 else
197 addr = 0;
198 struct type *type;
199 type = alloc_type (objfile);
200 TYPE_CODE (type) = TYPE_CODE_STRUCT;
201 INIT_CPLUS_SPECIFIC (type);
202 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name), &objfile->type_obstack);
203 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
204 TYPE ? = addr;
205 return type;
206 #else
207 /* FIXME - should search inferior's symbol table. */
208 return NULL;
209 #endif
210 }
211
212 /* Return a nul-terminated string (allocated on OBSTACK) for
213 a name given by NAME (which has type Utf8Const*). */
214
215 char *
216 get_java_utf8_name (struct obstack *obstack, struct value *name)
217 {
218 char *chrs;
219 struct value *temp = name;
220 int name_length;
221 CORE_ADDR data_addr;
222 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
223 name_length = (int) value_as_long (temp);
224 data_addr = VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
225 + TYPE_LENGTH (VALUE_TYPE (temp));
226 chrs = obstack_alloc (obstack, name_length + 1);
227 chrs[name_length] = '\0';
228 read_memory (data_addr, chrs, name_length);
229 return chrs;
230 }
231
232 struct value *
233 java_class_from_object (struct value *obj_val)
234 {
235 /* This is all rather inefficient, since the offsets of vtable and
236 class are fixed. FIXME */
237 struct value *vtable_val;
238
239 if (TYPE_CODE (VALUE_TYPE (obj_val)) == TYPE_CODE_PTR
240 && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (obj_val))) == 0)
241 obj_val = value_at (get_java_object_type (),
242 value_as_address (obj_val), NULL);
243
244 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
245 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
246 }
247
248 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
249 static int
250 java_class_is_primitive (struct value *clas)
251 {
252 struct value *vtable = value_struct_elt (&clas, NULL, "vtable", NULL, "struct");
253 CORE_ADDR i = value_as_address (vtable);
254 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
255 }
256
257 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
258
259 struct type *
260 type_from_class (struct value *clas)
261 {
262 struct type *type;
263 char *name;
264 struct value *temp;
265 struct objfile *objfile;
266 struct value *utf8_name;
267 char *nptr;
268 CORE_ADDR addr;
269 struct block *bl;
270 int i;
271 int is_array = 0;
272
273 type = check_typedef (VALUE_TYPE (clas));
274 if (TYPE_CODE (type) == TYPE_CODE_PTR)
275 {
276 if (value_logical_not (clas))
277 return NULL;
278 clas = value_ind (clas);
279 }
280 addr = VALUE_ADDRESS (clas) + VALUE_OFFSET (clas);
281
282 #if 0
283 get_java_class_symtab ();
284 bl = BLOCKVECTOR_BLOCK (BLOCKVECTOR (class_symtab), GLOBAL_BLOCK);
285 for (i = BLOCK_NSYMS (bl); --i >= 0;)
286 {
287 struct symbol *sym = BLOCK_SYM (bl, i);
288 if (SYMBOL_VALUE_ADDRESS (sym) == addr)
289 return SYMBOL_TYPE (sym);
290 }
291 #endif
292
293 objfile = get_dynamics_objfile ();
294 if (java_class_is_primitive (clas))
295 {
296 struct value *sig;
297 temp = clas;
298 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
299 return java_primitive_type (value_as_long (sig));
300 }
301
302 /* Get Class name. */
303 /* if clasloader non-null, prepend loader address. FIXME */
304 temp = clas;
305 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
306 name = get_java_utf8_name (&objfile->type_obstack, utf8_name);
307 for (nptr = name; *nptr != 0; nptr++)
308 {
309 if (*nptr == '/')
310 *nptr = '.';
311 }
312
313 type = java_lookup_class (name);
314 if (type != NULL)
315 return type;
316
317 type = alloc_type (objfile);
318 TYPE_CODE (type) = TYPE_CODE_STRUCT;
319 INIT_CPLUS_SPECIFIC (type);
320
321 if (name[0] == '[')
322 {
323 char *signature = name;
324 int namelen = java_demangled_signature_length (signature);
325 if (namelen > strlen (name))
326 name = obstack_alloc (&objfile->type_obstack, namelen + 1);
327 java_demangled_signature_copy (name, signature);
328 name[namelen] = '\0';
329 is_array = 1;
330 temp = clas;
331 /* Set array element type. */
332 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
333 VALUE_TYPE (temp) = lookup_pointer_type (VALUE_TYPE (clas));
334 TYPE_TARGET_TYPE (type) = type_from_class (temp);
335 }
336
337 ALLOCATE_CPLUS_STRUCT_TYPE (type);
338 TYPE_TAG_NAME (type) = name;
339
340 add_class_symtab_symbol (add_class_symbol (type, addr));
341 return java_link_class_type (type, clas);
342 }
343
344 /* Fill in class TYPE with data from the CLAS value. */
345
346 struct type *
347 java_link_class_type (struct type *type, struct value *clas)
348 {
349 struct value *temp;
350 char *unqualified_name;
351 char *name = TYPE_TAG_NAME (type);
352 int ninterfaces, nfields, nmethods;
353 int type_is_object = 0;
354 struct fn_field *fn_fields;
355 struct fn_fieldlist *fn_fieldlists;
356 struct value *fields;
357 struct value *methods;
358 struct value *method = NULL;
359 struct value *field = NULL;
360 int i, j;
361 struct objfile *objfile = get_dynamics_objfile ();
362 struct type *tsuper;
363
364 unqualified_name = strrchr (name, '.');
365 if (unqualified_name == NULL)
366 unqualified_name = name;
367
368 temp = clas;
369 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
370 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
371 {
372 tsuper = get_java_object_type ();
373 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
374 tsuper = TYPE_TARGET_TYPE (tsuper);
375 type_is_object = 1;
376 }
377 else
378 tsuper = type_from_class (temp);
379
380 #if 1
381 ninterfaces = 0;
382 #else
383 temp = clas;
384 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
385 #endif
386 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
387 temp = clas;
388 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count", NULL, "structure"));
389 nfields += TYPE_N_BASECLASSES (type);
390 nfields++; /* Add one for dummy "class" field. */
391 TYPE_NFIELDS (type) = nfields;
392 TYPE_FIELDS (type) = (struct field *)
393 TYPE_ALLOC (type, sizeof (struct field) * nfields);
394
395 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
396
397 TYPE_FIELD_PRIVATE_BITS (type) =
398 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
399 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
400
401 TYPE_FIELD_PROTECTED_BITS (type) =
402 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
403 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
404
405 TYPE_FIELD_IGNORE_BITS (type) =
406 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
407 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
408
409 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
410 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
411 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
412
413 if (tsuper != NULL)
414 {
415 TYPE_BASECLASS (type, 0) = tsuper;
416 if (type_is_object)
417 SET_TYPE_FIELD_PRIVATE (type, 0);
418 }
419
420 i = strlen (name);
421 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
422 {
423 /* FIXME */
424 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
425 }
426 else
427 {
428 temp = clas;
429 temp = value_struct_elt (&temp, NULL, "size_in_bytes", NULL, "structure");
430 TYPE_LENGTH (type) = value_as_long (temp);
431 }
432
433 fields = NULL;
434 nfields--; /* First set up dummy "class" field. */
435 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields),
436 VALUE_ADDRESS (clas) + VALUE_OFFSET (clas));
437 TYPE_FIELD_NAME (type, nfields) = "class";
438 TYPE_FIELD_TYPE (type, nfields) = VALUE_TYPE (clas);
439 SET_TYPE_FIELD_PRIVATE (type, nfields);
440
441 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
442 {
443 int accflags;
444 int boffset;
445 if (fields == NULL)
446 {
447 temp = clas;
448 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
449 field = value_ind (fields);
450 }
451 else
452 { /* Re-use field value for next field. */
453 VALUE_ADDRESS (field) += TYPE_LENGTH (VALUE_TYPE (field));
454 VALUE_LAZY (field) = 1;
455 }
456 temp = field;
457 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
458 TYPE_FIELD_NAME (type, i) =
459 get_java_utf8_name (&objfile->type_obstack, temp);
460 temp = field;
461 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
462 NULL, "structure"));
463 temp = field;
464 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
465 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
466 NULL, "structure"));
467 if (accflags & 0x0001) /* public access */
468 {
469 /* ??? */
470 }
471 if (accflags & 0x0002) /* private access */
472 {
473 SET_TYPE_FIELD_PRIVATE (type, i);
474 }
475 if (accflags & 0x0004) /* protected access */
476 {
477 SET_TYPE_FIELD_PROTECTED (type, i);
478 }
479 if (accflags & 0x0008) /* ACC_STATIC */
480 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
481 else
482 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
483 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
484 {
485 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
486 }
487 else
488 {
489 struct type *ftype;
490 temp = field;
491 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
492 ftype = type_from_class (temp);
493 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
494 ftype = lookup_pointer_type (ftype);
495 TYPE_FIELD_TYPE (type, i) = ftype;
496 }
497 }
498
499 temp = clas;
500 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
501 NULL, "structure"));
502 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
503 j = nmethods * sizeof (struct fn_field);
504 fn_fields = (struct fn_field *)
505 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
506 memset (fn_fields, 0, j);
507 fn_fieldlists = (struct fn_fieldlist *)
508 alloca (nmethods * sizeof (struct fn_fieldlist));
509
510 methods = NULL;
511 for (i = 0; i < nmethods; i++)
512 {
513 char *mname;
514 int k;
515 if (methods == NULL)
516 {
517 temp = clas;
518 methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
519 method = value_ind (methods);
520 }
521 else
522 { /* Re-use method value for next method. */
523 VALUE_ADDRESS (method) += TYPE_LENGTH (VALUE_TYPE (method));
524 VALUE_LAZY (method) = 1;
525 }
526
527 /* Get method name. */
528 temp = method;
529 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
530 mname = get_java_utf8_name (&objfile->type_obstack, temp);
531 if (strcmp (mname, "<init>") == 0)
532 mname = unqualified_name;
533
534 /* Check for an existing method with the same name.
535 * This makes building the fn_fieldslists an O(nmethods**2)
536 * operation. That could be using hashing, but I doubt it
537 * is worth it. Note that we do maintain the order of methods
538 * in the inferior's Method table (as long as that is grouped
539 * by method name), which I think is desirable. --PB */
540 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
541 {
542 if (--j < 0)
543 { /* No match - new method name. */
544 j = TYPE_NFN_FIELDS (type)++;
545 fn_fieldlists[j].name = mname;
546 fn_fieldlists[j].length = 1;
547 fn_fieldlists[j].fn_fields = &fn_fields[i];
548 k = i;
549 break;
550 }
551 if (strcmp (mname, fn_fieldlists[j].name) == 0)
552 { /* Found an existing method with the same name. */
553 int l;
554 if (mname != unqualified_name)
555 obstack_free (&objfile->type_obstack, mname);
556 mname = fn_fieldlists[j].name;
557 fn_fieldlists[j].length++;
558 k = i - k; /* Index of new slot. */
559 /* Shift intervening fn_fields (between k and i) down. */
560 for (l = i; l > k; l--)
561 fn_fields[l] = fn_fields[l - 1];
562 for (l = TYPE_NFN_FIELDS (type); --l > j;)
563 fn_fieldlists[l].fn_fields++;
564 break;
565 }
566 k += fn_fieldlists[j].length;
567 }
568 fn_fields[k].physname = "";
569 fn_fields[k].is_stub = 1;
570 fn_fields[k].type = make_function_type (java_void_type, NULL); /* FIXME */
571 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
572 }
573
574 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
575 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
576 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
577 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
578
579 return type;
580 }
581
582 static struct type *java_object_type;
583
584 struct type *
585 get_java_object_type (void)
586 {
587 if (java_object_type == NULL)
588 {
589 struct symbol *sym;
590 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_NAMESPACE,
591 (int *) 0, (struct symtab **) NULL);
592 if (sym == NULL)
593 error ("cannot find java.lang.Object");
594 java_object_type = SYMBOL_TYPE (sym);
595 }
596 return java_object_type;
597 }
598
599 int
600 get_java_object_header_size (void)
601 {
602 struct type *objtype = get_java_object_type ();
603 if (objtype == NULL)
604 return (2 * TARGET_PTR_BIT / TARGET_CHAR_BIT);
605 else
606 return TYPE_LENGTH (objtype);
607 }
608
609 int
610 is_object_type (struct type *type)
611 {
612 CHECK_TYPEDEF (type);
613 if (TYPE_CODE (type) == TYPE_CODE_PTR)
614 {
615 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
616 char *name;
617 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
618 return 0;
619 while (TYPE_N_BASECLASSES (ttype) > 0)
620 ttype = TYPE_BASECLASS (ttype, 0);
621 name = TYPE_TAG_NAME (ttype);
622 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
623 return 1;
624 name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
625 if (name != NULL && strcmp (name, "vtable") == 0)
626 {
627 if (java_object_type == NULL)
628 java_object_type = type;
629 return 1;
630 }
631 }
632 return 0;
633 }
634
635 struct type *
636 java_primitive_type (int signature)
637 {
638 switch (signature)
639 {
640 case 'B':
641 return java_byte_type;
642 case 'S':
643 return java_short_type;
644 case 'I':
645 return java_int_type;
646 case 'J':
647 return java_long_type;
648 case 'Z':
649 return java_boolean_type;
650 case 'C':
651 return java_char_type;
652 case 'F':
653 return java_float_type;
654 case 'D':
655 return java_double_type;
656 case 'V':
657 return java_void_type;
658 }
659 error ("unknown signature '%c' for primitive type", (char) signature);
660 }
661
662 /* If name[0 .. namelen-1] is the name of a primitive Java type,
663 return that type. Otherwise, return NULL. */
664
665 struct type *
666 java_primitive_type_from_name (char *name, int namelen)
667 {
668 switch (name[0])
669 {
670 case 'b':
671 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
672 return java_byte_type;
673 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
674 return java_boolean_type;
675 break;
676 case 'c':
677 if (namelen == 4 && memcmp (name, "char", 4) == 0)
678 return java_char_type;
679 case 'd':
680 if (namelen == 6 && memcmp (name, "double", 6) == 0)
681 return java_double_type;
682 break;
683 case 'f':
684 if (namelen == 5 && memcmp (name, "float", 5) == 0)
685 return java_float_type;
686 break;
687 case 'i':
688 if (namelen == 3 && memcmp (name, "int", 3) == 0)
689 return java_int_type;
690 break;
691 case 'l':
692 if (namelen == 4 && memcmp (name, "long", 4) == 0)
693 return java_long_type;
694 break;
695 case 's':
696 if (namelen == 5 && memcmp (name, "short", 5) == 0)
697 return java_short_type;
698 break;
699 case 'v':
700 if (namelen == 4 && memcmp (name, "void", 4) == 0)
701 return java_void_type;
702 break;
703 }
704 return NULL;
705 }
706
707 /* Return the length (in bytes) of demangled name of the Java type
708 signature string SIGNATURE. */
709
710 static int
711 java_demangled_signature_length (char *signature)
712 {
713 int array = 0;
714 for (; *signature == '['; signature++)
715 array += 2; /* Two chars for "[]". */
716 switch (signature[0])
717 {
718 case 'L':
719 /* Subtract 2 for 'L' and ';'. */
720 return strlen (signature) - 2 + array;
721 default:
722 return strlen (TYPE_NAME (java_primitive_type (signature[0]))) + array;
723 }
724 }
725
726 /* Demangle the Java type signature SIGNATURE, leaving the result in RESULT. */
727
728 static void
729 java_demangled_signature_copy (char *result, char *signature)
730 {
731 int array = 0;
732 char *ptr;
733 int i;
734 while (*signature == '[')
735 {
736 array++;
737 signature++;
738 }
739 switch (signature[0])
740 {
741 case 'L':
742 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
743 signature++;
744 ptr = result;
745 for (; *signature != ';' && *signature != '\0'; signature++)
746 {
747 if (*signature == '/')
748 *ptr++ = '.';
749 else
750 *ptr++ = *signature;
751 }
752 break;
753 default:
754 ptr = TYPE_NAME (java_primitive_type (signature[0]));
755 i = strlen (ptr);
756 strcpy (result, ptr);
757 ptr = result + i;
758 break;
759 }
760 while (--array >= 0)
761 {
762 *ptr++ = '[';
763 *ptr++ = ']';
764 }
765 }
766
767 /* Return the demangled name of the Java type signature string SIGNATURE,
768 as a freshly allocated copy. */
769
770 char *
771 java_demangle_type_signature (char *signature)
772 {
773 int length = java_demangled_signature_length (signature);
774 char *result = xmalloc (length + 1);
775 java_demangled_signature_copy (result, signature);
776 result[length] = '\0';
777 return result;
778 }
779
780 struct type *
781 java_lookup_type (char *signature)
782 {
783 switch (signature[0])
784 {
785 case 'L':
786 case '[':
787 error ("java_lookup_type not fully implemented");
788 default:
789 return java_primitive_type (signature[0]);
790 }
791 }
792
793 /* Return the type of TYPE followed by DIMS pairs of [ ].
794 If DIMS == 0, TYPE is returned. */
795
796 struct type *
797 java_array_type (struct type *type, int dims)
798 {
799 struct type *range_type;
800
801 while (dims-- > 0)
802 {
803 range_type = create_range_type (NULL, builtin_type_int, 0, 0);
804 /* FIXME This is bogus! Java arrays are not gdb arrays! */
805 type = create_array_type (NULL, type, range_type);
806 }
807
808 return type;
809 }
810
811 /* Create a Java string in the inferior from a (Utf8) literal. */
812
813 static struct value *
814 java_value_string (char *ptr, int len)
815 {
816 error ("not implemented - java_value_string"); /* FIXME */
817 }
818
819 /* Print the character C on STREAM as part of the contents of a literal
820 string whose delimiter is QUOTER. Note that that format for printing
821 characters and strings is language specific. */
822
823 static void
824 java_emit_char (int c, struct ui_file *stream, int quoter)
825 {
826 switch (c)
827 {
828 case '\\':
829 case '\'':
830 fprintf_filtered (stream, "\\%c", c);
831 break;
832 case '\b':
833 fputs_filtered ("\\b", stream);
834 break;
835 case '\t':
836 fputs_filtered ("\\t", stream);
837 break;
838 case '\n':
839 fputs_filtered ("\\n", stream);
840 break;
841 case '\f':
842 fputs_filtered ("\\f", stream);
843 break;
844 case '\r':
845 fputs_filtered ("\\r", stream);
846 break;
847 default:
848 if (isprint (c))
849 fputc_filtered (c, stream);
850 else
851 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
852 break;
853 }
854 }
855
856 static struct value *
857 evaluate_subexp_java (struct type *expect_type, register struct expression *exp,
858 register int *pos, enum noside noside)
859 {
860 int pc = *pos;
861 int i;
862 char *name;
863 enum exp_opcode op = exp->elts[*pos].opcode;
864 struct value *arg1;
865 struct value *arg2;
866 struct type *type;
867 switch (op)
868 {
869 case UNOP_IND:
870 if (noside == EVAL_SKIP)
871 goto standard;
872 (*pos)++;
873 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
874 if (is_object_type (VALUE_TYPE (arg1)))
875 {
876 struct type *type;
877
878 type = type_from_class (java_class_from_object (arg1));
879 arg1 = value_cast (lookup_pointer_type (type), arg1);
880 }
881 if (noside == EVAL_SKIP)
882 goto nosideret;
883 return value_ind (arg1);
884
885 case BINOP_SUBSCRIPT:
886 (*pos)++;
887 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
888 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
889 if (noside == EVAL_SKIP)
890 goto nosideret;
891 /* If the user attempts to subscript something that is not an
892 array or pointer type (like a plain int variable for example),
893 then report this as an error. */
894
895 COERCE_REF (arg1);
896 type = check_typedef (VALUE_TYPE (arg1));
897 if (TYPE_CODE (type) == TYPE_CODE_PTR)
898 type = check_typedef (TYPE_TARGET_TYPE (type));
899 name = TYPE_NAME (type);
900 if (name == NULL)
901 name = TYPE_TAG_NAME (type);
902 i = name == NULL ? 0 : strlen (name);
903 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
904 && i > 2 && name[i - 1] == ']')
905 {
906 CORE_ADDR address;
907 long length, index;
908 struct type *el_type;
909 char buf4[4];
910
911 struct value *clas = java_class_from_object (arg1);
912 struct value *temp = clas;
913 /* Get CLASS_ELEMENT_TYPE of the array type. */
914 temp = value_struct_elt (&temp, NULL, "methods",
915 NULL, "structure");
916 VALUE_TYPE (temp) = VALUE_TYPE (clas);
917 el_type = type_from_class (temp);
918 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
919 el_type = lookup_pointer_type (el_type);
920
921 if (noside == EVAL_AVOID_SIDE_EFFECTS)
922 return value_zero (el_type, VALUE_LVAL (arg1));
923 address = value_as_address (arg1);
924 address += JAVA_OBJECT_SIZE;
925 read_memory (address, buf4, 4);
926 length = (long) extract_signed_integer (buf4, 4);
927 index = (long) value_as_long (arg2);
928 if (index >= length || index < 0)
929 error ("array index (%ld) out of bounds (length: %ld)",
930 index, length);
931 address = (address + 4) + index * TYPE_LENGTH (el_type);
932 return value_at (el_type, address, NULL);
933 }
934 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
935 {
936 if (noside == EVAL_AVOID_SIDE_EFFECTS)
937 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
938 else
939 return value_subscript (arg1, arg2);
940 }
941 if (name)
942 error ("cannot subscript something of type `%s'", name);
943 else
944 error ("cannot subscript requested type");
945
946 case OP_STRING:
947 (*pos)++;
948 i = longest_to_int (exp->elts[pc + 1].longconst);
949 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
950 if (noside == EVAL_SKIP)
951 goto nosideret;
952 return java_value_string (&exp->elts[pc + 2].string, i);
953
954 case STRUCTOP_STRUCT:
955 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
956 /* Convert object field (such as TYPE.class) to reference. */
957 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT)
958 arg1 = value_addr (arg1);
959 return arg1;
960 default:
961 break;
962 }
963 standard:
964 return evaluate_subexp_standard (expect_type, exp, pos, noside);
965 nosideret:
966 return value_from_longest (builtin_type_long, (LONGEST) 1);
967 }
968
969 static struct type *
970 java_create_fundamental_type (struct objfile *objfile, int typeid)
971 {
972 switch (typeid)
973 {
974 case FT_VOID:
975 return java_void_type;
976 case FT_BOOLEAN:
977 return java_boolean_type;
978 case FT_CHAR:
979 return java_char_type;
980 case FT_FLOAT:
981 return java_float_type;
982 case FT_DBL_PREC_FLOAT:
983 return java_double_type;
984 case FT_BYTE:
985 case FT_SIGNED_CHAR:
986 return java_byte_type;
987 case FT_SHORT:
988 case FT_SIGNED_SHORT:
989 return java_short_type;
990 case FT_INTEGER:
991 case FT_SIGNED_INTEGER:
992 return java_int_type;
993 case FT_LONG:
994 case FT_SIGNED_LONG:
995 return java_long_type;
996 }
997 return c_create_fundamental_type (objfile, typeid);
998 }
999
1000 static char *java_demangle (const char *mangled, int options)
1001 {
1002 return cplus_demangle (mangled, options | DMGL_JAVA);
1003 }
1004
1005
1006 /* Table mapping opcodes into strings for printing operators
1007 and precedences of the operators. */
1008
1009 const struct op_print java_op_print_tab[] =
1010 {
1011 {",", BINOP_COMMA, PREC_COMMA, 0},
1012 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1013 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1014 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1015 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1016 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1017 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1018 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1019 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1020 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1021 {">=", BINOP_GEQ, PREC_ORDER, 0},
1022 {">", BINOP_GTR, PREC_ORDER, 0},
1023 {"<", BINOP_LESS, PREC_ORDER, 0},
1024 {">>", BINOP_RSH, PREC_SHIFT, 0},
1025 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1026 #if 0
1027 {">>>", BINOP_ ? ? ?, PREC_SHIFT, 0},
1028 #endif
1029 {"+", BINOP_ADD, PREC_ADD, 0},
1030 {"-", BINOP_SUB, PREC_ADD, 0},
1031 {"*", BINOP_MUL, PREC_MUL, 0},
1032 {"/", BINOP_DIV, PREC_MUL, 0},
1033 {"%", BINOP_REM, PREC_MUL, 0},
1034 {"-", UNOP_NEG, PREC_PREFIX, 0},
1035 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1036 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1037 {"*", UNOP_IND, PREC_PREFIX, 0},
1038 #if 0
1039 {"instanceof", ? ? ?, ? ? ?, 0},
1040 #endif
1041 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1042 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1043 {NULL, 0, 0, 0}
1044 };
1045
1046 const struct language_defn java_language_defn =
1047 {
1048 "java", /* Language name */
1049 language_java,
1050 c_builtin_types,
1051 range_check_off,
1052 type_check_off,
1053 case_sensitive_on,
1054 java_parse,
1055 java_error,
1056 evaluate_subexp_java,
1057 c_printchar, /* Print a character constant */
1058 c_printstr, /* Function to print string constant */
1059 java_emit_char, /* Function to print a single character */
1060 java_create_fundamental_type, /* Create fundamental type in this language */
1061 java_print_type, /* Print a type using appropriate syntax */
1062 java_val_print, /* Print a value using appropriate syntax */
1063 java_value_print, /* Print a top-level value */
1064 NULL, /* Language specific skip_trampoline */
1065 java_demangle, /* Language specific symbol demangler */
1066 {"", "", "", ""}, /* Binary format info */
1067 {"0%lo", "0", "o", ""}, /* Octal format info */
1068 {"%ld", "", "d", ""}, /* Decimal format info */
1069 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1070 java_op_print_tab, /* expression operators for printing */
1071 0, /* not c-style arrays */
1072 0, /* String lower bound */
1073 &builtin_type_char, /* Type of string elements */
1074 LANG_MAGIC
1075 };
1076
1077 void
1078 _initialize_java_language (void)
1079 {
1080
1081 java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
1082 java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
1083 java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
1084 java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
1085 java_boolean_type = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
1086 java_char_type = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
1087 java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
1088 java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
1089 java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
1090
1091 add_language (&java_language_defn);
1092 }
1093
1094 /* Cleanup code that should be run on every "run".
1095 We should use make_run_cleanup to have this be called.
1096 But will that mess up values in value histry? FIXME */
1097
1098 extern void java_rerun_cleanup (void);
1099 void
1100 java_rerun_cleanup (void)
1101 {
1102 if (class_symtab != NULL)
1103 {
1104 free_symtab (class_symtab); /* ??? */
1105 class_symtab = NULL;
1106 }
1107 if (dynamics_objfile != NULL)
1108 {
1109 free_objfile (dynamics_objfile);
1110 dynamics_objfile = NULL;
1111 }
1112
1113 java_object_type = NULL;
1114 }