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