summaryrefslogtreecommitdiff
path: root/codegen/valaccodebasemodule.vala
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2020-03-18 14:05:13 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2020-03-18 14:05:13 +0100
commitb95a766454eb914f4eb5a68e9040e03faffabf52 (patch)
treef8f34ebf108a5b7f7c5b09ad9bcab0a2eaa9fe2e /codegen/valaccodebasemodule.vala
parent5a97fa5a1b56e3a942f01d21efc203da25187d03 (diff)
downloadvala-b95a766454eb914f4eb5a68e9040e03faffabf52.tar.gz
codegen: Use memset to initialize inline-allocated array with non-constant size
In addition to a0bb129e5a2e8580eb272d9a68ba054e7b170dba Fixes https://gitlab.gnome.org/GNOME/vala/issues/910
Diffstat (limited to 'codegen/valaccodebasemodule.vala')
-rw-r--r--codegen/valaccodebasemodule.vala50
1 files changed, 40 insertions, 10 deletions
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 27b0ba101..a21b8f315 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1453,6 +1453,19 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
return (null != cparenthesized && is_constant_ccode_expression (cparenthesized.inner));
}
+ public static bool is_constant_ccode (CodeNode expr) {
+ if (expr is Constant) {
+ // Local constants are not considered constant in C
+ return !(((Constant) expr).parent_symbol is Block);
+ } else if (expr is MemberAccess) {
+ return is_constant_ccode (((MemberAccess) expr).symbol_reference);
+ } else if (expr is CastExpression) {
+ return is_constant_ccode (((CastExpression) expr).inner);
+ }
+
+ return false;
+ }
+
/**
* Returns whether the passed cexpr is a pure expression, i.e. an
* expression without side-effects.
@@ -2523,9 +2536,17 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
// try to initialize uninitialized variables
// initialization not necessary for variables stored in closure
- if (is_init_allowed (local.variable_type)) {
+ CCodeExpression? size = null;
+ if (!requires_memset_init (local, out size)) {
cvar.initializer = default_value_for_type (local.variable_type, true);
cvar.init0 = true;
+ } else if (size != null && local.initializer == null) {
+ cfile.add_include ("string.h");
+ var memset_call = new CCodeFunctionCall (new CCodeIdentifier ("memset"));
+ memset_call.add_argument (get_variable_cexpression (local.name));
+ memset_call.add_argument (new CCodeConstant ("0"));
+ memset_call.add_argument (size);
+ ccode.add_expression (memset_call);
}
ccode.add_declaration (get_ccode_name (local.variable_type), cvar);
@@ -3786,9 +3807,17 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
} else {
var cvar = new CCodeVariableDeclarator (local.name, null, get_ccode_declarator_suffix (local.variable_type));
- if (init && is_init_allowed (local.variable_type)) {
+ CCodeExpression? size = null;
+ if (init && !requires_memset_init (local, out size)) {
cvar.initializer = default_value_for_type (local.variable_type, true, on_error);
cvar.init0 = true;
+ } else if (init && size != null && local.initializer == null) {
+ cfile.add_include ("string.h");
+ var memset_call = new CCodeFunctionCall (new CCodeIdentifier ("memset"));
+ memset_call.add_argument (get_variable_cexpression (local.name));
+ memset_call.add_argument (new CCodeConstant ("0"));
+ memset_call.add_argument (size);
+ ccode.add_expression (memset_call);
}
ccode.add_declaration (get_ccode_name (local.variable_type), cvar);
}
@@ -6249,15 +6278,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
return true;
}
- public bool is_init_allowed (DataType type) {
- unowned ArrayType? array_type = type as ArrayType;
- if (array_type != null && array_type.inline_allocated
- && array_type.fixed_length) {
- unowned Constant? c = array_type.length.symbol_reference as Constant;
- // our local constants are not actual constants in C
- return (c == null || !(c.parent_symbol is Block));
+ public bool requires_memset_init (Variable variable, out CCodeExpression? size) {
+ unowned ArrayType? array_type = variable.variable_type as ArrayType;
+ if (array_type != null && array_type.fixed_length && !is_constant_ccode (array_type.length)) {
+ var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+ sizeof_call.add_argument (new CCodeIdentifier (get_ccode_name (array_type.element_type)));
+ size = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, get_ccodenode (array_type.length), sizeof_call);
+ return true;
}
- return true;
+ size = null;
+ return false;
}
public CCodeDeclaratorSuffix? get_ccode_declarator_suffix (DataType type) {