Stmt
이 페이지에서

Stmt

타입, IR 분류

IR이 담을 수 있는 모든 문장 형태입니다. FuncDecl.body를 이루는, 순서가 정해진 readonly Stmt[]입니다.

import type { Stmt } from 'typeshade'
import type { Stmt } from 'typeshade/core/ir'

설명은 커밋 af54ddad5의 검토된 컴파일러 문서를 번역한 것입니다. 시그니처와 예제 코드는 원문을 따르며, 필요한 경우 설명에 편집상 수정이 반영됩니다.

구문

type Stmt =
| {
readonly s: 'let';
readonly name: string;
readonly expr: Expr;
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| {
readonly s: 'var';
readonly name: string;
readonly type: ShaderType;
readonly init?: Expr;
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| {
readonly s: 'assign';
readonly target: Expr;
readonly expr: Expr;
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| {
readonly s: 'assignOp';
readonly target: Expr;
readonly bop: BinOp;
readonly expr: Expr;
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| {
readonly s: 'if';
readonly arms: ReadonlyArray<{ readonly cond: Expr; readonly body: readonly Stmt[] }>;
readonly elseBody?: readonly Stmt[];
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| {
readonly s: 'return';
readonly expr?: Expr;
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| {
readonly s: 'for';
readonly init: Stmt;
readonly cond: Expr;
readonly update: Stmt;
readonly body: readonly Stmt[];
/** What the front end proved counts this loop (Rule 7.5): present on a `for` and a
* `for…of` it lowered, absent on a `while`, which is a `for` over a synthetic counter,
* and on a pass-synthesised loop. The independence proof of a kernel function's loop
* (Rule 8.22) reads it rather than guessing from the counter's name. */
readonly counted?: {
/** The counter's IR name. */
readonly name: string;
readonly op: 'add' | 'mul' | 'div';
/** The addend of an additive step (`i++` is `1`, `i -= 2` is `-2`), and the factor or
* divisor of a multiplicative one. */
readonly step: number;
/** Present when the header folds to constants, absent for a runtime start or bound. */
readonly start?: number;
readonly bound?: number;
readonly trips?: number;
};
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| {
readonly s: 'switch';
readonly scrut: Expr;
/** One clause per entry. `values` is the SELECTORS that share the clause's body, in
* source order: WGSL spells them `case 0, 1: { … }` and GLSL ES 3.00
* `case 0: case 1: { … }`, which is why the IR carries the list rather than one
* number and a fall-through. Never empty. */
readonly cases: ReadonlyArray<{
readonly values: readonly number[];
readonly body: readonly Stmt[];
}>;
readonly defaultBody?: readonly Stmt[];
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| { readonly s: 'break'; readonly span?: SourceSpan }
| { readonly s: 'continue'; readonly span?: SourceSpan }
| { readonly s: 'discard'; readonly span?: SourceSpan }
| {
/** A call kept for its effect and nothing else: `store(gid.x);` today, and the
* `workgroupBarrier();`, `textureStore(...)` and `atomicAdd(...)` family it makes a place
* for. `expr` is the `call` the front end lowered; an op other than `call` reaches here
* only through a pass that folded one, and both emitters still spell it. WGSL takes a
* user function's dropped result bare and needs `_ = ` before a value-returning builtin;
* GLSL ES 3.00 takes the bare call in every case; the CPU backends evaluate it for its
* effect. A pass that assumes expressions are pure must treat this statement as the
* one place an effect is kept on purpose (see `passes/effects.ts`). */
readonly s: 'call';
readonly expr: Expr;
/** Where this statement came from in its authored `"use typeshade"` source; absent on
* an EDSL-authored or pass-synthesised statement. Read it with {@link sourceSpanOf}. */
readonly span?: SourceSpan;
}
| { readonly s: 'placeholder'; readonly tag: string; readonly span?: SourceSpan }
| { readonly s: 'raw'; readonly wgsl: string; readonly glsl?: string; readonly span?: SourceSpan }
| {
readonly s: 'raw';
readonly wgsl?: string;
readonly glsl: string;
readonly span?: SourceSpan;
};

설명

Expr처럼 이 타입도 판별 유니온이며(판별 키는 s), 이를 뒷받침하는 클래스가 없습니다. 그래서 tsc가 백엔드의 생성 switch에 모든 경우가 빠짐없이 있는지 검사할 수 있고, 패스는 일반 데이터를 매핑하여 본문을 다시 만들 수 있습니다. 이 객체 리터럴을 직접 만들 필요는 없습니다. fn 본문의 두 번째 콜백 인자로 전달되는 Builder가 한 번에 하나씩 추가한다(b.let(...), b.var(...), b.if(...), b.ret(...), …). 그러므로 본문은 항상 소스 순서대로 조립됩니다.

함께 보기

소스

src/core/ir/nodes.ts, 176행, 커밋 af54ddad5 기준

이 페이지 편집 문제 보고