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