Explorer: Add virtual destructor support (#2695)

### Features

* Add virtual `destructor`s support (Closes #2521)
* Check virtual override for virtual destructors
* Error if attempting to `Delete` a class that does not have virtual destructors from a base class pointer 

### Implementation

* Update parser to support virtual override introducers for destructors
* Check virtual override for class destructor and add to class vtable if necessary
* Add corresponding tests

### Notes

Contrary to initial implementation, this implementation leverages the `Address` structure and implements a new `Address::DowncastedAddress()` method to get address from child most class from a base class address. This avoids the need to use `GetAllocationId` and its issues when it comes to having multiple values for an `AllocationId`.

### Next work

Following this PR, we need to:

* Check when using `Delete` that the class was allocated with `New` (WIP)
* Drop the old `GetAllocationId(Value*)` in favor of a better system (WIP)
This commit is contained in:
Adrien Leravat
2023-03-22 09:58:30 -07:00
committed by GitHub
parent 782bd87316
commit df289efac4
14 changed files with 320 additions and 20 deletions
+35 -8
View File
@@ -1449,15 +1449,42 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
CARBON_CHECK(args.size() == 1);
CARBON_CHECK(act.pos() > 0);
const auto* ptr = cast<PointerValue>(args[0]);
if (act.pos() == 1) {
CARBON_ASSIGN_OR_RETURN(
const auto* pointee,
this->heap_.Read(ptr->address(), exp.source_loc()));
return todo_.Spawn(std::make_unique<DestroyAction>(
arena_->New<LValue>(ptr->address()), pointee));
CARBON_ASSIGN_OR_RETURN(const auto* pointee,
heap_.Read(ptr->address(), exp.source_loc()));
if (const auto* class_value = dyn_cast<NominalClassValue>(pointee)) {
// Handle destruction from base class pointer.
const auto* child_class_value = *class_value->class_value_ptr();
bool is_subtyped = child_class_value != class_value;
if (is_subtyped) {
// Error if destructor is not virtual.
const auto& class_type =
cast<NominalClassType>(class_value->type());
const auto& class_decl = class_type.declaration();
if ((*class_decl.destructor())->virt_override() ==
VirtualOverride::None) {
return ProgramError(exp.source_loc())
<< "Deallocating a derived class from base class "
"pointer requires a virtual destructor";
}
}
const Address obj_addr = is_subtyped
? ptr->address().DowncastedAddress()
: ptr->address();
if (act.pos() == 1) {
return todo_.Spawn(std::make_unique<DestroyAction>(
arena_->New<LValue>(obj_addr), child_class_value));
} else {
heap_.Deallocate(obj_addr);
return todo_.FinishAction(TupleValue::Empty());
}
} else {
heap_.Deallocate(ptr->address());
return todo_.FinishAction(TupleValue::Empty());
if (act.pos() == 1) {
return todo_.Spawn(std::make_unique<DestroyAction>(
arena_->New<LValue>(ptr->address()), pointee));
} else {
heap_.Deallocate(ptr->address());
return todo_.FinishAction(TupleValue::Empty());
}
}
}
case IntrinsicExpression::Intrinsic::Rand: {