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