function_decl.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/lower/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/lower/testdata/interop/cpp/function_decl.carbon
  10. // ============================================================================
  11. // function_decl
  12. // ============================================================================
  13. // --- function_decl.h
  14. void foo();
  15. // --- import_function_decl.carbon
  16. library "[[@TEST_NAME]]";
  17. import Cpp library "function_decl.h";
  18. fn MyF() {
  19. Cpp.foo();
  20. }
  21. // ============================================================================
  22. // inline_function_decl
  23. // ============================================================================
  24. // --- inline_function_decl.h
  25. inline void foo() {}
  26. // --- import_inline_function_decl.carbon
  27. library "[[@TEST_NAME]]";
  28. import Cpp library "inline_function_decl.h";
  29. fn MyF() {
  30. Cpp.foo();
  31. }
  32. // ============================================================================
  33. // inline_used_function_decl
  34. // ============================================================================
  35. // --- inline_used_function_decl.h
  36. inline __attribute__((used)) void foo() {}
  37. // --- import_inline_used_function_decl.carbon
  38. library "[[@TEST_NAME]]";
  39. import Cpp library "inline_used_function_decl.h";
  40. fn MyF() {
  41. Cpp.foo();
  42. }
  43. // ============================================================================
  44. // multiple_inline_function_decls
  45. // ============================================================================
  46. // --- multiple_inline_function_decls.h
  47. inline void foo1() {}
  48. inline void foo2() {}
  49. inline void foo3() {}
  50. // --- import_multiple_inline_function_decls.carbon
  51. library "[[@TEST_NAME]]";
  52. import Cpp library "multiple_inline_function_decls.h";
  53. fn MyF() {
  54. Cpp.foo1();
  55. Cpp.foo2();
  56. Cpp.foo3();
  57. }
  58. // ============================================================================
  59. // inline_recursive_function_decl
  60. // ============================================================================
  61. // --- inline_recursive_function_decl.h
  62. inline void foo1() {}
  63. inline void foo2() { foo1(); }
  64. // --- todo_import_inline_recursive_function_decl.carbon
  65. library "[[@TEST_NAME]]";
  66. import Cpp library "inline_recursive_function_decl.h";
  67. fn MyF() {
  68. // TODO: This should generate the definition of the inline function `foo1()`.
  69. Cpp.foo2();
  70. }
  71. // CHECK:STDOUT: ; ModuleID = 'import_function_decl.carbon'
  72. // CHECK:STDOUT: source_filename = "import_function_decl.carbon"
  73. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  74. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !7 {
  77. // CHECK:STDOUT: entry:
  78. // CHECK:STDOUT: call void @_Z3foov(), !dbg !10
  79. // CHECK:STDOUT: ret void, !dbg !11
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: declare void @_Z3foov()
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  85. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  88. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  89. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  90. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  91. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  92. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  93. // CHECK:STDOUT: !6 = !DIFile(filename: "import_function_decl.carbon", directory: "")
  94. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  95. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  96. // CHECK:STDOUT: !9 = !{}
  97. // CHECK:STDOUT: !10 = !DILocation(line: 7, column: 3, scope: !7)
  98. // CHECK:STDOUT: !11 = !DILocation(line: 6, column: 1, scope: !7)
  99. // CHECK:STDOUT: ; ModuleID = 'import_inline_function_decl.carbon'
  100. // CHECK:STDOUT: source_filename = "import_inline_function_decl.carbon"
  101. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  102. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: $_Z3foov = comdat any
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !7 {
  107. // CHECK:STDOUT: entry:
  108. // CHECK:STDOUT: call void @_Z3foov(), !dbg !10
  109. // CHECK:STDOUT: ret void, !dbg !11
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  113. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z3foov() #0 comdat {
  114. // CHECK:STDOUT: entry:
  115. // CHECK:STDOUT: ret void
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: attributes #0 = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  121. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  124. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  125. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  126. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  127. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  128. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  129. // CHECK:STDOUT: !6 = !DIFile(filename: "import_inline_function_decl.carbon", directory: "")
  130. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  131. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  132. // CHECK:STDOUT: !9 = !{}
  133. // CHECK:STDOUT: !10 = !DILocation(line: 7, column: 3, scope: !7)
  134. // CHECK:STDOUT: !11 = !DILocation(line: 6, column: 1, scope: !7)
  135. // CHECK:STDOUT: ; ModuleID = 'import_inline_used_function_decl.carbon'
  136. // CHECK:STDOUT: source_filename = "import_inline_used_function_decl.carbon"
  137. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  138. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: $_Z3foov = comdat any
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: @llvm.compiler.used = appending global [1 x ptr] [ptr @_Z3foov], section "llvm.metadata"
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !7 {
  145. // CHECK:STDOUT: entry:
  146. // CHECK:STDOUT: call void @_Z3foov(), !dbg !10
  147. // CHECK:STDOUT: ret void, !dbg !11
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  151. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z3foov() #0 comdat {
  152. // CHECK:STDOUT: entry:
  153. // CHECK:STDOUT: ret void
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: ; uselistorder directives
  157. // CHECK:STDOUT: uselistorder ptr @_Z3foov, { 1, 0 }
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: attributes #0 = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  162. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  165. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  166. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  167. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  168. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  169. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  170. // CHECK:STDOUT: !6 = !DIFile(filename: "import_inline_used_function_decl.carbon", directory: "")
  171. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  172. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  173. // CHECK:STDOUT: !9 = !{}
  174. // CHECK:STDOUT: !10 = !DILocation(line: 7, column: 3, scope: !7)
  175. // CHECK:STDOUT: !11 = !DILocation(line: 6, column: 1, scope: !7)
  176. // CHECK:STDOUT: ; ModuleID = 'import_multiple_inline_function_decls.carbon'
  177. // CHECK:STDOUT: source_filename = "import_multiple_inline_function_decls.carbon"
  178. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  179. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: $_Z4foo1v = comdat any
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: $_Z4foo2v = comdat any
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: $_Z4foo3v = comdat any
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !7 {
  188. // CHECK:STDOUT: entry:
  189. // CHECK:STDOUT: call void @_Z4foo1v(), !dbg !10
  190. // CHECK:STDOUT: call void @_Z4foo2v(), !dbg !11
  191. // CHECK:STDOUT: call void @_Z4foo3v(), !dbg !12
  192. // CHECK:STDOUT: ret void, !dbg !13
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  196. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo1v() #0 comdat {
  197. // CHECK:STDOUT: entry:
  198. // CHECK:STDOUT: ret void
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  202. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo2v() #0 comdat {
  203. // CHECK:STDOUT: entry:
  204. // CHECK:STDOUT: ret void
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT:
  207. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  208. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo3v() #0 comdat {
  209. // CHECK:STDOUT: entry:
  210. // CHECK:STDOUT: ret void
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: attributes #0 = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  214. // CHECK:STDOUT:
  215. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  216. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  217. // CHECK:STDOUT:
  218. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  219. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  220. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  221. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  222. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  223. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  224. // CHECK:STDOUT: !6 = !DIFile(filename: "import_multiple_inline_function_decls.carbon", directory: "")
  225. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  226. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  227. // CHECK:STDOUT: !9 = !{}
  228. // CHECK:STDOUT: !10 = !DILocation(line: 7, column: 3, scope: !7)
  229. // CHECK:STDOUT: !11 = !DILocation(line: 8, column: 3, scope: !7)
  230. // CHECK:STDOUT: !12 = !DILocation(line: 9, column: 3, scope: !7)
  231. // CHECK:STDOUT: !13 = !DILocation(line: 6, column: 1, scope: !7)
  232. // CHECK:STDOUT: ; ModuleID = 'todo_import_inline_recursive_function_decl.carbon'
  233. // CHECK:STDOUT: source_filename = "todo_import_inline_recursive_function_decl.carbon"
  234. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  235. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: $_Z4foo2v = comdat any
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !7 {
  240. // CHECK:STDOUT: entry:
  241. // CHECK:STDOUT: call void @_Z4foo2v(), !dbg !10
  242. // CHECK:STDOUT: ret void, !dbg !11
  243. // CHECK:STDOUT: }
  244. // CHECK:STDOUT:
  245. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline optnone
  246. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo2v() #0 comdat {
  247. // CHECK:STDOUT: entry:
  248. // CHECK:STDOUT: call void @_Z4foo1v()
  249. // CHECK:STDOUT: ret void
  250. // CHECK:STDOUT: }
  251. // CHECK:STDOUT:
  252. // CHECK:STDOUT: declare void @_Z4foo1v() #1
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: attributes #0 = { mustprogress noinline optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  255. // CHECK:STDOUT: attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  256. // CHECK:STDOUT:
  257. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  258. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  259. // CHECK:STDOUT:
  260. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  261. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  262. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  263. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  264. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  265. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  266. // CHECK:STDOUT: !6 = !DIFile(filename: "todo_import_inline_recursive_function_decl.carbon", directory: "")
  267. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  268. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  269. // CHECK:STDOUT: !9 = !{}
  270. // CHECK:STDOUT: !10 = !DILocation(line: 8, column: 3, scope: !7)
  271. // CHECK:STDOUT: !11 = !DILocation(line: 6, column: 1, scope: !7)