Skip to content

Add StructType for referencing defined structs in variable declarations #152

@omsherikar

Description

@omsherikar

Description

Enable variables to be declared with struct types by introducing a lightweight StructType reference that resolves to a previously defined struct.

This phase allows declaring struct variables, but does not introduce value semantics, copying, or passing structs by value.


Background


Tasks

1. Add StructType

  • Create StructType in src/irx/system.py
  • Inherit from astx.DataType
  • Store only:
    • struct_name: str

StructType acts purely as a symbolic reference.
Struct definitions are resolved during code generation.


2. Update variable declaration codegen

Update visit(astx.VariableDeclaration) in llvmliteir.py:

  • Detect when type_ is StructType
  • Look up the corresponding LLVM struct type using struct_name
  • Allocate the struct using ir_builder.alloca(llvm_struct_type)
  • Register the variable as holding a pointer to the struct

Error Handling (Required)

Raise a compile-time error if:

  • The referenced struct name is not defined

Example Usage

# Reference a previously defined struct
point_type = StructType(struct_name="Point")

# Declare a struct variable
point_var = astx.VariableDeclaration(name="p", type_=point_type)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions