Don't use class-initialization for the owner union
[deliverable/binutils-gdb.git] / gdb / python / py-symbol.c
CommitLineData
f3e9a817
PM
1/* Python interface to symbols.
2
42a4f53d 3 Copyright (C) 2008-2019 Free Software Foundation, Inc.
f3e9a817
PM
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#include "defs.h"
21#include "block.h"
f3e9a817
PM
22#include "frame.h"
23#include "symtab.h"
24#include "python-internal.h"
25#include "objfiles.h"
086baaf1 26#include "symfile.h"
f3e9a817
PM
27
28typedef struct sympy_symbol_object {
29 PyObject_HEAD
30 /* The GDB symbol structure this object is wrapping. */
31 struct symbol *symbol;
32 /* A symbol object is associated with an objfile, so keep track with
33 doubly-linked list, rooted in the objfile. This lets us
34 invalidate the underlying struct symbol when the objfile is
35 deleted. */
36 struct sympy_symbol_object *prev;
37 struct sympy_symbol_object *next;
38} symbol_object;
39
40/* Require a valid symbol. All access to symbol_object->symbol should be
41 gated by this call. */
42#define SYMPY_REQUIRE_VALID(symbol_obj, symbol) \
43 do { \
44 symbol = symbol_object_to_symbol (symbol_obj); \
45 if (symbol == NULL) \
46 { \
47 PyErr_SetString (PyExc_RuntimeError, \
48 _("Symbol is invalid.")); \
49 return NULL; \
50 } \
51 } while (0)
52
53static const struct objfile_data *sympy_objfile_data_key;
54
55static PyObject *
56sympy_str (PyObject *self)
57{
58 PyObject *result;
59 struct symbol *symbol = NULL;
60
61 SYMPY_REQUIRE_VALID (self, symbol);
62
63 result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));
64
65 return result;
66}
67
457e09f0
DE
68static PyObject *
69sympy_get_type (PyObject *self, void *closure)
70{
71 struct symbol *symbol = NULL;
72
73 SYMPY_REQUIRE_VALID (self, symbol);
74
75 if (SYMBOL_TYPE (symbol) == NULL)
76 {
77 Py_INCREF (Py_None);
78 return Py_None;
79 }
80
81 return type_to_type_object (SYMBOL_TYPE (symbol));
82}
83
f3e9a817
PM
84static PyObject *
85sympy_get_symtab (PyObject *self, void *closure)
86{
87 struct symbol *symbol = NULL;
88
89 SYMPY_REQUIRE_VALID (self, symbol);
90
1994afbf
DE
91 if (!SYMBOL_OBJFILE_OWNED (symbol))
92 Py_RETURN_NONE;
93
08be3fe3 94 return symtab_to_symtab_object (symbol_symtab (symbol));
f3e9a817
PM
95}
96
97static PyObject *
98sympy_get_name (PyObject *self, void *closure)
99{
100 struct symbol *symbol = NULL;
101
102 SYMPY_REQUIRE_VALID (self, symbol);
103
104 return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
105}
106
107static PyObject *
108sympy_get_linkage_name (PyObject *self, void *closure)
109{
110 struct symbol *symbol = NULL;
111
112 SYMPY_REQUIRE_VALID (self, symbol);
113
114 return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
115}
116
117static PyObject *
118sympy_get_print_name (PyObject *self, void *closure)
119{
120 struct symbol *symbol = NULL;
121
122 SYMPY_REQUIRE_VALID (self, symbol);
123
124 return sympy_str (self);
125}
126
127static PyObject *
128sympy_get_addr_class (PyObject *self, void *closure)
129{
130 struct symbol *symbol = NULL;
131
132 SYMPY_REQUIRE_VALID (self, symbol);
133
134 return PyInt_FromLong (SYMBOL_CLASS (symbol));
135}
136
137static PyObject *
138sympy_is_argument (PyObject *self, void *closure)
139{
140 struct symbol *symbol = NULL;
141
142 SYMPY_REQUIRE_VALID (self, symbol);
143
144 return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
145}
146
147static PyObject *
148sympy_is_constant (PyObject *self, void *closure)
149{
150 struct symbol *symbol = NULL;
fe978cb0 151 enum address_class theclass;
f3e9a817
PM
152
153 SYMPY_REQUIRE_VALID (self, symbol);
154
fe978cb0 155 theclass = SYMBOL_CLASS (symbol);
f3e9a817 156
fe978cb0 157 return PyBool_FromLong (theclass == LOC_CONST || theclass == LOC_CONST_BYTES);
f3e9a817
PM
158}
159
160static PyObject *
161sympy_is_function (PyObject *self, void *closure)
162{
163 struct symbol *symbol = NULL;
fe978cb0 164 enum address_class theclass;
f3e9a817
PM
165
166 SYMPY_REQUIRE_VALID (self, symbol);
167
fe978cb0 168 theclass = SYMBOL_CLASS (symbol);
f3e9a817 169
fe978cb0 170 return PyBool_FromLong (theclass == LOC_BLOCK);
f3e9a817
PM
171}
172
173static PyObject *
174sympy_is_variable (PyObject *self, void *closure)
175{
176 struct symbol *symbol = NULL;
fe978cb0 177 enum address_class theclass;
f3e9a817
PM
178
179 SYMPY_REQUIRE_VALID (self, symbol);
180
fe978cb0 181 theclass = SYMBOL_CLASS (symbol);
f3e9a817
PM
182
183 return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
fe978cb0
PA
184 && (theclass == LOC_LOCAL || theclass == LOC_REGISTER
185 || theclass == LOC_STATIC || theclass == LOC_COMPUTED
186 || theclass == LOC_OPTIMIZED_OUT));
f3e9a817
PM
187}
188
f0823d2c
TT
189/* Implementation of gdb.Symbol.needs_frame -> Boolean.
190 Returns true iff the symbol needs a frame for evaluation. */
191
192static PyObject *
193sympy_needs_frame (PyObject *self, void *closure)
194{
195 struct symbol *symbol = NULL;
f0823d2c
TT
196 int result = 0;
197
198 SYMPY_REQUIRE_VALID (self, symbol);
199
a70b8144 200 try
f0823d2c
TT
201 {
202 result = symbol_read_needs_frame (symbol);
203 }
230d2906 204 catch (const gdb_exception &except)
492d29ea
PA
205 {
206 GDB_PY_HANDLE_EXCEPTION (except);
207 }
f0823d2c
TT
208
209 if (result)
210 Py_RETURN_TRUE;
211 Py_RETURN_FALSE;
212}
213
64e7d9dd
TT
214/* Implementation of gdb.Symbol.line -> int.
215 Returns the line number at which the symbol was defined. */
216
217static PyObject *
218sympy_line (PyObject *self, void *closure)
219{
220 struct symbol *symbol = NULL;
221
222 SYMPY_REQUIRE_VALID (self, symbol);
223
224 return PyInt_FromLong (SYMBOL_LINE (symbol));
225}
226
29703da4
PM
227/* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
228 Returns True if this Symbol still exists in GDB. */
229
230static PyObject *
231sympy_is_valid (PyObject *self, PyObject *args)
232{
233 struct symbol *symbol = NULL;
234
235 symbol = symbol_object_to_symbol (self);
236 if (symbol == NULL)
237 Py_RETURN_FALSE;
238
239 Py_RETURN_TRUE;
240}
241
f0823d2c
TT
242/* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns
243 the value of the symbol, or an error in various circumstances. */
244
245static PyObject *
246sympy_value (PyObject *self, PyObject *args)
247{
248 struct symbol *symbol = NULL;
249 struct frame_info *frame_info = NULL;
250 PyObject *frame_obj = NULL;
251 struct value *value = NULL;
f0823d2c
TT
252
253 if (!PyArg_ParseTuple (args, "|O", &frame_obj))
254 return NULL;
255
256 if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
257 {
258 PyErr_SetString (PyExc_TypeError, "argument is not a frame");
259 return NULL;
260 }
261
262 SYMPY_REQUIRE_VALID (self, symbol);
263 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
264 {
265 PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
266 return NULL;
267 }
268
a70b8144 269 try
f0823d2c
TT
270 {
271 if (frame_obj != NULL)
272 {
273 frame_info = frame_object_to_frame_info (frame_obj);
274 if (frame_info == NULL)
c6910659 275 error (_("invalid frame"));
f0823d2c 276 }
256458bc 277
f0823d2c 278 if (symbol_read_needs_frame (symbol) && frame_info == NULL)
c6910659 279 error (_("symbol requires a frame to compute its value"));
f0823d2c 280
63e43d3a
PMR
281 /* TODO: currently, we have no way to recover the block in which SYMBOL
282 was found, so we have no block to pass to read_var_value. This will
283 yield an incorrect value when symbol is not local to FRAME_INFO (this
284 can happen with nested functions). */
285 value = read_var_value (symbol, NULL, frame_info);
f0823d2c 286 }
230d2906 287 catch (const gdb_exception &except)
492d29ea
PA
288 {
289 GDB_PY_HANDLE_EXCEPTION (except);
290 }
f0823d2c
TT
291
292 return value_to_value_object (value);
293}
294
f3e9a817
PM
295/* Given a symbol, and a symbol_object that has previously been
296 allocated and initialized, populate the symbol_object with the
297 struct symbol data. Also, register the symbol_object life-cycle
b021a221 298 with the life-cycle of the object file associated with this
f3e9a817
PM
299 symbol, if needed. */
300static void
301set_symbol (symbol_object *obj, struct symbol *symbol)
302{
303 obj->symbol = symbol;
304 obj->prev = NULL;
1994afbf
DE
305 if (SYMBOL_OBJFILE_OWNED (symbol)
306 && symbol_symtab (symbol) != NULL)
f3e9a817 307 {
08be3fe3 308 struct objfile *objfile = symbol_objfile (symbol);
f3e9a817 309
19ba03f4
SM
310 obj->next = ((struct sympy_symbol_object *)
311 objfile_data (objfile, sympy_objfile_data_key));
f3e9a817
PM
312 if (obj->next)
313 obj->next->prev = obj;
08be3fe3 314 set_objfile_data (objfile, sympy_objfile_data_key, obj);
f3e9a817
PM
315 }
316 else
317 obj->next = NULL;
318}
319
320/* Create a new symbol object (gdb.Symbol) that encapsulates the struct
321 symbol object from GDB. */
322PyObject *
323symbol_to_symbol_object (struct symbol *sym)
324{
325 symbol_object *sym_obj;
326
327 sym_obj = PyObject_New (symbol_object, &symbol_object_type);
328 if (sym_obj)
329 set_symbol (sym_obj, sym);
330
331 return (PyObject *) sym_obj;
332}
333
334/* Return the symbol that is wrapped by this symbol object. */
335struct symbol *
336symbol_object_to_symbol (PyObject *obj)
337{
338 if (! PyObject_TypeCheck (obj, &symbol_object_type))
339 return NULL;
340 return ((symbol_object *) obj)->symbol;
341}
342
343static void
344sympy_dealloc (PyObject *obj)
345{
346 symbol_object *sym_obj = (symbol_object *) obj;
347
348 if (sym_obj->prev)
349 sym_obj->prev->next = sym_obj->next;
08be3fe3 350 else if (sym_obj->symbol != NULL
1994afbf 351 && SYMBOL_OBJFILE_OWNED (sym_obj->symbol)
08be3fe3 352 && symbol_symtab (sym_obj->symbol) != NULL)
f3e9a817 353 {
08be3fe3 354 set_objfile_data (symbol_objfile (sym_obj->symbol),
f3e9a817
PM
355 sympy_objfile_data_key, sym_obj->next);
356 }
357 if (sym_obj->next)
358 sym_obj->next->prev = sym_obj->prev;
359 sym_obj->symbol = NULL;
360}
361
362/* Implementation of
363 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
364 A tuple with 2 elements is always returned. The first is the symbol
365 object or None, the second is a boolean with the value of
366 is_a_field_of_this (see comment in lookup_symbol_in_language). */
6e6fbe60 367
f3e9a817
PM
368PyObject *
369gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
370{
1993b719
TT
371 int domain = VAR_DOMAIN;
372 struct field_of_this_result is_a_field_of_this;
f3e9a817 373 const char *name;
2adadf51 374 static const char *keywords[] = { "name", "block", "domain", NULL };
76dce0be 375 struct symbol *symbol = NULL;
37fce74f 376 PyObject *block_obj = NULL, *sym_obj, *bool_obj;
9df2fbc4 377 const struct block *block = NULL;
f3e9a817 378
2adadf51
PA
379 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
380 &block_object_type, &block_obj,
381 &domain))
f3e9a817
PM
382 return NULL;
383
384 if (block_obj)
385 block = block_object_to_block (block_obj);
386 else
387 {
388 struct frame_info *selected_frame;
f3e9a817 389
a70b8144 390 try
f3e9a817 391 {
626e7282
JK
392 selected_frame = get_selected_frame (_("No frame selected."));
393 block = get_frame_block (selected_frame, NULL);
f3e9a817 394 }
230d2906 395 catch (const gdb_exception &except)
492d29ea
PA
396 {
397 GDB_PY_HANDLE_EXCEPTION (except);
398 }
f3e9a817
PM
399 }
400
a70b8144 401 try
76dce0be 402 {
aead7601
SM
403 symbol = lookup_symbol (name, block, (domain_enum) domain,
404 &is_a_field_of_this).symbol;
76dce0be 405 }
230d2906 406 catch (const gdb_exception &except)
492d29ea
PA
407 {
408 GDB_PY_HANDLE_EXCEPTION (except);
409 }
f3e9a817 410
7780f186 411 gdbpy_ref<> ret_tuple (PyTuple_New (2));
37fce74f 412 if (ret_tuple == NULL)
f3e9a817
PM
413 return NULL;
414
415 if (symbol)
416 {
417 sym_obj = symbol_to_symbol_object (symbol);
418 if (!sym_obj)
37fce74f 419 return NULL;
f3e9a817
PM
420 }
421 else
422 {
423 sym_obj = Py_None;
424 Py_INCREF (Py_None);
425 }
37fce74f 426 PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj);
f3e9a817 427
1993b719 428 bool_obj = (is_a_field_of_this.type != NULL) ? Py_True : Py_False;
f3e9a817 429 Py_INCREF (bool_obj);
37fce74f 430 PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj);
f3e9a817 431
37fce74f 432 return ret_tuple.release ();
f3e9a817
PM
433}
434
6e6fbe60
DE
435/* Implementation of
436 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
437
438PyObject *
439gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
440{
441 int domain = VAR_DOMAIN;
442 const char *name;
2adadf51 443 static const char *keywords[] = { "name", "domain", NULL };
76dce0be 444 struct symbol *symbol = NULL;
6e6fbe60
DE
445 PyObject *sym_obj;
446
2adadf51
PA
447 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
448 &domain))
6e6fbe60
DE
449 return NULL;
450
a70b8144 451 try
76dce0be 452 {
aead7601 453 symbol = lookup_global_symbol (name, NULL, (domain_enum) domain).symbol;
76dce0be 454 }
230d2906 455 catch (const gdb_exception &except)
492d29ea
PA
456 {
457 GDB_PY_HANDLE_EXCEPTION (except);
458 }
6e6fbe60
DE
459
460 if (symbol)
461 {
462 sym_obj = symbol_to_symbol_object (symbol);
463 if (!sym_obj)
464 return NULL;
465 }
466 else
467 {
468 sym_obj = Py_None;
469 Py_INCREF (Py_None);
470 }
471
472 return sym_obj;
473}
474
2906593f 475/* Implementation of
09ff83af 476 gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */
2906593f
CB
477
478PyObject *
479gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
480{
481 const char *name;
482 int domain = VAR_DOMAIN;
483 static const char *keywords[] = { "name", "domain", NULL };
484 struct symbol *symbol = NULL;
485 PyObject *sym_obj;
486
487 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
488 &domain))
489 return NULL;
490
09ff83af
AB
491 /* In order to find static symbols associated with the "current" object
492 file ahead of those from other object files, we first need to see if
493 we can acquire a current block. If this fails however, then we still
494 want to search all static symbols, so don't throw an exception just
495 yet. */
496 const struct block *block = NULL;
2906593f
CB
497 try
498 {
09ff83af
AB
499 struct frame_info *selected_frame
500 = get_selected_frame (_("No frame selected."));
501 block = get_frame_block (selected_frame, NULL);
502 }
503 catch (const gdb_exception &except)
504 {
505 /* Nothing. */
506 }
507
508 try
509 {
510 if (block != nullptr)
511 symbol
512 = lookup_symbol_in_static_block (name, block,
513 (domain_enum) domain).symbol;
514
515 if (symbol == nullptr)
516 symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
2906593f
CB
517 }
518 catch (const gdb_exception &except)
519 {
520 GDB_PY_HANDLE_EXCEPTION (except);
521 }
522
523 if (symbol)
524 {
525 sym_obj = symbol_to_symbol_object (symbol);
526 if (!sym_obj)
527 return NULL;
528 }
529 else
530 {
531 sym_obj = Py_None;
532 Py_INCREF (Py_None);
533 }
534
535 return sym_obj;
536}
537
086baaf1
AB
538/* Implementation of
539 gdb.lookup_static_symbols (name [, domain]) -> symbol list.
540
541 Returns a list of all static symbols matching NAME in DOMAIN. */
542
543PyObject *
544gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
545{
546 const char *name;
547 int domain = VAR_DOMAIN;
548 static const char *keywords[] = { "name", "domain", NULL };
549
550 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
551 &domain))
552 return NULL;
553
554 gdbpy_ref<> return_list (PyList_New (0));
555 if (return_list == NULL)
556 return NULL;
557
558 try
559 {
560 /* Expand any symtabs that contain potentially matching symbols. */
561 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
562 expand_symtabs_matching (NULL, lookup_name, NULL, NULL, ALL_DOMAIN);
563
564 for (objfile *objfile : current_program_space->objfiles ())
565 {
566 for (compunit_symtab *cust : objfile->compunits ())
567 {
568 const struct blockvector *bv;
569 const struct block *block;
570
571 bv = COMPUNIT_BLOCKVECTOR (cust);
572 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
573
574 if (block != nullptr)
575 {
576 symbol *symbol = lookup_symbol_in_static_block
577 (name, block, (domain_enum) domain).symbol;
578
579 if (symbol != nullptr)
580 {
581 PyObject *sym_obj
582 = symbol_to_symbol_object (symbol);
583 if (PyList_Append (return_list.get (), sym_obj) == -1)
584 return NULL;
585 }
586 }
587 }
588 }
589 }
590 catch (const gdb_exception &except)
591 {
592 GDB_PY_HANDLE_EXCEPTION (except);
593 }
594
595 return return_list.release ();
596}
597
f3e9a817
PM
598/* This function is called when an objfile is about to be freed.
599 Invalidate the symbol as further actions on the symbol would result
600 in bad data. All access to obj->symbol should be gated by
601 SYMPY_REQUIRE_VALID which will raise an exception on invalid
602 symbols. */
603static void
604del_objfile_symbols (struct objfile *objfile, void *datum)
605{
19ba03f4 606 symbol_object *obj = (symbol_object *) datum;
f3e9a817
PM
607 while (obj)
608 {
609 symbol_object *next = obj->next;
610
611 obj->symbol = NULL;
612 obj->next = NULL;
613 obj->prev = NULL;
614
615 obj = next;
616 }
617}
618
999633ed 619int
f3e9a817
PM
620gdbpy_initialize_symbols (void)
621{
622 if (PyType_Ready (&symbol_object_type) < 0)
999633ed 623 return -1;
f3e9a817
PM
624
625 /* Register an objfile "free" callback so we can properly
626 invalidate symbol when an object file that is about to be
627 deleted. */
628 sympy_objfile_data_key
629 = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
630
999633ed
TT
631 if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
632 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
633 LOC_CONST) < 0
634 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC",
635 LOC_STATIC) < 0
636 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER",
637 LOC_REGISTER) < 0
638 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG",
639 LOC_ARG) < 0
640 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG",
641 LOC_REF_ARG) < 0
642 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL",
643 LOC_LOCAL) < 0
644 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF",
645 LOC_TYPEDEF) < 0
646 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL",
647 LOC_LABEL) < 0
648 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK",
649 LOC_BLOCK) < 0
650 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
651 LOC_CONST_BYTES) < 0
652 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
653 LOC_UNRESOLVED) < 0
654 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
655 LOC_OPTIMIZED_OUT) < 0
656 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED",
657 LOC_COMPUTED) < 0
51e78fc5
TT
658 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMMON_BLOCK",
659 LOC_COMMON_BLOCK) < 0
999633ed
TT
660 || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
661 LOC_REGPARM_ADDR) < 0
662 || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN",
663 UNDEF_DOMAIN) < 0
664 || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN",
665 VAR_DOMAIN) < 0
666 || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN",
667 STRUCT_DOMAIN) < 0
51e78fc5
TT
668 || PyModule_AddIntConstant (gdb_module, "SYMBOL_MODULE_DOMAIN",
669 MODULE_DOMAIN) < 0
670 || PyModule_AddIntConstant (gdb_module, "SYMBOL_COMMON_BLOCK_DOMAIN",
671 COMMON_BLOCK_DOMAIN) < 0)
672 return -1;
673
674 /* These remain defined for compatibility, but as they were never
675 correct, they are no longer documented. Eventually we can remove
676 them. These exist because at one time, enum search_domain and
677 enum domain_enum_tag were combined -- but different values were
678 used differently. Here we try to give them values that will make
679 sense if they are passed to gdb.lookup_symbol. */
680 if (PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
681 VAR_DOMAIN) < 0
999633ed 682 || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
51e78fc5 683 VAR_DOMAIN) < 0
999633ed 684 || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN",
51e78fc5 685 VAR_DOMAIN) < 0)
999633ed 686 return -1;
f3e9a817 687
aa36459a
TT
688 return gdb_pymodule_addobject (gdb_module, "Symbol",
689 (PyObject *) &symbol_object_type);
f3e9a817
PM
690}
691
692\f
693
0d1f4ceb 694static gdb_PyGetSetDef symbol_object_getset[] = {
457e09f0
DE
695 { "type", sympy_get_type, NULL,
696 "Type of the symbol.", NULL },
f3e9a817
PM
697 { "symtab", sympy_get_symtab, NULL,
698 "Symbol table in which the symbol appears.", NULL },
699 { "name", sympy_get_name, NULL,
700 "Name of the symbol, as it appears in the source code.", NULL },
701 { "linkage_name", sympy_get_linkage_name, NULL,
9a2b4c1b
MS
702 "Name of the symbol, as used by the linker (i.e., may be mangled).",
703 NULL },
f3e9a817
PM
704 { "print_name", sympy_get_print_name, NULL,
705 "Name of the symbol in a form suitable for output.\n\
706This is either name or linkage_name, depending on whether the user asked GDB\n\
707to display demangled or mangled names.", NULL },
708 { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
709 { "is_argument", sympy_is_argument, NULL,
710 "True if the symbol is an argument of a function." },
711 { "is_constant", sympy_is_constant, NULL,
712 "True if the symbol is a constant." },
713 { "is_function", sympy_is_function, NULL,
714 "True if the symbol is a function or method." },
715 { "is_variable", sympy_is_variable, NULL,
716 "True if the symbol is a variable." },
f0823d2c
TT
717 { "needs_frame", sympy_needs_frame, NULL,
718 "True if the symbol requires a frame for evaluation." },
64e7d9dd
TT
719 { "line", sympy_line, NULL,
720 "The source line number at which the symbol was defined." },
f3e9a817
PM
721 { NULL } /* Sentinel */
722};
723
29703da4
PM
724static PyMethodDef symbol_object_methods[] = {
725 { "is_valid", sympy_is_valid, METH_NOARGS,
726 "is_valid () -> Boolean.\n\
727Return true if this symbol is valid, false if not." },
f0823d2c
TT
728 { "value", sympy_value, METH_VARARGS,
729 "value ([frame]) -> gdb.Value\n\
730Return the value of the symbol." },
29703da4
PM
731 {NULL} /* Sentinel */
732};
733
f3e9a817 734PyTypeObject symbol_object_type = {
9a27f2c6 735 PyVarObject_HEAD_INIT (NULL, 0)
f3e9a817
PM
736 "gdb.Symbol", /*tp_name*/
737 sizeof (symbol_object), /*tp_basicsize*/
738 0, /*tp_itemsize*/
739 sympy_dealloc, /*tp_dealloc*/
740 0, /*tp_print*/
741 0, /*tp_getattr*/
742 0, /*tp_setattr*/
743 0, /*tp_compare*/
744 0, /*tp_repr*/
745 0, /*tp_as_number*/
746 0, /*tp_as_sequence*/
747 0, /*tp_as_mapping*/
748 0, /*tp_hash */
749 0, /*tp_call*/
750 sympy_str, /*tp_str*/
751 0, /*tp_getattro*/
752 0, /*tp_setattro*/
753 0, /*tp_as_buffer*/
754 Py_TPFLAGS_DEFAULT, /*tp_flags*/
755 "GDB symbol object", /*tp_doc */
756 0, /*tp_traverse */
757 0, /*tp_clear */
758 0, /*tp_richcompare */
759 0, /*tp_weaklistoffset */
760 0, /*tp_iter */
761 0, /*tp_iternext */
29703da4 762 symbol_object_methods, /*tp_methods */
f3e9a817
PM
763 0, /*tp_members */
764 symbol_object_getset /*tp_getset */
765};
This page took 1.275371 seconds and 4 git commands to generate.