string_view.carbon 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/primitives.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/stdlib/string_view.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/stdlib/string_view.carbon
  12. // TODO: Tests marked as `fail_todo_5891_` to fixed as a follow-up of https://github.com/carbon-language/carbon-lang/pull/5891.
  13. // --- string_view.h
  14. namespace std {
  15. using size_t = __SIZE_TYPE__;
  16. inline namespace __1 {
  17. template<typename T> struct char_traits {};
  18. template<typename CharT, typename Traits = char_traits<CharT>>
  19. class basic_string_view {
  20. public:
  21. basic_string_view() = default;
  22. size_t size() const { return size_; }
  23. private:
  24. const CharT* data_;
  25. size_t size_;
  26. };
  27. using string_view = basic_string_view<char>;
  28. }
  29. }
  30. auto Consume(std::string_view sv) -> void;
  31. auto Produce() -> std::string_view;
  32. // --- fail_todo_5891_import_multiple.carbon
  33. library "[[@TEST_NAME]]";
  34. import Cpp library "string_view.h";
  35. //@dump-sem-ir-begin
  36. fn F() {
  37. // CHECK:STDERR: fail_todo_5891_import_multiple.carbon:[[@LINE+7]]:15: error: call argument of type `str` is not supported [CppCallArgTypeNotSupported]
  38. // CHECK:STDERR: Cpp.Consume("hello");
  39. // CHECK:STDERR: ^~~~~~~
  40. // CHECK:STDERR: fail_todo_5891_import_multiple.carbon:[[@LINE+4]]:3: note: in call to Cpp function here [InCallToCppFunction]
  41. // CHECK:STDERR: Cpp.Consume("hello");
  42. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  43. // CHECK:STDERR:
  44. Cpp.Consume("hello");
  45. }
  46. //@dump-sem-ir-end
  47. //@dump-sem-ir-begin
  48. fn G() -> str {
  49. return Cpp.Produce();
  50. }
  51. //@dump-sem-ir-end
  52. // CHECK:STDOUT: --- fail_todo_5891_import_multiple.carbon
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: constants {
  55. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  56. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  57. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  58. // CHECK:STDOUT: %str.ee0: type = class_type @String [concrete]
  59. // CHECK:STDOUT: %int_64: Core.IntLiteral = int_value 64 [concrete]
  60. // CHECK:STDOUT: %u64: type = class_type @UInt, @UInt(%int_64) [concrete]
  61. // CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete]
  62. // CHECK:STDOUT: %u8: type = class_type @UInt, @UInt(%int_8) [concrete]
  63. // CHECK:STDOUT: %ptr.3e8: type = ptr_type %u8 [concrete]
  64. // CHECK:STDOUT: %.fd2: type = cpp_overload_set_type @Produce [concrete]
  65. // CHECK:STDOUT: %empty_struct.c28: %.fd2 = struct_value () [concrete]
  66. // CHECK:STDOUT: %str.3b1: %ptr.3e8 = string_literal "hello" [concrete]
  67. // CHECK:STDOUT: %int_5: %u64 = int_value 5 [concrete]
  68. // CHECK:STDOUT: %String.val: %str.ee0 = struct_value (%str.3b1, %int_5) [concrete]
  69. // CHECK:STDOUT: %pattern_type.461: type = pattern_type %str.ee0 [concrete]
  70. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  71. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  72. // CHECK:STDOUT: %.a47: type = cpp_overload_set_type @Produce__carbon_thunk [concrete]
  73. // CHECK:STDOUT: %empty_struct.ab9: %.a47 = struct_value () [concrete]
  74. // CHECK:STDOUT: %ptr.85f: type = ptr_type %str.ee0 [concrete]
  75. // CHECK:STDOUT: %Produce__carbon_thunk.type: type = fn_type @Produce__carbon_thunk [concrete]
  76. // CHECK:STDOUT: %Produce__carbon_thunk: %Produce__carbon_thunk.type = struct_value () [concrete]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: imports {
  80. // CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
  81. // CHECK:STDOUT: .Consume = %.f17
  82. // CHECK:STDOUT: .Produce = %.5d1
  83. // CHECK:STDOUT: import Cpp//...
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %.f17: %.fd2 = cpp_overload_set_value @Produce [concrete = constants.%empty_struct.c28]
  86. // CHECK:STDOUT: %.5d1: %.a47 = cpp_overload_set_value @Produce__carbon_thunk [concrete = constants.%empty_struct.ab9]
  87. // CHECK:STDOUT: %Produce__carbon_thunk.decl: %Produce__carbon_thunk.type = fn_decl @Produce__carbon_thunk [concrete = constants.%Produce__carbon_thunk] {
  88. // CHECK:STDOUT: <elided>
  89. // CHECK:STDOUT: } {
  90. // CHECK:STDOUT: <elided>
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: file {
  95. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  96. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  97. // CHECK:STDOUT: %return.patt: %pattern_type.461 = return_slot_pattern [concrete]
  98. // CHECK:STDOUT: %return.param_patt: %pattern_type.461 = out_param_pattern %return.patt, call_param0 [concrete]
  99. // CHECK:STDOUT: } {
  100. // CHECK:STDOUT: %return.param: ref %str.ee0 = out_param call_param0
  101. // CHECK:STDOUT: %return: ref %str.ee0 = return_slot %return.param
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn @F() {
  106. // CHECK:STDOUT: !entry:
  107. // CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  108. // CHECK:STDOUT: %Consume.ref: %.fd2 = name_ref Consume, imports.%.f17 [concrete = constants.%empty_struct.c28]
  109. // CHECK:STDOUT: %str: %ptr.3e8 = string_literal "hello" [concrete = constants.%str.3b1]
  110. // CHECK:STDOUT: %int_5: %u64 = int_value 5 [concrete = constants.%int_5]
  111. // CHECK:STDOUT: %String.val: %str.ee0 = struct_value (%str, %int_5) [concrete = constants.%String.val]
  112. // CHECK:STDOUT: return
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: fn @G() -> %return.param: %str.ee0 {
  116. // CHECK:STDOUT: !entry:
  117. // CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  118. // CHECK:STDOUT: %Produce.ref: %.a47 = name_ref Produce, imports.%.5d1 [concrete = constants.%empty_struct.ab9]
  119. // CHECK:STDOUT: %.loc20: ref %str.ee0 = splice_block %return {}
  120. // CHECK:STDOUT: %addr: %ptr.85f = addr_of %.loc20
  121. // CHECK:STDOUT: %Produce__carbon_thunk.call: init %empty_tuple.type = call imports.%Produce__carbon_thunk.decl(%addr)
  122. // CHECK:STDOUT: %.loc21: init %str.ee0 = in_place_init %Produce__carbon_thunk.call, %.loc20
  123. // CHECK:STDOUT: return %.loc21 to %return
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: