copy.carbon 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/copy";
  5. // Copying an object, which is a conversion from a value representation to an
  6. // initializing representation.
  7. interface Copy {
  8. fn Op[self: Self]() -> Self;
  9. }
  10. private fn Bool() -> type = "bool.make_type";
  11. private fn CharLiteral() -> type = "char_literal.make_type";
  12. private fn FloatLiteral() -> type = "float_literal.make_type";
  13. private fn IntLiteral() -> type = "int_literal.make_type";
  14. impl forall [T:! Copy] const T as Copy {
  15. fn Op[self: Self]() -> Self { return (self as T).(Copy.Op)() as const T; }
  16. }
  17. impl Bool() as Copy {
  18. fn Op[self: Self]() -> Self = "primitive_copy";
  19. }
  20. impl CharLiteral() as Copy {
  21. fn Op[self: Self]() -> Self = "primitive_copy";
  22. }
  23. impl FloatLiteral() as Copy {
  24. fn Op[self: Self]() -> Self = "primitive_copy";
  25. }
  26. impl IntLiteral() as Copy {
  27. fn Op[self: Self]() -> Self = "primitive_copy";
  28. }
  29. impl type as Copy {
  30. fn Op[self: Self]() -> Self = "primitive_copy";
  31. }
  32. impl forall [T:! type] T* as Copy {
  33. fn Op[self: Self]() -> Self = "primitive_copy";
  34. }
  35. // TODO: Implement tuple copy as a variadic generic impl.
  36. impl () as Copy {
  37. fn Op[self: Self]() -> Self = "no_op";
  38. }
  39. impl forall [T:! Copy] (T,) as Copy {
  40. fn Op[self: Self]() -> Self {
  41. return (self.0.Op(),);
  42. }
  43. }
  44. impl forall [T:! Copy, U:! Copy] (T, U) as Copy {
  45. fn Op[self: Self]() -> Self {
  46. return (self.0.Op(), self.1.Op());
  47. }
  48. }
  49. impl forall [T:! Copy, U:! Copy, V:! Copy] (T, U, V) as Copy {
  50. fn Op[self: Self]() -> Self {
  51. return (self.0.Op(), self.1.Op(), self.2.Op());
  52. }
  53. }