1 /* Java language support routines for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2003 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 type *java_lookup_type (char *signature);
62 static struct value *java_value_string (char *ptr, int len);
64 static void java_emit_char (int c, struct ui_file * stream, int quoter);
66 /* This objfile contains symtabs that have been dynamically created
67 to record dynamically loaded Java classes and dynamically
68 compiled java methods. */
70 static struct objfile *dynamics_objfile = NULL;
72 static struct type *java_link_class_type (struct type *, struct value *);
74 /* FIXME: carlton/2003-02-04: This is the main or only caller of
75 allocate_objfile with first argument NULL; as a result, this code
76 breaks every so often. Somebody should write a test case that
77 exercises GDB in various ways (e.g. something involving loading a
78 dynamic library) after this code has been called. */
80 static struct objfile *
81 get_dynamics_objfile (void)
83 if (dynamics_objfile == NULL)
85 dynamics_objfile = allocate_objfile (NULL, 0);
87 return dynamics_objfile;
91 /* symtab contains classes read from the inferior. */
93 static struct symtab *class_symtab = NULL;
95 static void free_class_block (struct symtab *symtab);
97 static struct symtab *
98 get_java_class_symtab (void)
100 if (class_symtab == NULL)
102 struct objfile *objfile = get_dynamics_objfile ();
103 struct blockvector *bv;
105 class_symtab = allocate_symtab ("<java-classes>", objfile);
106 class_symtab->language = language_java;
107 bv = (struct blockvector *)
108 obstack_alloc (&objfile->symbol_obstack,
109 sizeof (struct blockvector) + sizeof (struct block *));
110 BLOCKVECTOR_NBLOCKS (bv) = 1;
111 BLOCKVECTOR (class_symtab) = bv;
113 /* Allocate dummy STATIC_BLOCK. */
114 bl = (struct block *)
115 obstack_alloc (&objfile->symbol_obstack, sizeof (struct block));
116 BLOCK_DICT (bl) = dict_create_linear (&objfile->symbol_obstack,
118 BLOCK_START (bl) = 0;
120 BLOCK_FUNCTION (bl) = NULL;
121 BLOCK_SUPERBLOCK (bl) = NULL;
122 BLOCK_NAMESPACE (bl) = NULL;
123 BLOCK_GCC_COMPILED (bl) = 0;
124 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
126 /* Allocate GLOBAL_BLOCK. This has to be relocatable. */
127 bl = (struct block *)
128 obstack_alloc (&objfile->symbol_obstack, sizeof (struct block));
129 *bl = *BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
130 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
131 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
132 class_symtab->free_func = free_class_block;
138 add_class_symtab_symbol (struct symbol *sym)
140 struct symtab *symtab = get_java_class_symtab ();
141 struct blockvector *bv = BLOCKVECTOR (symtab);
142 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
145 static struct symbol *add_class_symbol (struct type *type, CORE_ADDR addr);
147 static struct symbol *
148 add_class_symbol (struct type *type, CORE_ADDR addr)
151 sym = (struct symbol *)
152 obstack_alloc (&dynamics_objfile->symbol_obstack, sizeof (struct symbol));
153 memset (sym, 0, sizeof (struct symbol));
154 SYMBOL_LANGUAGE (sym) = language_java;
155 DEPRECATED_SYMBOL_NAME (sym) = TYPE_TAG_NAME (type);
156 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
157 /* SYMBOL_VALUE (sym) = valu; */
158 SYMBOL_TYPE (sym) = type;
159 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
160 SYMBOL_VALUE_ADDRESS (sym) = addr;
164 /* Free the dynamic symbols block. */
166 free_class_block (struct symtab *symtab)
168 struct blockvector *bv = BLOCKVECTOR (symtab);
169 struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
171 dict_free (BLOCK_DICT (bl));
176 java_lookup_class (char *name)
179 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN,
180 (int *) 0, (struct symtab **) NULL);
182 return SYMBOL_TYPE (sym);
185 if (called from parser)
187 call lookup_class (or similar) in inferior;
191 addr = found in inferior;
196 type = alloc_type (objfile);
197 TYPE_CODE (type) = TYPE_CODE_STRUCT;
198 INIT_CPLUS_SPECIFIC (type);
199 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name), &objfile->type_obstack);
200 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
204 /* FIXME - should search inferior's symbol table. */
209 /* Return a nul-terminated string (allocated on OBSTACK) for
210 a name given by NAME (which has type Utf8Const*). */
213 get_java_utf8_name (struct obstack *obstack, struct value *name)
216 struct value *temp = name;
219 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
220 name_length = (int) value_as_long (temp);
221 data_addr = VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
222 + TYPE_LENGTH (VALUE_TYPE (temp));
223 chrs = obstack_alloc (obstack, name_length + 1);
224 chrs[name_length] = '\0';
225 read_memory (data_addr, chrs, name_length);
230 java_class_from_object (struct value *obj_val)
232 /* This is all rather inefficient, since the offsets of vtable and
233 class are fixed. FIXME */
234 struct value *vtable_val;
236 if (TYPE_CODE (VALUE_TYPE (obj_val)) == TYPE_CODE_PTR
237 && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (obj_val))) == 0)
238 obj_val = value_at (get_java_object_type (),
239 value_as_address (obj_val), NULL);
241 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
242 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
245 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
247 java_class_is_primitive (struct value *clas)
249 struct value *vtable = value_struct_elt (&clas, NULL, "vtable", NULL, "struct");
250 CORE_ADDR i = value_as_address (vtable);
251 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
254 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
257 type_from_class (struct value *clas)
262 struct objfile *objfile;
263 struct value *utf8_name;
267 struct dict_iterator iter;
270 type = check_typedef (VALUE_TYPE (clas));
271 if (TYPE_CODE (type) == TYPE_CODE_PTR)
273 if (value_logical_not (clas))
275 clas = value_ind (clas);
277 addr = VALUE_ADDRESS (clas) + VALUE_OFFSET (clas);
280 get_java_class_symtab ();
281 bl = BLOCKVECTOR_BLOCK (BLOCKVECTOR (class_symtab), GLOBAL_BLOCK);
282 ALL_BLOCK_SYMBOLS (block, iter, sym)
284 if (SYMBOL_VALUE_ADDRESS (sym) == addr)
285 return SYMBOL_TYPE (sym);
289 objfile = get_dynamics_objfile ();
290 if (java_class_is_primitive (clas))
294 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
295 return java_primitive_type (value_as_long (sig));
298 /* Get Class name. */
299 /* if clasloader non-null, prepend loader address. FIXME */
301 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
302 name = get_java_utf8_name (&objfile->type_obstack, utf8_name);
303 for (nptr = name; *nptr != 0; nptr++)
309 type = java_lookup_class (name);
313 type = alloc_type (objfile);
314 TYPE_CODE (type) = TYPE_CODE_STRUCT;
315 INIT_CPLUS_SPECIFIC (type);
319 char *signature = name;
320 int namelen = java_demangled_signature_length (signature);
321 if (namelen > strlen (name))
322 name = obstack_alloc (&objfile->type_obstack, namelen + 1);
323 java_demangled_signature_copy (name, signature);
324 name[namelen] = '\0';
327 /* Set array element type. */
328 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
329 VALUE_TYPE (temp) = lookup_pointer_type (VALUE_TYPE (clas));
330 TYPE_TARGET_TYPE (type) = type_from_class (temp);
333 ALLOCATE_CPLUS_STRUCT_TYPE (type);
334 TYPE_TAG_NAME (type) = name;
336 add_class_symtab_symbol (add_class_symbol (type, addr));
337 return java_link_class_type (type, clas);
340 /* Fill in class TYPE with data from the CLAS value. */
343 java_link_class_type (struct type *type, struct value *clas)
346 char *unqualified_name;
347 char *name = TYPE_TAG_NAME (type);
348 int ninterfaces, nfields, nmethods;
349 int type_is_object = 0;
350 struct fn_field *fn_fields;
351 struct fn_fieldlist *fn_fieldlists;
352 struct value *fields;
353 struct value *methods;
354 struct value *method = NULL;
355 struct value *field = NULL;
357 struct objfile *objfile = get_dynamics_objfile ();
360 unqualified_name = strrchr (name, '.');
361 if (unqualified_name == NULL)
362 unqualified_name = name;
365 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
366 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
368 tsuper = get_java_object_type ();
369 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
370 tsuper = TYPE_TARGET_TYPE (tsuper);
374 tsuper = type_from_class (temp);
380 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
382 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
384 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count", NULL, "structure"));
385 nfields += TYPE_N_BASECLASSES (type);
386 nfields++; /* Add one for dummy "class" field. */
387 TYPE_NFIELDS (type) = nfields;
388 TYPE_FIELDS (type) = (struct field *)
389 TYPE_ALLOC (type, sizeof (struct field) * nfields);
391 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
393 TYPE_FIELD_PRIVATE_BITS (type) =
394 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
395 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
397 TYPE_FIELD_PROTECTED_BITS (type) =
398 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
399 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
401 TYPE_FIELD_IGNORE_BITS (type) =
402 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
403 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
405 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
406 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
407 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
411 TYPE_BASECLASS (type, 0) = tsuper;
413 SET_TYPE_FIELD_PRIVATE (type, 0);
417 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
420 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
425 temp = value_struct_elt (&temp, NULL, "size_in_bytes", NULL, "structure");
426 TYPE_LENGTH (type) = value_as_long (temp);
430 nfields--; /* First set up dummy "class" field. */
431 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields),
432 VALUE_ADDRESS (clas) + VALUE_OFFSET (clas));
433 TYPE_FIELD_NAME (type, nfields) = "class";
434 TYPE_FIELD_TYPE (type, nfields) = VALUE_TYPE (clas);
435 SET_TYPE_FIELD_PRIVATE (type, nfields);
437 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
444 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
445 field = value_ind (fields);
448 { /* Re-use field value for next field. */
449 VALUE_ADDRESS (field) += TYPE_LENGTH (VALUE_TYPE (field));
450 VALUE_LAZY (field) = 1;
453 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
454 TYPE_FIELD_NAME (type, i) =
455 get_java_utf8_name (&objfile->type_obstack, temp);
457 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
460 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
461 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
463 if (accflags & 0x0001) /* public access */
467 if (accflags & 0x0002) /* private access */
469 SET_TYPE_FIELD_PRIVATE (type, i);
471 if (accflags & 0x0004) /* protected access */
473 SET_TYPE_FIELD_PROTECTED (type, i);
475 if (accflags & 0x0008) /* ACC_STATIC */
476 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
478 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
479 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
481 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
487 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
488 ftype = type_from_class (temp);
489 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
490 ftype = lookup_pointer_type (ftype);
491 TYPE_FIELD_TYPE (type, i) = ftype;
496 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
498 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
499 j = nmethods * sizeof (struct fn_field);
500 fn_fields = (struct fn_field *)
501 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
502 memset (fn_fields, 0, j);
503 fn_fieldlists = (struct fn_fieldlist *)
504 alloca (nmethods * sizeof (struct fn_fieldlist));
507 for (i = 0; i < nmethods; i++)
514 methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
515 method = value_ind (methods);
518 { /* Re-use method value for next method. */
519 VALUE_ADDRESS (method) += TYPE_LENGTH (VALUE_TYPE (method));
520 VALUE_LAZY (method) = 1;
523 /* Get method name. */
525 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
526 mname = get_java_utf8_name (&objfile->type_obstack, temp);
527 if (strcmp (mname, "<init>") == 0)
528 mname = unqualified_name;
530 /* Check for an existing method with the same name.
531 * This makes building the fn_fieldslists an O(nmethods**2)
532 * operation. That could be using hashing, but I doubt it
533 * is worth it. Note that we do maintain the order of methods
534 * in the inferior's Method table (as long as that is grouped
535 * by method name), which I think is desirable. --PB */
536 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
539 { /* No match - new method name. */
540 j = TYPE_NFN_FIELDS (type)++;
541 fn_fieldlists[j].name = mname;
542 fn_fieldlists[j].length = 1;
543 fn_fieldlists[j].fn_fields = &fn_fields[i];
547 if (strcmp (mname, fn_fieldlists[j].name) == 0)
548 { /* Found an existing method with the same name. */
550 if (mname != unqualified_name)
551 obstack_free (&objfile->type_obstack, mname);
552 mname = fn_fieldlists[j].name;
553 fn_fieldlists[j].length++;
554 k = i - k; /* Index of new slot. */
555 /* Shift intervening fn_fields (between k and i) down. */
556 for (l = i; l > k; l--)
557 fn_fields[l] = fn_fields[l - 1];
558 for (l = TYPE_NFN_FIELDS (type); --l > j;)
559 fn_fieldlists[l].fn_fields++;
562 k += fn_fieldlists[j].length;
564 fn_fields[k].physname = "";
565 fn_fields[k].is_stub = 1;
566 fn_fields[k].type = make_function_type (java_void_type, NULL); /* FIXME */
567 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
570 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
571 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
572 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
573 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
578 static struct type *java_object_type;
581 get_java_object_type (void)
583 if (java_object_type == NULL)
586 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN,
587 (int *) 0, (struct symtab **) NULL);
589 error ("cannot find java.lang.Object");
590 java_object_type = SYMBOL_TYPE (sym);
592 return java_object_type;
596 get_java_object_header_size (void)
598 struct type *objtype = get_java_object_type ();
600 return (2 * TARGET_PTR_BIT / TARGET_CHAR_BIT);
602 return TYPE_LENGTH (objtype);
606 is_object_type (struct type *type)
608 CHECK_TYPEDEF (type);
609 if (TYPE_CODE (type) == TYPE_CODE_PTR)
611 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
613 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
615 while (TYPE_N_BASECLASSES (ttype) > 0)
616 ttype = TYPE_BASECLASS (ttype, 0);
617 name = TYPE_TAG_NAME (ttype);
618 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
620 name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
621 if (name != NULL && strcmp (name, "vtable") == 0)
623 if (java_object_type == NULL)
624 java_object_type = type;
632 java_primitive_type (int signature)
637 return java_byte_type;
639 return java_short_type;
641 return java_int_type;
643 return java_long_type;
645 return java_boolean_type;
647 return java_char_type;
649 return java_float_type;
651 return java_double_type;
653 return java_void_type;
655 error ("unknown signature '%c' for primitive type", (char) signature);
658 /* If name[0 .. namelen-1] is the name of a primitive Java type,
659 return that type. Otherwise, return NULL. */
662 java_primitive_type_from_name (char *name, int namelen)
667 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
668 return java_byte_type;
669 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
670 return java_boolean_type;
673 if (namelen == 4 && memcmp (name, "char", 4) == 0)
674 return java_char_type;
676 if (namelen == 6 && memcmp (name, "double", 6) == 0)
677 return java_double_type;
680 if (namelen == 5 && memcmp (name, "float", 5) == 0)
681 return java_float_type;
684 if (namelen == 3 && memcmp (name, "int", 3) == 0)
685 return java_int_type;
688 if (namelen == 4 && memcmp (name, "long", 4) == 0)
689 return java_long_type;
692 if (namelen == 5 && memcmp (name, "short", 5) == 0)
693 return java_short_type;
696 if (namelen == 4 && memcmp (name, "void", 4) == 0)
697 return java_void_type;
703 /* Return the length (in bytes) of demangled name of the Java type
704 signature string SIGNATURE. */
707 java_demangled_signature_length (char *signature)
710 for (; *signature == '['; signature++)
711 array += 2; /* Two chars for "[]". */
712 switch (signature[0])
715 /* Subtract 2 for 'L' and ';'. */
716 return strlen (signature) - 2 + array;
718 return strlen (TYPE_NAME (java_primitive_type (signature[0]))) + array;
722 /* Demangle the Java type signature SIGNATURE, leaving the result in RESULT. */
725 java_demangled_signature_copy (char *result, char *signature)
730 while (*signature == '[')
735 switch (signature[0])
738 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
741 for (; *signature != ';' && *signature != '\0'; signature++)
743 if (*signature == '/')
750 ptr = TYPE_NAME (java_primitive_type (signature[0]));
752 strcpy (result, ptr);
763 /* Return the demangled name of the Java type signature string SIGNATURE,
764 as a freshly allocated copy. */
767 java_demangle_type_signature (char *signature)
769 int length = java_demangled_signature_length (signature);
770 char *result = xmalloc (length + 1);
771 java_demangled_signature_copy (result, signature);
772 result[length] = '\0';
777 java_lookup_type (char *signature)
779 switch (signature[0])
783 error ("java_lookup_type not fully implemented");
785 return java_primitive_type (signature[0]);
789 /* Return the type of TYPE followed by DIMS pairs of [ ].
790 If DIMS == 0, TYPE is returned. */
793 java_array_type (struct type *type, int dims)
795 struct type *range_type;
799 range_type = create_range_type (NULL, builtin_type_int, 0, 0);
800 /* FIXME This is bogus! Java arrays are not gdb arrays! */
801 type = create_array_type (NULL, type, range_type);
807 /* Create a Java string in the inferior from a (Utf8) literal. */
809 static struct value *
810 java_value_string (char *ptr, int len)
812 error ("not implemented - java_value_string"); /* FIXME */
815 /* Print the character C on STREAM as part of the contents of a literal
816 string whose delimiter is QUOTER. Note that that format for printing
817 characters and strings is language specific. */
820 java_emit_char (int c, struct ui_file *stream, int quoter)
826 fprintf_filtered (stream, "\\%c", c);
829 fputs_filtered ("\\b", stream);
832 fputs_filtered ("\\t", stream);
835 fputs_filtered ("\\n", stream);
838 fputs_filtered ("\\f", stream);
841 fputs_filtered ("\\r", stream);
845 fputc_filtered (c, stream);
847 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
852 static struct value *
853 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
854 int *pos, enum noside noside)
859 enum exp_opcode op = exp->elts[*pos].opcode;
866 if (noside == EVAL_SKIP)
869 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
870 if (is_object_type (VALUE_TYPE (arg1)))
874 type = type_from_class (java_class_from_object (arg1));
875 arg1 = value_cast (lookup_pointer_type (type), arg1);
877 if (noside == EVAL_SKIP)
879 return value_ind (arg1);
881 case BINOP_SUBSCRIPT:
883 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
884 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
885 if (noside == EVAL_SKIP)
887 /* If the user attempts to subscript something that is not an
888 array or pointer type (like a plain int variable for example),
889 then report this as an error. */
892 type = check_typedef (VALUE_TYPE (arg1));
893 if (TYPE_CODE (type) == TYPE_CODE_PTR)
894 type = check_typedef (TYPE_TARGET_TYPE (type));
895 name = TYPE_NAME (type);
897 name = TYPE_TAG_NAME (type);
898 i = name == NULL ? 0 : strlen (name);
899 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
900 && i > 2 && name[i - 1] == ']')
904 struct type *el_type;
907 struct value *clas = java_class_from_object (arg1);
908 struct value *temp = clas;
909 /* Get CLASS_ELEMENT_TYPE of the array type. */
910 temp = value_struct_elt (&temp, NULL, "methods",
912 VALUE_TYPE (temp) = VALUE_TYPE (clas);
913 el_type = type_from_class (temp);
914 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
915 el_type = lookup_pointer_type (el_type);
917 if (noside == EVAL_AVOID_SIDE_EFFECTS)
918 return value_zero (el_type, VALUE_LVAL (arg1));
919 address = value_as_address (arg1);
920 address += JAVA_OBJECT_SIZE;
921 read_memory (address, buf4, 4);
922 length = (long) extract_signed_integer (buf4, 4);
923 index = (long) value_as_long (arg2);
924 if (index >= length || index < 0)
925 error ("array index (%ld) out of bounds (length: %ld)",
927 address = (address + 4) + index * TYPE_LENGTH (el_type);
928 return value_at (el_type, address, NULL);
930 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
932 if (noside == EVAL_AVOID_SIDE_EFFECTS)
933 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
935 return value_subscript (arg1, arg2);
938 error ("cannot subscript something of type `%s'", name);
940 error ("cannot subscript requested type");
944 i = longest_to_int (exp->elts[pc + 1].longconst);
945 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
946 if (noside == EVAL_SKIP)
948 return java_value_string (&exp->elts[pc + 2].string, i);
950 case STRUCTOP_STRUCT:
951 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
952 /* Convert object field (such as TYPE.class) to reference. */
953 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT)
954 arg1 = value_addr (arg1);
960 return evaluate_subexp_standard (expect_type, exp, pos, noside);
962 return value_from_longest (builtin_type_long, (LONGEST) 1);
966 java_create_fundamental_type (struct objfile *objfile, int typeid)
971 return java_void_type;
973 return java_boolean_type;
975 return java_char_type;
977 return java_float_type;
978 case FT_DBL_PREC_FLOAT:
979 return java_double_type;
982 return java_byte_type;
984 case FT_SIGNED_SHORT:
985 return java_short_type;
987 case FT_SIGNED_INTEGER:
988 return java_int_type;
991 return java_long_type;
993 return c_create_fundamental_type (objfile, typeid);
996 static char *java_demangle (const char *mangled, int options)
998 return cplus_demangle (mangled, options | DMGL_JAVA);
1002 /* Table mapping opcodes into strings for printing operators
1003 and precedences of the operators. */
1005 const struct op_print java_op_print_tab[] =
1007 {",", BINOP_COMMA, PREC_COMMA, 0},
1008 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1009 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1010 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1011 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1012 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1013 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1014 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1015 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1016 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1017 {">=", BINOP_GEQ, PREC_ORDER, 0},
1018 {">", BINOP_GTR, PREC_ORDER, 0},
1019 {"<", BINOP_LESS, PREC_ORDER, 0},
1020 {">>", BINOP_RSH, PREC_SHIFT, 0},
1021 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1023 {">>>", BINOP_ ? ? ?, PREC_SHIFT, 0},
1025 {"+", BINOP_ADD, PREC_ADD, 0},
1026 {"-", BINOP_SUB, PREC_ADD, 0},
1027 {"*", BINOP_MUL, PREC_MUL, 0},
1028 {"/", BINOP_DIV, PREC_MUL, 0},
1029 {"%", BINOP_REM, PREC_MUL, 0},
1030 {"-", UNOP_NEG, PREC_PREFIX, 0},
1031 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1032 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1033 {"*", UNOP_IND, PREC_PREFIX, 0},
1035 {"instanceof", ? ? ?, ? ? ?, 0},
1037 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1038 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1042 const struct language_defn java_language_defn =
1044 "java", /* Language name */
1052 evaluate_subexp_java,
1053 c_printchar, /* Print a character constant */
1054 c_printstr, /* Function to print string constant */
1055 java_emit_char, /* Function to print a single character */
1056 java_create_fundamental_type, /* Create fundamental type in this language */
1057 java_print_type, /* Print a type using appropriate syntax */
1058 java_val_print, /* Print a value using appropriate syntax */
1059 java_value_print, /* Print a top-level value */
1060 NULL, /* Language specific skip_trampoline */
1061 value_of_this, /* value_of_this */
1062 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1063 java_demangle, /* Language specific symbol demangler */
1064 {"", "", "", ""}, /* Binary format info */
1065 {"0%lo", "0", "o", ""}, /* Octal format info */
1066 {"%ld", "", "d", ""}, /* Decimal format info */
1067 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1068 java_op_print_tab, /* expression operators for printing */
1069 0, /* not c-style arrays */
1070 0, /* String lower bound */
1071 &builtin_type_char, /* Type of string elements */
1076 _initialize_java_language (void)
1079 java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
1080 java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
1081 java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
1082 java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
1083 java_boolean_type = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
1084 java_char_type = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
1085 java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
1086 java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
1087 java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
1089 add_language (&java_language_defn);
1092 /* Cleanup code that should be run on every "run".
1093 We should use make_run_cleanup to have this be called.
1094 But will that mess up values in value histry? FIXME */
1096 extern void java_rerun_cleanup (void);
1098 java_rerun_cleanup (void)
1100 if (class_symtab != NULL)
1102 free_symtab (class_symtab); /* ??? */
1103 class_symtab = NULL;
1105 if (dynamics_objfile != NULL)
1107 free_objfile (dynamics_objfile);
1108 dynamics_objfile = NULL;
1111 java_object_type = NULL;