1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/* valaccodearraycreationexpressionbinding.vala
*
* Copyright (C) 2006-2008 Jürg Billeter, Raffaele Sandrini
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
* Raffaele Sandrini <raffaele@sandrini.ch>
*/
using GLib;
using Gee;
/**
* The link between an assignment and generated code.
*/
public class Vala.CCodeArrayCreationExpressionBinding : CCodeExpressionBinding {
public ArrayCreationExpression! array_creation_expression { get; set; }
public CCodeArrayCreationExpressionBinding (construct CodeGenerator! codegen, construct ArrayCreationExpression! array_creation_expression) {
}
public override void emit () {
var expr = array_creation_expression;
expr.accept_children (codegen);
var gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
gnew.add_argument (new CCodeIdentifier (expr.element_type.get_cname ()));
bool first = true;
CCodeExpression cexpr = null;
foreach (Expression size in expr.get_sizes ()) {
CCodeExpression csize = (CCodeExpression) size.ccodenode;
if (!codegen.is_pure_ccode_expression (csize)) {
var temp_var = codegen.get_temp_variable_declarator (codegen.int_type, false, expr);
var name_cnode = new CCodeIdentifier (temp_var.name);
size.ccodenode = name_cnode;
codegen.temp_vars.insert (0, temp_var);
csize = new CCodeParenthesizedExpression (new CCodeAssignment (name_cnode, csize));
}
if (expr.element_type.data_type != null && expr.element_type.data_type.is_reference_type ()) {
// add extra item to have array NULL-terminated for all reference types
csize = new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, csize, new CCodeConstant ("1"));
}
if (first) {
cexpr = csize;
first = false;
} else {
cexpr = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, cexpr, csize);
}
}
gnew.add_argument (cexpr);
if (expr.initializer_list != null) {
// FIXME rank > 1 not supported yet
if (expr.rank > 1) {
expr.error = true;
Report.error (expr.source_reference, "Creating arrays with rank greater than 1 with initializers is not supported yet");
}
var ce = new CCodeCommaExpression ();
var temp_var = codegen.get_temp_variable_declarator (expr.static_type, true, expr);
var name_cnode = new CCodeIdentifier (temp_var.name);
int i = 0;
codegen.temp_vars.insert (0, temp_var);
ce.append_expression (new CCodeAssignment (name_cnode, gnew));
foreach (Expression e in expr.initializer_list.get_initializers ()) {
ce.append_expression (new CCodeAssignment (new CCodeElementAccess (name_cnode, new CCodeConstant (i.to_string ())), (CCodeExpression) e.ccodenode));
i++;
}
ce.append_expression (name_cnode);
codenode = ce;
} else {
codenode = gnew;
}
expr.ccodenode = codenode;
}
}
|