libgupc/
[official-gcc.git] / libgo / runtime / go-convert-interface.c
blob8ce82ea5ed617930e701aaf18ce6148132d6dd75
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"
11 #include "runtime.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
20 panics. */
22 void *
23 __go_convert_interface_2 (const struct __go_type_descriptor *lhs_descriptor,
24 const struct __go_type_descriptor *rhs_descriptor,
25 _Bool may_fail)
27 const struct __go_interface_type *lhs_interface;
28 int lhs_method_count;
29 const struct __go_interface_method* lhs_methods;
30 const void **methods;
31 const struct __go_uncommon_type *rhs_uncommon;
32 int rhs_method_count;
33 const struct __go_method *p_rhs_method;
34 int i;
36 if (rhs_descriptor == NULL)
38 /* A nil value always converts to nil. */
39 return NULL;
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;
56 if (may_fail)
57 return NULL;
59 runtime_newTypeAssertionError (NULL, rhs_descriptor->__reflection,
60 lhs_descriptor->__reflection,
61 lhs_methods[0].__name,
62 &panic_arg);
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);
70 methods = NULL;
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,
80 p_rhs_method->__name)
81 || !__go_ptr_strings_equal (p_lhs_method->__pkg_path,
82 p_rhs_method->__pkg_path)))
84 ++p_rhs_method;
85 --rhs_method_count;
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;
94 if (methods != NULL)
95 __go_free (methods);
97 if (may_fail)
98 return NULL;
100 runtime_newTypeAssertionError (NULL, rhs_descriptor->__reflection,
101 lhs_descriptor->__reflection,
102 p_lhs_method->__name, &panic_arg);
103 __go_panic (panic_arg);
106 if (methods == NULL)
108 methods = (const void **) __go_alloc ((lhs_method_count + 1)
109 * sizeof (void *));
111 /* The first field in the method table is always the type of
112 the object. */
113 methods[0] = rhs_descriptor;
116 methods[i + 1] = p_rhs_method->__function;
119 return methods;
122 /* This is called by the compiler to convert a value from one
123 interface type to another. */
125 void *
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);