[vala/wip/dbusgen: 119/144] Initial commit of dbusgen



commit b5e860ab982f4e4562af0a628d6e9f6f1fe3c1e4
Author: Chris Daley <chebizarro gmail com>
Date:   Mon Nov 20 13:49:16 2017 -0800

    Initial commit of dbusgen

 Makefile.am                            |   1 +
 configure.ac                           |   2 +
 dbusgen/.gitignore                     |   1 +
 dbusgen/Makefile.am                    |  72 ++++++
 dbusgen/valadbusgen.pc.in              |  14 ++
 dbusgen/valadbusgen.vala               | 195 ++++++++++++++++
 dbusgen/valadbusnamespacestrategy.vala |  28 +++
 dbusgen/valadbusparser.vala            | 406 +++++++++++++++++++++++++++++++++
 dbusgen/valadbusvariantmodule.vala     | 205 +++++++++++++++++
 9 files changed, 924 insertions(+)
---
diff --git a/Makefile.am b/Makefile.am
index f70234759..7a32cddbb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ if ENABLE_VALADOC
 SUBDIRS += \
        libvaladoc \
        valadoc \
+       dbusgen \
        $(NULL)
 endif
 
diff --git a/configure.ac b/configure.ac
index 408dfd86f..b37f445fb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -215,6 +215,8 @@ AC_CONFIG_FILES([Makefile
            doc/manual/Makefile
            doc/manual/version.xml
            gobject-introspection/Makefile
+           dbusgen/valadbusgen.pc
+           dbusgen/Makefile
            vapigen/vapigen.pc
            vapigen/Makefile
            vapigen/vala-gen-introspect/Makefile
diff --git a/dbusgen/.gitignore b/dbusgen/.gitignore
new file mode 100644
index 000000000..a5294aa13
--- /dev/null
+++ b/dbusgen/.gitignore
@@ -0,0 +1 @@
+valadbusgen
diff --git a/dbusgen/Makefile.am b/dbusgen/Makefile.am
new file mode 100644
index 000000000..d5175eb80
--- /dev/null
+++ b/dbusgen/Makefile.am
@@ -0,0 +1,72 @@
+include $(top_srcdir)/Makefile.common
+
+NULL =
+
+AM_CPPFLAGS = \
+       $(GLIB_CFLAGS) \
+       -I$(top_srcdir)/gee \
+       -I$(top_srcdir)/vala \
+       $(NULL)
+
+bin_PROGRAMS = \
+       valadbusgen \
+       $(NULL)
+
+BUILT_SOURCES = valadbusgen.vala.stamp
+
+valadbusgen_VALASOURCES = \
+       valadbusgen.vala \
+       valadbusparser.vala \
+       valadbusvariantmodule.vala \
+       valadbusnamespacestrategy.vala \
+       $(NULL)
+
+valadbusgen_SOURCES = \
+       valadbusgen.vala.stamp \
+       $(valadbusgen_VALASOURCES:.vala=.c) \
+       $(NULL)
+
+
+valadbusgen.vala.stamp: $(valadbusgen_VALASOURCES)
+       $(VALA_V)$(VALAC) \
+               $(VALAFLAGS) \
+               -C \
+               --vapidir $(top_srcdir)/vapi \
+               --vapidir $(top_srcdir)/gee --pkg gee \
+               --vapidir $(top_srcdir)/vala --pkg vala \
+               --pkg config \
+               $^
+       @touch $@
+
+
+valadbusgen_LDADD = \
+       $(GLIB_LIBS) \
+       $(top_builddir)/vala/libvala@PACKAGE_SUFFIX@.la \
+       $(NULL)
+
+pkgconfigdir = $(libdir)/pkgconfig
+pkgconfig_DATA = valadbusgen@PACKAGE_SUFFIX@.pc
+
+valadbusgen@PACKAGE_SUFFIX@.pc: valadbusgen.pc
+       cp $< $@
+
+EXTRA_DIST = \
+       $(valadbusgen_VALASOURCES) \
+       valadbusgen.vala.stamp \
+       $(NULL)
+
+CLEANFILES = \
+       valadbusgen@PACKAGE_SUFFIX@.pc
+       $(NULL)
+
+if ENABLE_UNVERSIONED
+install-exec-hook:
+       cd $(DESTDIR)$(bindir) && $(LN_S) -f valadbusgen@PACKAGE_SUFFIX@$(EXEEXT) valadbusgen$(EXEEXT)
+
+install-data-hook:
+       cd $(DESTDIR)$(pkgconfigdir) && $(LN_S) -f valadbusgen@PACKAGE_SUFFIX@.pc valadbusgen.pc
+endif
+
+MAINTAINERCLEANFILES = \
+       $(valadbusgen_VALASOURCES:.vala=.c) \
+       $(NULL)
diff --git a/dbusgen/valadbusgen.pc.in b/dbusgen/valadbusgen.pc.in
new file mode 100644
index 000000000..3ce60dbff
--- /dev/null
+++ b/dbusgen/valadbusgen.pc.in
@@ -0,0 +1,14 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+bindir=@bindir@
+datarootdir=@datarootdir@
+datadir=@datadir@
+includedir=@includedir@
+
+vapidir=${datadir}/vala/vapi
+vapidir_versioned=${datadir}/vala@PACKAGE_SUFFIX@/vapi
+
+Name: valadbusgen
+Description: Vala DBus Interface Generator
+Version: @VERSION@
diff --git a/dbusgen/valadbusgen.vala b/dbusgen/valadbusgen.vala
new file mode 100644
index 000000000..c133bff0e
--- /dev/null
+++ b/dbusgen/valadbusgen.vala
@@ -0,0 +1,195 @@
+/* valadbusgen.vala
+ *
+ * Copyright (C) 2017 Chris Daley
+ * Copyright (C) 2006-2010  Jürg Billeter
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Chris Daley <chebizarro gmail com>
+ *     Jürg Billeter <j bitron ch>
+ */
+
+using GLib;
+
+public class Vala.DBusGen {
+
+       public class ConcatenationStrategy : NamespaceStrategy {
+
+               public override string? get_namespace (string ns) {
+                       return null;
+               }
+
+               public override string get_name (string ns) {
+
+                       string[] parts = ns.split(".");
+                       StringBuilder capitalized_name = new StringBuilder();
+                       foreach (string part in parts) {
+                               if (part != "") {
+                                       capitalized_name.append(part.substring(0, 1).up());
+                                       capitalized_name.append(part.substring(1, part.length - 1));
+                               }
+                       }
+                       return capitalized_name.str;
+               }
+
+       }
+
+       static string directory;
+       static bool version;
+       static bool quiet_mode = false;
+       static bool disable_warnings;
+       [CCode (array_length = false, array_null_terminated = true)]
+       static string[] sources;
+       [CCode (array_length = false, array_null_terminated = true)]
+       static string[] vapi_directories;
+       [CCode (array_length = false, array_null_terminated = true)]
+       static string[] packages;
+       static bool nostdpkg;
+       static int dbus_timeout = 120000;
+
+       CodeContext context;
+
+       const OptionEntry[] options = {
+               { "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings 
in DIRECTORY", "DIRECTORY..." },
+               { "nostdpkg", 0, 0, OptionArg.NONE, ref nostdpkg, "Do not include standard packages", null },
+               { "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", 
"PACKAGE..." },
+               { "directory", 'd', 0, OptionArg.FILENAME, ref directory, "Output directory", "DIRECTORY" },
+               { "disable-warnings", 0, 0, OptionArg.NONE, ref disable_warnings, "Disable warnings", null },
+               { "dbus-timeout", 0, 0, OptionArg.INT, ref dbus_timeout, "DBus timeout", null },
+               { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
+               { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", 
null },
+               { OPTION_REMAINING, 0, 0, OptionArg.FILENAME_ARRAY, ref sources, null, "FILE..." },
+               { null }
+       };
+
+       private int quit () {
+               if (context.report.get_errors () == 0) {
+                       if (!quiet_mode) {
+                               stdout.printf ("Generation succeeded - %d warning(s)\n", 
context.report.get_warnings ());
+                       }
+                       return 0;
+               } else {
+                       if (!quiet_mode) {
+                               stdout.printf ("Generation failed: %d error(s), %d warning(s)\n", 
context.report.get_errors (), context.report.get_warnings ());
+                       }
+                       return 1;
+               }
+       }
+
+       private int run () {
+               context = new CodeContext ();
+
+               context.report.enable_warnings = !disable_warnings;
+               context.vapi_directories = vapi_directories;
+               context.report.set_verbose_errors (!quiet_mode);
+               CodeContext.push (context);
+
+               context.profile = Profile.GOBJECT;
+               context.add_define ("GOBJECT");
+
+               context.add_external_package ("glib-2.0");
+               context.add_external_package ("gobject-2.0");
+
+               if (packages != null) {
+                       foreach (string package in packages) {
+                               context.add_external_package (package);
+                       }
+                       packages = null;
+               }
+
+               if (context.report.get_errors () > 0) {
+                       return quit ();
+               }
+
+               var valaparser = new Parser ();
+               valaparser.parse (context);
+
+               if (context.report.get_errors () > 0) {
+                       return quit ();
+               }
+
+               foreach (string source in sources) {
+                       if (FileUtils.test (source, FileTest.EXISTS) && source.has_suffix (".xml")) {
+                               var source_file = new SourceFile (context, SourceFileType.SOURCE, source);
+                               source_file.explicit = true;
+                               context.add_source_file (source_file);
+                       } else {
+                               Report.error (null, "%s not found".printf (source));
+                       }
+               }
+
+               if (context.report.get_errors () > 0) {
+                       return quit ();
+               }
+
+               var parser = new DBusParser ();
+               parser.dbus_timeout = dbus_timeout;
+               parser.namespace_strategy = new ConcatenationStrategy ();
+               parser.parse (context);
+
+               if (context.report.get_errors () > 0) {
+                       return quit ();
+               }
+
+               context.check ();
+
+               if (context.report.get_errors () > 0) {
+                       return quit ();
+               }
+
+               var interface_writer = new CodeWriter (CodeWriterType.FAST);
+
+               foreach (SourceFile source in context.get_source_files ()) {
+                       string filename = Path.get_basename (source.filename);
+
+                       if (filename.has_suffix (".xml")) {
+                               if (directory != null) {
+                                       filename = Path.build_path ("/", directory, filename + ".vala");
+                               }
+                               interface_writer.write_file (context, filename + ".vala");
+                       }
+               }
+
+               return quit ();
+       }
+
+       static int main (string[] args) {
+               Intl.setlocale (LocaleCategory.ALL, "");
+               try {
+                       var opt_context = new OptionContext ("- Vala DBus Interface Generator");
+                       opt_context.set_help_enabled (true);
+                       opt_context.add_main_entries (options, null);
+                       opt_context.parse (ref args);
+               } catch (OptionError e) {
+                       stdout.printf ("%s\n", e.message);
+                       stdout.printf ("Run '%s --help' to see a full list of available command line 
options.\n", args[0]);
+                       return 1;
+               }
+
+               if (version) {
+                       stdout.printf ("Vala DBus Interface Generator %s\n", Vala.BUILD_VERSION);
+                       return 0;
+               }
+
+               if (sources == null) {
+                       stderr.printf ("No source file specified.\n");
+                       return 1;
+               }
+
+               var DBusGen = new DBusGen ();
+               return DBusGen.run ();
+       }
+}
diff --git a/dbusgen/valadbusnamespacestrategy.vala b/dbusgen/valadbusnamespacestrategy.vala
new file mode 100644
index 000000000..3c95f1b6e
--- /dev/null
+++ b/dbusgen/valadbusnamespacestrategy.vala
@@ -0,0 +1,28 @@
+/* valadbusnamespacestrategy.vala
+ *
+ * Copyright (C) 2017 Chris Daley
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Chris Daley <chebizarro gmail com>
+ */
+
+public abstract class Vala.NamespaceStrategy {
+
+       public abstract string? get_namespace (string ns);
+
+       public abstract string get_name (string ns);
+}
diff --git a/dbusgen/valadbusparser.vala b/dbusgen/valadbusparser.vala
new file mode 100644
index 000000000..b1649bd5d
--- /dev/null
+++ b/dbusgen/valadbusparser.vala
@@ -0,0 +1,406 @@
+/* valadbusparser.vala
+ *
+ * Copyright (C) 2017 Chris Daley
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Chris Daley <chebizarro gmail com>
+ */
+
+using GLib;
+
+/**
+ * Code visitor parsing all DBus Interface files.
+ */
+public class Vala.DBusParser : CodeVisitor {
+
+       private CodeContext context;
+
+       private DBusVariantModule dbus_module;
+
+       private SourceFile current_source_file;
+
+       private CodeNode current_node;
+       private Namespace current_ns;
+       private Interface current_iface;
+       private Callable current_method;
+       private Parameter current_param;
+       private Property current_property;
+
+       private MarkupReader reader;
+
+       private MarkupTokenType current_token;
+       private SourceLocation begin;
+       private SourceLocation end;
+
+       public int dbus_timeout { get; set; }
+
+       public NamespaceStrategy namespace_strategy { get; set; }
+
+       /**
+        * Parse all DBus Interface files in the specified code context and
+        * build a code tree.
+        *
+        * @param context a code context
+        */
+       public void parse (CodeContext context) {
+
+               this.context = context;
+               current_ns = context.root;
+
+               dbus_module = new DBusVariantModule (context);
+
+               context.accept (this);
+       }
+
+       public override void visit_source_file (SourceFile source_file) {
+               if (source_file.filename.has_suffix (".xml")) {
+                       parse_file (source_file);
+               }
+       }
+
+       private void parse_file (SourceFile source_file) {
+
+               this.current_source_file = source_file;
+               this.reader = new MarkupReader (source_file.filename);
+
+               current_source_file.add_node (context.root);
+
+               next ();
+               next ();
+               next ();
+
+               parse_node ();
+               this.current_source_file = null;
+
+               this.reader = null;
+
+       }
+
+       private void parse_node () {
+               start_element ("node");
+
+               next ();
+
+               while (current_token == MarkupTokenType.START_ELEMENT) {
+
+                       switch (reader.name) {
+                               case "interface":
+                                       parse_namespace ();
+                                       parse_interface ();
+                                       break;
+                               //case "node":
+                               //      parse_node ();
+                                       //break;
+                               default:
+                                       parse_extension ();
+                                       break;
+                       }
+               }
+
+               end_element ("node");
+       }
+
+       private void parse_namespace () {
+               string? name = reader.get_attribute ("name");
+
+               if (name == null) {
+                       Report.error (get_current_src (), "Interface declarations require a name attribute");
+                       return;
+               }
+
+               string? ns_name = namespace_strategy.get_namespace (name);
+
+               if (ns_name != null) {
+                       var ns = new Namespace (ns_name, get_current_src ());
+                       current_ns.add_namespace (ns);
+                       current_ns = ns;
+               }
+       }
+
+       private void parse_interface () {
+               start_element ("interface");
+
+               string? name = reader.get_attribute ("name");
+
+               if (name == null) {
+                       Report.error (get_current_src (), "Interface declarations require a name attribute");
+                       return;
+               }
+
+               string iface_name = namespace_strategy.get_name (name);
+
+               current_node = current_iface = new Interface (iface_name, get_current_src ());
+
+               current_iface.access = SymbolAccessibility.PUBLIC;
+               current_iface.add_prerequisite (new ObjectType ((ObjectTypeSymbol) dbus_module.gobject_type));
+               current_iface.set_attribute_string ("DBus", "name", name);
+               current_iface.set_attribute_integer ("DBus", "timeout", dbus_timeout);
+
+               current_ns.add_interface (current_iface);
+
+               next ();
+
+               parse_interface_body ();
+
+               end_element ("interface");
+
+       }
+
+       private void parse_interface_body () {
+
+               while (current_token == MarkupTokenType.START_ELEMENT) {
+
+                       switch (reader.name) {
+                               case "annotation":
+                                       parse_annotation ();
+                                       break;
+                               case "method":
+                                       parse_method ();
+                                       break;
+                               case "signal":
+                                       parse_signal ();
+                                       break;
+                               case "property":
+                                       parse_property ();
+                                       break;
+                               default:
+                                       parse_extension ();
+                                       break;
+                       }
+               }
+       }
+
+       private void parse_annotation () {
+               start_element ("annotation");
+
+               string? anno = reader.get_attribute ("name");
+
+               if (anno == null) {
+                       Report.error (get_current_src (), "Annotations require a name attribute");
+                       return;
+               }
+
+               switch (anno) {
+
+                       case "org.freedesktop.DBus.Deprecated":
+                               current_node.set_attribute_bool ("Version", "deprecated", true);
+                               string? replaced_by = reader.get_attribute ("replaced-by");
+                               if (replaced_by != null) {
+                                       current_node.set_attribute_string ("Version", "replacement", 
replaced_by);
+                               }
+                               break;
+                       case "org.freedesktop.DBus.GLib.Async":
+                               if (current_node is Method) {
+                                       ((Method)current_method).is_async_callback = true;
+                               }
+                               break;
+                       case "org.freedesktop.DBus.GLib.NoReply":
+                               if (current_node is Method) {
+                                       current_node.set_attribute_bool ("DBus", "no_reply", true);
+                               }
+                               break;
+                       default:
+                               break;
+               }
+
+               next ();
+
+               end_element ("annotation");
+       }
+
+       private void parse_method () {
+               start_element ("method");
+               string? name = reader.get_attribute ("name");
+
+               SourceReference src = new SourceReference (current_source_file, begin, end);
+
+               if (name == null) {
+                       Report.error (src, "Interface method declarations require a name attribute");
+                       return;
+               }
+
+               DataType return_type = new VoidType ();
+
+               current_node = current_method = new Method (name, return_type, src);
+               current_iface.add_method ((Method)current_method);
+               ((Method)current_method).is_abstract = true;
+               ((Method)current_method).access = SymbolAccessibility.PUBLIC;
+
+               next ();
+
+               parse_method_body ();
+
+               end_element ("method");
+       }
+
+       private void parse_property () {
+
+               start_element ("property");
+
+               Map<string,string> attribs = reader.get_attributes ();
+
+               if (attribs["name"] == null) {
+                       Report.error (get_current_src (), "Interface property declarations require a name 
attribute");
+                       return;
+               }
+
+               if (attribs["type"] == null) {
+                       Report.error (get_current_src (), "Interface property declarations require a type 
attribute");
+                       return;
+               }
+
+               DataType type = dbus_module.get_dbus_type (attribs["type"]);
+
+               PropertyAccessor get_access = null;
+               PropertyAccessor set_access = null;
+
+               if (attribs["access"] == "read" || attribs["access"] == "readwrite") {
+                       get_access = new PropertyAccessor (true, false, false, type, null, get_current_src 
());
+               }
+               if (attribs["access"] == "write" || attribs["access"] == "readwrite") {
+                       set_access = new PropertyAccessor (false, true, false, type, null, get_current_src 
());
+               }
+
+               current_node = current_property = new Property (attribs["name"], type, get_access, 
set_access, get_current_src ());
+               current_property.is_abstract = true;
+               current_iface.add_property (current_property);
+
+               next ();
+
+               while (current_token == MarkupTokenType.START_ELEMENT) {
+                       if (reader.name == "annotation") {
+                               parse_annotation ();
+                       }
+               }
+
+               end_element ("property");
+
+       }
+
+       private void parse_method_body () {
+
+               while (current_token == MarkupTokenType.START_ELEMENT) {
+
+                       switch (reader.name) {
+                               case "annotation":
+                                       parse_annotation ();
+                                       break;
+                               case "arg":
+                                       parse_arg ();
+                                       break;
+                               default:
+                                       parse_extension ();
+                                       break;
+                       }
+               }
+       }
+
+       private void parse_arg () {
+               start_element ("arg");
+
+               Map<string,string> attribs = reader.get_attributes ();
+
+               if (attribs["name"] == null) {
+                       Report.error (get_current_src () , "Formal Parameters require names");
+                       return;
+               }
+
+               var type = dbus_module.get_dbus_type (attribs["type"]);
+               type.value_owned = false;
+
+               current_node = current_param = new Parameter (attribs["name"], type, get_current_src ());
+
+               current_method.add_parameter (current_param);
+
+               if (current_method is Method) {
+                       if (attribs["direction"] != null && attribs["direction"] == "out") {
+                               current_param.direction = ParameterDirection.OUT;
+                               type.value_owned = true;
+                       }
+               }
+
+               next ();
+               end_element ("arg");
+       }
+
+       private void parse_extension () {
+               next ();
+       }
+
+       private void parse_signal () {
+               start_element ("signal");
+
+               string? name = reader.get_attribute ("name");
+
+               if (name == null) {
+                       Report.error (get_current_src (), "Interface signal declarations require a name 
attribute");
+                       return;
+               }
+
+               current_node = current_method = new Signal (name, new VoidType ());
+               current_iface.add_signal ((Signal)current_node);
+               ((Signal)current_node).access = SymbolAccessibility.PUBLIC;
+
+               next ();
+
+               parse_method_body ();
+
+               end_element ("signal");
+       }
+
+       private void next () {
+               current_token = reader.read_token (out begin, out end);
+       }
+
+       private void start_element (string name) {
+
+               if (current_token != MarkupTokenType.START_ELEMENT || reader.name != name) {
+                       // error
+                       Report.error (get_current_src (), "expected start element of `%s'".printf (name));
+               }
+       }
+
+       private void end_element (string name) {
+               while (current_token != MarkupTokenType.END_ELEMENT || reader.name != name) {
+                       Report.warning (get_current_src (), "expected end element of `%s'".printf (name));
+                       skip_element ();
+               }
+               next ();
+       }
+
+       private SourceReference get_current_src () {
+               return new SourceReference (this.current_source_file, begin, end);
+       }
+
+       private void skip_element () {
+               next ();
+
+               int level = 1;
+               while (level > 0) {
+                       if (current_token == MarkupTokenType.START_ELEMENT) {
+                               level++;
+                       } else if (current_token == MarkupTokenType.END_ELEMENT) {
+                               level--;
+                       } else if (current_token == MarkupTokenType.EOF) {
+                               Report.error (get_current_src (), "unexpected end of file");
+                               break;
+                       }
+                       next ();
+               }
+       }
+
+}
diff --git a/dbusgen/valadbusvariantmodule.vala b/dbusgen/valadbusvariantmodule.vala
new file mode 100644
index 000000000..d4f2046d2
--- /dev/null
+++ b/dbusgen/valadbusvariantmodule.vala
@@ -0,0 +1,205 @@
+/* valadbusvariantmodule.vala
+ *
+ * Copyright (C) 2017 Chris Daley
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Chris Daley <chebizarro gmail com>
+ */
+
+public class Vala.DBusVariantModule {
+
+       private CodeContext context;
+
+       public Symbol root_symbol;
+
+       public DataType void_type = new VoidType ();
+       public DataType bool_type;
+       public DataType char_type;
+       public DataType uchar_type;
+       public DataType? unichar_type;
+       public DataType short_type;
+       public DataType ushort_type;
+       public DataType int_type;
+       public DataType uint_type;
+       public DataType long_type;
+       public DataType ulong_type;
+       public DataType int8_type;
+       public DataType uint8_type;
+       public DataType int16_type;
+       public DataType uint16_type;
+       public DataType int32_type;
+       public DataType uint32_type;
+       public DataType int64_type;
+       public DataType uint64_type;
+       public DataType string_type;
+       public DataType regex_type;
+       public DataType float_type;
+       public DataType double_type;
+       public TypeSymbol gtype_type;
+       public TypeSymbol gobject_type;
+       public ErrorType gerror_type;
+       public Class glist_type;
+       public Class gslist_type;
+       public Class gnode_type;
+       public Class gqueue_type;
+       public Class gvaluearray_type;
+       public TypeSymbol gstringbuilder_type;
+       public TypeSymbol garray_type;
+       public TypeSymbol gbytearray_type;
+       public TypeSymbol gptrarray_type;
+       public TypeSymbol gthreadpool_type;
+       public DataType gdestroynotify_type;
+       public DataType gquark_type;
+       public Struct gvalue_type;
+       public Class gvariant_type;
+       public Struct mutex_type;
+       public Struct gmutex_type;
+       public Struct grecmutex_type;
+       public Struct grwlock_type;
+       public Struct gcond_type;
+       public Class gsource_type;
+       public TypeSymbol type_module_type;
+       public TypeSymbol dbus_proxy_type;
+
+       public DBusVariantModule (CodeContext context) {
+
+               this.context = context;
+
+               root_symbol = context.root;
+
+               bool_type = new BooleanType ((Struct) root_symbol.scope.lookup ("bool"));
+               char_type = new IntegerType ((Struct) root_symbol.scope.lookup ("char"));
+               uchar_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uchar"));
+               short_type = new IntegerType ((Struct) root_symbol.scope.lookup ("short"));
+               ushort_type = new IntegerType ((Struct) root_symbol.scope.lookup ("ushort"));
+               int_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int"));
+               uint_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint"));
+               long_type = new IntegerType ((Struct) root_symbol.scope.lookup ("long"));
+               ulong_type = new IntegerType ((Struct) root_symbol.scope.lookup ("ulong"));
+               int8_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int8"));
+               uint8_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint8"));
+               int16_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int16"));
+               uint16_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint16"));
+               int32_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int32"));
+               uint32_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint32"));
+               int64_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int64"));
+               uint64_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint64"));
+               float_type = new FloatingType ((Struct) root_symbol.scope.lookup ("float"));
+               double_type = new FloatingType ((Struct) root_symbol.scope.lookup ("double"));
+               string_type = new ObjectType ((Class) root_symbol.scope.lookup ("string"));
+               var unichar_struct = (Struct) root_symbol.scope.lookup ("unichar");
+               if (unichar_struct != null) {
+                       unichar_type = new IntegerType (unichar_struct);
+               }
+               var glib_ns = root_symbol.scope.lookup ("GLib");
+
+               gtype_type = (TypeSymbol) glib_ns.scope.lookup ("Type");
+               gobject_type = (TypeSymbol) glib_ns.scope.lookup ("Object");
+               gerror_type = new ErrorType (null, null);
+               glist_type = (Class) glib_ns.scope.lookup ("List");
+               gslist_type = (Class) glib_ns.scope.lookup ("SList");
+               gnode_type = (Class) glib_ns.scope.lookup ("Node");
+               gqueue_type = (Class) glib_ns.scope.lookup ("Queue");
+               gvaluearray_type = (Class) glib_ns.scope.lookup ("ValueArray");
+               gstringbuilder_type = (TypeSymbol) glib_ns.scope.lookup ("StringBuilder");
+               garray_type = (TypeSymbol) glib_ns.scope.lookup ("Array");
+               gbytearray_type = (TypeSymbol) glib_ns.scope.lookup ("ByteArray");
+               gptrarray_type = (TypeSymbol) glib_ns.scope.lookup ("PtrArray");
+               gthreadpool_type = (TypeSymbol) glib_ns.scope.lookup ("ThreadPool");
+               gdestroynotify_type = new DelegateType ((Delegate) glib_ns.scope.lookup ("DestroyNotify"));
+
+               gquark_type = new IntegerType ((Struct) glib_ns.scope.lookup ("Quark"));
+               gvalue_type = (Struct) glib_ns.scope.lookup ("Value");
+               gvariant_type = (Class) glib_ns.scope.lookup ("Variant");
+               gsource_type = (Class) glib_ns.scope.lookup ("Source");
+
+               gmutex_type = (Struct) glib_ns.scope.lookup ("Mutex");
+               grecmutex_type = (Struct) glib_ns.scope.lookup ("RecMutex");
+               grwlock_type = (Struct) glib_ns.scope.lookup ("RWLock");
+               gcond_type = (Struct) glib_ns.scope.lookup ("Cond");
+
+               mutex_type = grecmutex_type;
+       }
+
+       public DataType? get_dbus_type (string type) {
+               if (VariantType.string_is_valid (type)) {
+                       VariantType vrnt = new VariantType (type);
+                       return get_variant_type (vrnt);
+
+               } else {
+                       string emessage = "The Variant Type string: %s is invalid".printf (type);
+                       Report.error (null, emessage);
+                       return null;
+               }
+       }
+
+       private DataType get_variant_type (VariantType type) {
+
+               if (type.equal (VariantType.BOOLEAN)) {
+                       return bool_type;
+               } else if (type.equal (VariantType.BYTE)) {
+                       return char_type;
+               } else if (type.equal (VariantType.INT16)) {
+                       return int16_type;
+               } else if (type.equal (VariantType.UINT16)) {
+                       return uint16_type;
+               } else if (type.equal (VariantType.INT32)) {
+                       return int32_type;
+               } else if (type.equal (VariantType.UINT32)) {
+                       return uint32_type;
+               } else if (type.equal (VariantType.INT64)) {
+                       return int64_type;
+               } else if (type.equal (VariantType.UINT64)) {
+                       return uint64_type;
+               } else if (type.equal (VariantType.DOUBLE)) {
+                       return double_type;
+               } else if (type.equal (VariantType.STRING)) {
+                       return string_type.copy ();
+               } else if (type.equal (VariantType.OBJECT_PATH)) {
+                       return string_type;
+               } else if (type.equal (VariantType.SIGNATURE)) {
+                       return string_type;
+               } else if (type.equal (VariantType.VARIANT) || type.equal (VariantType.ANY) || type.equal 
(VariantType.BASIC) || type.equal (VariantType.MAYBE) || type.equal (VariantType.TUPLE)) {
+                       return new ObjectType ((ObjectTypeSymbol) gvariant_type);
+               }
+
+               return new ObjectType ((ObjectTypeSymbol) gvariant_type);
+
+               if (type.equal (VariantType.UNIT)) {
+                       return string_type;
+               } else if (type.equal (VariantType.MAYBE)) {
+                       return string_type;
+               } else if (type.equal (VariantType.OBJECT_PATH_ARRAY) || type.equal (VariantType.ARRAY) || 
type.equal (VariantType.STRING_ARRAY) || type.equal (VariantType.BYTESTRING_ARRAY)) {
+
+                       var element = new ObjectType ((ObjectTypeSymbol) gvariant_type); //get_variant_type 
(type.element ());
+                       return new ArrayType (element, 0, null);
+
+               } else if (type.equal (VariantType.DICT_ENTRY)) {
+                       return string_type;
+               } else if (type.equal (VariantType.DICTIONARY)) {
+                       return string_type;
+               } else if (type.equal (VariantType.BYTESTRING)) {
+                       return string_type;
+               } else if (type.equal (VariantType.VARDICT)) {
+                       return string_type;
+               } else if (type.equal (VariantType.HANDLE)) {
+                       return string_type;
+               }
+
+       }
+
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]