def gen_ocaml_keyedunions(ty, interface, indent, parent = None):
s = ""
+ union_type = ""
if ty.rawname is not None:
# Non-anonymous types need no special handling
s += " | ".join(u) + "\n"
ty.union_name = name
+ union_type = "?%s:%s" % (munge_name(nparent), ty.keyvar.type.rawname)
+
if s == "":
- return None
- return s.replace("\n", "\n%s" % indent)
+ return None, None
+ return s.replace("\n", "\n%s" % indent), union_type
def gen_ocaml_ml(ty, interface, indent=""):
s += "module %s = struct\n" % module_name
# Handle KeyedUnions...
+ union_types = []
for f in ty.fields:
- ku = gen_ocaml_keyedunions(f.type, interface, "\t")
+ ku, union_type = gen_ocaml_keyedunions(f.type, interface, "\t")
if ku is not None:
s += ku
s += "\n"
+ if union_type is not None:
+ union_types.append(union_type)
s += "\ttype t =\n"
s += "\t{\n"
s += gen_struct(ty)
s += "\t}\n"
-
+
+ if ty.init_fn is not None:
+ union_args = "".join([u + " -> " for u in union_types])
+ if interface:
+ s += "\tval default : ctx -> %sunit -> t\n" % union_args
+ else:
+ s += "\texternal default : ctx -> %sunit -> t = \"stub_libxl_%s_init\"\n" % (union_args, ty.rawname)
+
if functions.has_key(ty.rawname):
for name,args in functions[ty.rawname]:
s += "\texternal %s : " % name
s += ");\n"
return s
+def gen_c_default(ty):
+ s = "/* Get the defaults for %s */\n" % ty.rawname
+ # Handle KeyedUnions...
+ union_types = []
+ for f in ty.fields:
+ if isinstance(f.type, idl.KeyedUnion):
+ union_types.append(f.type.keyvar)
+
+ s += "value stub_libxl_%s_init(value ctx, %svalue unit)\n" % (ty.rawname,
+ "".join(["value " + u.name + ", " for u in union_types]))
+ s += "{\n"
+ s += "\tCAMLparam%d(ctx, %sunit);\n" % (len(union_types) + 2, "".join([u.name + ", " for u in union_types]))
+ s += "\tCAMLlocal1(val);\n"
+ s += "\tlibxl_%s c_val;\n" % ty.rawname
+ s += "\tlibxl_%s_init(&c_val);\n" % ty.rawname
+ for u in union_types:
+ s += "\tif (%s != Val_none) {\n" % u.name
+ s += "\t\t%s c = 0;\n" % u.type.typename
+ s += "\t\t%s_val(CTX, &c, Some_val(%s));\n" % (u.type.rawname, u.name)
+ s += "\t\tlibxl_%s_init_%s(&c_val, c);\n" % (ty.rawname, u.name)
+ s += "\t}\n"
+ s += "\tval = Val_%s(&c_val);\n" % ty.rawname
+ if ty.dispose_fn:
+ s += "\tlibxl_%s_dispose(&c_val);\n" % ty.rawname
+ s += "\tCAMLreturn(val);\n"
+ s += "}\n"
+ return s
+
+def gen_c_defaults(ty):
+ s = gen_c_default(ty)
+ return s
+
def autogen_header(open_comment, close_comment):
s = open_comment + " AUTO-GENERATED FILE DO NOT EDIT " + close_comment + "\n"
s += open_comment + " autogenerated by \n"
if ty.marshal_in():
cinc.write(gen_c_val(ty))
cinc.write("\n")
- if ty.marshal_out():
- cinc.write(gen_Val_ocaml(ty))
- cinc.write("\n")
+ cinc.write(gen_Val_ocaml(ty))
+ cinc.write("\n")
if functions.has_key(ty.rawname):
cinc.write(gen_c_stub_prototype(ty, functions[ty.rawname]))
cinc.write("\n")
+ if ty.init_fn is not None:
+ cinc.write(gen_c_defaults(ty))
+ cinc.write("\n")
#sys.stdout.write("\n")
ml.write("(* END OF AUTO-GENERATED CODE *)\n")