Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.42.0

0.42.0 / src / sema / interop.lucb

#==============================================================================================
#
#   interop - Checked ownership declarations for the package description
#
#   DESCRIPTION:
#       Recognition uses the standard module's declaration identity, never a user type's
#       short name.
#
#==============================================================================================

import front.ast
import sema.check
import sema.types

## The checked ownership facts of a native interop type, read from its interop.Type constant.
pub struct Declaration:
    ## The interop.Type / interop.ViewType constant that declares the ownership.
    pub let descriptor: ast.Node*
    ## The disposal method or function, or none for a view.
    pub let dispose: ast.Node*?
    ## Whether the owner exposes a public disposal method (closeable = true).
    pub let closeable: bool
    ## Whether this is a borrowing view (interop.ViewType) rather than an owner.
    pub let view: bool
    ## Whether a view permits public mutation.
    pub let mutable: bool

## Whether type `id` is the standard `interop` module's declaration named `name`.
pub func standard(checker: check.Checker*, id: u32, name: str) -> bool:
    let native = checker.table.get(id)
    let declaration = native.decl else return false
    for module in checker.modules.span():
        if module.is_prelude and module.name == "interop":
            return check.find_decl(module.tree, name) == declaration
    return false

## A carrier names its concrete owner independently of aliases and import spelling.
pub func reference(checker: check.Checker*, id: u32) -> u32?:
    let native = checker.table.get(id)
    if native.args.length == 1 and (standard(checker, id, "Reference") or standard(checker, id, "View")):
        return native.args[0]
    return none

func argument(call: ast.Node*, name: str) -> ast.Node*?:
    var cursor = call.body
    while let parameter = cursor:
        if parameter.text == name:
            return parameter.left
        cursor = parameter.next
    return none

## There is exactly one public ownership declaration beside the concrete type.
## Its checked function types already guarantee disposal/trace ABI correctness.
pub func owned(checker: check.Checker*, id: u32) -> Declaration?!:
    let native = checker.table.get(id)
    if native.kind != types.Kind.struct_ or native.args.length != 0:
        return none
    let type_decl = native.decl else return none
    let owner = try checker.owner_of(type_decl) else return none
    let module = checker.modules.get(owner)
    if check.find_decl(module.tree, type_decl.text) == type_decl:
        var found: Declaration? = none
        var cursor = module.tree.body
        while let declaration = cursor:
            cursor = declaration.next
            if declaration.kind != ast.Kind.constant or not declaration.has(ast.flag_pub):
                continue
            let carrier = checker.table.get(declaration.type_id)
            let view = standard(checker, declaration.type_id, "ViewType")
            if carrier.args.length != 1 or carrier.args[0] != id or not (view or standard(checker, declaration.type_id, "Type")):
                continue
            if found != none:
                error(check.type_error, "a native type must have exactly one public interop ownership declaration")
            if not type_decl.has(ast.flag_pub):
                error(check.type_error, "a public interop.Type must describe a public struct")
            let value = declaration.left else error(check.type_error, "an interop.Type needs its constant constructor")
            if value.kind != ast.Kind.call:
                error(check.type_error, "declare native ownership with a direct constant interop.Type constructor")
            var dispose: ast.Node*? = none
            var closeable = false
            var mutable = false
            if view:
                if let option = argument(value, "mutable"):
                    mutable = checker.constant_bool(option) else error(check.type_error, "interop.ViewType mutable must be constant")
                if not mutable:
                    var members = type_decl.body
                    while let member = members:
                        if member.has(ast.flag_pub) and member.has(ast.flag_mutating) and member.text != "init":
                            error(check.type_error, "a view with public methods that change their receiver must declare mutable = true")
                        members = member.next
            else:
                let cleanup = argument(value, "dispose") else error(check.type_error, "name the interop.Type disposal argument explicitly")
                dispose = cleanup.resolved else error(check.type_error, "interop.Type disposal must name a concrete method or function")
                if let option = argument(value, "closeable"):
                    closeable = checker.constant_bool(option) else error(check.type_error, "interop.Type closeable must be constant")
                if closeable:
                    let method = dispose else error(check.type_error, "a closeable owner needs its disposal method")
                    if check.find_decl(type_decl, method.text) != method or not method.has(ast.flag_pub) or method.has(ast.flag_static) or method.right != none:
                        error(check.type_error, "closeable ownership requires a public disposal method without parameters")
            found = Declaration(descriptor = declaration, dispose = dispose, closeable = closeable, view = view, mutable = mutable)
        return found
    return none