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. */
11 #include "go-string.h"
13 #include "interface.h"
15 /* This is called when converting one interface type into another
16 interface type. LHS_DESCRIPTOR is the type descriptor of the
17 resulting interface. RHS_DESCRIPTOR is the type descriptor of the
18 object being converted. This builds and returns a new interface
19 method table. If any method in the LHS_DESCRIPTOR interface is not
20 implemented by the object, the conversion fails. If the conversion
21 fails, then if MAY_FAIL is true this returns NULL; otherwise, it
25 __go_convert_interface_2 (const struct __go_type_descriptor
*lhs_descriptor
,
26 const struct __go_type_descriptor
*rhs_descriptor
,
29 const struct __go_interface_type
*lhs_interface
;
31 const struct __go_interface_method
* lhs_methods
;
33 const struct __go_uncommon_type
*rhs_uncommon
;
35 const struct __go_method
*p_rhs_method
;
38 if (rhs_descriptor
== NULL
)
40 /* A nil value always converts to nil. */
44 __go_assert ((lhs_descriptor
->__code
& GO_CODE_MASK
) == GO_INTERFACE
);
45 lhs_interface
= (const struct __go_interface_type
*) lhs_descriptor
;
46 lhs_method_count
= lhs_interface
->__methods
.__count
;
47 lhs_methods
= ((const struct __go_interface_method
*)
48 lhs_interface
->__methods
.__values
);
50 /* This should not be called for an empty interface. */
51 __go_assert (lhs_method_count
> 0);
53 rhs_uncommon
= rhs_descriptor
->__uncommon
;
54 if (rhs_uncommon
== NULL
|| rhs_uncommon
->__methods
.__count
== 0)
56 struct __go_empty_interface panic_arg
;
61 runtime_newTypeAssertionError (NULL
, rhs_descriptor
->__reflection
,
62 lhs_descriptor
->__reflection
,
63 lhs_methods
[0].__name
,
65 __go_panic (panic_arg
);
68 rhs_method_count
= rhs_uncommon
->__methods
.__count
;
69 p_rhs_method
= ((const struct __go_method
*)
70 rhs_uncommon
->__methods
.__values
);
74 for (i
= 0; i
< lhs_method_count
; ++i
)
76 const struct __go_interface_method
*p_lhs_method
;
78 p_lhs_method
= &lhs_methods
[i
];
80 while (rhs_method_count
> 0
81 && (!__go_ptr_strings_equal (p_lhs_method
->__name
,
83 || !__go_ptr_strings_equal (p_lhs_method
->__pkg_path
,
84 p_rhs_method
->__pkg_path
)))
90 if (rhs_method_count
== 0
91 || !__go_type_descriptors_equal (p_lhs_method
->__type
,
92 p_rhs_method
->__mtype
))
94 struct __go_empty_interface panic_arg
;
102 runtime_newTypeAssertionError (NULL
, rhs_descriptor
->__reflection
,
103 lhs_descriptor
->__reflection
,
104 p_lhs_method
->__name
, &panic_arg
);
105 __go_panic (panic_arg
);
110 methods
= (const void **) __go_alloc ((lhs_method_count
+ 1)
113 /* The first field in the method table is always the type of
115 methods
[0] = rhs_descriptor
;
118 methods
[i
+ 1] = p_rhs_method
->__function
;
124 /* This is called by the compiler to convert a value from one
125 interface type to another. */
128 __go_convert_interface (const struct __go_type_descriptor
*lhs_descriptor
,
129 const struct __go_type_descriptor
*rhs_descriptor
)
131 return __go_convert_interface_2 (lhs_descriptor
, rhs_descriptor
, 0);