compiler, runtime: replace hashmap code with Go 1.7 hashmap
[official-gcc.git] / libgo / runtime / go-type-interface.c
blobffba7b28a3566c76e5e19d9a566e34f11174c46d
1 /* go-type-interface.c -- hash and equality interface functions.
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 #include "runtime.h"
8 #include "interface.h"
9 #include "go-type.h"
11 /* A hash function for an interface. */
13 uintptr_t
14 __go_type_hash_interface (const void *vval, uintptr_t seed,
15 uintptr_t key_size __attribute__ ((unused)))
17 const struct __go_interface *val;
18 const struct __go_type_descriptor *descriptor;
19 uintptr_t size;
21 val = (const struct __go_interface *) vval;
22 if (val->__methods == NULL)
23 return 0;
24 descriptor = (const struct __go_type_descriptor *) val->__methods[0];
25 if (descriptor->__hashfn == NULL)
26 runtime_panicstring ("hash of unhashable type");
27 size = descriptor->__size;
28 if (__go_is_pointer_type (descriptor))
29 return __go_call_hashfn (descriptor->__hashfn, &val->__object, seed, size);
30 else
31 return __go_call_hashfn (descriptor->__hashfn, val->__object, seed, size);
34 const FuncVal __go_type_hash_interface_descriptor =
35 { (void *) __go_type_hash_interface };
37 /* An equality function for an interface. */
39 _Bool
40 __go_type_equal_interface (const void *vv1, const void *vv2,
41 uintptr_t key_size __attribute__ ((unused)))
43 const struct __go_interface *v1;
44 const struct __go_interface *v2;
45 const struct __go_type_descriptor* v1_descriptor;
46 const struct __go_type_descriptor* v2_descriptor;
48 v1 = (const struct __go_interface *) vv1;
49 v2 = (const struct __go_interface *) vv2;
50 if (v1->__methods == NULL || v2->__methods == NULL)
51 return v1->__methods == v2->__methods;
52 v1_descriptor = (const struct __go_type_descriptor *) v1->__methods[0];
53 v2_descriptor = (const struct __go_type_descriptor *) v2->__methods[0];
54 if (!__go_type_descriptors_equal (v1_descriptor, v2_descriptor))
55 return 0;
56 if (v1_descriptor->__equalfn == NULL)
57 runtime_panicstring ("comparing uncomparable types");
58 if (__go_is_pointer_type (v1_descriptor))
59 return v1->__object == v2->__object;
60 else
61 return __go_call_equalfn (v1_descriptor->__equalfn, v1->__object,
62 v2->__object, v1_descriptor->__size);
65 const FuncVal __go_type_equal_interface_descriptor =
66 { (void *) __go_type_equal_interface };