prelude.carbon 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. package Carbon api;
  5. // ----------------------
  6. // Conversion interfaces.
  7. // ----------------------
  8. // Explicitly convert `Self` to `T`.
  9. interface As(T:! Type) {
  10. fn Convert[me: Self]() -> T;
  11. }
  12. // Implicitly convert `Self` to `T`.
  13. interface ImplicitAs(T:! Type) {
  14. fn Convert[me: Self]() -> T;
  15. }
  16. // TODO: ImplicitAs(T) should extend As(T).
  17. impl forall [T:! Type, U:! ImplicitAs(T)] U as As(T) {
  18. fn Convert[me: Self]() -> T { return me.Convert(); }
  19. }
  20. // Every type implicitly converts to itself.
  21. impl forall [T:! Type] T as ImplicitAs(T) {
  22. fn Convert[me: Self]() -> T { return me; }
  23. }
  24. // TODO: Simplify this once we have variadics.
  25. // TODO: Should these be final?
  26. impl forall [U1:! Type, T1:! ImplicitAs(U1)]
  27. (T1,) as ImplicitAs((U1,)) {
  28. fn Convert[me: Self]() -> (U1,) {
  29. let (v1: T1,) = me;
  30. return (v1.Convert(),);
  31. }
  32. }
  33. impl forall [U1:! Type, U2:! Type, T1:! ImplicitAs(U1), T2:! ImplicitAs(U2)]
  34. (T1, T2) as ImplicitAs((U1, U2)) {
  35. fn Convert[me: Self]() -> (U1, U2) {
  36. let (v1: T1, v2: T2) = me;
  37. return (v1.Convert(), v2.Convert());
  38. }
  39. }
  40. impl forall [U1:! Type, U2:! Type, U3:! Type,
  41. T1:! ImplicitAs(U1), T2:! ImplicitAs(U2), T3:! ImplicitAs(U3)]
  42. (T1, T2, T3) as ImplicitAs((U1, U2, U3)) {
  43. fn Convert[me: Self]() -> (U1, U2, U3) {
  44. let (v1: T1, v2: T2, v3: T3) = me;
  45. return (v1.Convert(), v2.Convert(), v3.Convert());
  46. }
  47. }
  48. // ----------------------
  49. // Comparison interfaces.
  50. // ----------------------
  51. interface EqWith(U:! Type) {
  52. fn Equal[me: Self](other: U) -> bool;
  53. // TODO: NotEqual with default impl
  54. }
  55. // TODO: constraint Eq { ... }
  56. // TODO: Simplify this once we have variadics
  57. impl forall [T2:! Type, U2:! Type, T1:! EqWith(T2), U1:! EqWith(U2)]
  58. (T1, U1) as EqWith((T2, U2)) {
  59. fn Equal[me: Self](other: (T2, U2)) -> bool {
  60. let (l1: T1, l2: U1) = me;
  61. let (r1: T2, r2: U2) = other;
  62. return l1 == r1 and l2 == r2;
  63. }
  64. }
  65. impl i32 as EqWith(Self) {
  66. fn Equal[me: Self](other: Self) -> bool {
  67. return __intrinsic_int_eq(me, other);
  68. }
  69. }
  70. impl String as EqWith(Self) {
  71. fn Equal[me: Self](other: Self) -> bool {
  72. return __intrinsic_str_eq(me, other);
  73. }
  74. }
  75. // ----------------------
  76. // Arithmetic interfaces.
  77. // ----------------------
  78. interface Negate {
  79. // TODO: = Self
  80. let Result:! Type;
  81. fn Op[me: Self]() -> Result;
  82. }
  83. interface AddWith(U:! Type) {
  84. // TODO: = Self
  85. let Result:! Type;
  86. fn Op[me: Self](other: U) -> Result;
  87. }
  88. // TODO: constraint Add { ... }
  89. interface SubWith(U:! Type) {
  90. // TODO: = Self
  91. let Result:! Type;
  92. fn Op[me: Self](other: U) -> Result;
  93. }
  94. // TODO: constraint Sub { ... }
  95. interface MulWith(U:! Type) {
  96. // TODO: = Self
  97. let Result:! Type;
  98. fn Op[me: Self](other: U) -> Result;
  99. }
  100. // TODO: constraint Mul { ... }
  101. interface ModWith(U:! Type) {
  102. // TODO: = Self
  103. let Result:! Type;
  104. fn Op[me: Self](other: U) -> Result;
  105. }
  106. // TODO: constraint Mod { ... }
  107. // Note, these impls use the builtin addition for i32.
  108. external impl i32 as Negate where .Result == i32 {
  109. fn Op[me: i32]() -> i32 { return -me; }
  110. }
  111. external impl i32 as AddWith(i32) where .Result == i32 {
  112. fn Op[me: i32](other: i32) -> i32 { return me + other; }
  113. }
  114. external impl i32 as SubWith(i32) where .Result == i32 {
  115. fn Op[me: i32](other: i32) -> i32 { return me - other; }
  116. }
  117. external impl i32 as MulWith(i32) where .Result == i32 {
  118. fn Op[me: i32](other: i32) -> i32 { return me * other; }
  119. }
  120. external impl i32 as ModWith(i32) where .Result == i32 {
  121. fn Op[me: i32](other: i32) -> i32 { return me % other; }
  122. }
  123. // ---------------------------------
  124. // Bitwise and bit-shift interfaces.
  125. // ---------------------------------
  126. // Unary `^`.
  127. interface BitComplement {
  128. // TODO: = Self
  129. let Result:! Type;
  130. fn Op[me: Self]() -> Result;
  131. }
  132. // Binary `&`.
  133. interface BitAndWith(U:! Type) {
  134. // TODO: = Self
  135. let Result:! Type;
  136. fn Op[me: Self](other: U) -> Result;
  137. }
  138. // TODO:
  139. // constraint BitAnd {
  140. // extends BitAndWith(Self) where .Result == Self;
  141. // }
  142. // Binary `|`.
  143. interface BitOrWith(U:! Type) {
  144. // TODO: = Self
  145. let Result:! Type;
  146. fn Op[me: Self](other: U) -> Result;
  147. }
  148. // TODO:
  149. // constraint BitOr {
  150. // extends BitOrWith(Self) where .Result == Self;
  151. // }
  152. // Binary `^`.
  153. interface BitXorWith(U:! Type) {
  154. // TODO: = Self
  155. let Result:! Type;
  156. fn Op[me: Self](other: U) -> Result;
  157. }
  158. // TODO:
  159. // constraint BitXor {
  160. // extends BitXorWith(Self) where .Result == Self;
  161. // }
  162. // Binary `<<`.
  163. interface LeftShiftWith(U:! Type) {
  164. // TODO: = Self
  165. let Result:! Type;
  166. fn Op[me: Self](other: U) -> Result;
  167. }
  168. // TODO:
  169. // constraint LeftShift {
  170. // extends LeftShiftWith(Self) where .Result == Self;
  171. // }
  172. // Binary `>>`.
  173. interface RightShiftWith(U:! Type) {
  174. // TODO: = Self
  175. let Result:! Type;
  176. fn Op[me: Self](other: U) -> Result;
  177. }
  178. // TODO:
  179. // constraint RightShift {
  180. // extends RightShiftWith(Self) where .Result == Self;
  181. // }
  182. external impl i32 as BitComplement where .Result == i32 {
  183. fn Op[me: i32]() -> i32 {
  184. return __intrinsic_int_bit_complement(me);
  185. }
  186. }
  187. external impl i32 as BitAndWith(i32) where .Result == i32 {
  188. fn Op[me: i32](other: i32) -> i32 {
  189. return __intrinsic_int_bit_and(me, other);
  190. }
  191. }
  192. external impl i32 as BitOrWith(i32) where .Result == i32 {
  193. fn Op[me: i32](other: i32) -> i32 {
  194. return __intrinsic_int_bit_or(me, other);
  195. }
  196. }
  197. external impl i32 as BitXorWith(i32) where .Result == i32 {
  198. fn Op[me: i32](other: i32) -> i32 {
  199. return __intrinsic_int_bit_xor(me, other);
  200. }
  201. }
  202. external impl i32 as LeftShiftWith(i32) where .Result == i32 {
  203. fn Op[me: i32](other: i32) -> i32 {
  204. return __intrinsic_int_left_shift(me, other);
  205. }
  206. }
  207. external impl i32 as RightShiftWith(i32) where .Result == i32 {
  208. fn Op[me: i32](other: i32) -> i32 {
  209. return __intrinsic_int_right_shift(me, other);
  210. }
  211. }
  212. // ------------------------
  213. // Miscellaneous utilities.
  214. // ------------------------
  215. // Note that Print is experimental, and not part of an accepted proposal, but
  216. // is included here for printing state in tests.
  217. // TODO: Remove Print special casing once we have variadics or overloads.
  218. // fn Print(format_str: String) {
  219. // __intrinsic_print(format_str);
  220. // }
  221. fn Rand(low: i32, high: i32) -> i32{
  222. return __intrinsic_rand(low,high);
  223. }
  224. class Heap {
  225. fn New[T:! Type, me: Self](x : T) -> T* {
  226. return __intrinsic_new(x);
  227. }
  228. fn Delete[T:! Type, me: Self](p : T*) {
  229. __intrinsic_delete(p);
  230. }
  231. }
  232. var heap: Heap = {};