[vala/0.36] codegen: Support renamed signals
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/0.36] codegen: Support renamed signals
- Date: Thu, 5 Oct 2017 20:47:32 +0000 (UTC)
commit e2856e511213a43b3783680281aa7e604ac61594
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Tue Jun 6 17:39:41 2017 +0200
codegen: Support renamed signals
The CCode attribute 'cname' needs to be the canonical representation as it
is expected in C.
https://bugzilla.gnome.org/show_bug.cgi?id=731547
codegen/valaccodeattribute.vala | 24 ++++++++++++++++++---
codegen/valaccodebasemodule.vala | 4 ++-
codegen/valaccodedelegatemodule.vala | 2 +-
codegen/valagdbusclientmodule.vala | 2 +-
codegen/valagdbusmodule.vala | 12 +++++-----
codegen/valagdbusservermodule.vala | 4 +-
codegen/valagirwriter.vala | 2 +-
codegen/valagsignalmodule.vala | 8 +++---
tests/Makefile.am | 1 +
tests/objects/bug731547.vala | 36 ++++++++++++++++++++++++++++++++++
vala/valagirparser.vala | 6 +++++
11 files changed, 81 insertions(+), 20 deletions(-)
---
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index c6c88ba..d3a97d5 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -408,7 +408,12 @@ public class Vala.CCodeAttribute : AttributeCache {
_vfunc_name = ccode.get_string ("vfunc_name");
}
if (_vfunc_name == null) {
- _vfunc_name = sym.name;
+ Method m = node as Method;
+ if (m != null && m.signal_reference != null) {
+ _vfunc_name = CCodeBaseModule.get_ccode_lower_case_name
(m.signal_reference);
+ } else {
+ _vfunc_name = sym.name;
+ }
}
}
return _vfunc_name;
@@ -621,6 +626,9 @@ public class Vala.CCodeAttribute : AttributeCache {
if (m.is_async_callback) {
return "%s_co".printf (CCodeBaseModule.get_ccode_real_name ((Method)
m.parent_symbol));
}
+ if (m.signal_reference != null) {
+ return "%s%s".printf (CCodeBaseModule.get_ccode_lower_case_prefix
(m.parent_symbol), CCodeBaseModule.get_ccode_lower_case_name (m.signal_reference));
+ }
if (sym.name == "main" && sym.parent_symbol.name == null) {
// avoid conflict with generated main function
return "_vala_main";
@@ -639,7 +647,7 @@ public class Vala.CCodeAttribute : AttributeCache {
return "%sset_%s".printf (CCodeBaseModule.get_ccode_lower_case_prefix
(t), acc.prop.name);
}
} else if (sym is Signal) {
- return Symbol.camel_case_to_lower_case (sym.name);
+ return Symbol.camel_case_to_lower_case (sym.name).replace ("_", "-");;
} else if (sym is LocalVariable || sym is Parameter) {
return sym.name;
} else {
@@ -777,6 +785,8 @@ public class Vala.CCodeAttribute : AttributeCache {
csuffix = csuffix.substring (0, csuffix.length - "_class".length) + "class";
}
return csuffix;
+ } else if (sym is Signal) {
+ return CCodeBaseModule.get_ccode_attribute (sym).name.replace ("-", "_");
} else if (sym.name != null) {
return Symbol.camel_case_to_lower_case (sym.name);
}
@@ -1273,12 +1283,18 @@ public class Vala.CCodeAttribute : AttributeCache {
} else if (sym is Method) {
var m = (Method) sym;
if (m.base_method != null || m.base_interface_method != null) {
+ string m_name;
+ if (m.signal_reference != null) {
+ m_name = CCodeBaseModule.get_ccode_lower_case_name
(m.signal_reference);
+ } else {
+ m_name = m.name;
+ }
if (m.base_interface_type != null) {
return "%sreal_%s%s".printf
(CCodeBaseModule.get_ccode_lower_case_prefix (m.parent_symbol),
CCodeBaseModule.get_ccode_lower_case_prefix (m.base_interface_type.data_type),
- m.name);
+ m_name);
} else {
- return "%sreal_%s".printf
(CCodeBaseModule.get_ccode_lower_case_prefix (m.parent_symbol), m.name);
+ return "%sreal_%s".printf
(CCodeBaseModule.get_ccode_lower_case_prefix (m.parent_symbol), m_name);
}
} else {
return name;
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 56c4f6d..b5dc2c2 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -6140,6 +6140,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
if (sym is Delegate) {
return "%s%s%s".printf (get_ccode_lower_case_prefix (sym.parent_symbol),
infix, Symbol.camel_case_to_lower_case (sym.name));
+ } else if (sym is Signal) {
+ return get_ccode_attribute (sym).name.replace ("-", "_");
} else if (sym is ErrorCode) {
return get_ccode_name (sym).down ();
} else {
@@ -6472,7 +6474,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
public CCodeConstant get_signal_canonical_constant (Signal sig, string? detail = null) {
- return new CCodeConstant ("\"%s%s\"".printf (get_ccode_name (sig).replace ("_", "-"), (detail
!= null ? "::%s".printf (detail) : "")));
+ return new CCodeConstant ("\"%s%s\"".printf (get_ccode_name (sig), (detail != null ?
"::%s".printf (detail) : "")));
}
public static CCodeConstant get_enum_value_canonical_cconstant (EnumValue ev) {
diff --git a/codegen/valaccodedelegatemodule.vala b/codegen/valaccodedelegatemodule.vala
index 7150c4e..abb8892 100644
--- a/codegen/valaccodedelegatemodule.vala
+++ b/codegen/valaccodedelegatemodule.vala
@@ -181,7 +181,7 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule {
if (dynamic_sig != null) {
delegate_name = get_dynamic_signal_cname (dynamic_sig);
} else if (sig != null) {
- delegate_name = get_ccode_lower_case_prefix (sig.parent_symbol) + get_ccode_name
(sig);
+ delegate_name = get_ccode_lower_case_prefix (sig.parent_symbol) +
get_ccode_lower_case_name (sig);
} else {
delegate_name = Symbol.camel_case_to_lower_case (get_ccode_name (d));
}
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index d986b24..b03eddd 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -436,7 +436,7 @@ public class Vala.GDBusClientModule : GDBusModule {
}
string generate_dbus_signal_handler (Signal sig, ObjectTypeSymbol sym) {
- string wrapper_name = "_dbus_handle_%s_%s".printf (get_ccode_lower_case_name (sym),
get_ccode_name (sig));
+ string wrapper_name = "_dbus_handle_%s_%s".printf (get_ccode_lower_case_name (sym),
get_ccode_lower_case_name (sig));
var function = new CCodeFunction (wrapper_name);
function.modifiers = CCodeModifiers.STATIC;
diff --git a/codegen/valagdbusmodule.vala b/codegen/valagdbusmodule.vala
index 0a6404a..f8bdaa9 100644
--- a/codegen/valagdbusmodule.vala
+++ b/codegen/valagdbusmodule.vala
@@ -371,31 +371,31 @@ public class Vala.GDBusModule : GVariantModule {
info.append (new CCodeConstant ("\"%s\"".printf (get_type_signature
(param.variable_type, param))));
var cdecl = new CCodeDeclaration ("const GDBusArgInfo");
- cdecl.add_declarator (new CCodeVariableDeclarator ("_" +
get_ccode_lower_case_prefix (sym) + "dbus_arg_info_" + get_ccode_name (sig) + "_" + param.name, info));
+ cdecl.add_declarator (new CCodeVariableDeclarator ("_" +
get_ccode_lower_case_prefix (sym) + "dbus_arg_info_" + get_ccode_lower_case_name (sig) + "_" + param.name,
info));
cdecl.modifiers = CCodeModifiers.STATIC;
cfile.add_constant_declaration (cdecl);
- args_info.append (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF,
new CCodeIdentifier ("_" + get_ccode_lower_case_prefix (sym) + "dbus_arg_info_" + get_ccode_name (sig) + "_"
+ param.name)));
+ args_info.append (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF,
new CCodeIdentifier ("_" + get_ccode_lower_case_prefix (sym) + "dbus_arg_info_" + get_ccode_lower_case_name
(sig) + "_" + param.name)));
}
args_info.append (new CCodeConstant ("NULL"));
var cdecl = new CCodeDeclaration ("const GDBusArgInfo * const");
- cdecl.add_declarator (new CCodeVariableDeclarator ("_" + get_ccode_lower_case_prefix
(sym) + "dbus_arg_info_" + get_ccode_name (sig) + "[]", args_info));
+ cdecl.add_declarator (new CCodeVariableDeclarator ("_" + get_ccode_lower_case_prefix
(sym) + "dbus_arg_info_" + get_ccode_lower_case_name (sig) + "[]", args_info));
cdecl.modifiers = CCodeModifiers.STATIC;
cfile.add_constant_declaration (cdecl);
var info = new CCodeInitializerList ();
info.append (new CCodeConstant ("-1"));
info.append (new CCodeConstant ("\"%s\"".printf (get_dbus_name_for_member (sig))));
- info.append (new CCodeCastExpression (new CCodeUnaryExpression
(CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_" + get_ccode_lower_case_prefix (sym) +
"dbus_arg_info_" + get_ccode_name (sig))), "GDBusArgInfo **"));
+ info.append (new CCodeCastExpression (new CCodeUnaryExpression
(CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_" + get_ccode_lower_case_prefix (sym) +
"dbus_arg_info_" + get_ccode_lower_case_name (sig))), "GDBusArgInfo **"));
cdecl = new CCodeDeclaration ("const GDBusSignalInfo");
- cdecl.add_declarator (new CCodeVariableDeclarator ("_" + get_ccode_lower_case_prefix
(sym) + "dbus_signal_info_" + get_ccode_name (sig), info));
+ cdecl.add_declarator (new CCodeVariableDeclarator ("_" + get_ccode_lower_case_prefix
(sym) + "dbus_signal_info_" + get_ccode_lower_case_name (sig), info));
cdecl.modifiers = CCodeModifiers.STATIC;
cfile.add_constant_declaration (cdecl);
- infos.append (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new
CCodeIdentifier ("_" + get_ccode_lower_case_prefix (sym) + "dbus_signal_info_" + get_ccode_name (sig))));
+ infos.append (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new
CCodeIdentifier ("_" + get_ccode_lower_case_prefix (sym) + "dbus_signal_info_" + get_ccode_lower_case_name
(sig))));
}
infos.append (new CCodeConstant ("NULL"));
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index d0c87fd..68909c0 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -409,7 +409,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
}
string generate_dbus_signal_wrapper (Signal sig, ObjectTypeSymbol sym, string dbus_iface_name) {
- string wrapper_name = "_dbus_%s_%s".printf (get_ccode_lower_case_name (sym), get_ccode_name
(sig));
+ string wrapper_name = "_dbus_%s_%s".printf (get_ccode_lower_case_name (sym),
get_ccode_lower_case_name (sig));
var function = new CCodeFunction (wrapper_name, "void");
function.modifiers = CCodeModifiers.STATIC;
@@ -616,7 +616,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
// disconnect the signals
var disconnect_call = new CCodeFunctionCall (new CCodeIdentifier
("g_signal_handlers_disconnect_by_func"));
disconnect_call.add_argument (new CCodeElementAccess (new CCodeIdentifier
("data"), new CCodeConstant ("0")));
- disconnect_call.add_argument (new CCodeIdentifier ("_dbus_%s_%s".printf
(get_ccode_lower_case_name (sym), get_ccode_name (sig))));
+ disconnect_call.add_argument (new CCodeIdentifier ("_dbus_%s_%s".printf
(get_ccode_lower_case_name (sym), get_ccode_lower_case_name (sig))));
disconnect_call.add_argument (new CCodeIdentifier ("data"));
ccode.add_expression (disconnect_call);
}
diff --git a/codegen/valagirwriter.vala b/codegen/valagirwriter.vala
index 60315d4..68d9a04 100644
--- a/codegen/valagirwriter.vala
+++ b/codegen/valagirwriter.vala
@@ -417,7 +417,7 @@ public class Vala.GIRWriter : CodeVisitor {
foreach (Signal sig in cl.get_signals ()) {
if (sig.default_handler != null) {
write_indent ();
- buffer.append_printf ("<field name=\"%s\">\n", sig.name);
+ buffer.append_printf ("<field name=\"%s\">\n",
CCodeBaseModule.get_ccode_lower_case_name (sig));
indent++;
write_signature (sig.default_handler, "callback", false, true);
indent--;
diff --git a/codegen/valagsignalmodule.vala b/codegen/valagsignalmodule.vala
index a31f35d..306c374 100644
--- a/codegen/valagsignalmodule.vala
+++ b/codegen/valagsignalmodule.vala
@@ -137,7 +137,7 @@ public class Vala.GSignalModule : GObjectModule {
private CCodeExpression get_signal_id_cexpression (Signal sig) {
var cl = (TypeSymbol) sig.parent_symbol;
var signal_array = new CCodeIdentifier ("%s_signals".printf (get_ccode_lower_case_name (cl)));
- var signal_enum_value = new CCodeIdentifier ("%s_%s_SIGNAL".printf (get_ccode_upper_case_name
(cl), sig.name.ascii_up ()));
+ var signal_enum_value = new CCodeIdentifier ("%s_%s_SIGNAL".printf (get_ccode_upper_case_name
(cl), get_ccode_upper_case_name (sig)));
return new CCodeElementAccess (signal_array, signal_enum_value);
}
@@ -181,7 +181,7 @@ public class Vala.GSignalModule : GObjectModule {
}
}
- signal_enum.add_value (new CCodeEnumValue ("%s_%s_SIGNAL".printf (get_ccode_upper_case_name
((TypeSymbol)sig.parent_symbol), sig.name.ascii_up ())));
+ signal_enum.add_value (new CCodeEnumValue ("%s_%s_SIGNAL".printf (get_ccode_upper_case_name
((TypeSymbol)sig.parent_symbol), get_ccode_upper_case_name (sig))));
sig.accept_children (this);
@@ -561,7 +561,7 @@ public class Vala.GSignalModule : GObjectModule {
}
emitter_func = get_ccode_lower_case_name (sig.emitter);
} else {
- emitter_func = "%s_%s".printf (get_ccode_lower_case_name (cl),
sig.name);
+ emitter_func = "%s_%s".printf (get_ccode_lower_case_name (cl),
get_ccode_lower_case_name (sig));
}
var ccall = new CCodeFunctionCall (new CCodeIdentifier (emitter_func));
@@ -662,7 +662,7 @@ public class Vala.GSignalModule : GObjectModule {
// dynamic_signal_connect or dynamic_signal_disconnect
// second argument: signal name
- ccall.add_argument (new CCodeConstant ("\"%s\"".printf (sig.name)));
+ ccall.add_argument (new CCodeConstant ("\"%s\"".printf (get_ccode_name (sig))));
} else if (!disconnect) {
// g_signal_connect_object or g_signal_connect
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ab30d62..99077d0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -217,6 +217,7 @@ TESTS = \
objects/bug701978.vala \
objects/bug702736.vala \
objects/bug702846.vala \
+ objects/bug731547.vala \
objects/bug751338.vala \
objects/bug758816.vala \
objects/bug760031.test \
diff --git a/tests/objects/bug731547.vala b/tests/objects/bug731547.vala
new file mode 100644
index 0000000..a42ab73
--- /dev/null
+++ b/tests/objects/bug731547.vala
@@ -0,0 +1,36 @@
+public class Foo : Object {
+ [CCode (cname = "baz")]
+ public signal void foo ();
+ [CCode (cname = "baz-bar")]
+ public signal void foo_bar ();
+ [CCode (cname = "baz-virt")]
+ public virtual signal void foo_virt () {
+ callback ();
+ }
+}
+
+int baz = 0;
+
+void callback () {
+ baz++;
+}
+
+void main () {
+ var foo = new Foo ();
+ foo.foo.connect (callback);
+ foo.foo_bar.connect (callback);
+
+ foo.foo ();
+ assert (baz == 1);
+ foo.foo_bar ();
+ assert (baz == 2);
+ foo.foo_virt ();
+ assert (baz == 3);
+
+ Signal.emit_by_name (foo, "baz");
+ assert (baz == 4);
+ Signal.emit_by_name (foo, "baz-bar");
+ assert (baz == 5);
+ Signal.emit_by_name (foo, "baz-virt");
+ assert (baz == 6);
+}
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index 68a2b89..faaf4b1 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -3089,6 +3089,12 @@ public class Vala.GirParser : CodeVisitor {
s.comment = comment;
s.external = true;
+ if (s is Signal) {
+ if (current.girdata["name"] != name.replace ("_", "-")) {
+ s.set_attribute_string ("CCode", "cname", current.girdata["name"]);
+ }
+ }
+
if (s is Method) {
var m = (Method) s;
if (symbol_type == "virtual-method" || symbol_type == "callback") {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]