import.carbon 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // --- todo_import_function_api.carbon
  47. library "[[@TEST_NAME]]";
  48. import library "function_api";
  49. // TODO: Fix this test as a follow-up of https://github.com/carbon-language/carbon-lang/pull/5891.
  50. fn F() {
  51. //@dump-sem-ir-begin
  52. // FooShort(8 as i16);
  53. // FooInt(9);
  54. //@dump-sem-ir-end
  55. }
  56. // CHECK:STDOUT: --- fail_todo_import_namespace_api.carbon
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: constants {
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: imports {
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: fn @F() {
  65. // CHECK:STDOUT: !entry:
  66. // CHECK:STDOUT: name_binding_decl {
  67. // CHECK:STDOUT: %x.patt: <error> = binding_pattern x [concrete]
  68. // CHECK:STDOUT: %x.var_patt: <error> = var_pattern %x.patt [concrete]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %x.var: ref <error> = var %x.var_patt [concrete = <error>]
  71. // CHECK:STDOUT: <elided>
  72. // CHECK:STDOUT: %x: <error> = bind_name x, <error> [concrete = <error>]
  73. // CHECK:STDOUT: <elided>
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: --- todo_import_function_api.carbon
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: constants {
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: imports {
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: fn @F() {
  85. // CHECK:STDOUT: !entry:
  86. // CHECK:STDOUT: <elided>
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: