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