1 /* Java language support routines for GDB, the GNU debugger.
3 Copyright (C) 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
24 #include "expression.h"
25 #include "parser-defs.h"
31 #include "gdb_string.h"
38 #include "dictionary.h"
40 #include "gdb_assert.h"
42 struct type *java_int_type;
43 struct type *java_byte_type;
44 struct type *java_short_type;
45 struct type *java_long_type;
46 struct type *java_boolean_type;
47 struct type *java_char_type;
48 struct type *java_float_type;
49 struct type *java_double_type;
50 struct type *java_void_type;
54 extern void _initialize_java_language (void);
56 static int java_demangled_signature_length (char *);
57 static void java_demangled_signature_copy (char *, char *);
59 static struct symtab *get_java_class_symtab (void);
60 static char *get_java_utf8_name (struct obstack *obstack, struct value *name);
61 static int java_class_is_primitive (struct value *clas);
62 static struct value *java_value_string (char *ptr, int len);
64 static void java_emit_char (int c, struct type *type,
65 struct ui_file * stream, int quoter);
67 static char *java_class_name_from_physname (const char *physname);
69 /* This objfile contains symtabs that have been dynamically created
70 to record dynamically loaded Java classes and dynamically
71 compiled java methods. */
73 static struct objfile *dynamics_objfile = NULL;
75 static struct type *java_link_class_type (struct type *, struct value *);
77 /* FIXME: carlton/2003-02-04: This is the main or only caller of
78 allocate_objfile with first argument NULL; as a result, this code
79 breaks every so often. Somebody should write a test case that
80 exercises GDB in various ways (e.g. something involving loading a
81 dynamic library) after this code has been called. */
83 static struct objfile *
84 get_dynamics_objfile (void)
86 if (dynamics_objfile == NULL)
88 dynamics_objfile = allocate_objfile (NULL, 0);
90 return dynamics_objfile;
94 /* symtab contains classes read from the inferior. */
96 static struct symtab *class_symtab = NULL;
98 static void free_class_block (struct symtab *symtab);
100 static struct symtab *
101 get_java_class_symtab (void)
103 if (class_symtab == NULL)
105 struct objfile *objfile = get_dynamics_objfile ();
106 struct blockvector *bv;
108 class_symtab = allocate_symtab ("<java-classes>", objfile);
109 class_symtab->language = language_java;
110 bv = (struct blockvector *)
111 obstack_alloc (&objfile->objfile_obstack,
112 sizeof (struct blockvector) + sizeof (struct block *));
113 BLOCKVECTOR_NBLOCKS (bv) = 1;
114 BLOCKVECTOR (class_symtab) = bv;
116 /* Allocate dummy STATIC_BLOCK. */
117 bl = allocate_block (&objfile->objfile_obstack);
118 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
120 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
122 /* Allocate GLOBAL_BLOCK. */
123 bl = allocate_block (&objfile->objfile_obstack);
124 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
125 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
126 class_symtab->free_func = free_class_block;
132 add_class_symtab_symbol (struct symbol *sym)
134 struct symtab *symtab = get_java_class_symtab ();
135 struct blockvector *bv = BLOCKVECTOR (symtab);
136 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
139 static struct symbol *add_class_symbol (struct type *type, CORE_ADDR addr);
141 static struct symbol *
142 add_class_symbol (struct type *type, CORE_ADDR addr)
145 sym = (struct symbol *)
146 obstack_alloc (&dynamics_objfile->objfile_obstack, sizeof (struct symbol));
147 memset (sym, 0, sizeof (struct symbol));
148 SYMBOL_LANGUAGE (sym) = language_java;
149 SYMBOL_SET_LINKAGE_NAME (sym, TYPE_TAG_NAME (type));
150 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
151 /* SYMBOL_VALUE (sym) = valu; */
152 SYMBOL_TYPE (sym) = type;
153 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
154 SYMBOL_VALUE_ADDRESS (sym) = addr;
158 /* Free the dynamic symbols block. */
160 free_class_block (struct symtab *symtab)
162 struct blockvector *bv = BLOCKVECTOR (symtab);
163 struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
165 dict_free (BLOCK_DICT (bl));
170 java_lookup_class (char *name)
173 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN, NULL);
175 return SYMBOL_TYPE (sym);
178 if (called from parser)
180 call lookup_class (or similar) in inferior;
184 addr = found in inferior;
189 type = alloc_type (objfile);
190 TYPE_CODE (type) = TYPE_CODE_STRUCT;
191 INIT_CPLUS_SPECIFIC (type);
192 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name), &objfile->objfile_obstack);
193 TYPE_STUB (type) = 1;
197 /* FIXME - should search inferior's symbol table. */
202 /* Return a nul-terminated string (allocated on OBSTACK) for
203 a name given by NAME (which has type Utf8Const*). */
206 get_java_utf8_name (struct obstack *obstack, struct value *name)
209 struct value *temp = name;
212 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
213 name_length = (int) value_as_long (temp);
214 data_addr = value_address (temp) + TYPE_LENGTH (value_type (temp));
215 chrs = obstack_alloc (obstack, name_length + 1);
216 chrs[name_length] = '\0';
217 read_memory (data_addr, (gdb_byte *) 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));
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);
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 /* Do not use the "fake" dynamics objfile to own dynamically generated
306 types, as it does not provide an architecture, and it would not help
307 manage the lifetime of these types anyway. */
308 type = alloc_type (NULL);
309 TYPE_CODE (type) = TYPE_CODE_STRUCT;
310 INIT_CPLUS_SPECIFIC (type);
314 char *signature = name;
315 int namelen = java_demangled_signature_length (signature);
316 if (namelen > strlen (name))
317 name = obstack_alloc (&objfile->objfile_obstack, namelen + 1);
318 java_demangled_signature_copy (name, signature);
319 name[namelen] = '\0';
322 /* Set array element type. */
323 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
324 deprecated_set_value_type (temp, lookup_pointer_type (value_type (clas)));
325 TYPE_TARGET_TYPE (type) = type_from_class (temp);
328 ALLOCATE_CPLUS_STRUCT_TYPE (type);
329 TYPE_TAG_NAME (type) = name;
331 add_class_symtab_symbol (add_class_symbol (type, addr));
332 return java_link_class_type (type, clas);
335 /* Fill in class TYPE with data from the CLAS value. */
338 java_link_class_type (struct type *type, struct value *clas)
341 char *unqualified_name;
342 char *name = TYPE_TAG_NAME (type);
343 int ninterfaces, nfields, nmethods;
344 int type_is_object = 0;
345 struct fn_field *fn_fields;
346 struct fn_fieldlist *fn_fieldlists;
347 struct value *fields;
348 struct value *methods;
349 struct value *method = NULL;
350 struct value *field = NULL;
352 struct objfile *objfile = get_dynamics_objfile ();
355 gdb_assert (name != NULL);
356 unqualified_name = strrchr (name, '.');
357 if (unqualified_name == NULL)
358 unqualified_name = name;
361 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
362 if (strcmp (name, "java.lang.Object") == 0)
364 tsuper = get_java_object_type ();
365 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
366 tsuper = TYPE_TARGET_TYPE (tsuper);
370 tsuper = type_from_class (temp);
376 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
378 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
380 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count", NULL, "structure"));
381 nfields += TYPE_N_BASECLASSES (type);
382 nfields++; /* Add one for dummy "class" field. */
383 TYPE_NFIELDS (type) = nfields;
384 TYPE_FIELDS (type) = (struct field *)
385 TYPE_ALLOC (type, sizeof (struct field) * nfields);
387 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
389 TYPE_FIELD_PRIVATE_BITS (type) =
390 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
391 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
393 TYPE_FIELD_PROTECTED_BITS (type) =
394 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
395 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
397 TYPE_FIELD_IGNORE_BITS (type) =
398 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
399 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
401 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
402 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
403 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
407 TYPE_BASECLASS (type, 0) = tsuper;
409 SET_TYPE_FIELD_PRIVATE (type, 0);
413 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
416 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
421 temp = value_struct_elt (&temp, NULL, "size_in_bytes", NULL, "structure");
422 TYPE_LENGTH (type) = value_as_long (temp);
426 nfields--; /* First set up dummy "class" field. */
427 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas));
428 TYPE_FIELD_NAME (type, nfields) = "class";
429 TYPE_FIELD_TYPE (type, nfields) = value_type (clas);
430 SET_TYPE_FIELD_PRIVATE (type, nfields);
432 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
439 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
440 field = value_ind (fields);
443 { /* Re-use field value for next field. */
445 = value_address (field) + TYPE_LENGTH (value_type (field));
446 set_value_address (field, addr);
447 set_value_lazy (field, 1);
450 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
451 TYPE_FIELD_NAME (type, i) =
452 get_java_utf8_name (&objfile->objfile_obstack, temp);
454 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
457 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
458 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
460 if (accflags & 0x0001) /* public access */
464 if (accflags & 0x0002) /* private access */
466 SET_TYPE_FIELD_PRIVATE (type, i);
468 if (accflags & 0x0004) /* protected access */
470 SET_TYPE_FIELD_PROTECTED (type, i);
472 if (accflags & 0x0008) /* ACC_STATIC */
473 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
475 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
476 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
478 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
484 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
485 ftype = type_from_class (temp);
486 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
487 ftype = lookup_pointer_type (ftype);
488 TYPE_FIELD_TYPE (type, i) = ftype;
493 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
495 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
496 j = nmethods * sizeof (struct fn_field);
497 fn_fields = (struct fn_field *)
498 obstack_alloc (&dynamics_objfile->objfile_obstack, j);
499 memset (fn_fields, 0, j);
500 fn_fieldlists = (struct fn_fieldlist *)
501 alloca (nmethods * sizeof (struct fn_fieldlist));
504 for (i = 0; i < nmethods; i++)
511 methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
512 method = value_ind (methods);
515 { /* Re-use method value for next method. */
517 = value_address (method) + TYPE_LENGTH (value_type (method));
518 set_value_address (method, addr);
519 set_value_lazy (method, 1);
522 /* Get method name. */
524 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
525 mname = get_java_utf8_name (&objfile->objfile_obstack, temp);
526 if (strcmp (mname, "<init>") == 0)
527 mname = unqualified_name;
529 /* Check for an existing method with the same name.
530 * This makes building the fn_fieldslists an O(nmethods**2)
531 * operation. That could be using hashing, but I doubt it
532 * is worth it. Note that we do maintain the order of methods
533 * in the inferior's Method table (as long as that is grouped
534 * by method name), which I think is desirable. --PB */
535 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
538 { /* No match - new method name. */
539 j = TYPE_NFN_FIELDS (type)++;
540 fn_fieldlists[j].name = mname;
541 fn_fieldlists[j].length = 1;
542 fn_fieldlists[j].fn_fields = &fn_fields[i];
546 if (strcmp (mname, fn_fieldlists[j].name) == 0)
547 { /* Found an existing method with the same name. */
549 if (mname != unqualified_name)
550 obstack_free (&objfile->objfile_obstack, mname);
551 mname = fn_fieldlists[j].name;
552 fn_fieldlists[j].length++;
553 k = i - k; /* Index of new slot. */
554 /* Shift intervening fn_fields (between k and i) down. */
555 for (l = i; l > k; l--)
556 fn_fields[l] = fn_fields[l - 1];
557 for (l = TYPE_NFN_FIELDS (type); --l > j;)
558 fn_fieldlists[l].fn_fields++;
561 k += fn_fieldlists[j].length;
563 fn_fields[k].physname = "";
564 fn_fields[k].is_stub = 1;
566 fn_fields[k].type = lookup_function_type (java_void_type);
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->objfile_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, NULL);
588 error (_("cannot find java.lang.Object"));
589 java_object_type = SYMBOL_TYPE (sym);
591 return java_object_type;
595 get_java_object_header_size (struct gdbarch *gdbarch)
597 struct type *objtype = get_java_object_type ();
599 return (2 * gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
601 return TYPE_LENGTH (objtype);
605 is_object_type (struct type *type)
607 CHECK_TYPEDEF (type);
608 if (TYPE_CODE (type) == TYPE_CODE_PTR)
610 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
612 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
614 while (TYPE_N_BASECLASSES (ttype) > 0)
615 ttype = TYPE_BASECLASS (ttype, 0);
616 name = TYPE_TAG_NAME (ttype);
617 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
619 name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
620 if (name != NULL && strcmp (name, "vtable") == 0)
622 if (java_object_type == NULL)
623 java_object_type = type;
631 java_primitive_type (int signature)
636 return java_byte_type;
638 return java_short_type;
640 return java_int_type;
642 return java_long_type;
644 return java_boolean_type;
646 return java_char_type;
648 return java_float_type;
650 return java_double_type;
652 return java_void_type;
654 error (_("unknown signature '%c' for primitive type"), (char) signature);
657 /* If name[0 .. namelen-1] is the name of a primitive Java type,
658 return that type. Otherwise, return NULL. */
661 java_primitive_type_from_name (char *name, int namelen)
666 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
667 return java_byte_type;
668 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
669 return java_boolean_type;
672 if (namelen == 4 && memcmp (name, "char", 4) == 0)
673 return java_char_type;
675 if (namelen == 6 && memcmp (name, "double", 6) == 0)
676 return java_double_type;
679 if (namelen == 5 && memcmp (name, "float", 5) == 0)
680 return java_float_type;
683 if (namelen == 3 && memcmp (name, "int", 3) == 0)
684 return java_int_type;
687 if (namelen == 4 && memcmp (name, "long", 4) == 0)
688 return java_long_type;
691 if (namelen == 5 && memcmp (name, "short", 5) == 0)
692 return java_short_type;
695 if (namelen == 4 && memcmp (name, "void", 4) == 0)
696 return java_void_type;
702 /* Return the length (in bytes) of demangled name of the Java type
703 signature string SIGNATURE. */
706 java_demangled_signature_length (char *signature)
709 for (; *signature == '['; signature++)
710 array += 2; /* Two chars for "[]". */
711 switch (signature[0])
714 /* Subtract 2 for 'L' and ';'. */
715 return strlen (signature) - 2 + array;
717 return strlen (TYPE_NAME (java_primitive_type (signature[0]))) + array;
721 /* Demangle the Java type signature SIGNATURE, leaving the result in RESULT. */
724 java_demangled_signature_copy (char *result, char *signature)
729 while (*signature == '[')
734 switch (signature[0])
737 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
740 for (; *signature != ';' && *signature != '\0'; signature++)
742 if (*signature == '/')
749 ptr = TYPE_NAME (java_primitive_type (signature[0]));
751 strcpy (result, ptr);
762 /* Return the demangled name of the Java type signature string SIGNATURE,
763 as a freshly allocated copy. */
766 java_demangle_type_signature (char *signature)
768 int length = java_demangled_signature_length (signature);
769 char *result = xmalloc (length + 1);
770 java_demangled_signature_copy (result, signature);
771 result[length] = '\0';
775 /* Return the type of TYPE followed by DIMS pairs of [ ].
776 If DIMS == 0, TYPE is returned. */
779 java_array_type (struct type *type, int dims)
781 struct type *range_type;
785 range_type = create_range_type (NULL, builtin_type_int32, 0, 0);
786 /* FIXME This is bogus! Java arrays are not gdb arrays! */
787 type = create_array_type (NULL, type, range_type);
793 /* Create a Java string in the inferior from a (Utf8) literal. */
795 static struct value *
796 java_value_string (char *ptr, int len)
798 error (_("not implemented - java_value_string")); /* FIXME */
801 /* Print the character C on STREAM as part of the contents of a literal
802 string whose delimiter is QUOTER. Note that that format for printing
803 characters and strings is language specific. */
806 java_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
812 fprintf_filtered (stream, "\\%c", c);
815 fputs_filtered ("\\b", stream);
818 fputs_filtered ("\\t", stream);
821 fputs_filtered ("\\n", stream);
824 fputs_filtered ("\\f", stream);
827 fputs_filtered ("\\r", stream);
831 fputc_filtered (c, stream);
833 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
838 static struct value *
839 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
840 int *pos, enum noside noside)
845 enum exp_opcode op = exp->elts[*pos].opcode;
852 if (noside == EVAL_SKIP)
855 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
856 if (is_object_type (value_type (arg1)))
860 type = type_from_class (java_class_from_object (arg1));
861 arg1 = value_cast (lookup_pointer_type (type), arg1);
863 if (noside == EVAL_SKIP)
865 return value_ind (arg1);
867 case BINOP_SUBSCRIPT:
869 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
870 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
871 if (noside == EVAL_SKIP)
873 /* If the user attempts to subscript something that is not an
874 array or pointer type (like a plain int variable for example),
875 then report this as an error. */
877 arg1 = coerce_ref (arg1);
878 type = check_typedef (value_type (arg1));
879 if (TYPE_CODE (type) == TYPE_CODE_PTR)
880 type = check_typedef (TYPE_TARGET_TYPE (type));
881 name = TYPE_NAME (type);
883 name = TYPE_TAG_NAME (type);
884 i = name == NULL ? 0 : strlen (name);
885 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
886 && i > 2 && name[i - 1] == ']')
890 struct type *el_type;
893 struct value *clas = java_class_from_object (arg1);
894 struct value *temp = clas;
895 /* Get CLASS_ELEMENT_TYPE of the array type. */
896 temp = value_struct_elt (&temp, NULL, "methods",
898 deprecated_set_value_type (temp, value_type (clas));
899 el_type = type_from_class (temp);
900 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
901 el_type = lookup_pointer_type (el_type);
903 if (noside == EVAL_AVOID_SIDE_EFFECTS)
904 return value_zero (el_type, VALUE_LVAL (arg1));
905 address = value_as_address (arg1);
906 address += get_java_object_header_size (exp->gdbarch);
907 read_memory (address, buf4, 4);
908 length = (long) extract_signed_integer (buf4, 4);
909 index = (long) value_as_long (arg2);
910 if (index >= length || index < 0)
911 error (_("array index (%ld) out of bounds (length: %ld)"),
913 address = (address + 4) + index * TYPE_LENGTH (el_type);
914 return value_at (el_type, address);
916 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
918 if (noside == EVAL_AVOID_SIDE_EFFECTS)
919 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
921 return value_subscript (arg1, arg2);
924 error (_("cannot subscript something of type `%s'"), name);
926 error (_("cannot subscript requested type"));
930 i = longest_to_int (exp->elts[pc + 1].longconst);
931 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
932 if (noside == EVAL_SKIP)
934 return java_value_string (&exp->elts[pc + 2].string, i);
937 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
938 /* Convert object field (such as TYPE.class) to reference. */
939 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_STRUCT)
940 arg1 = value_addr (arg1);
946 return evaluate_subexp_standard (expect_type, exp, pos, noside);
948 return value_from_longest (builtin_type_int8, (LONGEST) 1);
951 static char *java_demangle (const char *mangled, int options)
953 return cplus_demangle (mangled, options | DMGL_JAVA);
956 /* Find the member function name of the demangled name NAME. NAME
957 must be a method name including arguments, in order to correctly
958 locate the last component.
960 This function return a pointer to the first dot before the
961 member function name, or NULL if the name was not of the
965 java_find_last_component (const char *name)
969 /* Find argument list. */
970 p = strchr (name, '(');
975 /* Back up and find first dot prior to argument list. */
976 while (p > name && *p != '.')
985 /* Return the name of the class containing method PHYSNAME. */
988 java_class_name_from_physname (const char *physname)
993 char *demangled_name = java_demangle (physname, DMGL_PARAMS | DMGL_ANSI);
995 if (demangled_name == NULL)
998 end = java_find_last_component (demangled_name);
1001 ret = xmalloc (end - demangled_name + 1);
1002 memcpy (ret, demangled_name, end - demangled_name);
1003 ret[end - demangled_name] = '\0';
1006 xfree (demangled_name);
1010 /* Table mapping opcodes into strings for printing operators
1011 and precedences of the operators. */
1013 const struct op_print java_op_print_tab[] =
1015 {",", BINOP_COMMA, PREC_COMMA, 0},
1016 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1017 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1018 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1019 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1020 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1021 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1022 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1023 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1024 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1025 {">=", BINOP_GEQ, PREC_ORDER, 0},
1026 {">", BINOP_GTR, PREC_ORDER, 0},
1027 {"<", BINOP_LESS, PREC_ORDER, 0},
1028 {">>", BINOP_RSH, PREC_SHIFT, 0},
1029 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1031 {">>>", BINOP_ ? ? ?, PREC_SHIFT, 0},
1033 {"+", BINOP_ADD, PREC_ADD, 0},
1034 {"-", BINOP_SUB, PREC_ADD, 0},
1035 {"*", BINOP_MUL, PREC_MUL, 0},
1036 {"/", BINOP_DIV, PREC_MUL, 0},
1037 {"%", BINOP_REM, PREC_MUL, 0},
1038 {"-", UNOP_NEG, PREC_PREFIX, 0},
1039 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1040 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1041 {"*", UNOP_IND, PREC_PREFIX, 0},
1043 {"instanceof", ? ? ?, ? ? ?, 0},
1045 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1046 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1050 enum java_primitive_types
1052 java_primitive_type_int,
1053 java_primitive_type_short,
1054 java_primitive_type_long,
1055 java_primitive_type_byte,
1056 java_primitive_type_boolean,
1057 java_primitive_type_char,
1058 java_primitive_type_float,
1059 java_primitive_type_double,
1060 java_primitive_type_void,
1061 nr_java_primitive_types
1065 java_language_arch_info (struct gdbarch *gdbarch,
1066 struct language_arch_info *lai)
1068 lai->string_char_type = java_char_type;
1069 lai->primitive_type_vector
1070 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_java_primitive_types + 1,
1072 lai->primitive_type_vector [java_primitive_type_int]
1074 lai->primitive_type_vector [java_primitive_type_short]
1076 lai->primitive_type_vector [java_primitive_type_long]
1078 lai->primitive_type_vector [java_primitive_type_byte]
1080 lai->primitive_type_vector [java_primitive_type_boolean]
1081 = java_boolean_type;
1082 lai->primitive_type_vector [java_primitive_type_char]
1084 lai->primitive_type_vector [java_primitive_type_float]
1086 lai->primitive_type_vector [java_primitive_type_double]
1088 lai->primitive_type_vector [java_primitive_type_void]
1091 lai->bool_type_symbol = "boolean";
1092 lai->bool_type_default = java_boolean_type;
1095 const struct exp_descriptor exp_descriptor_java =
1097 print_subexp_standard,
1098 operator_length_standard,
1100 dump_subexp_body_standard,
1101 evaluate_subexp_java
1104 const struct language_defn java_language_defn =
1106 "java", /* Language name */
1113 &exp_descriptor_java,
1117 c_printchar, /* Print a character constant */
1118 c_printstr, /* Function to print string constant */
1119 java_emit_char, /* Function to print a single character */
1120 java_print_type, /* Print a type using appropriate syntax */
1121 default_print_typedef, /* Print a typedef using appropriate syntax */
1122 java_val_print, /* Print a value using appropriate syntax */
1123 java_value_print, /* Print a top-level value */
1124 NULL, /* Language specific skip_trampoline */
1125 "this", /* name_of_this */
1126 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1127 basic_lookup_transparent_type,/* lookup_transparent_type */
1128 java_demangle, /* Language specific symbol demangler */
1129 java_class_name_from_physname,/* Language specific class name */
1130 java_op_print_tab, /* expression operators for printing */
1131 0, /* not c-style arrays */
1132 0, /* String lower bound */
1133 default_word_break_characters,
1134 default_make_symbol_completion_list,
1135 java_language_arch_info,
1136 default_print_array_index,
1137 default_pass_by_reference,
1143 _initialize_java_language (void)
1146 java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
1147 java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
1148 java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
1149 java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
1150 java_boolean_type = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
1151 java_char_type = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
1152 java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
1153 java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
1154 java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
1156 add_language (&java_language_defn);