[vala/wip/profile-posix: 28/28] codegen: Support string comparision (POSIX)
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/profile-posix: 28/28] codegen: Support string comparision (POSIX)
- Date: Fri, 23 Mar 2018 07:32:09 +0000 (UTC)
commit ab4d6e03c782afedf296900cf5cacf6c71e39d30
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Mon Mar 19 17:04:21 2018 +0100
codegen: Support string comparision (POSIX)
codegen/valaccodebasemodule.vala | 55 +++++++++++++++++++++++++++++++++++++-
1 files changed, 54 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 12b0847..c5b8096 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5584,7 +5584,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
|| expr.operator == BinaryOperator.GREATER_THAN
|| expr.operator == BinaryOperator.LESS_THAN_OR_EQUAL
|| expr.operator == BinaryOperator.GREATER_THAN_OR_EQUAL) {
- var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_strcmp0"));
+ CCodeFunctionCall ccall;
+ if (context.profile == Profile.POSIX) {
+ ccall = new CCodeFunctionCall (new CCodeIdentifier
(generate_cmp_wrapper (new CCodeIdentifier ("strcmp"))));
+ } else {
+ ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_strcmp0"));
+ }
ccall.add_argument (cleft);
ccall.add_argument (cright);
cleft = ccall;
@@ -5683,6 +5688,54 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
return array_contains_func;
}
+ string generate_cmp_wrapper (CCodeIdentifier cmpid) {
+ // generate and call NULL-aware cmp function to reduce number
+ // of temporary variables and simplify code
+
+ string cmp0_func = "_%s0".printf (cmpid.name);
+
+ // g_strcmp0 is already NULL-safe
+ if (cmpid.name == "g_strcmp0") {
+ cmp0_func = cmpid.name;
+ } else if (add_wrapper (cmp0_func)) {
+ var cmp0_fun = new CCodeFunction (cmp0_func, "int");
+ cmp0_fun.add_parameter (new CCodeParameter ("s1", "const void *"));
+ cmp0_fun.add_parameter (new CCodeParameter ("s2", "const void *"));
+ cmp0_fun.modifiers = CCodeModifiers.STATIC;
+
+ push_function (cmp0_fun);
+
+ // s1 != s2;
+ var noteq = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new
CCodeIdentifier ("s1"), new CCodeIdentifier ("s2"));
+
+ // if (!s1) return -(s1 != s2);
+ {
+ var cexp = new CCodeUnaryExpression (CCodeUnaryOperator.LOGICAL_NEGATION, new
CCodeIdentifier ("s1"));
+ ccode.open_if (cexp);
+ ccode.add_return (new CCodeUnaryExpression (CCodeUnaryOperator.MINUS, noteq));
+ ccode.close ();
+ }
+ // if (!s2) return s1 != s2;
+ {
+ var cexp = new CCodeUnaryExpression (CCodeUnaryOperator.LOGICAL_NEGATION, new
CCodeIdentifier ("s2"));
+ ccode.open_if (cexp);
+ ccode.add_return (noteq);
+ ccode.close ();
+ }
+ // return strcmp (s1, s2);
+ var cmp_call = new CCodeFunctionCall (cmpid);
+ cmp_call.add_argument (new CCodeIdentifier ("s1"));
+ cmp_call.add_argument (new CCodeIdentifier ("s2"));
+ ccode.add_return (cmp_call);
+
+ pop_function ();
+
+ cfile.add_function (cmp0_fun);
+ }
+
+ return cmp0_func;
+ }
+
public override void visit_type_check (TypeCheck expr) {
generate_type_declaration (expr.type_reference, cfile);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]