inspection.scm 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;;; inspection.scm --- Inspection support for R6RS records
  2. ;; Copyright (C) 2010 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library 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 GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (library (rnrs records inspection (6))
  18. (export record?
  19. record-rtd
  20. record-type-name
  21. record-type-parent
  22. record-type-uid
  23. record-type-generative?
  24. record-type-sealed?
  25. record-type-opaque?
  26. record-type-field-names
  27. record-field-mutable?)
  28. (import (rnrs arithmetic bitwise (6))
  29. (rnrs base (6))
  30. (rnrs records procedural (6))
  31. (only (guile) struct-ref struct-vtable vtable-index-layout @@))
  32. (define record-internal? (@@ (rnrs records procedural) record-internal?))
  33. (define rtd-index-name (@@ (rnrs records procedural) rtd-index-name))
  34. (define rtd-index-parent (@@ (rnrs records procedural) rtd-index-parent))
  35. (define rtd-index-uid (@@ (rnrs records procedural) rtd-index-uid))
  36. (define rtd-index-sealed? (@@ (rnrs records procedural) rtd-index-sealed?))
  37. (define rtd-index-opaque? (@@ (rnrs records procedural) rtd-index-opaque?))
  38. (define rtd-index-field-names
  39. (@@ (rnrs records procedural) rtd-index-field-names))
  40. (define rtd-index-field-bit-field
  41. (@@ (rnrs records procedural) rtd-index-field-bit-field))
  42. (define (record? obj)
  43. (and (record-internal? obj)
  44. (not (record-type-opaque? (struct-vtable obj)))))
  45. (define (record-rtd record)
  46. (or (and (record-internal? record)
  47. (let ((rtd (struct-vtable record)))
  48. (and (not (struct-ref rtd rtd-index-opaque?)) rtd)))
  49. (assertion-violation 'record-rtd "not a record" record)))
  50. (define (guarantee-rtd who rtd)
  51. (if (record-type-descriptor? rtd)
  52. rtd
  53. (assertion-violation who "not a record type descriptor" rtd)))
  54. (define (record-type-name rtd)
  55. (struct-ref (guarantee-rtd 'record-type-name rtd) rtd-index-name))
  56. (define (record-type-parent rtd)
  57. (struct-ref (guarantee-rtd 'record-type-parent rtd) rtd-index-parent))
  58. (define (record-type-uid rtd)
  59. (struct-ref (guarantee-rtd 'record-type-uid rtd) rtd-index-uid))
  60. (define (record-type-generative? rtd)
  61. (not (record-type-uid (guarantee-rtd 'record-type-generative? rtd))))
  62. (define (record-type-sealed? rtd)
  63. (struct-ref (guarantee-rtd 'record-type-sealed? rtd) rtd-index-sealed?))
  64. (define (record-type-opaque? rtd)
  65. (struct-ref (guarantee-rtd 'record-type-opaque? rtd) rtd-index-opaque?))
  66. (define (record-type-field-names rtd)
  67. (struct-ref (guarantee-rtd 'record-type-field-names rtd) rtd-index-field-names))
  68. (define (record-field-mutable? rtd k)
  69. (bitwise-bit-set? (struct-ref (guarantee-rtd 'record-field-mutable? rtd)
  70. rtd-index-field-bit-field)
  71. k))
  72. )