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