destroy.carbon 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. package Core library "prelude/destroy";
  5. import library "prelude/types/bool";
  6. import library "prelude/types/int_literal";
  7. // Object destruction, including running `fn destroy()`. Note this is distinct
  8. // from memory deallocation.
  9. interface Destroy {
  10. fn Op[addr self: Self*]();
  11. }
  12. // Add destruction for essential builtin types. This can't be done earlier
  13. // because `Destroy` is using them.
  14. // TODO: This should match trivial destruction.
  15. impl bool as Destroy {
  16. fn Op[addr self: Self*]() = "no_op";
  17. }
  18. impl type as Destroy {
  19. fn Op[addr self: Self*]() = "no_op";
  20. }
  21. impl forall [PointeeT:! type] PointeeT* as Destroy {
  22. fn Op[addr self: Self*]() = "no_op";
  23. }
  24. // Handles builtin aggregate type destruction.
  25. private fn CanAggregateDestroy() -> type = "type.can_aggregate_destroy";
  26. impl forall [AggregateT:! CanAggregateDestroy()] AggregateT as Destroy {
  27. fn Op[addr self: Self*]() = "type.aggregate_destroy";
  28. }
  29. // `const` instances always use the non-`const` destructor.
  30. impl forall [NonConstT:! Destroy] const NonConstT as Destroy {
  31. fn Op[addr self: Self*]() { (self unsafe as NonConstT*)->(Destroy.Op)(); }
  32. }