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