Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / runtime / go-type-interface.c
blob9750b843c49e4b173d3efce4a2901e19be9e68ec
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 "interface.h"
8 #include "go-type.h"
10 /* A hash function for an interface. */
12 size_t
13 __go_type_hash_interface (const void *vval,
14 size_t key_size __attribute__ ((unused)))
16 const struct __go_interface *val;
17 const struct __go_type_descriptor *descriptor;
18 size_t size;
20 val = (const struct __go_interface *) vval;
21 if (val->__methods == NULL)
22 return 0;
23 descriptor = (const struct __go_type_descriptor *) val->__methods[0];
24 size = descriptor->__size;
25 if (__go_is_pointer_type (descriptor))
26 return descriptor->__hashfn (&val->__object, size);
27 else
28 return descriptor->__hashfn (val->__object, size);
31 /* An equality function for an interface. */
33 _Bool
34 __go_type_equal_interface (const void *vv1, const void *vv2,
35 size_t key_size __attribute__ ((unused)))
37 const struct __go_interface *v1;
38 const struct __go_interface *v2;
39 const struct __go_type_descriptor* v1_descriptor;
40 const struct __go_type_descriptor* v2_descriptor;
42 v1 = (const struct __go_interface *) vv1;
43 v2 = (const struct __go_interface *) vv2;
44 if (v1->__methods == NULL || v2->__methods == NULL)
45 return v1->__methods == v2->__methods;
46 v1_descriptor = (const struct __go_type_descriptor *) v1->__methods[0];
47 v2_descriptor = (const struct __go_type_descriptor *) v2->__methods[0];
48 if (!__go_type_descriptors_equal (v1_descriptor, v2_descriptor))
49 return 0;
50 if (__go_is_pointer_type (v1_descriptor))
51 return v1->__object == v2->__object;
52 else
53 return v1_descriptor->__equalfn (v1->__object, v2->__object,
54 v1_descriptor->__size);