optional.carbon 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // This is an integration test for the Core.Optional in the prelude, so use the
  6. // real prelude.
  7. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
  8. // EXTRA-ARGS: --target=x86_64-linux-gnu
  9. //
  10. // AUTOUPDATE
  11. // TIP: To test this file alone, run:
  12. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/primitives/optional.carbon
  13. // TIP: To dump output, run:
  14. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/primitives/optional.carbon
  15. fn Convert(o: Core.Optional(i32*)) -> Core.Optional(i32) {
  16. if (o.HasValue()) {
  17. return *o.Get();
  18. }
  19. return Core.Optional(i32).None();
  20. }
  21. // CHECK:STDOUT: ; ModuleID = 'optional.carbon'
  22. // CHECK:STDOUT: source_filename = "optional.carbon"
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: define void @_CConvert.Main(ptr sret({ i32, i1 }) %return, ptr %o) !dbg !4 {
  25. // CHECK:STDOUT: entry:
  26. // CHECK:STDOUT: %.loc18_20.1.temp = alloca { i32, i1 }, align 8, !dbg !7
  27. // CHECK:STDOUT: %Optional.HasValue.call = call i1 @_CHasValue.Optional.Core.a5473232b4618d29(ptr %o), !dbg !8
  28. // CHECK:STDOUT: br i1 %Optional.HasValue.call, label %if.then, label %if.else, !dbg !9
  29. // CHECK:STDOUT:
  30. // CHECK:STDOUT: if.then: ; preds = %entry
  31. // CHECK:STDOUT: %Optional.Get.call = call ptr @_CGet.Optional.Core.a5473232b4618d29(ptr %o), !dbg !10
  32. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc18_20.1.temp), !dbg !7
  33. // CHECK:STDOUT: %.loc18_12.2 = load i32, ptr %Optional.Get.call, align 4, !dbg !11
  34. // CHECK:STDOUT: call void @"_CConvert.b97494f2edbcdd31:ImplicitAs.Core.e126642f41a9ea1c"(ptr %.loc18_20.1.temp, i32 %.loc18_12.2), !dbg !7
  35. // CHECK:STDOUT: ret void, !dbg !7
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: if.else: ; preds = %entry
  38. // CHECK:STDOUT: call void @_CNone.Optional.Core.e126642f41a9ea1c(ptr %return), !dbg !12
  39. // CHECK:STDOUT: ret void, !dbg !13
  40. // CHECK:STDOUT: }
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: define linkonce_odr i1 @_CHasValue.Optional.Core.a5473232b4618d29(ptr %self) !dbg !14 {
  43. // CHECK:STDOUT: %1 = load ptr, ptr %self, align 8, !dbg !16
  44. // CHECK:STDOUT: %2 = icmp eq ptr %1, null, !dbg !16
  45. // CHECK:STDOUT: ret i1 %2, !dbg !17
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: define linkonce_odr ptr @_CGet.Optional.Core.a5473232b4618d29(ptr %self) !dbg !18 {
  49. // CHECK:STDOUT: %1 = call ptr @"_CGet.79f1318a93915de5:OptionalStorage.Core.b88d1103f417c6d4"(ptr %self), !dbg !19
  50. // CHECK:STDOUT: ret ptr %1, !dbg !20
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  54. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #0
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: define linkonce_odr void @"_CConvert.b97494f2edbcdd31:ImplicitAs.Core.e126642f41a9ea1c"(ptr sret({ i32, i1 }) %return, i32 %self) !dbg !21 {
  57. // CHECK:STDOUT: call void @_CSome.Optional.Core.e126642f41a9ea1c(ptr %return, i32 %self), !dbg !22
  58. // CHECK:STDOUT: ret void, !dbg !23
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: define linkonce_odr void @_CNone.Optional.Core.e126642f41a9ea1c(ptr sret({ i32, i1 }) %return) !dbg !24 {
  62. // CHECK:STDOUT: call void @"_CNone.2a0d145dae8e0e7f:OptionalStorage.Core.de631560529e9861"(ptr %return), !dbg !25
  63. // CHECK:STDOUT: ret void, !dbg !26
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: define linkonce_odr ptr @"_CGet.79f1318a93915de5:OptionalStorage.Core.b88d1103f417c6d4"(ptr %value) !dbg !27 {
  67. // CHECK:STDOUT: %1 = load ptr, ptr %value, align 8, !dbg !28
  68. // CHECK:STDOUT: ret ptr %1, !dbg !29
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: define linkonce_odr void @_CSome.Optional.Core.e126642f41a9ea1c(ptr sret({ i32, i1 }) %return, i32 %value) !dbg !30 {
  72. // CHECK:STDOUT: call void @"_CSome.2a0d145dae8e0e7f:OptionalStorage.Core.de631560529e9861"(ptr %return, i32 %value), !dbg !31
  73. // CHECK:STDOUT: ret void, !dbg !32
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: define linkonce_odr void @"_CNone.2a0d145dae8e0e7f:OptionalStorage.Core.de631560529e9861"(ptr sret({ i32, i1 }) %return) !dbg !33 {
  77. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { i32, i1 }, ptr %return, i32 0, i32 1, !dbg !34
  78. // CHECK:STDOUT: store i8 0, ptr %has_value, align 1, !dbg !34
  79. // CHECK:STDOUT: ret void, !dbg !35
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: define linkonce_odr void @"_CSome.2a0d145dae8e0e7f:OptionalStorage.Core.de631560529e9861"(ptr sret({ i32, i1 }) %return, i32 %self) !dbg !36 {
  83. // CHECK:STDOUT: %value = getelementptr inbounds nuw { i32, i1 }, ptr %return, i32 0, i32 0, !dbg !37
  84. // CHECK:STDOUT: store i32 %self, ptr %value, align 4, !dbg !37
  85. // CHECK:STDOUT: %has_value = getelementptr inbounds nuw { i32, i1 }, ptr %return, i32 0, i32 1, !dbg !38
  86. // CHECK:STDOUT: store i8 1, ptr %has_value, align 1, !dbg !38
  87. // CHECK:STDOUT: ret void, !dbg !39
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  93. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  96. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  97. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  98. // CHECK:STDOUT: !3 = !DIFile(filename: "optional.carbon", directory: "")
  99. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.Main", scope: null, file: !3, line: 16, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  100. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  101. // CHECK:STDOUT: !6 = !{}
  102. // CHECK:STDOUT: !7 = !DILocation(line: 18, column: 5, scope: !4)
  103. // CHECK:STDOUT: !8 = !DILocation(line: 17, column: 7, scope: !4)
  104. // CHECK:STDOUT: !9 = !DILocation(line: 17, column: 6, scope: !4)
  105. // CHECK:STDOUT: !10 = !DILocation(line: 18, column: 13, scope: !4)
  106. // CHECK:STDOUT: !11 = !DILocation(line: 18, column: 12, scope: !4)
  107. // CHECK:STDOUT: !12 = !DILocation(line: 20, column: 10, scope: !4)
  108. // CHECK:STDOUT: !13 = !DILocation(line: 20, column: 3, scope: !4)
  109. // CHECK:STDOUT: !14 = distinct !DISubprogram(name: "HasValue", linkageName: "_CHasValue.Optional.Core.a5473232b4618d29", scope: null, file: !15, line: 31, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  110. // CHECK:STDOUT: !15 = !DIFile(filename: "{{.*}}/prelude/types/optional.carbon", directory: "")
  111. // CHECK:STDOUT: !16 = !DILocation(line: 32, column: 12, scope: !14)
  112. // CHECK:STDOUT: !17 = !DILocation(line: 32, column: 5, scope: !14)
  113. // CHECK:STDOUT: !18 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.Optional.Core.a5473232b4618d29", scope: null, file: !15, line: 34, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  114. // CHECK:STDOUT: !19 = !DILocation(line: 35, column: 12, scope: !18)
  115. // CHECK:STDOUT: !20 = !DILocation(line: 35, column: 5, scope: !18)
  116. // CHECK:STDOUT: !21 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.b97494f2edbcdd31:ImplicitAs.Core.e126642f41a9ea1c", scope: null, file: !15, line: 39, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  117. // CHECK:STDOUT: !22 = !DILocation(line: 40, column: 14, scope: !21)
  118. // CHECK:STDOUT: !23 = !DILocation(line: 40, column: 7, scope: !21)
  119. // CHECK:STDOUT: !24 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.e126642f41a9ea1c", scope: null, file: !15, line: 25, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  120. // CHECK:STDOUT: !25 = !DILocation(line: 26, column: 12, scope: !24)
  121. // CHECK:STDOUT: !26 = !DILocation(line: 26, column: 5, scope: !24)
  122. // CHECK:STDOUT: !27 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.79f1318a93915de5:OptionalStorage.Core.b88d1103f417c6d4", scope: null, file: !15, line: 92, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  123. // CHECK:STDOUT: !28 = !DILocation(line: 93, column: 12, scope: !27)
  124. // CHECK:STDOUT: !29 = !DILocation(line: 93, column: 5, scope: !27)
  125. // CHECK:STDOUT: !30 = distinct !DISubprogram(name: "Some", linkageName: "_CSome.Optional.Core.e126642f41a9ea1c", scope: null, file: !15, line: 28, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  126. // CHECK:STDOUT: !31 = !DILocation(line: 29, column: 12, scope: !30)
  127. // CHECK:STDOUT: !32 = !DILocation(line: 29, column: 5, scope: !30)
  128. // CHECK:STDOUT: !33 = distinct !DISubprogram(name: "None", linkageName: "_CNone.2a0d145dae8e0e7f:OptionalStorage.Core.de631560529e9861", scope: null, file: !15, line: 60, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  129. // CHECK:STDOUT: !34 = !DILocation(line: 62, column: 5, scope: !33)
  130. // CHECK:STDOUT: !35 = !DILocation(line: 63, column: 5, scope: !33)
  131. // CHECK:STDOUT: !36 = distinct !DISubprogram(name: "Some", linkageName: "_CSome.2a0d145dae8e0e7f:OptionalStorage.Core.de631560529e9861", scope: null, file: !15, line: 65, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  132. // CHECK:STDOUT: !37 = !DILocation(line: 69, column: 5, scope: !36)
  133. // CHECK:STDOUT: !38 = !DILocation(line: 70, column: 5, scope: !36)
  134. // CHECK:STDOUT: !39 = !DILocation(line: 71, column: 5, scope: !36)