def get_buffer_code(): nonlocal reported_missing_lifetime if not reported_missing_lifetime: if ((arg.ret or arg.output and depth > 0) and type_.buffer) and type_.lifetime == "AVA_CALL": reported_missing_lifetime = True generate_expects( False, "Returned buffers with call lifetime are almost always incorrect. " "(You may want to set a lifetime.)", ) return Expr( f""" {DECLARE_BUFFER_SIZE_EXPR} {type_.attach_to(src_name)}; {src_name} = ({type_.spelling})({get_transfer_buffer_expr(local_value, type_, not_null=True)}); """ ).then( Expr(type_.lifetime) .not_equals("AVA_CALL") .if_then_else( f"""{get_buffer( param_value, cast_type, local_value, type_, original_type=original_type, not_null=True, declare_buffer_size=False )}""", f"""__buffer_size = {compute_buffer_size(type_, original_type)};""", ) .then(Expr(arg.output).if_then_else(f"AVA_DEBUG_ASSERT({param_value} != NULL);")) )
def _sort_fields(field_infos: List[tuple]): dag = {} fields = {name: (name, k, c) for name, k, c in field_infos} # TODO: Sort based on simple string containment. for name, kernel, _ in field_infos: dag[name] = set(n for n in fields.keys() if n in str(kernel)) # Compute an order which honors the deps try: return [fields[n] for n in toposort_flatten(dag, sort=True)] except CircularDependencyError: generate_expects( False, "Struct fields may be processed out of order, due to hacks in the current implementation." ) return field_infos