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