2026-10-22 12:58:19 +02:00
|
|
|
/// The types the compiler inferred for a file, from its last check.
|
|
|
|
|
///
|
|
|
|
|
/// The compiler hands each program it checked to `Inference.observe`, and the
|
|
|
|
|
/// names in it are read off with their types: each use of a name, at the type
|
|
|
|
|
/// it has there, and each binding, at the type it binds. A declaration that
|
|
|
|
|
/// failed to check has no types, so a name in it has none until it checks.
|
|
|
|
|
module BjolangLsp.Types
|
|
|
|
|
|
|
|
|
|
open System
|
|
|
|
|
open Bjolang
|
|
|
|
|
open Bjolang.Lexer
|
|
|
|
|
open Bjolang.TypedAST
|
|
|
|
|
|
|
|
|
|
/// A name as an expression, and the type it has there. `Generic` when it is
|
|
|
|
|
/// a generic binding, which this use instantiated.
|
|
|
|
|
type Use =
|
|
|
|
|
{ Name: string
|
|
|
|
|
Range: Range
|
|
|
|
|
Type: string
|
|
|
|
|
Generic: bool }
|
|
|
|
|
|
|
|
|
|
/// A name a form binds, and the type it binds it at. `Within` is the binding
|
|
|
|
|
/// form: where the name is written in it is the reader's business, and the
|
|
|
|
|
/// form holds it.
|
|
|
|
|
type Bound =
|
|
|
|
|
{ Name: string
|
|
|
|
|
Within: Range
|
|
|
|
|
Type: string }
|
|
|
|
|
|
2026-11-03 14:45:55 +01:00
|
|
|
/// A field read, `(.x r)` or `(record-ref r x)`: the field, the form, the
|
|
|
|
|
/// field's type there, and the record's type.
|
|
|
|
|
type FieldUse =
|
|
|
|
|
{ Field: string
|
|
|
|
|
Range: Range
|
|
|
|
|
Type: string
|
|
|
|
|
Record: string }
|
|
|
|
|
|
|
|
|
|
type Index =
|
|
|
|
|
{ Uses: Use list
|
|
|
|
|
Bounds: Bound list
|
|
|
|
|
Fields: FieldUse list }
|
2026-10-22 12:58:19 +02:00
|
|
|
|
2026-11-03 14:45:55 +01:00
|
|
|
let private empty = { Uses = []; Bounds = []; Fields = [] }
|
2026-10-22 12:58:19 +02:00
|
|
|
|
|
|
|
|
/// A name the compiler invented in place of one the program wrote: `x__3`,
|
|
|
|
|
/// a generated suspending copy `f__bjo`, and `unused__4` for `_`.
|
|
|
|
|
let private invented (name: string) =
|
|
|
|
|
name.EndsWith "__bjo" || name.StartsWith "unused__" || name.StartsWith "#"
|
|
|
|
|
|
|
|
|
|
/// What the program wrote: a renamed binder without its `__12`.
|
|
|
|
|
let private written (name: string) =
|
|
|
|
|
Text.RegularExpressions.Regex.Replace(name, @"__\d+$", "")
|
|
|
|
|
|
|
|
|
|
/// Reads `decls` into an index of the names in `file`, showing each type
|
|
|
|
|
/// with `env`'s registry: an associated type at a known implementor shows as
|
|
|
|
|
/// what it is there.
|
|
|
|
|
let private read (env: Env) (file: string) (decls: TDecl list) : Index =
|
|
|
|
|
let uses = ResizeArray<Use>()
|
|
|
|
|
let bounds = ResizeArray<Bound>()
|
2026-11-03 14:45:55 +01:00
|
|
|
let fields = ResizeArray<FieldUse>()
|
2026-10-22 12:58:19 +02:00
|
|
|
let inFile (r: Range) = r.File <> "" && IO.Path.GetFullPath r.File = file
|
|
|
|
|
|
2026-11-03 14:45:55 +01:00
|
|
|
// The file's own types by the names it writes: `Leg`, not `fields/Leg`.
|
|
|
|
|
let ownQualifier =
|
|
|
|
|
Text.RegularExpressions.Regex(
|
|
|
|
|
@"(?<![\w./-])" + Text.RegularExpressions.Regex.Escape(IO.Path.GetFileNameWithoutExtension file) + "/"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-22 12:58:19 +02:00
|
|
|
let show (t: HMType) =
|
|
|
|
|
let settled =
|
|
|
|
|
try
|
|
|
|
|
Unification.prune env.Registry t
|
|
|
|
|
with _ ->
|
|
|
|
|
t
|
|
|
|
|
|
2026-11-03 14:45:55 +01:00
|
|
|
ownQualifier.Replace(DotNetInterop.showType settled, "")
|
2026-10-22 12:58:19 +02:00
|
|
|
|
|
|
|
|
let bind (name: string) (within: Range) (t: HMType) =
|
|
|
|
|
if inFile within && not (invented name) then
|
|
|
|
|
bounds.Add
|
|
|
|
|
{ Name = written name
|
|
|
|
|
Within = within
|
|
|
|
|
Type = show t }
|
|
|
|
|
|
|
|
|
|
let rec pattern (p: TypedPattern) =
|
|
|
|
|
match p.Node with
|
|
|
|
|
| TPIdent n -> bind n p.Range p.Type
|
|
|
|
|
| TPAs(inner, n) ->
|
|
|
|
|
bind n p.Range p.Type
|
|
|
|
|
pattern inner
|
|
|
|
|
| TPList(ps, rest)
|
|
|
|
|
| TPVec(ps, rest)
|
|
|
|
|
| TPArray(ps, rest) ->
|
|
|
|
|
List.iter pattern ps
|
|
|
|
|
Option.iter pattern rest
|
|
|
|
|
| TPTuple ps
|
|
|
|
|
| TPConstruct(_, ps)
|
|
|
|
|
| TPOr ps
|
|
|
|
|
| TPAnd ps -> List.iter pattern ps
|
|
|
|
|
| TPRecord(_, fields) -> fields |> List.iter (snd >> pattern)
|
|
|
|
|
| TPApp(_, inner) -> pattern inner
|
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
|
|
let parameters (names: string list) (t: HMType) (within: Range) =
|
|
|
|
|
match Unification.prune env.Registry t with
|
|
|
|
|
| TFun(args, _, _) when args.Length = names.Length -> List.iter2 (fun n a -> bind n within a) names args
|
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
|
|
let node () (e: TypedExpr) =
|
|
|
|
|
match e.Node with
|
|
|
|
|
| TIdent(name, tArgs) when inFile e.Range && not (invented name) ->
|
|
|
|
|
uses.Add
|
|
|
|
|
{ Name = written name
|
|
|
|
|
Range = e.Range
|
|
|
|
|
Type = show e.Type
|
|
|
|
|
Generic = not tArgs.IsEmpty }
|
2026-11-03 14:45:55 +01:00
|
|
|
| TGetField(target, field) when inFile e.Range ->
|
|
|
|
|
fields.Add
|
|
|
|
|
{ Field = field
|
|
|
|
|
Range = e.Range
|
|
|
|
|
Type = show e.Type
|
|
|
|
|
Record = show target.Type }
|
2026-10-22 12:58:19 +02:00
|
|
|
| TLet(name, _, _, value, _) -> bind name e.Range value.Type
|
|
|
|
|
| TLetMutable(name, value, _) -> bind name e.Range value.Type
|
|
|
|
|
| TLetRec(bindings, _) -> for (name, _, _, value) in bindings do bind name e.Range value.Type
|
|
|
|
|
| TLetTuple(names, value, _) ->
|
|
|
|
|
match Unification.prune env.Registry value.Type with
|
|
|
|
|
| TTuple items when items.Length = names.Length -> List.iter2 (fun n t -> bind n e.Range t) names items
|
|
|
|
|
| _ -> ()
|
|
|
|
|
| TLambda(names, _) -> parameters names e.Type e.Range
|
|
|
|
|
| TMatch(_, clauses) -> for c in clauses do pattern c.Pattern
|
|
|
|
|
| TDefMatch(binder, _, _, arms) ->
|
|
|
|
|
pattern binder
|
|
|
|
|
for a in arms do pattern a.Pattern
|
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
|
|
let expr (e: TypedExpr) = TypeVisitor.foldExpr node () e
|
|
|
|
|
|
|
|
|
|
let rec decl (d: TDecl) =
|
|
|
|
|
match d with
|
|
|
|
|
| TModule(_, inner, _) -> List.iter decl inner
|
|
|
|
|
| TImpl(_, _, _, _, _, _, methods, _) -> List.iter decl methods
|
|
|
|
|
| TDefun(name, _, _, _, _, _, _, _, _) when invented name -> ()
|
|
|
|
|
| TDefun(name, _, mandatory, keywords, rest, ret, effect, body, r) ->
|
|
|
|
|
let restArray = rest |> Option.map (fun (_, t) -> TCon("Array", [ t ])) |> Option.toList
|
|
|
|
|
let funType = TFun((mandatory |> List.map snd) @ (keywords |> List.map (fun (_, t, _) -> t)) @ restArray, ret, effect)
|
|
|
|
|
bind name r funType
|
|
|
|
|
|
|
|
|
|
for (n, t) in mandatory do
|
|
|
|
|
bind n r t
|
|
|
|
|
|
|
|
|
|
for (n, t, value) in keywords do
|
|
|
|
|
bind n r t
|
|
|
|
|
expr value
|
|
|
|
|
|
|
|
|
|
rest |> Option.iter (fun (n, t) -> bind n r (TCon("Array", [ t ])))
|
|
|
|
|
expr body
|
|
|
|
|
| TDef(name, value, t, r)
|
|
|
|
|
| TDefMutable(name, value, t, r) ->
|
|
|
|
|
bind name r t
|
|
|
|
|
expr value
|
|
|
|
|
| TDefTuple(names, value, t, r) ->
|
|
|
|
|
match Unification.prune env.Registry t with
|
|
|
|
|
| TTuple items when items.Length = names.Length -> List.iter2 (fun n t -> bind n r t) names items
|
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
|
|
expr value
|
|
|
|
|
| TDefPattern(p, value, _, _) ->
|
|
|
|
|
pattern p
|
|
|
|
|
expr value
|
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
|
|
List.iter decl decls
|
2026-11-03 14:45:55 +01:00
|
|
|
{ Uses = List.ofSeq uses
|
|
|
|
|
Bounds = List.ofSeq bounds
|
|
|
|
|
Fields = List.ofSeq fields }
|
2026-10-22 12:58:19 +02:00
|
|
|
|
|
|
|
|
/// Each file's index, from its last check.
|
Checks run on a thread of their own
A check held up every request, because the compiler could run one
compilation at a time. It can now run each in a context of its own.
- A check runs on a checking thread, in Compilation.inNew. The thread
reports a check to the thread that serves requests, which writes to the
client.
- The outline, definitions, references, completion, signature help and
formatting are answered while a check runs.
- A hover, a code action and bjolang/expandMacro are answered when the
check of their file ends, with the results of the text as it stands.
Other requests are served meanwhile.
- A file that changes again before its check starts is checked once.
- The tables that a check writes are concurrent dictionaries.
- Standard output is the protocol's; the console's output goes to standard
error.
- smoke: with a check of 1.6 s running, an outline is answered in 0.1 s.
Needs the compiler at its commit "Diagnostics.captured collects the output
of its own context".
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 09:32:15 +01:00
|
|
|
let private byFile = Collections.Concurrent.ConcurrentDictionary<string, Index>()
|
2026-10-22 12:58:19 +02:00
|
|
|
|
|
|
|
|
/// Each file's text as its last check read it: where an index's ranges are.
|
Checks run on a thread of their own
A check held up every request, because the compiler could run one
compilation at a time. It can now run each in a context of its own.
- A check runs on a checking thread, in Compilation.inNew. The thread
reports a check to the thread that serves requests, which writes to the
client.
- The outline, definitions, references, completion, signature help and
formatting are answered while a check runs.
- A hover, a code action and bjolang/expandMacro are answered when the
check of their file ends, with the results of the text as it stands.
Other requests are served meanwhile.
- A file that changes again before its check starts is checked once.
- The tables that a check writes are concurrent dictionaries.
- Standard output is the protocol's; the console's output goes to standard
error.
- smoke: with a check of 1.6 s running, an outline is answered in 0.1 s.
Needs the compiler at its commit "Diagnostics.captured collects the output
of its own context".
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 09:32:15 +01:00
|
|
|
let private checkedTexts = Collections.Concurrent.ConcurrentDictionary<string, string>()
|
2026-10-22 12:58:19 +02:00
|
|
|
|
|
|
|
|
/// The text of `path` its last check read, if it was checked.
|
|
|
|
|
let checkedText (path: string) : string option =
|
|
|
|
|
match checkedTexts.TryGetValue(IO.Path.GetFullPath path) with
|
|
|
|
|
| true, text -> Some text
|
|
|
|
|
| _ -> None
|
|
|
|
|
|
|
|
|
|
/// Runs `check` of `text`, the text of `path`, with the compiler handing over
|
|
|
|
|
/// what it checked, and keeps the index of the names in `path` as the file's.
|
|
|
|
|
/// A check that got no further than the reader leaves the file with none.
|
|
|
|
|
let recording (path: string) (text: string) (check: unit -> 'a) : 'a =
|
|
|
|
|
let full = IO.Path.GetFullPath path
|
|
|
|
|
let mutable index = empty
|
|
|
|
|
|
2026-11-25 08:39:42 +01:00
|
|
|
Inference.observe.Value <-
|
2026-10-22 12:58:19 +02:00
|
|
|
Some(fun env decls ->
|
|
|
|
|
// The first program with names in the file is the file's: one
|
|
|
|
|
// checked before it is a dependency's.
|
2026-11-03 14:45:55 +01:00
|
|
|
if index.Uses.IsEmpty && index.Bounds.IsEmpty && index.Fields.IsEmpty then
|
2026-10-22 12:58:19 +02:00
|
|
|
index <- read env full decls)
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
check ()
|
|
|
|
|
finally
|
2026-11-25 08:39:42 +01:00
|
|
|
Inference.observe.Value <- None
|
2026-10-22 12:58:19 +02:00
|
|
|
byFile[full] <- index
|
|
|
|
|
checkedTexts[full] <- text
|
|
|
|
|
|
|
|
|
|
let private holds (inner: Range) (outer: Range) =
|
|
|
|
|
(inner.Start.Line > outer.Start.Line
|
|
|
|
|
|| (inner.Start.Line = outer.Start.Line && inner.Start.Column >= outer.Start.Column))
|
|
|
|
|
&& (inner.End.Line < outer.End.Line
|
|
|
|
|
|| (inner.End.Line = outer.End.Line && inner.End.Column <= outer.End.Column))
|
|
|
|
|
|
|
|
|
|
let private size (r: Range) = (r.End.Line - r.Start.Line) * 100000 + (r.End.Column - r.Start.Column)
|
|
|
|
|
|
|
|
|
|
let private indexOf (path: string) =
|
|
|
|
|
match byFile.TryGetValue(IO.Path.GetFullPath path) with
|
|
|
|
|
| true, index -> Some index
|
|
|
|
|
| _ -> None
|
|
|
|
|
|
|
|
|
|
/// The use of `name` written at `at`, if the last check typed it.
|
|
|
|
|
let useAt (path: string) (name: string) (at: Range) : Use option =
|
|
|
|
|
indexOf path
|
|
|
|
|
|> Option.bind (fun index ->
|
|
|
|
|
index.Uses
|
|
|
|
|
|> List.tryFind (fun u -> u.Name = name && u.Range.Start = at.Start))
|
|
|
|
|
|
|
|
|
|
/// The type `name`, written at `at` in its binding form, is bound at: the
|
|
|
|
|
/// innermost binding of that name that holds it.
|
|
|
|
|
let boundAt (path: string) (name: string) (at: Range) : string option =
|
|
|
|
|
indexOf path
|
|
|
|
|
|> Option.bind (fun index ->
|
|
|
|
|
index.Bounds
|
|
|
|
|
|> List.filter (fun b -> b.Name = name && holds at b.Within)
|
|
|
|
|
|> List.sortBy (fun b -> size b.Within)
|
|
|
|
|
|> List.tryHead
|
|
|
|
|
|> Option.map (fun b -> b.Type))
|
2026-11-03 14:45:55 +01:00
|
|
|
|
|
|
|
|
/// The field read written at `at`: the innermost one of `field` whose form
|
|
|
|
|
/// holds it.
|
|
|
|
|
let fieldAt (path: string) (field: string) (at: Range) : FieldUse option =
|
|
|
|
|
indexOf path
|
|
|
|
|
|> Option.bind (fun index ->
|
|
|
|
|
index.Fields
|
|
|
|
|
|> List.filter (fun f -> f.Field = field && holds at f.Range)
|
|
|
|
|
|> List.sortBy (fun f -> size f.Range)
|
|
|
|
|
|> List.tryHead)
|