Skip to content

Commit

Permalink
tools: ynl-gen: fill in support for MultiAttr scalars
Browse files Browse the repository at this point in the history
The handshake family needs support for MultiAttr scalars.
Right now we only support code gen for MultiAttr nested
types.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Jakub Kicinski committed Jun 7, 2023
1 parent e721466 commit 2cc9671
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions tools/net/ynl/ynl-gen-c.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ def free(self, ri, var, ref):
def arg_member(self, ri):
member = self._complex_member_type(ri)
if member:
return [member + ' *' + self.c_name]
arg = [member + ' *' + self.c_name]
if self.presence_type() == 'count':
arg += ['unsigned int n_' + self.c_name]
return arg
raise Exception(f"Struct member not implemented for class type {self.type}")

def struct_member(self, ri):
Expand Down Expand Up @@ -188,9 +191,12 @@ def setter(self, ri, space, direction, deref=False, ref=None):
code.append(presence + ' = 1;')
code += self._setter_lines(ri, member, presence)

ri.cw.write_func('static inline void',
f"{op_prefix(ri, direction, deref=deref)}_set_{'_'.join(ref)}",
body=code,
func_name = f"{op_prefix(ri, direction, deref=deref)}_set_{'_'.join(ref)}"
free = bool([x for x in code if 'free(' in x])
alloc = bool([x for x in code if 'alloc(' in x])
if free and not alloc:
func_name = '__' + func_name
ri.cw.write_func('static inline void', func_name, body=code,
args=[f'{type_name(ri, direction, deref=deref)} *{var}'] + self.arg_member(ri))


Expand Down Expand Up @@ -444,6 +450,13 @@ def is_multi_val(self):
def presence_type(self):
return 'count'

def _mnl_type(self):
t = self.type
# mnl does not have a helper for signed types
if t[0] == 's':
t = 'u' + t[1:]
return t

def _complex_member_type(self, ri):
if 'type' not in self.attr or self.attr['type'] == 'nest':
return f"struct {self.nested_render_name}"
Expand All @@ -457,9 +470,14 @@ def free_needs_iter(self):
return 'type' not in self.attr or self.attr['type'] == 'nest'

def free(self, ri, var, ref):
if 'type' not in self.attr or self.attr['type'] == 'nest':
if self.attr['type'] in scalars:
ri.cw.p(f"free({var}->{ref}{self.c_name});")
elif 'type' not in self.attr or self.attr['type'] == 'nest':
ri.cw.p(f"for (i = 0; i < {var}->{ref}n_{self.c_name}; i++)")
ri.cw.p(f'{self.nested_render_name}_free(&{var}->{ref}{self.c_name}[i]);')
ri.cw.p(f"free({var}->{ref}{self.c_name});")
else:
raise Exception(f"Free of MultiAttr sub-type {self.attr['type']} not supported yet")

def _attr_typol(self):
if 'type' not in self.attr or self.attr['type'] == 'nest':
Expand All @@ -472,6 +490,25 @@ def _attr_typol(self):
def _attr_get(self, ri, var):
return f'{var}->n_{self.c_name}++;', None, None

def attr_put(self, ri, var):
if self.attr['type'] in scalars:
put_type = self._mnl_type()
ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
ri.cw.p(f"mnl_attr_put_{put_type}(nlh, {self.enum_name}, {var}->{self.c_name}[i]);")
elif 'type' not in self.attr or self.attr['type'] == 'nest':
ri.cw.p(f"for (unsigned int i = 0; i < {var}->n_{self.c_name}; i++)")
self._attr_put_line(ri, var, f"{self.nested_render_name}_put(nlh, " +
f"{self.enum_name}, &{var}->{self.c_name}[i])")
else:
raise Exception(f"Put of MultiAttr sub-type {self.attr['type']} not supported yet")

def _setter_lines(self, ri, member, presence):
# For multi-attr we have a count, not presence, hack up the presence
presence = presence[:-(len('_present.') + len(self.c_name))] + "n_" + self.c_name
return [f"free({member});",
f"{member} = {self.c_name};",
f"{presence} = n_{self.c_name};"]


class TypeArrayNest(Type):
def is_multi_val(self):
Expand Down

0 comments on commit 2cc9671

Please sign in to comment.