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"
40 struct type *java_int_type;
41 struct type *java_byte_type;
42 struct type *java_short_type;
43 struct type *java_long_type;
44 struct type *java_boolean_type;
45 struct type *java_char_type;
46 struct type *java_float_type;
47 struct type *java_double_type;
48 struct type *java_void_type;
52 extern void _initialize_java_language (void);
54 static int java_demangled_signature_length (char *);
55 static void java_demangled_signature_copy (char *, char *);
57 static struct symtab *get_java_class_symtab (void);
58 static char *get_java_utf8_name (struct obstack *obstack, struct value *name);
59 static int java_class_is_primitive (struct value *clas);
60 static struct type *java_lookup_type (char *signature);
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 /* This objfile contains symtabs that have been dynamically created
66 to record dynamically loaded Java classes and dynamically
67 compiled java methods. */
69 static struct objfile *dynamics_objfile = NULL;
71 static struct type *java_link_class_type (struct type *, struct value *);
73 /* FIXME: carlton/2003-02-04: This is the main or only caller of
74 allocate_objfile with first argument NULL; as a result, this code
75 breaks every so often. Somebody should write a test case that
76 exercises GDB in various ways (e.g. something involving loading a
77 dynamic library) after this code has been called. */
79 static struct objfile *
80 get_dynamics_objfile (void)
82 if (dynamics_objfile == NULL)
84 dynamics_objfile = allocate_objfile (NULL, 0);
86 return dynamics_objfile;
90 /* symtab contains classes read from the inferior. */
92 static struct symtab *class_symtab = NULL;
94 /* Maximum number of class in class_symtab before relocation is needed. */
96 static int class_symtab_space;
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->symbol_obstack, sizeof (struct blockvector));
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_NSYMS (bl) = 0;
117 BLOCK_HASHTABLE (bl) = 0;
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 class_symtab_space = 128;
128 bl = xmmalloc (objfile->md,
129 sizeof (struct block)
130 + ((class_symtab_space - 1) * sizeof (struct symbol *)));
131 *bl = *BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
132 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
133 class_symtab->free_ptr = (char *) bl;
139 add_class_symtab_symbol (struct symbol *sym)
141 struct symtab *symtab = get_java_class_symtab ();
142 struct blockvector *bv = BLOCKVECTOR (symtab);
143 struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
144 if (BLOCK_NSYMS (bl) >= class_symtab_space)
146 /* Need to re-allocate. */
147 class_symtab_space *= 2;
148 bl = xmrealloc (symtab->objfile->md, bl,
149 sizeof (struct block)
150 + ((class_symtab_space - 1) * sizeof (struct symbol *)));
151 class_symtab->free_ptr = (char *) bl;
152 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
155 BLOCK_SYM (bl, BLOCK_NSYMS (bl)) = sym;
156 BLOCK_NSYMS (bl) = BLOCK_NSYMS (bl) + 1;
159 static struct symbol *add_class_symbol (struct type *type, CORE_ADDR addr);
161 static struct symbol *
162 add_class_symbol (struct type *type, CORE_ADDR addr)
165 sym = (struct symbol *)
166 obstack_alloc (&dynamics_objfile->symbol_obstack, sizeof (struct symbol));
167 memset (sym, 0, sizeof (struct symbol));
168 SYMBOL_LANGUAGE (sym) = language_java;
169 DEPRECATED_SYMBOL_NAME (sym) = TYPE_TAG_NAME (type);
170 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
171 /* SYMBOL_VALUE (sym) = valu; */
172 SYMBOL_TYPE (sym) = type;
173 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
174 SYMBOL_VALUE_ADDRESS (sym) = addr;
180 java_lookup_class (char *name)
183 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN,
184 (int *) 0, (struct symtab **) NULL);
186 return SYMBOL_TYPE (sym);
189 if (called from parser)
191 call lookup_class (or similar) in inferior;
195 addr = found in inferior;
200 type = alloc_type (objfile);
201 TYPE_CODE (type) = TYPE_CODE_STRUCT;
202 INIT_CPLUS_SPECIFIC (type);
203 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name), &objfile->type_obstack);
204 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
208 /* FIXME - should search inferior's symbol table. */
213 /* Return a nul-terminated string (allocated on OBSTACK) for
214 a name given by NAME (which has type Utf8Const*). */
217 get_java_utf8_name (struct obstack *obstack, struct value *name)
220 struct value *temp = name;
223 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
224 name_length = (int) value_as_long (temp);
225 data_addr = VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
226 + TYPE_LENGTH (VALUE_TYPE (temp));
227 chrs = obstack_alloc (obstack, name_length + 1);
228 chrs[name_length] = '\0';
229 read_memory (data_addr, chrs, name_length);
234 java_class_from_object (struct value *obj_val)
236 /* This is all rather inefficient, since the offsets of vtable and
237 class are fixed. FIXME */
238 struct value *vtable_val;
240 if (TYPE_CODE (VALUE_TYPE (obj_val)) == TYPE_CODE_PTR
241 && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (obj_val))) == 0)
242 obj_val = value_at (get_java_object_type (),
243 value_as_address (obj_val), NULL);
245 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
246 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
249 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
251 java_class_is_primitive (struct value *clas)
253 struct value *vtable = value_struct_elt (&clas, NULL, "vtable", NULL, "struct");
254 CORE_ADDR i = value_as_address (vtable);
255 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
258 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
261 type_from_class (struct value *clas)
266 struct objfile *objfile;
267 struct value *utf8_name;
274 type = check_typedef (VALUE_TYPE (clas));
275 if (TYPE_CODE (type) == TYPE_CODE_PTR)
277 if (value_logical_not (clas))
279 clas = value_ind (clas);
281 addr = VALUE_ADDRESS (clas) + VALUE_OFFSET (clas);
284 get_java_class_symtab ();
285 bl = BLOCKVECTOR_BLOCK (BLOCKVECTOR (class_symtab), GLOBAL_BLOCK);
286 for (i = BLOCK_NSYMS (bl); --i >= 0;)
288 struct symbol *sym = BLOCK_SYM (bl, i);
289 if (SYMBOL_VALUE_ADDRESS (sym) == addr)
290 return SYMBOL_TYPE (sym);
294 objfile = get_dynamics_objfile ();
295 if (java_class_is_primitive (clas))
299 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
300 return java_primitive_type (value_as_long (sig));
303 /* Get Class name. */
304 /* if clasloader non-null, prepend loader address. FIXME */
306 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
307 name = get_java_utf8_name (&objfile->type_obstack, utf8_name);
308 for (nptr = name; *nptr != 0; nptr++)
314 type = java_lookup_class (name);
318 type = alloc_type (objfile);
319 TYPE_CODE (type) = TYPE_CODE_STRUCT;
320 INIT_CPLUS_SPECIFIC (type);
324 char *signature = name;
325 int namelen = java_demangled_signature_length (signature);
326 if (namelen > strlen (name))
327 name = obstack_alloc (&objfile->type_obstack, namelen + 1);
328 java_demangled_signature_copy (name, signature);
329 name[namelen] = '\0';
332 /* Set array element type. */
333 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
334 VALUE_TYPE (temp) = lookup_pointer_type (VALUE_TYPE (clas));
335 TYPE_TARGET_TYPE (type) = type_from_class (temp);
338 ALLOCATE_CPLUS_STRUCT_TYPE (type);
339 TYPE_TAG_NAME (type) = name;
341 add_class_symtab_symbol (add_class_symbol (type, addr));
342 return java_link_class_type (type, clas);
345 /* Fill in class TYPE with data from the CLAS value. */
348 java_link_class_type (struct type *type, struct value *clas)
351 char *unqualified_name;
352 char *name = TYPE_TAG_NAME (type);
353 int ninterfaces, nfields, nmethods;
354 int type_is_object = 0;
355 struct fn_field *fn_fields;
356 struct fn_fieldlist *fn_fieldlists;
357 struct value *fields;
358 struct value *methods;
359 struct value *method = NULL;
360 struct value *field = NULL;
362 struct objfile *objfile = get_dynamics_objfile ();
365 unqualified_name = strrchr (name, '.');
366 if (unqualified_name == NULL)
367 unqualified_name = name;
370 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
371 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
373 tsuper = get_java_object_type ();
374 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
375 tsuper = TYPE_TARGET_TYPE (tsuper);
379 tsuper = type_from_class (temp);
385 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
387 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
389 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count", NULL, "structure"));
390 nfields += TYPE_N_BASECLASSES (type);
391 nfields++; /* Add one for dummy "class" field. */
392 TYPE_NFIELDS (type) = nfields;
393 TYPE_FIELDS (type) = (struct field *)
394 TYPE_ALLOC (type, sizeof (struct field) * nfields);
396 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
398 TYPE_FIELD_PRIVATE_BITS (type) =
399 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
400 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
402 TYPE_FIELD_PROTECTED_BITS (type) =
403 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
404 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
406 TYPE_FIELD_IGNORE_BITS (type) =
407 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
408 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
410 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
411 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
412 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
416 TYPE_BASECLASS (type, 0) = tsuper;
418 SET_TYPE_FIELD_PRIVATE (type, 0);
422 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
425 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
430 temp = value_struct_elt (&temp, NULL, "size_in_bytes", NULL, "structure");
431 TYPE_LENGTH (type) = value_as_long (temp);
435 nfields--; /* First set up dummy "class" field. */
436 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields),
437 VALUE_ADDRESS (clas) + VALUE_OFFSET (clas));
438 TYPE_FIELD_NAME (type, nfields) = "class";
439 TYPE_FIELD_TYPE (type, nfields) = VALUE_TYPE (clas);
440 SET_TYPE_FIELD_PRIVATE (type, nfields);
442 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
449 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
450 field = value_ind (fields);
453 { /* Re-use field value for next field. */
454 VALUE_ADDRESS (field) += TYPE_LENGTH (VALUE_TYPE (field));
455 VALUE_LAZY (field) = 1;
458 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
459 TYPE_FIELD_NAME (type, i) =
460 get_java_utf8_name (&objfile->type_obstack, temp);
462 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
465 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
466 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
468 if (accflags & 0x0001) /* public access */
472 if (accflags & 0x0002) /* private access */
474 SET_TYPE_FIELD_PRIVATE (type, i);
476 if (accflags & 0x0004) /* protected access */
478 SET_TYPE_FIELD_PROTECTED (type, i);
480 if (accflags & 0x0008) /* ACC_STATIC */
481 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
483 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
484 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
486 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
492 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
493 ftype = type_from_class (temp);
494 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
495 ftype = lookup_pointer_type (ftype);
496 TYPE_FIELD_TYPE (type, i) = ftype;
501 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
503 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
504 j = nmethods * sizeof (struct fn_field);
505 fn_fields = (struct fn_field *)
506 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
507 memset (fn_fields, 0, j);
508 fn_fieldlists = (struct fn_fieldlist *)
509 alloca (nmethods * sizeof (struct fn_fieldlist));
512 for (i = 0; i < nmethods; i++)
519 methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
520 method = value_ind (methods);
523 { /* Re-use method value for next method. */
524 VALUE_ADDRESS (method) += TYPE_LENGTH (VALUE_TYPE (method));
525 VALUE_LAZY (method) = 1;
528 /* Get method name. */
530 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
531 mname = get_java_utf8_name (&objfile->type_obstack, temp);
532 if (strcmp (mname, "<init>") == 0)
533 mname = unqualified_name;
535 /* Check for an existing method with the same name.
536 * This makes building the fn_fieldslists an O(nmethods**2)
537 * operation. That could be using hashing, but I doubt it
538 * is worth it. Note that we do maintain the order of methods
539 * in the inferior's Method table (as long as that is grouped
540 * by method name), which I think is desirable. --PB */
541 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
544 { /* No match - new method name. */
545 j = TYPE_NFN_FIELDS (type)++;
546 fn_fieldlists[j].name = mname;
547 fn_fieldlists[j].length = 1;
548 fn_fieldlists[j].fn_fields = &fn_fields[i];
552 if (strcmp (mname, fn_fieldlists[j].name) == 0)
553 { /* Found an existing method with the same name. */
555 if (mname != unqualified_name)
556 obstack_free (&objfile->type_obstack, mname);
557 mname = fn_fieldlists[j].name;
558 fn_fieldlists[j].length++;
559 k = i - k; /* Index of new slot. */
560 /* Shift intervening fn_fields (between k and i) down. */
561 for (l = i; l > k; l--)
562 fn_fields[l] = fn_fields[l - 1];
563 for (l = TYPE_NFN_FIELDS (type); --l > j;)
564 fn_fieldlists[l].fn_fields++;
567 k += fn_fieldlists[j].length;
569 fn_fields[k].physname = "";
570 fn_fields[k].is_stub = 1;
571 fn_fields[k].type = make_function_type (java_void_type, NULL); /* FIXME */
572 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
575 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
576 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
577 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
578 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
583 static struct type *java_object_type;
586 get_java_object_type (void)
588 if (java_object_type == NULL)
591 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN,
592 (int *) 0, (struct symtab **) NULL);
594 error ("cannot find java.lang.Object");
595 java_object_type = SYMBOL_TYPE (sym);
597 return java_object_type;
601 get_java_object_header_size (void)
603 struct type *objtype = get_java_object_type ();
605 return (2 * TARGET_PTR_BIT / TARGET_CHAR_BIT);
607 return TYPE_LENGTH (objtype);
611 is_object_type (struct type *type)
613 CHECK_TYPEDEF (type);
614 if (TYPE_CODE (type) == TYPE_CODE_PTR)
616 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
618 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
620 while (TYPE_N_BASECLASSES (ttype) > 0)
621 ttype = TYPE_BASECLASS (ttype, 0);
622 name = TYPE_TAG_NAME (ttype);
623 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
625 name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
626 if (name != NULL && strcmp (name, "vtable") == 0)
628 if (java_object_type == NULL)
629 java_object_type = type;
637 java_primitive_type (int signature)
642 return java_byte_type;
644 return java_short_type;
646 return java_int_type;
648 return java_long_type;
650 return java_boolean_type;
652 return java_char_type;
654 return java_float_type;
656 return java_double_type;
658 return java_void_type;
660 error ("unknown signature '%c' for primitive type", (char) signature);
663 /* If name[0 .. namelen-1] is the name of a primitive Java type,
664 return that type. Otherwise, return NULL. */
667 java_primitive_type_from_name (char *name, int namelen)
672 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
673 return java_byte_type;
674 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
675 return java_boolean_type;
678 if (namelen == 4 && memcmp (name, "char", 4) == 0)
679 return java_char_type;
681 if (namelen == 6 && memcmp (name, "double", 6) == 0)
682 return java_double_type;
685 if (namelen == 5 && memcmp (name, "float", 5) == 0)
686 return java_float_type;
689 if (namelen == 3 && memcmp (name, "int", 3) == 0)
690 return java_int_type;
693 if (namelen == 4 && memcmp (name, "long", 4) == 0)
694 return java_long_type;
697 if (namelen == 5 && memcmp (name, "short", 5) == 0)
698 return java_short_type;
701 if (namelen == 4 && memcmp (name, "void", 4) == 0)
702 return java_void_type;
708 /* Return the length (in bytes) of demangled name of the Java type
709 signature string SIGNATURE. */
712 java_demangled_signature_length (char *signature)
715 for (; *signature == '['; signature++)
716 array += 2; /* Two chars for "[]". */
717 switch (signature[0])
720 /* Subtract 2 for 'L' and ';'. */
721 return strlen (signature) - 2 + array;
723 return strlen (TYPE_NAME (java_primitive_type (signature[0]))) + array;
727 /* Demangle the Java type signature SIGNATURE, leaving the result in RESULT. */
730 java_demangled_signature_copy (char *result, char *signature)
735 while (*signature == '[')
740 switch (signature[0])
743 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
746 for (; *signature != ';' && *signature != '\0'; signature++)
748 if (*signature == '/')
755 ptr = TYPE_NAME (java_primitive_type (signature[0]));
757 strcpy (result, ptr);
768 /* Return the demangled name of the Java type signature string SIGNATURE,
769 as a freshly allocated copy. */
772 java_demangle_type_signature (char *signature)
774 int length = java_demangled_signature_length (signature);
775 char *result = xmalloc (length + 1);
776 java_demangled_signature_copy (result, signature);
777 result[length] = '\0';
782 java_lookup_type (char *signature)
784 switch (signature[0])
788 error ("java_lookup_type not fully implemented");
790 return java_primitive_type (signature[0]);
794 /* Return the type of TYPE followed by DIMS pairs of [ ].
795 If DIMS == 0, TYPE is returned. */
798 java_array_type (struct type *type, int dims)
800 struct type *range_type;
804 range_type = create_range_type (NULL, builtin_type_int, 0, 0);
805 /* FIXME This is bogus! Java arrays are not gdb arrays! */
806 type = create_array_type (NULL, type, range_type);
812 /* Create a Java string in the inferior from a (Utf8) literal. */
814 static struct value *
815 java_value_string (char *ptr, int len)
817 error ("not implemented - java_value_string"); /* FIXME */
820 /* Print the character C on STREAM as part of the contents of a literal
821 string whose delimiter is QUOTER. Note that that format for printing
822 characters and strings is language specific. */
825 java_emit_char (int c, struct ui_file *stream, int quoter)
831 fprintf_filtered (stream, "\\%c", c);
834 fputs_filtered ("\\b", stream);
837 fputs_filtered ("\\t", stream);
840 fputs_filtered ("\\n", stream);
843 fputs_filtered ("\\f", stream);
846 fputs_filtered ("\\r", stream);
850 fputc_filtered (c, stream);
852 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
857 static struct value *
858 evaluate_subexp_java (struct type *expect_type, register struct expression *exp,
859 register int *pos, enum noside noside)
864 enum exp_opcode op = exp->elts[*pos].opcode;
871 if (noside == EVAL_SKIP)
874 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
875 if (is_object_type (VALUE_TYPE (arg1)))
879 type = type_from_class (java_class_from_object (arg1));
880 arg1 = value_cast (lookup_pointer_type (type), arg1);
882 if (noside == EVAL_SKIP)
884 return value_ind (arg1);
886 case BINOP_SUBSCRIPT:
888 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
889 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
890 if (noside == EVAL_SKIP)
892 /* If the user attempts to subscript something that is not an
893 array or pointer type (like a plain int variable for example),
894 then report this as an error. */
897 type = check_typedef (VALUE_TYPE (arg1));
898 if (TYPE_CODE (type) == TYPE_CODE_PTR)
899 type = check_typedef (TYPE_TARGET_TYPE (type));
900 name = TYPE_NAME (type);
902 name = TYPE_TAG_NAME (type);
903 i = name == NULL ? 0 : strlen (name);
904 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
905 && i > 2 && name[i - 1] == ']')
909 struct type *el_type;
912 struct value *clas = java_class_from_object (arg1);
913 struct value *temp = clas;
914 /* Get CLASS_ELEMENT_TYPE of the array type. */
915 temp = value_struct_elt (&temp, NULL, "methods",
917 VALUE_TYPE (temp) = VALUE_TYPE (clas);
918 el_type = type_from_class (temp);
919 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
920 el_type = lookup_pointer_type (el_type);
922 if (noside == EVAL_AVOID_SIDE_EFFECTS)
923 return value_zero (el_type, VALUE_LVAL (arg1));
924 address = value_as_address (arg1);
925 address += JAVA_OBJECT_SIZE;
926 read_memory (address, buf4, 4);
927 length = (long) extract_signed_integer (buf4, 4);
928 index = (long) value_as_long (arg2);
929 if (index >= length || index < 0)
930 error ("array index (%ld) out of bounds (length: %ld)",
932 address = (address + 4) + index * TYPE_LENGTH (el_type);
933 return value_at (el_type, address, NULL);
935 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
937 if (noside == EVAL_AVOID_SIDE_EFFECTS)
938 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
940 return value_subscript (arg1, arg2);
943 error ("cannot subscript something of type `%s'", name);
945 error ("cannot subscript requested type");
949 i = longest_to_int (exp->elts[pc + 1].longconst);
950 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
951 if (noside == EVAL_SKIP)
953 return java_value_string (&exp->elts[pc + 2].string, i);
955 case STRUCTOP_STRUCT:
956 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
957 /* Convert object field (such as TYPE.class) to reference. */
958 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT)
959 arg1 = value_addr (arg1);
965 return evaluate_subexp_standard (expect_type, exp, pos, noside);
967 return value_from_longest (builtin_type_long, (LONGEST) 1);
971 java_create_fundamental_type (struct objfile *objfile, int typeid)
976 return java_void_type;
978 return java_boolean_type;
980 return java_char_type;
982 return java_float_type;
983 case FT_DBL_PREC_FLOAT:
984 return java_double_type;
987 return java_byte_type;
989 case FT_SIGNED_SHORT:
990 return java_short_type;
992 case FT_SIGNED_INTEGER:
993 return java_int_type;
996 return java_long_type;
998 return c_create_fundamental_type (objfile, typeid);
1001 static char *java_demangle (const char *mangled, int options)
1003 return cplus_demangle (mangled, options | DMGL_JAVA);
1007 /* Table mapping opcodes into strings for printing operators
1008 and precedences of the operators. */
1010 const struct op_print java_op_print_tab[] =
1012 {",", BINOP_COMMA, PREC_COMMA, 0},
1013 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1014 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1015 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1016 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1017 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1018 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1019 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1020 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1021 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1022 {">=", BINOP_GEQ, PREC_ORDER, 0},
1023 {">", BINOP_GTR, PREC_ORDER, 0},
1024 {"<", BINOP_LESS, PREC_ORDER, 0},
1025 {">>", BINOP_RSH, PREC_SHIFT, 0},
1026 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1028 {">>>", BINOP_ ? ? ?, PREC_SHIFT, 0},
1030 {"+", BINOP_ADD, PREC_ADD, 0},
1031 {"-", BINOP_SUB, PREC_ADD, 0},
1032 {"*", BINOP_MUL, PREC_MUL, 0},
1033 {"/", BINOP_DIV, PREC_MUL, 0},
1034 {"%", BINOP_REM, PREC_MUL, 0},
1035 {"-", UNOP_NEG, PREC_PREFIX, 0},
1036 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1037 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1038 {"*", UNOP_IND, PREC_PREFIX, 0},
1040 {"instanceof", ? ? ?, ? ? ?, 0},
1042 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1043 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1047 const struct language_defn java_language_defn =
1049 "java", /* Language name */
1057 evaluate_subexp_java,
1058 c_printchar, /* Print a character constant */
1059 c_printstr, /* Function to print string constant */
1060 java_emit_char, /* Function to print a single character */
1061 java_create_fundamental_type, /* Create fundamental type in this language */
1062 java_print_type, /* Print a type using appropriate syntax */
1063 java_val_print, /* Print a value using appropriate syntax */
1064 java_value_print, /* Print a top-level value */
1065 NULL, /* Language specific skip_trampoline */
1066 java_demangle, /* Language specific symbol demangler */
1067 {"", "", "", ""}, /* Binary format info */
1068 {"0%lo", "0", "o", ""}, /* Octal format info */
1069 {"%ld", "", "d", ""}, /* Decimal format info */
1070 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1071 java_op_print_tab, /* expression operators for printing */
1072 0, /* not c-style arrays */
1073 0, /* String lower bound */
1074 &builtin_type_char, /* Type of string elements */
1079 _initialize_java_language (void)
1082 java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
1083 java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
1084 java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
1085 java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
1086 java_boolean_type = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
1087 java_char_type = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
1088 java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
1089 java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
1090 java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
1092 add_language (&java_language_defn);
1095 /* Cleanup code that should be run on every "run".
1096 We should use make_run_cleanup to have this be called.
1097 But will that mess up values in value histry? FIXME */
1099 extern void java_rerun_cleanup (void);
1101 java_rerun_cleanup (void)
1103 if (class_symtab != NULL)
1105 free_symtab (class_symtab); /* ??? */
1106 class_symtab = NULL;
1108 if (dynamics_objfile != NULL)
1110 free_objfile (dynamics_objfile);
1111 dynamics_objfile = NULL;
1114 java_object_type = NULL;