import.carbon 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/basics/import/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/basics/import/import.carbon
  12. // ============================================================================
  13. // Import C++ struct indirectly
  14. // ============================================================================
  15. // --- struct.h
  16. struct MyStruct { void Foo(); };
  17. // --- struct_api.carbon
  18. library "[[@TEST_NAME]]";
  19. import Cpp library "struct.h";
  20. alias MyStructAlias = Cpp.MyStruct;
  21. // --- fail_todo_import_struct_api.carbon
  22. library "[[@TEST_NAME]]";
  23. // CHECK:STDERR: fail_todo_import_struct_api.carbon:[[@LINE+6]]:1: in import [InImport]
  24. // CHECK:STDERR: struct_api.carbon:4:10: in file included here [InCppInclude]
  25. // CHECK:STDERR: ./struct.h:2:8: error: semantics TODO: `indirect import of C++ declaration with no direct Cpp import` [SemanticsTodo]
  26. // CHECK:STDERR: struct MyStruct { void Foo(); };
  27. // CHECK:STDERR: ^
  28. // CHECK:STDERR:
  29. import library "struct_api";
  30. fn F() {
  31. //@dump-sem-ir-begin
  32. var x: MyStructAlias;
  33. x.Foo();
  34. //@dump-sem-ir-end
  35. }
  36. // ============================================================================
  37. // Import C++ function indirectly
  38. // ============================================================================
  39. // --- function.h
  40. auto foo_short(short x) -> void;
  41. auto foo_int(int x) -> void;
  42. // --- function_api.carbon
  43. library "[[@TEST_NAME]]";
  44. import Cpp library "function.h";
  45. alias FooShort = Cpp.foo_short;
  46. alias FooInt = Cpp.foo_int;
  47. // --- fail_todo_import_function_api.carbon
  48. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_short` indirectly` [SemanticsTodo]
  49. // CHECK:STDERR:
  50. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_short` indirectly` [SemanticsTodo]
  51. // CHECK:STDERR:
  52. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_int` indirectly` [SemanticsTodo]
  53. // CHECK:STDERR:
  54. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_int` indirectly` [SemanticsTodo]
  55. // CHECK:STDERR:
  56. library "[[@TEST_NAME]]";
  57. import library "function_api";
  58. fn F() {
  59. //@dump-sem-ir-begin
  60. FooShort(8 as i16);
  61. FooInt(9);
  62. //@dump-sem-ir-end
  63. }
  64. // ============================================================================
  65. // Import Cpp without a header
  66. // ============================================================================
  67. // --- no_header.carbon
  68. library "[[@TEST_NAME]]";
  69. import Cpp;
  70. fn F(input: Cpp.int) {
  71. let unused output: i32 = input;
  72. }
  73. // ============================================================================
  74. // Import Cpp without a header, with a header and inline
  75. // ============================================================================
  76. // --- header_function.h
  77. auto MyHeaderFunction(int) -> void;
  78. // --- no_header_with_header_inline.carbon
  79. library "[[@TEST_NAME]]";
  80. import Cpp;
  81. import Cpp library "header_function.h";
  82. import Cpp inline '''
  83. auto MyInlineFunction(short) -> void;
  84. ''';
  85. fn F() {
  86. Cpp.MyHeaderFunction(8 as Cpp.int);
  87. Cpp.MyInlineFunction(9 as Cpp.short);
  88. }
  89. // CHECK:STDOUT: --- fail_todo_import_struct_api.carbon
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: imports {
  92. // CHECK:STDOUT: %Main.MyStructAlias: type = import_ref Main//struct_api, MyStructAlias, loaded [concrete = <error>]
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: fn @F() {
  96. // CHECK:STDOUT: !entry:
  97. // CHECK:STDOUT: name_binding_decl {
  98. // CHECK:STDOUT: %x.patt: <error> = ref_binding_pattern x [concrete]
  99. // CHECK:STDOUT: %x.var_patt: <error> = var_pattern %x.patt [concrete]
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %x.var: ref <error> = var %x.var_patt [concrete = <error>]
  102. // CHECK:STDOUT: assign %x.var, <error>
  103. // CHECK:STDOUT: %MyStructAlias.ref: type = name_ref MyStructAlias, imports.%Main.MyStructAlias [concrete = <error>]
  104. // CHECK:STDOUT: %x: ref <error> = ref_binding x, <error> [concrete = <error>]
  105. // CHECK:STDOUT: %x.ref: ref <error> = name_ref x, %x [concrete = <error>]
  106. // CHECK:STDOUT: %Foo.ref: <error> = name_ref Foo, <error> [concrete = <error>]
  107. // CHECK:STDOUT: <elided>
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: --- fail_todo_import_function_api.carbon
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: constants {
  113. // CHECK:STDOUT: %int_8.b85: Core.IntLiteral = int_value 8 [concrete]
  114. // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete]
  115. // CHECK:STDOUT: %i16: type = class_type @Int, @Int(%int_16) [concrete]
  116. // CHECK:STDOUT: %As.type.359: type = facet_type <@As, @As(%i16)> [concrete]
  117. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  118. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.09e: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%To) [symbolic]
  119. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.dbe: %Core.IntLiteral.as.As.impl.Convert.type.09e = struct_value () [symbolic]
  120. // CHECK:STDOUT: %As.impl_witness.b61: <witness> = impl_witness imports.%As.impl_witness_table.9fc, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  121. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.c60: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  122. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.a42: %Core.IntLiteral.as.As.impl.Convert.type.c60 = struct_value () [concrete]
  123. // CHECK:STDOUT: %As.facet: %As.type.359 = facet_value Core.IntLiteral, (%As.impl_witness.b61) [concrete]
  124. // CHECK:STDOUT: %As.WithSelf.Convert.type.2c0: type = fn_type @As.WithSelf.Convert, @As.WithSelf(%i16, %As.facet) [concrete]
  125. // CHECK:STDOUT: %.70c: type = fn_type_with_self_type %As.WithSelf.Convert.type.2c0, %As.facet [concrete]
  126. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.bound: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.a42 [concrete]
  127. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.As.impl.Convert.a42, @Core.IntLiteral.as.As.impl.Convert(%int_16) [concrete]
  128. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.specific_fn [concrete]
  129. // CHECK:STDOUT: %int_8.823: %i16 = int_value 8 [concrete]
  130. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete]
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: imports {
  134. // CHECK:STDOUT: %Main.FooShort: <error> = import_ref Main//function_api, FooShort, loaded [concrete = <error>]
  135. // CHECK:STDOUT: %Main.FooInt: <error> = import_ref Main//function_api, FooInt, loaded [concrete = <error>]
  136. // CHECK:STDOUT: %Core.import_ref.ca0: @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert.type (%Core.IntLiteral.as.As.impl.Convert.type.09e) = 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.dbe)]
  137. // CHECK:STDOUT: %As.impl_witness_table.9fc = impl_witness_table (%Core.import_ref.ca0), @Core.IntLiteral.as.As.impl [concrete]
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: fn @F() {
  141. // CHECK:STDOUT: !entry:
  142. // CHECK:STDOUT: %FooShort.ref: <error> = name_ref FooShort, imports.%Main.FooShort [concrete = <error>]
  143. // CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  144. // CHECK:STDOUT: %i16: type = type_literal constants.%i16 [concrete = constants.%i16]
  145. // CHECK:STDOUT: %impl.elem0: %.70c = impl_witness_access constants.%As.impl_witness.b61, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.a42]
  146. // CHECK:STDOUT: %bound_method.loc16_14.1: <bound method> = bound_method %int_8, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
  147. // 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]
  148. // CHECK:STDOUT: %bound_method.loc16_14.2: <bound method> = bound_method %int_8, %specific_fn [concrete = constants.%bound_method]
  149. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc16_14.2(%int_8) [concrete = constants.%int_8.823]
  150. // CHECK:STDOUT: %.loc16_14.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_8.823]
  151. // CHECK:STDOUT: %.loc16_14.2: %i16 = converted %int_8, %.loc16_14.1 [concrete = constants.%int_8.823]
  152. // CHECK:STDOUT: %FooInt.ref: <error> = name_ref FooInt, imports.%Main.FooInt [concrete = <error>]
  153. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete = constants.%int_9]
  154. // CHECK:STDOUT: <elided>
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: