summaryrefslogtreecommitdiff
path: root/codegen/valaccodetransformer.vala
blob: 8504c489b46eeb89f48c5a3c293751c1ad9a8091 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* valaccodetransformer.vala
 *
 * Copyright (C) 2012  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 simplyfing the code tree for the C codegen.
 */
public class Vala.CCodeTransformer : CodeTransformer {
	public override void visit_source_file (SourceFile source_file) {
		source_file.accept_children (this);
	}

	public override void visit_class (Class cl) {
		cl.accept_children (this);
	}

	public override void visit_struct (Struct st) {
		st.accept_children (this);
	}

	public override void visit_interface (Interface iface) {
		iface.accept_children (this);
	}

	public override void visit_enum (Enum en) {
		en.accept_children (this);
	}

	public override void visit_enum_value (EnumValue ev) {
		ev.accept_children (this);
	}

	public override void visit_error_domain (ErrorDomain edomain) {
		edomain.accept_children (this);
	}

	public override void visit_error_code (ErrorCode ecode) {
		ecode.accept_children (this);
	}

	public override void visit_delegate (Delegate d) {
		d.accept_children (this);
	}

	public override void visit_constant (Constant c) {
		c.active = true;
		c.accept_children (this);
	}

	public override void visit_field (Field f) {
		f.accept_children (this);
	}

	public override void visit_method (Method m) {
		if (m.body == null) {
			return;
		}

		m.accept_children (this);
	}

	public override void visit_creation_method (CreationMethod m) {
		m.accept_children (this);
	}

	public override void visit_formal_parameter (Parameter p) {
		p.accept_children (this);
	}

	public override void visit_property (Property prop) {
		prop.accept_children (this);
	}

	public override void visit_property_accessor (PropertyAccessor acc) {
		acc.accept_children (this);
	}

	public override void visit_signal (Signal sig) {
		sig.accept_children (this);
	}

	public override void visit_constructor (Constructor c) {
		c.accept_children (this);
	}

	public override void visit_destructor (Destructor d) {
		d.accept_children (this);
	}

	public override void visit_block (Block b) {
		b.accept_children (this);

		foreach (LocalVariable local in b.get_local_variables ()) {
			local.active = false;
		}
		foreach (Constant constant in b.get_local_constants ()) {
			constant.active = false;
		}
	}

	public override void visit_declaration_statement (DeclarationStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_local_variable (LocalVariable local) {
		local.active = true;
		local.accept_children (this);
	}

	public override void visit_initializer_list (InitializerList list) {
		list.accept_children (this);
	}

	public override void visit_expression_statement (ExpressionStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_if_statement (IfStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_switch_statement (SwitchStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_switch_section (SwitchSection section) {
		section.accept_children (this);
	}

	public override void visit_switch_label (SwitchLabel label) {
		label.accept_children (this);
	}

	bool always_true (Expression condition) {
		var literal = condition as BooleanLiteral;
		return (literal != null && literal.value);
	}

	bool always_false (Expression condition) {
		var literal = condition as BooleanLiteral;
		return (literal != null && !literal.value);
	}

	public override void visit_loop (Loop loop) {
		loop.accept_children (this);
	}

	public override void visit_while_statement (WhileStatement stmt) {
		// convert to simple loop
		push_builder (new CodeBuilder (context, stmt, stmt.source_reference));
		Expression cond = null;
		if (!always_false (stmt.condition)) {
			b.open_loop ();
			if (!always_true (stmt.condition)) {
				cond = expression (@"!$(stmt.condition)");
				b.open_if (cond);
				b.add_break ();
				b.close ();
			}
			b.add_statement (stmt.body);
			b.close ();
		}

		var parent_block = context.analyzer.get_current_block (stmt);
		context.analyzer.replaced_nodes.add (stmt);
		parent_block.replace_statement (stmt, new EmptyStatement (stmt.source_reference));

		stmt.body.checked = false;
		b.check (this);
		pop_builder ();
	}

	public override void visit_do_statement (DoStatement stmt) {
		// convert to simple loop
		push_builder (new CodeBuilder (context, stmt, stmt.source_reference));

		b.open_loop ();
		// do not generate variable and if block if condition is always true
		if (!always_true (stmt.condition)) {
			var notfirst = b.add_temp_declaration (null, expression ("false"));
			b.open_if (expression (notfirst));
			b.open_if (new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, stmt.condition, stmt.source_reference));
			b.add_break ();
			b.close ();
			b.add_else ();
			b.add_assignment (expression (notfirst), expression ("true"));
			b.close ();
		}
		b.add_statement (stmt.body);
		b.close ();

		var parent_block = context.analyzer.get_current_block (stmt);
		context.analyzer.replaced_nodes.add (stmt);
		parent_block.replace_statement (stmt, new EmptyStatement (stmt.source_reference));

		stmt.body.checked = false;
		b.check (this);
		pop_builder ();
	}

	public override void visit_for_statement (ForStatement stmt) {
		// convert to simple loop
		push_builder (new CodeBuilder (context, stmt, stmt.source_reference));

		// initializer
		foreach (var init_expr in stmt.get_initializer ()) {
			b.add_expression (init_expr);
		}

		if (stmt.condition == null || !always_false (stmt.condition)) {
			b.open_loop ();
			var notfirst = b.add_temp_declaration (null, expression ("false"));
			b.open_if (expression (notfirst));
			foreach (var it_expr in stmt.get_iterator ()) {
				b.add_expression (it_expr);
			}
			b.add_else ();
			b.add_assignment (expression (notfirst), expression ("true"));
			b.close ();

			if (stmt.condition != null && !always_true (stmt.condition)) {
				b.open_if (new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, stmt.condition, stmt.source_reference));
				b.add_break ();
				b.close ();
			}
			b.add_statement (stmt.body);

			b.close ();
		}

		var parent_block = context.analyzer.get_current_block (stmt);
		context.analyzer.replaced_nodes.add (stmt);
		parent_block.replace_statement (stmt, new EmptyStatement (stmt.source_reference));

		stmt.body.checked = false;
		b.check (this);
		pop_builder ();
	}

	public override void visit_foreach_statement (ForeachStatement stmt) {
		push_builder (new CodeBuilder (context, stmt, stmt.source_reference));
		var collection = b.add_temp_declaration (stmt.collection.value_type, stmt.collection);

		stmt.body.remove_local_variable (stmt.element_variable);
		stmt.element_variable.checked = false;
		var decl = new DeclarationStatement (stmt.element_variable, stmt.element_variable.source_reference);

		switch (stmt.foreach_iteration) {
		case ForeachIteration.ARRAY:
		case ForeachIteration.GVALUE_ARRAY:
			// array or GValueArray
			var array = collection;
			if (stmt.foreach_iteration == ForeachIteration.GVALUE_ARRAY) {
				array = collection+".values";
			}
			var i = b.add_temp_declaration (null, expression ("0"));
			b.open_for (null, expression (@"$i < $array.length"), expression (@"$i++"));
			stmt.element_variable.initializer = expression (@"$array[$i]");
			break;
		case ForeachIteration.GLIST:
			// GList or GSList
			var iter_type = stmt.collection.value_type.copy ();
			iter_type.value_owned = false;
			var iter = b.add_temp_declaration (iter_type, expression (collection));
			b.open_for (null, expression (@"$iter != null"), expression (@"$iter = $iter.next"));
			stmt.element_variable.initializer = expression (@"$iter.data");
			break;
		case ForeachIteration.INDEX:
			// get()+size
			var size = b.add_temp_declaration (null, expression (@"$collection.size"));
			var i = b.add_temp_declaration (null, expression ("0"));
			b.open_for (null, expression (@"$i < $size"), expression (@"$i++"));
			stmt.element_variable.initializer = expression (@"$collection.get ($i)");
			break;
		case ForeachIteration.NEXT_VALUE:
			// iterator+next_value()
			var iterator = b.add_temp_declaration (null, expression (@"$collection.iterator ()"));
			var temp = b.add_temp_declaration (stmt.type_reference);
			b.open_while (expression (@"($temp = $iterator.next_value ()) != null"));
			stmt.element_variable.initializer = expression (temp);
			break;
		case ForeachIteration.NEXT_GET:
			// iterator+next()+get()
			var iterator = b.add_temp_declaration (null, expression (@"$collection.iterator ()"));
			b.open_while (expression (@"$iterator.next ()"));
			stmt.element_variable.initializer = expression (@"$iterator.get ()");
			break;
		default:
			assert_not_reached ();
		}

		stmt.body.insert_statement (0, decl);
		b.add_statement (stmt.body);
		b.close ();

		var parent_block = context.analyzer.get_current_block (stmt);
		context.analyzer.replaced_nodes.add (stmt);
		parent_block.replace_statement (stmt, new EmptyStatement (stmt.source_reference));

		stmt.body.checked = false;
		b.check (this);
		pop_builder ();
	}

	public override void visit_break_statement (BreakStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_continue_statement (ContinueStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_return_statement (ReturnStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_yield_statement (YieldStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_throw_statement (ThrowStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_try_statement (TryStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_catch_clause (CatchClause clause) {
		clause.accept_children (this);
	}

	public override void visit_lock_statement (LockStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_unlock_statement (UnlockStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_delete_statement (DeleteStatement stmt) {
		stmt.accept_children (this);
	}

	public override void visit_expression (Expression expr) {
		expr.accept_children (this);
	}

	public override void visit_method_call (MethodCall expr) {
		if (expr.tree_can_fail) {
			if (expr.parent_node is LocalVariable || expr.parent_node is ExpressionStatement) {
				// simple statements, no side effects after method call
			} else if (!(context.analyzer.get_current_non_local_symbol (expr) is Block)) {
				// can't handle errors in field initializers
				Report.error (expr.source_reference, "Field initializers must not throw errors");
			} else {
				// store parent_node as we need to replace the expression in the old parent node later on
				var old_parent_node = expr.parent_node;
				var formal_target_type = copy_type (expr.target_type);
				var target_type = copy_type (expr.target_type);
				push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));

				var local = b.add_temp_declaration (copy_type (expr.value_type), expr);
				var replacement = return_temp_access (local, expr.value_type, target_type, formal_target_type);

				context.analyzer.replaced_nodes.add (expr);
				old_parent_node.replace_expression (expr, replacement);
				b.check (this);
				pop_builder ();
				check (replacement);
			}
		}
	}

	public override void visit_conditional_expression (ConditionalExpression expr) {
		// convert to if statement
		Expression replacement = null;
		var old_parent_node = expr.parent_node;
		var formal_target_type = copy_type (expr.target_type);
		var target_type = copy_type (expr.target_type);
		push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));

		var result = b.add_temp_declaration (expr.value_type);
		b.open_if (expr.condition);
		b.add_assignment (expression (result), expr.true_expression);
		b.add_else ();
		b.add_assignment (expression (result), expr.false_expression);
		b.close ();

		replacement = return_temp_access (result, expr.value_type, target_type, formal_target_type);
		context.analyzer.replaced_nodes.add (expr);
		old_parent_node.replace_expression (expr, replacement);
		b.check (this);
		pop_builder ();
		check (replacement);
	}

	public override void visit_binary_expression (BinaryExpression expr) {
		var parent_statement = expr.parent_statement;
		if (parent_statement == null) {
			base.visit_binary_expression (expr);
			return;
		}

		Expression replacement = null;
		var old_parent_node = expr.parent_node;
		var target_type = copy_type (expr.target_type);
		push_builder (new CodeBuilder (context, parent_statement, expr.source_reference));

		if (context.analyzer.get_current_non_local_symbol (expr) is Block
		    && (expr.operator == BinaryOperator.AND || expr.operator == BinaryOperator.OR)) {
			var is_and = expr.operator == BinaryOperator.AND;
			var result = b.add_temp_declaration (data_type ("bool"));
			b.open_if (expr.left);
			if (is_and) {
				b.add_assignment (expression (result), expr.right);
			} else {
				b.add_expression (expression (@"$result = true"));
			}
			b.add_else ();
			if (is_and) {
				b.add_expression (expression (@"$result = false"));
			} else {
				b.add_assignment (expression (result), expr.right);
			}
			b.close ();
			replacement = expression (result);
		} else if (expr.operator == BinaryOperator.COALESCE) {
			var result = b.add_temp_declaration (copy_type (expr.value_type), expr.left);

			b.open_if (expression (@"$result == null"));
			b.add_assignment (expression (result), expr.right);
			b.close ();

			replacement = return_temp_access (result, expr.value_type, target_type);
		} else if (expr.operator == BinaryOperator.IN && !(expr.left.value_type.compatible (context.analyzer.int_type) && expr.right.value_type.compatible (context.analyzer.int_type)) && !(expr.right.value_type is ArrayType)) {
			// neither enums nor array, it's contains()
			var call = new MethodCall (new MemberAccess (expr.right, "contains", expr.source_reference), expr.source_reference);
			call.add_argument (expr.left);
			replacement = call;
		}

		if (replacement != null) {
			replacement.target_type = target_type;
			context.analyzer.replaced_nodes.add (expr);
			old_parent_node.replace_expression (expr, replacement);
			b.check (this);
			pop_builder ();
			check (replacement);
		} else {
			pop_builder ();
			base.visit_binary_expression (expr);
		}
	}

	public override void visit_unary_expression (UnaryExpression expr) {
		var parent_statement = expr.parent_statement;
		if (parent_statement == null) {
			base.visit_unary_expression (expr);
			return;
		}

		if (expr.operator == UnaryOperator.INCREMENT || expr.operator == UnaryOperator.DECREMENT) {
			var old_parent_node = expr.parent_node;
			var target_type = expr.target_type != null ? expr.target_type.copy () : null;

			push_builder (new CodeBuilder (context, parent_statement, expr.source_reference));
			Expression replacement;
			if (expr.operator == UnaryOperator.INCREMENT) {
				replacement = expression (@"$(expr.inner) = $(expr.inner) + 1");
			} else {
				replacement = expression (@"$(expr.inner) = $(expr.inner) - 1");
			}
			replacement.target_type = target_type;
			context.analyzer.replaced_nodes.add (expr);
			old_parent_node.replace_expression (expr, replacement);
			b.check (this);

			pop_builder ();
			check (replacement);
			return;
		}

		base.visit_unary_expression (expr);
	}

	public override void visit_object_creation_expression (ObjectCreationExpression expr) {
		if (expr.tree_can_fail) {
			if (expr.parent_node is LocalVariable || expr.parent_node is ExpressionStatement) {
				// simple statements, no side effects after method call
			} else if (!(context.analyzer.get_current_non_local_symbol (expr) is Block)) {
				// can't handle errors in field initializers
				Report.error (expr.source_reference, "Field initializers must not throw errors");
			} else {
				var old_parent_node = expr.parent_node;
				var target_type = copy_type (expr.target_type);
				var formal_target_type = copy_type (expr.formal_target_type);
				push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));

				var local = b.add_temp_declaration (expr.value_type, expr);
				var replacement = return_temp_access (local, expr.value_type, target_type, formal_target_type);

				context.analyzer.replaced_nodes.add (expr);
				old_parent_node.replace_expression (expr, replacement);
				b.check (this);
				pop_builder ();
				check (replacement);
			}
		}
	}

	Expression stringify (Expression expr) {
		if (expr.value_type.data_type != null && expr.value_type.data_type.is_subtype_of (context.analyzer.string_type.data_type)) {
			return expr;
		} else {
			return expression (@"$expr.to_string ()");
		}
	}

	public override void visit_template (Template expr) {
		push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));

		Expression replacement;

		var expression_list = expr.get_expressions ();
		if (expression_list.size == 0) {
			replacement = expression ("\"\"");
		} else {
			replacement = stringify (expression_list[0]);
			if (expression_list.size > 1) {
				var concat = (MethodCall) expression (@"$replacement.concat()");
				for (int i = 1; i < expression_list.size; i++) {
					concat.add_argument (stringify (expression_list[i]));
				}
				replacement = concat;
			}
		}
		replacement.target_type = expr.target_type;

		context.analyzer.replaced_nodes.add (expr);
		expr.parent_node.replace_expression (expr, replacement);
		b.check (this);
		pop_builder ();
		check (replacement);
	}

	public override void visit_postfix_expression (PostfixExpression expr) {
		push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));

		var result = b.add_temp_declaration (copy_type (expr.value_type), expr.inner);
		var op = expr.increment ? "+ 1" : "- 1";
		b.add_expression (expression (@"$(expr.inner) = $result $op"));

		var replacement = return_temp_access (result, expr.value_type, expr.target_type);

		context.analyzer.replaced_nodes.add (expr);
		expr.parent_node.replace_expression (expr, replacement);
		b.check (this);
		pop_builder ();
		check (replacement);
	}

	public override void visit_assignment (Assignment a) {
		a.accept_children (this);
	}
}