ludic/selfhost/backend/emit_vis.ludic
Orkuncakilkaya ce80e64b24 feat(lang): 0.S - components take states in their header; migrate writes them, never inside a name, never outside the programs given, never into a package's state
- component Name (a: mut A, b: B) { ... }: every getter, default, function and event takes the
  header's states before the instance; a member's call to another passes them on; the glue is
  supplied them; the template never sees them; a read-only one is read-only in every member
- ludic migrate state: a component's members' needs go into its header (added to an existing one,
  mut added where now changed); a field read or a member call inside a component is the compiler's,
  so nothing is written inside a name and no ', )' is left; an entry point that declares states
  already gets the rest after them
- a program's module named like a package gets <Name>AppState; a program's own file its own state;
  a friend module's files go by directory; a package's settable var stays state
- it writes only under the programs and directories given (and runtime/ with --runtime), and
  refuses the whole run naming any other file that would have to change
- a name a package already moved into its state is rewritten through it; a read of the runtime's
  var through the runtime function that answers it (gl_w: gl_width())
- a state's instance supplied by the runtime is not a uses reference
- tests: state/component, rejected/state_component_ro, rendering/ui_render3d, migrate component
  and foreign cases

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 17:11:15 +03:00

62 lines
2.9 KiB
Text

# emit_vis.ludic — L3: module scope. A file belongs to the module its own `module NAME` line or
# its importer's names (frontend/parse.ludic, module_of); a file in no module is public. A
# reference from one module to another module's function, global, record or event needs that
# declaration `export`ed, and - when the referring module says `uses` - that module (or, for a
# package with no module line, the package) named there (frontend/modules.ludic), unless the
# referring module is a friend of it (a test harness). The engine's runtime needs no naming.
# LUDIC_VIS_REPORT=1 prints every violation as `vis: <file>: <module>.<name>` (a private name) or
# `uses: <file>: ...` (a module not in the uses list) and goes on, so a tool can add the exports
# and the uses a codebase needs before the rule is switched on for it.
var g_vis_report: int = -1
var g_vis_off: bool = false # a port's binding: checked where it is written (ports.ludic)
function vis_reporting() -> bool {
if g_vis_report < 0 {
g_vis_report = 0
if getenv("LUDIC_VIS_REPORT") != null { g_vis_report = 1 }
}
return g_vis_report == 1
}
# `what` is the name as written at the reference
# a generic function's instance is named for its type arguments (grow$int); a message names grow
function vis_plain(what: pointer) -> pointer {
var i = 0
while i < len(what) {
if what[i] == '$' { return what[0 .. i] }
i += 1
}
return what
}
function vis_say(m: pointer) -> void {
file_write(file_stderr(), m, len(m))
}
function vis_check(d: Node, what0: pointer) -> void {
if d == null { return }
port_check_bound(d)
deps_edge(d, what0) # LUDIC_DEPS: the graph ludic deps reads (emit_deps.ludic)
if g_vis_off { return }
if d.file == null { return }
# 0.S: a state's instance, supplied by the runtime (a component's or a view's glue, an entry's
# parameters): the code that names it only passes it on
if d.kind == N_VAR and d.uns == 1 and is_state_ty(d.ty) and len(d.s) > 6 and (d.s[0 .. 6] == "state$") { return }
let what = vis_plain(what0)
var here = g_err_file
if here == null { return }
let from = module_of(here)
let uto = module_for_uses(d.file)
if not (uto == "") and not (from == uto) and not module_friend_of(from, uto) and not module_may_use(from, uto) {
if vis_reporting() {
vis_say(`uses: {d.file}:{itoa(d.line)}: {uto}.{what} used from {here} (module {from})\n`)
} else {
perr(`{from} uses {uto}.{what} ({d.file}:{itoa(d.line)}): add 'uses {uto}' to {from}'s module line, or take it through a port`)
}
}
let to = module_of(d.file)
if (to == "") or (from == to) { return }
if module_friend_of(from, to) { return }
if d.vis == 1 { return }
if vis_reporting() {
vis_say(`vis: {d.file}:{itoa(d.line)}: {to}.{what} used from {here}\n`)
return
}
perr(`{what} is private to module {to}; mark it 'export' where it is declared ({d.file})`)
}