go-convert-interface.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* go-convert-interface.c -- convert interfaces for Go.
  2. Copyright 2009 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include "runtime.h"
  6. #include "go-alloc.h"
  7. #include "go-assert.h"
  8. #include "go-panic.h"
  9. #include "go-string.h"
  10. #include "go-type.h"
  11. #include "interface.h"
  12. /* This is called when converting one interface type into another
  13. interface type. LHS_DESCRIPTOR is the type descriptor of the
  14. resulting interface. RHS_DESCRIPTOR is the type descriptor of the
  15. object being converted. This builds and returns a new interface
  16. method table. If any method in the LHS_DESCRIPTOR interface is not
  17. implemented by the object, the conversion fails. If the conversion
  18. fails, then if MAY_FAIL is true this returns NULL; otherwise, it
  19. panics. */
  20. void *
  21. __go_convert_interface_2 (const struct __go_type_descriptor *lhs_descriptor,
  22. const struct __go_type_descriptor *rhs_descriptor,
  23. _Bool may_fail)
  24. {
  25. const struct __go_interface_type *lhs_interface;
  26. int lhs_method_count;
  27. const struct __go_interface_method* lhs_methods;
  28. const void **methods;
  29. const struct __go_uncommon_type *rhs_uncommon;
  30. int rhs_method_count;
  31. const struct __go_method *p_rhs_method;
  32. int i;
  33. if (rhs_descriptor == NULL)
  34. {
  35. /* A nil value always converts to nil. */
  36. return NULL;
  37. }
  38. __go_assert ((lhs_descriptor->__code & GO_CODE_MASK) == GO_INTERFACE);
  39. lhs_interface = (const struct __go_interface_type *) lhs_descriptor;
  40. lhs_method_count = lhs_interface->__methods.__count;
  41. lhs_methods = ((const struct __go_interface_method *)
  42. lhs_interface->__methods.__values);
  43. /* This should not be called for an empty interface. */
  44. __go_assert (lhs_method_count > 0);
  45. rhs_uncommon = rhs_descriptor->__uncommon;
  46. if (rhs_uncommon == NULL || rhs_uncommon->__methods.__count == 0)
  47. {
  48. struct __go_empty_interface panic_arg;
  49. if (may_fail)
  50. return NULL;
  51. runtime_newTypeAssertionError (NULL, rhs_descriptor->__reflection,
  52. lhs_descriptor->__reflection,
  53. lhs_methods[0].__name,
  54. &panic_arg);
  55. __go_panic (panic_arg);
  56. }
  57. rhs_method_count = rhs_uncommon->__methods.__count;
  58. p_rhs_method = ((const struct __go_method *)
  59. rhs_uncommon->__methods.__values);
  60. methods = NULL;
  61. for (i = 0; i < lhs_method_count; ++i)
  62. {
  63. const struct __go_interface_method *p_lhs_method;
  64. p_lhs_method = &lhs_methods[i];
  65. while (rhs_method_count > 0
  66. && (!__go_ptr_strings_equal (p_lhs_method->__name,
  67. p_rhs_method->__name)
  68. || !__go_ptr_strings_equal (p_lhs_method->__pkg_path,
  69. p_rhs_method->__pkg_path)))
  70. {
  71. ++p_rhs_method;
  72. --rhs_method_count;
  73. }
  74. if (rhs_method_count == 0
  75. || !__go_type_descriptors_equal (p_lhs_method->__type,
  76. p_rhs_method->__mtype))
  77. {
  78. struct __go_empty_interface panic_arg;
  79. if (methods != NULL)
  80. __go_free (methods);
  81. if (may_fail)
  82. return NULL;
  83. runtime_newTypeAssertionError (NULL, rhs_descriptor->__reflection,
  84. lhs_descriptor->__reflection,
  85. p_lhs_method->__name, &panic_arg);
  86. __go_panic (panic_arg);
  87. }
  88. if (methods == NULL)
  89. {
  90. methods = (const void **) __go_alloc ((lhs_method_count + 1)
  91. * sizeof (void *));
  92. /* The first field in the method table is always the type of
  93. the object. */
  94. methods[0] = rhs_descriptor;
  95. }
  96. methods[i + 1] = p_rhs_method->__function;
  97. }
  98. return methods;
  99. }
  100. /* This is called by the compiler to convert a value from one
  101. interface type to another. */
  102. void *
  103. __go_convert_interface (const struct __go_type_descriptor *lhs_descriptor,
  104. const struct __go_type_descriptor *rhs_descriptor)
  105. {
  106. return __go_convert_interface_2 (lhs_descriptor, rhs_descriptor, 0);
  107. }