Skip to content

Commit e636fca

Browse files
add assignment info class family and change how assignments are handled
1 parent 5512bf5 commit e636fca

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from enum import Enum, auto
2+
from typing import Any, Callable, Dict, List, Optional, TypedDict
3+
from dataclasses import dataclass
4+
5+
from pythonbpf.vmlinux_parser.dependency_node import Field
6+
7+
8+
@dataclass
9+
class AssignmentType(Enum):
10+
CONSTANT = auto()
11+
STRUCT = auto()
12+
ARRAY = auto() # probably won't be used
13+
FUNCTION_POINTER = auto()
14+
POINTER = auto() # again, probably won't be used
15+
16+
@dataclass
17+
class FunctionSignature(TypedDict):
18+
return_type: str
19+
param_types: List[str]
20+
varargs: bool
21+
22+
23+
# Thew name of the assignment will be in the dict that uses this class
24+
@dataclass
25+
class AssignmentInfo(TypedDict):
26+
value_type: AssignmentType
27+
python_type: type
28+
value: Optional[Any]
29+
pointer_level: Optional[int]
30+
signature: Optional[FunctionSignature] # For function pointers
31+
# The key of the dict is the name of the field.
32+
# Value is a tuple that contains the global variable representing that field
33+
# along with all the information about that field as a Field type.
34+
members: Optional[Dict[str, tuple[str, Field]]] # For structs.

pythonbpf/vmlinux_parser/import_detector.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import importlib
55
import inspect
66

7+
from .assignment_info import AssignmentInfo, AssignmentType
78
from .dependency_handler import DependencyHandler
89
from .ir_gen import IRGenerator
910
from .class_handler import process_vmlinux_class
@@ -82,7 +83,7 @@ def vmlinux_proc(tree: ast.AST, module):
8283
# initialise dependency handler
8384
handler = DependencyHandler()
8485
# initialise assignment dictionary of name to type
85-
assignments: dict[str, tuple[type, Any]] = {}
86+
assignments: dict[str, AssignmentInfo] = {}
8687

8788
if not import_statements:
8889
logger.info("No vmlinux imports found")
@@ -132,16 +133,31 @@ def vmlinux_proc(tree: ast.AST, module):
132133
return assignments
133134

134135

135-
def process_vmlinux_assign(node, module, assignments: dict[str, tuple[type, Any]]):
136-
# Check if this is a simple assignment with a constant value
136+
def process_vmlinux_assign(node, module, assignments: dict[str, AssignmentInfo]):
137+
"""Process assignments from vmlinux module."""
138+
# Only handle single-target assignments
137139
if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
138140
target_name = node.targets[0].id
141+
142+
# Handle constant value assignments
139143
if isinstance(node.value, ast.Constant):
140-
assignments[target_name] = (type(node.value.value), node.value.value)
144+
# Fixed: using proper TypedDict creation syntax with named arguments
145+
assignments[target_name] = AssignmentInfo(
146+
value_type=AssignmentType.CONSTANT,
147+
python_type=type(node.value.value),
148+
value=node.value.value,
149+
pointer_level=None,
150+
signature=None,
151+
members=None
152+
)
141153
logger.info(
142154
f"Added assignment: {target_name} = {node.value.value!r} of type {type(node.value.value)}"
143155
)
156+
157+
# Handle other assignment types that we may need to support
144158
else:
145-
raise ValueError(f"Unsupported assignment type for {target_name}")
159+
logger.warning(
160+
f"Unsupported assignment type for {target_name}: {ast.dump(node.value)}"
161+
)
146162
else:
147163
raise ValueError("Not a simple assignment")

tests/passing_tests/vmlinux/simple_struct_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ def LICENSE() -> str:
2727

2828

2929
compile_to_ir("simple_struct_test.py", "simple_struct_test.ll")
30-
compile()

0 commit comments

Comments
 (0)