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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/* valaccodeassignmentmodule.vala
*
* Copyright (C) 2006-2010 Jürg Billeter
* Copyright (C) 2006-2008 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;
/**
* The link between an assignment and generated code.
*/
public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
TargetValue emit_simple_assignment (Assignment assignment) {
Variable variable = (Variable) assignment.left.symbol_reference;
if (requires_destroy (assignment.left.value_type)) {
/* unref old value */
ccode.add_expression (destroy_value (assignment.left.target_value));
}
if (assignment.operator == AssignmentOperator.SIMPLE) {
store_value (assignment.left.target_value, assignment.right.target_value);
} else {
CCodeAssignmentOperator cop;
if (assignment.operator == AssignmentOperator.BITWISE_OR) {
cop = CCodeAssignmentOperator.BITWISE_OR;
} else if (assignment.operator == AssignmentOperator.BITWISE_AND) {
cop = CCodeAssignmentOperator.BITWISE_AND;
} else if (assignment.operator == AssignmentOperator.BITWISE_XOR) {
cop = CCodeAssignmentOperator.BITWISE_XOR;
} else if (assignment.operator == AssignmentOperator.ADD) {
cop = CCodeAssignmentOperator.ADD;
} else if (assignment.operator == AssignmentOperator.SUB) {
cop = CCodeAssignmentOperator.SUB;
} else if (assignment.operator == AssignmentOperator.MUL) {
cop = CCodeAssignmentOperator.MUL;
} else if (assignment.operator == AssignmentOperator.DIV) {
cop = CCodeAssignmentOperator.DIV;
} else if (assignment.operator == AssignmentOperator.PERCENT) {
cop = CCodeAssignmentOperator.PERCENT;
} else if (assignment.operator == AssignmentOperator.SHIFT_LEFT) {
cop = CCodeAssignmentOperator.SHIFT_LEFT;
} else if (assignment.operator == AssignmentOperator.SHIFT_RIGHT) {
cop = CCodeAssignmentOperator.SHIFT_RIGHT;
} else {
assert_not_reached ();
}
CCodeExpression codenode = new CCodeAssignment (get_cvalue (assignment.left), get_cvalue (assignment.right), cop);
ccode.add_expression (codenode);
}
if (assignment.left.value_type is ArrayType && (((ArrayType) assignment.left.value_type).inline_allocated)) {
return load_variable (variable, assignment.left.target_value);
} else {
return store_temp_value (assignment.left.target_value, assignment);
}
}
public override void visit_assignment (Assignment assignment) {
if (assignment.left.error || assignment.right.error) {
assignment.error = true;
return;
}
if (assignment.left.symbol_reference is Property) {
var ma = assignment.left as MemberAccess;
var prop = (Property) assignment.left.symbol_reference;
store_property (prop, ma.inner, assignment.right.target_value);
assignment.target_value = assignment.right.target_value;
} else {
assignment.target_value = emit_simple_assignment (assignment);
}
}
public override void store_value (TargetValue lvalue, TargetValue value) {
var array_type = lvalue.value_type as ArrayType;
if (array_type != null && array_type.fixed_length) {
cfile.add_include ("string.h");
// it is necessary to use memcpy for fixed-length (stack-allocated) arrays
// simple assignments do not work in C
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeIdentifier (get_ccode_name (array_type.element_type)));
var size = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeConstant ("%d".printf (array_type.length)), sizeof_call);
var ccopy = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
ccopy.add_argument (get_cvalue_ (lvalue));
ccopy.add_argument (get_cvalue_ (value));
ccopy.add_argument (size);
ccode.add_expression (ccopy);
return;
}
var cexpr = get_cvalue_ (value);
if (get_ctype (lvalue) != null) {
cexpr = new CCodeCastExpression (cexpr, get_ctype (lvalue));
}
ccode.add_assignment (get_cvalue_ (lvalue), cexpr);
if (array_type != null && ((GLibValue) lvalue).array_length_cvalues != null) {
var glib_value = (GLibValue) value;
if (glib_value.array_length_cvalues != null) {
for (int dim = 1; dim <= array_type.rank; dim++) {
ccode.add_assignment (get_array_length_cvalue (lvalue, dim), get_array_length_cvalue (value, dim));
}
} else if (glib_value.array_null_terminated) {
requires_array_length = true;
var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length"));
len_call.add_argument (get_cvalue_ (value));
ccode.add_assignment (get_array_length_cvalue (lvalue, 1), len_call);
} else {
for (int dim = 1; dim <= array_type.rank; dim++) {
ccode.add_assignment (get_array_length_cvalue (lvalue, dim), new CCodeConstant ("-1"));
}
}
if (array_type.rank == 1 && get_array_size_cvalue (lvalue) != null) {
ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_length_cvalue (lvalue, 1));
}
}
var delegate_type = lvalue.value_type as DelegateType;
if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
if (get_delegate_target_cvalue (lvalue) != null) {
ccode.add_assignment (get_delegate_target_cvalue (lvalue), get_delegate_target_cvalue (value));
if (get_delegate_target_destroy_notify_cvalue (lvalue) != null) {
ccode.add_assignment (get_delegate_target_destroy_notify_cvalue (lvalue), get_delegate_target_destroy_notify_cvalue (value));
}
}
}
}
public override void store_local (LocalVariable local, TargetValue value, bool initializer) {
if (!initializer && requires_destroy (local.variable_type)) {
/* unref old value */
ccode.add_expression (destroy_local (local));
}
store_value (get_local_cvalue (local), value);
}
public override void store_parameter (Parameter param, TargetValue value) {
if (requires_destroy (param.variable_type)) {
/* unref old value */
ccode.add_expression (destroy_parameter (param));
}
store_value (get_parameter_cvalue (param), value);
}
public override void store_field (Field field, TargetValue? instance, TargetValue value) {
var lvalue = get_field_cvalue (field, instance);
var type = lvalue.value_type;
if (lvalue.actual_value_type != null) {
type = lvalue.actual_value_type;
}
if (requires_destroy (type)) {
/* unref old value */
ccode.add_expression (destroy_field (field, instance));
}
store_value (lvalue, value);
}
}
|