PR middle-end/53411
[official-gcc.git] / libgo / runtime / go-assert-interface.c
blob94bdaeef42983a8ebbd2b28f0cb23899b92bfc8a
1 /* go-assert-interface.c -- interface type assertion for Go.
3 Copyright 2010 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 by the compiler to implement a type assertion from
14 one interface type to another. This returns the value that should
15 go in the first field of the result tuple. The result may be an
16 empty or a non-empty interface. */
18 const void *
19 __go_assert_interface (const struct __go_type_descriptor *lhs_descriptor,
20 const struct __go_type_descriptor *rhs_descriptor)
22 const struct __go_interface_type *lhs_interface;
24 if (rhs_descriptor == NULL)
26 struct __go_empty_interface panic_arg;
28 /* A type assertion is not permitted with a nil interface. */
30 runtime_newTypeAssertionError (NULL, NULL, lhs_descriptor->__reflection,
31 NULL, &panic_arg);
32 __go_panic (panic_arg);
35 /* A type assertion to an empty interface just returns the object
36 descriptor. */
38 __go_assert (lhs_descriptor->__code == GO_INTERFACE);
39 lhs_interface = (const struct __go_interface_type *) lhs_descriptor;
40 if (lhs_interface->__methods.__count == 0)
41 return rhs_descriptor;
43 return __go_convert_interface_2 (lhs_descriptor, rhs_descriptor, 0);