overloaded.carbon 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/operators/overloaded.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/operators/overloaded.carbon
  12. class Number {
  13. var is_positive: bool;
  14. }
  15. impl Number as Core.Negate where .Result = Number {
  16. fn Op[self: Number]() -> Number {
  17. return {.is_positive = not self.is_positive};
  18. }
  19. }
  20. impl Number as Core.MulWith(Number) where .Result = Number {
  21. fn Op[self: Number](other: Number) -> Number {
  22. return {.is_positive = (self.is_positive and other.is_positive) or
  23. (not self.is_positive and not other.is_positive)};
  24. }
  25. }
  26. fn Calculate(a: Number, b: Number) -> Number {
  27. return -a * b;
  28. }
  29. // CHECK:STDOUT: ; ModuleID = 'overloaded.carbon'
  30. // CHECK:STDOUT: source_filename = "overloaded.carbon"
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: ; Function Attrs: nounwind
  33. // CHECK:STDOUT: define void @"_COp.Number.Main:Negate.Core"(ptr sret({ i1 }) %return, ptr %self) #0 !dbg !4 {
  34. // CHECK:STDOUT: entry:
  35. // CHECK:STDOUT: %.loc19_36.1.is_positive = getelementptr inbounds nuw { i1 }, ptr %self, i32 0, i32 0, !dbg !10
  36. // CHECK:STDOUT: %.loc19_36.2 = load i8, ptr %.loc19_36.1.is_positive, align 1, !dbg !10
  37. // CHECK:STDOUT: %.loc19_36.21 = trunc i8 %.loc19_36.2 to i1, !dbg !10
  38. // CHECK:STDOUT: %.loc19_28 = xor i1 %.loc19_36.21, true, !dbg !11
  39. // CHECK:STDOUT: %.loc19_48.2.is_positive = getelementptr inbounds nuw { i1 }, ptr %return, i32 0, i32 0, !dbg !12
  40. // CHECK:STDOUT: %.loc19_48.3 = zext i1 %.loc19_28 to i8, !dbg !12
  41. // CHECK:STDOUT: store i8 %.loc19_48.3, ptr %.loc19_48.2.is_positive, align 1, !dbg !12
  42. // CHECK:STDOUT: ret void, !dbg !13
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: ; Function Attrs: nounwind
  46. // CHECK:STDOUT: define void @"_COp.Number.Main:MulWith.44027391c5135f93.Core"(ptr sret({ i1 }) %return, ptr %self, ptr %other) #0 !dbg !14 {
  47. // CHECK:STDOUT: entry:
  48. // CHECK:STDOUT: %.loc24_33.1.is_positive = getelementptr inbounds nuw { i1 }, ptr %self, i32 0, i32 0, !dbg !20
  49. // CHECK:STDOUT: %.loc24_33.2 = load i8, ptr %.loc24_33.1.is_positive, align 1, !dbg !20
  50. // CHECK:STDOUT: %.loc24_33.21 = trunc i8 %.loc24_33.2 to i1, !dbg !20
  51. // CHECK:STDOUT: br i1 %.loc24_33.21, label %and.rhs.loc24, label %and.result.loc24, !dbg !20
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: and.rhs.loc24: ; preds = %entry
  54. // CHECK:STDOUT: %.loc24_55.1.is_positive = getelementptr inbounds nuw { i1 }, ptr %other, i32 0, i32 0, !dbg !21
  55. // CHECK:STDOUT: %.loc24_55.2 = load i8, ptr %.loc24_55.1.is_positive, align 1, !dbg !21
  56. // CHECK:STDOUT: %.loc24_55.22 = trunc i8 %.loc24_55.2 to i1, !dbg !21
  57. // CHECK:STDOUT: br label %and.result.loc24, !dbg !20
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: and.result.loc24: ; preds = %and.rhs.loc24, %entry
  60. // CHECK:STDOUT: %0 = phi i1 [ false, %entry ], [ %.loc24_55.22, %and.rhs.loc24 ]
  61. // CHECK:STDOUT: %.loc24_69.1 = xor i1 %0, true, !dbg !22
  62. // CHECK:STDOUT: br i1 %.loc24_69.1, label %or.rhs, label %or.result, !dbg !22
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: or.rhs: ; preds = %and.result.loc24
  65. // CHECK:STDOUT: %.loc25_38.1.is_positive = getelementptr inbounds nuw { i1 }, ptr %self, i32 0, i32 0, !dbg !23
  66. // CHECK:STDOUT: %.loc25_38.2 = load i8, ptr %.loc25_38.1.is_positive, align 1, !dbg !23
  67. // CHECK:STDOUT: %.loc25_38.23 = trunc i8 %.loc25_38.2 to i1, !dbg !23
  68. // CHECK:STDOUT: %.loc25_30 = xor i1 %.loc25_38.23, true, !dbg !24
  69. // CHECK:STDOUT: br i1 %.loc25_30, label %and.rhs.loc25, label %and.result.loc25, !dbg !24
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: and.rhs.loc25: ; preds = %or.rhs
  72. // CHECK:STDOUT: %.loc25_64.1.is_positive = getelementptr inbounds nuw { i1 }, ptr %other, i32 0, i32 0, !dbg !25
  73. // CHECK:STDOUT: %.loc25_64.2 = load i8, ptr %.loc25_64.1.is_positive, align 1, !dbg !25
  74. // CHECK:STDOUT: %.loc25_64.24 = trunc i8 %.loc25_64.2 to i1, !dbg !25
  75. // CHECK:STDOUT: %.loc25_55 = xor i1 %.loc25_64.24, true, !dbg !26
  76. // CHECK:STDOUT: br label %and.result.loc25, !dbg !24
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: and.result.loc25: ; preds = %and.rhs.loc25, %or.rhs
  79. // CHECK:STDOUT: %1 = phi i1 [ false, %or.rhs ], [ %.loc25_55, %and.rhs.loc25 ]
  80. // CHECK:STDOUT: br label %or.result, !dbg !22
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: or.result: ; preds = %and.result.loc25, %and.result.loc24
  83. // CHECK:STDOUT: %2 = phi i1 [ true, %and.result.loc24 ], [ %1, %and.result.loc25 ]
  84. // CHECK:STDOUT: %.loc25_77.2.is_positive = getelementptr inbounds nuw { i1 }, ptr %return, i32 0, i32 0, !dbg !27
  85. // CHECK:STDOUT: %.loc25_77.3 = zext i1 %2 to i8, !dbg !27
  86. // CHECK:STDOUT: store i8 %.loc25_77.3, ptr %.loc25_77.2.is_positive, align 1, !dbg !27
  87. // CHECK:STDOUT: ret void, !dbg !28
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: ; Function Attrs: nounwind
  91. // CHECK:STDOUT: define void @_CCalculate.Main(ptr sret({ i1 }) %return, ptr %a, ptr %b) #0 !dbg !29 {
  92. // CHECK:STDOUT: entry:
  93. // CHECK:STDOUT: %.loc30_10.1.temp = alloca { i1 }, align 8, !dbg !33
  94. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc30_10.1.temp), !dbg !33
  95. // CHECK:STDOUT: call void @"_COp.Number.Main:Negate.Core"(ptr %.loc30_10.1.temp, ptr %a), !dbg !33
  96. // CHECK:STDOUT: call void @"_COp.Number.Main:MulWith.44027391c5135f93.Core"(ptr %return, ptr %.loc30_10.1.temp, ptr %b), !dbg !33
  97. // CHECK:STDOUT: ret void, !dbg !34
  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)) #1
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: attributes #0 = { nounwind }
  104. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  107. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  110. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  111. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  112. // CHECK:STDOUT: !3 = !DIFile(filename: "overloaded.carbon", directory: "")
  113. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Op", linkageName: "_COp.Number.Main:Negate.Core", scope: null, file: !3, line: 18, type: !5, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !8)
  114. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  115. // CHECK:STDOUT: !6 = !{!7, !7}
  116. // CHECK:STDOUT: !7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8)
  117. // CHECK:STDOUT: !8 = !{!9}
  118. // CHECK:STDOUT: !9 = !DILocalVariable(arg: 1, scope: !4, type: !7)
  119. // CHECK:STDOUT: !10 = !DILocation(line: 19, column: 32, scope: !4)
  120. // CHECK:STDOUT: !11 = !DILocation(line: 19, column: 28, scope: !4)
  121. // CHECK:STDOUT: !12 = !DILocation(line: 19, column: 12, scope: !4)
  122. // CHECK:STDOUT: !13 = !DILocation(line: 19, column: 5, scope: !4)
  123. // CHECK:STDOUT: !14 = distinct !DISubprogram(name: "Op", linkageName: "_COp.Number.Main:MulWith.44027391c5135f93.Core", scope: null, file: !3, line: 23, type: !15, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !17)
  124. // CHECK:STDOUT: !15 = !DISubroutineType(types: !16)
  125. // CHECK:STDOUT: !16 = !{!7, !7, !7}
  126. // CHECK:STDOUT: !17 = !{!18, !19}
  127. // CHECK:STDOUT: !18 = !DILocalVariable(arg: 1, scope: !14, type: !7)
  128. // CHECK:STDOUT: !19 = !DILocalVariable(arg: 2, scope: !14, type: !7)
  129. // CHECK:STDOUT: !20 = !DILocation(line: 24, column: 29, scope: !14)
  130. // CHECK:STDOUT: !21 = !DILocation(line: 24, column: 50, scope: !14)
  131. // CHECK:STDOUT: !22 = !DILocation(line: 24, column: 28, scope: !14)
  132. // CHECK:STDOUT: !23 = !DILocation(line: 25, column: 34, scope: !14)
  133. // CHECK:STDOUT: !24 = !DILocation(line: 25, column: 30, scope: !14)
  134. // CHECK:STDOUT: !25 = !DILocation(line: 25, column: 59, scope: !14)
  135. // CHECK:STDOUT: !26 = !DILocation(line: 25, column: 55, scope: !14)
  136. // CHECK:STDOUT: !27 = !DILocation(line: 24, column: 12, scope: !14)
  137. // CHECK:STDOUT: !28 = !DILocation(line: 24, column: 5, scope: !14)
  138. // CHECK:STDOUT: !29 = distinct !DISubprogram(name: "Calculate", linkageName: "_CCalculate.Main", scope: null, file: !3, line: 29, type: !15, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !30)
  139. // CHECK:STDOUT: !30 = !{!31, !32}
  140. // CHECK:STDOUT: !31 = !DILocalVariable(arg: 1, scope: !29, type: !7)
  141. // CHECK:STDOUT: !32 = !DILocalVariable(arg: 2, scope: !29, type: !7)
  142. // CHECK:STDOUT: !33 = !DILocation(line: 30, column: 10, scope: !29)
  143. // CHECK:STDOUT: !34 = !DILocation(line: 30, column: 3, scope: !29)