go-iface.goc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package runtime
  5. #include "runtime.h"
  6. #include "go-type.h"
  7. #include "interface.h"
  8. typedef struct __go_type_descriptor descriptor;
  9. typedef const struct __go_type_descriptor const_descriptor;
  10. typedef struct __go_interface interface;
  11. typedef struct __go_empty_interface empty_interface;
  12. // Compare two type descriptors.
  13. func ifacetypeeq(a *descriptor, b *descriptor) (eq bool) {
  14. eq = __go_type_descriptors_equal(a, b);
  15. }
  16. // Return the descriptor for an empty interface type.n
  17. func efacetype(e empty_interface) (d *const_descriptor) {
  18. return e.__type_descriptor;
  19. }
  20. // Return the descriptor for a non-empty interface type.
  21. func ifacetype(i interface) (d *const_descriptor) {
  22. if (i.__methods == nil) {
  23. return nil;
  24. }
  25. d = i.__methods[0];
  26. }
  27. // Convert an empty interface to an empty interface.
  28. func ifaceE2E2(e empty_interface) (ret empty_interface, ok bool) {
  29. ret = e;
  30. ok = ret.__type_descriptor != nil;
  31. }
  32. // Convert a non-empty interface to an empty interface.
  33. func ifaceI2E2(i interface) (ret empty_interface, ok bool) {
  34. if (i.__methods == nil) {
  35. ret.__type_descriptor = nil;
  36. ret.__object = nil;
  37. ok = 0;
  38. } else {
  39. ret.__type_descriptor = i.__methods[0];
  40. ret.__object = i.__object;
  41. ok = 1;
  42. }
  43. }
  44. // Convert an empty interface to a non-empty interface.
  45. func ifaceE2I2(inter *descriptor, e empty_interface) (ret interface, ok bool) {
  46. if (e.__type_descriptor == nil) {
  47. ret.__methods = nil;
  48. ret.__object = nil;
  49. ok = 0;
  50. } else {
  51. ret.__methods = __go_convert_interface_2(inter,
  52. e.__type_descriptor,
  53. 1);
  54. ret.__object = e.__object;
  55. ok = ret.__methods != nil;
  56. }
  57. }
  58. // Convert a non-empty interface to a non-empty interface.
  59. func ifaceI2I2(inter *descriptor, i interface) (ret interface, ok bool) {
  60. if (i.__methods == nil) {
  61. ret.__methods = nil;
  62. ret.__object = nil;
  63. ok = 0;
  64. } else {
  65. ret.__methods = __go_convert_interface_2(inter,
  66. i.__methods[0], 1);
  67. ret.__object = i.__object;
  68. ok = ret.__methods != nil;
  69. }
  70. }
  71. // Convert an empty interface to a pointer type.
  72. func ifaceE2T2P(inter *descriptor, e empty_interface) (ret *void, ok bool) {
  73. if (!__go_type_descriptors_equal(inter, e.__type_descriptor)) {
  74. ret = nil;
  75. ok = 0;
  76. } else {
  77. ret = e.__object;
  78. ok = 1;
  79. }
  80. }
  81. // Convert a non-empty interface to a pointer type.
  82. func ifaceI2T2P(inter *descriptor, i interface) (ret *void, ok bool) {
  83. if (i.__methods == nil
  84. || !__go_type_descriptors_equal(inter, i.__methods[0])) {
  85. ret = nil;
  86. ok = 0;
  87. } else {
  88. ret = i.__object;
  89. ok = 1;
  90. }
  91. }
  92. // Convert an empty interface to a non-pointer type.
  93. func ifaceE2T2(inter *descriptor, e empty_interface, ret *void) (ok bool) {
  94. if (!__go_type_descriptors_equal(inter, e.__type_descriptor)) {
  95. __builtin_memset(ret, 0, inter->__size);
  96. ok = 0;
  97. } else {
  98. __builtin_memcpy(ret, e.__object, inter->__size);
  99. ok = 1;
  100. }
  101. }
  102. // Convert a non-empty interface to a non-pointer type.
  103. func ifaceI2T2(inter *descriptor, i interface, ret *void) (ok bool) {
  104. if (i.__methods == nil
  105. || !__go_type_descriptors_equal(inter, i.__methods[0])) {
  106. __builtin_memset(ret, 0, inter->__size);
  107. ok = 0;
  108. } else {
  109. __builtin_memcpy(ret, i.__object, inter->__size);
  110. ok = 1;
  111. }
  112. }
  113. // Return whether we can convert an interface to a type.
  114. func ifaceI2Tp(to *descriptor, from *descriptor) (ok bool) {
  115. ok = __go_can_convert_to_interface(to, from);
  116. }