- Add Bugzilla PRs to ChangeLog reverting AIX long double size.
[official-gcc.git] / libgo / runtime / go-convert-interface.c
blob259456cda160fcb63138d8c8083a95335e64c593
1 /* go-convert-interface.c -- convert interfaces for Go.
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 "go-alloc.h"
8 #include "go-assert.h"
9 #include "go-panic.h"
10 #include "interface.h"
12 /* This is called when converting one interface type into another
13 interface type. LHS_DESCRIPTOR is the type descriptor of the
14 resulting interface. RHS_DESCRIPTOR is the type descriptor of the
15 object being converted. This builds and returns a new interface
16 method table. If any method in the LHS_DESCRIPTOR interface is not
17 implemented by the object, the conversion fails. If the conversion
18 fails, then if MAY_FAIL is true this returns NULL; otherwise, it
19 panics. */
21 void *
22 __go_convert_interface_2 (const struct __go_type_descriptor *lhs_descriptor,
23 const struct __go_type_descriptor *rhs_descriptor,
24 _Bool may_fail)
26 const struct __go_interface_type *lhs_interface;
27 int lhs_method_count;
28 const struct __go_interface_method* lhs_methods;
29 const void **methods;
30 const struct __go_uncommon_type *rhs_uncommon;
31 int rhs_method_count;
32 const struct __go_method *p_rhs_method;
33 int i;
35 if (rhs_descriptor == NULL)
37 /* A nil value always converts to nil. */
38 return NULL;
41 __go_assert (lhs_descriptor->__code == GO_INTERFACE);
42 lhs_interface = (const struct __go_interface_type *) lhs_descriptor;
43 lhs_method_count = lhs_interface->__methods.__count;
44 lhs_methods = ((const struct __go_interface_method *)
45 lhs_interface->__methods.__values);
47 /* This should not be called for an empty interface. */
48 __go_assert (lhs_method_count > 0);
50 rhs_uncommon = rhs_descriptor->__uncommon;
51 if (rhs_uncommon == NULL || rhs_uncommon->__methods.__count == 0)
53 struct __go_empty_interface panic_arg;
55 if (may_fail)
56 return NULL;
58 newTypeAssertionError (NULL,
59 rhs_descriptor,
60 lhs_descriptor,
61 NULL,
62 rhs_descriptor->__reflection,
63 lhs_descriptor->__reflection,
64 lhs_methods[0].__name,
65 &panic_arg);
66 __go_panic (panic_arg);
69 rhs_method_count = rhs_uncommon->__methods.__count;
70 p_rhs_method = ((const struct __go_method *)
71 rhs_uncommon->__methods.__values);
73 methods = NULL;
75 for (i = 0; i < lhs_method_count; ++i)
77 const struct __go_interface_method *p_lhs_method;
79 p_lhs_method = &lhs_methods[i];
81 while (rhs_method_count > 0
82 && (!__go_ptr_strings_equal (p_lhs_method->__name,
83 p_rhs_method->__name)
84 || !__go_ptr_strings_equal (p_lhs_method->__pkg_path,
85 p_rhs_method->__pkg_path)))
87 ++p_rhs_method;
88 --rhs_method_count;
91 if (rhs_method_count == 0
92 || !__go_type_descriptors_equal (p_lhs_method->__type,
93 p_rhs_method->__mtype))
95 struct __go_empty_interface panic_arg;
97 if (methods != NULL)
98 __go_free (methods);
100 if (may_fail)
101 return NULL;
103 newTypeAssertionError (NULL,
104 rhs_descriptor,
105 lhs_descriptor,
106 NULL,
107 rhs_descriptor->__reflection,
108 lhs_descriptor->__reflection,
109 p_lhs_method->__name,
110 &panic_arg);
111 __go_panic (panic_arg);
114 if (methods == NULL)
116 methods = (const void **) __go_alloc ((lhs_method_count + 1)
117 * sizeof (void *));
119 /* The first field in the method table is always the type of
120 the object. */
121 methods[0] = rhs_descriptor;
124 methods[i + 1] = p_rhs_method->__function;
127 return methods;
130 /* This is called by the compiler to convert a value from one
131 interface type to another. */
133 void *
134 __go_convert_interface (const struct __go_type_descriptor *lhs_descriptor,
135 const struct __go_type_descriptor *rhs_descriptor)
137 return __go_convert_interface_2 (lhs_descriptor, rhs_descriptor, 0);