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. */
10 #include "interface.h"
13 /* This is called when converting one interface type into another
14 interface type. LHS_DESCRIPTOR is the type descriptor of the
15 resulting interface. RHS_DESCRIPTOR is the type descriptor of the
16 object being converted. This builds and returns a new interface
17 method table. If any method in the LHS_DESCRIPTOR interface is not
18 implemented by the object, the conversion fails. If the conversion
19 fails, then if MAY_FAIL is true this returns NULL; otherwise, it
23 __go_convert_interface_2 (const struct __go_type_descriptor
*lhs_descriptor
,
24 const struct __go_type_descriptor
*rhs_descriptor
,
27 const struct __go_interface_type
*lhs_interface
;
29 const struct __go_interface_method
* lhs_methods
;
31 const struct __go_uncommon_type
*rhs_uncommon
;
33 const struct __go_method
*p_rhs_method
;
36 if (rhs_descriptor
== NULL
)
38 /* A nil value always converts to nil. */
42 __go_assert (lhs_descriptor
->__code
== GO_INTERFACE
);
43 lhs_interface
= (const struct __go_interface_type
*) lhs_descriptor
;
44 lhs_method_count
= lhs_interface
->__methods
.__count
;
45 lhs_methods
= ((const struct __go_interface_method
*)
46 lhs_interface
->__methods
.__values
);
48 /* This should not be called for an empty interface. */
49 __go_assert (lhs_method_count
> 0);
51 rhs_uncommon
= rhs_descriptor
->__uncommon
;
52 if (rhs_uncommon
== NULL
|| rhs_uncommon
->__methods
.__count
== 0)
54 struct __go_empty_interface panic_arg
;
59 runtime_newTypeAssertionError (NULL
, rhs_descriptor
->__reflection
,
60 lhs_descriptor
->__reflection
,
61 lhs_methods
[0].__name
,
63 __go_panic (panic_arg
);
66 rhs_method_count
= rhs_uncommon
->__methods
.__count
;
67 p_rhs_method
= ((const struct __go_method
*)
68 rhs_uncommon
->__methods
.__values
);
72 for (i
= 0; i
< lhs_method_count
; ++i
)
74 const struct __go_interface_method
*p_lhs_method
;
76 p_lhs_method
= &lhs_methods
[i
];
78 while (rhs_method_count
> 0
79 && (!__go_ptr_strings_equal (p_lhs_method
->__name
,
81 || !__go_ptr_strings_equal (p_lhs_method
->__pkg_path
,
82 p_rhs_method
->__pkg_path
)))
88 if (rhs_method_count
== 0
89 || !__go_type_descriptors_equal (p_lhs_method
->__type
,
90 p_rhs_method
->__mtype
))
92 struct __go_empty_interface panic_arg
;
100 runtime_newTypeAssertionError (NULL
, rhs_descriptor
->__reflection
,
101 lhs_descriptor
->__reflection
,
102 p_lhs_method
->__name
, &panic_arg
);
103 __go_panic (panic_arg
);
108 methods
= (const void **) __go_alloc ((lhs_method_count
+ 1)
111 /* The first field in the method table is always the type of
113 methods
[0] = rhs_descriptor
;
116 methods
[i
+ 1] = p_rhs_method
->__function
;
122 /* This is called by the compiler to convert a value from one
123 interface type to another. */
126 __go_convert_interface (const struct __go_type_descriptor
*lhs_descriptor
,
127 const struct __go_type_descriptor
*rhs_descriptor
)
129 return __go_convert_interface_2 (lhs_descriptor
, rhs_descriptor
, 0);