A module declares parameters as its first declaration, `#module_parameters(STB := false, CACHE: s32 = 8)`, and an import picks their values, `#import "fonts"(STB = true)`. Each parameter is an ordinary constant of the module, so `#if` keeps unchosen code out of C, Go, ZX, Wasm and native code alike. One program binds a module's parameters once. Every import has to give every parameter the same value, so disagreeing imports fail with module.parameter_conflict instead of compiling a second copy. Unknown names and values that do not fit their type (integers by range) fail with module.parameter_unknown and module.parameter_type at the import. The grammar and literal rules live in compiler_declaration.zi. The bindings are checked IR, ZirModule.parameters and ZirImport.parameters, so they port with the checker to Ziran. Saved .zir becomes version 65. It keeps each import's canonical list and each module's types, defaults and bound values. A saved import that disagrees with its module is rejected like source, and so is a bound value that differs from the module's constant. The unused reserved import slot is gone. Also: a constant whose string holds `#import`, such as module source in #string, is no longer parsed as an import. The C include path now lists the IR headers first, so a stale copy that another generated module keeps can't shadow them in an incremental build. tests/module_parameters_cases.zi runs two programs that share one module with different choices. They run from source and saved IR on C, Go, the engine, native code and Wasm, along with the named rejections and edited saved IR. Grammar cases are in tests/spec/compiler_declaration_test.zi.
168 lines
16 KiB
Ziran
168 lines
16 KiB
Ziran
#import "package_common"
|
|
|
|
#scope_file
|
|
PkgExplanation :: struct {
|
|
prefix: string
|
|
summary: string
|
|
action: string
|
|
}
|
|
|
|
#scope_file
|
|
DIAGNOSTIC_CODES: string = "assembly.codec\nassembly.container\nassembly.encode\nassembly.execute\nassembly.gap.cpu-feature\nassembly.gap.machine-host\nassembly.gap.machine-parallel\nassembly.gap.machine-platform-service-unavailable\nassembly.gap.os-service\nassembly.gap.target-unimplemented\nassembly.inspect\nassembly.lower\nassembly.machine\nassembly.optimize\nassembly.output\nassembly.verify\ncheck.array_signature\ncheck.assembly\ncheck.assert\ncheck.cleanup\ncheck.compile_time\ncheck.constant\ncheck.constant_type\ncheck.consteval\ncheck.enum\ncheck.enum_scope\ncheck.file_scope\ncheck.foreign\ncheck.global\ncheck.if_case\ncheck.import\ncheck.inline\ncheck.jai_syntax\ncheck.record\ncheck.signature\ncheck.slice_lifetime\ncheck.slice_signature\ncheck.slot_initializer\ncheck.slot_signature\ncheck.specialize\ncheck.storage\ncheck.template\ncheck.type\ncheck.type_application\ncheck.type_order\ncheck.using_scope\ncheck.vec_copy\ncommand.arguments\ncompiler.context\ncompiler.internal\ncompiler.memory\nemit.array_count\nemit.array_element\nemit.array_target\nemit.expression\nemit.loop_nesting\nemit.parallel\nformat.changed\nformat.input\nformat.output\ngpu.allocator\ngpu.device\ngpu.pointer\nhost.bind\nlaw.gate\nlaw.identity\nlaw.waiver\nlint.bool\nlint.cast\nlint.char\nmodule.ambiguous\nmodule.compile_cycle\nmodule.compile_depth\nmodule.duplicate\nmodule.extension\nmodule.identity\nmodule.input\nmodule.limit\nmodule.not_found\nmodule.parameter_conflict\nmodule.parameter_type\nmodule.parameter_unknown\nmodule.path\nmodule.root\npackage.ambiguous\npackage.command\npackage.identity\npackage.map\npackage.module\npackage.not_found\npackage.process\npackage.project\npackage.scope\npackage.tool\nparallel.effect\nparallel.leave\nparallel.memory\nparallel.nest\nparallel.region\nparse.caller_location\nparse.fatal\nparse.jai_char\nparse.jai_syntax\nparse.modifier\nparse.procedure_name\nparse.syntax\nparse.this\nproof.claim\nproof.graph\nproof.identity\nproof.syntax\nwasm.container\nwasm.gap.address\nwasm.gap.aggregate\nwasm.gap.allocate\nwasm.gap.call\nwasm.gap.compare\nwasm.gap.constant\nwasm.gap.control\nwasm.gap.convert\nwasm.gap.data-size\nwasm.gap.edge\nwasm.gap.entry\nwasm.gap.float-operator\nwasm.gap.float-width\nwasm.gap.foreign-service\nwasm.gap.frame\nwasm.gap.fuel\nwasm.gap.function\nwasm.gap.global\nwasm.gap.global-constant\nwasm.gap.global-layout\nwasm.gap.host\nwasm.gap.host-capability-abi\nwasm.gap.initializer\nwasm.gap.instruction\nwasm.gap.integer-width\nwasm.gap.load\nwasm.gap.offset\nwasm.gap.operand\nwasm.gap.operator\nwasm.gap.platform-service-abi\nwasm.gap.platform-service-unavailable\nwasm.gap.resize\nwasm.gap.result\nwasm.gap.signature\nwasm.gap.store\nwasm.gap.type\nwasm.input\nwasm.output\nwasm.verify\nzir.input\nzir.invalid\nzir.noncanonical\nzir.output\nzir.validation\nzir_c.body\nzir_c.entry\nzir_c.enum\nzir_c.global\nzir_c.record\nzir_c.toolchain\nzir_go.bind\nzir_go.body\nzir_go.callback\nzir_go.enum\nzir_go.exe\nzir_go.export\nzir_go.global\nzir_go.import\nzir_go.parameter\nzir_go.tests\nzir_go.type\nzir_go.union\nzx.assets\nzx.bind\nzx.call\nzx.capability\nzx.container\nzx.debug\nzx.entry\nzx.host_alias\nzx.input\nzx.module\nzx.native\nzx.output\nzx.runtime\nzx.state\nzx.statement\nzx.union\nzx.wasm\n";
|
|
|
|
PkgStartsWith :: (value: string, prefix: string) -> bool {
|
|
if prefix.count > value.count { return false }
|
|
return value[0:prefix.count] == prefix
|
|
}
|
|
|
|
PkgKnownDiagnostic :: (code: string) -> bool {
|
|
at: s32 = 0
|
|
while at < cast(s32)DIAGNOSTIC_CODES.count {
|
|
end: s32 = at
|
|
while end < cast(s32)DIAGNOSTIC_CODES.count &&
|
|
DIAGNOSTIC_CODES[end] != cast(u8)10 { end += 1 }
|
|
if DIAGNOSTIC_CODES[at:end] == code { return true }
|
|
if end >= cast(s32)DIAGNOSTIC_CODES.count { break }
|
|
at = end + 1
|
|
}
|
|
return false
|
|
}
|
|
|
|
PkgPrintCodeList :: (json: bool) -> void {
|
|
if !json {
|
|
PkgPrint(DIAGNOSTIC_CODES, false)
|
|
return
|
|
}
|
|
PkgPrint("{\"schema_version\":1,\"stability\":\"stable-code\",\"codes\":[", false)
|
|
at: s32 = 0
|
|
first: bool = true
|
|
while at < cast(s32)DIAGNOSTIC_CODES.count {
|
|
end: s32 = at
|
|
while end < cast(s32)DIAGNOSTIC_CODES.count &&
|
|
DIAGNOSTIC_CODES[end] != cast(u8)10 { end += 1 }
|
|
if end > at {
|
|
if !first { PkgPrint(",", false) }
|
|
PkgJsonString(DIAGNOSTIC_CODES[at:end])
|
|
first = false
|
|
}
|
|
if end >= cast(s32)DIAGNOSTIC_CODES.count { break }
|
|
at = end + 1
|
|
}
|
|
PkgPrint("]}\n", false)
|
|
}
|
|
|
|
PkgPrintExplanation :: (code: string, json: bool) -> void {
|
|
// Each native emitter has its own entry; the other areas follow.
|
|
emitters: [2]PkgExplanation = .[
|
|
.{prefix="zir_c.", summary="The native C emitter cannot lower this checked construct.", action="Use a supported C construct or target, and compare source with saved-IR output before retrying."},
|
|
.{prefix="zir_go.", summary="The native Go emitter cannot lower this import, type, signature, or construct.", action="Use a Go-compatible import or API shape, or target C and the host interface for native ABI code."}
|
|
]
|
|
items := PkgExplanation.[
|
|
.{prefix="command.", summary="A command argument is invalid or missing.", action="Use the command usage shown in the diagnostic and retry with valid arguments."},
|
|
.{prefix="compiler.context", summary="A compiler operation retained programs past its compilation context.", action="Keep the source and command that reproduce the failure and report the compiler bug."},
|
|
.{prefix="compiler.memory", summary="The compiler could not allocate working memory.", action="Free memory or reduce the workload, then retry the same command."},
|
|
.{prefix="compiler.internal", summary="A compiler consistency check failed.", action="Keep the source and command that reproduce the failure and report the compiler bug."},
|
|
.{prefix="format.", summary="The formatter could not read or replace a file, or a check found formatting changes.", action="Check the named file and permissions; run ziran fmt without --check to apply formatting."},
|
|
.{prefix="package.command", summary="The package command cannot complete with these arguments or inputs.", action="Use the command usage and reported input problem to correct the request before retrying."},
|
|
.{prefix="parse.", summary="The source parser rejected syntax or a directive.", action="Compare the exact span with current Jai-style Ziran syntax in ziran guide and the language reference; do not invent C-style syntax."},
|
|
.{prefix="check.slice_lifetime", summary="A borrowed slice or text view has an invalid lifetime or mutable-backing conflict.", action="Keep the owner live and immutable for the whole view lifetime, or copy the bytes when independence is needed."},
|
|
.{prefix="check.cleanup", summary="A declaration conflicts with lexical cleanup or ownership rules.", action="Check declaration order, moves, and lexical scope; consume owned storage exactly once."},
|
|
.{prefix="check.vec_copy", summary="An owned vector or aggregate containing a vector is being copied.", action="Move it, pass a pointer, or redesign the aggregate so ownership is not duplicated."},
|
|
.{prefix="check.", summary="The type checker rejected a declaration, expression, signature, or effect.", action="Inspect the span and inferred types, query ziran api for visible declarations, and make source and saved-IR semantics agree."},
|
|
.{prefix="proof.", summary="A scalar theorem certificate has invalid structure or identity.", action="Check typed theorem bindings, import qualification, proof steps, and duplicate certificates in docs/LAWS.md."},
|
|
.{prefix="law.", summary="A compile-time law gate rejected the program.", action="Inspect the named law and payload; fix the checked behavior or add a narrowly scoped valid waiver."},
|
|
.{prefix="lint.bool", summary="An integer local only ever holds 0 or 1.", action="Declare it bool."},
|
|
.{prefix="lint.cast", summary="A cast the program no longer needs.", action="Remove it; a cast that sets arithmetic width stays."},
|
|
.{prefix="lint.char", summary="A byte is compared with a character code written as a number.", action="Write the character: #char \"A\" rather than 65."},
|
|
.{prefix="module.", summary="A module identity, path, cycle, limit, input, or parameter binding is invalid.", action="Check roots, module paths, import spelling, dependency cycles, file extensions, and that every import of a module gives its declared #module_parameters the same fitting values."},
|
|
.{prefix="package.ambiguous", summary="A short import names a module that more than one direct dependency exports.", action="Import it as NAME/Module with the dependency name that should provide it; ziran pkg list shows each dependency's modules."},
|
|
.{prefix="package.project", summary="A package-qualified import such as NAME/Module was checked without a project.", action="Declare the dependency in ziran.toml and run the command with --project, or import std/NAME or a module on the module path."},
|
|
.{prefix="package.", summary="The package lock, map, identity, or scope is invalid.", action="Regenerate and inspect ziran.lock, check package roots and aliases, then retry the project command."},
|
|
.{prefix="wasm.gap.", summary="The zx2wasm lowering of portable ZX has no code for a form in this program.", action="Run the .zx with zi2zx run or keep it to the forms the message names as supported; another engine is not selected automatically."},
|
|
.{prefix="wasm.", summary="The executable ZX given to zx2wasm is unreadable or fails verification.", action="Rebuild the .zx with zi2zx from current source; an image from another toolchain version is rejected."},
|
|
.{prefix="assembly.gap.machine-platform-service-unavailable", summary="The machine-IR ELF path does not implement a requested typed platform service.", action="Use a service implemented by the selected machine target or add its target-owned veneer; no host binding or alternate engine is substituted."},
|
|
.{prefix="assembly.gap.target-unimplemented", summary="The selected direct assembly target row is a Gap, Excluded or Unknown, so preflight rejected it before reading input or writing output.", action="Run ziran capabilities --json and read assembly_targets for the row state; select an implemented row or use another explicitly selected target, which is a separate target, not a fallback."},
|
|
.{prefix="assembly.gap.cpu-feature", summary="The selected direct assembly row does not implement a required CPU feature, so preflight rejected it before any side effect.", action="Check the row's features in ziran capabilities --json; a lower-feature variant or portable body does not satisfy the requirement."},
|
|
.{prefix="assembly.gap.os-service", summary="The selected direct assembly row does not supply a required operating-system service, so preflight rejected it before any side effect.", action="Check the row's services in ziran capabilities --json; typed host capabilities and other engines do not substitute for a missing service."},
|
|
.{prefix="assembly.", summary="The direct backend could not verify, encode, or write the program.", action="Use zi2zx --emit lir to inspect verified instructions and zi2zx --opt --emit opt-report for optimization accounting; treat an unsupported target or capability as a hard gap until that implementation exists."},
|
|
.{prefix="emit.", summary="A native emitter cannot lower this checked construct for the selected target.", action="Check ziran capabilities for the target, then use a supported construct or another supported target."},
|
|
.{prefix="parallel.", summary="A parallel region violates effect, memory, nesting, or control-flow rules.", action="Make region effects legal, keep memory access disjoint, and use supported forward-range control flow."},
|
|
.{prefix="gpu.device", summary="A GPU-annotated region has no device backend, so the target reports an explicit Gap.", action="Use ordinary #parallel for documented CPU execution or wait for a complete GPU backend; no CPU fallback or other engine is selected automatically."},
|
|
.{prefix="gpu.", summary="A GPU-annotated region contains an unsupported pointer or allocator form.", action="Remove foreign pointers and unsupported allocation from the region; there is no CPU fallback path."},
|
|
.{prefix="zx.wasm", summary="A checked program or bundle uses a construct outside the wasm execution tier's scalar subset.", action="Use portable scalar constructs the wasm tier lowers, or run the program through the native tier or the portable engine instead."},
|
|
.{prefix="zx.", summary="A portable bundle failed linking, verification, binding, or execution.", action="Check entry and host-capability names, run from a valid bundle, and compare source and saved-IR linking."},
|
|
.{prefix="zir.", summary="A saved-IR input or serialization is invalid or noncanonical.", action="Regenerate .zir from current source instead of editing it, and verify that source and saved-IR checks agree."},
|
|
.{prefix="vm.", summary="The engine verifier rejected control flow.", action="Remove unreachable control flow or use a supported branch shape that the verifier can prove safe."},
|
|
.{prefix="host.", summary="A host capability or binding is missing or malformed.", action="Register every required host function before running or linking the bundle."}
|
|
]
|
|
item: PkgExplanation = items[items.count - 1]
|
|
found: bool = false
|
|
index: s32 = 0
|
|
while index < cast(s32)emitters.count {
|
|
if PkgStartsWith(code, emitters[index].prefix) {
|
|
item = emitters[index]
|
|
found = true
|
|
break
|
|
}
|
|
index += 1
|
|
}
|
|
index = 0
|
|
while !found && index < cast(s32)items.count {
|
|
if PkgStartsWith(code, items[index].prefix) {
|
|
item = items[index]
|
|
found = true
|
|
}
|
|
index += 1
|
|
}
|
|
if json {
|
|
PkgPrint("{\"schema_version\":1,\"stability\":\"stable-code\",\"code\":", false)
|
|
PkgJsonString(code)
|
|
PkgPrint(",\"summary\":", false)
|
|
PkgJsonString(item.summary)
|
|
PkgPrint(",\"next_step\":", false)
|
|
PkgJsonString(item.action)
|
|
PkgPrint("}\n", false)
|
|
} else {
|
|
PkgPrint("code: ", false)
|
|
PkgPrint(code, false)
|
|
PkgPrint("\nsummary: ", false)
|
|
PkgPrint(item.summary, false)
|
|
PkgPrint("\nnext step: ", false)
|
|
PkgPrint(item.action, false)
|
|
PkgPrint("\nCode identities are stable; messages and guidance may improve.\n", false)
|
|
}
|
|
}
|
|
|
|
#scope_export
|
|
Explain :: (arguments: []string) -> s32 {
|
|
json: bool = false
|
|
code: string = ""
|
|
positional: s32 = 0
|
|
index: s32 = 0
|
|
while index < cast(s32)arguments.count {
|
|
part: string = arguments[index]
|
|
if part == "--json" {
|
|
if json { return PkgFail("usage: ziran explain CODE [--json] | ziran explain --list [--json]") }
|
|
json = true
|
|
} else {
|
|
if positional != 0 { return PkgFail("usage: ziran explain CODE [--json] | ziran explain --list [--json]") }
|
|
code = part
|
|
positional += 1
|
|
}
|
|
index += 1
|
|
}
|
|
if positional == 0 {
|
|
return PkgFail("usage: ziran explain CODE [--json] | ziran explain --list [--json]")
|
|
}
|
|
if code == "--list" {
|
|
PkgPrintCodeList(json)
|
|
return 0
|
|
}
|
|
if !PkgKnownDiagnostic(code) {
|
|
return PkgFail(PkgJoin3("unknown diagnostic code: ", code,
|
|
"\nRun ziran explain --list for known codes."))
|
|
}
|
|
PkgPrintExplanation(code, json)
|
|
return 0
|
|
}
|