import.carbon 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_todo_import_namespace_api.carbon
  24. library "[[@TEST_NAME]]";
  25. import library "namespace_api";
  26. fn F() {
  27. //@dump-sem-ir-begin
  28. // CHECK:STDERR: fail_todo_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. //@dump-sem-ir-end
  34. }
  35. // ============================================================================
  36. // Import C++ function indirectly
  37. // ============================================================================
  38. // --- function.h
  39. auto foo_short(short x) -> void;
  40. auto foo_int(int x) -> void;
  41. // --- function_api.carbon
  42. library "[[@TEST_NAME]]";
  43. import Cpp library "function.h";
  44. alias FooShort = Cpp.foo_short;
  45. alias FooInt = Cpp.foo_int;
  46. // --- fail_todo_import_function_api.carbon
  47. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_short` indirectly` [SemanticsTodo]
  48. // CHECK:STDERR:
  49. // CHECK:STDERR: fail_todo_import_function_api.carbon: error: semantics TODO: `Unsupported: Importing C++ function `foo_int` indirectly` [SemanticsTodo]
  50. // CHECK:STDERR:
  51. library "[[@TEST_NAME]]";
  52. import library "function_api";
  53. fn F() {
  54. //@dump-sem-ir-begin
  55. FooShort(8 as i16);
  56. FooInt(9);
  57. //@dump-sem-ir-end
  58. }
  59. // CHECK:STDOUT: --- fail_todo_import_namespace_api.carbon
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: constants {
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: imports {
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: fn @F() {
  68. // CHECK:STDOUT: !entry:
  69. // CHECK:STDOUT: name_binding_decl {
  70. // CHECK:STDOUT: %x.patt: <error> = binding_pattern x [concrete]
  71. // CHECK:STDOUT: %x.var_patt: <error> = var_pattern %x.patt [concrete]
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %x.var: ref <error> = var %x.var_patt [concrete = <error>]
  74. // CHECK:STDOUT: <elided>
  75. // CHECK:STDOUT: %x: <error> = bind_name x, <error> [concrete = <error>]
  76. // CHECK:STDOUT: <elided>
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: --- fail_todo_import_function_api.carbon
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: constants {
  82. // CHECK:STDOUT: %int_8.b85: Core.IntLiteral = int_value 8 [concrete]
  83. // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete]
  84. // CHECK:STDOUT: %i16: type = class_type @Int, @Int(%int_16) [concrete]
  85. // CHECK:STDOUT: %As.type.771: type = facet_type <@As, @As(%i16)> [concrete]
  86. // CHECK:STDOUT: %As.Convert.type.be5: type = fn_type @As.Convert, @As(%i16) [concrete]
  87. // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  88. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.565: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%To) [symbolic]
  89. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.d2c: %Core.IntLiteral.as.As.impl.Convert.type.565 = struct_value () [symbolic]
  90. // CHECK:STDOUT: %As.impl_witness.2d2: <witness> = impl_witness imports.%As.impl_witness_table.5ad, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  91. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.38a: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  92. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.97a: %Core.IntLiteral.as.As.impl.Convert.type.38a = struct_value () [concrete]
  93. // CHECK:STDOUT: %As.facet: %As.type.771 = facet_value Core.IntLiteral, (%As.impl_witness.2d2) [concrete]
  94. // CHECK:STDOUT: %.026: type = fn_type_with_self_type %As.Convert.type.be5, %As.facet [concrete]
  95. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.bound: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.97a [concrete]
  96. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.As.impl.Convert.97a, @Core.IntLiteral.as.As.impl.Convert(%int_16) [concrete]
  97. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_8.b85, %Core.IntLiteral.as.As.impl.Convert.specific_fn [concrete]
  98. // CHECK:STDOUT: %int_8.823: %i16 = int_value 8 [concrete]
  99. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete]
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: imports {
  103. // CHECK:STDOUT: %Main.FooShort: <error> = import_ref Main//function_api, FooShort, loaded [concrete = <error>]
  104. // CHECK:STDOUT: %Main.FooInt: <error> = import_ref Main//function_api, FooInt, loaded [concrete = <error>]
  105. // CHECK:STDOUT: %Core.import_ref.99c: @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert.type (%Core.IntLiteral.as.As.impl.Convert.type.565) = import_ref Core//prelude/parts/int, loc32_39, loaded [symbolic = @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert (constants.%Core.IntLiteral.as.As.impl.Convert.d2c)]
  106. // CHECK:STDOUT: %As.impl_witness_table.5ad = impl_witness_table (%Core.import_ref.99c), @Core.IntLiteral.as.As.impl [concrete]
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: fn @F() {
  110. // CHECK:STDOUT: !entry:
  111. // CHECK:STDOUT: %FooShort.ref: <error> = name_ref FooShort, imports.%Main.FooShort [concrete = <error>]
  112. // CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  113. // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
  114. // CHECK:STDOUT: %i16: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
  115. // CHECK:STDOUT: %impl.elem0: %.026 = impl_witness_access constants.%As.impl_witness.2d2, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.97a]
  116. // CHECK:STDOUT: %bound_method.loc12_14.1: <bound method> = bound_method %int_8, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
  117. // 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]
  118. // CHECK:STDOUT: %bound_method.loc12_14.2: <bound method> = bound_method %int_8, %specific_fn [concrete = constants.%bound_method]
  119. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc12_14.2(%int_8) [concrete = constants.%int_8.823]
  120. // CHECK:STDOUT: %.loc12_14.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_8.823]
  121. // CHECK:STDOUT: %.loc12_14.2: %i16 = converted %int_8, %.loc12_14.1 [concrete = constants.%int_8.823]
  122. // CHECK:STDOUT: %FooInt.ref: <error> = name_ref FooInt, imports.%Main.FooInt [concrete = <error>]
  123. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete = constants.%int_9]
  124. // CHECK:STDOUT: <elided>
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: