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