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