Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/irx/builders/llvmliteir.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class LLVMLiteIRVisitor(BuilderVisitor):
_llvm: VariablesLLVM

function_protos: dict[str, astx.FunctionPrototype]
struct_types: dict[str, ir.IdentifiedStructType]
struct_defs: dict[str, astx.StructDefStmt]
result_stack: list[ir.Value | ir.Function] = []

def __init__(self) -> None:
Expand All @@ -164,6 +166,8 @@ def __init__(self) -> None:
# named_values as instance variable so it isn't shared across instances
self.named_values: dict[str, Any] = {}
self.function_protos: dict[str, astx.FunctionPrototype] = {}
self.struct_types: dict[str, ir.IdentifiedStructType] = {}
self.struct_defs: dict[str, astx.StructDefStmt] = {}
self.result_stack: list[ir.Value | ir.Function] = []

self.initialize()
Expand Down Expand Up @@ -1914,6 +1918,24 @@ def visit(self, node: astx.FunctionDef) -> None:
self.visit(node.body)
self.result_stack.append(fn)

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.StructDefStmt) -> None:
"""Translate ASTx StructDefStmt to LLVM-IR."""
if node.name in self.struct_types:
raise Exception(f"Struct '{node.name}' already defined.")
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message format is inconsistent with other error messages in the codebase. Most error messages use a prefix like "[EE]:" (see line 145) or "codegen:" (see lines 500, 507, 512, etc.). Consider using a consistent prefix format such as "codegen: Struct 'name' already defined." for better error tracking and consistency.

Suggested change
raise Exception(f"Struct '{node.name}' already defined.")
raise Exception(f"codegen: Struct '{node.name}' already defined.")

Copilot uses AI. Check for mistakes.

struct_type = self._llvm.module.context.get_identified_type(node.name)
self.struct_types[node.name] = struct_type
self.struct_defs[node.name] = node

field_types = []
for attr in node.attributes:
type_str = attr.type_.__class__.__name__.lower()
field_type = self._llvm.get_data_type(type_str)
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type conversion logic using attr.type_.class.name.lower() may fail if get_data_type doesn't recognize the type name. When get_data_type encounters an unknown type at line 145, it raises an exception with "[EE]: Type name {type_name} not valid." However, this could be misleading for struct field types. Consider adding a try-except block or pre-validating the type to provide a more specific error message indicating which field in which struct has an unsupported type.

Suggested change
field_type = self._llvm.get_data_type(type_str)
try:
field_type = self._llvm.get_data_type(type_str)
except Exception as e:
# Provide more context for unsupported struct field types.
field_name = getattr(attr, "name", "<unknown>")
raise Exception(
f"Unsupported type '{type_str}' for field '{field_name}' "
f"in struct '{node.name}'."
) from e

Copilot uses AI. Check for mistakes.
field_types.append(field_type)

struct_type.set_body(*field_types)
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation doesn't handle the case where node.attributes is empty. Creating a struct with no fields will result in calling struct_type.set_body() with no arguments, which may not be the intended behavior for LLVM IR. Consider adding a check to handle empty structs or document this behavior if it's intentional.

Suggested change
struct_type.set_body(*field_types)
if field_types:
# Struct with one or more fields.
struct_type.set_body(*field_types)
else:
# Intentionally define an empty struct when there are no attributes.
struct_type.set_body()

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The visit method for StructDefStmt does not append to result_stack. Other similar visitor methods (like visit for FunctionDef at line 1919 and FunctionPrototype at line 1958) append their results to result_stack. This could cause issues if code expects to retrieve the struct type from the result stack. Consider appending struct_type to result_stack for consistency.

Suggested change
struct_type.set_body(*field_types)
struct_type.set_body(*field_types)
self.result_stack.append(struct_type)

Copilot uses AI. Check for mistakes.

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.FunctionPrototype) -> None:
"""Translate ASTx Function Prototype to LLVM-IR."""
Expand Down
Loading