lib/std/lang/lower/regions.rad 4.6 KiB raw
1
//! Borrowed result contracts for RIL function boundaries.
2
//!
3
//! The region positions of a type number the regions of its references: the
4
//! region of a reference's address, then the positions of its target, or the
5
//! region parameters of a record or union. The layout of a type has one region
6
//! per position, so a contract names result and source regions by position.
7
8
use super::Lowerer;
9
use super::LowerError;
10
use std::lang::resolver::Type;
11
use std::lang::il;
12
use std::lang::resolver;
13
use std::lang::types;
14
15
/// Lower the region sources of a function result, and the sources that it stores
16
/// into the objects of its parameters, to RIL borrowed sources.
17
export fn contract(
18
    low: &mut Lowerer, fnType: *resolver::FnType, buffered: bool
19
) -> *[il::Borrow] throws (LowerError) {
20
    let res = &low.resolver;
21
    let hidden: u32 = 1 if buffered else 0;
22
    let results = try represented(res, *fnType.returnType);
23
    let mut out: *mut [il::Borrow] = &mut [];
24
    for borrow in fnType.borrows {
25
        for region, result in results {
26
            if region == borrow.result {
27
                try sources(
28
                    res, fnType, nil, result, borrow.param, borrow.source, hidden, &mut out
29
                );
30
            }
31
        }
32
    }
33
    for store in resolver::refs::storesOf(res, fnType) {
34
        let target = *fnType.paramTypes[store.target];
35
        // A reference's object has the regions of its pointee, after its address.
36
        let skip: u32 = 1 if addressed(res, target) else 0;
37
        for region, k in try represented(res, target) {
38
            if region == store.region and k >= skip {
39
                let into = store.target + hidden;
40
                try sources(
41
                    res, fnType, into, k - skip, store.param, store.source, hidden, &mut out
42
                );
43
            }
44
        }
45
    }
46
    return &out[..];
47
}
48
49
/// Append a borrow of each RIL region of parameter `param` that represents
50
/// position `position` of its type, as a source of region `result` of the
51
/// result, or of the object of parameter `target`.
52
fn sources(
53
    res: &resolver::Resolver,
54
    fnType: *resolver::FnType,
55
    target: ?u32,
56
    result: u32,
57
    param: u32,
58
    position: u32,
59
    hidden: u32,
60
    out: &mut *mut [il::Borrow]
61
) throws (LowerError) {
62
    let input = *fnType.paramTypes[param];
63
    let pointer = addressed(res, input);
64
    for at, k in try represented(res, input) {
65
        if at <> position {
66
            continue;
67
        }
68
        // A reference passes its target's layout, so its address is the parameter.
69
        let mut source: ?u32 = k;
70
        if pointer {
71
            set source = nil;
72
            if k > 0 {
73
                set source = k - 1;
74
            }
75
        }
76
        let item = il::Borrow { target, result, param: param + hidden, source };
77
        try out.append(item, res.arena) catch {
78
            throw LowerError::AllocationFailed;
79
        };
80
    }
81
}
82
83
/// Return the field of a record that is passed as its only field.
84
fn scalarField(res: &resolver::Resolver, ty: Type) -> ?resolver::RecordField {
85
    if not super::isScalarRecord(res, ty) {
86
        return nil;
87
    }
88
    let case Type::Nominal(id) = ty else return nil;
89
    let case resolver::NominalType::Record(info) = res.nominals[*id] else return nil;
90
    return info.fields[0];
91
}
92
93
/// Whether the RIL value of type `ty` is a non-owning pointer, whose first region
94
/// is its address. A record passed as its field has the value of the field.
95
fn addressed(res: &resolver::Resolver, ty: Type) -> bool {
96
    if super::isAggregateType(res, ty) {
97
        return false;
98
    }
99
    if let field = scalarField(res, ty) {
100
        return addressed(res, field.fieldType);
101
    }
102
    match ty {
103
        case Type::Optional(inner)       => return addressed(res, *inner),
104
        case Type::Pointer { class, .. } => return class == types::PointerClass::Ref,
105
        else                             => return false,
106
    }
107
}
108
109
/// Region position of a type for each region of its RIL value: the address and
110
/// pointee regions of a pointer, the layout regions of an aggregate, or those of
111
/// the field of a record that is passed as its field.
112
fn represented(res: &resolver::Resolver, ty: Type) -> *[u32] throws (LowerError) {
113
    let mut out: *mut [u32] = &mut [];
114
    if let field = scalarField(res, ty) {
115
        for at in try represented(res, field.fieldType) {
116
            try out.append(field.regions[at], res.arena) catch {
117
                throw LowerError::AllocationFailed;
118
            };
119
        }
120
        return &out[..];
121
    }
122
    for i in 0 .. resolver::refs::positions(res, ty) {
123
        try out.append(i, res.arena) catch {
124
            throw LowerError::AllocationFailed;
125
        };
126
    }
127
    return &out[..];
128
}