vla: introduce new bound type abstraction adapt uses
[deliverable/binutils-gdb.git] / gdb / go-lang.c
CommitLineData
a766d390
DE
1/* Go language support routines for GDB, the GNU debugger.
2
ecd75fc8 3 Copyright (C) 2012-2014 Free Software Foundation, Inc.
a766d390
DE
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20/* TODO:
21 - split stacks
22 - printing of native types
23 - goroutines
24 - lots more
25 - gccgo mangling needs redoing
26 It's too hard, for example, to know whether one is looking at a mangled
27 Go symbol or not, and their are ambiguities, e.g., the demangler may
28 get passed *any* symbol, including symbols from other languages
29 and including symbols that are already demangled.
30 One thought is to at least add an _G prefix.
31 - 6g mangling isn't supported yet
32*/
33
34#include "defs.h"
35#include "gdb_assert.h"
36#include "gdb_obstack.h"
0e9f083f 37#include <string.h>
a766d390
DE
38#include "block.h"
39#include "symtab.h"
40#include "language.h"
a53b64ea 41#include "varobj.h"
a766d390
DE
42#include "go-lang.h"
43#include "c-lang.h"
44#include "parser-defs.h"
45
46#include <ctype.h>
47
48/* The main function in the main package. */
49static const char GO_MAIN_MAIN[] = "main.main";
50
51/* Function returning the special symbol name used by Go for the main
52 procedure in the main program if it is found in minimal symbol list.
53 This function tries to find minimal symbols so that it finds them even
54 if the program was compiled without debugging information. */
55
56const char *
57go_main_name (void)
58{
3b7344d5 59 struct bound_minimal_symbol msym;
a766d390
DE
60
61 msym = lookup_minimal_symbol (GO_MAIN_MAIN, NULL, NULL);
3b7344d5 62 if (msym.minsym != NULL)
a766d390
DE
63 return GO_MAIN_MAIN;
64
65 /* No known entry procedure found, the main program is probably not Go. */
66 return NULL;
67}
68
69/* Return non-zero if TYPE is a gccgo string.
70 We assume CHECK_TYPEDEF has already been done. */
71
72static int
73gccgo_string_p (struct type *type)
74{
75 /* gccgo strings don't necessarily have a name we can use. */
76
77 if (TYPE_NFIELDS (type) == 2)
78 {
79 struct type *type0 = TYPE_FIELD_TYPE (type, 0);
80 struct type *type1 = TYPE_FIELD_TYPE (type, 1);
81
82 CHECK_TYPEDEF (type0);
83 CHECK_TYPEDEF (type1);
84
85 if (TYPE_CODE (type0) == TYPE_CODE_PTR
86 && strcmp (TYPE_FIELD_NAME (type, 0), "__data") == 0
87 && TYPE_CODE (type1) == TYPE_CODE_INT
88 && strcmp (TYPE_FIELD_NAME (type, 1), "__length") == 0)
89 {
90 struct type *target_type = TYPE_TARGET_TYPE (type0);
91
92 CHECK_TYPEDEF (target_type);
93
94 if (TYPE_CODE (target_type) == TYPE_CODE_INT
95 && TYPE_LENGTH (target_type) == 1
96 && strcmp (TYPE_NAME (target_type), "uint8") == 0)
97 return 1;
98 }
99 }
100
101 return 0;
102}
103
104/* Return non-zero if TYPE is a 6g string.
105 We assume CHECK_TYPEDEF has already been done. */
106
107static int
108sixg_string_p (struct type *type)
109{
110 if (TYPE_NFIELDS (type) == 2
111 && TYPE_TAG_NAME (type) != NULL
112 && strcmp (TYPE_TAG_NAME (type), "string") == 0)
113 return 1;
114
115 return 0;
116}
117
118/* Classify the kind of Go object that TYPE is.
119 TYPE is a TYPE_CODE_STRUCT, used to represent a Go object. */
120
121enum go_type
122go_classify_struct_type (struct type *type)
123{
124 CHECK_TYPEDEF (type);
125
126 /* Recognize strings as they're useful to be able to print without
127 pretty-printers. */
128 if (gccgo_string_p (type)
129 || sixg_string_p (type))
130 return GO_TYPE_STRING;
131
132 return GO_TYPE_NONE;
133}
134
135/* Subroutine of unpack_mangled_go_symbol to simplify it.
136 Given "[foo.]bar.baz", store "bar" in *PACKAGEP and "baz" in *OBJECTP.
137 We stomp on the last '.' to nul-terminate "bar".
138 The caller is responsible for memory management. */
139
140static void
141unpack_package_and_object (char *buf,
142 const char **packagep, const char **objectp)
143{
144 char *last_dot;
145
146 last_dot = strrchr (buf, '.');
147 gdb_assert (last_dot != NULL);
148 *objectp = last_dot + 1;
149 *last_dot = '\0';
150 last_dot = strrchr (buf, '.');
151 if (last_dot != NULL)
152 *packagep = last_dot + 1;
153 else
154 *packagep = buf;
155}
156
157/* Given a mangled Go symbol, find its package name, object name, and
158 method type (if present).
159 E.g., for "libgo_net.textproto.String.N33_libgo_net.textproto.ProtocolError"
160 *PACKAGEP = "textproto"
161 *OBJECTP = "String"
162 *METHOD_TYPE_PACKAGEP = "textproto"
163 *METHOD_TYPE_OBJECTP = "ProtocolError"
164
165 Space for the resulting strings is malloc'd in one buffer.
166 PACKAGEP,OBJECTP,METHOD_TYPE* will (typically) point into this buffer.
167 [There are a few exceptions, but the caller is still responsible for
168 freeing the resulting pointer.]
169 A pointer to this buffer is returned, or NULL if symbol isn't a
170 mangled Go symbol.
171 The caller is responsible for freeing the result.
172
173 *METHOD_TYPE_IS_POINTERP is set to a boolean indicating if
174 the method type is a pointer.
175
176 There may be value in returning the outer container,
177 i.e., "net" in the above example, but for now it's not needed.
178 Plus it's currently not straightforward to compute,
179 it comes from -fgo-prefix, and there's no algorithm to compute it.
180
181 If we ever need to unpack the method type, this routine should work
182 for that too. */
183
184static char *
185unpack_mangled_go_symbol (const char *mangled_name,
186 const char **packagep,
187 const char **objectp,
188 const char **method_type_packagep,
189 const char **method_type_objectp,
190 int *method_type_is_pointerp)
191{
192 char *buf;
193 char *p;
194 int len = strlen (mangled_name);
195 /* Pointer to last digit in "N<digit(s)>_". */
196 char *saw_digit;
197 /* Pointer to "N" if valid "N<digit(s)>_" found. */
198 char *method_type;
199 /* Pointer to the first '.'. */
200 char *first_dot;
201 /* Pointer to the last '.'. */
202 char *last_dot;
203 /* Non-zero if we saw a pointer indicator. */
204 int saw_pointer;
205
206 *packagep = *objectp = NULL;
207 *method_type_packagep = *method_type_objectp = NULL;
208 *method_type_is_pointerp = 0;
209
210 /* main.init is mangled specially. */
211 if (strcmp (mangled_name, "__go_init_main") == 0)
212 {
213 char *package = xstrdup ("main");
214
215 *packagep = package;
216 *objectp = "init";
217 return package;
218 }
219
220 /* main.main is mangled specially (missing prefix). */
221 if (strcmp (mangled_name, "main.main") == 0)
222 {
223 char *package = xstrdup ("main");
224
225 *packagep = package;
226 *objectp = "main";
227 return package;
228 }
229
230 /* We may get passed, e.g., "main.T.Foo", which is *not* mangled.
231 Alas it looks exactly like "prefix.package.object."
232 To cope for now we only recognize the following prefixes:
233
234 go: the default
235 libgo_.*: used by gccgo's runtime
236
237 Thus we don't support -fgo-prefix (except as used by the runtime). */
238 if (strncmp (mangled_name, "go.", 3) != 0
239 && strncmp (mangled_name, "libgo_", 6) != 0)
240 return NULL;
241
242 /* Quick check for whether a search may be fruitful. */
243 /* Ignore anything with @plt, etc. in it. */
244 if (strchr (mangled_name, '@') != NULL)
245 return NULL;
246 /* It must have at least two dots. */
247 first_dot = strchr (mangled_name, '.');
248 if (first_dot == NULL)
249 return NULL;
250 /* Treat "foo.bar" as unmangled. It can collide with lots of other
251 languages and it's not clear what the consequences are.
252 And except for main.main, all gccgo symbols are at least
253 prefix.package.object. */
254 last_dot = strrchr (mangled_name, '.');
255 if (last_dot == first_dot)
256 return NULL;
257
258 /* More quick checks. */
259 if (last_dot[1] == '\0' /* foo. */
260 || last_dot[-1] == '.') /* foo..bar */
261 return NULL;
262
263 /* At this point we've decided we have a mangled Go symbol. */
264
265 buf = xstrdup (mangled_name);
266
267 /* Search backwards looking for "N<digit(s)>". */
268 p = buf + len;
269 saw_digit = method_type = NULL;
270 saw_pointer = 0;
271 while (p > buf)
272 {
273 int current = *(const unsigned char *) --p;
274 int current_is_digit = isdigit (current);
275
276 if (saw_digit)
277 {
278 if (current_is_digit)
279 continue;
280 if (current == 'N'
281 && ((p > buf && p[-1] == '.')
282 || (p > buf + 1 && p[-1] == 'p' && p[-2] == '.')))
283 {
284 if (atoi (p + 1) == strlen (saw_digit + 2))
285 {
286 if (p[-1] == '.')
287 method_type = p - 1;
288 else
289 {
290 gdb_assert (p[-1] == 'p');
291 saw_pointer = 1;
292 method_type = p - 2;
293 }
294 break;
295 }
296 }
297 /* Not what we're looking for, reset and keep looking. */
298 saw_digit = NULL;
299 saw_pointer = 0;
300 continue;
301 }
302 if (current_is_digit && p[1] == '_')
303 {
304 /* Possible start of method "this" [sic] type. */
305 saw_digit = p;
306 continue;
307 }
308 }
309
310 if (method_type != NULL
311 /* Ensure not something like "..foo". */
312 && (method_type > buf && method_type[-1] != '.'))
313 {
314 unpack_package_and_object (saw_digit + 2,
315 method_type_packagep, method_type_objectp);
316 *method_type = '\0';
317 *method_type_is_pointerp = saw_pointer;
318 }
319
320 unpack_package_and_object (buf, packagep, objectp);
321 return buf;
322}
323
324/* Implements the la_demangle language_defn routine for language Go.
325
326 N.B. This may get passed *any* symbol, including symbols from other
327 languages and including symbols that are already demangled.
328 Both of these situations are kinda unfortunate, but that's how things
329 are today.
330
331 N.B. This currently only supports gccgo's mangling.
332
333 N.B. gccgo's mangling needs, I think, changing.
334 This demangler can't work in all situations,
335 thus not too much effort is currently put into it. */
336
337char *
338go_demangle (const char *mangled_name, int options)
339{
340 struct obstack tempbuf;
341 char *result;
342 char *name_buf;
343 const char *package_name;
344 const char *object_name;
345 const char *method_type_package_name;
346 const char *method_type_object_name;
347 int method_type_is_pointer;
348
349 if (mangled_name == NULL)
350 return NULL;
351
352 name_buf = unpack_mangled_go_symbol (mangled_name,
353 &package_name, &object_name,
354 &method_type_package_name,
355 &method_type_object_name,
356 &method_type_is_pointer);
357 if (name_buf == NULL)
358 return NULL;
359
360 obstack_init (&tempbuf);
361
362 /* Print methods as they appear in "method expressions". */
363 if (method_type_package_name != NULL)
364 {
365 /* FIXME: Seems like we should include package_name here somewhere. */
366 if (method_type_is_pointer)
367 obstack_grow_str (&tempbuf, "(*");
368 obstack_grow_str (&tempbuf, method_type_package_name);
369 obstack_grow_str (&tempbuf, ".");
370 obstack_grow_str (&tempbuf, method_type_object_name);
371 if (method_type_is_pointer)
372 obstack_grow_str (&tempbuf, ")");
373 obstack_grow_str (&tempbuf, ".");
374 obstack_grow_str (&tempbuf, object_name);
375 }
376 else
377 {
378 obstack_grow_str (&tempbuf, package_name);
379 obstack_grow_str (&tempbuf, ".");
380 obstack_grow_str (&tempbuf, object_name);
381 }
382 obstack_grow_str0 (&tempbuf, "");
383
384 result = xstrdup (obstack_finish (&tempbuf));
385 obstack_free (&tempbuf, NULL);
386 xfree (name_buf);
387 return result;
388}
389
390/* Given a Go symbol, return its package or NULL if unknown.
391 Space for the result is malloc'd, caller must free. */
392
393char *
394go_symbol_package_name (const struct symbol *sym)
395{
396 const char *mangled_name = SYMBOL_LINKAGE_NAME (sym);
397 const char *package_name;
398 const char *object_name;
399 const char *method_type_package_name;
400 const char *method_type_object_name;
401 int method_type_is_pointer;
402 char *name_buf;
403 char *result;
404
405 gdb_assert (SYMBOL_LANGUAGE (sym) == language_go);
406 name_buf = unpack_mangled_go_symbol (mangled_name,
407 &package_name, &object_name,
408 &method_type_package_name,
409 &method_type_object_name,
410 &method_type_is_pointer);
411 /* Some Go symbols don't have mangled form we interpret (yet). */
412 if (name_buf == NULL)
413 return NULL;
414 result = xstrdup (package_name);
415 xfree (name_buf);
416 return result;
417}
418
419/* Return the package that BLOCK is in, or NULL if there isn't one.
420 Space for the result is malloc'd, caller must free. */
421
422char *
423go_block_package_name (const struct block *block)
424{
425 while (block != NULL)
426 {
427 struct symbol *function = BLOCK_FUNCTION (block);
428
429 if (function != NULL)
430 {
431 char *package_name = go_symbol_package_name (function);
432
433 if (package_name != NULL)
434 return package_name;
435
436 /* Stop looking if we find a function without a package name.
437 We're most likely outside of Go and thus the concept of the
438 "current" package is gone. */
439 return NULL;
440 }
441
442 block = BLOCK_SUPERBLOCK (block);
443 }
444
445 return NULL;
446}
447
448/* Table mapping opcodes into strings for printing operators
449 and precedences of the operators.
450 TODO(dje): &^ ? */
451
452static const struct op_print go_op_print_tab[] =
453{
454 {",", BINOP_COMMA, PREC_COMMA, 0},
455 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
456 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
457 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
458 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
459 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
460 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
461 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
462 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
463 {"<=", BINOP_LEQ, PREC_ORDER, 0},
464 {">=", BINOP_GEQ, PREC_ORDER, 0},
465 {">", BINOP_GTR, PREC_ORDER, 0},
466 {"<", BINOP_LESS, PREC_ORDER, 0},
467 {">>", BINOP_RSH, PREC_SHIFT, 0},
468 {"<<", BINOP_LSH, PREC_SHIFT, 0},
469 {"+", BINOP_ADD, PREC_ADD, 0},
470 {"-", BINOP_SUB, PREC_ADD, 0},
471 {"*", BINOP_MUL, PREC_MUL, 0},
472 {"/", BINOP_DIV, PREC_MUL, 0},
473 {"%", BINOP_REM, PREC_MUL, 0},
474 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
475 {"-", UNOP_NEG, PREC_PREFIX, 0},
476 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
477 {"^", UNOP_COMPLEMENT, PREC_PREFIX, 0},
478 {"*", UNOP_IND, PREC_PREFIX, 0},
479 {"&", UNOP_ADDR, PREC_PREFIX, 0},
480 {"unsafe.Sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
481 {"++", UNOP_POSTINCREMENT, PREC_SUFFIX, 0},
482 {"--", UNOP_POSTDECREMENT, PREC_SUFFIX, 0},
483 {NULL, 0, 0, 0}
484};
485
486enum go_primitive_types {
487 go_primitive_type_void,
488 go_primitive_type_char,
489 go_primitive_type_bool,
490 go_primitive_type_int,
491 go_primitive_type_uint,
492 go_primitive_type_uintptr,
493 go_primitive_type_int8,
494 go_primitive_type_int16,
495 go_primitive_type_int32,
496 go_primitive_type_int64,
497 go_primitive_type_uint8,
498 go_primitive_type_uint16,
499 go_primitive_type_uint32,
500 go_primitive_type_uint64,
501 go_primitive_type_float32,
502 go_primitive_type_float64,
503 go_primitive_type_complex64,
504 go_primitive_type_complex128,
505 nr_go_primitive_types
506};
507
508static void
509go_language_arch_info (struct gdbarch *gdbarch,
510 struct language_arch_info *lai)
511{
512 const struct builtin_go_type *builtin = builtin_go_type (gdbarch);
513
514 lai->string_char_type = builtin->builtin_char;
515
516 lai->primitive_type_vector
517 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_go_primitive_types + 1,
518 struct type *);
519
520 lai->primitive_type_vector [go_primitive_type_void]
521 = builtin->builtin_void;
522 lai->primitive_type_vector [go_primitive_type_char]
523 = builtin->builtin_char;
524 lai->primitive_type_vector [go_primitive_type_bool]
525 = builtin->builtin_bool;
526 lai->primitive_type_vector [go_primitive_type_int]
527 = builtin->builtin_int;
528 lai->primitive_type_vector [go_primitive_type_uint]
529 = builtin->builtin_uint;
530 lai->primitive_type_vector [go_primitive_type_uintptr]
531 = builtin->builtin_uintptr;
532 lai->primitive_type_vector [go_primitive_type_int8]
533 = builtin->builtin_int8;
534 lai->primitive_type_vector [go_primitive_type_int16]
535 = builtin->builtin_int16;
536 lai->primitive_type_vector [go_primitive_type_int32]
537 = builtin->builtin_int32;
538 lai->primitive_type_vector [go_primitive_type_int64]
539 = builtin->builtin_int64;
540 lai->primitive_type_vector [go_primitive_type_uint8]
541 = builtin->builtin_uint8;
542 lai->primitive_type_vector [go_primitive_type_uint16]
543 = builtin->builtin_uint16;
544 lai->primitive_type_vector [go_primitive_type_uint32]
545 = builtin->builtin_uint32;
546 lai->primitive_type_vector [go_primitive_type_uint64]
547 = builtin->builtin_uint64;
548 lai->primitive_type_vector [go_primitive_type_float32]
549 = builtin->builtin_float32;
550 lai->primitive_type_vector [go_primitive_type_float64]
551 = builtin->builtin_float64;
552 lai->primitive_type_vector [go_primitive_type_complex64]
553 = builtin->builtin_complex64;
554 lai->primitive_type_vector [go_primitive_type_complex128]
555 = builtin->builtin_complex128;
556
557 lai->bool_type_symbol = "bool";
558 lai->bool_type_default = builtin->builtin_bool;
559}
560
561static const struct language_defn go_language_defn =
562{
563 "go",
6abde28f 564 "Go",
a766d390
DE
565 language_go,
566 range_check_off,
a766d390
DE
567 case_sensitive_on,
568 array_row_major,
569 macro_expansion_no,
570 &exp_descriptor_c,
571 go_parse,
572 go_error,
573 null_post_parser,
574 c_printchar, /* Print a character constant. */
575 c_printstr, /* Function to print string constant. */
576 c_emit_char, /* Print a single char. */
577 go_print_type, /* Print a type using appropriate syntax. */
578 c_print_typedef, /* Print a typedef using appropriate
579 syntax. */
580 go_val_print, /* Print a value using appropriate syntax. */
581 c_value_print, /* Print a top-level value. */
582 default_read_var_value, /* la_read_var_value */
583 NULL, /* Language specific skip_trampoline. */
584 NULL, /* name_of_this */
585 basic_lookup_symbol_nonlocal,
586 basic_lookup_transparent_type,
587 go_demangle, /* Language specific symbol demangler. */
588 NULL, /* Language specific
589 class_name_from_physname. */
590 go_op_print_tab, /* Expression operators for printing. */
591 1, /* C-style arrays. */
592 0, /* String lower bound. */
593 default_word_break_characters,
594 default_make_symbol_completion_list,
595 go_language_arch_info,
596 default_print_array_index,
597 default_pass_by_reference,
598 c_get_string,
599 NULL,
600 iterate_over_symbols,
a53b64ea 601 &default_varobj_ops,
a766d390
DE
602 LANG_MAGIC
603};
604
605static void *
606build_go_types (struct gdbarch *gdbarch)
607{
608 struct builtin_go_type *builtin_go_type
609 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_go_type);
610
611 builtin_go_type->builtin_void
612 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
613 builtin_go_type->builtin_char
614 = arch_character_type (gdbarch, 8, 1, "char");
615 builtin_go_type->builtin_bool
616 = arch_boolean_type (gdbarch, 8, 0, "bool");
617 builtin_go_type->builtin_int
618 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "int");
619 builtin_go_type->builtin_uint
620 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "uint");
621 builtin_go_type->builtin_uintptr
622 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr");
623 builtin_go_type->builtin_int8
624 = arch_integer_type (gdbarch, 8, 0, "int8");
625 builtin_go_type->builtin_int16
626 = arch_integer_type (gdbarch, 16, 0, "int16");
627 builtin_go_type->builtin_int32
628 = arch_integer_type (gdbarch, 32, 0, "int32");
629 builtin_go_type->builtin_int64
630 = arch_integer_type (gdbarch, 64, 0, "int64");
631 builtin_go_type->builtin_uint8
632 = arch_integer_type (gdbarch, 8, 1, "uint8");
633 builtin_go_type->builtin_uint16
634 = arch_integer_type (gdbarch, 16, 1, "uint16");
635 builtin_go_type->builtin_uint32
636 = arch_integer_type (gdbarch, 32, 1, "uint32");
637 builtin_go_type->builtin_uint64
638 = arch_integer_type (gdbarch, 64, 1, "uint64");
639 builtin_go_type->builtin_float32
640 = arch_float_type (gdbarch, 32, "float32", NULL);
641 builtin_go_type->builtin_float64
642 = arch_float_type (gdbarch, 64, "float64", NULL);
643 builtin_go_type->builtin_complex64
644 = arch_complex_type (gdbarch, "complex64",
645 builtin_go_type->builtin_float32);
646 builtin_go_type->builtin_complex128
647 = arch_complex_type (gdbarch, "complex128",
648 builtin_go_type->builtin_float64);
649
650 return builtin_go_type;
651}
652
653static struct gdbarch_data *go_type_data;
654
655const struct builtin_go_type *
656builtin_go_type (struct gdbarch *gdbarch)
657{
658 return gdbarch_data (gdbarch, go_type_data);
659}
660
661extern initialize_file_ftype _initialize_go_language;
662
663void
664_initialize_go_language (void)
665{
666 go_type_data = gdbarch_data_register_post_init (build_go_types);
667
668 add_language (&go_language_defn);
669}
This page took 0.230418 seconds and 4 git commands to generate.