uint.carbon 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/as.carbon
  6. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int_literal.carbon
  7. // --- min_prelude/parts/uint.carbon
  8. package Core library "prelude/parts/uint";
  9. export import library "prelude/parts/as";
  10. export import library "prelude/parts/int_literal";
  11. private fn MakeUInt(size: IntLiteral()) -> type = "int.make_type_unsigned";
  12. class UInt(N:! IntLiteral()) {
  13. adapt MakeUInt(N);
  14. }
  15. // Conversions.
  16. impl forall [To:! IntLiteral()] IntLiteral() as ImplicitAs(UInt(To)) {
  17. fn Convert[self: Self]() -> UInt(To) = "int.convert_checked";
  18. }
  19. final impl forall [From:! IntLiteral()] UInt(From) as ImplicitAs(IntLiteral()) {
  20. fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
  21. }
  22. // TODO: Remove these once ImplicitAs extends As.
  23. impl forall [To:! IntLiteral()] IntLiteral() as As(UInt(To)) {
  24. fn Convert[self: Self]() -> UInt(To) = "int.convert_checked";
  25. }
  26. final impl forall [From:! IntLiteral()] UInt(From) as As(IntLiteral()) {
  27. fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
  28. }
  29. // Negate.
  30. final impl forall [N:! IntLiteral()]
  31. UInt(N) as Negate where .Result = Self {
  32. fn Op[self: Self]() -> Self = "int.unegate";
  33. }