deref.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // EXTRA-ARGS: --clang-arg=-std=c++20
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/operators/deref.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/operators/deref.carbon
  14. // --- const_deref.carbon
  15. library "[[@TEST_NAME]]";
  16. import Cpp inline '''c++
  17. class ConstDeref {
  18. public:
  19. auto operator*() const -> int&;
  20. };
  21. ''';
  22. fn TestDerefPass(var int_ptr: Cpp.ConstDeref) -> ref i32 {
  23. //@dump-sem-ir-begin
  24. return int_ptr.(Core.CppUnsafeDeref.Op)();
  25. //@dump-sem-ir-end
  26. }
  27. // --- mutable_deref.carbon
  28. library "[[@TEST_NAME]]";
  29. import Cpp inline '''c++
  30. class MutableDeref {
  31. public:
  32. auto operator*() -> int&;
  33. };
  34. ''';
  35. fn TestDerefPass(var int_ptr: Cpp.MutableDeref) -> ref i32 {
  36. //@dump-sem-ir-begin
  37. return int_ptr.(Core.CppUnsafeDeref.Op)();
  38. //@dump-sem-ir-end
  39. }
  40. // --- fail_todo_multi_deref.carbon
  41. library "[[@TEST_NAME]]";
  42. import Cpp inline '''c++
  43. class MultiDeref {
  44. public:
  45. auto operator*() & -> int&;
  46. auto operator*() const& -> const int&;
  47. auto operator*() && -> int&&;
  48. auto operator*() const&& -> int const&&;
  49. };
  50. ''';
  51. fn TestDerefPass(var int_ptr: Cpp.MultiDeref) -> ref i32 {
  52. // CHECK:STDERR: fail_todo_multi_deref.carbon:[[@LINE+4]]:10: error: semantics TODO: `operator* overload sets not implemented yet` [SemanticsTodo]
  53. // CHECK:STDERR: return int_ptr.(Core.CppUnsafeDeref.Op)();
  54. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. // CHECK:STDERR:
  56. return int_ptr.(Core.CppUnsafeDeref.Op)();
  57. }
  58. // --- fail_todo_maybe_deref.carbon
  59. library "[[@TEST_NAME]]";
  60. import Cpp inline '''c++
  61. template<class T, class U>
  62. concept SameImpl = __is_same(T, U);
  63. template<class T, class U>
  64. concept SameAs = SameImpl<T, U> && SameImpl<U, T>;
  65. template<class T>
  66. class MaybeDeref {
  67. public:
  68. auto operator*() const -> int&
  69. requires SameAs<T, int>;
  70. auto operator*() const -> const T&;
  71. };
  72. ''';
  73. fn TestDerefPass(var int_ptr: Cpp.MaybeDeref(i32)) -> ref i32 {
  74. // CHECK:STDERR: fail_todo_maybe_deref.carbon:[[@LINE+4]]:10: error: semantics TODO: `operator* overload sets not implemented yet` [SemanticsTodo]
  75. // CHECK:STDERR: return int_ptr.(Core.CppUnsafeDeref.Op)();
  76. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. // CHECK:STDERR:
  78. return int_ptr.(Core.CppUnsafeDeref.Op)();
  79. }
  80. // --- fail_not_a_ptr.carbon
  81. library "[[@TEST_NAME]]";
  82. import Cpp inline '''c++
  83. class NotAPtr {};
  84. ''';
  85. fn TestDerefFail(not_ptr: Cpp.NotAPtr) {
  86. // CHECK:STDERR: fail_not_a_ptr.carbon:[[@LINE+4]]:3: error: cannot access member of interface `Core.CppUnsafeDeref` in type `Cpp.NotAPtr` that does not implement that interface [MissingImplInMemberAccess]
  87. // CHECK:STDERR: not_ptr.(Core.CppUnsafeDeref.Op)();
  88. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. // CHECK:STDERR:
  90. not_ptr.(Core.CppUnsafeDeref.Op)();
  91. }
  92. // CHECK:STDOUT: --- const_deref.carbon
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: constants {
  95. // CHECK:STDOUT: %ConstDeref: type = class_type @ConstDeref [concrete]
  96. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  97. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  98. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  99. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  100. // CHECK:STDOUT: %CppUnsafeDeref.type: type = facet_type <@CppUnsafeDeref> [concrete]
  101. // CHECK:STDOUT: %Self: %CppUnsafeDeref.type = symbolic_binding Self, 0 [symbolic]
  102. // CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.792: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%Self) [symbolic]
  103. // CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.a80: %CppUnsafeDeref.WithSelf.Op.type.792 = struct_value () [symbolic]
  104. // CHECK:STDOUT: %CppUnsafeDeref.assoc_type: type = assoc_entity_type @CppUnsafeDeref [concrete]
  105. // CHECK:STDOUT: %assoc1: %CppUnsafeDeref.assoc_type = assoc_entity element1, imports.%Core.import_ref.ad7 [concrete]
  106. // CHECK:STDOUT: %ConstDeref.cpp_operator.type: type = fn_type @ConstDeref.cpp_operator [concrete]
  107. // CHECK:STDOUT: %ConstDeref.cpp_operator: %ConstDeref.cpp_operator.type = struct_value () [concrete]
  108. // CHECK:STDOUT: %operator_Star__carbon_thunk.type: type = fn_type @operator_Star__carbon_thunk [concrete]
  109. // CHECK:STDOUT: %operator_Star__carbon_thunk: %operator_Star__carbon_thunk.type = struct_value () [concrete]
  110. // CHECK:STDOUT: %ConstDeref.Op.type: type = fn_type @ConstDeref.Op [concrete]
  111. // CHECK:STDOUT: %ConstDeref.Op: %ConstDeref.Op.type = struct_value () [concrete]
  112. // CHECK:STDOUT: %custom_witness.5a5: <witness> = custom_witness (%i32, %ConstDeref.Op), @CppUnsafeDeref [concrete]
  113. // CHECK:STDOUT: %CppUnsafeDeref.facet.5c0: %CppUnsafeDeref.type = facet_value %ConstDeref, (%custom_witness.5a5) [concrete]
  114. // CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.d8f: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%CppUnsafeDeref.facet.5c0) [concrete]
  115. // CHECK:STDOUT: %.d81: type = fn_type_with_self_type %CppUnsafeDeref.WithSelf.Op.type.d8f, %CppUnsafeDeref.facet.5c0 [concrete]
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: imports {
  119. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  120. // CHECK:STDOUT: .Int = %Core.Int
  121. // CHECK:STDOUT: .CppUnsafeDeref = %Core.CppUnsafeDeref
  122. // CHECK:STDOUT: import Core//prelude
  123. // CHECK:STDOUT: import Core//prelude/...
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  126. // CHECK:STDOUT: %Core.CppUnsafeDeref: type = import_ref Core//prelude/operators/deref, CppUnsafeDeref, loaded [concrete = constants.%CppUnsafeDeref.type]
  127. // CHECK:STDOUT: %Core.import_ref.83d: %CppUnsafeDeref.assoc_type = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [concrete = constants.%assoc1]
  128. // CHECK:STDOUT: %Core.import_ref.ad7: @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op.type (%CppUnsafeDeref.WithSelf.Op.type.792) = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [symbolic = @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op (constants.%CppUnsafeDeref.WithSelf.Op.a80)]
  129. // CHECK:STDOUT: %ConstDeref.cpp_operator.decl: %ConstDeref.cpp_operator.type = fn_decl @ConstDeref.cpp_operator [concrete = constants.%ConstDeref.cpp_operator] {
  130. // CHECK:STDOUT: <elided>
  131. // CHECK:STDOUT: } {
  132. // CHECK:STDOUT: <elided>
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: %operator_Star__carbon_thunk.decl: %operator_Star__carbon_thunk.type = fn_decl @operator_Star__carbon_thunk [concrete = constants.%operator_Star__carbon_thunk] {
  135. // CHECK:STDOUT: <elided>
  136. // CHECK:STDOUT: } {
  137. // CHECK:STDOUT: <elided>
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: fn @TestDerefPass(%int_ptr.param: ref %ConstDeref) -> ref %i32 {
  142. // CHECK:STDOUT: !entry:
  143. // CHECK:STDOUT: %int_ptr.ref: ref %ConstDeref = name_ref int_ptr, %int_ptr
  144. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  145. // CHECK:STDOUT: %CppUnsafeDeref.ref: type = name_ref CppUnsafeDeref, imports.%Core.CppUnsafeDeref [concrete = constants.%CppUnsafeDeref.type]
  146. // CHECK:STDOUT: %Op.ref.loc13_38: %CppUnsafeDeref.assoc_type = name_ref Op, imports.%Core.import_ref.83d [concrete = constants.%assoc1]
  147. // CHECK:STDOUT: %impl_witness_assoc_constant: type = impl_witness_assoc_constant @ConstDeref.cpp_operator.%i32.2 [concrete = constants.%i32]
  148. // CHECK:STDOUT: <elided>
  149. // CHECK:STDOUT: %impl.elem1: %.d81 = impl_witness_access constants.%custom_witness.5a5, element1 [concrete = constants.%ConstDeref.Op]
  150. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_ptr.ref, %impl.elem1
  151. // CHECK:STDOUT: %Op.ref.loc13_43: %ConstDeref.cpp_operator.type = name_ref Op, imports.%ConstDeref.cpp_operator.decl [concrete = constants.%ConstDeref.cpp_operator]
  152. // CHECK:STDOUT: %ConstDeref.cpp_operator.bound: <bound method> = bound_method %int_ptr.ref, %Op.ref.loc13_43
  153. // CHECK:STDOUT: %.loc13: %ConstDeref = acquire_value %int_ptr.ref
  154. // CHECK:STDOUT: %operator_Star__carbon_thunk.call: ref %i32 = call imports.%operator_Star__carbon_thunk.decl(%.loc13)
  155. // CHECK:STDOUT: return %operator_Star__carbon_thunk.call
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: --- mutable_deref.carbon
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: constants {
  161. // CHECK:STDOUT: %MutableDeref: type = class_type @MutableDeref [concrete]
  162. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  163. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  164. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  165. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  166. // CHECK:STDOUT: %CppUnsafeDeref.type: type = facet_type <@CppUnsafeDeref> [concrete]
  167. // CHECK:STDOUT: %Self: %CppUnsafeDeref.type = symbolic_binding Self, 0 [symbolic]
  168. // CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.792: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%Self) [symbolic]
  169. // CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.a80: %CppUnsafeDeref.WithSelf.Op.type.792 = struct_value () [symbolic]
  170. // CHECK:STDOUT: %CppUnsafeDeref.assoc_type: type = assoc_entity_type @CppUnsafeDeref [concrete]
  171. // CHECK:STDOUT: %assoc1: %CppUnsafeDeref.assoc_type = assoc_entity element1, imports.%Core.import_ref.ad7 [concrete]
  172. // CHECK:STDOUT: %MutableDeref.cpp_operator.type: type = fn_type @MutableDeref.cpp_operator [concrete]
  173. // CHECK:STDOUT: %MutableDeref.cpp_operator: %MutableDeref.cpp_operator.type = struct_value () [concrete]
  174. // CHECK:STDOUT: %custom_witness.e20: <witness> = custom_witness (%i32, %MutableDeref.cpp_operator), @CppUnsafeDeref [concrete]
  175. // CHECK:STDOUT: %CppUnsafeDeref.facet.3f6: %CppUnsafeDeref.type = facet_value %MutableDeref, (%custom_witness.e20) [concrete]
  176. // CHECK:STDOUT: %CppUnsafeDeref.WithSelf.Op.type.cfd: type = fn_type @CppUnsafeDeref.WithSelf.Op, @CppUnsafeDeref.WithSelf(%CppUnsafeDeref.facet.3f6) [concrete]
  177. // CHECK:STDOUT: %.c6d: type = fn_type_with_self_type %CppUnsafeDeref.WithSelf.Op.type.cfd, %CppUnsafeDeref.facet.3f6 [concrete]
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: imports {
  181. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  182. // CHECK:STDOUT: .Int = %Core.Int
  183. // CHECK:STDOUT: .CppUnsafeDeref = %Core.CppUnsafeDeref
  184. // CHECK:STDOUT: import Core//prelude
  185. // CHECK:STDOUT: import Core//prelude/...
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  188. // CHECK:STDOUT: %Core.CppUnsafeDeref: type = import_ref Core//prelude/operators/deref, CppUnsafeDeref, loaded [concrete = constants.%CppUnsafeDeref.type]
  189. // CHECK:STDOUT: %Core.import_ref.83d: %CppUnsafeDeref.assoc_type = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [concrete = constants.%assoc1]
  190. // CHECK:STDOUT: %Core.import_ref.ad7: @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op.type (%CppUnsafeDeref.WithSelf.Op.type.792) = import_ref Core//prelude/operators/deref, loc{{\d+_\d+}}, loaded [symbolic = @CppUnsafeDeref.WithSelf.%CppUnsafeDeref.WithSelf.Op (constants.%CppUnsafeDeref.WithSelf.Op.a80)]
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: fn @TestDerefPass(%int_ptr.param: ref %MutableDeref) -> ref %i32 {
  194. // CHECK:STDOUT: !entry:
  195. // CHECK:STDOUT: %int_ptr.ref: ref %MutableDeref = name_ref int_ptr, %int_ptr
  196. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  197. // CHECK:STDOUT: %CppUnsafeDeref.ref: type = name_ref CppUnsafeDeref, imports.%Core.CppUnsafeDeref [concrete = constants.%CppUnsafeDeref.type]
  198. // CHECK:STDOUT: %Op.ref: %CppUnsafeDeref.assoc_type = name_ref Op, imports.%Core.import_ref.83d [concrete = constants.%assoc1]
  199. // CHECK:STDOUT: %impl_witness_assoc_constant: type = impl_witness_assoc_constant @MutableDeref.cpp_operator.%i32.2 [concrete = constants.%i32]
  200. // CHECK:STDOUT: %impl.elem1: %.c6d = impl_witness_access constants.%custom_witness.e20, element1 [concrete = constants.%MutableDeref.cpp_operator]
  201. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_ptr.ref, %impl.elem1
  202. // CHECK:STDOUT: %MutableDeref.cpp_operator.call: ref %i32 = call %bound_method(%int_ptr.ref)
  203. // CHECK:STDOUT: return %MutableDeref.cpp_operator.call
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT: