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