diff --git a/introspect/ast_nodes.py b/introspect/ast_nodes.py index 9aa58c00..867f49ad 100644 --- a/introspect/ast_nodes.py +++ b/introspect/ast_nodes.py @@ -234,6 +234,7 @@ class StructFieldDecl: 'AnonymousUnionDecl', ] doc: str + array_extent: Optional[Tuple[Union[str, int], ...]] = None def __str__(self): return self.type.decl(self.name) diff --git a/introspect/codegen/generate_structs.py b/introspect/codegen/generate_structs.py index 7e2038b5..1c50db22 100644 --- a/introspect/codegen/generate_structs.py +++ b/introspect/codegen/generate_structs.py @@ -47,6 +47,8 @@ _EXCLUDED = ( 'mjResource_', ) +_ARRAY_COMMENT_PATTERN = re.compile(r'(.+?)\s\s+\((.+) x (.+)\)\Z') + def traverse(node, visitor): visitor.visit(node) @@ -112,8 +114,24 @@ class MjStructVisitor: doc = self._make_comment(child) if 'name' in node: field_type = self._normalize_type(node['type']['qualType']) + m = _ARRAY_COMMENT_PATTERN.match(doc) + if m is None: + array_extent = None + else: + doc = m.group(1) + array_extent_0 = m.group(2) + array_extent_1 = m.group(3) + try: + array_extent_1 = int(array_extent_1) + except ValueError: + pass + if array_extent_1 == 1: + array_extent = (array_extent_0,) + else: + array_extent = (array_extent_0, array_extent_1) return ast_nodes.StructFieldDecl( - name=node['name'], type=field_type, doc=doc) + name=node['name'], type=field_type, doc=doc, + array_extent=array_extent) else: return _AnonymousTypePlaceholder(self._make_anonymous_key(node)) diff --git a/introspect/structs.py b/introspect/structs.py index 22e745c5..96772af3 100644 --- a/introspect/structs.py +++ b/introspect/structs.py @@ -1255,406 +1255,464 @@ STRUCTS: Mapping[str, StructDecl] = dict([ type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='qpos values at default pose (nq x 1)', + doc='qpos values at default pose', + array_extent=('nq',), ), StructFieldDecl( name='qpos_spring', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='reference pose for springs (nq x 1)', + doc='reference pose for springs', + array_extent=('nq',), ), StructFieldDecl( name='body_parentid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of body's parent (nbody x 1)", + doc="id of body's parent", + array_extent=('nbody',), ), StructFieldDecl( name='body_rootid', type=PointerType( inner_type=ValueType(name='int'), ), - doc='id of root above body (nbody x 1)', + doc='id of root above body', + array_extent=('nbody',), ), StructFieldDecl( name='body_weldid', type=PointerType( inner_type=ValueType(name='int'), ), - doc='id of body that this body is welded to (nbody x 1)', + doc='id of body that this body is welded to', + array_extent=('nbody',), ), StructFieldDecl( name='body_mocapid', type=PointerType( inner_type=ValueType(name='int'), ), - doc='id of mocap data; -1: none (nbody x 1)', + doc='id of mocap data; -1: none', + array_extent=('nbody',), ), StructFieldDecl( name='body_jntnum', type=PointerType( inner_type=ValueType(name='int'), ), - doc='number of joints for this body (nbody x 1)', + doc='number of joints for this body', + array_extent=('nbody',), ), StructFieldDecl( name='body_jntadr', type=PointerType( inner_type=ValueType(name='int'), ), - doc='start addr of joints; -1: no joints (nbody x 1)', + doc='start addr of joints; -1: no joints', + array_extent=('nbody',), ), StructFieldDecl( name='body_dofnum', type=PointerType( inner_type=ValueType(name='int'), ), - doc='number of motion degrees of freedom (nbody x 1)', + doc='number of motion degrees of freedom', + array_extent=('nbody',), ), StructFieldDecl( name='body_dofadr', type=PointerType( inner_type=ValueType(name='int'), ), - doc='start addr of dofs; -1: no dofs (nbody x 1)', + doc='start addr of dofs; -1: no dofs', + array_extent=('nbody',), ), StructFieldDecl( name='body_treeid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of body's kinematic tree; -1: static (nbody x 1)", + doc="id of body's kinematic tree; -1: static", + array_extent=('nbody',), ), StructFieldDecl( name='body_geomnum', type=PointerType( inner_type=ValueType(name='int'), ), - doc='number of geoms (nbody x 1)', + doc='number of geoms', + array_extent=('nbody',), ), StructFieldDecl( name='body_geomadr', type=PointerType( inner_type=ValueType(name='int'), ), - doc='start addr of geoms; -1: no geoms (nbody x 1)', + doc='start addr of geoms; -1: no geoms', + array_extent=('nbody',), ), StructFieldDecl( name='body_simple', type=PointerType( inner_type=ValueType(name='mjtByte'), ), - doc='1: diag M; 2: diag M, sliders only (nbody x 1)', + doc='1: diag M; 2: diag M, sliders only', + array_extent=('nbody',), ), StructFieldDecl( name='body_sameframe', type=PointerType( inner_type=ValueType(name='mjtByte'), ), - doc='same frame as inertia (mjtSameframe) (nbody x 1)', + doc='same frame as inertia (mjtSameframe)', + array_extent=('nbody',), ), StructFieldDecl( name='body_pos', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='position offset rel. to parent body (nbody x 3)', + doc='position offset rel. to parent body', + array_extent=('nbody', 3), ), StructFieldDecl( name='body_quat', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='orientation offset rel. to parent body (nbody x 4)', + doc='orientation offset rel. to parent body', + array_extent=('nbody', 4), ), StructFieldDecl( name='body_ipos', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='local position of center of mass (nbody x 3)', + doc='local position of center of mass', + array_extent=('nbody', 3), ), StructFieldDecl( name='body_iquat', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='local orientation of inertia ellipsoid (nbody x 4)', + doc='local orientation of inertia ellipsoid', + array_extent=('nbody', 4), ), StructFieldDecl( name='body_mass', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='mass (nbody x 1)', + doc='mass', + array_extent=('nbody',), ), StructFieldDecl( name='body_subtreemass', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='mass of subtree starting at this body (nbody x 1)', + doc='mass of subtree starting at this body', + array_extent=('nbody',), ), StructFieldDecl( name='body_inertia', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='diagonal inertia in ipos/iquat frame (nbody x 3)', + doc='diagonal inertia in ipos/iquat frame', + array_extent=('nbody', 3), ), StructFieldDecl( name='body_invweight0', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='mean inv inert in qpos0 (trn, rot) (nbody x 2)', + doc='mean inv inert in qpos0 (trn, rot)', + array_extent=('nbody', 2), ), StructFieldDecl( name='body_gravcomp', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='antigravity force, units of body weight (nbody x 1)', + doc='antigravity force, units of body weight', + array_extent=('nbody',), ), StructFieldDecl( name='body_margin', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='MAX over all geom margins (nbody x 1)', + doc='MAX over all geom margins', + array_extent=('nbody',), ), StructFieldDecl( name='body_user', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='user data (nbody x nuser_body)', # pylint: disable=line-too-long + doc='user data', + array_extent=('nbody', 'nuser_body'), ), StructFieldDecl( name='body_plugin', type=PointerType( inner_type=ValueType(name='int'), ), - doc='plugin instance id; -1: not in use (nbody x 1)', + doc='plugin instance id; -1: not in use', + array_extent=('nbody',), ), StructFieldDecl( name='body_contype', type=PointerType( inner_type=ValueType(name='int'), ), - doc='OR over all geom contypes (nbody x 1)', + doc='OR over all geom contypes', + array_extent=('nbody',), ), StructFieldDecl( name='body_conaffinity', type=PointerType( inner_type=ValueType(name='int'), ), - doc='OR over all geom conaffinities (nbody x 1)', + doc='OR over all geom conaffinities', + array_extent=('nbody',), ), StructFieldDecl( name='body_bvhadr', type=PointerType( inner_type=ValueType(name='int'), ), - doc='address of bvh root (nbody x 1)', + doc='address of bvh root', + array_extent=('nbody',), ), StructFieldDecl( name='body_bvhnum', type=PointerType( inner_type=ValueType(name='int'), ), - doc='number of bounding volumes (nbody x 1)', + doc='number of bounding volumes', + array_extent=('nbody',), ), StructFieldDecl( name='bvh_depth', type=PointerType( inner_type=ValueType(name='int'), ), - doc='depth in the bounding volume hierarchy (nbvh x 1)', + doc='depth in the bounding volume hierarchy', + array_extent=('nbvh',), ), StructFieldDecl( name='bvh_child', type=PointerType( inner_type=ValueType(name='int'), ), - doc='left and right children in tree (nbvh x 2)', + doc='left and right children in tree', + array_extent=('nbvh', 2), ), StructFieldDecl( name='bvh_nodeid', type=PointerType( inner_type=ValueType(name='int'), ), - doc='geom or elem id of node; -1: non-leaf (nbvh x 1)', + doc='geom or elem id of node; -1: non-leaf', + array_extent=('nbvh',), ), StructFieldDecl( name='bvh_aabb', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='local bounding box (center, size) (nbvhstatic x 6)', # pylint: disable=line-too-long + doc='local bounding box (center, size)', + array_extent=('nbvhstatic', 6), ), StructFieldDecl( name='jnt_type', type=PointerType( inner_type=ValueType(name='int'), ), - doc='type of joint (mjtJoint) (njnt x 1)', + doc='type of joint (mjtJoint)', + array_extent=('njnt',), ), StructFieldDecl( name='jnt_qposadr', type=PointerType( inner_type=ValueType(name='int'), ), - doc="start addr in 'qpos' for joint's data (njnt x 1)", + doc="start addr in 'qpos' for joint's data", + array_extent=('njnt',), ), StructFieldDecl( name='jnt_dofadr', type=PointerType( inner_type=ValueType(name='int'), ), - doc="start addr in 'qvel' for joint's data (njnt x 1)", + doc="start addr in 'qvel' for joint's data", + array_extent=('njnt',), ), StructFieldDecl( name='jnt_bodyid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of joint's body (njnt x 1)", + doc="id of joint's body", + array_extent=('njnt',), ), StructFieldDecl( name='jnt_group', type=PointerType( inner_type=ValueType(name='int'), ), - doc='group for visibility (njnt x 1)', + doc='group for visibility', + array_extent=('njnt',), ), StructFieldDecl( name='jnt_limited', type=PointerType( inner_type=ValueType(name='mjtByte'), ), - doc='does joint have limits (njnt x 1)', + doc='does joint have limits', + array_extent=('njnt',), ), StructFieldDecl( name='jnt_actfrclimited', type=PointerType( inner_type=ValueType(name='mjtByte'), ), - doc='does joint have actuator force limits (njnt x 1)', + doc='does joint have actuator force limits', + array_extent=('njnt',), ), StructFieldDecl( name='jnt_actgravcomp', type=PointerType( inner_type=ValueType(name='mjtByte'), ), - doc='is gravcomp force applied via actuators (njnt x 1)', + doc='is gravcomp force applied via actuators', + array_extent=('njnt',), ), StructFieldDecl( name='jnt_solref', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='constraint solver reference: limit (njnt x mjNREF)', + doc='constraint solver reference: limit', + array_extent=('njnt', 'mjNREF'), ), StructFieldDecl( name='jnt_solimp', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='constraint solver impedance: limit (njnt x mjNIMP)', + doc='constraint solver impedance: limit', + array_extent=('njnt', 'mjNIMP'), ), StructFieldDecl( name='jnt_pos', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='local anchor position (njnt x 3)', + doc='local anchor position', + array_extent=('njnt', 3), ), StructFieldDecl( name='jnt_axis', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='local joint axis (njnt x 3)', + doc='local joint axis', + array_extent=('njnt', 3), ), StructFieldDecl( name='jnt_stiffness', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='stiffness coefficient (njnt x 1)', + doc='stiffness coefficient', + array_extent=('njnt',), ), StructFieldDecl( name='jnt_range', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='joint limits (njnt x 2)', + doc='joint limits', + array_extent=('njnt', 2), ), StructFieldDecl( name='jnt_actfrcrange', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='range of total actuator force (njnt x 2)', + doc='range of total actuator force', + array_extent=('njnt', 2), ), StructFieldDecl( name='jnt_margin', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='min distance for limit detection (njnt x 1)', + doc='min distance for limit detection', + array_extent=('njnt',), ), StructFieldDecl( name='jnt_user', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='user data (njnt x nuser_jnt)', # pylint: disable=line-too-long + doc='user data', + array_extent=('njnt', 'nuser_jnt'), ), StructFieldDecl( name='dof_bodyid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of dof's body (nv x 1)", + doc="id of dof's body", + array_extent=('nv',), ), StructFieldDecl( name='dof_jntid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of dof's joint (nv x 1)", + doc="id of dof's joint", + array_extent=('nv',), ), StructFieldDecl( name='dof_parentid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of dof's parent; -1: none (nv x 1)", + doc="id of dof's parent; -1: none", + array_extent=('nv',), ), StructFieldDecl( name='dof_treeid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of dof's kinematic tree (nv x 1)", + doc="id of dof's kinematic tree", + array_extent=('nv',), ), StructFieldDecl( name='dof_Madr', type=PointerType( inner_type=ValueType(name='int'), ), - doc='dof address in M-diagonal (nv x 1)', + doc='dof address in M-diagonal', + array_extent=('nv',), ), StructFieldDecl( name='dof_simplenum', type=PointerType( inner_type=ValueType(name='int'), ), - doc='number of consecutive simple dofs (nv x 1)', + doc='number of consecutive simple dofs', + array_extent=('nv',), ), StructFieldDecl( name='dof_solref', @@ -1675,175 +1733,200 @@ STRUCTS: Mapping[str, StructDecl] = dict([ type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='dof friction loss (nv x 1)', + doc='dof friction loss', + array_extent=('nv',), ), StructFieldDecl( name='dof_armature', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='dof armature inertia/mass (nv x 1)', + doc='dof armature inertia/mass', + array_extent=('nv',), ), StructFieldDecl( name='dof_damping', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='damping coefficient (nv x 1)', + doc='damping coefficient', + array_extent=('nv',), ), StructFieldDecl( name='dof_invweight0', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='diag. inverse inertia in qpos0 (nv x 1)', + doc='diag. inverse inertia in qpos0', + array_extent=('nv',), ), StructFieldDecl( name='dof_M0', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='diag. inertia in qpos0 (nv x 1)', + doc='diag. inertia in qpos0', + array_extent=('nv',), ), StructFieldDecl( name='geom_type', type=PointerType( inner_type=ValueType(name='int'), ), - doc='geometric type (mjtGeom) (ngeom x 1)', + doc='geometric type (mjtGeom)', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_contype', type=PointerType( inner_type=ValueType(name='int'), ), - doc='geom contact type (ngeom x 1)', + doc='geom contact type', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_conaffinity', type=PointerType( inner_type=ValueType(name='int'), ), - doc='geom contact affinity (ngeom x 1)', + doc='geom contact affinity', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_condim', type=PointerType( inner_type=ValueType(name='int'), ), - doc='contact dimensionality (1, 3, 4, 6) (ngeom x 1)', + doc='contact dimensionality (1, 3, 4, 6)', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_bodyid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of geom's body (ngeom x 1)", + doc="id of geom's body", + array_extent=('ngeom',), ), StructFieldDecl( name='geom_dataid', type=PointerType( inner_type=ValueType(name='int'), ), - doc="id of geom's mesh/hfield; -1: none (ngeom x 1)", + doc="id of geom's mesh/hfield; -1: none", + array_extent=('ngeom',), ), StructFieldDecl( name='geom_matid', type=PointerType( inner_type=ValueType(name='int'), ), - doc='material id for rendering; -1: none (ngeom x 1)', + doc='material id for rendering; -1: none', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_group', type=PointerType( inner_type=ValueType(name='int'), ), - doc='group for visibility (ngeom x 1)', + doc='group for visibility', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_priority', type=PointerType( inner_type=ValueType(name='int'), ), - doc='geom contact priority (ngeom x 1)', + doc='geom contact priority', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_plugin', type=PointerType( inner_type=ValueType(name='int'), ), - doc='plugin instance id; -1: not in use (ngeom x 1)', + doc='plugin instance id; -1: not in use', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_sameframe', type=PointerType( inner_type=ValueType(name='mjtByte'), ), - doc='same frame as body (mjtSameframe) (ngeom x 1)', + doc='same frame as body (mjtSameframe)', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_solmix', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='mixing coef for solref/imp in geom pair (ngeom x 1)', + doc='mixing coef for solref/imp in geom pair', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_solref', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='constraint solver reference: contact (ngeom x mjNREF)', # pylint: disable=line-too-long + doc='constraint solver reference: contact', + array_extent=('ngeom', 'mjNREF'), ), StructFieldDecl( name='geom_solimp', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='constraint solver impedance: contact (ngeom x mjNIMP)', # pylint: disable=line-too-long + doc='constraint solver impedance: contact', + array_extent=('ngeom', 'mjNIMP'), ), StructFieldDecl( name='geom_size', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='geom-specific size parameters (ngeom x 3)', + doc='geom-specific size parameters', + array_extent=('ngeom', 3), ), StructFieldDecl( name='geom_aabb', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='bounding box, (center, size) (ngeom x 6)', + doc='bounding box, (center, size)', + array_extent=('ngeom', 6), ), StructFieldDecl( name='geom_rbound', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='radius of bounding sphere (ngeom x 1)', + doc='radius of bounding sphere', + array_extent=('ngeom',), ), StructFieldDecl( name='geom_pos', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='local position offset rel. to body (ngeom x 3)', + doc='local position offset rel. to body', + array_extent=('ngeom', 3), ), StructFieldDecl( name='geom_quat', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='local orientation offset rel. to body (ngeom x 4)', + doc='local orientation offset rel. to body', + array_extent=('ngeom', 4), ), StructFieldDecl( name='geom_friction', type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='friction for (slide, spin, roll) (ngeom x 3)', + doc='friction for (slide, spin, roll)', + array_extent=('ngeom', 3), ), StructFieldDecl( name='geom_margin', @@ -1857,133 +1940,152 @@ STRUCTS: Mapping[str, StructDecl] = dict([ type=PointerType( inner_type=ValueType(name='mjtNum'), ), - doc='include in solver if dist