1 /* go-type.h -- basic information for a Go type.
2 
3    Copyright 2009 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6 
7 #ifndef LIBGO_GO_TYPE_H
8 #define LIBGO_GO_TYPE_H
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "array.h"
14 
15 struct String;
16 
17 /* Many of the types in this file must match the data structures
18    generated by the compiler, and must also match the Go types which
19    appear in go/runtime/type.go and go/reflect/type.go.  */
20 
21 /* Type kinds.  These are used to get the type descriptor to use for
22    the type itself, when using unsafe.Typeof or unsafe.Reflect.  The
23    values here must match the values generated by the compiler (the
24    RUNTIME_TYPE_KIND_xxx values in gcc/go/types.h).  These are macros
25    rather than an enum to make it easy to change values in the future
26    and hard to get confused about it.
27 
28    These correspond to the kind values used by the gc compiler.  */
29 
30 #define GO_BOOL 1
31 #define GO_INT 2
32 #define GO_INT8 3
33 #define GO_INT16 4
34 #define GO_INT32 5
35 #define GO_INT64 6
36 #define GO_UINT 7
37 #define GO_UINT8 8
38 #define GO_UINT16 9
39 #define GO_UINT32 10
40 #define GO_UINT64 11
41 #define GO_UINTPTR 12
42 #define GO_FLOAT32 13
43 #define GO_FLOAT64 14
44 #define GO_COMPLEX64 15
45 #define GO_COMPLEX128 16
46 #define GO_ARRAY 17
47 #define GO_CHAN 18
48 #define GO_FUNC 19
49 #define GO_INTERFACE 20
50 #define GO_MAP 21
51 #define GO_PTR 22
52 #define GO_SLICE 23
53 #define GO_STRING 24
54 #define GO_STRUCT 25
55 #define GO_UNSAFE_POINTER 26
56 
57 #define GO_DIRECT_IFACE (1 << 5)
58 #define GO_GC_PROG (1 << 6)
59 #define GO_NO_POINTERS (1 << 7)
60 
61 #define GO_CODE_MASK 0x1f
62 
63 /* For each Go type the compiler constructs one of these structures.
64    This is used for type reflection, interfaces, maps, and reference
65    counting.  */
66 
67 struct __go_type_descriptor
68 {
69   /* The size in bytes of a value of this type.  Note that all types
70      in Go have a fixed size.  */
71   uintptr_t __size;
72 
73   /* The size of the memory prefix of a value of this type that holds
74      all pointers.  */
75   uintptr_t __ptrdata;
76 
77   /* The type's hash code.  */
78   uint32_t __hash;
79 
80   /* The type code for this type, one of the type kind values above.
81      This is used by unsafe.Reflect and unsafe.Typeof to determine the
82      type descriptor to return for this type itself.  It is also used
83      by reflect.toType when mapping to a reflect Type structure.  */
84   unsigned char __code;
85 
86   /* The alignment in bytes of a variable with this type.  */
87   unsigned char __align;
88 
89   /* The alignment in bytes of a struct field with this type.  */
90   unsigned char __field_align;
91 
92   /* This function takes a pointer to a value of this type, and the
93      size of this type, and returns a hash code.  We pass the size
94      explicitly becaues it means that we can share a single instance
95      of this function for various different types.  */
96   const FuncVal *__hashfn;
97 
98   /* This function takes two pointers to values of this type, and the
99      size of this type, and returns whether the values are equal.  */
100   const FuncVal *__equalfn;
101 
102   /* The garbage collection data. */
103   const byte *__gcdata;
104 
105   /* A string describing this type.  This is only used for
106      debugging.  */
107   const struct String *__reflection;
108 
109   /* A pointer to fields which are only used for some types.  */
110   const struct __go_uncommon_type *__uncommon;
111 
112   /* The descriptor for the type which is a pointer to this type.
113      This may be NULL.  */
114   const struct __go_type_descriptor *__pointer_to_this;
115 };
116 
117 /* The information we store for each method of a type.  */
118 
119 struct __go_method
120 {
121   /* The name of the method.  */
122   const struct String *__name;
123 
124   /* This is NULL for an exported method, or the name of the package
125      where it lives.  */
126   const struct String *__pkg_path;
127 
128   /* The type of the method, without the receiver.  This will be a
129      function type.  */
130   const struct __go_type_descriptor *__mtype;
131 
132   /* The type of the method, with the receiver.  This will be a
133      function type.  */
134   const struct __go_type_descriptor *__type;
135 
136   /* A pointer to the code which implements the method.  This is
137      really a function pointer.  */
138   const void *__function;
139 };
140 
141 /* Additional information that we keep for named types and for types
142    with methods.  */
143 
144 struct __go_uncommon_type
145 {
146   /* The name of the type.  */
147   const struct String *__name;
148 
149   /* The type's package.  This is NULL for builtin types.  */
150   const struct String *__pkg_path;
151 
152   /* The type's methods.  This is an array of struct __go_method.  */
153   struct __go_open_array __methods;
154 };
155 
156 /* The type descriptor for a fixed array type.  */
157 
158 struct __go_array_type
159 {
160   /* Starts like all type descriptors.  */
161   struct __go_type_descriptor __common;
162 
163   /* The element type.  */
164   struct __go_type_descriptor *__element_type;
165 
166   /* The type of a slice of the same element type.  */
167   struct __go_type_descriptor *__slice_type;
168 
169   /* The length of the array.  */
170   uintptr_t __len;
171 };
172 
173 /* The type descriptor for a slice.  */
174 
175 struct __go_slice_type
176 {
177   /* Starts like all other type descriptors.  */
178   struct __go_type_descriptor __common;
179 
180   /* The element type.  */
181   struct __go_type_descriptor *__element_type;
182 };
183 
184 /* The direction of a channel.  */
185 #define CHANNEL_RECV_DIR 1
186 #define CHANNEL_SEND_DIR 2
187 #define CHANNEL_BOTH_DIR (CHANNEL_RECV_DIR | CHANNEL_SEND_DIR)
188 
189 /* The type descriptor for a channel.  */
190 
191 struct __go_channel_type
192 {
193   /* Starts like all other type descriptors.  */
194   struct __go_type_descriptor __common;
195 
196   /* The element type.  */
197   const struct __go_type_descriptor *__element_type;
198 
199   /* The direction.  */
200   uintptr_t __dir;
201 };
202 
203 /* The type descriptor for a function.  */
204 
205 struct __go_func_type
206 {
207   /* Starts like all other type descriptors.  */
208   struct __go_type_descriptor __common;
209 
210   /* Whether this is a varargs function.  If this is true, there will
211      be at least one parameter.  For "..." the last parameter type is
212      "interface{}".  For "... T" the last parameter type is "[]T".  */
213   _Bool __dotdotdot;
214 
215   /* The input parameter types.  This is an array of pointers to
216      struct __go_type_descriptor.  */
217   struct __go_open_array __in;
218 
219   /* The output parameter types.  This is an array of pointers to
220      struct __go_type_descriptor.  */
221   struct __go_open_array __out;
222 };
223 
224 /* A method on an interface type.  */
225 
226 struct __go_interface_method
227 {
228   /* The name of the method.  */
229   const struct String *__name;
230 
231   /* This is NULL for an exported method, or the name of the package
232      where it lives.  */
233   const struct String *__pkg_path;
234 
235   /* The real type of the method.  */
236   struct __go_type_descriptor *__type;
237 };
238 
239 /* An interface type.  */
240 
241 struct __go_interface_type
242 {
243   /* Starts like all other type descriptors.  */
244   struct __go_type_descriptor __common;
245 
246   /* Array of __go_interface_method .  The methods are sorted in the
247      same order that they appear in the definition of the
248      interface.  */
249   struct __go_open_array __methods;
250 };
251 
252 /* A map type.  */
253 
254 struct __go_map_type
255 {
256   /* Starts like all other type descriptors.  */
257   struct __go_type_descriptor __common;
258 
259   /* The map key type.  */
260   const struct __go_type_descriptor *__key_type;
261 
262   /* The map value type.  */
263   const struct __go_type_descriptor *__val_type;
264 
265   /* The map bucket type.  */
266   const struct __go_type_descriptor *__bucket_type;
267 
268   /* The map header type.  */
269   const struct __go_type_descriptor *__hmap_type;
270 
271   /* The size of the key slot.  */
272   uint8_t __key_size;
273 
274   /* Whether to store a pointer to key rather than the key itself.  */
275   uint8_t __indirect_key;
276 
277   /* The size of the value slot.  */
278   uint8_t __value_size;
279 
280   /* Whether to store a pointer to value rather than the value itself.  */
281   uint8_t __indirect_value;
282 
283   /* The size of a bucket.  */
284   uint16_t __bucket_size;
285 
286   /* Whether the key type is reflexive--whether k==k for all keys.  */
287   _Bool __reflexive_key;
288 
289   /* Whether we should update the key when overwriting an entry.  */
290   _Bool __need_key_update;
291 };
292 
293 /* A pointer type.  */
294 
295 struct __go_ptr_type
296 {
297   /* Starts like all other type descriptors.  */
298   struct __go_type_descriptor __common;
299 
300   /* The type to which this points.  */
301   const struct __go_type_descriptor *__element_type;
302 };
303 
304 /* A field in a structure.  */
305 
306 struct __go_struct_field
307 {
308   /* The name of the field--NULL for an anonymous field.  */
309   const struct String *__name;
310 
311   /* This is NULL for an exported method, or the name of the package
312      where it lives.  */
313   const struct String *__pkg_path;
314 
315   /* The type of the field.  */
316   const struct __go_type_descriptor *__type;
317 
318   /* The field tag, or NULL.  */
319   const struct String *__tag;
320 
321   /* The offset of the field in the struct.  */
322   uintptr_t __offset;
323 };
324 
325 /* A struct type.  */
326 
327 struct __go_struct_type
328 {
329   /* Starts like all other type descriptors.  */
330   struct __go_type_descriptor __common;
331 
332   /* An array of struct __go_struct_field.  */
333   struct __go_open_array __fields;
334 };
335 
336 /* Whether a type descriptor is a pointer.  */
337 
338 static inline _Bool
__go_is_pointer_type(const struct __go_type_descriptor * td)339 __go_is_pointer_type (const struct __go_type_descriptor *td)
340 {
341   return ((td->__code & GO_CODE_MASK) == GO_PTR
342 	  || (td->__code & GO_CODE_MASK) == GO_UNSAFE_POINTER);
343 }
344 
345 /* Call a type hash function, given the __hashfn value.  */
346 
347 static inline uintptr_t
__go_call_hashfn(const FuncVal * hashfn,const void * p,uintptr_t seed,uintptr_t size)348 __go_call_hashfn (const FuncVal *hashfn, const void *p, uintptr_t seed,
349 		  uintptr_t size)
350 {
351   uintptr_t (*h) (const void *, uintptr_t, uintptr_t) = (void *) hashfn->fn;
352   return __builtin_call_with_static_chain (h (p, seed, size), hashfn);
353 }
354 
355 /* Call a type equality function, given the __equalfn value.  */
356 
357 static inline _Bool
__go_call_equalfn(const FuncVal * equalfn,const void * p1,const void * p2,uintptr_t size)358 __go_call_equalfn (const FuncVal *equalfn, const void *p1, const void *p2,
359 		   uintptr_t size)
360 {
361   _Bool (*e) (const void *, const void *, uintptr_t) = (void *) equalfn->fn;
362   return __builtin_call_with_static_chain (e (p1, p2, size), equalfn);
363 }
364 
365 extern _Bool
366 __go_type_descriptors_equal(const struct __go_type_descriptor*,
367 			    const struct __go_type_descriptor*);
368 
369 #endif /* !defined(LIBGO_GO_TYPE_H) */
370