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