Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Find destructor by trait #162

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ppl/src/core.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ fn<T> reference to <ref: Reference<T>> => ref
//=================================

//=================================
// Destoy
// Destructible
//=================================
/// Trait for things that have a destructor
trait Destroy:
trait Destructible:
fn destroy <:&mut Self>
//=================================

Expand Down
2 changes: 1 addition & 1 deletion src/semantics/contexts/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl BuiltinTraits<'_> {
.as_trait()
}

builtin_traits!(clonnable);
builtin_traits!(clonnable, destructible);
}

/// Helper struct to get builtin types
Expand Down
16 changes: 13 additions & 3 deletions src/semantics/contexts/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,19 @@ pub trait Context: FindDeclaration + AddDeclaration + Display {
where
Self: Sized,
{
let ty = self.builtin().types().reference_mut_to(ty);
let name = format!("destroy <:{ty}>");
self.function_with_name(&name)
if ty.is_any_reference() {
return None;
}

match ty {
Type::Class(c) => c
.implements(self.builtin().traits().destructible())
.within(self)
.ok()?
.into_iter()
.next(),
_ => None,
}
}

/// Find clone function for type
Expand Down
8 changes: 4 additions & 4 deletions src/tests/destructor/src/main.ppl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type Destructible
type DestructibleClass

fn destroy <:&mut Destructible> => println "destructor"
fn destroy <:&mut DestructibleClass> => println "destructor"

let mut x = Destructible {}
x = Destructible {} // Should call destructor
let mut x = DestructibleClass {}
x = DestructibleClass {} // Should call destructor

println "done"

Expand Down
16 changes: 8 additions & 8 deletions src/tests/snapshots/ppl__tests__destructor.hir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
source: src/tests/mod.rs
expression: hir
---
type Destructible
type DestructibleClass

fn destroy <$arg0: ReferenceMut<Destructible>> -> None:
let $tmp@54: None = println "destructor"
return ($tmp@54:None)
fn destroy <$arg0: ReferenceMut<DestructibleClass>> -> None:
let $tmp@64: None = println "destructor"
return ($tmp@64:None)

let mut x: Destructible = Destructible { }
destroy (x:Destructible)
(x:Destructible) = Destructible { }
let mut x: DestructibleClass = DestructibleClass { }
destroy (x:DestructibleClass)
(x:DestructibleClass) = DestructibleClass { }
println "done"
destroy (x:Destructible)
destroy (x:DestructibleClass)
16 changes: 8 additions & 8 deletions src/tests/snapshots/ppl__tests__destructor.ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ declare %Integer @integer_from_i64(i64)
define void @main.execute() !dbg !9 {
call void @initialize(), !dbg !10
call void @initialize.1(), !dbg !11
call void @"destroy <:ReferenceMut<Destructible>>"(ptr @x), !dbg !12
call void @"destroy <:ReferenceMut<DestructibleClass>>"(ptr @x), !dbg !12
%1 = alloca ptr, align 8, !dbg !13
store ptr %1, ptr @x, align 8, !dbg !13
%2 = call %String @string_from_c_string_and_length(ptr @2, i64 4), !dbg !14
call void @"println <:String>"(%String %2), !dbg !14
call void @"destroy <:ReferenceMut<Destructible>>"(ptr @x), !dbg !15
call void @"destroy <:ReferenceMut<DestructibleClass>>"(ptr @x), !dbg !15
br label %return, !dbg !15

return: ; preds = %0
ret void
}

define void @"destroy <:ReferenceMut<Destructible>>"(ptr %0) !dbg !16 {
define void @"destroy <:ReferenceMut<DestructibleClass>>"(ptr %0) !dbg !16 {
%"$arg0" = alloca ptr, align 8
store ptr %0, ptr %"$arg0", align 8
%2 = call %String @string_from_c_string_and_length(ptr @1, i64 10), !dbg !17
Expand Down Expand Up @@ -82,17 +82,17 @@ return: ; preds = %0
!4 = !DISubroutineType(types: !5)
!5 = !{!6}
!6 = !DIBasicType(name: "i32", size: 32, encoding: DW_ATE_signed)
!7 = !DILocation(line: 5, column: 31, scope: !3)
!7 = !DILocation(line: 5, column: 16, scope: !3)
!8 = !DILocation(line: 0, scope: !3)
!9 = distinct !DISubprogram(name: "main.execute", linkageName: "main.execute", scope: !2, file: !2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!10 = !DILocation(line: 5, column: 31, scope: !9)
!10 = !DILocation(line: 5, column: 16, scope: !9)
!11 = !DILocation(line: 4, column: 12, scope: !9)
!12 = !DILocation(line: 5, scope: !9)
!13 = !DILocation(line: 5, column: 4, scope: !9)
!14 = !DILocation(line: 7, column: 8, scope: !9)
!15 = !DILocation(line: 4, scope: !9)
!16 = distinct !DISubprogram(name: "destroy <:ReferenceMut<Destructible>>", linkageName: "destroy <:ReferenceMut<Destructible>>", scope: !9, file: !2, line: 2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!17 = !DILocation(line: 2, column: 43, scope: !16)
!18 = !DILocation(line: 2, column: 35, scope: !16)
!16 = distinct !DISubprogram(name: "destroy <:ReferenceMut<DestructibleClass>>", linkageName: "destroy <:ReferenceMut<DestructibleClass>>", scope: !9, file: !2, line: 2, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!17 = !DILocation(line: 2, column: 48, scope: !16)
!18 = !DILocation(line: 2, column: 40, scope: !16)
!19 = distinct !DISubprogram(name: "initialize.1", linkageName: "initialize.1", scope: !9, file: !2, line: 4, type: !4, spFlags: DISPFlagDefinition, unit: !1)
!20 = !DILocation(line: 4, column: 12, scope: !19)
Loading