[vala/0.48] codegen: Use CCodeDeclaratorSuffix to emit array length of constants
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/0.48] codegen: Use CCodeDeclaratorSuffix to emit array length of constants
- Date: Sun, 28 Feb 2021 17:26:26 +0000 (UTC)
commit 10c85d227922c05b435f5695072856cb6536689d
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Mon Feb 1 14:54:55 2021 +0100
codegen: Use CCodeDeclaratorSuffix to emit array length of constants
ccode/valaccodevariabledeclarator.vala | 24 +++++++++++----
codegen/valaccodebasemodule.vala | 53 ++++++++++++++++------------------
2 files changed, 43 insertions(+), 34 deletions(-)
---
diff --git a/ccode/valaccodevariabledeclarator.vala b/ccode/valaccodevariabledeclarator.vala
index 17fa478a3..b1918b110 100644
--- a/ccode/valaccodevariabledeclarator.vala
+++ b/ccode/valaccodevariabledeclarator.vala
@@ -102,20 +102,32 @@ public class Vala.CCodeVariableDeclarator : CCodeDeclarator {
public class Vala.CCodeDeclaratorSuffix {
bool array;
- CCodeExpression? array_length;
+ List<CCodeExpression>? array_length;
public CCodeDeclaratorSuffix.with_array (CCodeExpression? array_length = null) {
+ if (array_length != null) {
+ this.array_length = new ArrayList<CCodeExpression> ();
+ this.array_length.add (array_length);
+ }
+ array = true;
+ }
+
+ public CCodeDeclaratorSuffix.with_multi_array (List<CCodeExpression>? array_length = null) {
this.array_length = array_length;
array = true;
}
public void write (CCodeWriter writer) {
- if (array) {
- writer.write_string ("[");
- if (array_length != null) {
- array_length.write (writer);
+ if (array_length != null && array_length.size > 0) {
+ foreach (var length in array_length) {
+ writer.write_string ("[");
+ if (length != null) {
+ length.write (writer);
+ }
+ writer.write_string ("]");
}
- writer.write_string ("]");
+ } else if (array) {
+ writer.write_string ("[]");
}
}
}
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index e08556b90..f92764e35 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -938,7 +938,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
}
- private void constant_array_ranks_sizes (InitializerList initializer_list, int[] sizes, int rank = 0)
{
+ static void constant_array_ranks_sizes (InitializerList initializer_list, int[] sizes, int rank = 0) {
sizes[rank] = int.max (sizes[rank], initializer_list.size);
rank++;
foreach (var expr in initializer_list.get_initializers()) {
@@ -948,6 +948,25 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
}
+ CCodeDeclaratorSuffix? get_constant_declarator_suffix (Constant c) {
+ unowned ArrayType? array = c.type_reference as ArrayType;
+ unowned InitializerList? initializer_list = c.value as InitializerList;
+ if (array == null || initializer_list == null) {
+ if (c.type_reference.compatible (string_type)) {
+ return new CCodeDeclaratorSuffix.with_array ();
+ }
+ return null;
+ }
+
+ var lengths = new ArrayList<CCodeExpression> ();
+ int[] sizes = new int[array.rank];
+ constant_array_ranks_sizes (initializer_list, sizes);
+ for (int i = 0; i < array.rank; i++) {
+ lengths.add (new CCodeConstant ("%d".printf (sizes[i])));
+ }
+ return new CCodeDeclaratorSuffix.with_multi_array (lengths);
+ }
+
public void generate_constant_declaration (Constant c, CCodeFile decl_space, bool definition = false)
{
if (c.parent_symbol is Block) {
// local constant
@@ -966,16 +985,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
var initializer_list = c.value as InitializerList;
if (initializer_list != null) {
var cdecl = new CCodeDeclaration (get_ccode_const_name (c.type_reference));
- var arr = "";
- if (c.type_reference is ArrayType) {
- var array = (ArrayType) c.type_reference;
- int[] sizes = new int[array.rank];
- constant_array_ranks_sizes (initializer_list, sizes);
- for (int i = 0; i < array.rank; i++) {
- arr += "[%d]".printf (sizes[i]);
- }
- }
-
var cinitializer = get_cvalue (c.value);
if (!definition) {
// never output value in header
@@ -983,7 +992,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
cinitializer = null;
}
- cdecl.add_declarator (new CCodeVariableDeclarator ("%s%s".printf
(get_ccode_name (c), arr), cinitializer));
+ cdecl.add_declarator (new CCodeVariableDeclarator (get_ccode_name (c),
cinitializer, get_constant_declarator_suffix (c)));
if (c.is_private_symbol ()) {
cdecl.modifiers = CCodeModifiers.STATIC;
} else {
@@ -1014,28 +1023,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
c.value.emit (this);
- string type_name = get_ccode_const_name (c.type_reference);
- string arr = "";
- if (c.type_reference is ArrayType) {
- var array = (ArrayType) c.type_reference;
- var initializer_list = c.value as InitializerList;
- if (initializer_list != null) {
- int[] sizes = new int[array.rank];
- constant_array_ranks_sizes (initializer_list, sizes);
- for (int i = 0; i < array.rank; i++) {
- arr += "[%d]".printf (sizes[i]);
- }
- }
- }
-
+ string type_name;
if (c.type_reference.compatible (string_type)) {
type_name = "const char";
- arr = "[]";
+ } else {
+ type_name = get_ccode_const_name (c.type_reference);
}
var cinitializer = get_cvalue (c.value);
- ccode.add_declaration (type_name, new CCodeVariableDeclarator ("%s%s".printf
(get_ccode_name (c), arr), cinitializer), CCodeModifiers.STATIC);
+ ccode.add_declaration (type_name, new CCodeVariableDeclarator (get_ccode_name (c),
cinitializer, get_constant_declarator_suffix (c)), CCodeModifiers.STATIC);
} else {
generate_constant_declaration (c, cfile, true);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]