types.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Declarations for the type system interface.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_TYPES__
  14. #define __KP_TYPES__ 1
  15. #include "interp.hpp"
  16. #include "array.hpp"
  17. KP_DECLS_BEGIN
  18. struct instance : public finobj
  19. {
  20. static const uint32_t init_flag = 1u << 20;
  21. static const uint32_t ishared_flag = 1u << 21;
  22. static const int code = typecode::INSTANCE;
  23. object ptype;
  24. object slots;
  25. object tspec; // for type descriptor metatypes.
  26. object builtin;
  27. struct slotdef_iter
  28. {
  29. valref slotdefs;
  30. uint32_t idx;
  31. object bt_room[6];
  32. local_varobj<array> bt_mem;
  33. valref builtin;
  34. slotdef_iter (interpreter *interp, object inst);
  35. slotdef_iter (interpreter *interp, const slotdef_iter& right) :
  36. slotdefs (interp, *right.slotdefs), idx (right.idx),
  37. builtin (interp, UNBOUND)
  38. {
  39. if (*right.builtin == UNBOUND)
  40. return;
  41. copy_objs (this->bt_room, right.bt_room, KP_NELEM (this->bt_room));
  42. this->bt_mem.local_init (this->bt_room, KP_NELEM (this->bt_room));
  43. *this->builtin = this->bt_mem.as_obj ();
  44. }
  45. slotdef_iter& operator++ ();
  46. slotdef_iter operator++ (int);
  47. object operator* () const;
  48. bool valid ()
  49. {
  50. return (*this->slotdefs != UNBOUND || *this->builtin != UNBOUND);
  51. }
  52. };
  53. unsigned int type_code () const;
  54. };
  55. inline instance* as_instance (object obj)
  56. {
  57. return ((instance *)unmask (obj));
  58. }
  59. inline bool instance_p (object obj)
  60. {
  61. return (itype (obj) == typecode::INSTANCE);
  62. }
  63. inline bool typespec_p (object obj)
  64. {
  65. return (instance_p (obj) && as_instance(obj)->tspec != UNBOUND);
  66. }
  67. inline bool builtin_typespec_p (object obj)
  68. {
  69. return (typespec_p (obj) &&
  70. itype (as_instance(obj)->slots) == typecode::INT);
  71. }
  72. struct stream;
  73. struct io_info;
  74. struct pack_info;
  75. // Return the type of OBJ.
  76. KP_EXPORT object type (object obj);
  77. // Create a new typespec from NAME, PARENTS and SLOTDEFS.
  78. KP_EXPORT result<object> type (interpreter *interp, object name,
  79. object parents, object slotdefs);
  80. // Return the type-name of OBJ as a symbol.
  81. KP_EXPORT object type_name (object obj);
  82. // Return the inherited builtin member in OBJ.
  83. KP_EXPORT object builtin_member (object obj);
  84. // Allocate an instance from a typespec and arguments.
  85. KP_EXPORT result<object> alloc_inst (interpreter *interp, object xtype,
  86. object *argv, int argc);
  87. // Get the value for slot NAME in instance INST.
  88. KP_EXPORT result<object> get_w (interpreter *interp, object inst,
  89. object name, object dfl);
  90. // Set the value for slot NAME in instance INST to VAL.
  91. KP_EXPORT result<object> nput_w (interpreter *interp, object inst,
  92. object name, object va);
  93. // Test if OBJ is of type TYPE.
  94. KP_EXPORT int instanceof (object obj, object type);
  95. // Test if T1 is a subtype of T2.
  96. KP_EXPORT int subtype_p (object t1, object t2);
  97. // Mutate a slot in an instance.
  98. KP_EXPORT result<object> nzap_w (interpreter *interp, object inst, object name,
  99. uint32_t flags, object fn,
  100. object *argv, int argc);
  101. // Write an instance to a stream.
  102. KP_EXPORT result<int64_t> write_w (interpreter *interp,
  103. stream *strm, object obj, io_info& info);
  104. // Serialize an instance in a stream.
  105. KP_EXPORT result<int64_t> pack_w (interpreter *interp,
  106. stream *strm, object obj, pack_info& info);
  107. // Deserialize an instance from a stream.
  108. KP_EXPORT result<object> unpack_w (interpreter *interp,
  109. stream *strm, pack_info& info, bool save);
  110. // Return the typespec for a builtin typecode.
  111. KP_EXPORT object builtin_type (unsigned int code);
  112. // Init OP for types.
  113. KP_EXPORT init_op init_types;
  114. KP_DECLS_END
  115. #endif