about summary refs log tree commit diff
path: root/cmd
diff options
context:
space:
mode:
authorAlexey Yerin <yyp@disroot.org>2023-10-04 20:59:45 +0300
committerAlexey Yerin <yyp@disroot.org>2023-10-04 20:59:45 +0300
commit07cff557463d13bbd1baa970f609c8dbb13e16a8 (patch)
tree5ed04987ffde8fd18f7e58886cdb3e7e57465609 /cmd
parenta3f09a04518ce9ed7cb93719149f75c36ad02856 (diff)
Update for opaque and other Hare changes
Diffstat (limited to 'cmd')
-rw-r--r--cmd/demo/main.ha4
-rw-r--r--cmd/demo4/main.ha4
-rw-r--r--cmd/hare-gi/ctype.ha21
-rw-r--r--cmd/hare-gi/emit.ha42
4 files changed, 46 insertions, 25 deletions
diff --git a/cmd/demo/main.ha b/cmd/demo/main.ha
index cf807c6..0926134 100644
--- a/cmd/demo/main.ha
+++ b/cmd/demo/main.ha
@@ -6,7 +6,7 @@ use os;
 use rt;
 use types::c;
 
-fn about_clicked(btn: *gtk::Button, data: *void) void = {
+fn about_clicked(btn: *gtk::Button, data: *opaque) void = {
 	let dialog = gtk::about_dialog_new();
 	const authors: []*c::char = [
 		c::fromstr("Harriet?"),
@@ -28,7 +28,7 @@ fn about_clicked(btn: *gtk::Button, data: *void) void = {
 	gtk::widget_show_all(dialog);
 };
 
-fn activate(app: *gio::Application, data: *void) void = {
+fn activate(app: *gio::Application, data: *opaque) void = {
 	const app = app: *gtk::Application;
 
 	let button = gtk::button_new_with_label(c::fromstr("About"));
diff --git a/cmd/demo4/main.ha b/cmd/demo4/main.ha
index c46bed2..ea8649c 100644
--- a/cmd/demo4/main.ha
+++ b/cmd/demo4/main.ha
@@ -6,7 +6,7 @@ use os;
 use rt;
 use types::c;
 
-fn about_clicked(btn: *gtk4::Button, data: *void) void = {
+fn about_clicked(btn: *gtk4::Button, data: *opaque) void = {
 	let dialog = gtk4::about_dialog_new();
 	const authors: []*c::char = [
 		c::fromstr("Harriet?"),
@@ -28,7 +28,7 @@ fn about_clicked(btn: *gtk4::Button, data: *void) void = {
 	gtk4::widget_show(dialog);
 };
 
-fn activate(app: *gio::Application, data: *void) void = {
+fn activate(app: *gio::Application, data: *opaque) void = {
 	const app = app: *gtk4::Application;
 
 	let button = gtk4::button_new_with_label(c::fromstr("About"));
diff --git a/cmd/hare-gi/ctype.ha b/cmd/hare-gi/ctype.ha
index 185be7d..c687703 100644
--- a/cmd/hare-gi/ctype.ha
+++ b/cmd/hare-gi/ctype.ha
@@ -9,13 +9,15 @@ type ctype = ((cmodule, str) | str | cbuiltin | cpointer);
 type cpointer = *ctype;
 type cbuiltin = enum {
 	VOID,
+	OPAQUE,
 	INT,
 	UINT,
 	BOOL,
 	SIZE,
 	RUNE,
 	VALIST,
-	VOID_POINTER,
+	UINTPTR,
+	OPAQUE_POINTER,
 
 	CHAR,
 	UCHAR,
@@ -50,13 +52,15 @@ fn ctype_finish(type_: ctype) void = {
 
 fn cbuiltin_str(b: cbuiltin) const str = switch (b) {
 case cbuiltin::VOID => yield "void";
+case cbuiltin::OPAQUE => yield "opaque";
 case cbuiltin::INT => yield "int";
 case cbuiltin::UINT => yield "uint";
 case cbuiltin::BOOL => yield "bool";
 case cbuiltin::SIZE => yield "size";
 case cbuiltin::RUNE => yield "rune";
 case cbuiltin::VALIST => yield "valist";
-case cbuiltin::VOID_POINTER => yield "*void";
+case cbuiltin::UINTPTR => yield "uintptr";
+case cbuiltin::OPAQUE_POINTER => yield "*opaque";
 
 case cbuiltin::CHAR => yield "c::char";
 case cbuiltin::UCHAR => yield "c::uchar";
@@ -130,10 +134,11 @@ let map: [](str, ctype) = [
 	("guint", cbuiltin::UINT),
 	("gfloat", cbuiltin::F32),
 	("gdouble", cbuiltin::F64),
-	("gpointer", cbuiltin::VOID_POINTER),
-	("gconstpointer", cbuiltin::VOID_POINTER),
+	("gpointer", cbuiltin::OPAQUE_POINTER),
+	("gconstpointer", cbuiltin::OPAQUE_POINTER),
 	("grefcount", cbuiltin::UINT),
 	("gatomicrefcount", cbuiltin::UINT),
+	("guintptr", cbuiltin::UINTPTR),
 
 	("gint8", cbuiltin::I8), ("guint8", cbuiltin::U8),
 	("gint16", cbuiltin::I16), ("guint16", cbuiltin::U16),
@@ -191,6 +196,14 @@ fn parse_ctype(type_: str) ctype = {
 	case let r: rune =>
 		switch (r) {
 		case '*' =>
+			match (current) {
+			case let b: cbuiltin =>
+				if (b == cbuiltin::VOID) {
+					current = cbuiltin::OPAQUE_POINTER;
+					continue;
+				};
+			case => void;
+			};
 			current = alloc(current): cpointer: ctype;
 		case => abort();
 		};
diff --git a/cmd/hare-gi/emit.ha b/cmd/hare-gi/emit.ha
index 9f3486e..cf0ecce 100644
--- a/cmd/hare-gi/emit.ha
+++ b/cmd/hare-gi/emit.ha
@@ -27,7 +27,7 @@ fn emit(ctx: *context) (void | io::error) = {
 
 		if (ns.output_file == -1) continue;
 
-		static let wbuf: [os::BUFSIZ]u8 = [0...];
+		static let wbuf: [os::BUFSZ]u8 = [0...];
 		const stream = bufio::init(ns.output_file, [], wbuf);
 		ctx.current.output = &stream;
 
@@ -86,7 +86,7 @@ fn emit_alias(ctx: *context, alias: *gir::alias) (void | io::error) = {
 	emit_doc(ctx, alias)?;
 	fmt::fprintf(ctx.current.output, "export type {} = ",
 		fix_identifier(alias.name))?;
-	emit_type(ctx, alias.inner)?;
+	emit_type(ctx, alias.inner, ctype_flag::VOID_IS_OPAQUE)?;
 	fmt::fprintln(ctx.current.output, ";")?;
 };
 
@@ -101,7 +101,7 @@ fn emit_class(ctx: *context, class: *gir::class) (void | io::error) = {
 		fix_identifier(class.name))?;
 	if (len(class.entries) == 0) {
 		// opaque
-		fmt::fprintln(ctx.current.output, "*void;")?;
+		fmt::fprintln(ctx.current.output, "*opaque;")?;
 	} else {
 		fmt::fprintln(ctx.current.output, "struct {")?;
 		for (let i = 0z; i < len(class.entries); i += 1) {
@@ -146,7 +146,7 @@ fn emit_interface(ctx: *context, iface: *gir::interface) (void | io::error) = {
 		fix_identifier(iface.name))?;
 	if (len(iface.entries) == 0) {
 		// opaque
-		fmt::fprintln(ctx.current.output, "*void;")?;
+		fmt::fprintln(ctx.current.output, "*opaque;")?;
 	} else {
 		fmt::fprintln(ctx.current.output, "struct {")?;
 		for (let i = 0z; i < len(iface.entries); i += 1) {
@@ -190,7 +190,7 @@ fn emit_union(ctx: *context, union_: *gir::union_) (void | io::error) = {
 	fmt::fprintf(ctx.current.output, "export type {} = ",
 		fix_identifier(union_.name))?;
 	if (len(union_.entries) == 0) {
-		fmt::fprintln(ctx.current.output, "void;")?; // FIXME
+		fmt::fprintln(ctx.current.output, "opaque;")?; // FIXME
 	} else {
 		fmt::fprintln(ctx.current.output, "union {")?;
 		for (let i = 0z; i < len(union_.entries); i += 1) {
@@ -221,8 +221,8 @@ fn emit_record(ctx: *context, record: *gir::record) (void | io::error) = {
 	emit_doc(ctx, record)?;
 	fmt::fprintf(ctx.current.output, "export type {} = ",
 		fix_identifier(record.name))?;
-	if (record.opaque || len(record.entries) == 0) {
-		fmt::fprintln(ctx.current.output, "*void;")?;
+	if (record.opaque_ || len(record.entries) == 0) {
+		fmt::fprintln(ctx.current.output, "*opaque;")?;
 	} else {
 		fmt::fprintln(ctx.current.output, "struct {")?;
 		for (let i = 0z; i < len(record.entries); i += 1) {
@@ -404,13 +404,13 @@ fn emit_signal(ctx: *context, signal: *gir::signal) (void | io::error) = {
 	//
 	// export fn window_connect_activate(
 	// 	instance: *gtk::Window,
-	// 	handler: *fn(instance: *gtk::Window, data: *void) void,
-	// 	data: nullable *void,
+	// 	handler: *fn(instance: *gtk::Window, data: *opaque) void,
+	// 	data: nullable *opaque,
 	// ) u64 = gobject::signal_connect_data(
 	// 	instance,
 	// 	*(&"activate\0": []u8): *[*]u8: *c::char,
 	// 	handler: gobject::Callback,
-	// 	data: *void,
+	// 	data: *opaque,
 	// 	null: gobject::ClosureNotify, 0,
 	// );
 
@@ -434,10 +434,10 @@ fn emit_signal(ctx: *context, signal: *gir::signal) (void | io::error) = {
 		fmt::fprint(ctx.current.output, ", ")?;
 		emit_params(ctx, signal.params)?;
 	};
-	fmt::fprint(ctx.current.output, ", data: *void) ")?;
+	fmt::fprint(ctx.current.output, ", data: *opaque) ")?;
 	emit_return_value(ctx, signal.return_value)?;
 	fmt::fprintln(ctx.current.output, ",")?;
-	fmt::fprintln(ctx.current.output, "\t" "data: nullable *void,")?;
+	fmt::fprintln(ctx.current.output, "\t" "data: nullable *opaque,")?;
 	fmt::fprint(ctx.current.output, ") u64 = ")?;
 	emit_object(ctx, ctx.gobject, "signal_connect_data")?;
 	fmt::fprintln(ctx.current.output, "(")?;
@@ -449,7 +449,7 @@ fn emit_signal(ctx: *context, signal: *gir::signal) (void | io::error) = {
 	fmt::fprint(ctx.current.output,    "\t" "handler: ")?;
 	emit_object(ctx, ctx.gobject, "Callback")?;
 	fmt::fprintln(ctx.current.output, ",")?;
-	fmt::fprintln(ctx.current.output,  "\t" "data: *void,")?;
+	fmt::fprintln(ctx.current.output,  "\t" "data: *opaque,")?;
 	fmt::fprint(ctx.current.output,    "\t" "null: ")?;
 	emit_object(ctx, ctx.gobject, "ClosureNotify")?;
 	fmt::fprintln(ctx.current.output, ", 0,")?;
@@ -584,13 +584,17 @@ fn emit_doc(ctx: *context, item: *gir::documentation) (void | io::error) = {
 	};
 };
 
-fn emit_type(ctx: *context, t: (gir::any_type | gir::callback)) (void | io::error) = {
+type ctype_flag = enum {
+	VOID_IS_OPAQUE,
+};
+
+fn emit_type(ctx: *context, t: (gir::any_type | gir::callback), flags: ctype_flag...) (void | io::error) = {
 	match (t) {
 	case let t: gir::simple_type =>
 		if (len(t.c_type) > 0) {
 			const parsed = parse_ctype(t.c_type);
 			defer ctype_finish(parsed);
-			return emit_c_type(ctx, parsed);
+			return emit_c_type(ctx, parsed, flags...);
 		} else {
 			const (first, second) = strings::cut(t.name, ".");
 			if (len(second) == 0) {
@@ -613,7 +617,7 @@ fn emit_type(ctx: *context, t: (gir::any_type | gir::callback)) (void | io::erro
 					};
 				case => void;
 				};
-				return emit_c_type(ctx, parsed);
+				return emit_c_type(ctx, parsed, flags...);
 			} else {
 				const ns = match (get_namespace(ctx, first)) {
 				case let ns: *namespace =>
@@ -665,7 +669,7 @@ fn emit_callback_type(ctx: *context, cb: *gir::callback) (void | io::error) = {
 	emit_return_value(ctx, cb.return_value)?;
 };
 
-fn emit_c_type(ctx: *context, type_: ctype) (void | io::error) = {
+fn emit_c_type(ctx: *context, type_: ctype, flags: ctype_flag...) (void | io::error) = {
 	match (type_) {
 	case let special: (cmodule, str) =>
 		const (mod, id) = special;
@@ -690,6 +694,10 @@ fn emit_c_type(ctx: *context, type_: ctype) (void | io::error) = {
 		if (cbuiltin_needs_import(b)) {
 			add_import(ctx.current, ["types", "c"]);
 		};
+		if (b == cbuiltin::VOID && len(flags) > 0) {
+			// there's only one flag - VOID_IS_OPAQUE
+			b = cbuiltin::OPAQUE;
+		};
 		fmt::fprint(ctx.current.output, cbuiltin_str(b))?;
 	case let p: cpointer =>
 		fmt::fprint(ctx.current.output, '*')?;