method.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/convert.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/check/testdata/interop/cpp/class/method.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/method.carbon
  12. // --- object_param_qualifiers.h
  13. struct HasQualifiers {
  14. void plain();
  15. void const_this() const;
  16. void volatile_this() volatile;
  17. void ref_this() &;
  18. void const_ref_this() const&;
  19. void ref_ref_this() &&;
  20. void const_ref_ref_this() const&&;
  21. };
  22. // --- use_object_param_qualifiers.carbon
  23. library "[[@TEST_NAME]]";
  24. import Cpp library "object_param_qualifiers.h";
  25. fn F(v: Cpp.HasQualifiers, p: Cpp.HasQualifiers*) {
  26. //@dump-sem-ir-begin
  27. v.const_this();
  28. v.const_ref_this();
  29. p->plain();
  30. p->ref_this();
  31. p->const_this();
  32. p->const_ref_this();
  33. //@dump-sem-ir-end
  34. }
  35. // --- fail_bad_object_param_qualifiers_by_value.carbon
  36. library "[[@TEST_NAME]]";
  37. import Cpp library "object_param_qualifiers.h";
  38. fn Value(v: Cpp.HasQualifiers) {
  39. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+5]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  40. // CHECK:STDERR: v.plain();
  41. // CHECK:STDERR: ^
  42. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon: note: initializing function parameter [InCallToFunctionParam]
  43. // CHECK:STDERR:
  44. v.plain();
  45. // TODO: This should remain invalid once we support `volatile`.
  46. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+12]]:3: error: semantics TODO: `Unsupported: qualified type: volatile struct HasQualifiers` [SemanticsTodo]
  47. // CHECK:STDERR: v.volatile_this();
  48. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  49. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+9]]:3: note: in `Cpp` name lookup for `volatile_this` [InCppNameLookup]
  50. // CHECK:STDERR: v.volatile_this();
  51. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  52. // CHECK:STDERR:
  53. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+5]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  54. // CHECK:STDERR: v.volatile_this();
  55. // CHECK:STDERR: ^
  56. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon: note: initializing function parameter [InCallToFunctionParam]
  57. // CHECK:STDERR:
  58. v.volatile_this();
  59. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+5]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  60. // CHECK:STDERR: v.ref_this();
  61. // CHECK:STDERR: ^
  62. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon: note: initializing function parameter [InCallToFunctionParam]
  63. // CHECK:STDERR:
  64. v.ref_this();
  65. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: object parameter type: struct HasQualifiers &&` [SemanticsTodo]
  66. // CHECK:STDERR: v.ref_ref_this();
  67. // CHECK:STDERR: ^~~~~~~~~~~~~~
  68. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `ref_ref_this` [InCppNameLookup]
  69. // CHECK:STDERR: v.ref_ref_this();
  70. // CHECK:STDERR: ^~~~~~~~~~~~~~
  71. // CHECK:STDERR:
  72. v.ref_ref_this();
  73. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: object parameter type: const struct HasQualifiers &&` [SemanticsTodo]
  74. // CHECK:STDERR: v.const_ref_ref_this();
  75. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  76. // CHECK:STDERR: fail_bad_object_param_qualifiers_by_value.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `const_ref_ref_this` [InCppNameLookup]
  77. // CHECK:STDERR: v.const_ref_ref_this();
  78. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  79. // CHECK:STDERR:
  80. v.const_ref_ref_this();
  81. }
  82. // --- fail_todo_bad_object_param_qualifiers_by_ref.carbon
  83. library "[[@TEST_NAME]]";
  84. import Cpp library "object_param_qualifiers.h";
  85. fn Ref(p: Cpp.HasQualifiers*) {
  86. // TODO: This should eventually be accepted if we support `volatile`.
  87. // CHECK:STDERR: fail_todo_bad_object_param_qualifiers_by_ref.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: qualified type: volatile struct HasQualifiers` [SemanticsTodo]
  88. // CHECK:STDERR: p->volatile_this();
  89. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  90. // CHECK:STDERR: fail_todo_bad_object_param_qualifiers_by_ref.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `volatile_this` [InCppNameLookup]
  91. // CHECK:STDERR: p->volatile_this();
  92. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  93. // CHECK:STDERR:
  94. p->volatile_this();
  95. }
  96. // --- fail_bad_object_param_qualifiers_ref_ref.carbon
  97. library "[[@TEST_NAME]]";
  98. import Cpp library "object_param_qualifiers.h";
  99. fn Ref(p: Cpp.HasQualifiers*) {
  100. // CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: object parameter type: struct HasQualifiers &&` [SemanticsTodo]
  101. // CHECK:STDERR: p->ref_ref_this();
  102. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  103. // CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `ref_ref_this` [InCppNameLookup]
  104. // CHECK:STDERR: p->ref_ref_this();
  105. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  106. // CHECK:STDERR:
  107. p->ref_ref_this();
  108. // CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: object parameter type: const struct HasQualifiers &&` [SemanticsTodo]
  109. // CHECK:STDERR: p->const_ref_ref_this();
  110. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  111. // CHECK:STDERR: fail_bad_object_param_qualifiers_ref_ref.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `const_ref_ref_this` [InCppNameLookup]
  112. // CHECK:STDERR: p->const_ref_ref_this();
  113. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  114. // CHECK:STDERR:
  115. p->const_ref_ref_this();
  116. }
  117. // CHECK:STDOUT: --- use_object_param_qualifiers.carbon
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: constants {
  120. // CHECK:STDOUT: %HasQualifiers: type = class_type @HasQualifiers [concrete]
  121. // CHECK:STDOUT: %ptr.ec3: type = ptr_type %HasQualifiers [concrete]
  122. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  123. // CHECK:STDOUT: %HasQualifiers.const_this.type: type = fn_type @HasQualifiers.const_this [concrete]
  124. // CHECK:STDOUT: %HasQualifiers.const_this: %HasQualifiers.const_this.type = struct_value () [concrete]
  125. // CHECK:STDOUT: %HasQualifiers.const_ref_this.type: type = fn_type @HasQualifiers.const_ref_this [concrete]
  126. // CHECK:STDOUT: %HasQualifiers.const_ref_this: %HasQualifiers.const_ref_this.type = struct_value () [concrete]
  127. // CHECK:STDOUT: %HasQualifiers.plain.type: type = fn_type @HasQualifiers.plain [concrete]
  128. // CHECK:STDOUT: %HasQualifiers.plain: %HasQualifiers.plain.type = struct_value () [concrete]
  129. // CHECK:STDOUT: %HasQualifiers.ref_this.type: type = fn_type @HasQualifiers.ref_this [concrete]
  130. // CHECK:STDOUT: %HasQualifiers.ref_this: %HasQualifiers.ref_this.type = struct_value () [concrete]
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: imports {
  134. // CHECK:STDOUT: %HasQualifiers.const_this.decl: %HasQualifiers.const_this.type = fn_decl @HasQualifiers.const_this [concrete = constants.%HasQualifiers.const_this] {
  135. // CHECK:STDOUT: <elided>
  136. // CHECK:STDOUT: } {
  137. // CHECK:STDOUT: <elided>
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: %HasQualifiers.const_ref_this.decl: %HasQualifiers.const_ref_this.type = fn_decl @HasQualifiers.const_ref_this [concrete = constants.%HasQualifiers.const_ref_this] {
  140. // CHECK:STDOUT: <elided>
  141. // CHECK:STDOUT: } {
  142. // CHECK:STDOUT: <elided>
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: %HasQualifiers.plain.decl: %HasQualifiers.plain.type = fn_decl @HasQualifiers.plain [concrete = constants.%HasQualifiers.plain] {
  145. // CHECK:STDOUT: <elided>
  146. // CHECK:STDOUT: } {
  147. // CHECK:STDOUT: <elided>
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: %HasQualifiers.ref_this.decl: %HasQualifiers.ref_this.type = fn_decl @HasQualifiers.ref_this [concrete = constants.%HasQualifiers.ref_this] {
  150. // CHECK:STDOUT: <elided>
  151. // CHECK:STDOUT: } {
  152. // CHECK:STDOUT: <elided>
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn @F(%v.param: %HasQualifiers, %p.param: %ptr.ec3) {
  157. // CHECK:STDOUT: !entry:
  158. // CHECK:STDOUT: %v.ref.loc8: %HasQualifiers = name_ref v, %v
  159. // CHECK:STDOUT: %const_this.ref.loc8: %HasQualifiers.const_this.type = name_ref const_this, imports.%HasQualifiers.const_this.decl [concrete = constants.%HasQualifiers.const_this]
  160. // CHECK:STDOUT: %HasQualifiers.const_this.bound.loc8: <bound method> = bound_method %v.ref.loc8, %const_this.ref.loc8
  161. // CHECK:STDOUT: %HasQualifiers.const_this.call.loc8: init %empty_tuple.type = call %HasQualifiers.const_this.bound.loc8(%v.ref.loc8)
  162. // CHECK:STDOUT: %v.ref.loc9: %HasQualifiers = name_ref v, %v
  163. // CHECK:STDOUT: %const_ref_this.ref.loc9: %HasQualifiers.const_ref_this.type = name_ref const_ref_this, imports.%HasQualifiers.const_ref_this.decl [concrete = constants.%HasQualifiers.const_ref_this]
  164. // CHECK:STDOUT: %HasQualifiers.const_ref_this.bound.loc9: <bound method> = bound_method %v.ref.loc9, %const_ref_this.ref.loc9
  165. // CHECK:STDOUT: %HasQualifiers.const_ref_this.call.loc9: init %empty_tuple.type = call %HasQualifiers.const_ref_this.bound.loc9(%v.ref.loc9)
  166. // CHECK:STDOUT: %p.ref.loc11: %ptr.ec3 = name_ref p, %p
  167. // CHECK:STDOUT: %.loc11: ref %HasQualifiers = deref %p.ref.loc11
  168. // CHECK:STDOUT: %plain.ref: %HasQualifiers.plain.type = name_ref plain, imports.%HasQualifiers.plain.decl [concrete = constants.%HasQualifiers.plain]
  169. // CHECK:STDOUT: %HasQualifiers.plain.bound: <bound method> = bound_method %.loc11, %plain.ref
  170. // CHECK:STDOUT: %addr.loc11: %ptr.ec3 = addr_of %.loc11
  171. // CHECK:STDOUT: %HasQualifiers.plain.call: init %empty_tuple.type = call %HasQualifiers.plain.bound(%addr.loc11)
  172. // CHECK:STDOUT: %p.ref.loc12: %ptr.ec3 = name_ref p, %p
  173. // CHECK:STDOUT: %.loc12: ref %HasQualifiers = deref %p.ref.loc12
  174. // CHECK:STDOUT: %ref_this.ref: %HasQualifiers.ref_this.type = name_ref ref_this, imports.%HasQualifiers.ref_this.decl [concrete = constants.%HasQualifiers.ref_this]
  175. // CHECK:STDOUT: %HasQualifiers.ref_this.bound: <bound method> = bound_method %.loc12, %ref_this.ref
  176. // CHECK:STDOUT: %addr.loc12: %ptr.ec3 = addr_of %.loc12
  177. // CHECK:STDOUT: %HasQualifiers.ref_this.call: init %empty_tuple.type = call %HasQualifiers.ref_this.bound(%addr.loc12)
  178. // CHECK:STDOUT: %p.ref.loc13: %ptr.ec3 = name_ref p, %p
  179. // CHECK:STDOUT: %.loc13_4.1: ref %HasQualifiers = deref %p.ref.loc13
  180. // CHECK:STDOUT: %const_this.ref.loc13: %HasQualifiers.const_this.type = name_ref const_this, imports.%HasQualifiers.const_this.decl [concrete = constants.%HasQualifiers.const_this]
  181. // CHECK:STDOUT: %HasQualifiers.const_this.bound.loc13: <bound method> = bound_method %.loc13_4.1, %const_this.ref.loc13
  182. // CHECK:STDOUT: %.loc13_4.2: %HasQualifiers = bind_value %.loc13_4.1
  183. // CHECK:STDOUT: %HasQualifiers.const_this.call.loc13: init %empty_tuple.type = call %HasQualifiers.const_this.bound.loc13(%.loc13_4.2)
  184. // CHECK:STDOUT: %p.ref.loc14: %ptr.ec3 = name_ref p, %p
  185. // CHECK:STDOUT: %.loc14_4.1: ref %HasQualifiers = deref %p.ref.loc14
  186. // CHECK:STDOUT: %const_ref_this.ref.loc14: %HasQualifiers.const_ref_this.type = name_ref const_ref_this, imports.%HasQualifiers.const_ref_this.decl [concrete = constants.%HasQualifiers.const_ref_this]
  187. // CHECK:STDOUT: %HasQualifiers.const_ref_this.bound.loc14: <bound method> = bound_method %.loc14_4.1, %const_ref_this.ref.loc14
  188. // CHECK:STDOUT: %.loc14_4.2: %HasQualifiers = bind_value %.loc14_4.1
  189. // CHECK:STDOUT: %HasQualifiers.const_ref_this.call.loc14: init %empty_tuple.type = call %HasQualifiers.const_ref_this.bound.loc14(%.loc14_4.2)
  190. // CHECK:STDOUT: <elided>
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT: