Add nullable attribute to AST nodes for function parameters in API introspection. Fixes #309

This change introduces an `nullable` flag to `ArrayType`, `PointerType`, and `FunctionParameterDecl`. The code generation process now parses function comments from `mujoco.h` to identify parameters marked as "Nullable" and sets this flag accordingly.

PiperOrigin-RevId: 797870883
Change-Id: Ic7cac5a7a9c14177fbf97b8655c5c4809d4ef683
This commit is contained in:
Google DeepMind
2025-08-21 12:33:58 -07:00
committed by Copybara-Service
parent 3dec35a91e
commit ecfe65242f
5 changed files with 148 additions and 10 deletions
+12 -3
View File
@@ -62,9 +62,11 @@ class ValueType:
name: str
is_const: bool = False
is_volatile: bool = False
nullable: bool = False
def __init__(self, name: str, is_const: bool = False,
is_volatile: bool = False):
is_volatile: bool = False,
nullable: bool = False):
is_valid_type_name = (
name == 'void *(*)(void *)' or
VALID_TYPE_NAME_PATTERN.fullmatch(name) or
@@ -74,6 +76,7 @@ class ValueType:
self.name = name
self.is_const = is_const
self.is_volatile = is_volatile
self.nullable = nullable
def decl(self, name_or_decl: Optional[str] = None) -> str:
parts = []
@@ -96,9 +99,11 @@ class ArrayType:
inner_type: Union[ValueType, 'PointerType']
extents: Tuple[int, ...]
nullable: bool = False
def __init__(self, inner_type: Union[ValueType, 'PointerType'],
extents: Sequence[int]):
def __init__(
self, inner_type: Union[ValueType, 'PointerType'], extents: Sequence[int]
):
self.inner_type = inner_type
self.extents = tuple(extents)
@@ -119,6 +124,7 @@ class PointerType:
"""Represents a C pointer type."""
inner_type: Union[ValueType, ArrayType, 'PointerType']
nullable: bool = False
is_const: bool = False
is_volatile: bool = False
is_restrict: bool = False
@@ -126,6 +132,8 @@ class PointerType:
def decl(self, name_or_decl: Optional[str] = None) -> str:
"""Creates a string that declares an object of this type."""
parts = ['*']
if self.nullable:
parts.append('nullable')
if self.is_const:
parts.append('const')
if self.is_volatile:
@@ -155,6 +163,7 @@ class FunctionParameterDecl:
name: str
type: Union[ValueType, ArrayType, PointerType]
nullable: bool = False
def __str__(self):
return self.type.decl(self.name)