gdb - Local mods (compile)
[dragonfly.git] / contrib / gdb-7 / gdb / guile / lib / gdb / types.scm
1 ;; Type utilities.
2 ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 ;;
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.
8 ;;
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.
13 ;;
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/>.
16
17 (define-module (gdb types)
18   #:use-module (gdb)
19   #:use-module (gdb iterator)
20   #:use-module (gdb support))
21
22 (define-public (type-has-field-deep? type field-name)
23   "Return #t if the type, including baseclasses, has the specified field.
24
25   Arguments:
26     type: The type to examine.  It must be a struct or union.
27     field-name: The name of the field to look up.
28
29   Returns:
30     True if the field is present either in type_ or any baseclass.
31
32   Raises:
33     wrong-type-arg: The type is not a struct or union."
34
35   (define (search-class type)
36     (let ((find-in-baseclass (lambda (field)
37                                (if (field-baseclass? field)
38                                    (search-class (field-type field))
39                                    ;; Not a baseclass, search ends now.
40                                    ;; Return #:end to end search.
41                                    #:end))))
42       (let ((search-baseclasses
43              (lambda (type)
44                (iterator-until find-in-baseclass
45                                (make-field-iterator type)))))
46         (or (type-has-field? type field-name)
47             (not (eq? (search-baseclasses type) #:end))))))
48
49   (if (= (type-code type) TYPE_CODE_REF)
50       (set! type (type-target type)))
51   (set! type (type-strip-typedefs type))
52
53   (assert-type (memq (type-code type) (list TYPE_CODE_STRUCT TYPE_CODE_UNION))
54                type SCM_ARG1 'type-has-field-deep? "struct or union")
55
56   (search-class type))
57
58 (define-public (make-enum-hashtable enum-type)
59   "Return a hash table from a program's enum type.
60
61   Elements in the hash table are fetched with hashq-ref.
62
63   Arguments:
64     enum-type: The enum to compute the hash table for.
65
66   Returns:
67     The hash table of the enum.
68
69   Raises:
70     wrong-type-arg: The type is not an enum."
71
72   (assert-type (= (type-code enum-type) TYPE_CODE_ENUM)
73                enum-type SCM_ARG1 'make-enum-hashtable "enum")
74   (let ((htab (make-hash-table)))
75     (for-each (lambda (enum)
76                 (hash-set! htab (field-name enum) (field-enumval enum)))
77               (type-fields enum-type))
78     htab))