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