2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libgo / runtime / go-assert-interface.c
blob2510f9aef8b797f02a9341a319e6e2da4f2efc03
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 "runtime.h"
8 #include "go-alloc.h"
9 #include "go-assert.h"
10 #include "go-panic.h"
11 #include "go-type.h"
12 #include "interface.h"
14 /* This is called by the compiler to implement a type assertion from
15 one interface type to another. This returns the value that should
16 go in the first field of the result tuple. The result may be an
17 empty or a non-empty interface. */
19 const void *
20 __go_assert_interface (const struct __go_type_descriptor *lhs_descriptor,
21 const struct __go_type_descriptor *rhs_descriptor)
23 const struct __go_interface_type *lhs_interface;
25 if (rhs_descriptor == NULL)
27 struct __go_empty_interface panic_arg;
29 /* A type assertion is not permitted with a nil interface. */
31 runtime_newTypeAssertionError (NULL, NULL, lhs_descriptor->__reflection,
32 NULL, &panic_arg);
33 __go_panic (panic_arg);
36 /* A type assertion to an empty interface just returns the object
37 descriptor. */
39 __go_assert (lhs_descriptor->__code == GO_INTERFACE);
40 lhs_interface = (const struct __go_interface_type *) lhs_descriptor;
41 if (lhs_interface->__methods.__count == 0)
42 return rhs_descriptor;
44 return __go_convert_interface_2 (lhs_descriptor, rhs_descriptor, 0);