Skip to content

feat: implement enums, type aliases, and advanced constant evaluation - #285

Merged
LunaStev merged 1 commit into
wavefnd:masterfrom
LunaStev:feat/implement-enums-type-aliases-and-advanced-constant-evaluation
Feb 9, 2026
Merged

feat: implement enums, type aliases, and advanced constant evaluation#285
LunaStev merged 1 commit into
wavefnd:masterfrom
LunaStev:feat/implement-enums-type-aliases-and-advanced-constant-evaluation

Conversation

@LunaStev

@LunaStev LunaStev commented Feb 9, 2026

Copy link
Copy Markdown
Member

This PR significantly expands the Wave type system and compile-time capabilities. It introduces enum and type alias support, supported by a new pre-codegen Type Resolution Pass. Additionally, the constant evaluation engine has been overhauled to support complex nested aggregates and iterative dependency resolution, while the ABI layer has been hardened for safer C FFI interactions.

Key Changes

1. Enums & Type Aliases

  • Enum Syntax: Introduced enum Name -> Repr { Variant, ... }. Variants are automatically treated as global constants of the specified representation type.
  • Type Aliasing: Added the type Alias = Target syntax, allowing for cleaner and more descriptive type naming.
  • Type Resolution Pass: Implemented a mandatory pass before code generation that flattens type aliases and resolves enum references across the entire AST (functions, structs, variables).

2. Advanced Constant Evaluation

  • Aggregate Support: The constant evaluator now supports struct literals and array literals, allowing complex data structures to be defined at compile-time.
  • Iterative Resolution: Implemented a multi-round resolution strategy. Constants can now depend on other constants regardless of their declaration order in the source code.
  • Extended Keyword Support: true, false, and null are now valid primary expressions in constant contexts.

3. Backend & ABI Improvements

  • Safe Aggregate Packing: Refactored the FFI packing/unpacking logic (pack_agg_to_int) to use build_memcpy instead of raw bit-casting. This ensures that alignment requirements are respected when passing small structs through integer registers.
  • Vector Coercion: Added support for coercing aggregates to LLVM Vector types, improving the handling of Homogeneous Floating-point Aggregates (HFAs) in the System V ABI.
  • Project Hygiene: The backend now automatically ensures the target/ directory exists before emitting object files.

4. Parser & Verification

  • New Keywords: Registered type and enum as reserved keywords.
  • Identifier Validation: Enhanced the semantic verification pass to detect and error on the usage of undeclared identifiers in expressions.
  • Global Registration: The verification pass now correctly registers enum variants as global constants, making them available project-wide.

Example Usage

type MyInt = i32;

enum ShaderUniformType -> MyInt {
    A = 0,
    B,
    C = 10,
    D
}

const X: MyInt = 123;
const Y: MyInt = B;
const Z: ShaderUniformType = D;

fun f(t: ShaderUniformType) -> MyInt {
    return t;
}

fun g(v: MyInt) -> MyInt {
    return v;
}

fun main() {
    println("{}", f(A)); // 0
    println("{}", f(B)); // 1
    println("{}", f(C)); // 10
    println("{}", f(D)); // 11

    println("{}", g(X)); // 123
    println("{}", g(Y)); // 1
    println("{}", f(Z)); // 11
}

Benefits

  • Flexibility: Type aliases and enums allow for much more expressive and maintainable codebases.
  • Compile-time Power: Complex constant evaluation reduces runtime overhead by shifting computations to the compiler.
  • FFI Robustness: Using memcpy for aggregate packing prevents subtle memory corruption bugs related to data alignment during C library calls.

This commit introduces support for enums and type aliases, along with a
significantly more powerful constant evaluation engine. It also improves
ABI-related type coercion and provides better frontend validation.

Changes:
- **Enums & Type Aliases**:
  - Introduced `enum Name -> Repr { Variant, ... }` syntax. Enum variants
    are automatically treated as global constants.
  - Added `type Alias = Target` syntax for type aliasing.
  - Implemented a pre-codegen **Type Resolution Pass** that flattens aliases
    and resolves enum types across functions, structs, and variables.
- **Advanced Constant Evaluation**:
  - Overhauled the constant evaluator to support complex expressions,
    including **struct literals** and **array literals** in constants.
  - Implemented **iterative (multi-round) resolution** for constants,
    allowing constants to depend on other constants defined elsewhere in
    the program.
  - Added support for `true`, `false`, and `null` keywords in constant
    contexts.
- **Backend & ABI Improvements**:
  - Refactored aggregate packing (`pack_agg_to_int`) and unpacking to use
    `build_memcpy` instead of bit-casting, ensuring safer handling of
    alignment requirements during FFI calls.
  - Added robust coercion logic between aggregates (structs/arrays) and
    LLVM **Vector types**, improving support for Homogeneous
    Floating-point Aggregates (HFAs).
  - Added automatic creation of the `target/` directory in the backend.
- **Parser & Verification**:
  - Added keywords `type` and `enum` to the lexer and parser.
  - Enhanced the verification pass to detect usage of undeclared
    identifiers in expressions.
  - Updated `validate_program` to register enum variants as global constants.
- **Maintenance**:
  - Bumped version to `0.1.7-pre-beta`.
  - Added comprehensive test cases (`test81.wave`, etc.) for enums,
    aliases, and nested constant dependencies.

This update significantly increases the language's type system
flexibility and enables more complex compile-time computations.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev
LunaStev merged commit 189c8fc into wavefnd:master Feb 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant