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