[Vala] Patch: allow TypeSymbols in emitted C code for some C library macros



[This time with the patch attached]

The attached patch to vala 0.3.5 allows vala to emit naked type symbols
as arguments to ellipses functions.
(The ellipses is really a red-herring, but it is currently the favoured
way to pass non-type-checked arguments to library macros which vala is
wrapping as functions - maybe some work could be done on "void" here, or
"any" [like python] for un-type-checked arguments)

This function is useful when a macro wants the C type passing, generally
for stringifying or casting; I particularly need it to make use of this
Samba4 talloc library macro:

#define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type),
#type)

which is a pretty darn useful macro, wrapped with this vapi:
  [CCode (sentinel="")]
  public void* zero(void* mem_ctx, ...);

NOTE: only one of the fragment for
gobject/valaccodeinvocationexpressionbinding.vala
and
gobject/valaccodegenerator.vala
is needed.

valaccodeinvocationexpressionbinding has the most specific patch only
covering the ellipses case in method invocation generation,
valaccodegenerator has a more general case and triggers quite often and
so for all I know has some broader side effects.

Comments?

Sam

diff --git a/ccode/Makefile.am b/ccode/Makefile.am
index d335e02..7d0b016 100644
--- a/ccode/Makefile.am
+++ b/ccode/Makefile.am
@@ -61,6 +61,7 @@ libvalaccode_la_VALASOURCES = \
        valaccodewhilestatement.vala \
        valaccodewriter.vala \
        valaccodeelementaccess.vala \
+       ccode/valaccodetypesymbol.vala \
        $(NULL)
 
 libvalaccode_la_SOURCES = \
diff --git a/ccode/valaccodetypesymbol.vala b/ccode/valaccodetypesymbol.vala
new file mode 100644
index 0000000..d9c0e1b
--- /dev/null
+++ b/ccode/valaccodetypesymbol.vala
@@ -0,0 +1,39 @@
+/* valaccodetypesymbol.vala
+ *
+ * Copyright (C) 2008 Sam Liddicott
+ *
+ * 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:
+ *     Sam Liddicott <sam liddicott com>
+ */
+
+using GLib;
+
+/**
+ * Represents a type symbol in the C code, which is only any use if it is being
+ * passed to a library C macro instead of a function.
+ */
+public class Vala.CCodeTypeSymbol : CCodeExpression {
+        public string c_type_name { get; construct; }
+
+       public CCodeTypeSymbol(string _c_type_name) {
+               c_type_name = _c_type_name;
+       }
+
+       public override void write (CCodeWriter writer) {
+               writer.write_string (c_type_name);
+       }
+}
diff --git a/gobject/valaccodegenerator.vala b/gobject/valaccodegenerator.vala
index 29ce6c3..049a1eb 100644
--- a/gobject/valaccodegenerator.vala
+++ b/gobject/valaccodegenerator.vala
@@ -2651,6 +2651,10 @@ public class Vala.CCodeGenerator : CodeGenerator {
                if (expr.ccodenode != null && !expr.lvalue) {
                        // memory management, implicit casts, and boxing/unboxing
                        expr.ccodenode = transform_expression ((CCodeExpression) expr.ccodenode, 
expr.value_type, expr.target_type, expr);
+               } else if (expr.value_type == null && expr.ccodenode == null && expr.symbol_reference is 
TypeSymbol) {
+                       /* naked TypeSymbol's emit the underlying C type. The semantic analyser restricts this
+                                to working only on ellipses arguments; useful for some library C macros */
+                       expr.ccodenode = new CCodeTypeSymbol((expr.symbol_reference as 
TypeSymbol).get_cname());
                }
        }
 
diff --git a/gobject/valaccodeinvocationexpressionbinding.vala 
b/gobject/valaccodeinvocationexpressionbinding.vala
index 744216a..54b136e 100644
--- a/gobject/valaccodeinvocationexpressionbinding.vala
+++ b/gobject/valaccodeinvocationexpressionbinding.vala
@@ -309,6 +309,12 @@ public class Vala.CCodeInvocationExpressionBinding : CCodeExpressionBinding {
                                        if (param.ctype != null) {
                                                cexpr = new CCodeCastExpression (cexpr, param.ctype);
                                        }
+                               } else {
+                                       /* Passthrough TypeSymbol for ellipses */
+                                       if (cexpr == null && arg.value_type == null && arg.symbol_reference 
is TypeSymbol) {
+                                               cexpr = new CCodeTypeSymbol((arg.symbol_reference as 
TypeSymbol).get_cname());
+                                               arg.ccodenode = cexpr;
+                                       }
                                }
                                arg_pos = codegen.get_param_pos (param.cparameter_position, ellipsis);
                        } else {
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 1634636..3d2c706 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -2290,7 +2290,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                                        return false;
                                } else if (arg.value_type == null) {
                                        // disallow untyped arguments except for type inference of callbacks
-                                       if (!(arg.symbol_reference is Method)) {
+                                       if (!(arg.symbol_reference is Method) && !(arg.symbol_reference is 
TypeSymbol)) {
                                                expr.error = true;
                                                Report.error (expr.source_reference, "Invalid type for 
argument %d".printf (i + 1));
                                                return false;


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