FuncDecl
On this page

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 only string
paramsread only 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'; }[]
retread only ShaderType
bodyread only readonly Stmt[]
attrsoptionalread only readonly string[]

Stage and pipeline attributes emitted before fn, such as @compute or @workgroup_size(64). Empty for ordinary helper functions. This is the emitted spelling; stage and workgroupSize below are what reflect and the backends read first, with these strings as the fallback for a hand-built FuncDecl literal.

stageoptionalread only 'vertex' | 'fragment' | 'compute'

Structured pipeline stage. Set by fn()’s opts.stage.

workgroupSizeoptionalread only number

Structured workgroup size for a compute stage: the x extent.

workgroupShapeoptionalread only WorkgroupShape

The three workgroup extents of a compute stage, present when y or z is not 1. An absent shape is [workgroupSize, 1, 1]; read it through workgroupShapeOf.

portableoptionalread only boolean

Marks a compute entry as a portable kernel. Set by fn()’s opts.portable, which rejects it on any other stage with SD0110. A portable kernel emits on both backends: as a native @compute entry on WGSL, and through the lowerComputeToFragment rewrite 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 with SD0111 on every emit, on both backends.

Structured only, with no attrs spelling: portable is not a WGSL attribute, so declaring it changes nothing in the emitted source.

kerneloptionalread only boolean

Marks 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 attrs spelling: it is not a WGSL attribute.

retAttroptionalread only string

Return-value attribute for a bare (non-struct) stage output, e.g. a fragment -> @location(0) vec4<f32>.

retBuiltinoptionalread only string

Structured builtin id when retAttr came from a builtin(name, type) FieldSpec. The spelling stays in retAttr; the id lives here so a backend can check it against its builtin vocabulary without re-parsing the attribute string.

opaqueoptionalread only boolean

Keep this function’s body out of its call sites: the emit optimizer never inlines a call to it. fp64Lower sets 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 attrs spelling (like portable above): it is not a WGSL attribute and changes nothing in the emitted source.

spanoptionalread only SourceSpan

Where this function was declared in its authored "use typeshade" source: the whole declaration, from the first decorator or the export keyword through the closing brace. Absent on an EDSL-authored or pass-synthesised function. Read it with sourceSpanOf.

nameSpanoptionalread only SourceSpan

The 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 span is.

allowEarlyReturnoptionalread only boolean

Documented 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 only readonly 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

Edit this page Report a problem