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