Verified Commit 828d5b65 authored by Daniel García Moreno's avatar Daniel García Moreno Committed by Jordan Petridis

gir: Constructor and return for methods

parent fac4b566
use std::fs::File;
use quote::ToTokens;
use xml::writer::{EmitterConfig, XmlEvent};
use xml::EventWriter;
use hir::{Program, Class, Slot};
use hir::{Program, Class, Slot, Method, Ty};
use glib_utils::*;
use gen::names::Names;
......@@ -48,6 +49,7 @@ pub fn generate_gir(class: &Class) -> Result<()> {
.attr("name", &name)
.attr("c:identifier-prefixes", &name)
.attr("c:symbol-prefixes", &lower_name)
// TODO: Get the version and shared-library from Cargo.toml?
//.attr("version", "1.0.0")
//.attr("shared-library", "lib.so.0")
)?;
......@@ -59,15 +61,26 @@ pub fn generate_gir(class: &Class) -> Result<()> {
.attr("c:symbol-prefix", &lower_name)
.attr("glib:type-struct", &names.vtable().to_string())
.attr("glib:get-type", &names.get_type_fn().to_string())
// TODO: Find the parent namespace and class
//.attr("parent", "GObject.Object")
)?;
gen_constructor_xml(&mut w, &names)?;
for slot in class.slots.iter() {
let _ = gen_slot_xml(&mut w, &names, slot);
}
// closing class
w.write(XmlEvent::end_element())?;
// TODO: add Class records here
// <record name="CounterClass"
// c:type="CounterClass"
// disguised="1"
// glib:is-gtype-struct-for="Counter">
// </record>
// closing namespace
w.write(XmlEvent::end_element())?;
// closing repository
......@@ -78,32 +91,45 @@ pub fn generate_gir(class: &Class) -> Result<()> {
}
fn gen_slot_xml(w: &mut EventWriter<&mut File>, names: &Names, slot: &Slot) -> Result<()> {
// <method name="get_margin" c:identifier="gepub_widget_get_margin">
// <doc xml:space="preserve">Gets the widget left and right margin</doc>
// <method name="add" c:identifier="counter_add">
// <return-value transfer-ownership="none">
// <type name="gint" c:type="gint"/>
// </return-value>
// <parameters>
// <instance-parameter name="widget" transfer-ownership="none">
// <doc xml:space="preserve">a #GepubWidget</doc>
// <type name="Widget" c:type="GepubWidget*"/>
// </instance-parameter>
// <parameter name="x" transfer-ownership="none">
// <type name="gint" c:type="gint"/>
// </parameter>
// </parameters>
// </method>
// <method name="get" c:identifier="counter_get">
// <return-value transfer-ownership="none">
// <type name="gint" c:type="gint"/>
// </return-value>
// </method>
match slot {
Slot::Method(ref m) => {
let name = m.sig.name.to_string();
let identifier = names.exported_fn(&m.sig.name).to_string();
Slot::Method(Method {
public: true,
ref sig,
..
}) => {
let name = sig.name.to_string();
let identifier = names.exported_fn(&sig.name).to_string();
let output_ctype = type_to_ctype(&sig.output);
w.write(XmlEvent::start_element("method")
.attr("name", &name)
.attr("c:identifier", &identifier)
)?;
// TODO: add return-value here
// TODO: add parameters here
// TODO: add doc here?
// FIXME, calculate the ownership transfer for pointer types
w.write(XmlEvent::start_element("return-value").attr("transfer-ownership", "none"))?;
w.write(XmlEvent::start_element("type").attr("name", &output_ctype)
.attr("c:type", &output_ctype))?;
w.write(XmlEvent::end_element())?; // closing type
w.write(XmlEvent::end_element())?; // closing return-value
// TODO: add parameters here
// TODO: add doc here?
w.write(XmlEvent::end_element())?;
},
......@@ -115,3 +141,41 @@ fn gen_slot_xml(w: &mut EventWriter<&mut File>, names: &Names, slot: &Slot) -> R
Ok(())
}
fn gen_constructor_xml(w: &mut EventWriter<&mut File>, names: &Names) -> Result<()> {
// <constructor name="new" c:identifier="counter_new">
// <return-value transfer-ownership="full">
// <type name="Counter" c:type="Counter*"/>
// </return-value>
// </constructor>
let identifier = names.exported_fn_from_str("new").to_string();
let name = names.instance().to_string();
let ctype = format!("{}*", name);
w.write(XmlEvent::start_element("constructor")
.attr("name", "new")
.attr("c:identifier", &identifier)
)?;
w.write(XmlEvent::start_element("return-value").attr("transfer-ownership", "full"))?;
w.write(XmlEvent::start_element("type").attr("name", &name).attr("c:type", &ctype))?;
w.write(XmlEvent::end_element())?; // closing type
w.write(XmlEvent::end_element())?; // closing return-value
// closing constructor
w.write(XmlEvent::end_element())?;
Ok(())
}
fn type_to_ctype(type_: &Ty) -> String {
match *type_ {
Ty::Integer(_) => "gint".to_string(),
Ty::Unit => "void".to_string(),
Ty::Char(_) => "gchar*".to_string(),
Ty::Bool(_) => "gboolean".to_string(),
// These two need to be correctly implemented when support
Ty::Borrowed(ref t) => format!("{}*", &type_to_ctype(t.as_ref())),
Ty::Owned(ref t) => t.into_token_stream().to_string(),
}
}
......@@ -24,6 +24,14 @@ gobject_gen! {
pub fn get(&self) -> u32 {
self.get_priv().f.get()
}
pub fn return_none(&self) {
// noop
}
pub fn return_bool(&self) -> bool {
true
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment