2 # Copyright (C) 2010-2012 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 """Utilities for working with gdb.Types."""
22 def get_basic_type(type_
):
23 """Return the "basic" type of a type.
26 type_: The type to reduce to its basic type.
29 type_ with const/volatile is stripped away,
30 and typedefs/references converted to the underlying type.
33 while (type_
.code
== gdb
.TYPE_CODE_REF
or
34 type_
.code
== gdb
.TYPE_CODE_TYPEDEF
):
35 if type_
.code
== gdb
.TYPE_CODE_REF
:
36 type_
= type_
.target()
38 type_
= type_
.strip_typedefs()
39 return type_
.unqualified()
42 def has_field(type_
, field
):
43 """Return True if a type has the specified field.
46 type_: The type to examine.
47 It must be one of gdb.TYPE_CODE_STRUCT, gdb.TYPE_CODE_UNION.
48 field: The name of the field to look up.
51 True if the field is present either in type_ or any baseclass.
54 TypeError: The type is not a struct or union.
57 type_
= get_basic_type(type_
)
58 if (type_
.code
!= gdb
.TYPE_CODE_STRUCT
and
59 type_
.code
!= gdb
.TYPE_CODE_UNION
):
60 raise TypeError("not a struct or union")
61 for f
in type_
.fields():
63 if has_field(f
.type, field
):
66 # NOTE: f.name could be None
72 def make_enum_dict(enum_type
):
73 """Return a dictionary from a program's enum type.
76 enum_type: The enum to compute the dictionary for.
79 The dictionary of the enum.
82 TypeError: The type is not an enum.
85 if enum_type
.code
!= gdb
.TYPE_CODE_ENUM
:
86 raise TypeError("not an enum type")
88 for field
in enum_type
.fields():
89 # The enum's value is stored in "enumval".
90 enum_dict
[field
.name
] = field
.enumval
94 def deep_items (type_
):
95 """Return an iterator that recursively traverses anonymous fields.
98 type_: The type to traverse. It should be one of
99 gdb.TYPE_CODE_STRUCT or gdb.TYPE_CODE_UNION.
102 an iterator similar to gdb.Type.iteritems(), i.e., it returns
103 pairs of key, value, but for any anonymous struct or union
104 field that field is traversed recursively, depth-first.
106 for k
, v
in type_
.iteritems ():
110 for i
in deep_items (v
.type):
This page took 0.040025 seconds and 4 git commands to generate.