import.carbon 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/int.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/import.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/import.carbon
  12. // ============================================================================
  13. // Import C++ namespace indirectly
  14. // ============================================================================
  15. // --- namespace.h
  16. namespace MyNamespace {
  17. class MyClass {};
  18. } // namespace MyNamespace
  19. // --- namespace_api.carbon
  20. library "[[@TEST_NAME]]";
  21. import Cpp library "namespace.h";
  22. alias MyNamespaceAlias = Cpp.MyNamespace;
  23. // --- fail_import_namespace_api.carbon
  24. library "[[@TEST_NAME]]";
  25. import library "namespace_api";
  26. fn F() {
  27. // Imports are not implicitly re-exported, so lookup is expected to fail.
  28. // CHECK:STDERR: fail_import_namespace_api.carbon:[[@LINE+4]]:10: error: member name `MyClass` not found in `Cpp.MyNamespace` [MemberNameNotFoundInInstScope]
  29. // CHECK:STDERR: var x: MyNamespaceAlias.MyClass;
  30. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  31. // CHECK:STDERR:
  32. var x: MyNamespaceAlias.MyClass;
  33. }
  34. // ============================================================================
  35. // Import C++ struct indirectly
  36. // ============================================================================
  37. // --- struct.h
  38. struct MyStruct { void Foo(); };
  39. // --- struct_api.carbon
  40. library "[[@TEST_NAME]]";
  41. import Cpp library "struct.h";
  42. alias MyStructAlias = Cpp.MyStruct;
  43. // --- fail_todo_import_struct_api.carbon
  44. // CHECK:STDERR: fail_todo_import_struct_api.carbon: error: semantics TODO: `Unsupported: Importing C++ indirectly` [SemanticsTodo]
  45. // CHECK:STDERR:
  46. library "[[@TEST_NAME]]";
  47. import library "struct_api";
  48. fn F() {
  49. //@dump-sem-ir-begin
  50. var x: MyStructAlias;
  51. x.Foo();
  52. //@dump-sem-ir-end
  53. }
  54. // ============================================================================
  55. // Import C++ function indirectly
  56. // ============================================================================
  57. // --- function.h
  58. auto foo_short(short x) -> void;
  59. auto foo_int(int x) -> void;
  60. // --- function_api.carbon
  61. library "[[@TEST_NAME]]";
  62. import Cpp library "function.h";
  63. alias FooShort = Cpp.foo_short;
  64. alias FooInt = Cpp.foo_int;
  65. // --- fail_todo_import_function_api.carbon
  66. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_short` indirectly` [SemanticsTodo]
  67. // CHECK:STDERR:
  68. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_short` indirectly` [SemanticsTodo]
  69. // CHECK:STDERR:
  70. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_int` indirectly` [SemanticsTodo]
  71. // CHECK:STDERR:
  72. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_int` indirectly` [SemanticsTodo]
  73. // CHECK:STDERR:
  74. library "[[@TEST_NAME]]";
  75. import library "function_api";
  76. fn F() {
  77. //@dump-sem-ir-begin
  78. FooShort(8 as i16);
  79. FooInt(9);
  80. //@dump-sem-ir-end
  81. }
  82. // ============================================================================
  83. // Import Cpp without a header
  84. // ============================================================================
  85. // --- no_header.carbon
  86. library "[[@TEST_NAME]]";
  87. import Cpp;
  88. fn F(input: Cpp.int) {
  89. let output: i32 = input;
  90. }
  91. // ============================================================================
  92. // Import Cpp without a header, with a header and inline
  93. // ============================================================================
  94. // --- header_function.h
  95. auto MyHeaderFunction(int) -> void;
  96. // --- no_header_with_header_inline.carbon
  97. library "[[@TEST_NAME]]";
  98. import Cpp;
  99. import Cpp library "header_function.h";
  100. import Cpp inline '''
  101. auto MyInlineFunction(short) -> void;
  102. ''';
  103. fn F() {
  104. Cpp.MyHeaderFunction(8 as Cpp.int);
  105. Cpp.MyInlineFunction(9 as Cpp.short);
  106. }
  107. // CHECK:STDOUT: --- fail_todo_import_struct_api.carbon
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: constants {
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: imports {
  113. // CHECK:STDOUT: %Main.MyStructAlias: type = import_ref Main//struct_api, MyStructAlias, loaded [concrete = <error>]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @F() {
  117. // CHECK:STDOUT: !entry:
  118. // CHECK:STDOUT: name_binding_decl {
  119. // CHECK:STDOUT: %x.patt: <error> = ref_binding_pattern x [concrete]
  120. // CHECK:STDOUT: %x.var_patt: <error> = var_pattern %x.patt [concrete]
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: %x.var: ref <error> = var %x.var_patt [concrete = <error>]
  123. // CHECK:STDOUT: %MyStructAlias.ref: type = name_ref MyStructAlias, imports.%Main.MyStructAlias [concrete = <error>]
  124. // CHECK:STDOUT: %x: ref <error> = ref_binding x, <error> [concrete = <error>]
  125. // CHECK:STDOUT: %x.ref: ref <error> = name_ref x, %x [concrete = <error>]
  126. // CHECK:STDOUT: %Foo.ref: <error> = name_ref Foo, <error> [concrete = <error>]
  127. // CHECK:STDOUT: <elided>
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: --- fail_todo_import_function_api.carbon
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: constants {
  133. // CHECK:STDOUT: %int_8.b85: Core.IntLiteral = int_value 8 [concrete]
  134. // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete]
  135. // CHECK:STDOUT: %i16: type = class_type @Int, @Int(%int_16) [concrete]
  136. // CHECK:STDOUT: %As.type.771: type = facet_type <@As, @As(%i16)> [concrete]
  137. // CHECK:STDOUT: %As.Convert.type.be5: type = fn_type @As.Convert, @As(%i16) [concrete]
  138. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  139. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.0fd: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%To) [symbolic]
  140. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.2b1: %Core.IntLiteral.as.As.impl.Convert.type.0fd = struct_value () [symbolic]
  141. // CHECK:STDOUT: %As.impl_witness.c72: <witness> = impl_witness imports.%As.impl_witness_table.251, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  142. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.2f9: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  143. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.228: %Core.IntLiteral.as.As.impl.Convert.type.2f9 = struct_value () [concrete]
  144. // CHECK:STDOUT: %As.facet: %As.type.771 = facet_value Core.IntLiteral, (%As.impl_witness.c72) [concrete]
  145. // CHECK:STDOUT: %.28a: type = fn_type_with_self_type %As.Convert.type.be5, %As.facet [concrete]
  146. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.bound: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.228 [concrete]
  147. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.As.impl.Convert.228, @Core.IntLiteral.as.As.impl.Convert(%int_16) [concrete]
  148. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.specific_fn [concrete]
  149. // CHECK:STDOUT: %int_8.823: %i16 = int_value 8 [concrete]
  150. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete]
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: imports {
  154. // CHECK:STDOUT: %Main.FooShort: <error> = import_ref Main//function_api, FooShort, loaded [concrete = <error>]
  155. // CHECK:STDOUT: %Main.FooInt: <error> = import_ref Main//function_api, FooInt, loaded [concrete = <error>]
  156. // CHECK:STDOUT: %Core.import_ref.05d: @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert.type (%Core.IntLiteral.as.As.impl.Convert.type.0fd) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert (constants.%Core.IntLiteral.as.As.impl.Convert.2b1)]
  157. // CHECK:STDOUT: %As.impl_witness_table.251 = impl_witness_table (%Core.import_ref.05d), @Core.IntLiteral.as.As.impl [concrete]
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: fn @F() {
  161. // CHECK:STDOUT: !entry:
  162. // CHECK:STDOUT: %FooShort.ref: <error> = name_ref FooShort, imports.%Main.FooShort [concrete = <error>]
  163. // CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  164. // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
  165. // CHECK:STDOUT: %i16: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
  166. // CHECK:STDOUT: %impl.elem0: %.28a = impl_witness_access constants.%As.impl_witness.c72, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.228]
  167. // CHECK:STDOUT: %bound_method.loc16_14.1: <bound method> = bound_method %int_8, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
  168. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Core.IntLiteral.as.As.impl.Convert(constants.%int_16) [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.specific_fn]
  169. // CHECK:STDOUT: %bound_method.loc16_14.2: <bound method> = bound_method %int_8, %specific_fn [concrete = constants.%bound_method]
  170. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc16_14.2(%int_8) [concrete = constants.%int_8.823]
  171. // CHECK:STDOUT: %.loc16_14.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_8.823]
  172. // CHECK:STDOUT: %.loc16_14.2: %i16 = converted %int_8, %.loc16_14.1 [concrete = constants.%int_8.823]
  173. // CHECK:STDOUT: %FooInt.ref: <error> = name_ref FooInt, imports.%Main.FooInt [concrete = <error>]
  174. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete = constants.%int_9]
  175. // CHECK:STDOUT: <elided>
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT: