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