in scripts/gen_wrappers.py [0:0]
def get_struct_dict(struct, struct_name, array_shapes):
struct_dict = OrderedDict()
struct_dict[struct_name] = OrderedDict([('scalars', []),
('arrays', []),
('arrays2d', []),
('ptrs', []),
('depends_on_model', False)])
for child in struct.children():
child_name = child[1].name
child_type = child[1].type
decl = child[1].children()[0][1]
if isinstance(child_type, ArrayDecl):
if hasattr(decl.type.type, "names"):
array_type = ' '.join(decl.type.type.names)
array_size = extract_size_info(decl.dim)
struct_dict[struct_name]['arrays'].append((child_name,
array_type,
array_size))
elif isinstance(decl.type, PtrDecl):
print("skipping pointer array: %s.%s" % (struct_name, child_name))
continue
elif hasattr(decl.type.type.type, "names"):
# assuming a 2d array
array_type = ' '.join(decl.type.type.type.names)
s1 = extract_size_info(decl.dim)
s2 = extract_size_info(decl.type.dim)
struct_dict[struct_name]['arrays2d'].append((child_name, array_type, (s1, s2)))
else:
print("skipping unknown array case: %s.%s\n%s" % (struct_name, child_name, child))
elif isinstance(child_type, TypeDecl):
if isinstance(decl.type, pycparser.c_ast.Struct):
fixed_name = decl.declname
if fixed_name == "global":
fixed_name = "global_"
name = struct_name + "_" + fixed_name
child_struct_dict = get_struct_dict(
decl.type, name, array_shapes)
struct_dict = OrderedDict(struct_dict, **child_struct_dict)
struct_dict[struct_name]['scalars'].append((fixed_name, name))
else:
field_type = ' '.join(decl.type.names)
struct_dict[struct_name]['scalars'].append(
(child_name, field_type))
elif isinstance(child_type, PtrDecl):
ptr_type = ' '.join(decl.type.type.names)
n = struct_name + '.' + child_name
if n not in array_shapes:
print('Warning: skipping {} due to unknown shape'.format(n))
else:
struct_dict[struct_name]['ptrs'].append(
(child_name, ptr_type, array_shapes[n]))
# Structs needing array shapes must get them through mjModel
# but mjModel itself doesn't need to be passed an extra mjModel.
# TODO: depends_on_model should be set to True if any member of this struct depends on mjModel
# but currently that never happens.
if struct_name != 'mjModel':
struct_dict[struct_name]['depends_on_model'] = True
elif isinstance(child_type, Union):
# I'm ignoring unions for now until we think they're necessary
continue
else:
raise NotImplementedError
assert isinstance(struct_dict, OrderedDict), 'Must be deterministic'
return struct_dict