enum.carbon 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/full.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/interop/cpp/enum.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/enum.carbon
  12. // --- enum_member.h
  13. struct C {
  14. enum E : short {
  15. a,
  16. b = 7,
  17. c
  18. };
  19. static void F(E);
  20. };
  21. // --- fail_todo_pass_as_arg.carbon
  22. library "[[@TEST_NAME]]";
  23. import Cpp library "enum_member.h";
  24. fn Pass() {
  25. Cpp.C.F(Cpp.C.a);
  26. Cpp.C.F(Cpp.C.a);
  27. Cpp.C.F(Cpp.C.b);
  28. Cpp.C.F(Cpp.C.c);
  29. Cpp.C.F(Cpp.C.E.a);
  30. Cpp.C.F(Cpp.C.E.b);
  31. // CHECK:STDERR: fail_todo_pass_as_arg.carbon:[[@LINE+28]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  32. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.c);
  33. // CHECK:STDERR: ^~~~~~~~~
  34. // CHECK:STDERR:
  35. // CHECK:STDERR: fail_todo_pass_as_arg.carbon:[[@LINE-5]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  36. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.b);
  37. // CHECK:STDERR: ^~~~~~~~~
  38. // CHECK:STDERR:
  39. // CHECK:STDERR: fail_todo_pass_as_arg.carbon:[[@LINE-10]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  40. // CHECK:STDERR: Cpp.C.F(Cpp.C.E.a);
  41. // CHECK:STDERR: ^~~~~~~~~
  42. // CHECK:STDERR:
  43. // CHECK:STDERR: fail_todo_pass_as_arg.carbon:[[@LINE-15]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  44. // CHECK:STDERR: Cpp.C.F(Cpp.C.c);
  45. // CHECK:STDERR: ^~~~~~~
  46. // CHECK:STDERR:
  47. // CHECK:STDERR: fail_todo_pass_as_arg.carbon:[[@LINE-20]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  48. // CHECK:STDERR: Cpp.C.F(Cpp.C.b);
  49. // CHECK:STDERR: ^~~~~~~
  50. // CHECK:STDERR:
  51. // CHECK:STDERR: fail_todo_pass_as_arg.carbon:[[@LINE-25]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  52. // CHECK:STDERR: Cpp.C.F(Cpp.C.a);
  53. // CHECK:STDERR: ^~~~~~~
  54. // CHECK:STDERR:
  55. // CHECK:STDERR: fail_todo_pass_as_arg.carbon:[[@LINE-30]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  56. // CHECK:STDERR: Cpp.C.F(Cpp.C.a);
  57. // CHECK:STDERR: ^~~~~~~
  58. // CHECK:STDERR:
  59. Cpp.C.F(Cpp.C.E.c);
  60. }
  61. // --- fail_todo_convert.carbon
  62. library "[[@TEST_NAME]]";
  63. import Cpp library "enum_member.h";
  64. fn TakeI16(n: i16);
  65. fn PassEnum() {
  66. TakeI16(Cpp.C.b as i16);
  67. }
  68. fn ConvertEnumToI16(e: Cpp.C.E) {
  69. TakeI16(e as i16);
  70. }
  71. fn ConvertI16ToEnum(n: i16) {
  72. // CHECK:STDERR: fail_todo_convert.carbon:[[@LINE+4]]:11: error: cannot access member of interface `Core.Destroy` in type `Cpp.E` that does not implement that interface [MissingImplInMemberAccess]
  73. // CHECK:STDERR: Cpp.C.F(n as Cpp.C.E);
  74. // CHECK:STDERR: ^~~~~~~~~~~~
  75. // CHECK:STDERR:
  76. Cpp.C.F(n as Cpp.C.E);
  77. }
  78. // --- bitmask.h
  79. enum Bits {
  80. A = 1 << 0,
  81. B = 1 << 1,
  82. C = 1 << 2,
  83. };
  84. void Take(Bits b);
  85. // --- use_bitmask.carbon
  86. library "[[@TEST_NAME]]";
  87. import Cpp library "bitmask.h";
  88. // TODO: Once we support interop with C++-defined operators, we should be able
  89. // to use `|` below rather than declaring our own builtin.
  90. fn BitOr(a: Cpp.Bits, b: Cpp.Bits) -> Cpp.Bits = "int.or";
  91. fn Call() {
  92. Cpp.Take(BitOr(Cpp.A, Cpp.C));
  93. }