enum.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/full.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/lower/testdata/interop/cpp/enum.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/enum.carbon
  12. // --- enum_member.h
  13. struct C {
  14. enum E : short {
  15. a,
  16. b = 7,
  17. c
  18. };
  19. static void F(E);
  20. };
  21. // --- pass_as_arg.carbon
  22. library "[[@TEST_NAME]]";
  23. import Cpp library "enum_member.h";
  24. fn Pass() {
  25. Cpp.C.F(Cpp.C.a);
  26. Cpp.C.F(Cpp.C.a);
  27. Cpp.C.F(Cpp.C.b);
  28. Cpp.C.F(Cpp.C.c);
  29. Cpp.C.F(Cpp.C.E.a);
  30. Cpp.C.F(Cpp.C.E.b);
  31. Cpp.C.F(Cpp.C.E.c);
  32. }
  33. // --- convert.carbon
  34. library "[[@TEST_NAME]]";
  35. import Cpp library "enum_member.h";
  36. fn TakeI16(n: i16);
  37. fn PassEnum() {
  38. TakeI16(Cpp.C.b as i16);
  39. }
  40. fn ConvertEnumToI16(e: Cpp.C.E) {
  41. TakeI16(e as i16);
  42. }
  43. fn ConvertI16ToEnum(n: i16) {
  44. Cpp.C.F(n as Cpp.C.E);
  45. }
  46. // --- bitmask.h
  47. enum Bits {
  48. A = 1 << 0,
  49. B = 1 << 1,
  50. C = 1 << 2,
  51. };
  52. void Take(Bits b);
  53. // --- use_bitmask.carbon
  54. library "[[@TEST_NAME]]";
  55. import Cpp library "bitmask.h";
  56. // TODO: Once we support interop with C++-defined operators, we should be able
  57. // to use `|` below rather than declaring our own builtin.
  58. fn BitOr(a: Cpp.Bits, b: Cpp.Bits) -> Cpp.Bits = "int.or";
  59. fn Call() {
  60. Cpp.Take(BitOr(Cpp.A, Cpp.C));
  61. }
  62. // CHECK:STDOUT: ; ModuleID = 'pass_as_arg.carbon'
  63. // CHECK:STDOUT: source_filename = "pass_as_arg.carbon"
  64. // 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"
  65. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: define void @_CPass.Main() !dbg !7 {
  68. // CHECK:STDOUT: entry:
  69. // CHECK:STDOUT: %.loc7_16.1.temp = alloca i16, align 2, !dbg !10
  70. // CHECK:STDOUT: %.loc8_16.1.temp = alloca i16, align 2, !dbg !11
  71. // CHECK:STDOUT: %.loc9_16.1.temp = alloca i16, align 2, !dbg !12
  72. // CHECK:STDOUT: %.loc10_16.1.temp = alloca i16, align 2, !dbg !13
  73. // CHECK:STDOUT: %.loc11_18.1.temp = alloca i16, align 2, !dbg !14
  74. // CHECK:STDOUT: %.loc12_18.1.temp = alloca i16, align 2, !dbg !15
  75. // CHECK:STDOUT: %.loc13_18.1.temp = alloca i16, align 2, !dbg !16
  76. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc7_16.1.temp), !dbg !10
  77. // CHECK:STDOUT: store i16 0, ptr %.loc7_16.1.temp, align 2, !dbg !10
  78. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc7_16.1.temp), !dbg !17
  79. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc8_16.1.temp), !dbg !11
  80. // CHECK:STDOUT: store i16 0, ptr %.loc8_16.1.temp, align 2, !dbg !11
  81. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc8_16.1.temp), !dbg !18
  82. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc9_16.1.temp), !dbg !12
  83. // CHECK:STDOUT: store i16 7, ptr %.loc9_16.1.temp, align 2, !dbg !12
  84. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc9_16.1.temp), !dbg !19
  85. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc10_16.1.temp), !dbg !13
  86. // CHECK:STDOUT: store i16 8, ptr %.loc10_16.1.temp, align 2, !dbg !13
  87. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc10_16.1.temp), !dbg !20
  88. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc11_18.1.temp), !dbg !14
  89. // CHECK:STDOUT: store i16 0, ptr %.loc11_18.1.temp, align 2, !dbg !14
  90. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc11_18.1.temp), !dbg !21
  91. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc12_18.1.temp), !dbg !15
  92. // CHECK:STDOUT: store i16 7, ptr %.loc12_18.1.temp, align 2, !dbg !15
  93. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc12_18.1.temp), !dbg !22
  94. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc13_18.1.temp), !dbg !16
  95. // CHECK:STDOUT: store i16 8, ptr %.loc13_18.1.temp, align 2, !dbg !16
  96. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc13_18.1.temp), !dbg !23
  97. // CHECK:STDOUT: ret void, !dbg !24
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  101. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #0
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress
  104. // CHECK:STDOUT: define dso_local void @_ZN1C1FENS_1EE.carbon_thunk(ptr %0) #1 {
  105. // CHECK:STDOUT: entry:
  106. // CHECK:STDOUT: %.addr = alloca ptr, align 8
  107. // CHECK:STDOUT: store ptr %0, ptr %.addr, align 8
  108. // CHECK:STDOUT: %1 = load ptr, ptr %.addr, align 8
  109. // CHECK:STDOUT: %2 = load i16, ptr %1, align 2
  110. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE(i16 signext %2)
  111. // CHECK:STDOUT: ret void
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: declare void @_ZN1C1FENS_1EE(i16 signext) #2
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: ; uselistorder directives
  117. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 6, 5, 4, 3, 2, 1, 0 }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  120. // CHECK:STDOUT: attributes #1 = { alwaysinline mustprogress "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" }
  121. // CHECK:STDOUT: attributes #2 = { "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" }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  124. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  127. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  128. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  129. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  130. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  131. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  132. // CHECK:STDOUT: !6 = !DIFile(filename: "pass_as_arg.carbon", directory: "")
  133. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "Pass", linkageName: "_CPass.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  134. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  135. // CHECK:STDOUT: !9 = !{}
  136. // CHECK:STDOUT: !10 = !DILocation(line: 7, column: 11, scope: !7)
  137. // CHECK:STDOUT: !11 = !DILocation(line: 8, column: 11, scope: !7)
  138. // CHECK:STDOUT: !12 = !DILocation(line: 9, column: 11, scope: !7)
  139. // CHECK:STDOUT: !13 = !DILocation(line: 10, column: 11, scope: !7)
  140. // CHECK:STDOUT: !14 = !DILocation(line: 11, column: 11, scope: !7)
  141. // CHECK:STDOUT: !15 = !DILocation(line: 12, column: 11, scope: !7)
  142. // CHECK:STDOUT: !16 = !DILocation(line: 13, column: 11, scope: !7)
  143. // CHECK:STDOUT: !17 = !DILocation(line: 7, column: 3, scope: !7)
  144. // CHECK:STDOUT: !18 = !DILocation(line: 8, column: 3, scope: !7)
  145. // CHECK:STDOUT: !19 = !DILocation(line: 9, column: 3, scope: !7)
  146. // CHECK:STDOUT: !20 = !DILocation(line: 10, column: 3, scope: !7)
  147. // CHECK:STDOUT: !21 = !DILocation(line: 11, column: 3, scope: !7)
  148. // CHECK:STDOUT: !22 = !DILocation(line: 12, column: 3, scope: !7)
  149. // CHECK:STDOUT: !23 = !DILocation(line: 13, column: 3, scope: !7)
  150. // CHECK:STDOUT: !24 = !DILocation(line: 6, column: 1, scope: !7)
  151. // CHECK:STDOUT: ; ModuleID = 'convert.carbon'
  152. // CHECK:STDOUT: source_filename = "convert.carbon"
  153. // 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"
  154. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: declare void @_CTakeI16.Main(i16)
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: define void @_CPassEnum.Main() !dbg !7 {
  159. // CHECK:STDOUT: entry:
  160. // CHECK:STDOUT: call void @_CTakeI16.Main(i16 7), !dbg !10
  161. // CHECK:STDOUT: ret void, !dbg !11
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: define void @_CConvertEnumToI16.Main(i16 %e) !dbg !12 {
  165. // CHECK:STDOUT: entry:
  166. // CHECK:STDOUT: call void @_CTakeI16.Main(i16 %e), !dbg !13
  167. // CHECK:STDOUT: ret void, !dbg !14
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: define void @_CConvertI16ToEnum.Main(i16 %n) !dbg !15 {
  171. // CHECK:STDOUT: entry:
  172. // CHECK:STDOUT: %.loc17_13.3.temp = alloca i16, align 2, !dbg !16
  173. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc17_13.3.temp), !dbg !16
  174. // CHECK:STDOUT: store i16 %n, ptr %.loc17_13.3.temp, align 2, !dbg !16
  175. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE.carbon_thunk(ptr %.loc17_13.3.temp), !dbg !17
  176. // CHECK:STDOUT: ret void, !dbg !18
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  180. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #0
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress
  183. // CHECK:STDOUT: define dso_local void @_ZN1C1FENS_1EE.carbon_thunk(ptr %0) #1 {
  184. // CHECK:STDOUT: entry:
  185. // CHECK:STDOUT: %.addr = alloca ptr, align 8
  186. // CHECK:STDOUT: store ptr %0, ptr %.addr, align 8
  187. // CHECK:STDOUT: %1 = load ptr, ptr %.addr, align 8
  188. // CHECK:STDOUT: %2 = load i16, ptr %1, align 2
  189. // CHECK:STDOUT: call void @_ZN1C1FENS_1EE(i16 signext %2)
  190. // CHECK:STDOUT: ret void
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: declare void @_ZN1C1FENS_1EE(i16 signext) #2
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  196. // CHECK:STDOUT: attributes #1 = { alwaysinline mustprogress "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" }
  197. // CHECK:STDOUT: attributes #2 = { "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" }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  200. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  203. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  204. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  205. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  206. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  207. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  208. // CHECK:STDOUT: !6 = !DIFile(filename: "convert.carbon", directory: "")
  209. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "PassEnum", linkageName: "_CPassEnum.Main", scope: null, file: !6, line: 8, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  210. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  211. // CHECK:STDOUT: !9 = !{}
  212. // CHECK:STDOUT: !10 = !DILocation(line: 9, column: 3, scope: !7)
  213. // CHECK:STDOUT: !11 = !DILocation(line: 8, column: 1, scope: !7)
  214. // CHECK:STDOUT: !12 = distinct !DISubprogram(name: "ConvertEnumToI16", linkageName: "_CConvertEnumToI16.Main", scope: null, file: !6, line: 12, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  215. // CHECK:STDOUT: !13 = !DILocation(line: 13, column: 3, scope: !12)
  216. // CHECK:STDOUT: !14 = !DILocation(line: 12, column: 1, scope: !12)
  217. // CHECK:STDOUT: !15 = distinct !DISubprogram(name: "ConvertI16ToEnum", linkageName: "_CConvertI16ToEnum.Main", scope: null, file: !6, line: 16, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  218. // CHECK:STDOUT: !16 = !DILocation(line: 17, column: 11, scope: !15)
  219. // CHECK:STDOUT: !17 = !DILocation(line: 17, column: 3, scope: !15)
  220. // CHECK:STDOUT: !18 = !DILocation(line: 16, column: 1, scope: !15)
  221. // CHECK:STDOUT: ; ModuleID = 'use_bitmask.carbon'
  222. // CHECK:STDOUT: source_filename = "use_bitmask.carbon"
  223. // 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"
  224. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: define void @_CCall.Main() !dbg !7 {
  227. // CHECK:STDOUT: entry:
  228. // CHECK:STDOUT: call void @_Z4Take4Bits(i32 5), !dbg !10
  229. // CHECK:STDOUT: ret void, !dbg !11
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: declare void @_Z4Take4Bits(i32)
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  235. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  238. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  239. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  240. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  241. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  242. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  243. // CHECK:STDOUT: !6 = !DIFile(filename: "use_bitmask.carbon", directory: "")
  244. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main", scope: null, file: !6, line: 10, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  245. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  246. // CHECK:STDOUT: !9 = !{}
  247. // CHECK:STDOUT: !10 = !DILocation(line: 11, column: 3, scope: !7)
  248. // CHECK:STDOUT: !11 = !DILocation(line: 10, column: 1, scope: !7)