summaryrefslogtreecommitdiff
path: root/gdb/guile/lib/gdb/types.scm
blob: 1339ff02e5d3e550f3799ebe62e29eb8fb2d0b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
;; Type utilities.
;; Copyright (C) 2010-2019 Free Software Foundation, Inc.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gdb types)
  #:use-module (gdb)
  #:use-module (gdb iterator)
  #:use-module (gdb support))

(define-public (type-has-field-deep? type field-name)
  "Return #t if the type, including baseclasses, has the specified field.

  Arguments:
    type: The type to examine.  It must be a struct or union.
    field-name: The name of the field to look up.

  Returns:
    True if the field is present either in type_ or any baseclass.

  Raises:
    wrong-type-arg: The type is not a struct or union."

  (define (search-class type)
    (let ((find-in-baseclass (lambda (field)
			       (if (field-baseclass? field)
				   (search-class (field-type field))
				   ;; Not a baseclass, search ends now.
				   ;; Return #:end to end search.
				   #:end))))
      (let ((search-baseclasses
	     (lambda (type)
	       (iterator-until find-in-baseclass
			       (make-field-iterator type)))))
	(or (type-has-field? type field-name)
	    (not (eq? (search-baseclasses type) #:end))))))

  (if (= (type-code type) TYPE_CODE_REF)
      (set! type (type-target type)))
  (set! type (type-strip-typedefs type))

  (assert-type (memq (type-code type) (list TYPE_CODE_STRUCT TYPE_CODE_UNION))
	       type SCM_ARG1 'type-has-field-deep? "struct or union")

  (search-class type))

(define-public (make-enum-hashtable enum-type)
  "Return a hash table from a program's enum type.

  Elements in the hash table are fetched with hashq-ref.

  Arguments:
    enum-type: The enum to compute the hash table for.

  Returns:
    The hash table of the enum.

  Raises:
    wrong-type-arg: The type is not an enum."

  (assert-type (= (type-code enum-type) TYPE_CODE_ENUM)
	       enum-type SCM_ARG1 'make-enum-hashtable "enum")
  (let ((htab (make-hash-table)))
    (for-each (lambda (enum)
		(hash-set! htab (field-name enum) (field-enumval enum)))
	      (type-fields enum-type))
    htab))