* MAINTAINERS: Move Jim Blandy to past maintainers.
[deliverable/binutils-gdb.git] / gdb / jv-lang.c
1 /* Java language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 1997, 1998, 1999, 2000, 2003, 2004, 2005, 2007, 2008, 2009,
4 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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 #include "gdb_assert.h"
41
42 /* Local functions */
43
44 extern void _initialize_java_language (void);
45
46 static int java_demangled_signature_length (char *);
47 static void java_demangled_signature_copy (char *, char *);
48
49 static struct symtab *get_java_class_symtab (struct gdbarch *gdbarch);
50 static char *get_java_utf8_name (struct obstack *obstack, struct value *name);
51 static int java_class_is_primitive (struct value *clas);
52 static struct value *java_value_string (char *ptr, int len);
53
54 static void java_emit_char (int c, struct type *type,
55 struct ui_file * stream, int quoter);
56
57 static char *java_class_name_from_physname (const char *physname);
58
59 static const struct objfile_data *jv_dynamics_objfile_data_key;
60 static const struct objfile_data *jv_type_objfile_data_key;
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
66 static struct objfile *dynamics_objfile = NULL;
67
68 /* symtab contains classes read from the inferior. */
69
70 static struct symtab *class_symtab = NULL;
71
72 static struct type *java_link_class_type (struct gdbarch *,
73 struct type *, struct value *);
74
75 /* An instance of this structure is used to store some data that must
76 be freed. */
77
78 struct jv_per_objfile_data
79 {
80 /* The expandable dictionary we use. */
81 struct dictionary *dict;
82 };
83
84 /* A function called when the dynamics_objfile is freed. We use this
85 to clean up some internal state. */
86 static void
87 jv_per_objfile_free (struct objfile *objfile, void *data)
88 {
89 struct jv_per_objfile_data *jv_data = data;
90
91 gdb_assert (objfile == dynamics_objfile);
92 /* Clean up all our cached state. */
93 dynamics_objfile = NULL;
94 class_symtab = NULL;
95
96 if (jv_data->dict)
97 dict_free (jv_data->dict);
98 xfree (jv_data);
99 }
100
101 /* FIXME: carlton/2003-02-04: This is the main or only caller of
102 allocate_objfile with first argument NULL; as a result, this code
103 breaks every so often. Somebody should write a test case that
104 exercises GDB in various ways (e.g. something involving loading a
105 dynamic library) after this code has been called. */
106
107 static struct objfile *
108 get_dynamics_objfile (struct gdbarch *gdbarch)
109 {
110 if (dynamics_objfile == NULL)
111 {
112 struct jv_per_objfile_data *data;
113
114 /* Mark it as shared so that it is cleared when the inferior is
115 re-run. */
116 dynamics_objfile = allocate_objfile (NULL, OBJF_SHARED);
117 dynamics_objfile->gdbarch = gdbarch;
118
119 data = XCNEW (struct jv_per_objfile_data);
120 set_objfile_data (dynamics_objfile, jv_dynamics_objfile_data_key, data);
121 }
122 return dynamics_objfile;
123 }
124
125 static struct symtab *
126 get_java_class_symtab (struct gdbarch *gdbarch)
127 {
128 if (class_symtab == NULL)
129 {
130 struct objfile *objfile = get_dynamics_objfile (gdbarch);
131 struct blockvector *bv;
132 struct block *bl;
133 struct jv_per_objfile_data *jv_data;
134
135 class_symtab = allocate_symtab ("<java-classes>", objfile);
136 class_symtab->language = language_java;
137 bv = (struct blockvector *)
138 obstack_alloc (&objfile->objfile_obstack,
139 sizeof (struct blockvector) + sizeof (struct block *));
140 BLOCKVECTOR_NBLOCKS (bv) = 1;
141 BLOCKVECTOR (class_symtab) = bv;
142
143 /* Allocate dummy STATIC_BLOCK. */
144 bl = allocate_block (&objfile->objfile_obstack);
145 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
146 NULL);
147 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
148
149 /* Allocate GLOBAL_BLOCK. */
150 bl = allocate_block (&objfile->objfile_obstack);
151 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
152 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
153
154 /* Arrange to free the dict. */
155 jv_data = objfile_data (objfile, jv_dynamics_objfile_data_key);
156 jv_data->dict = BLOCK_DICT (bl);
157 }
158 return class_symtab;
159 }
160
161 static void
162 add_class_symtab_symbol (struct symbol *sym)
163 {
164 struct symtab *symtab
165 = get_java_class_symtab (get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile));
166 struct blockvector *bv = BLOCKVECTOR (symtab);
167
168 dict_add_symbol (BLOCK_DICT (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)), sym);
169 }
170
171 static struct symbol *
172 add_class_symbol (struct type *type, CORE_ADDR addr)
173 {
174 struct symbol *sym;
175
176 sym = (struct symbol *)
177 obstack_alloc (&dynamics_objfile->objfile_obstack, sizeof (struct symbol));
178 memset (sym, 0, sizeof (struct symbol));
179 SYMBOL_SET_LANGUAGE (sym, language_java);
180 SYMBOL_SET_LINKAGE_NAME (sym, TYPE_TAG_NAME (type));
181 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
182 /* SYMBOL_VALUE (sym) = valu; */
183 SYMBOL_TYPE (sym) = type;
184 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
185 SYMBOL_VALUE_ADDRESS (sym) = addr;
186 return sym;
187 }
188
189 struct type *
190 java_lookup_class (char *name)
191 {
192 struct symbol *sym;
193
194 sym = lookup_symbol (name, expression_context_block, STRUCT_DOMAIN, NULL);
195 if (sym != NULL)
196 return SYMBOL_TYPE (sym);
197 /* FIXME - should search inferior's symbol table. */
198 return NULL;
199 }
200
201 /* Return a nul-terminated string (allocated on OBSTACK) for
202 a name given by NAME (which has type Utf8Const*). */
203
204 char *
205 get_java_utf8_name (struct obstack *obstack, struct value *name)
206 {
207 char *chrs;
208 struct value *temp = name;
209 int name_length;
210 CORE_ADDR data_addr;
211
212 temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
213 name_length = (int) value_as_long (temp);
214 data_addr = value_address (temp) + TYPE_LENGTH (value_type (temp));
215 chrs = obstack_alloc (obstack, name_length + 1);
216 chrs[name_length] = '\0';
217 read_memory (data_addr, (gdb_byte *) chrs, name_length);
218 return chrs;
219 }
220
221 struct value *
222 java_class_from_object (struct value *obj_val)
223 {
224 /* This is all rather inefficient, since the offsets of vtable and
225 class are fixed. FIXME */
226 struct value *vtable_val;
227
228 if (TYPE_CODE (value_type (obj_val)) == TYPE_CODE_PTR
229 && TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (obj_val))) == 0)
230 obj_val = value_at (get_java_object_type (),
231 value_as_address (obj_val));
232
233 vtable_val = value_struct_elt (&obj_val, NULL, "vtable", NULL, "structure");
234 return value_struct_elt (&vtable_val, NULL, "class", NULL, "structure");
235 }
236
237 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
238 static int
239 java_class_is_primitive (struct value *clas)
240 {
241 struct value *vtable = value_struct_elt (&clas, NULL, "vtable",
242 NULL, "struct");
243 CORE_ADDR i = value_as_address (vtable);
244
245 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
246 }
247
248 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
249
250 struct type *
251 type_from_class (struct gdbarch *gdbarch, struct value *clas)
252 {
253 struct type *type;
254 char *name;
255 struct value *temp;
256 struct objfile *objfile;
257 struct value *utf8_name;
258 char *nptr;
259 CORE_ADDR addr;
260 int is_array = 0;
261
262 type = check_typedef (value_type (clas));
263 if (TYPE_CODE (type) == TYPE_CODE_PTR)
264 {
265 if (value_logical_not (clas))
266 return NULL;
267 clas = value_ind (clas);
268 }
269 addr = value_address (clas);
270
271 objfile = get_dynamics_objfile (gdbarch);
272 if (java_class_is_primitive (clas))
273 {
274 struct value *sig;
275
276 temp = clas;
277 sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
278 return java_primitive_type (gdbarch, value_as_long (sig));
279 }
280
281 /* Get Class name. */
282 /* If clasloader non-null, prepend loader address. FIXME */
283 temp = clas;
284 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
285 name = get_java_utf8_name (&objfile->objfile_obstack, utf8_name);
286 for (nptr = name; *nptr != 0; nptr++)
287 {
288 if (*nptr == '/')
289 *nptr = '.';
290 }
291
292 type = java_lookup_class (name);
293 if (type != NULL)
294 return type;
295
296 type = alloc_type (objfile);
297 TYPE_CODE (type) = TYPE_CODE_STRUCT;
298 INIT_CPLUS_SPECIFIC (type);
299
300 if (name[0] == '[')
301 {
302 char *signature = name;
303 int namelen = java_demangled_signature_length (signature);
304
305 if (namelen > strlen (name))
306 name = obstack_alloc (&objfile->objfile_obstack, namelen + 1);
307 java_demangled_signature_copy (name, signature);
308 name[namelen] = '\0';
309 is_array = 1;
310 temp = clas;
311 /* Set array element type. */
312 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
313 deprecated_set_value_type (temp,
314 lookup_pointer_type (value_type (clas)));
315 TYPE_TARGET_TYPE (type) = type_from_class (gdbarch, temp);
316 }
317
318 ALLOCATE_CPLUS_STRUCT_TYPE (type);
319 TYPE_TAG_NAME (type) = name;
320
321 add_class_symtab_symbol (add_class_symbol (type, addr));
322 return java_link_class_type (gdbarch, type, clas);
323 }
324
325 /* Fill in class TYPE with data from the CLAS value. */
326
327 static struct type *
328 java_link_class_type (struct gdbarch *gdbarch,
329 struct type *type, struct value *clas)
330 {
331 struct value *temp;
332 char *unqualified_name;
333 char *name = TYPE_TAG_NAME (type);
334 int ninterfaces, nfields, nmethods;
335 int type_is_object = 0;
336 struct fn_field *fn_fields;
337 struct fn_fieldlist *fn_fieldlists;
338 struct value *fields;
339 struct value *methods;
340 struct value *method = NULL;
341 struct value *field = NULL;
342 int i, j;
343 struct objfile *objfile = get_dynamics_objfile (gdbarch);
344 struct type *tsuper;
345
346 gdb_assert (name != NULL);
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 (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 (gdbarch, 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",
368 NULL, "structure"));
369 #endif
370 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
371 temp = clas;
372 nfields = value_as_long (value_struct_elt (&temp, NULL, "field_count",
373 NULL, "structure"));
374 nfields += TYPE_N_BASECLASSES (type);
375 nfields++; /* Add one for dummy "class" field. */
376 TYPE_NFIELDS (type) = nfields;
377 TYPE_FIELDS (type) = (struct field *)
378 TYPE_ALLOC (type, sizeof (struct field) * nfields);
379
380 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
381
382 TYPE_FIELD_PRIVATE_BITS (type) =
383 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
384 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
385
386 TYPE_FIELD_PROTECTED_BITS (type) =
387 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
388 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
389
390 TYPE_FIELD_IGNORE_BITS (type) =
391 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
392 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
393
394 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
395 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
396 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
397
398 if (tsuper != NULL)
399 {
400 TYPE_BASECLASS (type, 0) = tsuper;
401 if (type_is_object)
402 SET_TYPE_FIELD_PRIVATE (type, 0);
403 }
404
405 i = strlen (name);
406 if (i > 2 && name[i - 1] == ']' && tsuper != NULL)
407 {
408 /* FIXME */
409 TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4; /* size with "length" */
410 }
411 else
412 {
413 temp = clas;
414 temp = value_struct_elt (&temp, NULL, "size_in_bytes",
415 NULL, "structure");
416 TYPE_LENGTH (type) = value_as_long (temp);
417 }
418
419 fields = NULL;
420 nfields--; /* First set up dummy "class" field. */
421 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas));
422 TYPE_FIELD_NAME (type, nfields) = "class";
423 TYPE_FIELD_TYPE (type, nfields) = value_type (clas);
424 SET_TYPE_FIELD_PRIVATE (type, nfields);
425
426 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
427 {
428 int accflags;
429 int boffset;
430
431 if (fields == NULL)
432 {
433 temp = clas;
434 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
435 field = value_ind (fields);
436 }
437 else
438 { /* Re-use field value for next field. */
439 CORE_ADDR addr
440 = value_address (field) + TYPE_LENGTH (value_type (field));
441
442 set_value_address (field, addr);
443 set_value_lazy (field, 1);
444 }
445 temp = field;
446 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
447 TYPE_FIELD_NAME (type, i) =
448 get_java_utf8_name (&objfile->objfile_obstack, temp);
449 temp = field;
450 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
451 NULL, "structure"));
452 temp = field;
453 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
454 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
455 NULL, "structure"));
456 if (accflags & 0x0001) /* public access */
457 {
458 /* ??? */
459 }
460 if (accflags & 0x0002) /* private access */
461 {
462 SET_TYPE_FIELD_PRIVATE (type, i);
463 }
464 if (accflags & 0x0004) /* protected access */
465 {
466 SET_TYPE_FIELD_PROTECTED (type, i);
467 }
468 if (accflags & 0x0008) /* ACC_STATIC */
469 SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
470 else
471 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
472 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
473 {
474 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
475 }
476 else
477 {
478 struct type *ftype;
479
480 temp = field;
481 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
482 ftype = type_from_class (gdbarch, temp);
483 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
484 ftype = lookup_pointer_type (ftype);
485 TYPE_FIELD_TYPE (type, i) = ftype;
486 }
487 }
488
489 temp = clas;
490 nmethods = value_as_long (value_struct_elt (&temp, NULL, "method_count",
491 NULL, "structure"));
492 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
493 j = nmethods * sizeof (struct fn_field);
494 fn_fields = (struct fn_field *)
495 obstack_alloc (&dynamics_objfile->objfile_obstack, j);
496 memset (fn_fields, 0, j);
497 fn_fieldlists = (struct fn_fieldlist *)
498 alloca (nmethods * sizeof (struct fn_fieldlist));
499
500 methods = NULL;
501 for (i = 0; i < nmethods; i++)
502 {
503 char *mname;
504 int k;
505
506 if (methods == NULL)
507 {
508 temp = clas;
509 methods = value_struct_elt (&temp, NULL, "methods",
510 NULL, "structure");
511 method = value_ind (methods);
512 }
513 else
514 { /* Re-use method value for next method. */
515 CORE_ADDR addr
516 = value_address (method) + TYPE_LENGTH (value_type (method));
517
518 set_value_address (method, addr);
519 set_value_lazy (method, 1);
520 }
521
522 /* Get method name. */
523 temp = method;
524 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
525 mname = get_java_utf8_name (&objfile->objfile_obstack, temp);
526 if (strcmp (mname, "<init>") == 0)
527 mname = unqualified_name;
528
529 /* Check for an existing method with the same name.
530 * This makes building the fn_fieldslists an O(nmethods**2)
531 * operation. That could be using hashing, but I doubt it
532 * is worth it. Note that we do maintain the order of methods
533 * in the inferior's Method table (as long as that is grouped
534 * by method name), which I think is desirable. --PB */
535 for (k = 0, j = TYPE_NFN_FIELDS (type);;)
536 {
537 if (--j < 0)
538 { /* No match - new method name. */
539 j = TYPE_NFN_FIELDS (type)++;
540 fn_fieldlists[j].name = mname;
541 fn_fieldlists[j].length = 1;
542 fn_fieldlists[j].fn_fields = &fn_fields[i];
543 k = i;
544 break;
545 }
546 if (strcmp (mname, fn_fieldlists[j].name) == 0)
547 { /* Found an existing method with the same name. */
548 int l;
549
550 if (mname != unqualified_name)
551 obstack_free (&objfile->objfile_obstack, mname);
552 mname = fn_fieldlists[j].name;
553 fn_fieldlists[j].length++;
554 k = i - k; /* Index of new slot. */
555 /* Shift intervening fn_fields (between k and i) down. */
556 for (l = i; l > k; l--)
557 fn_fields[l] = fn_fields[l - 1];
558 for (l = TYPE_NFN_FIELDS (type); --l > j;)
559 fn_fieldlists[l].fn_fields++;
560 break;
561 }
562 k += fn_fieldlists[j].length;
563 }
564 fn_fields[k].physname = "";
565 fn_fields[k].is_stub = 1;
566 /* FIXME */
567 fn_fields[k].type = lookup_function_type
568 (builtin_java_type (gdbarch)->builtin_void);
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->objfile_obstack, j);
575 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
576
577 return type;
578 }
579
580 static struct type *java_object_type;
581
582 /* A free function that is attached to the objfile defining
583 java_object_type. This is used to clear the cached type whenever
584 its owning objfile is destroyed. */
585 static void
586 jv_clear_object_type (struct objfile *objfile, void *ignore)
587 {
588 java_object_type = NULL;
589 }
590
591 static void
592 set_java_object_type (struct type *type)
593 {
594 struct objfile *owner;
595
596 gdb_assert (java_object_type == NULL);
597
598 owner = TYPE_OBJFILE (type);
599 if (owner)
600 set_objfile_data (owner, jv_type_objfile_data_key, &java_object_type);
601 java_object_type = type;
602 }
603
604 struct type *
605 get_java_object_type (void)
606 {
607 if (java_object_type == NULL)
608 {
609 struct symbol *sym;
610
611 sym = lookup_symbol ("java.lang.Object", NULL, STRUCT_DOMAIN, NULL);
612 if (sym == NULL)
613 error (_("cannot find java.lang.Object"));
614 set_java_object_type (SYMBOL_TYPE (sym));
615 }
616 return java_object_type;
617 }
618
619 int
620 get_java_object_header_size (struct gdbarch *gdbarch)
621 {
622 struct type *objtype = get_java_object_type ();
623
624 if (objtype == NULL)
625 return (2 * gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
626 else
627 return TYPE_LENGTH (objtype);
628 }
629
630 int
631 is_object_type (struct type *type)
632 {
633 CHECK_TYPEDEF (type);
634 if (TYPE_CODE (type) == TYPE_CODE_PTR)
635 {
636 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
637 char *name;
638 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
639 return 0;
640 while (TYPE_N_BASECLASSES (ttype) > 0)
641 ttype = TYPE_BASECLASS (ttype, 0);
642 name = TYPE_TAG_NAME (ttype);
643 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
644 return 1;
645 name
646 = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char *) 0;
647 if (name != NULL && strcmp (name, "vtable") == 0)
648 {
649 if (java_object_type == NULL)
650 set_java_object_type (type);
651 return 1;
652 }
653 }
654 return 0;
655 }
656
657 struct type *
658 java_primitive_type (struct gdbarch *gdbarch, int signature)
659 {
660 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
661
662 switch (signature)
663 {
664 case 'B':
665 return builtin->builtin_byte;
666 case 'S':
667 return builtin->builtin_short;
668 case 'I':
669 return builtin->builtin_int;
670 case 'J':
671 return builtin->builtin_long;
672 case 'Z':
673 return builtin->builtin_boolean;
674 case 'C':
675 return builtin->builtin_char;
676 case 'F':
677 return builtin->builtin_float;
678 case 'D':
679 return builtin->builtin_double;
680 case 'V':
681 return builtin->builtin_void;
682 }
683 error (_("unknown signature '%c' for primitive type"), (char) signature);
684 }
685
686 /* If name[0 .. namelen-1] is the name of a primitive Java type,
687 return that type. Otherwise, return NULL. */
688
689 struct type *
690 java_primitive_type_from_name (struct gdbarch *gdbarch,
691 char *name, int namelen)
692 {
693 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
694
695 switch (name[0])
696 {
697 case 'b':
698 if (namelen == 4 && memcmp (name, "byte", 4) == 0)
699 return builtin->builtin_byte;
700 if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
701 return builtin->builtin_boolean;
702 break;
703 case 'c':
704 if (namelen == 4 && memcmp (name, "char", 4) == 0)
705 return builtin->builtin_char;
706 break;
707 case 'd':
708 if (namelen == 6 && memcmp (name, "double", 6) == 0)
709 return builtin->builtin_double;
710 break;
711 case 'f':
712 if (namelen == 5 && memcmp (name, "float", 5) == 0)
713 return builtin->builtin_float;
714 break;
715 case 'i':
716 if (namelen == 3 && memcmp (name, "int", 3) == 0)
717 return builtin->builtin_int;
718 break;
719 case 'l':
720 if (namelen == 4 && memcmp (name, "long", 4) == 0)
721 return builtin->builtin_long;
722 break;
723 case 's':
724 if (namelen == 5 && memcmp (name, "short", 5) == 0)
725 return builtin->builtin_short;
726 break;
727 case 'v':
728 if (namelen == 4 && memcmp (name, "void", 4) == 0)
729 return builtin->builtin_void;
730 break;
731 }
732 return NULL;
733 }
734
735 static char *
736 java_primitive_type_name (int signature)
737 {
738 switch (signature)
739 {
740 case 'B':
741 return "byte";
742 case 'S':
743 return "short";
744 case 'I':
745 return "int";
746 case 'J':
747 return "long";
748 case 'Z':
749 return "boolean";
750 case 'C':
751 return "char";
752 case 'F':
753 return "float";
754 case 'D':
755 return "double";
756 case 'V':
757 return "void";
758 }
759 error (_("unknown signature '%c' for primitive type"), (char) signature);
760 }
761
762 /* Return the length (in bytes) of demangled name of the Java type
763 signature string SIGNATURE. */
764
765 static int
766 java_demangled_signature_length (char *signature)
767 {
768 int array = 0;
769
770 for (; *signature == '['; signature++)
771 array += 2; /* Two chars for "[]". */
772 switch (signature[0])
773 {
774 case 'L':
775 /* Subtract 2 for 'L' and ';'. */
776 return strlen (signature) - 2 + array;
777 default:
778 return strlen (java_primitive_type_name (signature[0])) + array;
779 }
780 }
781
782 /* Demangle the Java type signature SIGNATURE, leaving the result in
783 RESULT. */
784
785 static void
786 java_demangled_signature_copy (char *result, char *signature)
787 {
788 int array = 0;
789 char *ptr;
790 int i;
791
792 while (*signature == '[')
793 {
794 array++;
795 signature++;
796 }
797 switch (signature[0])
798 {
799 case 'L':
800 /* Subtract 2 for 'L' and ';', but add 1 for final nul. */
801 signature++;
802 ptr = result;
803 for (; *signature != ';' && *signature != '\0'; signature++)
804 {
805 if (*signature == '/')
806 *ptr++ = '.';
807 else
808 *ptr++ = *signature;
809 }
810 break;
811 default:
812 ptr = java_primitive_type_name (signature[0]);
813 i = strlen (ptr);
814 strcpy (result, ptr);
815 ptr = result + i;
816 break;
817 }
818 while (--array >= 0)
819 {
820 *ptr++ = '[';
821 *ptr++ = ']';
822 }
823 }
824
825 /* Return the demangled name of the Java type signature string SIGNATURE,
826 as a freshly allocated copy. */
827
828 char *
829 java_demangle_type_signature (char *signature)
830 {
831 int length = java_demangled_signature_length (signature);
832 char *result = xmalloc (length + 1);
833
834 java_demangled_signature_copy (result, signature);
835 result[length] = '\0';
836 return result;
837 }
838
839 /* Return the type of TYPE followed by DIMS pairs of [ ].
840 If DIMS == 0, TYPE is returned. */
841
842 struct type *
843 java_array_type (struct type *type, int dims)
844 {
845 while (dims-- > 0)
846 {
847 /* FIXME This is bogus! Java arrays are not gdb arrays! */
848 type = lookup_array_range_type (type, 0, 0);
849 }
850
851 return type;
852 }
853
854 /* Create a Java string in the inferior from a (Utf8) literal. */
855
856 static struct value *
857 java_value_string (char *ptr, int len)
858 {
859 error (_("not implemented - java_value_string")); /* FIXME */
860 }
861
862 /* Print the character C on STREAM as part of the contents of a literal
863 string whose delimiter is QUOTER. Note that that format for printing
864 characters and strings is language specific. */
865
866 static void
867 java_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
868 {
869 switch (c)
870 {
871 case '\\':
872 case '\'':
873 fprintf_filtered (stream, "\\%c", c);
874 break;
875 case '\b':
876 fputs_filtered ("\\b", stream);
877 break;
878 case '\t':
879 fputs_filtered ("\\t", stream);
880 break;
881 case '\n':
882 fputs_filtered ("\\n", stream);
883 break;
884 case '\f':
885 fputs_filtered ("\\f", stream);
886 break;
887 case '\r':
888 fputs_filtered ("\\r", stream);
889 break;
890 default:
891 if (isprint (c))
892 fputc_filtered (c, stream);
893 else
894 fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
895 break;
896 }
897 }
898
899 static struct value *
900 evaluate_subexp_java (struct type *expect_type, struct expression *exp,
901 int *pos, enum noside noside)
902 {
903 int pc = *pos;
904 int i;
905 char *name;
906 enum exp_opcode op = exp->elts[*pos].opcode;
907 struct value *arg1;
908 struct value *arg2;
909 struct type *type;
910
911 switch (op)
912 {
913 case UNOP_IND:
914 if (noside == EVAL_SKIP)
915 goto standard;
916 (*pos)++;
917 arg1 = evaluate_subexp_java (NULL_TYPE, exp, pos, EVAL_NORMAL);
918 if (is_object_type (value_type (arg1)))
919 {
920 struct type *type;
921
922 type = type_from_class (exp->gdbarch, java_class_from_object (arg1));
923 arg1 = value_cast (lookup_pointer_type (type), arg1);
924 }
925 return value_ind (arg1);
926
927 case BINOP_SUBSCRIPT:
928 (*pos)++;
929 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
930 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
931 if (noside == EVAL_SKIP)
932 goto nosideret;
933 /* If the user attempts to subscript something that is not an
934 array or pointer type (like a plain int variable for example),
935 then report this as an error. */
936
937 arg1 = coerce_ref (arg1);
938 type = check_typedef (value_type (arg1));
939 if (TYPE_CODE (type) == TYPE_CODE_PTR)
940 type = check_typedef (TYPE_TARGET_TYPE (type));
941 name = TYPE_NAME (type);
942 if (name == NULL)
943 name = TYPE_TAG_NAME (type);
944 i = name == NULL ? 0 : strlen (name);
945 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
946 && i > 2 && name[i - 1] == ']')
947 {
948 enum bfd_endian byte_order = gdbarch_byte_order (exp->gdbarch);
949 CORE_ADDR address;
950 long length, index;
951 struct type *el_type;
952 gdb_byte buf4[4];
953
954 struct value *clas = java_class_from_object (arg1);
955 struct value *temp = clas;
956 /* Get CLASS_ELEMENT_TYPE of the array type. */
957 temp = value_struct_elt (&temp, NULL, "methods",
958 NULL, "structure");
959 deprecated_set_value_type (temp, value_type (clas));
960 el_type = type_from_class (exp->gdbarch, temp);
961 if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
962 el_type = lookup_pointer_type (el_type);
963
964 if (noside == EVAL_AVOID_SIDE_EFFECTS)
965 return value_zero (el_type, VALUE_LVAL (arg1));
966 address = value_as_address (arg1);
967 address += get_java_object_header_size (exp->gdbarch);
968 read_memory (address, buf4, 4);
969 length = (long) extract_signed_integer (buf4, 4, byte_order);
970 index = (long) value_as_long (arg2);
971 if (index >= length || index < 0)
972 error (_("array index (%ld) out of bounds (length: %ld)"),
973 index, length);
974 address = (address + 4) + index * TYPE_LENGTH (el_type);
975 return value_at (el_type, address);
976 }
977 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
978 {
979 if (noside == EVAL_AVOID_SIDE_EFFECTS)
980 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
981 else
982 return value_subscript (arg1, value_as_long (arg2));
983 }
984 if (name)
985 error (_("cannot subscript something of type `%s'"), name);
986 else
987 error (_("cannot subscript requested type"));
988
989 case OP_STRING:
990 (*pos)++;
991 i = longest_to_int (exp->elts[pc + 1].longconst);
992 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
993 if (noside == EVAL_SKIP)
994 goto nosideret;
995 return java_value_string (&exp->elts[pc + 2].string, i);
996
997 case STRUCTOP_PTR:
998 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
999 /* Convert object field (such as TYPE.class) to reference. */
1000 if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_STRUCT)
1001 arg1 = value_addr (arg1);
1002 return arg1;
1003 default:
1004 break;
1005 }
1006 standard:
1007 return evaluate_subexp_standard (expect_type, exp, pos, noside);
1008 nosideret:
1009 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1010 }
1011
1012 static char *java_demangle (const char *mangled, int options)
1013 {
1014 return cplus_demangle (mangled, options | DMGL_JAVA);
1015 }
1016
1017 /* Find the member function name of the demangled name NAME. NAME
1018 must be a method name including arguments, in order to correctly
1019 locate the last component.
1020
1021 This function return a pointer to the first dot before the
1022 member function name, or NULL if the name was not of the
1023 expected form. */
1024
1025 static const char *
1026 java_find_last_component (const char *name)
1027 {
1028 const char *p;
1029
1030 /* Find argument list. */
1031 p = strchr (name, '(');
1032
1033 if (p == NULL)
1034 return NULL;
1035
1036 /* Back up and find first dot prior to argument list. */
1037 while (p > name && *p != '.')
1038 p--;
1039
1040 if (p == name)
1041 return NULL;
1042
1043 return p;
1044 }
1045
1046 /* Return the name of the class containing method PHYSNAME. */
1047
1048 static char *
1049 java_class_name_from_physname (const char *physname)
1050 {
1051 char *ret = NULL;
1052 const char *end;
1053 char *demangled_name = java_demangle (physname, DMGL_PARAMS | DMGL_ANSI);
1054
1055 if (demangled_name == NULL)
1056 return NULL;
1057
1058 end = java_find_last_component (demangled_name);
1059 if (end != NULL)
1060 {
1061 ret = xmalloc (end - demangled_name + 1);
1062 memcpy (ret, demangled_name, end - demangled_name);
1063 ret[end - demangled_name] = '\0';
1064 }
1065
1066 xfree (demangled_name);
1067 return ret;
1068 }
1069
1070 /* Table mapping opcodes into strings for printing operators
1071 and precedences of the operators. */
1072
1073 const struct op_print java_op_print_tab[] =
1074 {
1075 {",", BINOP_COMMA, PREC_COMMA, 0},
1076 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1077 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1078 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1079 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1080 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1081 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1082 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1083 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1084 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1085 {">=", BINOP_GEQ, PREC_ORDER, 0},
1086 {">", BINOP_GTR, PREC_ORDER, 0},
1087 {"<", BINOP_LESS, PREC_ORDER, 0},
1088 {">>", BINOP_RSH, PREC_SHIFT, 0},
1089 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1090 {"+", BINOP_ADD, PREC_ADD, 0},
1091 {"-", BINOP_SUB, PREC_ADD, 0},
1092 {"*", BINOP_MUL, PREC_MUL, 0},
1093 {"/", BINOP_DIV, PREC_MUL, 0},
1094 {"%", BINOP_REM, PREC_MUL, 0},
1095 {"-", UNOP_NEG, PREC_PREFIX, 0},
1096 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1097 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1098 {"*", UNOP_IND, PREC_PREFIX, 0},
1099 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1100 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1101 {NULL, 0, 0, 0}
1102 };
1103
1104 enum java_primitive_types
1105 {
1106 java_primitive_type_int,
1107 java_primitive_type_short,
1108 java_primitive_type_long,
1109 java_primitive_type_byte,
1110 java_primitive_type_boolean,
1111 java_primitive_type_char,
1112 java_primitive_type_float,
1113 java_primitive_type_double,
1114 java_primitive_type_void,
1115 nr_java_primitive_types
1116 };
1117
1118 static void
1119 java_language_arch_info (struct gdbarch *gdbarch,
1120 struct language_arch_info *lai)
1121 {
1122 const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
1123
1124 lai->string_char_type = builtin->builtin_char;
1125 lai->primitive_type_vector
1126 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_java_primitive_types + 1,
1127 struct type *);
1128 lai->primitive_type_vector [java_primitive_type_int]
1129 = builtin->builtin_int;
1130 lai->primitive_type_vector [java_primitive_type_short]
1131 = builtin->builtin_short;
1132 lai->primitive_type_vector [java_primitive_type_long]
1133 = builtin->builtin_long;
1134 lai->primitive_type_vector [java_primitive_type_byte]
1135 = builtin->builtin_byte;
1136 lai->primitive_type_vector [java_primitive_type_boolean]
1137 = builtin->builtin_boolean;
1138 lai->primitive_type_vector [java_primitive_type_char]
1139 = builtin->builtin_char;
1140 lai->primitive_type_vector [java_primitive_type_float]
1141 = builtin->builtin_float;
1142 lai->primitive_type_vector [java_primitive_type_double]
1143 = builtin->builtin_double;
1144 lai->primitive_type_vector [java_primitive_type_void]
1145 = builtin->builtin_void;
1146
1147 lai->bool_type_symbol = "boolean";
1148 lai->bool_type_default = builtin->builtin_boolean;
1149 }
1150
1151 const struct exp_descriptor exp_descriptor_java =
1152 {
1153 print_subexp_standard,
1154 operator_length_standard,
1155 operator_check_standard,
1156 op_name_standard,
1157 dump_subexp_body_standard,
1158 evaluate_subexp_java
1159 };
1160
1161 const struct language_defn java_language_defn =
1162 {
1163 "java", /* Language name */
1164 language_java,
1165 range_check_off,
1166 type_check_off,
1167 case_sensitive_on,
1168 array_row_major,
1169 macro_expansion_no,
1170 &exp_descriptor_java,
1171 java_parse,
1172 java_error,
1173 null_post_parser,
1174 c_printchar, /* Print a character constant */
1175 c_printstr, /* Function to print string constant */
1176 java_emit_char, /* Function to print a single character */
1177 java_print_type, /* Print a type using appropriate syntax */
1178 default_print_typedef, /* Print a typedef using appropriate syntax */
1179 java_val_print, /* Print a value using appropriate syntax */
1180 java_value_print, /* Print a top-level value */
1181 NULL, /* Language specific skip_trampoline */
1182 "this", /* name_of_this */
1183 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1184 basic_lookup_transparent_type,/* lookup_transparent_type */
1185 java_demangle, /* Language specific symbol demangler */
1186 java_class_name_from_physname,/* Language specific class name */
1187 java_op_print_tab, /* expression operators for printing */
1188 0, /* not c-style arrays */
1189 0, /* String lower bound */
1190 default_word_break_characters,
1191 default_make_symbol_completion_list,
1192 java_language_arch_info,
1193 default_print_array_index,
1194 default_pass_by_reference,
1195 default_get_string,
1196 LANG_MAGIC
1197 };
1198
1199 static void *
1200 build_java_types (struct gdbarch *gdbarch)
1201 {
1202 struct builtin_java_type *builtin_java_type
1203 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_java_type);
1204
1205 builtin_java_type->builtin_int
1206 = arch_integer_type (gdbarch, 32, 0, "int");
1207 builtin_java_type->builtin_short
1208 = arch_integer_type (gdbarch, 16, 0, "short");
1209 builtin_java_type->builtin_long
1210 = arch_integer_type (gdbarch, 64, 0, "long");
1211 builtin_java_type->builtin_byte
1212 = arch_integer_type (gdbarch, 8, 0, "byte");
1213 builtin_java_type->builtin_boolean
1214 = arch_boolean_type (gdbarch, 8, 0, "boolean");
1215 builtin_java_type->builtin_char
1216 = arch_character_type (gdbarch, 16, 1, "char");
1217 builtin_java_type->builtin_float
1218 = arch_float_type (gdbarch, 32, "float", NULL);
1219 builtin_java_type->builtin_double
1220 = arch_float_type (gdbarch, 64, "double", NULL);
1221 builtin_java_type->builtin_void
1222 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1223
1224 return builtin_java_type;
1225 }
1226
1227 static struct gdbarch_data *java_type_data;
1228
1229 const struct builtin_java_type *
1230 builtin_java_type (struct gdbarch *gdbarch)
1231 {
1232 return gdbarch_data (gdbarch, java_type_data);
1233 }
1234
1235 void
1236 _initialize_java_language (void)
1237 {
1238 jv_dynamics_objfile_data_key
1239 = register_objfile_data_with_cleanup (NULL, jv_per_objfile_free);
1240 jv_type_objfile_data_key
1241 = register_objfile_data_with_cleanup (NULL, jv_clear_object_type);
1242
1243 java_type_data = gdbarch_data_register_post_init (build_java_types);
1244
1245 add_language (&java_language_defn);
1246 }
This page took 0.069123 seconds and 4 git commands to generate.