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