go-can-convert-interface.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* go-can-convert-interface.c -- can we convert to an interface?
  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-assert.h"
  7. #include "go-string.h"
  8. #include "go-type.h"
  9. #include "interface.h"
  10. /* Return whether we can convert from the type in FROM_DESCRIPTOR to
  11. the interface in TO_DESCRIPTOR. This is used for type
  12. switches. */
  13. _Bool
  14. __go_can_convert_to_interface (
  15. const struct __go_type_descriptor *to_descriptor,
  16. const struct __go_type_descriptor *from_descriptor)
  17. {
  18. const struct __go_interface_type *to_interface;
  19. int to_method_count;
  20. const struct __go_interface_method *to_method;
  21. const struct __go_uncommon_type *from_uncommon;
  22. int from_method_count;
  23. const struct __go_method *from_method;
  24. int i;
  25. /* In a type switch FROM_DESCRIPTOR can be NULL. */
  26. if (from_descriptor == NULL)
  27. return 0;
  28. __go_assert ((to_descriptor->__code & GO_CODE_MASK) == GO_INTERFACE);
  29. to_interface = (const struct __go_interface_type *) to_descriptor;
  30. to_method_count = to_interface->__methods.__count;
  31. to_method = ((const struct __go_interface_method *)
  32. to_interface->__methods.__values);
  33. from_uncommon = from_descriptor->__uncommon;
  34. if (from_uncommon == NULL)
  35. {
  36. from_method_count = 0;
  37. from_method = NULL;
  38. }
  39. else
  40. {
  41. from_method_count = from_uncommon->__methods.__count;
  42. from_method = ((const struct __go_method *)
  43. from_uncommon->__methods.__values);
  44. }
  45. for (i = 0; i < to_method_count; ++i)
  46. {
  47. while (from_method_count > 0
  48. && (!__go_ptr_strings_equal (from_method->__name,
  49. to_method->__name)
  50. || !__go_ptr_strings_equal (from_method->__pkg_path,
  51. to_method->__pkg_path)))
  52. {
  53. ++from_method;
  54. --from_method_count;
  55. }
  56. if (from_method_count == 0)
  57. return 0;
  58. if (!__go_type_descriptors_equal (from_method->__mtype,
  59. to_method->__type))
  60. return 0;
  61. ++to_method;
  62. ++from_method;
  63. --from_method_count;
  64. }
  65. return 1;
  66. }