lib/std/lang/types.rad 1.6 KiB raw
1
//! Shared Radiance language types.
2
3
/// Ownership and safety class for pointer-like types.
4
export union PointerClass: Copy {
5
    /// Owned pointer, eg. `*T`.
6
    Owned,
7
    /// Reference, borrowed pointer, eg. `&T`.
8
    Ref,
9
    /// Reference retained by one resolved lexical region.
10
    Region(*unsafe Region),
11
    /// Unsafe, raw pointer, eg. `*unsafe T`.
12
    Unsafe,
13
}
14
15
/// Source construct that introduces a region identity.
16
export union RegionOrigin: Copy {
17
    /// Caller-supplied declaration parameter.
18
    Parameter,
19
    /// Concrete region delimited by a lexical block.
20
    Block,
21
}
22
23
/// Semantic identity and ancestry of a source region.
24
export record Region: Copy {
25
    /// Globally unique AST node ID of the region declaration.
26
    id: u32,
27
    /// Source construct that supplies the region lifetime.
28
    origin: RegionOrigin,
29
    /// Source spelling used in diagnostics.
30
    name: *[u8],
31
    /// Proven enclosing region, if one is declared.
32
    parent: ?*unsafe Region,
33
}
34
35
/// Return whether a pointer class denotes checked borrowed storage.
36
export fn isReference(class: PointerClass) -> bool {
37
    match class {
38
        case PointerClass::Ref, PointerClass::Region(_) => return true,
39
        else => return false,
40
    }
41
}
42
43
/// Return whether a region is equal to, or contains, another region.
44
/// Parent links must form an acyclic graph within one resolver.
45
export unsafe fn regionContains(parent: *unsafe Region, child: *unsafe Region) -> bool {
46
    let mut current: ?*unsafe Region = child;
47
    while let region = current {
48
        if region.id == parent.id {
49
            return true;
50
        }
51
        set current = region.parent;
52
    }
53
    return false;
54
}