abstract.carbon 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/destroy.carbon
  6. // EXTRA-ARGS: --clang-arg=-Wno-abstract-final-class
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/abstract.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/abstract.carbon
  13. // --- abstract.h
  14. struct A {
  15. virtual void f() = 0;
  16. };
  17. // --- derive_base_from_abstract.carbon
  18. library "[[@TEST_NAME]]";
  19. import Cpp library "abstract.h";
  20. base class B {
  21. extend base: Cpp.A;
  22. }
  23. // --- todo_fail_derive_final_from_abstract.carbon
  24. library "[[@TEST_NAME]]";
  25. import Cpp library "abstract.h";
  26. class C {
  27. // TODO: We should reject this because `f` is not implemented.
  28. extend base: Cpp.A;
  29. }
  30. // --- fail_todo_impl_abstract_member.carbon
  31. library "[[@TEST_NAME]]";
  32. import Cpp library "abstract.h";
  33. class C {
  34. extend base: Cpp.A;
  35. // CHECK:STDERR: fail_todo_impl_abstract_member.carbon:[[@LINE+4]]:3: error: impl without compatible virtual in base class [ImplWithoutVirtualInBase]
  36. // CHECK:STDERR: impl fn f[addr self: Self*]() {}
  37. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. // CHECK:STDERR:
  39. impl fn f[addr self: Self*]() {}
  40. }
  41. // --- abstract_final.h
  42. // C++ allows a class to be both abstract and final.
  43. struct AbstractFinal final {
  44. virtual void f() = 0;
  45. };
  46. // --- fail_derive_from_abstract_final.carbon
  47. library "[[@TEST_NAME]]";
  48. import Cpp library "abstract_final.h";
  49. class C {
  50. // CHECK:STDERR: fail_derive_from_abstract_final.carbon:[[@LINE+4]]:16: error: deriving from final type `AbstractFinal`; base type must be an `abstract` or `base` class [BaseIsFinal]
  51. // CHECK:STDERR: extend base: Cpp.AbstractFinal;
  52. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  53. // CHECK:STDERR:
  54. extend base: Cpp.AbstractFinal;
  55. }
  56. // --- use_abstract_final.carbon
  57. library "[[@TEST_NAME]]";
  58. import Cpp library "abstract_final.h";
  59. // We model an abstract final class as being final, rather than abstract. This
  60. // prevents inheritance from it, and there's no actual way to initialize an
  61. // instance of the type, but that does not prevent defining a function that
  62. // returns the type by value. Make sure that doesn't crash.
  63. //@dump-sem-ir-begin
  64. fn F() -> Cpp.AbstractFinal {
  65. return F();
  66. }
  67. //@dump-sem-ir-end
  68. // CHECK:STDOUT: --- use_abstract_final.carbon
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: constants {
  71. // CHECK:STDOUT: %AbstractFinal: type = class_type @AbstractFinal [concrete]
  72. // CHECK:STDOUT: %pattern_type.2e0: type = pattern_type %AbstractFinal [concrete]
  73. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  74. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  75. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  76. // CHECK:STDOUT: %T.as.Destroy.impl.Op.type.4da: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%AbstractFinal) [concrete]
  77. // CHECK:STDOUT: %T.as.Destroy.impl.Op.0fa: %T.as.Destroy.impl.Op.type.4da = struct_value () [concrete]
  78. // CHECK:STDOUT: %ptr.d4c: type = ptr_type %AbstractFinal [concrete]
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: imports {
  82. // CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
  83. // CHECK:STDOUT: .AbstractFinal = %AbstractFinal.decl
  84. // CHECK:STDOUT: import Cpp//...
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %AbstractFinal.decl: type = class_decl @AbstractFinal [concrete = constants.%AbstractFinal] {} {}
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: file {
  90. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  91. // CHECK:STDOUT: %return.patt: %pattern_type.2e0 = return_slot_pattern [concrete]
  92. // CHECK:STDOUT: %return.param_patt: %pattern_type.2e0 = out_param_pattern %return.patt, call_param0 [concrete]
  93. // CHECK:STDOUT: } {
  94. // CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  95. // CHECK:STDOUT: %AbstractFinal.ref: type = name_ref AbstractFinal, imports.%AbstractFinal.decl [concrete = constants.%AbstractFinal]
  96. // CHECK:STDOUT: %return.param: ref %AbstractFinal = out_param call_param0
  97. // CHECK:STDOUT: %return: ref %AbstractFinal = return_slot %return.param
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: fn @F() -> %return.param: %AbstractFinal {
  102. // CHECK:STDOUT: !entry:
  103. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  104. // CHECK:STDOUT: %.loc11: ref %AbstractFinal = splice_block %return {}
  105. // CHECK:STDOUT: %F.call: init %AbstractFinal = call %F.ref() to %.loc11
  106. // CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc11, constants.%T.as.Destroy.impl.Op.0fa
  107. // CHECK:STDOUT: <elided>
  108. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc11, %T.as.Destroy.impl.Op.specific_fn
  109. // CHECK:STDOUT: %addr: %ptr.d4c = addr_of %.loc11
  110. // CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method(%addr)
  111. // CHECK:STDOUT: return %F.call to %return
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: