as.carbon 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Core library "prelude/operators/as";
  5. import library "prelude/copy";
  6. interface UnsafeAs(Dest:! type) {
  7. fn Convert[self: Self]() -> Dest;
  8. }
  9. interface As(Dest:! type) {
  10. // TODO: extend UnsafeAs(Dest);
  11. fn Convert[self: Self]() -> Dest;
  12. }
  13. interface ImplicitAs(Dest:! type) {
  14. // TODO: extend As(Dest);
  15. fn Convert[self: Self]() -> Dest;
  16. }
  17. // Workaround: ImplicitAs extends As.
  18. impl forall [U:! type, T:! ImplicitAs(U)] T as As(U) {
  19. fn Convert[self: Self]() -> U { return self.Convert(); }
  20. }
  21. // Workaround: As extends UnsafeAs.
  22. impl forall [U:! type, T:! As(U)] T as UnsafeAs(U) {
  23. fn Convert[self: Self]() -> U { return self.Convert(); }
  24. }
  25. // Copyable types have an identity conversion that performs a copy.
  26. impl forall [T:! Copy] T as ImplicitAs(T) {
  27. fn Convert[self: Self]() -> Self { return self.(Copy.Op)(); }
  28. }
  29. // `const` can be added and removed when converting a value.
  30. impl forall [T:! type, U:! ImplicitAs(T)] U as ImplicitAs(const T) {
  31. fn Convert[self: U]() -> const T { return self.Convert(); }
  32. }
  33. impl forall [T:! type, U:! ImplicitAs(T)] const U as ImplicitAs(T) {
  34. fn Convert[self: const U]() -> T { return (self as U).Convert(); }
  35. }
  36. // `const` can be added to a pointer.
  37. // TODO: This is also provided as a builtin conversion. We provide it here so
  38. // that Optional(T*) can implicitly convert to Optional(const T*). See #5750.
  39. impl forall [T:! type] T* as ImplicitAs(const T*) {
  40. fn Convert[self: T*]() -> const T* = "pointer.unsafe_convert";
  41. }
  42. impl forall [T:! type] T* as As(const T*) {
  43. fn Convert[self: T*]() -> const T* = "pointer.unsafe_convert";
  44. }
  45. // Pointer types can be unsafely cast to other pointer types.
  46. // TODO: Should `unsafe as` be able to remove `const`?
  47. impl forall [T:! type, U:! type] T* as UnsafeAs(U*) {
  48. fn Convert[self: T*]() -> U* = "pointer.unsafe_convert";
  49. }
  50. interface IntFitsIn(Dest:! type) {}