summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2018-03-19 17:04:21 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2018-03-23 08:31:14 +0100
commitab4d6e03c782afedf296900cf5cacf6c71e39d30 (patch)
tree71e50bd1b902fe29f3e73ceb5300722a3b812a41
parent8c9f9fdfbd2ea5db1c5de48504ca99791732a302 (diff)
downloadvala-wip/profile-posix.tar.gz
codegen: Support string comparision (POSIX)wip/profile-posix
-rw-r--r--codegen/valaccodebasemodule.vala55
1 files changed, 54 insertions, 1 deletions
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 12b084757..c5b809616 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);