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