File: valaccodeunaryexpression.vala

package info (click to toggle)
vala 0.42.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 41,584 kB
  • sloc: ansic: 557,663; sh: 4,808; xml: 3,135; makefile: 2,864; yacc: 1,218; lex: 374; perl: 106
file content (96 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (3)
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
/* valaccodeunaryexpression.vala
 *
 * Copyright (C) 2006-2009  Jürg Billeter
 *
 * 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>
 */

using GLib;

/**
 * Represents an expression with one operand in the C code.
 */
public class Vala.CCodeUnaryExpression : CCodeExpression {
	/**
	 * The unary operator.
	 */
	public CCodeUnaryOperator operator { get; set; }

	/**
	 * The operand.
	 */
	public CCodeExpression inner { get; set; }

	public CCodeUnaryExpression (CCodeUnaryOperator op, CCodeExpression expr) {
		operator = op;
		inner = expr;
	}

	public override void write (CCodeWriter writer) {
		switch (operator) {
		case CCodeUnaryOperator.PLUS: writer.write_string ("+"); inner.write_inner (writer); break;
		case CCodeUnaryOperator.MINUS: writer.write_string ("-"); inner.write_inner (writer); break;
		case CCodeUnaryOperator.LOGICAL_NEGATION: writer.write_string ("!"); inner.write_inner (writer); break;
		case CCodeUnaryOperator.BITWISE_COMPLEMENT: writer.write_string ("~"); inner.write_inner (writer); break;
		case CCodeUnaryOperator.POINTER_INDIRECTION:
			var inner_unary = inner as CCodeUnaryExpression;
			if (inner_unary != null && inner_unary.operator == CCodeUnaryOperator.ADDRESS_OF) {
				// simplify expression
				inner_unary.inner.write (writer);
				return;
			}
			writer.write_string ("*");
			inner.write_inner (writer);
			break;
		case CCodeUnaryOperator.ADDRESS_OF:
			var inner_unary = inner as CCodeUnaryExpression;
			if (inner_unary != null && inner_unary.operator == CCodeUnaryOperator.POINTER_INDIRECTION) {
				// simplify expression
				inner_unary.inner.write (writer);
				return;
			}
			writer.write_string ("&");
			inner.write_inner (writer);
			break;
		case CCodeUnaryOperator.PREFIX_INCREMENT: writer.write_string ("++"); break;
		case CCodeUnaryOperator.PREFIX_DECREMENT: writer.write_string ("--"); break;
		case CCodeUnaryOperator.POSTFIX_INCREMENT: inner.write_inner (writer); writer.write_string ("++"); break;
		case CCodeUnaryOperator.POSTFIX_DECREMENT: inner.write_inner (writer); writer.write_string ("--"); break;
		default: assert_not_reached ();
		}
	}

	public override void write_inner (CCodeWriter writer) {
		writer.write_string ("(");
		this.write (writer);
		writer.write_string (")");
	}
}

public enum Vala.CCodeUnaryOperator {
	PLUS,
	MINUS,
	LOGICAL_NEGATION,
	BITWISE_COMPLEMENT,
	POINTER_INDIRECTION,
	ADDRESS_OF,
	PREFIX_INCREMENT,
	PREFIX_DECREMENT,
	POSTFIX_INCREMENT,
	POSTFIX_DECREMENT
}