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