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