go-type.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* go-type.h -- basic information for a Go type.
  2. Copyright 2009 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #ifndef LIBGO_GO_TYPE_H
  6. #define LIBGO_GO_TYPE_H
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include "array.h"
  10. struct String;
  11. /* Many of the types in this file must match the data structures
  12. generated by the compiler, and must also match the Go types which
  13. appear in go/runtime/type.go and go/reflect/type.go. */
  14. /* Type kinds. These are used to get the type descriptor to use for
  15. the type itself, when using unsafe.Typeof or unsafe.Reflect. The
  16. values here must match the values generated by the compiler (the
  17. RUNTIME_TYPE_KIND_xxx values in gcc/go/types.h). These are macros
  18. rather than an enum to make it easy to change values in the future
  19. and hard to get confused about it.
  20. These correspond to the kind values used by the gc compiler. */
  21. #define GO_BOOL 1
  22. #define GO_INT 2
  23. #define GO_INT8 3
  24. #define GO_INT16 4
  25. #define GO_INT32 5
  26. #define GO_INT64 6
  27. #define GO_UINT 7
  28. #define GO_UINT8 8
  29. #define GO_UINT16 9
  30. #define GO_UINT32 10
  31. #define GO_UINT64 11
  32. #define GO_UINTPTR 12
  33. #define GO_FLOAT32 13
  34. #define GO_FLOAT64 14
  35. #define GO_COMPLEX64 15
  36. #define GO_COMPLEX128 16
  37. #define GO_ARRAY 17
  38. #define GO_CHAN 18
  39. #define GO_FUNC 19
  40. #define GO_INTERFACE 20
  41. #define GO_MAP 21
  42. #define GO_PTR 22
  43. #define GO_SLICE 23
  44. #define GO_STRING 24
  45. #define GO_STRUCT 25
  46. #define GO_UNSAFE_POINTER 26
  47. #define GO_DIRECT_IFACE (1 << 5)
  48. #define GO_GC_PROG (1 << 6)
  49. #define GO_NO_POINTERS (1 << 7)
  50. #define GO_CODE_MASK 0x1f
  51. /* For each Go type the compiler constructs one of these structures.
  52. This is used for type reflection, interfaces, maps, and reference
  53. counting. */
  54. struct __go_type_descriptor
  55. {
  56. /* The type code for this type, one of the type kind values above.
  57. This is used by unsafe.Reflect and unsafe.Typeof to determine the
  58. type descriptor to return for this type itself. It is also used
  59. by reflect.toType when mapping to a reflect Type structure. */
  60. unsigned char __code;
  61. /* The alignment in bytes of a variable with this type. */
  62. unsigned char __align;
  63. /* The alignment in bytes of a struct field with this type. */
  64. unsigned char __field_align;
  65. /* The size in bytes of a value of this type. Note that all types
  66. in Go have a fixed size. */
  67. uintptr_t __size;
  68. /* The type's hash code. */
  69. uint32_t __hash;
  70. /* This function takes a pointer to a value of this type, and the
  71. size of this type, and returns a hash code. We pass the size
  72. explicitly becaues it means that we can share a single instance
  73. of this function for various different types. */
  74. uintptr_t (*__hashfn) (const void *, uintptr_t);
  75. /* This function takes two pointers to values of this type, and the
  76. size of this type, and returns whether the values are equal. */
  77. _Bool (*__equalfn) (const void *, const void *, uintptr_t);
  78. /* The garbage collection data. */
  79. const uintptr *__gc;
  80. /* A string describing this type. This is only used for
  81. debugging. */
  82. const struct String *__reflection;
  83. /* A pointer to fields which are only used for some types. */
  84. const struct __go_uncommon_type *__uncommon;
  85. /* The descriptor for the type which is a pointer to this type.
  86. This may be NULL. */
  87. const struct __go_type_descriptor *__pointer_to_this;
  88. /* A pointer to a zero value for this type. All types will point to
  89. the same zero value, go$zerovalue, which is a common variable so
  90. that it will be large enough. */
  91. void *__zero;
  92. };
  93. /* The information we store for each method of a type. */
  94. struct __go_method
  95. {
  96. /* The name of the method. */
  97. const struct String *__name;
  98. /* This is NULL for an exported method, or the name of the package
  99. where it lives. */
  100. const struct String *__pkg_path;
  101. /* The type of the method, without the receiver. This will be a
  102. function type. */
  103. const struct __go_type_descriptor *__mtype;
  104. /* The type of the method, with the receiver. This will be a
  105. function type. */
  106. const struct __go_type_descriptor *__type;
  107. /* A pointer to the code which implements the method. This is
  108. really a function pointer. */
  109. const void *__function;
  110. };
  111. /* Additional information that we keep for named types and for types
  112. with methods. */
  113. struct __go_uncommon_type
  114. {
  115. /* The name of the type. */
  116. const struct String *__name;
  117. /* The type's package. This is NULL for builtin types. */
  118. const struct String *__pkg_path;
  119. /* The type's methods. This is an array of struct __go_method. */
  120. struct __go_open_array __methods;
  121. };
  122. /* The type descriptor for a fixed array type. */
  123. struct __go_array_type
  124. {
  125. /* Starts like all type descriptors. */
  126. struct __go_type_descriptor __common;
  127. /* The element type. */
  128. struct __go_type_descriptor *__element_type;
  129. /* The type of a slice of the same element type. */
  130. struct __go_type_descriptor *__slice_type;
  131. /* The length of the array. */
  132. uintptr_t __len;
  133. };
  134. /* The type descriptor for a slice. */
  135. struct __go_slice_type
  136. {
  137. /* Starts like all other type descriptors. */
  138. struct __go_type_descriptor __common;
  139. /* The element type. */
  140. struct __go_type_descriptor *__element_type;
  141. };
  142. /* The direction of a channel. */
  143. #define CHANNEL_RECV_DIR 1
  144. #define CHANNEL_SEND_DIR 2
  145. #define CHANNEL_BOTH_DIR (CHANNEL_RECV_DIR | CHANNEL_SEND_DIR)
  146. /* The type descriptor for a channel. */
  147. struct __go_channel_type
  148. {
  149. /* Starts like all other type descriptors. */
  150. struct __go_type_descriptor __common;
  151. /* The element type. */
  152. const struct __go_type_descriptor *__element_type;
  153. /* The direction. */
  154. uintptr_t __dir;
  155. };
  156. /* The type descriptor for a function. */
  157. struct __go_func_type
  158. {
  159. /* Starts like all other type descriptors. */
  160. struct __go_type_descriptor __common;
  161. /* Whether this is a varargs function. If this is true, there will
  162. be at least one parameter. For "..." the last parameter type is
  163. "interface{}". For "... T" the last parameter type is "[]T". */
  164. _Bool __dotdotdot;
  165. /* The input parameter types. This is an array of pointers to
  166. struct __go_type_descriptor. */
  167. struct __go_open_array __in;
  168. /* The output parameter types. This is an array of pointers to
  169. struct __go_type_descriptor. */
  170. struct __go_open_array __out;
  171. };
  172. /* A method on an interface type. */
  173. struct __go_interface_method
  174. {
  175. /* The name of the method. */
  176. const struct String *__name;
  177. /* This is NULL for an exported method, or the name of the package
  178. where it lives. */
  179. const struct String *__pkg_path;
  180. /* The real type of the method. */
  181. struct __go_type_descriptor *__type;
  182. };
  183. /* An interface type. */
  184. struct __go_interface_type
  185. {
  186. /* Starts like all other type descriptors. */
  187. struct __go_type_descriptor __common;
  188. /* Array of __go_interface_method . The methods are sorted in the
  189. same order that they appear in the definition of the
  190. interface. */
  191. struct __go_open_array __methods;
  192. };
  193. /* A map type. */
  194. struct __go_map_type
  195. {
  196. /* Starts like all other type descriptors. */
  197. struct __go_type_descriptor __common;
  198. /* The map key type. */
  199. const struct __go_type_descriptor *__key_type;
  200. /* The map value type. */
  201. const struct __go_type_descriptor *__val_type;
  202. };
  203. /* A pointer type. */
  204. struct __go_ptr_type
  205. {
  206. /* Starts like all other type descriptors. */
  207. struct __go_type_descriptor __common;
  208. /* The type to which this points. */
  209. const struct __go_type_descriptor *__element_type;
  210. };
  211. /* A field in a structure. */
  212. struct __go_struct_field
  213. {
  214. /* The name of the field--NULL for an anonymous field. */
  215. const struct String *__name;
  216. /* This is NULL for an exported method, or the name of the package
  217. where it lives. */
  218. const struct String *__pkg_path;
  219. /* The type of the field. */
  220. const struct __go_type_descriptor *__type;
  221. /* The field tag, or NULL. */
  222. const struct String *__tag;
  223. /* The offset of the field in the struct. */
  224. uintptr_t __offset;
  225. };
  226. /* A struct type. */
  227. struct __go_struct_type
  228. {
  229. /* Starts like all other type descriptors. */
  230. struct __go_type_descriptor __common;
  231. /* An array of struct __go_struct_field. */
  232. struct __go_open_array __fields;
  233. };
  234. /* Whether a type descriptor is a pointer. */
  235. static inline _Bool
  236. __go_is_pointer_type (const struct __go_type_descriptor *td)
  237. {
  238. return ((td->__code & GO_CODE_MASK) == GO_PTR
  239. || (td->__code & GO_CODE_MASK) == GO_UNSAFE_POINTER);
  240. }
  241. extern _Bool
  242. __go_type_descriptors_equal(const struct __go_type_descriptor*,
  243. const struct __go_type_descriptor*);
  244. extern uintptr_t __go_type_hash_identity (const void *, uintptr_t);
  245. extern _Bool __go_type_equal_identity (const void *, const void *, uintptr_t);
  246. extern uintptr_t __go_type_hash_string (const void *, uintptr_t);
  247. extern _Bool __go_type_equal_string (const void *, const void *, uintptr_t);
  248. extern uintptr_t __go_type_hash_float (const void *, uintptr_t);
  249. extern _Bool __go_type_equal_float (const void *, const void *, uintptr_t);
  250. extern uintptr_t __go_type_hash_complex (const void *, uintptr_t);
  251. extern _Bool __go_type_equal_complex (const void *, const void *, uintptr_t);
  252. extern uintptr_t __go_type_hash_interface (const void *, uintptr_t);
  253. extern _Bool __go_type_equal_interface (const void *, const void *, uintptr_t);
  254. extern uintptr_t __go_type_hash_error (const void *, uintptr_t);
  255. extern _Bool __go_type_equal_error (const void *, const void *, uintptr_t);
  256. #endif /* !defined(LIBGO_GO_TYPE_H) */