FuncDecl
Interface in IR
A ModuleDecl.funcs entry: a WGSL/GLSL function, either an ordinary helper or a pipeline entry point (stage set).
import type { FuncDecl } from 'typeshade'
The signature, the description and the examples come from the compiler's own source at commit 26de7be8.
Syntax
interface FuncDecl { readonly name: string; readonly params: readonly { name: string; type: ShaderType; builtin?: string; location?: number; interpolate?: string; attr?: string; /** How the function takes this parameter. Absent is by value, which is every parameter * but one that the callee writes through to the caller's own value. `'inout'` says it * does: GLSL ES 3.00 spells that `inout T name`, WGSL a pointer. * * The IR says WHICH parameters are written, not how a target spells it, because the two * targets do not agree on that. GLSL's `inout` is copy-in/copy-out and takes any l-value * argument; WGSL's pointer is a reference and carries the ADDRESS SPACE in its type, so a * function taking `ptr<function, T>` cannot be handed `&buf[i]`. The WGSL backend's own * pass makes one copy of the function per address space its calls use; nothing about * that reaches here, and GLSL emits the one function. */ mode?: 'inout'; }[]; readonly ret: ShaderType; readonly body: readonly Stmt[]; readonly attrs?: readonly string[]; readonly stage?: 'vertex' | 'fragment' | 'compute'; readonly workgroupSize?: number; readonly workgroupShape?: WorkgroupShape; readonly portable?: boolean; readonly kernel?: boolean; readonly retAttr?: string; readonly retBuiltin?: string; readonly opaque?: boolean; readonly span?: SourceSpan; readonly nameSpan?: SourceSpan; readonly allowEarlyReturn?: boolean; readonly lintDisable?: readonly string[]; readonly [ASSEMBLED_AS]?: string;}Description
A ModuleDecl.funcs entry: a WGSL/GLSL function, either an ordinary helper or a
pipeline entry point (stage set). This is the object fn builds: the
FnHandle it returns mixes these fields onto itself, so it is at once a typed
callable in other function bodies and, unwrapped, the plain FuncDecl that
module({ funcs }) collects. Every backend (WGSL, GLSL, CPU) walks body directly.
Instance properties
nameread onlystringparamsread onlyreadonly { name: string; type: ShaderType; builtin?: string; location?: number; interpolate?: string; attr?: string; /** How the function takes this parameter. Absent is by value, which is every parameter * but one that the callee writes through to the caller's own value. `'inout'` says it * does: GLSL ES 3.00 spells that `inout T name`, WGSL a pointer. * * The IR says WHICH parameters are written, not how a target spells it, because the two * targets do not agree on that. GLSL's `inout` is copy-in/copy-out and takes any l-value * argument; WGSL's pointer is a reference and carries the ADDRESS SPACE in its type, so a * function taking `ptr<function, T>` cannot be handed `&buf[i]`. The WGSL backend's own * pass makes one copy of the function per address space its calls use; nothing about * that reaches here, and GLSL emits the one function. */ mode?: 'inout'; }[]retread onlyShaderTypebodyread onlyreadonly Stmt[]attrsoptionalread onlyreadonly string[]Stage and pipeline attributes emitted before
fn, such as@computeor@workgroup_size(64). Empty for ordinary helper functions. This is the emitted spelling;stageandworkgroupSizebelow are whatreflectand the backends read first, with these strings as the fallback for a hand-builtFuncDeclliteral.stageoptionalread only'vertex' | 'fragment' | 'compute'Structured pipeline stage. Set by
fn()’sopts.stage.workgroupSizeoptionalread onlynumberStructured workgroup size for a compute stage: the
xextent.workgroupShapeoptionalread onlyWorkgroupShapeThe three workgroup extents of a compute stage, present when
yorzis not 1. An absent shape is[workgroupSize, 1, 1]; read it throughworkgroupShapeOf.portableoptionalread onlybooleanMarks a compute entry as a portable kernel. Set by
fn()’sopts.portable, which rejects it on any other stage withSD0110. A portable kernel emits on both backends: as a native@computeentry on WGSL, and through thelowerComputeToFragmentrewrite on GLSL ES 3.00, which runs it as a fragment shader. The kernel must keep to the gather-only shape, where each invocation reads freely and stores exactly once to its own index of the output; any construct outside that shape fails validation withSD0111on every emit, on both backends.Structured only, with no
attrsspelling:portableis not a WGSL attribute, so declaring it changes nothing in the emitted source.kerneloptionalread onlybooleanMarks a kernel function (Rule 8.22): an exported function, not an entry, that takes an array with no size. It runs on the host’s side of the call, which dispatches its loops, so the WGSL and GLSL backends leave it out of what they emit, and no function calls it (Rule 8.6). Its array parameters are the caller’s storage, passed by reference (Rule 8.23).
Structured only, with no
attrsspelling: it is not a WGSL attribute.retAttroptionalread onlystringReturn-value attribute for a bare (non-struct) stage output, e.g. a fragment
-> @location(0) vec4<f32>.retBuiltinoptionalread onlystringStructured builtin id when
retAttrcame from abuiltin(name, type)FieldSpec. The spelling stays inretAttr; the id lives here so a backend can check it against its builtin vocabulary without re-parsing the attribute string.opaqueoptionalread onlybooleanKeep this function’s body out of its call sites: the emit optimizer never inlines a call to it.
fp64Lowersets it on every double-float helper it injects, because those bodies are error-free transformations that are algebraically trivial (e = b - (s - a)is 0 in real arithmetic), and flattening them hands the terms to passes and drivers that may legally cancel them.It is a property of the declaration, so it survives every rename a production emit applies to the function’s name.
Structured only, with no
attrsspelling (likeportableabove): it is not a WGSL attribute and changes nothing in the emitted source.spanoptionalread onlySourceSpanWhere this function was declared in its authored
"use typeshade"source: the whole declaration, from the first decorator or theexportkeyword through the closing brace. Absent on an EDSL-authored or pass-synthesised function. Read it withsourceSpanOf.nameSpanoptionalread onlySourceSpanThe span of just this function’s name in its authored source, so a stack frame can highlight the identifier rather than the whole body. Absent whenever
spanis.allowEarlyReturnoptionalread onlybooleanDocumented deviation from the single-exit lint rule: when true, the rule skips this function because it has an intentional early return, such as a guard that skips an expensive loop. Use sparingly, with a comment stating why.
lintDisableoptionalread onlyreadonly string[]Documented lint deviations: rule ids whose diagnostics are suppressed for this function (the general form of
allowEarlyReturn). Use sparingly, with a comment stating why.
See also
Source
src/core/ir/nodes.ts, line 588, at commit 26de7be8