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