1 /* go-type.h -- basic information for a Go type.
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. */
7 #ifndef LIBGO_GO_TYPE_H
8 #define LIBGO_GO_TYPE_H
13 #include "go-string.h"
16 /* Many of the types in this file must match the data structures
17 generated by the compiler, and must also match the Go types which
18 appear in go/runtime/type.go and go/reflect/type.go. */
20 /* Type kinds. These are used to get the type descriptor to use for
21 the type itself, when using unsafe.Typeof or unsafe.Reflect. The
22 values here must match the values generated by the compiler (the
23 RUNTIME_TYPE_KIND_xxx values in gcc/go/types.h). These are macros
24 rather than an enum to make it easy to change values in the future
25 and hard to get confused about it.
27 These correspond to the kind values used by the gc compiler. */
45 #define GO_COMPLEX64 17
46 #define GO_COMPLEX128 18
50 #define GO_INTERFACE 22
56 #define GO_UNSAFE_POINTER 28
58 /* For each Go type the compiler constructs one of these structures.
59 This is used for type reflectin, interfaces, maps, and reference
62 struct __go_type_descriptor
64 /* The type code for this type, a value in enum __go_type_codes.
65 This is used by unsafe.Reflect and unsafe.Typeof to determine the
66 type descriptor to return for this type itself. It is also used
67 by reflect.toType when mapping to a reflect Type structure. */
70 /* The alignment in bytes of a variable with this type. */
71 unsigned char __align;
73 /* The alignment in bytes of a struct field with this type. */
74 unsigned char __field_align;
76 /* The size in bytes of a value of this type. Note that all types
77 in Go have a fixed size. */
80 /* The type's hash code. */
83 /* This function takes a pointer to a value of this type, and the
84 size of this type, and returns a hash code. We pass the size
85 explicitly becaues it means that we can share a single instance
86 of this function for various different types. */
87 size_t (*__hashfn) (const void *, size_t);
89 /* This function takes two pointers to values of this type, and the
90 size of this type, and returns whether the values are equal. */
91 _Bool (*__equalfn) (const void *, const void *, size_t);
93 /* A string describing this type. This is only used for
95 const struct __go_string *__reflection;
97 /* A pointer to fields which are only used for some types. */
98 const struct __go_uncommon_type *__uncommon;
101 /* The information we store for each method of a type. */
105 /* The name of the method. */
106 const struct __go_string *__name;
108 /* This is NULL for an exported method, or the name of the package
110 const struct __go_string *__pkg_path;
112 /* The type of the method, without the receiver. This will be a
114 const struct __go_type_descriptor *__mtype;
116 /* The type of the method, with the receiver. This will be a
118 const struct __go_type_descriptor *__type;
120 /* A pointer to the code which implements the method. This is
121 really a function pointer. */
122 const void *__function;
125 /* Additional information that we keep for named types and for types
128 struct __go_uncommon_type
130 /* The name of the type. */
131 const struct __go_string *__name;
133 /* The type's package. This is NULL for builtin types. */
134 const struct __go_string *__pkg_path;
136 /* The type's methods. This is an array of struct __go_method. */
137 struct __go_open_array __methods;
140 /* The type descriptor for a fixed array type. */
142 struct __go_array_type
144 /* Starts like all type descriptors. */
145 struct __go_type_descriptor __common;
147 /* The element type. */
148 struct __go_type_descriptor *__element_type;
150 /* The length of the array. */
154 /* The type descriptor for a slice. */
156 struct __go_slice_type
158 /* Starts like all other type descriptors. */
159 struct __go_type_descriptor __common;
161 /* The element type. */
162 struct __go_type_descriptor *__element_type;
165 /* The direction of a channel. */
166 #define CHANNEL_RECV_DIR 1
167 #define CHANNEL_SEND_DIR 2
168 #define CHANNEL_BOTH_DIR (CHANNEL_RECV_DIR | CHANNEL_SEND_DIR)
170 /* The type descriptor for a channel. */
172 struct __go_channel_type
174 /* Starts like all other type descriptors. */
175 struct __go_type_descriptor __common;
177 /* The element type. */
178 const struct __go_type_descriptor *__element_type;
184 /* The type descriptor for a function. */
186 struct __go_func_type
188 /* Starts like all other type descriptors. */
189 struct __go_type_descriptor __common;
191 /* Whether this is a varargs function. If this is true, there will
192 be at least one parameter. For "..." the last parameter type is
193 "interface{}". For "... T" the last parameter type is "[]T". */
196 /* The input parameter types. This is an array of pointers to
197 struct __go_type_descriptor. */
198 struct __go_open_array __in;
200 /* The output parameter types. This is an array of pointers to
201 struct __go_type_descriptor. */
202 struct __go_open_array __out;
205 /* A method on an interface type. */
207 struct __go_interface_method
209 /* The name of the method. */
210 const struct __go_string *__name;
212 /* This is NULL for an exported method, or the name of the package
214 const struct __go_string *__pkg_path;
216 /* The real type of the method. */
217 struct __go_type_descriptor *__type;
220 /* An interface type. */
222 struct __go_interface_type
224 /* Starts like all other type descriptors. */
225 struct __go_type_descriptor __common;
227 /* Array of __go_interface_method . The methods are sorted in the
228 same order that they appear in the definition of the
230 struct __go_open_array __methods;
237 /* Starts like all other type descriptors. */
238 struct __go_type_descriptor __common;
240 /* The map key type. */
241 const struct __go_type_descriptor *__key_type;
243 /* The map value type. */
244 const struct __go_type_descriptor *__val_type;
247 /* A pointer type. */
251 /* Starts like all other type descriptors. */
252 struct __go_type_descriptor __common;
254 /* The type to which this points. */
255 const struct __go_type_descriptor *__element_type;
258 /* A field in a structure. */
260 struct __go_struct_field
262 /* The name of the field--NULL for an anonymous field. */
263 const struct __go_string *__name;
265 /* This is NULL for an exported method, or the name of the package
267 const struct __go_string *__pkg_path;
269 /* The type of the field. */
270 const struct __go_type_descriptor *__type;
272 /* The field tag, or NULL. */
273 const struct __go_string *__tag;
275 /* The offset of the field in the struct. */
281 struct __go_struct_type
283 /* Starts like all other type descriptors. */
284 struct __go_type_descriptor __common;
286 /* An array of struct __go_struct_field. */
287 struct __go_open_array __fields;
290 /* Whether a type descriptor is a pointer. */
293 __go_is_pointer_type (const struct __go_type_descriptor *td)
295 return td->__code == GO_PTR || td->__code == GO_UNSAFE_POINTER;
299 __go_type_descriptors_equal(const struct __go_type_descriptor*,
300 const struct __go_type_descriptor*);
302 extern size_t __go_type_hash_identity (const void *, size_t);
303 extern _Bool __go_type_equal_identity (const void *, const void *, size_t);
304 extern size_t __go_type_hash_string (const void *, size_t);
305 extern _Bool __go_type_equal_string (const void *, const void *, size_t);
306 extern size_t __go_type_hash_interface (const void *, size_t);
307 extern _Bool __go_type_equal_interface (const void *, const void *, size_t);
308 extern size_t __go_type_hash_error (const void *, size_t);
309 extern _Bool __go_type_equal_error (const void *, const void *, size_t);
311 #endif /* !defined(LIBGO_GO_TYPE_H) */