Skip to content
Systems programming for what’s next.
— Vex

Structural Declaration Generation ​

Vex can generate declarations without generating source text. A module generator returns compiler-owned DeclSet data composed from typed DeclExpr, DeclPattern, and DeclConstraint plans. The compiler lowers those plans directly to ordinary HIR.

This is the supported alternative to token streams, textual templates, synthetic ASTs, and Rust-style procedural macros.

Generator trigger ​

A top-level function is an eager module generator when it is:

  • declared const;
  • non-generic;
  • parameterless; and
  • declared to return DeclSet.

Parameterized or generic functions returning DeclSet may be called as helpers, but they are not discovered as module roots.

vex
const fn deriveModule(): DeclSet {
    let input = #DeclExpr.param<i32>("classify.input", "input", 0)
    let body = #DeclExpr.matchValue(
        input,
        [#DeclExpr.value(0), #DeclExpr.value(1)],
        [#DeclExpr.value(10), #DeclExpr.value(20)],
        #DeclExpr.add(input, #DeclExpr.value(40)),
    )

    return #DeclSet.addFunction(
        #DeclSet.empty(),
        "classify.function",
        "generatedClassify",
        body,
        false,
    )
}

fn main(): i32 {
    if generatedClassify(0) != 10 { return 1; }
    if generatedClassify(1) != 20 { return 2; }
    if generatedClassify(2) != 42 { return 3; }
    $println(generatedClassify(2));
    return 0;
}

Save as generated-classify.vx and run vex run generated-classify.vx. Expected stdout is 42, with exit status 0. The three checks exercise both explicit match arms and the generated fallback expression.

generatedClassify is available to ordinary name resolution, inference, linting, LSP analysis, monomorphization, fusion, and native codegen after the module expansion snapshot is sealed.

Stable role and presentation name ​

Every generated declaration or local has two different names:

  • role is its stable structural identity inside the generator invocation;
  • name is the spelling presented to Vex source and diagnostics.
vex
#DeclSet.addFunction(
    set,
    "codec.decode.function", // stable role
    "decodeUser",            // presentation name
    body,
    true,
)

Changing a presentation name does not redefine structural identity. Reusing a role for two different declarations in one invocation is rejected. Roles should be deterministic and derived from semantic structure, not iteration addresses, hash-map order, or host-global counters.

Declaration builders ​

The current accepted surface can emit:

FamilyBuilders
Functions and constantsaddConstFunction, addConst, addFunction, addFunctionWhere
TypesaddTypeAlias, addStruct, addGenericStruct, addEnum, addGenericEnum
MethodsaddValueMethod, addRefMethod, addRefMutMethod, addStaticMethod, plus their Where forms
ConformanceaddContractImpl, addAssociatedTypeImpl

The returned DeclSet is immutable: each builder accepts a set and returns the extended set. A declaration is committed only after its exact types, constraints, body, ownership behavior, and structural identity validate.

Expression plans ​

DeclExpr is a closed typed expression algebra, not a string builder.

PurposeBuilders
Values and parametersvalue, param, paramType
Localslocal, localMut, bind, assign
Scalar/controladd, lessThan, ifElse, block
LoopswhileLoop, forEach, breakLoop, continueLoop
MatchingmatchValue, matchPattern
Calls and resultscall, returnValue
Aggregatesaggregate

Each plan carries an exact Vex type. Builders reject unresolved types, missing fields, invalid calls, out-of-scope locals, illegal mutation, result-type drift, and ownership-invalid paths before native codegen.

Mutable generated locals must be explicit:

vex
let counter = #DeclExpr.localMut<i64>("counter.local", "counter")
let initialize = #DeclExpr.bind(counter, #DeclExpr.value(0i64), body)

#DeclExpr.assign accepts only a local created with mutation capability and only while that hygienic local is in scope.

Typed patterns ​

DeclPattern supports exact scalar, enum, tuple, struct, reference, binding, and or-pattern construction:

vex
enum Choice { First, Second, Other }

const fn deriveChoiceMatcher(): DeclSet {
    let input = #DeclExpr.param<Choice>("choice.input", "input", 0)
    let variants = #Type.info<Choice>().variants
    let accepted = #DeclPattern.anyOf([
        #DeclPattern.variantUnit(variants[0]),
        #DeclPattern.variantUnit(variants[1]),
    ])
    let body = #DeclExpr.matchPattern(
        input,
        [accepted],
        [#DeclExpr.value(true)],
        #DeclExpr.value(false),
    )

    return #DeclSet.addFunction(
        #DeclSet.empty(),
        "choice.accepted.function",
        "isGeneratedChoiceAccepted",
        body,
        false,
    )
}

Available pattern builders are wildcard, value, bind, ref, refMut, tuple, structFields, variantUnit, variant, variantStruct, and anyOf.

Important invariants:

  • literal patterns preserve their exact signed/unsigned integer or bool type;
  • enum variants and fields use reflected semantic identity, not their names;
  • anyOf requires at least two refutable alternatives of one exact type;
  • every or-pattern alternative must expose the same hygienic binding contract;
  • moved payloads, borrowed payloads, and remainder cleanup use ordinary Vex ownership rules.

Generate an enum classification function ​

An enum-based classifier shows how reflected variants and generated patterns work together. The declared order here is Inspect, List, Update, Delete; the generator groups the first two variants as read-only actions. The result is an ordinary boolean function, and main checks all four cases.

vex
enum Action { Inspect, List, Update, Delete }
const fn deriveReadPolicy(): DeclSet {
    let variants = #Type.info<Action>().variants;
    let input = #DeclExpr.param<Action>("read-policy.input", "action", 0);
    let readOnly = #DeclPattern.anyOf([
        #DeclPattern.variantUnit(variants[0]),
        #DeclPattern.variantUnit(variants[1]),
    ]);
    let body = #DeclExpr.matchPattern(input, [readOnly],
        [#DeclExpr.value(true)], #DeclExpr.value(false));
    return #DeclSet.addFunction(#DeclSet.empty(), "read-policy.function",
        "isReadOnly", body, false);
}
fn main(): i32 {
    if !isReadOnly(Action.Inspect) || !isReadOnly(Action.List) { return 1; }
    if isReadOnly(Action.Update) || isReadOnly(Action.Delete) { return 2; }
    $println("read policy: PASS");
    return 0;
}

Run vex run generated-read-policy.vx. Expected output is read policy: PASS and exit status 0.

variants[0] and variants[1] supply typed variant identities to the pattern builder; their array positions are an explicit policy in this small example. If the enum is reordered, update the selection and its cases together. This function classifies an action; authorization still requires the application's user, resource, and permission checks.

The parameter plan has slot zero, its Action type, and a stable role. The match body returns true for the combined pattern and false for the fallback. No generated source file, text substitution, or runtime reflection table is needed. The false argument to addFunction keeps the generated declaration non-public; it is unrelated to the function's boolean result.

Generic constraints ​

Generated generic callables can attach exact contract requirements with #DeclConstraint.contract and a Where builder.

vex
let marker = #DeclConstraint.contract<Marker<i64>>(parameter.ty)
let stable = #DeclConstraint.contract<Stable>(parameter.ty)

return #DeclSet.addFunctionWhere(
    #DeclSet.empty(),
    "generated.checked.function",
    "generatedChecked",
    body,
    false,
    [marker, stable],
)

Requirements retain contract identity and generic arguments. Duplicate exact requirements are idempotent. Ordinary Vex conformance inference decides whether a concrete type satisfies them.

Conjunctions of exact requirements are supported. Arbitrary disjunction, negation, compound-subject predicates, and user-defined type-set algebra are not part of the accepted V2 surface.

Identity, hygiene, and provenance ​

Generated identities are derived from the generator, invocation owner, stable module site, structural role, and declaration ordinal. They do not depend on a backend symbol spelling.

The compiler preserves:

  • the generator definition and invocation ancestry;
  • exact source and reflected declaration identities;
  • reminted generic type and const binders;
  • hygienic local roles and lexical scope;
  • sealed inference and ownership facts used by every consumer.

Diagnostics, lint, LSP, and native compilation consume the same expansion snapshot. A declaration accepted by one consumer cannot be reconstructed from names differently by another.

Invocation authority ​

DeclSet, DeclExpr, DeclPattern, and DeclConstraint are compiler-owned semantic values. #DeclSet.empty() and the transform builders work only while the compiler is executing an authorized module generator. Ordinary const evaluation cannot manufacture or persist a declaration set.

Helper functions called by the active generator inherit that exact invocation authority. Their generated values remain bound to the invocation and cannot be published through an unrelated persistent const result.

Codegen and fusion ​

Generated functions are not printed, reparsed, or compiled into a side bitcode file. Their typed HIR and sealed inference facts enter the normal callable registry directly. Consequently:

  • source and generated calls share monomorphization;
  • VexArch and std calls remain visible to whole-module fusion and inlining;
  • generated allocations and drops use the normal VUMM/Valence path;
  • normal reachability and dead-code elimination remove unused generated work;
  • ABI and target checks are identical to handwritten declarations.

Fail-closed rules ​

A generator fails compilation when it attempts to:

  • use transform builders outside an authorized invocation;
  • duplicate or drift a stable structural role;
  • construct unresolved, semantic-only, or target-unavailable runtime types;
  • refer to a field, variant, method, or contract by a mismatched identity;
  • leak a generated local outside its lexical scope;
  • violate mutability, borrow, move, drop, or result-type rules;
  • create an or-pattern with incompatible bindings;
  • introduce a generator cycle or exceed evaluation/expansion quotas.

There is no source-string fallback and no best-effort declaration emission.

Released under the MIT License.