summaryrefslogtreecommitdiff
path: root/codegen/valagvarianttransformer.vala
blob: d1f337a549edd3002e5761de67495af664a4a60f (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* valagvarianttransformer.vala
 *
 * Copyright (C) 2011  Luca Bruno
 *
 * 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:
 * 	Luca Bruno <lucabru@src.gnome.org>
 */

/**
 * Code visitor for transforming the code tree related to GVariant.
 */
public class Vala.GVariantTransformer : CodeTransformer {
	struct BasicTypeInfo {
		public unowned string signature;
		public unowned string type_name;
		public bool is_string;
	}

	const BasicTypeInfo[] basic_types = {
		{ "y", "byte", false },
		{ "b", "boolean", false },
		{ "n", "int16", false },
		{ "q", "uint16", false },
		{ "i", "int32", false },
		{ "u", "uint32", false },
		{ "x", "int64", false },
		{ "t", "uint64", false },
		{ "d", "double", false },
		{ "s", "string", true },
		{ "o", "object_path", true },
		{ "g", "signature", true }
	};

	CodeBuilder b;

	bool get_basic_type_info (string signature, out BasicTypeInfo basic_type) {
		if (signature != null) {
			foreach (BasicTypeInfo info in basic_types) {
				if (info.signature == signature) {
					basic_type = info;
					return true;
				}
			}
		}
		basic_type = BasicTypeInfo ();
		return false;
	}

	Expression expression (string str) {
		return new Parser().parse_expression_string (str, b.source_reference);
	}

	Expression serialize_basic (BasicTypeInfo basic_type, Expression expr) {
		var new_call = (ObjectCreationExpression) expression (@"new GLib.Variant.$(basic_type.type_name)()");
		new_call.add_argument (expr);
		return new_call;
	}

	public static string? get_dbus_signature (Symbol symbol) {
		return symbol.get_attribute_string ("DBus", "signature");
	}

	static bool is_string_marshalled_enum (TypeSymbol? symbol) {
		if (symbol != null && symbol is Enum) {
			return symbol.get_attribute_bool ("DBus", "use_string_marshalling");
		}
		return false;
	}

	public static string? get_type_signature (DataType datatype, Symbol? symbol = null) {
		if (symbol != null) {
			string sig = get_dbus_signature (symbol);
			if (sig != null) {
				// allow overriding signature in attribute, used for raw GVariants
				return sig;
			}
		}

		var array_type = datatype as ArrayType;

		if (array_type != null) {
			string element_type_signature = get_type_signature (array_type.element_type);

			if (element_type_signature == null) {
				return null;
			}

			return string.nfill (array_type.rank, 'a') + element_type_signature;
		} else if (is_string_marshalled_enum (datatype.data_type)) {
			return "s";
		} else if (datatype.data_type != null) {
			string sig = datatype.data_type.get_attribute_string ("CCode", "type_signature");

			var st = datatype.data_type as Struct;
			var en = datatype.data_type as Enum;
			if (sig == null && st != null) {
				var str = new StringBuilder ();
				str.append_c ('(');
				foreach (Field f in st.get_fields ()) {
					if (f.binding == MemberBinding.INSTANCE) {
						str.append (get_type_signature (f.variable_type, f));
					}
				}
				str.append_c (')');
				sig = str.str;
			} else if (sig == null && en != null) {
				if (en.is_flags) {
					return "u";
				} else {
					return "i";
				}
			}

			var type_args = datatype.get_type_arguments ();
			if (sig != null && "%s" in sig && type_args.size > 0) {
				string element_sig = "";
				foreach (DataType type_arg in type_args) {
					var s = get_type_signature (type_arg);
					if (s != null) {
						element_sig += s;
					}
				}

				sig = sig.printf (element_sig);
			}

			if (sig == null &&
			    (datatype.data_type.get_full_name () == "GLib.UnixInputStream" ||
			     datatype.data_type.get_full_name () == "GLib.UnixOutputStream" ||
			     datatype.data_type.get_full_name () == "GLib.Socket")) {
				return "h";
			}

			return sig;
		} else {
			return null;
		}
	}

	Expression serialize_array (ArrayType array_type, Expression array_expr) {
		string temp = b.add_temp_declaration (array_type, null);

		string[] indices = new string[array_type.rank];
		for (int dim=1; dim <= array_type.rank; dim++) {
			indices[dim-1] = b.add_temp_declaration (null, new IntegerLiteral ("0"));
		}
		return serialize_array_dim (array_type, 1, indices, temp);
	}

	Expression serialize_array_dim (ArrayType array_type, int dim, string[] indices, string array_var) {
		var builderinit = expression (@"new GLib.VariantBuilder (new GLib.VariantType (\"$(get_type_signature (array_type))\"))");

		var builder = b.add_temp_declaration (null, builderinit);

		Expression length = expression (array_var+".length");
		if (array_type.rank > 1) {
			ElementAccess ea = new ElementAccess (length, b.source_reference);
			ea.append_index (new IntegerLiteral ((dim-1).to_string (), b.source_reference));
			length = ea;
		}

		var index = indices[dim-1];
		var forcond = new BinaryExpression (BinaryOperator.LESS_THAN, expression (index), length, b.source_reference);
		var foriter = new PostfixExpression (expression (index), true, b.source_reference);
		b.open_for (null, forcond, foriter);

		Expression element_variant;
		if (dim < array_type.rank) {
			element_variant = serialize_array_dim (array_type, dim + 1, indices, array_var);
		} else {
			var element_expr = new ElementAccess (expression (array_var), b.source_reference);
			for (int i=0; i < dim; i++) {
				element_expr.append_index (expression (indices[i]));
			}
			element_variant = serialize_expression (array_type.element_type, element_expr);
		}

		var builder_add = new MethodCall (expression (builder+".add_value"), b.source_reference);
		builder_add.add_argument (element_variant);
		b.add_expression (builder_add);
		b.close ();

		var builder_end = new MethodCall (expression (builder+".end"), b.source_reference);
		return builder_end;
	}

	Expression? serialize_expression (DataType type, Expression expr) {
		BasicTypeInfo basic_type;
		Expression result = null;
		if (get_basic_type_info (get_type_signature (type), out basic_type)) {
			result = serialize_basic (basic_type, expr);
		} else if (expr.value_type is ArrayType) {
			result = serialize_array ((ArrayType) expr.value_type, expr);
		}
		return result;
	}

	public override void visit_expression (Expression expr) {
		base.visit_expression (expr);

		if (!(context.profile == Profile.GOBJECT && expr.target_type != null && expr.target_type.data_type == context.analyzer.gvariant_type.data_type && !(expr.value_type is NullType) && expr.value_type.data_type != context.analyzer.gvariant_type.data_type)) {
			// no implicit gvariant boxing
			return;
		}

		b = new CodeBuilder (context, expr.parent_statement, expr.source_reference);
		var old_parent_node = expr.parent_node;
		var target_type = expr.target_type.copy ();

		Expression result = serialize_expression (expr.value_type, expr);

		result.target_type = target_type;
		context.analyzer.replaced_nodes.add (expr);
		old_parent_node.replace_expression (expr, result);
		foreach (var node in b.check_nodes) {
			check (node);
		}
		check (result);
	}
}