prelude.carbon 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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[self: Self]() -> T;
  11. }
  12. // Implicitly convert `Self` to `T`.
  13. interface ImplicitAs(T:! type) {
  14. extend As(T);
  15. }
  16. // TODO: This should be private.
  17. interface __EqualConverter {
  18. let T:! type;
  19. fn Convert(t: T) -> Self;
  20. }
  21. fn __EqualConvert[T:! type](t: T, U:! __EqualConverter where .T = T) -> U {
  22. return U.Convert(t);
  23. }
  24. impl forall [U:! type] U as __EqualConverter where .T = U {
  25. fn Convert(u: U) -> U { return u; }
  26. }
  27. __match_first {
  28. // Pick up implicit conversions that are built into the compiler.
  29. // TODO: Split these into individual categories and implement as many as we can
  30. // in the prelude.
  31. impl forall [template U:! type, template T:! __intrinsic_implicit_as(U)]
  32. T as ImplicitAs(U) {
  33. fn Convert[self: Self]() -> U {
  34. return __intrinsic_implicit_as_convert(self, U);
  35. }
  36. }
  37. // Every type implicitly converts to single-step-equal types.
  38. impl forall [T:! type, U:! type where .Self == T] T as ImplicitAs(U) {
  39. fn Convert[self: Self]() -> U { return __EqualConvert(self, U); }
  40. }
  41. }
  42. // A tuple explicitly converts to another tuple if all its elements do.
  43. // TODO: Simplify this once we have variadics.
  44. // TODO: Should these be final?
  45. impl forall [U1:! type, T1:! As(U1)]
  46. (T1,) as As((U1,)) {
  47. fn Convert[self: Self]() -> (U1,) {
  48. let (v1: T1,) = self;
  49. return (v1.Convert(),);
  50. }
  51. }
  52. impl forall [U1:! type, U2:! type, T1:! As(U1), T2:! As(U2)]
  53. (T1, T2) as As((U1, U2)) {
  54. fn Convert[self: Self]() -> (U1, U2) {
  55. let (v1: T1, v2: T2) = self;
  56. return (v1.Convert(), v2.Convert());
  57. }
  58. }
  59. impl forall [U1:! type, U2:! type, U3:! type,
  60. T1:! As(U1), T2:! As(U2), T3:! As(U3)]
  61. (T1, T2, T3) as As((U1, U2, U3)) {
  62. fn Convert[self: Self]() -> (U1, U2, U3) {
  63. let (v1: T1, v2: T2, v3: T3) = self;
  64. return (v1.Convert(), v2.Convert(), v3.Convert());
  65. }
  66. }
  67. // ----------------------
  68. // Comparison interfaces.
  69. // ----------------------
  70. // ----------------------
  71. // EQUAL
  72. // ----------------------
  73. interface EqWith(U:! type) {
  74. fn Equal[self: Self](other: U) -> bool;
  75. fn NotEqual[self: Self](other: U) -> bool;
  76. }
  77. constraint Eq {
  78. extend EqWith(Self);
  79. }
  80. // TODO: Simplify this once we have variadics
  81. impl forall [T2:! type, U2:! type, T1:! EqWith(T2), U1:! EqWith(U2)]
  82. (T1, U1) as EqWith((T2, U2)) {
  83. fn Equal[self: Self](other: (T2, U2)) -> bool {
  84. let (l1: T1, l2: U1) = self;
  85. let (r1: T2, r2: U2) = other;
  86. return l1 == r1 and l2 == r2;
  87. }
  88. fn NotEqual[self: Self](other: (T2, U2)) -> bool {
  89. let (l1: T1, l2: U1) = self;
  90. let (r1: T2, r2: U2) = other;
  91. return l1 != r1 or l2 != r2;
  92. }
  93. }
  94. impl bool as EqWith(Self) {
  95. fn Equal[self: Self](other: Self) -> bool {
  96. return if self then other else not other;
  97. }
  98. fn NotEqual[self: Self](other: Self) -> bool {
  99. return if self then not other else other;
  100. }
  101. }
  102. impl i32 as EqWith(Self) {
  103. fn Equal[self: Self](other: Self) -> bool {
  104. return __intrinsic_int_eq(self, other);
  105. }
  106. fn NotEqual[self: Self](other: Self) -> bool {
  107. return not __intrinsic_int_eq(self, other);
  108. }
  109. }
  110. impl String as EqWith(Self) {
  111. fn Equal[self: Self](other: Self) -> bool {
  112. return __intrinsic_str_eq(self, other);
  113. }
  114. fn NotEqual[self: Self](other: Self) -> bool {
  115. return not __intrinsic_str_eq(self, other);
  116. }
  117. }
  118. // ----------------------
  119. // COMPARE
  120. // ----------------------
  121. choice Ordering {
  122. Less,
  123. Equivalent,
  124. Greater,
  125. Incomparable
  126. }
  127. // TODO: Per the design, this should be named `OrderedWith`.
  128. interface CompareWith(U:! type) {
  129. fn Compare[self: Self](u: U) -> Ordering;
  130. // TODO: Add `default fn` for Less, LessOrEquivalent, Greater, and GreaterOrEquivalent once it's available.
  131. }
  132. constraint Ordered {
  133. extend CompareWith(Self);
  134. }
  135. impl i32 as CompareWith(Self) {
  136. fn Compare[self: Self](other: Self) -> Ordering {
  137. var comp: i32 = __intrinsic_int_compare(self, other);
  138. if (comp == -1) {
  139. return Ordering.Less;
  140. }
  141. if (comp == 0) {
  142. return Ordering.Equivalent;
  143. }
  144. if (comp == 1) {
  145. return Ordering.Greater;
  146. }
  147. return Ordering.Incomparable;
  148. }
  149. }
  150. impl String as CompareWith(Self) {
  151. fn Compare[self: Self](other: Self) -> Ordering {
  152. var comp: i32 = __intrinsic_str_compare(self, other);
  153. if (comp == -1) {
  154. return Ordering.Less;
  155. }
  156. if (comp == 0) {
  157. return Ordering.Equivalent;
  158. }
  159. if (comp == 1) {
  160. return Ordering.Greater;
  161. }
  162. return Ordering.Incomparable;
  163. }
  164. }
  165. interface LessWith(U:! type) {
  166. fn Less[self: Self](other: U) -> bool;
  167. }
  168. interface LessEqWith(U:! type) {
  169. fn LessEq[self: Self](other: U) -> bool;
  170. }
  171. interface GreaterWith(U:! type) {
  172. fn Greater[self: Self](other: U) -> bool;
  173. }
  174. interface GreaterEqWith(U:! type) {
  175. fn GreaterEq[self: Self](other: U) -> bool;
  176. }
  177. impl i32 as LessWith(Self) {
  178. fn Less[self: Self](other: Self) -> bool {
  179. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  180. match (comp) {
  181. case Ordering.Less => {
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. }
  188. impl String as LessWith(Self) {
  189. fn Less[self: Self](other: Self) -> bool {
  190. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  191. match(comp){
  192. case Ordering.Less => {
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. }
  199. impl i32 as LessEqWith(Self) {
  200. fn LessEq[self: Self](other: Self) -> bool {
  201. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  202. match(comp){
  203. case Ordering.Less => {
  204. return true;
  205. }
  206. case Ordering.Equivalent => {
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. }
  213. impl String as LessEqWith(Self) {
  214. fn LessEq[self: Self](other: Self) -> bool {
  215. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  216. match(comp){
  217. case Ordering.Less => {
  218. return true;
  219. }
  220. case Ordering.Equivalent => {
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. }
  227. impl i32 as GreaterWith(Self) {
  228. fn Greater[self: Self](other: Self) -> bool {
  229. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  230. match(comp){
  231. case Ordering.Greater => {
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. }
  238. impl String as GreaterWith(Self) {
  239. fn Greater[self: Self](other: Self) -> bool {
  240. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  241. match(comp){
  242. case Ordering.Greater => {
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. }
  249. impl i32 as GreaterEqWith(Self) {
  250. fn GreaterEq[self: Self](other: Self) -> bool {
  251. var comp: Ordering = self.(CompareWith(i32).Compare)(other);
  252. match(comp){
  253. case Ordering.Greater => {
  254. return true;
  255. }
  256. case Ordering.Equivalent => {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. }
  263. impl String as GreaterEqWith(Self) {
  264. fn GreaterEq[self: Self](other: Self) -> bool {
  265. var comp: Ordering = self.(CompareWith(String).Compare)(other);
  266. match(comp){
  267. case Ordering.Greater => {
  268. return true;
  269. }
  270. case Ordering.Equivalent => {
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. }
  277. // ----------------------
  278. // Arithmetic interfaces.
  279. // ----------------------
  280. interface Negate {
  281. // TODO: = Self
  282. let Result:! type;
  283. fn Op[self: Self]() -> Result;
  284. }
  285. interface AddWith(U:! type) {
  286. // TODO: = Self
  287. let Result:! type;
  288. fn Op[self: Self](other: U) -> Result;
  289. }
  290. constraint Add {
  291. extend AddWith(Self) where .Result = Self;
  292. }
  293. interface SubWith(U:! type) {
  294. // TODO: = Self
  295. let Result:! type;
  296. fn Op[self: Self](other: U) -> Result;
  297. }
  298. constraint Sub {
  299. extend SubWith(Self) where .Result = Self;
  300. }
  301. interface MulWith(U:! type) {
  302. // TODO: = Self
  303. let Result:! type;
  304. fn Op[self: Self](other: U) -> Result;
  305. }
  306. constraint Mul {
  307. extend MulWith(Self) where .Result = Self;
  308. }
  309. interface DivWith(U:! type) {
  310. // TODO: = Self
  311. let Result:! type;
  312. fn Op[self: Self](other: U) -> Result;
  313. }
  314. constraint Div {
  315. extend DivWith(Self) where .Result = Self;
  316. }
  317. interface ModWith(U:! type) {
  318. // TODO: = Self
  319. let Result:! type;
  320. fn Op[self: Self](other: U) -> Result;
  321. }
  322. constraint Mod {
  323. extend ModWith(Self) where .Result = Self;
  324. }
  325. // Note, these impl declarations use the builtin addition for i32.
  326. impl i32 as Negate where .Result = i32 {
  327. fn Op[self: i32]() -> i32 { return -self; }
  328. }
  329. impl i32 as AddWith(i32) where .Result = i32 {
  330. fn Op[self: i32](other: i32) -> i32 { return self + other; }
  331. }
  332. impl i32 as SubWith(i32) where .Result = i32 {
  333. fn Op[self: i32](other: i32) -> i32 { return self - other; }
  334. }
  335. impl i32 as MulWith(i32) where .Result = i32 {
  336. fn Op[self: i32](other: i32) -> i32 { return self * other; }
  337. }
  338. impl i32 as DivWith(i32) where .Result = i32 {
  339. fn Op[self: i32](other: i32) -> i32 { return self / other; }
  340. }
  341. impl i32 as ModWith(i32) where .Result = i32 {
  342. fn Op[self: i32](other: i32) -> i32 { return self % other; }
  343. }
  344. // ---------------------------------
  345. // Bitwise and bit-shift interfaces.
  346. // ---------------------------------
  347. // Unary `^`.
  348. interface BitComplement {
  349. // TODO: = Self
  350. let Result:! type;
  351. fn Op[self: Self]() -> Result;
  352. }
  353. // Binary `&`.
  354. interface BitAndWith(U:! type) {
  355. // TODO: = Self
  356. let Result:! type;
  357. fn Op[self: Self](other: U) -> Result;
  358. }
  359. constraint BitAnd {
  360. extend BitAndWith(Self) where .Result = Self;
  361. }
  362. // Binary `|`.
  363. interface BitOrWith(U:! type) {
  364. // TODO: = Self
  365. let Result:! type;
  366. fn Op[self: Self](other: U) -> Result;
  367. }
  368. constraint BitOr {
  369. extend BitOrWith(Self) where .Result = Self;
  370. }
  371. // Binary `^`.
  372. interface BitXorWith(U:! type) {
  373. // TODO: = Self
  374. let Result:! type;
  375. fn Op[self: Self](other: U) -> Result;
  376. }
  377. constraint BitXor {
  378. extend BitXorWith(Self) where .Result = Self;
  379. }
  380. // Binary `<<`.
  381. interface LeftShiftWith(U:! type) {
  382. // TODO: = Self
  383. let Result:! type;
  384. fn Op[self: Self](other: U) -> Result;
  385. }
  386. constraint LeftShift {
  387. extend LeftShiftWith(Self) where .Result = Self;
  388. }
  389. // Binary `>>`.
  390. interface RightShiftWith(U:! type) {
  391. // TODO: = Self
  392. let Result:! type;
  393. fn Op[self: Self](other: U) -> Result;
  394. }
  395. constraint RightShift {
  396. extend RightShiftWith(Self) where .Result = Self;
  397. }
  398. impl i32 as BitComplement where .Result = i32 {
  399. fn Op[self: i32]() -> i32 {
  400. return __intrinsic_int_bit_complement(self);
  401. }
  402. }
  403. impl i32 as BitAndWith(i32) where .Result = i32 {
  404. fn Op[self: i32](other: i32) -> i32 {
  405. return __intrinsic_int_bit_and(self, other);
  406. }
  407. }
  408. impl i32 as BitOrWith(i32) where .Result = i32 {
  409. fn Op[self: i32](other: i32) -> i32 {
  410. return __intrinsic_int_bit_or(self, other);
  411. }
  412. }
  413. impl i32 as BitXorWith(i32) where .Result = i32 {
  414. fn Op[self: i32](other: i32) -> i32 {
  415. return __intrinsic_int_bit_xor(self, other);
  416. }
  417. }
  418. impl i32 as LeftShiftWith(i32) where .Result = i32 {
  419. fn Op[self: i32](other: i32) -> i32 {
  420. return __intrinsic_int_left_shift(self, other);
  421. }
  422. }
  423. impl i32 as RightShiftWith(i32) where .Result = i32 {
  424. fn Op[self: i32](other: i32) -> i32 {
  425. return __intrinsic_int_right_shift(self, other);
  426. }
  427. }
  428. // -----------------------------------
  429. // Assignment and compound assignment.
  430. // -----------------------------------
  431. interface AssignWith(U:! type) {
  432. fn Op[addr self: Self*](other: U);
  433. }
  434. constraint Assign { extend AssignWith(Self); }
  435. interface AddAssignWith(U:! type) {
  436. fn Op[addr self: Self*](other: U);
  437. }
  438. constraint AddAssign { extend AddAssignWith(Self); }
  439. interface SubAssignWith(U:! type) {
  440. fn Op[addr self: Self*](other: U);
  441. }
  442. constraint SubAssign { extend SubAssignWith(Self); }
  443. interface MulAssignWith(U:! type) {
  444. fn Op[addr self: Self*](other: U);
  445. }
  446. constraint MulAssign { extend MulAssignWith(Self); }
  447. interface DivAssignWith(U:! type) {
  448. fn Op[addr self: Self*](other: U);
  449. }
  450. constraint DivAssign { extend DivAssignWith(Self); }
  451. interface ModAssignWith(U:! type) {
  452. fn Op[addr self: Self*](other: U);
  453. }
  454. constraint ModAssign { extend ModAssignWith(Self); }
  455. interface BitAndAssignWith(U:! type) {
  456. fn Op[addr self: Self*](other: U);
  457. }
  458. constraint BitAssignAnd { extend BitAndAssignWith(Self); }
  459. interface BitOrAssignWith(U:! type) {
  460. fn Op[addr self: Self*](other: U);
  461. }
  462. constraint BitAssignOr { extend BitOrAssignWith(Self); }
  463. interface BitXorAssignWith(U:! type) {
  464. fn Op[addr self: Self*](other: U);
  465. }
  466. constraint BitAssignXor { extend BitXorAssignWith(Self); }
  467. interface LeftShiftAssignWith(U:! type) {
  468. fn Op[addr self: Self*](other: U);
  469. }
  470. constraint LeftShiftAssign { extend LeftShiftAssignWith(Self); }
  471. interface RightShiftAssignWith(U:! type) {
  472. fn Op[addr self: Self*](other: U);
  473. }
  474. constraint RightShiftAssign { extend RightShiftAssignWith(Self); }
  475. // TODO: This is temporary, and should eventually be replaced by
  476. // something more fine-grained. Not all class types should be
  477. // assignable.
  478. impl forall [T:! type, U:! ImplicitAs(T)]
  479. T as AssignWith(U) {
  480. fn Op[addr self: Self*](other: U) {
  481. *self = other.Convert();
  482. }
  483. }
  484. // TODO: Should `AddWith(U) & AssignWith(.Self.(AddWith(U).Result))` work?
  485. impl forall [U:! type, T:! AddWith(U) where .Self impls AssignWith(.Self.Result)]
  486. T as AddAssignWith(U) {
  487. fn Op[addr self: Self*](other: U) {
  488. *self = *self + other;
  489. }
  490. }
  491. impl forall [U:! type, T:! SubWith(U) where .Self impls AssignWith(.Self.Result)]
  492. T as SubAssignWith(U) {
  493. fn Op[addr self: Self*](other: U) {
  494. *self = *self - other;
  495. }
  496. }
  497. impl forall [U:! type, T:! MulWith(U) where .Self impls AssignWith(.Self.Result)]
  498. T as MulAssignWith(U) {
  499. fn Op[addr self: Self*](other: U) {
  500. *self = *self * other;
  501. }
  502. }
  503. impl forall [U:! type, T:! DivWith(U) where .Self impls AssignWith(.Self.Result)]
  504. T as DivAssignWith(U) {
  505. fn Op[addr self: Self*](other: U) {
  506. *self = *self / other;
  507. }
  508. }
  509. impl forall [U:! type, T:! ModWith(U) where .Self impls AssignWith(.Self.Result)]
  510. T as ModAssignWith(U) {
  511. fn Op[addr self: Self*](other: U) {
  512. *self = *self % other;
  513. }
  514. }
  515. impl forall [U:! type, T:! BitAndWith(U) where .Self impls AssignWith(.Self.Result)]
  516. T as BitAndAssignWith(U) {
  517. fn Op[addr self: Self*](other: U) {
  518. *self = *self & other;
  519. }
  520. }
  521. impl forall [U:! type, T:! BitOrWith(U) where .Self impls AssignWith(.Self.Result)]
  522. T as BitOrAssignWith(U) {
  523. fn Op[addr self: Self*](other: U) {
  524. *self = *self | other;
  525. }
  526. }
  527. impl forall [U:! type, T:! BitXorWith(U) where .Self impls AssignWith(.Self.Result)]
  528. T as BitXorAssignWith(U) {
  529. fn Op[addr self: Self*](other: U) {
  530. *self = *self ^ other;
  531. }
  532. }
  533. impl forall [U:! type, T:! LeftShiftWith(U) where .Self impls AssignWith(.Self.Result)]
  534. T as LeftShiftAssignWith(U) {
  535. fn Op[addr self: Self*](other: U) {
  536. *self = *self << other;
  537. }
  538. }
  539. impl forall [U:! type, T:! RightShiftWith(U) where .Self impls AssignWith(.Self.Result)]
  540. T as RightShiftAssignWith(U) {
  541. fn Op[addr self: Self*](other: U) {
  542. *self = *self >> other;
  543. }
  544. }
  545. // ------------------------
  546. // Increment and decrement.
  547. // ------------------------
  548. interface Inc {
  549. fn Op[addr self: Self*]();
  550. }
  551. interface Dec {
  552. fn Op[addr self: Self*]();
  553. }
  554. impl i32 as Inc {
  555. fn Op[addr self: Self*]() {
  556. *self = *self + 1;
  557. }
  558. }
  559. impl i32 as Dec {
  560. fn Op[addr self: Self*]() {
  561. *self = *self - 1;
  562. }
  563. }
  564. // ------------------------
  565. // Miscellaneous utilities.
  566. // ------------------------
  567. // Note that Print is experimental, and not part of an accepted proposal, but
  568. // is included here for printing state in tests.
  569. // TODO: Remove Print special casing once we have variadics or overloads.
  570. // fn Print(format_str: String) {
  571. // __intrinsic_print(format_str);
  572. // }
  573. fn Assert(condition: bool, message: String){
  574. __intrinsic_assert(condition, message);
  575. }
  576. fn Rand(low: i32, high: i32) -> i32{
  577. return __intrinsic_rand(low,high);
  578. }
  579. //-------------------------
  580. // Optional.
  581. //-------------------------
  582. choice OptionalElement(T:! type) {
  583. None,
  584. Element(T)
  585. }
  586. class Optional(T:! type) {
  587. fn CreateEmpty() -> Optional(T) {
  588. return {.element = OptionalElement(T).None};
  589. }
  590. fn Create(value: T) -> Optional(T) {
  591. return {.element = OptionalElement(T).Element(value)};
  592. }
  593. fn HasValue[self: Self]() -> bool {
  594. match(self.element) {
  595. case OptionalElement(T).None => { return false; }
  596. }
  597. return true;
  598. }
  599. fn Get[self: Self]() -> T {
  600. match(self.element) {
  601. case OptionalElement(T).Element(x: T) => {
  602. return x;
  603. }
  604. }
  605. Assert(false, "Attempted to unwrap empty Optional");
  606. // TODO: Drop return when we can flag unreachable paths.
  607. return self.Get();
  608. }
  609. var element: OptionalElement(T);
  610. }
  611. //-------------------------
  612. // Heap.
  613. //-------------------------
  614. class Heap {
  615. fn New[T:! type, self: Self](x : T) -> T* {
  616. return __intrinsic_new(x);
  617. }
  618. fn Delete[T:! type, self: Self](p : T*) {
  619. __intrinsic_delete(p);
  620. }
  621. fn PrintAllocs[self: Self]() {
  622. __intrinsic_print_allocs();
  623. }
  624. }
  625. var heap: Heap = {};