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