1 /* Java language support routines for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "expression.h"
25 #include "parser-defs.h"
31 #include "gdb_string.h"
38 #include "dictionary.h"
41 struct type *java_int_type;
42 struct type *java_byte_type;
43 struct type *java_short_type;
44 struct type *java_long_type;
45 struct type *java_boolean_type;
46 struct type *java_char_type;
47 struct type *java_float_type;
48 struct type *java_double_type;
49 struct type *java_void_type;
53 extern void _initialize_java_language (void);
55 static int java_demangled_signature_length (char *);
56 static void java_demangled_signature_copy (char *, char *);
58 static struct symtab *get_java_class_symtab (void);
59 static char *get_java_utf8_name (struct obstack *obstack, struct value *name);
60 static int java_class_is_primitive (struct value *clas);
61 static struct value *java_value_string (char *ptr, int len);
63 static void java_emit_char (int c, struct ui_file * stream, int quoter);
65 static char *java_class_name_from_physname (const char *physname);
67 /* This objfile contains symtabs that have been dynamically created
68 to record dynamically loaded Java classes and dynamically
69 compiled java methods. */
71 static struct objfile *dynamics_objfile = NULL;
73 static struct type *java_link_class_type (struct type *, struct value *);
75 /* FIXME: carlton/2003-02-04: This is the main or only caller of
76 allocate_objfile with first argument NULL; as a result, this code
77 breaks every so often. Somebody should write a test case that
78 exercises GDB in various ways (e.g. something involving loading a
79 dynamic library) after this code has been called. */
81 static struct objfile *
82 get_dynamics_objfile (void)
84 if (dynamics_objfile == NULL)
86 dynamics_objfile = allocate_objfile (NULL, 0);
88 return dynamics_objfile;
92 /* symtab contains classes read from the inferior. */
94 static struct symtab *class_symtab = NULL;
96 static void free_class_block (struct symtab *symtab);
98 static struct symtab *
99 get_java_class_symtab (void)
101 if (class_symtab == NULL)
103 struct objfile *objfile = get_dynamics_objfile ();
104 struct blockvector *bv;
106 class_symtab = allocate_symtab ("<java-classes>", objfile);
107 class_symtab->language = language_java;
108 bv = (struct blockvector *)
109 obstack_alloc (&objfile->objfile_obstack,
110 sizeof (struct blockvector) + sizeof (struct block *));
111 BLOCKVECTOR_NBLOCKS (bv) = 1;
112 BLOCKVECTOR (class_symtab) = bv;
114 /* Allocate dummy STATIC_BLOCK. */
115 bl = allocate_block (&objfile->objfile_obstack);
116 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
118 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
120 /* Allocate GLOBAL_BLOCK. */
121 bl = allocate_block (&objfile->objfile_obstack);
122 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
123 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
124 class_symtab->free_func = free_class_block;
130 add_class_symtab_symbol (struct symbol *sym)
132 struct symtab *symtab = get_java_class_symtab ();
133 struct blockvector *bv = BLOCKVECTOR (symtab);
134 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
137 static struct symbol *add_class_symbol (struct type *type, CORE_ADDR addr);
139 static struct symbol *
140 add_class_symbol (struct type *type, CORE_ADDR addr)
143 sym = (struct symbol *)
144 obstack_alloc (&dynamics_objfile->objfile_obstack, sizeof (struct symbol));
145 memset (sym, 0, sizeof (struct symbol));
146 SYMBOL_LANGUAGE (sym) = language_java;
147 DEPRECATED_SYMBOL_NAME (sym) = TYPE_TAG_NAME (type);
148 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
149 /* SYMBOL_VALUE (sym) = valu; */
150 SYMBOL_TYPE (sym) = type;
151 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
152 SYMBOL_VALUE_ADDRESS (sym) = addr;
156 /* Free the dynamic symbols block. */
158 free_class_block (struct symtab *symtab)
160 struct blockvector *bv = BLOCKVECTOR (symtab);
161 struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
163 dict_free (BLOCK_DICT (bl));
168 java_lookup_class (char *name)
171 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN,
172 (int *) 0, (struct symtab **) NULL);
174 return SYMBOL_TYPE (sym);
177 if (called from parser)
179 call lookup_class (or similar) in inferior;
183 addr = found in inferior;
188 type = alloc_type (objfile);
189 TYPE_CODE (type) = TYPE_CODE_STRUCT;
190 INIT_CPLUS_SPECIFIC (type);
191 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name), &objfile->objfile_obstack);
192 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
196 /* FIXME - should search inferior's symbol table. */
201 /* Return a nul-terminated string (allocated on OBSTACK) for
202 a name given by NAME (which has type Utf8Const*). */
205 get_java_utf8_name (struct obstack *obstack, struct value *name)
208 struct value *temp = name;
211 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
212 name_length = (int) value_as_long (temp);
213 data_addr = VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
214 + TYPE_LENGTH (VALUE_TYPE (temp));
215 chrs = obstack_alloc (obstack, name_length + 1);
216 chrs[name_length] = '\0';
217 read_memory (data_addr, chrs, name_length);
222 java_class_from_object (struct value *obj_val)
224 /* This is all rather inefficient, since the offsets of vtable and
225 class are fixed. FIXME */
226 struct value *vtable_val;
228 if (TYPE_CODE (VALUE_TYPE (obj_val)) == TYPE_CODE_PTR
229 && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (obj_val))) == 0)
230 obj_val = value_at (get_java_object_type (),
231 value_as_address (obj_val), NULL);
233 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
234 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
237 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
239 java_class_is_primitive (struct value *clas)
241 struct value *vtable = value_struct_elt (&clas, NULL, "vtable", NULL, "struct");
242 CORE_ADDR i = value_as_address (vtable);
243 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
246 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
249 type_from_class (struct value *clas)
254 struct objfile *objfile;
255 struct value *utf8_name;
259 struct dict_iterator iter;
262 type = check_typedef (VALUE_TYPE (clas));
263 if (TYPE_CODE (type) == TYPE_CODE_PTR)
265 if (value_logical_not (clas))
267 clas = value_ind (clas);
269 addr = VALUE_ADDRESS (clas) + VALUE_OFFSET (clas);
272 get_java_class_symtab ();
273 bl = BLOCKVECTOR_BLOCK (BLOCKVECTOR (class_symtab), GLOBAL_BLOCK);
274 ALL_BLOCK_SYMBOLS (block, iter, sym)
276 if (SYMBOL_VALUE_ADDRESS (sym) == addr)
277 return SYMBOL_TYPE (sym);
281 objfile = get_dynamics_objfile ();
282 if (java_class_is_primitive (clas))
286 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
287 return java_primitive_type (value_as_long (sig));
290 /* Get Class name. */
291 /* if clasloader non-null, prepend loader address. FIXME */
293 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
294 name = get_java_utf8_name (&objfile->objfile_obstack, utf8_name);
295 for (nptr = name; *nptr != 0; nptr++)
301 type = java_lookup_class (name);
305 type = alloc_type (objfile);
306 TYPE_CODE (type) = TYPE_CODE_STRUCT;
307 INIT_CPLUS_SPECIFIC (type);
311 char *signature = name;
312 int namelen = java_demangled_signature_length (signature);
313 if (namelen > strlen (name))
314 name = obstack_alloc (&objfile->objfile_obstack, namelen + 1);
315 java_demangled_signature_copy (name, signature);
316 name[namelen] = '\0';
319 /* Set array element type. */
320 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
321 VALUE_TYPE (temp) = lookup_pointer_type (VALUE_TYPE (clas));
322 TYPE_TARGET_TYPE (type) = type_from_class (temp);
325 ALLOCATE_CPLUS_STRUCT_TYPE (type);
326 TYPE_TAG_NAME (type) = name;
328 add_class_symtab_symbol (add_class_symbol (type, addr));
329 return java_link_class_type (type, clas);
332 /* Fill in class TYPE with data from the CLAS value. */
335 java_link_class_type (struct type *type, struct value *clas)
338 char *unqualified_name;
339 char *name = TYPE_TAG_NAME (type);
340 int ninterfaces, nfields, nmethods;
341 int type_is_object = 0;
342 struct fn_field *fn_fields;
343 struct fn_fieldlist *fn_fieldlists;
344 struct value *fields;
345 struct value *methods;
346 struct value *method = NULL;
347 struct value *field = NULL;
349 struct objfile *objfile = get_dynamics_objfile ();
352 unqualified_name = strrchr (name, '.');
353 if (unqualified_name == NULL)
354 unqualified_name = name;
357 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
358 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
360 tsuper = get_java_object_type ();
361 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
362 tsuper = TYPE_TARGET_TYPE (tsuper);
366 tsuper = type_from_class (temp);
372 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
374 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
376 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count", NULL, "structure"));
377 nfields += TYPE_N_BASECLASSES (type);
378 nfields++; /* Add one for dummy "class" field. */
379 TYPE_NFIELDS (type) = nfields;
380 TYPE_FIELDS (type) = (struct field *)
381 TYPE_ALLOC (type, sizeof (struct field) * nfields);
383 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
385 TYPE_FIELD_PRIVATE_BITS (type) =
386 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
387 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
389 TYPE_FIELD_PROTECTED_BITS (type) =
390 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
391 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
393 TYPE_FIELD_IGNORE_BITS (type) =
394 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
395 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
397 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
398 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
399 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
403 TYPE_BASECLASS (type, 0) = tsuper;
405 SET_TYPE_FIELD_PRIVATE (type, 0);
409 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
412 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
417 temp = value_struct_elt (&temp, NULL, "size_in_bytes", NULL, "structure");
418 TYPE_LENGTH (type) = value_as_long (temp);
422 nfields--; /* First set up dummy "class" field. */
423 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields),
424 VALUE_ADDRESS (clas) + VALUE_OFFSET (clas));
425 TYPE_FIELD_NAME (type, nfields) = "class";
426 TYPE_FIELD_TYPE (type, nfields) = VALUE_TYPE (clas);
427 SET_TYPE_FIELD_PRIVATE (type, nfields);
429 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
436 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
437 field = value_ind (fields);
440 { /* Re-use field value for next field. */
441 VALUE_ADDRESS (field) += TYPE_LENGTH (VALUE_TYPE (field));
442 VALUE_LAZY (field) = 1;
445 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
446 TYPE_FIELD_NAME (type, i) =
447 get_java_utf8_name (&objfile->objfile_obstack, temp);
449 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
452 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
453 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
455 if (accflags & 0x0001) /* public access */
459 if (accflags & 0x0002) /* private access */
461 SET_TYPE_FIELD_PRIVATE (type, i);
463 if (accflags & 0x0004) /* protected access */
465 SET_TYPE_FIELD_PROTECTED (type, i);
467 if (accflags & 0x0008) /* ACC_STATIC */
468 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
470 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
471 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
473 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
479 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
480 ftype = type_from_class (temp);
481 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
482 ftype = lookup_pointer_type (ftype);
483 TYPE_FIELD_TYPE (type, i) = ftype;
488 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
490 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
491 j = nmethods * sizeof (struct fn_field);
492 fn_fields = (struct fn_field *)
493 obstack_alloc (&dynamics_objfile->objfile_obstack, j);
494 memset (fn_fields, 0, j);
495 fn_fieldlists = (struct fn_fieldlist *)
496 alloca (nmethods * sizeof (struct fn_fieldlist));
499 for (i = 0; i < nmethods; i++)
506 methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
507 method = value_ind (methods);
510 { /* Re-use method value for next method. */
511 VALUE_ADDRESS (method) += TYPE_LENGTH (VALUE_TYPE (method));
512 VALUE_LAZY (method) = 1;
515 /* Get method name. */
517 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
518 mname = get_java_utf8_name (&objfile->objfile_obstack, temp);
519 if (strcmp (mname, "<init>") == 0)
520 mname = unqualified_name;
522 /* Check for an existing method with the same name.
523 * This makes building the fn_fieldslists an O(nmethods**2)
524 * operation. That could be using hashing, but I doubt it
525 * is worth it. Note that we do maintain the order of methods
526 * in the inferior's Method table (as long as that is grouped
527 * by method name), which I think is desirable. --PB */
528 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
531 { /* No match - new method name. */
532 j = TYPE_NFN_FIELDS (type)++;
533 fn_fieldlists[j].name = mname;
534 fn_fieldlists[j].length = 1;
535 fn_fieldlists[j].fn_fields = &fn_fields[i];
539 if (strcmp (mname, fn_fieldlists[j].name) == 0)
540 { /* Found an existing method with the same name. */
542 if (mname != unqualified_name)
543 obstack_free (&objfile->objfile_obstack, mname);
544 mname = fn_fieldlists[j].name;
545 fn_fieldlists[j].length++;
546 k = i - k; /* Index of new slot. */
547 /* Shift intervening fn_fields (between k and i) down. */
548 for (l = i; l > k; l--)
549 fn_fields[l] = fn_fields[l - 1];
550 for (l = TYPE_NFN_FIELDS (type); --l > j;)
551 fn_fieldlists[l].fn_fields++;
554 k += fn_fieldlists[j].length;
556 fn_fields[k].physname = "";
557 fn_fields[k].is_stub = 1;
558 fn_fields[k].type = make_function_type (java_void_type, NULL); /* FIXME */
559 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
562 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
563 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
564 obstack_alloc (&dynamics_objfile->objfile_obstack, j);
565 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
570 static struct type *java_object_type;
573 get_java_object_type (void)
575 if (java_object_type == NULL)
578 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN,
579 (int *) 0, (struct symtab **) NULL);
581 error ("cannot find java.lang.Object");
582 java_object_type = SYMBOL_TYPE (sym);
584 return java_object_type;
588 get_java_object_header_size (void)
590 struct type *objtype = get_java_object_type ();
592 return (2 * TARGET_PTR_BIT / TARGET_CHAR_BIT);
594 return TYPE_LENGTH (objtype);
598 is_object_type (struct type *type)
600 CHECK_TYPEDEF (type);
601 if (TYPE_CODE (type) == TYPE_CODE_PTR)
603 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
605 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
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)
612 name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
613 if (name != NULL && strcmp (name, "vtable") == 0)
615 if (java_object_type == NULL)
616 java_object_type = type;
624 java_primitive_type (int signature)
629 return java_byte_type;
631 return java_short_type;
633 return java_int_type;
635 return java_long_type;
637 return java_boolean_type;
639 return java_char_type;
641 return java_float_type;
643 return java_double_type;
645 return java_void_type;
647 error ("unknown signature '%c' for primitive type", (char) signature);
650 /* If name[0 .. namelen-1] is the name of a primitive Java type,
651 return that type. Otherwise, return NULL. */
654 java_primitive_type_from_name (char *name, int namelen)
659 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
660 return java_byte_type;
661 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
662 return java_boolean_type;
665 if (namelen == 4 && memcmp (name, "char", 4) == 0)
666 return java_char_type;
668 if (namelen == 6 && memcmp (name, "double", 6) == 0)
669 return java_double_type;
672 if (namelen == 5 && memcmp (name, "float", 5) == 0)
673 return java_float_type;
676 if (namelen == 3 && memcmp (name, "int", 3) == 0)
677 return java_int_type;
680 if (namelen == 4 && memcmp (name, "long", 4) == 0)
681 return java_long_type;
684 if (namelen == 5 && memcmp (name, "short", 5) == 0)
685 return java_short_type;
688 if (namelen == 4 && memcmp (name, "void", 4) == 0)
689 return java_void_type;
695 /* Return the length (in bytes) of demangled name of the Java type
696 signature string SIGNATURE. */
699 java_demangled_signature_length (char *signature)
702 for (; *signature == '['; signature++)
703 array += 2; /* Two chars for "[]". */
704 switch (signature[0])
707 /* Subtract 2 for 'L' and ';'. */
708 return strlen (signature) - 2 + array;
710 return strlen (TYPE_NAME (java_primitive_type (signature[0]))) + array;
714 /* Demangle the Java type signature SIGNATURE, leaving the result in RESULT. */
717 java_demangled_signature_copy (char *result, char *signature)
722 while (*signature == '[')
727 switch (signature[0])
730 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
733 for (; *signature != ';' && *signature != '\0'; signature++)
735 if (*signature == '/')
742 ptr = TYPE_NAME (java_primitive_type (signature[0]));
744 strcpy (result, ptr);
755 /* Return the demangled name of the Java type signature string SIGNATURE,
756 as a freshly allocated copy. */
759 java_demangle_type_signature (char *signature)
761 int length = java_demangled_signature_length (signature);
762 char *result = xmalloc (length + 1);
763 java_demangled_signature_copy (result, signature);
764 result[length] = '\0';
768 /* Return the type of TYPE followed by DIMS pairs of [ ].
769 If DIMS == 0, TYPE is returned. */
772 java_array_type (struct type *type, int dims)
774 struct type *range_type;
778 range_type = create_range_type (NULL, builtin_type_int, 0, 0);
779 /* FIXME This is bogus! Java arrays are not gdb arrays! */
780 type = create_array_type (NULL, type, range_type);
786 /* Create a Java string in the inferior from a (Utf8) literal. */
788 static struct value *
789 java_value_string (char *ptr, int len)
791 error ("not implemented - java_value_string"); /* FIXME */
794 /* Print the character C on STREAM as part of the contents of a literal
795 string whose delimiter is QUOTER. Note that that format for printing
796 characters and strings is language specific. */
799 java_emit_char (int c, struct ui_file *stream, int quoter)
805 fprintf_filtered (stream, "\\%c", c);
808 fputs_filtered ("\\b", stream);
811 fputs_filtered ("\\t", stream);
814 fputs_filtered ("\\n", stream);
817 fputs_filtered ("\\f", stream);
820 fputs_filtered ("\\r", stream);
824 fputc_filtered (c, stream);
826 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
831 static struct value *
832 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
833 int *pos, enum noside noside)
838 enum exp_opcode op = exp->elts[*pos].opcode;
845 if (noside == EVAL_SKIP)
848 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
849 if (is_object_type (VALUE_TYPE (arg1)))
853 type = type_from_class (java_class_from_object (arg1));
854 arg1 = value_cast (lookup_pointer_type (type), arg1);
856 if (noside == EVAL_SKIP)
858 return value_ind (arg1);
860 case BINOP_SUBSCRIPT:
862 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
863 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
864 if (noside == EVAL_SKIP)
866 /* If the user attempts to subscript something that is not an
867 array or pointer type (like a plain int variable for example),
868 then report this as an error. */
871 type = check_typedef (VALUE_TYPE (arg1));
872 if (TYPE_CODE (type) == TYPE_CODE_PTR)
873 type = check_typedef (TYPE_TARGET_TYPE (type));
874 name = TYPE_NAME (type);
876 name = TYPE_TAG_NAME (type);
877 i = name == NULL ? 0 : strlen (name);
878 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
879 && i > 2 && name[i - 1] == ']')
883 struct type *el_type;
886 struct value *clas = java_class_from_object (arg1);
887 struct value *temp = clas;
888 /* Get CLASS_ELEMENT_TYPE of the array type. */
889 temp = value_struct_elt (&temp, NULL, "methods",
891 VALUE_TYPE (temp) = VALUE_TYPE (clas);
892 el_type = type_from_class (temp);
893 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
894 el_type = lookup_pointer_type (el_type);
896 if (noside == EVAL_AVOID_SIDE_EFFECTS)
897 return value_zero (el_type, VALUE_LVAL (arg1));
898 address = value_as_address (arg1);
899 address += JAVA_OBJECT_SIZE;
900 read_memory (address, buf4, 4);
901 length = (long) extract_signed_integer (buf4, 4);
902 index = (long) value_as_long (arg2);
903 if (index >= length || index < 0)
904 error ("array index (%ld) out of bounds (length: %ld)",
906 address = (address + 4) + index * TYPE_LENGTH (el_type);
907 return value_at (el_type, address, NULL);
909 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
911 if (noside == EVAL_AVOID_SIDE_EFFECTS)
912 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
914 return value_subscript (arg1, arg2);
917 error ("cannot subscript something of type `%s'", name);
919 error ("cannot subscript requested type");
923 i = longest_to_int (exp->elts[pc + 1].longconst);
924 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
925 if (noside == EVAL_SKIP)
927 return java_value_string (&exp->elts[pc + 2].string, i);
929 case STRUCTOP_STRUCT:
930 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
931 /* Convert object field (such as TYPE.class) to reference. */
932 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT)
933 arg1 = value_addr (arg1);
939 return evaluate_subexp_standard (expect_type, exp, pos, noside);
941 return value_from_longest (builtin_type_long, (LONGEST) 1);
945 java_create_fundamental_type (struct objfile *objfile, int typeid)
950 return java_void_type;
952 return java_boolean_type;
954 return java_char_type;
956 return java_float_type;
957 case FT_DBL_PREC_FLOAT:
958 return java_double_type;
961 return java_byte_type;
963 case FT_SIGNED_SHORT:
964 return java_short_type;
966 case FT_SIGNED_INTEGER:
967 return java_int_type;
970 return java_long_type;
972 return c_create_fundamental_type (objfile, typeid);
975 static char *java_demangle (const char *mangled, int options)
977 return cplus_demangle (mangled, options | DMGL_JAVA);
980 /* Find the member function name of the demangled name NAME. NAME
981 must be a method name including arguments, in order to correctly
982 locate the last component.
984 This function return a pointer to the first dot before the
985 member function name, or NULL if the name was not of the
989 java_find_last_component (const char *name)
993 /* Find argument list. */
994 p = strchr (name, '(');
999 /* Back up and find first dot prior to argument list. */
1000 while (p > name && *p != '.')
1009 /* Return the name of the class containing method PHYSNAME. */
1012 java_class_name_from_physname (const char *physname)
1017 char *demangled_name = java_demangle (physname, DMGL_PARAMS | DMGL_ANSI);
1019 if (demangled_name == NULL)
1022 end = java_find_last_component (demangled_name);
1025 ret = xmalloc (end - demangled_name + 1);
1026 memcpy (ret, demangled_name, end - demangled_name);
1027 ret[end - demangled_name] = '\0';
1030 xfree (demangled_name);
1034 /* Table mapping opcodes into strings for printing operators
1035 and precedences of the operators. */
1037 const struct op_print java_op_print_tab[] =
1039 {",", BINOP_COMMA, PREC_COMMA, 0},
1040 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1041 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1042 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1043 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1044 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1045 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1046 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1047 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1048 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1049 {">=", BINOP_GEQ, PREC_ORDER, 0},
1050 {">", BINOP_GTR, PREC_ORDER, 0},
1051 {"<", BINOP_LESS, PREC_ORDER, 0},
1052 {">>", BINOP_RSH, PREC_SHIFT, 0},
1053 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1055 {">>>", BINOP_ ? ? ?, PREC_SHIFT, 0},
1057 {"+", BINOP_ADD, PREC_ADD, 0},
1058 {"-", BINOP_SUB, PREC_ADD, 0},
1059 {"*", BINOP_MUL, PREC_MUL, 0},
1060 {"/", BINOP_DIV, PREC_MUL, 0},
1061 {"%", BINOP_REM, PREC_MUL, 0},
1062 {"-", UNOP_NEG, PREC_PREFIX, 0},
1063 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1064 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1065 {"*", UNOP_IND, PREC_PREFIX, 0},
1067 {"instanceof", ? ? ?, ? ? ?, 0},
1069 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1070 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1074 const struct exp_descriptor exp_descriptor_java =
1076 print_subexp_standard,
1077 operator_length_standard,
1079 dump_subexp_body_standard,
1080 evaluate_subexp_java
1083 const struct language_defn java_language_defn =
1085 "java", /* Language name */
1091 &exp_descriptor_java,
1095 c_printchar, /* Print a character constant */
1096 c_printstr, /* Function to print string constant */
1097 java_emit_char, /* Function to print a single character */
1098 java_create_fundamental_type, /* Create fundamental type in this language */
1099 java_print_type, /* Print a type using appropriate syntax */
1100 java_val_print, /* Print a value using appropriate syntax */
1101 java_value_print, /* Print a top-level value */
1102 NULL, /* Language specific skip_trampoline */
1103 value_of_this, /* value_of_this */
1104 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1105 basic_lookup_transparent_type,/* lookup_transparent_type */
1106 java_demangle, /* Language specific symbol demangler */
1107 java_class_name_from_physname,/* Language specific class name */
1108 {"", "", "", ""}, /* Binary format info */
1109 {"0%lo", "0", "o", ""}, /* Octal format info */
1110 {"%ld", "", "d", ""}, /* Decimal format info */
1111 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1112 java_op_print_tab, /* expression operators for printing */
1113 0, /* not c-style arrays */
1114 0, /* String lower bound */
1116 default_word_break_characters,
1117 c_language_arch_info,
1122 _initialize_java_language (void)
1125 java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
1126 java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
1127 java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
1128 java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
1129 java_boolean_type = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
1130 java_char_type = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
1131 java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
1132 java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
1133 java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
1135 add_language (&java_language_defn);
1138 /* Cleanup code that should be run on every "run".
1139 We should use make_run_cleanup to have this be called.
1140 But will that mess up values in value histry? FIXME */
1142 extern void java_rerun_cleanup (void);
1144 java_rerun_cleanup (void)
1146 if (class_symtab != NULL)
1148 free_symtab (class_symtab); /* ??? */
1149 class_symtab = NULL;
1151 if (dynamics_objfile != NULL)
1153 free_objfile (dynamics_objfile);
1154 dynamics_objfile = NULL;
1157 java_object_type = NULL;