method.carbon 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/int.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/method.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/method.carbon
  12. // --- methods.h
  13. struct A {
  14. int by_val() const { return n; }
  15. int by_ref() { return n; }
  16. int n;
  17. };
  18. // --- call_by_val.carbon
  19. library "[[@TEST_NAME]]";
  20. import Cpp library "methods.h";
  21. fn UseVal(a: Cpp.A) -> i32 {
  22. return a.by_val();
  23. }
  24. // --- call_by_ref.carbon
  25. library "[[@TEST_NAME]]";
  26. import Cpp library "methods.h";
  27. fn UseVal(a: Cpp.A*) -> i32 {
  28. return a->by_ref();
  29. }
  30. // CHECK:STDOUT: ; ModuleID = 'call_by_val.carbon'
  31. // CHECK:STDOUT: source_filename = "call_by_val.carbon"
  32. // 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"
  33. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  34. // CHECK:STDOUT:
  35. // CHECK:STDOUT: %struct.A = type { i32 }
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: $_ZNK1A6by_valEv = comdat any
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: define i32 @_CUseVal.Main(ptr %a) !dbg !7 {
  40. // CHECK:STDOUT: entry:
  41. // CHECK:STDOUT: %A.by_val.call = call i32 @_ZNK1A6by_valEv(ptr %a), !dbg !10
  42. // CHECK:STDOUT: ret i32 %A.by_val.call, !dbg !11
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  46. // CHECK:STDOUT: define linkonce_odr dso_local i32 @_ZNK1A6by_valEv(ptr nonnull align 4 dereferenceable(4) %this) #0 comdat align 2 {
  47. // CHECK:STDOUT: entry:
  48. // CHECK:STDOUT: %this.addr = alloca ptr, align 8
  49. // CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8
  50. // CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8
  51. // CHECK:STDOUT: %n = getelementptr inbounds nuw %struct.A, ptr %this1, i32 0, i32 0
  52. // CHECK:STDOUT: %0 = load i32, ptr %n, align 4
  53. // CHECK:STDOUT: ret i32 %0
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // 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" }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  59. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  62. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  63. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  64. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  65. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  66. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  67. // CHECK:STDOUT: !6 = !DIFile(filename: "call_by_val.carbon", directory: "")
  68. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "UseVal", linkageName: "_CUseVal.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  69. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  70. // CHECK:STDOUT: !9 = !{}
  71. // CHECK:STDOUT: !10 = !DILocation(line: 7, column: 10, scope: !7)
  72. // CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !7)
  73. // CHECK:STDOUT: ; ModuleID = 'call_by_ref.carbon'
  74. // CHECK:STDOUT: source_filename = "call_by_ref.carbon"
  75. // 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"
  76. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: %struct.A = type { i32 }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: $_ZN1A6by_refEv = comdat any
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: define i32 @_CUseVal.Main(ptr %a) !dbg !7 {
  83. // CHECK:STDOUT: entry:
  84. // CHECK:STDOUT: %A.by_ref.call = call i32 @_ZN1A6by_refEv(ptr %a), !dbg !10
  85. // CHECK:STDOUT: ret i32 %A.by_ref.call, !dbg !11
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  89. // CHECK:STDOUT: define linkonce_odr dso_local i32 @_ZN1A6by_refEv(ptr nonnull align 4 dereferenceable(4) %this) #0 comdat align 2 {
  90. // CHECK:STDOUT: entry:
  91. // CHECK:STDOUT: %this.addr = alloca ptr, align 8
  92. // CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8
  93. // CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8
  94. // CHECK:STDOUT: %n = getelementptr inbounds nuw %struct.A, ptr %this1, i32 0, i32 0
  95. // CHECK:STDOUT: %0 = load i32, ptr %n, align 4
  96. // CHECK:STDOUT: ret i32 %0
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // 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" }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  102. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  105. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  106. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  107. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  108. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  109. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  110. // CHECK:STDOUT: !6 = !DIFile(filename: "call_by_ref.carbon", directory: "")
  111. // CHECK:STDOUT: !7 = distinct !DISubprogram(name: "UseVal", linkageName: "_CUseVal.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
  112. // CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
  113. // CHECK:STDOUT: !9 = !{}
  114. // CHECK:STDOUT: !10 = !DILocation(line: 7, column: 10, scope: !7)
  115. // CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !7)