2003-09-11 David Carlton <carlton@kealia.com>
[deliverable/binutils-gdb.git] / gdb / jv-lang.c
1 /* Java language support routines for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "gdbtypes.h"
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdb_string.h"
32 #include "value.h"
33 #include "c-lang.h"
34 #include "jv-lang.h"
35 #include "gdbcore.h"
36 #include "block.h"
37 #include "demangle.h"
38 #include "dictionary.h"
39 #include <ctype.h>
40
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;
50
51 /* Local functions */
52
53 extern void _initialize_java_language (void);
54
55 static int java_demangled_signature_length (char *);
56 static void java_demangled_signature_copy (char *, char *);
57
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);
63
64 static void java_emit_char (int c, struct ui_file * stream, int quoter);
65
66 /* This objfile contains symtabs that have been dynamically created
67 to record dynamically loaded Java classes and dynamically
68 compiled java methods. */
69
70 static struct objfile *dynamics_objfile = NULL;
71
72 static struct type *java_link_class_type (struct type *, struct value *);
73
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. */
79
80 static struct objfile *
81 get_dynamics_objfile (void)
82 {
83 if (dynamics_objfile == NULL)
84 {
85 dynamics_objfile = allocate_objfile (NULL, 0);
86 }
87 return dynamics_objfile;
88 }
89
90 #if 1
91 /* symtab contains classes read from the inferior. */
92
93 static struct symtab *class_symtab = NULL;
94
95 static void free_class_block (struct symtab *symtab);
96
97 static struct symtab *
98 get_java_class_symtab (void)
99 {
100 if (class_symtab == NULL)
101 {
102 struct objfile *objfile = get_dynamics_objfile ();
103 struct blockvector *bv;
104 struct block *bl;
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;
112
113 /* Allocate dummy STATIC_BLOCK. */
114 bl = allocate_block (&objfile->symbol_obstack);
115 BLOCK_DICT (bl) = dict_create_linear (&objfile->symbol_obstack,
116 NULL);
117 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
118
119 /* Allocate GLOBAL_BLOCK. */
120 bl = allocate_block (&objfile->symbol_obstack);
121 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
122 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
123 class_symtab->free_func = free_class_block;
124 }
125 return class_symtab;
126 }
127
128 static void
129 add_class_symtab_symbol (struct symbol *sym)
130 {
131 struct symtab *symtab = get_java_class_symtab ();
132 struct blockvector *bv = BLOCKVECTOR (symtab);
133 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
134 }
135
136 static struct symbol *add_class_symbol (struct type *type, CORE_ADDR addr);
137
138 static struct symbol *
139 add_class_symbol (struct type *type, CORE_ADDR addr)
140 {
141 struct symbol *sym;
142 sym = (struct symbol *)
143 obstack_alloc (&dynamics_objfile->symbol_obstack, sizeof (struct symbol));
144 memset (sym, 0, sizeof (struct symbol));
145 SYMBOL_LANGUAGE (sym) = language_java;
146 DEPRECATED_SYMBOL_NAME (sym) = TYPE_TAG_NAME (type);
147 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
148 /* SYMBOL_VALUE (sym) = valu; */
149 SYMBOL_TYPE (sym) = type;
150 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
151 SYMBOL_VALUE_ADDRESS (sym) = addr;
152 return sym;
153 }
154
155 /* Free the dynamic symbols block. */
156 static void
157 free_class_block (struct symtab *symtab)
158 {
159 struct blockvector *bv = BLOCKVECTOR (symtab);
160 struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
161
162 dict_free (BLOCK_DICT (bl));
163 }
164 #endif
165
166 struct type *
167 java_lookup_class (char *name)
168 {
169 struct symbol *sym;
170 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN,
171 (int *) 0, (struct symtab **) NULL);
172 if (sym != NULL)
173 return SYMBOL_TYPE (sym);
174 #if 0
175 CORE_ADDR addr;
176 if (called from parser)
177 {
178 call lookup_class (or similar) in inferior;
179 if not
180 found:
181 return NULL;
182 addr = found in inferior;
183 }
184 else
185 addr = 0;
186 struct type *type;
187 type = alloc_type (objfile);
188 TYPE_CODE (type) = TYPE_CODE_STRUCT;
189 INIT_CPLUS_SPECIFIC (type);
190 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name), &objfile->type_obstack);
191 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
192 TYPE ? = addr;
193 return type;
194 #else
195 /* FIXME - should search inferior's symbol table. */
196 return NULL;
197 #endif
198 }
199
200 /* Return a nul-terminated string (allocated on OBSTACK) for
201 a name given by NAME (which has type Utf8Const*). */
202
203 char *
204 get_java_utf8_name (struct obstack *obstack, struct value *name)
205 {
206 char *chrs;
207 struct value *temp = name;
208 int name_length;
209 CORE_ADDR data_addr;
210 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
211 name_length = (int) value_as_long (temp);
212 data_addr = VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
213 + TYPE_LENGTH (VALUE_TYPE (temp));
214 chrs = obstack_alloc (obstack, name_length + 1);
215 chrs[name_length] = '\0';
216 read_memory (data_addr, chrs, name_length);
217 return chrs;
218 }
219
220 struct value *
221 java_class_from_object (struct value *obj_val)
222 {
223 /* This is all rather inefficient, since the offsets of vtable and
224 class are fixed. FIXME */
225 struct value *vtable_val;
226
227 if (TYPE_CODE (VALUE_TYPE (obj_val)) == TYPE_CODE_PTR
228 && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (obj_val))) == 0)
229 obj_val = value_at (get_java_object_type (),
230 value_as_address (obj_val), NULL);
231
232 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
233 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
234 }
235
236 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
237 static int
238 java_class_is_primitive (struct value *clas)
239 {
240 struct value *vtable = value_struct_elt (&clas, NULL, "vtable", NULL, "struct");
241 CORE_ADDR i = value_as_address (vtable);
242 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
243 }
244
245 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
246
247 struct type *
248 type_from_class (struct value *clas)
249 {
250 struct type *type;
251 char *name;
252 struct value *temp;
253 struct objfile *objfile;
254 struct value *utf8_name;
255 char *nptr;
256 CORE_ADDR addr;
257 struct block *bl;
258 struct dict_iterator iter;
259 int is_array = 0;
260
261 type = check_typedef (VALUE_TYPE (clas));
262 if (TYPE_CODE (type) == TYPE_CODE_PTR)
263 {
264 if (value_logical_not (clas))
265 return NULL;
266 clas = value_ind (clas);
267 }
268 addr = VALUE_ADDRESS (clas) + VALUE_OFFSET (clas);
269
270 #if 0
271 get_java_class_symtab ();
272 bl = BLOCKVECTOR_BLOCK (BLOCKVECTOR (class_symtab), GLOBAL_BLOCK);
273 ALL_BLOCK_SYMBOLS (block, iter, sym)
274 {
275 if (SYMBOL_VALUE_ADDRESS (sym) == addr)
276 return SYMBOL_TYPE (sym);
277 }
278 #endif
279
280 objfile = get_dynamics_objfile ();
281 if (java_class_is_primitive (clas))
282 {
283 struct value *sig;
284 temp = clas;
285 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
286 return java_primitive_type (value_as_long (sig));
287 }
288
289 /* Get Class name. */
290 /* if clasloader non-null, prepend loader address. FIXME */
291 temp = clas;
292 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
293 name = get_java_utf8_name (&objfile->type_obstack, utf8_name);
294 for (nptr = name; *nptr != 0; nptr++)
295 {
296 if (*nptr == '/')
297 *nptr = '.';
298 }
299
300 type = java_lookup_class (name);
301 if (type != NULL)
302 return type;
303
304 type = alloc_type (objfile);
305 TYPE_CODE (type) = TYPE_CODE_STRUCT;
306 INIT_CPLUS_SPECIFIC (type);
307
308 if (name[0] == '[')
309 {
310 char *signature = name;
311 int namelen = java_demangled_signature_length (signature);
312 if (namelen > strlen (name))
313 name = obstack_alloc (&objfile->type_obstack, namelen + 1);
314 java_demangled_signature_copy (name, signature);
315 name[namelen] = '\0';
316 is_array = 1;
317 temp = clas;
318 /* Set array element type. */
319 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
320 VALUE_TYPE (temp) = lookup_pointer_type (VALUE_TYPE (clas));
321 TYPE_TARGET_TYPE (type) = type_from_class (temp);
322 }
323
324 ALLOCATE_CPLUS_STRUCT_TYPE (type);
325 TYPE_TAG_NAME (type) = name;
326
327 add_class_symtab_symbol (add_class_symbol (type, addr));
328 return java_link_class_type (type, clas);
329 }
330
331 /* Fill in class TYPE with data from the CLAS value. */
332
333 struct type *
334 java_link_class_type (struct type *type, struct value *clas)
335 {
336 struct value *temp;
337 char *unqualified_name;
338 char *name = TYPE_TAG_NAME (type);
339 int ninterfaces, nfields, nmethods;
340 int type_is_object = 0;
341 struct fn_field *fn_fields;
342 struct fn_fieldlist *fn_fieldlists;
343 struct value *fields;
344 struct value *methods;
345 struct value *method = NULL;
346 struct value *field = NULL;
347 int i, j;
348 struct objfile *objfile = get_dynamics_objfile ();
349 struct type *tsuper;
350
351 unqualified_name = strrchr (name, '.');
352 if (unqualified_name == NULL)
353 unqualified_name = name;
354
355 temp = clas;
356 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
357 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
358 {
359 tsuper = get_java_object_type ();
360 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
361 tsuper = TYPE_TARGET_TYPE (tsuper);
362 type_is_object = 1;
363 }
364 else
365 tsuper = type_from_class (temp);
366
367 #if 1
368 ninterfaces = 0;
369 #else
370 temp = clas;
371 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
372 #endif
373 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
374 temp = clas;
375 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count", NULL, "structure"));
376 nfields += TYPE_N_BASECLASSES (type);
377 nfields++; /* Add one for dummy "class" field. */
378 TYPE_NFIELDS (type) = nfields;
379 TYPE_FIELDS (type) = (struct field *)
380 TYPE_ALLOC (type, sizeof (struct field) * nfields);
381
382 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
383
384 TYPE_FIELD_PRIVATE_BITS (type) =
385 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
386 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
387
388 TYPE_FIELD_PROTECTED_BITS (type) =
389 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
390 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
391
392 TYPE_FIELD_IGNORE_BITS (type) =
393 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
394 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
395
396 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
397 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
398 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
399
400 if (tsuper != NULL)
401 {
402 TYPE_BASECLASS (type, 0) = tsuper;
403 if (type_is_object)
404 SET_TYPE_FIELD_PRIVATE (type, 0);
405 }
406
407 i = strlen (name);
408 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
409 {
410 /* FIXME */
411 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
412 }
413 else
414 {
415 temp = clas;
416 temp = value_struct_elt (&temp, NULL, "size_in_bytes", NULL, "structure");
417 TYPE_LENGTH (type) = value_as_long (temp);
418 }
419
420 fields = NULL;
421 nfields--; /* First set up dummy "class" field. */
422 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields),
423 VALUE_ADDRESS (clas) + VALUE_OFFSET (clas));
424 TYPE_FIELD_NAME (type, nfields) = "class";
425 TYPE_FIELD_TYPE (type, nfields) = VALUE_TYPE (clas);
426 SET_TYPE_FIELD_PRIVATE (type, nfields);
427
428 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
429 {
430 int accflags;
431 int boffset;
432 if (fields == NULL)
433 {
434 temp = clas;
435 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
436 field = value_ind (fields);
437 }
438 else
439 { /* Re-use field value for next field. */
440 VALUE_ADDRESS (field) += TYPE_LENGTH (VALUE_TYPE (field));
441 VALUE_LAZY (field) = 1;
442 }
443 temp = field;
444 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
445 TYPE_FIELD_NAME (type, i) =
446 get_java_utf8_name (&objfile->type_obstack, temp);
447 temp = field;
448 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
449 NULL, "structure"));
450 temp = field;
451 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
452 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
453 NULL, "structure"));
454 if (accflags & 0x0001) /* public access */
455 {
456 /* ??? */
457 }
458 if (accflags & 0x0002) /* private access */
459 {
460 SET_TYPE_FIELD_PRIVATE (type, i);
461 }
462 if (accflags & 0x0004) /* protected access */
463 {
464 SET_TYPE_FIELD_PROTECTED (type, i);
465 }
466 if (accflags & 0x0008) /* ACC_STATIC */
467 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
468 else
469 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
470 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
471 {
472 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
473 }
474 else
475 {
476 struct type *ftype;
477 temp = field;
478 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
479 ftype = type_from_class (temp);
480 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
481 ftype = lookup_pointer_type (ftype);
482 TYPE_FIELD_TYPE (type, i) = ftype;
483 }
484 }
485
486 temp = clas;
487 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
488 NULL, "structure"));
489 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
490 j = nmethods * sizeof (struct fn_field);
491 fn_fields = (struct fn_field *)
492 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
493 memset (fn_fields, 0, j);
494 fn_fieldlists = (struct fn_fieldlist *)
495 alloca (nmethods * sizeof (struct fn_fieldlist));
496
497 methods = NULL;
498 for (i = 0; i < nmethods; i++)
499 {
500 char *mname;
501 int k;
502 if (methods == NULL)
503 {
504 temp = clas;
505 methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
506 method = value_ind (methods);
507 }
508 else
509 { /* Re-use method value for next method. */
510 VALUE_ADDRESS (method) += TYPE_LENGTH (VALUE_TYPE (method));
511 VALUE_LAZY (method) = 1;
512 }
513
514 /* Get method name. */
515 temp = method;
516 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
517 mname = get_java_utf8_name (&objfile->type_obstack, temp);
518 if (strcmp (mname, "<init>") == 0)
519 mname = unqualified_name;
520
521 /* Check for an existing method with the same name.
522 * This makes building the fn_fieldslists an O(nmethods**2)
523 * operation. That could be using hashing, but I doubt it
524 * is worth it. Note that we do maintain the order of methods
525 * in the inferior's Method table (as long as that is grouped
526 * by method name), which I think is desirable. --PB */
527 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
528 {
529 if (--j < 0)
530 { /* No match - new method name. */
531 j = TYPE_NFN_FIELDS (type)++;
532 fn_fieldlists[j].name = mname;
533 fn_fieldlists[j].length = 1;
534 fn_fieldlists[j].fn_fields = &fn_fields[i];
535 k = i;
536 break;
537 }
538 if (strcmp (mname, fn_fieldlists[j].name) == 0)
539 { /* Found an existing method with the same name. */
540 int l;
541 if (mname != unqualified_name)
542 obstack_free (&objfile->type_obstack, mname);
543 mname = fn_fieldlists[j].name;
544 fn_fieldlists[j].length++;
545 k = i - k; /* Index of new slot. */
546 /* Shift intervening fn_fields (between k and i) down. */
547 for (l = i; l > k; l--)
548 fn_fields[l] = fn_fields[l - 1];
549 for (l = TYPE_NFN_FIELDS (type); --l > j;)
550 fn_fieldlists[l].fn_fields++;
551 break;
552 }
553 k += fn_fieldlists[j].length;
554 }
555 fn_fields[k].physname = "";
556 fn_fields[k].is_stub = 1;
557 fn_fields[k].type = make_function_type (java_void_type, NULL); /* FIXME */
558 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
559 }
560
561 j = TYPE_NFN_FIELDS (type) * sizeof (struct fn_fieldlist);
562 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
563 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
564 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
565
566 return type;
567 }
568
569 static struct type *java_object_type;
570
571 struct type *
572 get_java_object_type (void)
573 {
574 if (java_object_type == NULL)
575 {
576 struct symbol *sym;
577 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN,
578 (int *) 0, (struct symtab **) NULL);
579 if (sym == NULL)
580 error ("cannot find java.lang.Object");
581 java_object_type = SYMBOL_TYPE (sym);
582 }
583 return java_object_type;
584 }
585
586 int
587 get_java_object_header_size (void)
588 {
589 struct type *objtype = get_java_object_type ();
590 if (objtype == NULL)
591 return (2 * TARGET_PTR_BIT / TARGET_CHAR_BIT);
592 else
593 return TYPE_LENGTH (objtype);
594 }
595
596 int
597 is_object_type (struct type *type)
598 {
599 CHECK_TYPEDEF (type);
600 if (TYPE_CODE (type) == TYPE_CODE_PTR)
601 {
602 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
603 char *name;
604 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
605 return 0;
606 while (TYPE_N_BASECLASSES (ttype) > 0)
607 ttype = TYPE_BASECLASS (ttype, 0);
608 name = TYPE_TAG_NAME (ttype);
609 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
610 return 1;
611 name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
612 if (name != NULL && strcmp (name, "vtable") == 0)
613 {
614 if (java_object_type == NULL)
615 java_object_type = type;
616 return 1;
617 }
618 }
619 return 0;
620 }
621
622 struct type *
623 java_primitive_type (int signature)
624 {
625 switch (signature)
626 {
627 case 'B':
628 return java_byte_type;
629 case 'S':
630 return java_short_type;
631 case 'I':
632 return java_int_type;
633 case 'J':
634 return java_long_type;
635 case 'Z':
636 return java_boolean_type;
637 case 'C':
638 return java_char_type;
639 case 'F':
640 return java_float_type;
641 case 'D':
642 return java_double_type;
643 case 'V':
644 return java_void_type;
645 }
646 error ("unknown signature '%c' for primitive type", (char) signature);
647 }
648
649 /* If name[0 .. namelen-1] is the name of a primitive Java type,
650 return that type. Otherwise, return NULL. */
651
652 struct type *
653 java_primitive_type_from_name (char *name, int namelen)
654 {
655 switch (name[0])
656 {
657 case 'b':
658 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
659 return java_byte_type;
660 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
661 return java_boolean_type;
662 break;
663 case 'c':
664 if (namelen == 4 && memcmp (name, "char", 4) == 0)
665 return java_char_type;
666 case 'd':
667 if (namelen == 6 && memcmp (name, "double", 6) == 0)
668 return java_double_type;
669 break;
670 case 'f':
671 if (namelen == 5 && memcmp (name, "float", 5) == 0)
672 return java_float_type;
673 break;
674 case 'i':
675 if (namelen == 3 && memcmp (name, "int", 3) == 0)
676 return java_int_type;
677 break;
678 case 'l':
679 if (namelen == 4 && memcmp (name, "long", 4) == 0)
680 return java_long_type;
681 break;
682 case 's':
683 if (namelen == 5 && memcmp (name, "short", 5) == 0)
684 return java_short_type;
685 break;
686 case 'v':
687 if (namelen == 4 && memcmp (name, "void", 4) == 0)
688 return java_void_type;
689 break;
690 }
691 return NULL;
692 }
693
694 /* Return the length (in bytes) of demangled name of the Java type
695 signature string SIGNATURE. */
696
697 static int
698 java_demangled_signature_length (char *signature)
699 {
700 int array = 0;
701 for (; *signature == '['; signature++)
702 array += 2; /* Two chars for "[]". */
703 switch (signature[0])
704 {
705 case 'L':
706 /* Subtract 2 for 'L' and ';'. */
707 return strlen (signature) - 2 + array;
708 default:
709 return strlen (TYPE_NAME (java_primitive_type (signature[0]))) + array;
710 }
711 }
712
713 /* Demangle the Java type signature SIGNATURE, leaving the result in RESULT. */
714
715 static void
716 java_demangled_signature_copy (char *result, char *signature)
717 {
718 int array = 0;
719 char *ptr;
720 int i;
721 while (*signature == '[')
722 {
723 array++;
724 signature++;
725 }
726 switch (signature[0])
727 {
728 case 'L':
729 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
730 signature++;
731 ptr = result;
732 for (; *signature != ';' && *signature != '\0'; signature++)
733 {
734 if (*signature == '/')
735 *ptr++ = '.';
736 else
737 *ptr++ = *signature;
738 }
739 break;
740 default:
741 ptr = TYPE_NAME (java_primitive_type (signature[0]));
742 i = strlen (ptr);
743 strcpy (result, ptr);
744 ptr = result + i;
745 break;
746 }
747 while (--array >= 0)
748 {
749 *ptr++ = '[';
750 *ptr++ = ']';
751 }
752 }
753
754 /* Return the demangled name of the Java type signature string SIGNATURE,
755 as a freshly allocated copy. */
756
757 char *
758 java_demangle_type_signature (char *signature)
759 {
760 int length = java_demangled_signature_length (signature);
761 char *result = xmalloc (length + 1);
762 java_demangled_signature_copy (result, signature);
763 result[length] = '\0';
764 return result;
765 }
766
767 struct type *
768 java_lookup_type (char *signature)
769 {
770 switch (signature[0])
771 {
772 case 'L':
773 case '[':
774 error ("java_lookup_type not fully implemented");
775 default:
776 return java_primitive_type (signature[0]);
777 }
778 }
779
780 /* Return the type of TYPE followed by DIMS pairs of [ ].
781 If DIMS == 0, TYPE is returned. */
782
783 struct type *
784 java_array_type (struct type *type, int dims)
785 {
786 struct type *range_type;
787
788 while (dims-- > 0)
789 {
790 range_type = create_range_type (NULL, builtin_type_int, 0, 0);
791 /* FIXME This is bogus! Java arrays are not gdb arrays! */
792 type = create_array_type (NULL, type, range_type);
793 }
794
795 return type;
796 }
797
798 /* Create a Java string in the inferior from a (Utf8) literal. */
799
800 static struct value *
801 java_value_string (char *ptr, int len)
802 {
803 error ("not implemented - java_value_string"); /* FIXME */
804 }
805
806 /* Print the character C on STREAM as part of the contents of a literal
807 string whose delimiter is QUOTER. Note that that format for printing
808 characters and strings is language specific. */
809
810 static void
811 java_emit_char (int c, struct ui_file *stream, int quoter)
812 {
813 switch (c)
814 {
815 case '\\':
816 case '\'':
817 fprintf_filtered (stream, "\\%c", c);
818 break;
819 case '\b':
820 fputs_filtered ("\\b", stream);
821 break;
822 case '\t':
823 fputs_filtered ("\\t", stream);
824 break;
825 case '\n':
826 fputs_filtered ("\\n", stream);
827 break;
828 case '\f':
829 fputs_filtered ("\\f", stream);
830 break;
831 case '\r':
832 fputs_filtered ("\\r", stream);
833 break;
834 default:
835 if (isprint (c))
836 fputc_filtered (c, stream);
837 else
838 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
839 break;
840 }
841 }
842
843 static struct value *
844 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
845 int *pos, enum noside noside)
846 {
847 int pc = *pos;
848 int i;
849 char *name;
850 enum exp_opcode op = exp->elts[*pos].opcode;
851 struct value *arg1;
852 struct value *arg2;
853 struct type *type;
854 switch (op)
855 {
856 case UNOP_IND:
857 if (noside == EVAL_SKIP)
858 goto standard;
859 (*pos)++;
860 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
861 if (is_object_type (VALUE_TYPE (arg1)))
862 {
863 struct type *type;
864
865 type = type_from_class (java_class_from_object (arg1));
866 arg1 = value_cast (lookup_pointer_type (type), arg1);
867 }
868 if (noside == EVAL_SKIP)
869 goto nosideret;
870 return value_ind (arg1);
871
872 case BINOP_SUBSCRIPT:
873 (*pos)++;
874 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
875 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
876 if (noside == EVAL_SKIP)
877 goto nosideret;
878 /* If the user attempts to subscript something that is not an
879 array or pointer type (like a plain int variable for example),
880 then report this as an error. */
881
882 COERCE_REF (arg1);
883 type = check_typedef (VALUE_TYPE (arg1));
884 if (TYPE_CODE (type) == TYPE_CODE_PTR)
885 type = check_typedef (TYPE_TARGET_TYPE (type));
886 name = TYPE_NAME (type);
887 if (name == NULL)
888 name = TYPE_TAG_NAME (type);
889 i = name == NULL ? 0 : strlen (name);
890 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
891 && i > 2 && name[i - 1] == ']')
892 {
893 CORE_ADDR address;
894 long length, index;
895 struct type *el_type;
896 char buf4[4];
897
898 struct value *clas = java_class_from_object (arg1);
899 struct value *temp = clas;
900 /* Get CLASS_ELEMENT_TYPE of the array type. */
901 temp = value_struct_elt (&temp, NULL, "methods",
902 NULL, "structure");
903 VALUE_TYPE (temp) = VALUE_TYPE (clas);
904 el_type = type_from_class (temp);
905 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
906 el_type = lookup_pointer_type (el_type);
907
908 if (noside == EVAL_AVOID_SIDE_EFFECTS)
909 return value_zero (el_type, VALUE_LVAL (arg1));
910 address = value_as_address (arg1);
911 address += JAVA_OBJECT_SIZE;
912 read_memory (address, buf4, 4);
913 length = (long) extract_signed_integer (buf4, 4);
914 index = (long) value_as_long (arg2);
915 if (index >= length || index < 0)
916 error ("array index (%ld) out of bounds (length: %ld)",
917 index, length);
918 address = (address + 4) + index * TYPE_LENGTH (el_type);
919 return value_at (el_type, address, NULL);
920 }
921 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
922 {
923 if (noside == EVAL_AVOID_SIDE_EFFECTS)
924 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
925 else
926 return value_subscript (arg1, arg2);
927 }
928 if (name)
929 error ("cannot subscript something of type `%s'", name);
930 else
931 error ("cannot subscript requested type");
932
933 case OP_STRING:
934 (*pos)++;
935 i = longest_to_int (exp->elts[pc + 1].longconst);
936 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
937 if (noside == EVAL_SKIP)
938 goto nosideret;
939 return java_value_string (&exp->elts[pc + 2].string, i);
940
941 case STRUCTOP_STRUCT:
942 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
943 /* Convert object field (such as TYPE.class) to reference. */
944 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT)
945 arg1 = value_addr (arg1);
946 return arg1;
947 default:
948 break;
949 }
950 standard:
951 return evaluate_subexp_standard (expect_type, exp, pos, noside);
952 nosideret:
953 return value_from_longest (builtin_type_long, (LONGEST) 1);
954 }
955
956 static struct type *
957 java_create_fundamental_type (struct objfile *objfile, int typeid)
958 {
959 switch (typeid)
960 {
961 case FT_VOID:
962 return java_void_type;
963 case FT_BOOLEAN:
964 return java_boolean_type;
965 case FT_CHAR:
966 return java_char_type;
967 case FT_FLOAT:
968 return java_float_type;
969 case FT_DBL_PREC_FLOAT:
970 return java_double_type;
971 case FT_BYTE:
972 case FT_SIGNED_CHAR:
973 return java_byte_type;
974 case FT_SHORT:
975 case FT_SIGNED_SHORT:
976 return java_short_type;
977 case FT_INTEGER:
978 case FT_SIGNED_INTEGER:
979 return java_int_type;
980 case FT_LONG:
981 case FT_SIGNED_LONG:
982 return java_long_type;
983 }
984 return c_create_fundamental_type (objfile, typeid);
985 }
986
987 static char *java_demangle (const char *mangled, int options)
988 {
989 return cplus_demangle (mangled, options | DMGL_JAVA);
990 }
991
992
993 /* Table mapping opcodes into strings for printing operators
994 and precedences of the operators. */
995
996 const struct op_print java_op_print_tab[] =
997 {
998 {",", BINOP_COMMA, PREC_COMMA, 0},
999 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1000 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1001 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1002 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1003 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1004 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1005 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1006 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1007 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1008 {">=", BINOP_GEQ, PREC_ORDER, 0},
1009 {">", BINOP_GTR, PREC_ORDER, 0},
1010 {"<", BINOP_LESS, PREC_ORDER, 0},
1011 {">>", BINOP_RSH, PREC_SHIFT, 0},
1012 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1013 #if 0
1014 {">>>", BINOP_ ? ? ?, PREC_SHIFT, 0},
1015 #endif
1016 {"+", BINOP_ADD, PREC_ADD, 0},
1017 {"-", BINOP_SUB, PREC_ADD, 0},
1018 {"*", BINOP_MUL, PREC_MUL, 0},
1019 {"/", BINOP_DIV, PREC_MUL, 0},
1020 {"%", BINOP_REM, PREC_MUL, 0},
1021 {"-", UNOP_NEG, PREC_PREFIX, 0},
1022 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1023 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1024 {"*", UNOP_IND, PREC_PREFIX, 0},
1025 #if 0
1026 {"instanceof", ? ? ?, ? ? ?, 0},
1027 #endif
1028 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1029 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1030 {NULL, 0, 0, 0}
1031 };
1032
1033 const struct language_defn java_language_defn =
1034 {
1035 "java", /* Language name */
1036 language_java,
1037 c_builtin_types,
1038 range_check_off,
1039 type_check_off,
1040 case_sensitive_on,
1041 java_parse,
1042 java_error,
1043 evaluate_subexp_java,
1044 c_printchar, /* Print a character constant */
1045 c_printstr, /* Function to print string constant */
1046 java_emit_char, /* Function to print a single character */
1047 java_create_fundamental_type, /* Create fundamental type in this language */
1048 java_print_type, /* Print a type using appropriate syntax */
1049 java_val_print, /* Print a value using appropriate syntax */
1050 java_value_print, /* Print a top-level value */
1051 NULL, /* Language specific skip_trampoline */
1052 value_of_this, /* value_of_this */
1053 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1054 java_demangle, /* Language specific symbol demangler */
1055 {"", "", "", ""}, /* Binary format info */
1056 {"0%lo", "0", "o", ""}, /* Octal format info */
1057 {"%ld", "", "d", ""}, /* Decimal format info */
1058 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1059 java_op_print_tab, /* expression operators for printing */
1060 0, /* not c-style arrays */
1061 0, /* String lower bound */
1062 &builtin_type_char, /* Type of string elements */
1063 LANG_MAGIC
1064 };
1065
1066 void
1067 _initialize_java_language (void)
1068 {
1069
1070 java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
1071 java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
1072 java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
1073 java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
1074 java_boolean_type = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
1075 java_char_type = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
1076 java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
1077 java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
1078 java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
1079
1080 add_language (&java_language_defn);
1081 }
1082
1083 /* Cleanup code that should be run on every "run".
1084 We should use make_run_cleanup to have this be called.
1085 But will that mess up values in value histry? FIXME */
1086
1087 extern void java_rerun_cleanup (void);
1088 void
1089 java_rerun_cleanup (void)
1090 {
1091 if (class_symtab != NULL)
1092 {
1093 free_symtab (class_symtab); /* ??? */
1094 class_symtab = NULL;
1095 }
1096 if (dynamics_objfile != NULL)
1097 {
1098 free_objfile (dynamics_objfile);
1099 dynamics_objfile = NULL;
1100 }
1101
1102 java_object_type = NULL;
1103 }
This page took 0.052178 seconds and 5 git commands to generate.