function_decl.carbon 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/function_decl.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/function_decl.carbon
  10. // --- int_return_function_decl.h
  11. auto foo_int() -> int;
  12. // --- import_int_return_function_decl.carbon
  13. library "[[@TEST_NAME]]";
  14. import Cpp library "int_return_function_decl.h";
  15. fn Carbon_foo(val: i32);
  16. fn F() {
  17. Carbon_foo(Cpp.foo_int());
  18. }
  19. // --- float_return_function_decl.h
  20. auto foo_float() -> float;
  21. // --- fail_import_float_return_function_decl.carbon
  22. library "[[@TEST_NAME]]";
  23. import Cpp library "float_return_function_decl.h";
  24. fn F() {
  25. // CHECK:STDERR: fail_import_float_return_function_decl.carbon:[[@LINE+7]]:3: error: semantics TODO: `Unsupported: return type: float` [SemanticsTodo]
  26. // CHECK:STDERR: Cpp.foo_float();
  27. // CHECK:STDERR: ^~~~~~~~~~~~~
  28. // CHECK:STDERR: fail_import_float_return_function_decl.carbon:[[@LINE+4]]:3: note: in `Cpp` name lookup for `foo_float` [InCppNameLookup]
  29. // CHECK:STDERR: Cpp.foo_float();
  30. // CHECK:STDERR: ^~~~~~~~~~~~~
  31. // CHECK:STDERR:
  32. Cpp.foo_float();
  33. }
  34. // CHECK:STDOUT: --- import_int_return_function_decl.carbon
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: constants {
  37. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  38. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  39. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  40. // CHECK:STDOUT: %Carbon_foo.type: type = fn_type @Carbon_foo [concrete]
  41. // CHECK:STDOUT: %Carbon_foo: %Carbon_foo.type = struct_value () [concrete]
  42. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  43. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  44. // CHECK:STDOUT: %foo_int.type: type = fn_type @foo_int [concrete]
  45. // CHECK:STDOUT: %foo_int: %foo_int.type = struct_value () [concrete]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: imports {
  49. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  50. // CHECK:STDOUT: .Int = %Core.Int
  51. // CHECK:STDOUT: import Core//prelude
  52. // CHECK:STDOUT: import Core//prelude/...
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
  55. // CHECK:STDOUT: .foo_int = @F.%foo_int.decl
  56. // CHECK:STDOUT: import Cpp//...
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: file {
  61. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  62. // CHECK:STDOUT: .Core = imports.%Core
  63. // CHECK:STDOUT: .Cpp = imports.%Cpp
  64. // CHECK:STDOUT: .Carbon_foo = %Carbon_foo.decl
  65. // CHECK:STDOUT: .F = %F.decl
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Core.import = import Core
  68. // CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
  69. // CHECK:STDOUT: import Cpp "int_return_function_decl.h"
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Carbon_foo.decl: %Carbon_foo.type = fn_decl @Carbon_foo [concrete = constants.%Carbon_foo] {
  72. // CHECK:STDOUT: %val.patt: %i32 = binding_pattern val
  73. // CHECK:STDOUT: %val.param_patt: %i32 = value_param_pattern %val.patt, call_param0
  74. // CHECK:STDOUT: } {
  75. // CHECK:STDOUT: %val.param: %i32 = value_param call_param0
  76. // CHECK:STDOUT: %.loc6: type = splice_block %i32 [concrete = constants.%i32] {
  77. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  78. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %val: %i32 = bind_name val, %val.param
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: fn @Carbon_foo(%val.param_patt: %i32);
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: fn @F() {
  88. // CHECK:STDOUT: !entry:
  89. // CHECK:STDOUT: %Carbon_foo.ref: %Carbon_foo.type = name_ref Carbon_foo, file.%Carbon_foo.decl [concrete = constants.%Carbon_foo]
  90. // CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  91. // CHECK:STDOUT: %int_32.1: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  92. // CHECK:STDOUT: %i32.1: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  93. // CHECK:STDOUT: %int_32.2: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  94. // CHECK:STDOUT: %i32.2: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  95. // CHECK:STDOUT: %foo_int.decl: %foo_int.type = fn_decl @foo_int [concrete = constants.%foo_int] {} {}
  96. // CHECK:STDOUT: %foo_int.ref: %foo_int.type = name_ref foo_int, %foo_int.decl [concrete = constants.%foo_int]
  97. // CHECK:STDOUT: %foo_int.call: init %i32 = call %foo_int.ref()
  98. // CHECK:STDOUT: %.loc9_26.1: %i32 = value_of_initializer %foo_int.call
  99. // CHECK:STDOUT: %.loc9_26.2: %i32 = converted %foo_int.call, %.loc9_26.1
  100. // CHECK:STDOUT: %Carbon_foo.call: init %empty_tuple.type = call %Carbon_foo.ref(%.loc9_26.2)
  101. // CHECK:STDOUT: return
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: fn @foo_int[]() -> %i32;
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: --- fail_import_float_return_function_decl.carbon
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: constants {
  109. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  110. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: imports {
  114. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  115. // CHECK:STDOUT: import Core//prelude
  116. // CHECK:STDOUT: import Core//prelude/...
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
  119. // CHECK:STDOUT: .foo_float = <error>
  120. // CHECK:STDOUT: import Cpp//...
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: file {
  125. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  126. // CHECK:STDOUT: .Core = imports.%Core
  127. // CHECK:STDOUT: .Cpp = imports.%Cpp
  128. // CHECK:STDOUT: .F = %F.decl
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: %Core.import = import Core
  131. // CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
  132. // CHECK:STDOUT: import Cpp "float_return_function_decl.h"
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: fn @F() {
  138. // CHECK:STDOUT: !entry:
  139. // CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  140. // CHECK:STDOUT: %foo_float.ref: <error> = name_ref foo_float, <error> [concrete = <error>]
  141. // CHECK:STDOUT: return
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: