Release 0.41.92
[vala-gnome.git] / ccode / valaccodeunaryexpression.vala
blob548e42181e8dc7cc3ec2472c83474f91210ccdb8
1 /* valaccodeunaryexpression.vala
3 * Copyright (C) 2006-2009 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents an expression with one operand in the C code.
28 public class Vala.CCodeUnaryExpression : CCodeExpression {
29 /**
30 * The unary operator.
32 public CCodeUnaryOperator operator { get; set; }
34 /**
35 * The operand.
37 public CCodeExpression inner { get; set; }
39 public CCodeUnaryExpression (CCodeUnaryOperator op, CCodeExpression expr) {
40 operator = op;
41 inner = expr;
44 public override void write (CCodeWriter writer) {
45 switch (operator) {
46 case CCodeUnaryOperator.PLUS: writer.write_string ("+"); inner.write_inner (writer); break;
47 case CCodeUnaryOperator.MINUS: writer.write_string ("-"); inner.write_inner (writer); break;
48 case CCodeUnaryOperator.LOGICAL_NEGATION: writer.write_string ("!"); inner.write_inner (writer); break;
49 case CCodeUnaryOperator.BITWISE_COMPLEMENT: writer.write_string ("~"); inner.write_inner (writer); break;
50 case CCodeUnaryOperator.POINTER_INDIRECTION:
51 var inner_unary = inner as CCodeUnaryExpression;
52 if (inner_unary != null && inner_unary.operator == CCodeUnaryOperator.ADDRESS_OF) {
53 // simplify expression
54 inner_unary.inner.write (writer);
55 return;
57 writer.write_string ("*");
58 inner.write_inner (writer);
59 break;
60 case CCodeUnaryOperator.ADDRESS_OF:
61 var inner_unary = inner as CCodeUnaryExpression;
62 if (inner_unary != null && inner_unary.operator == CCodeUnaryOperator.POINTER_INDIRECTION) {
63 // simplify expression
64 inner_unary.inner.write (writer);
65 return;
67 writer.write_string ("&");
68 inner.write_inner (writer);
69 break;
70 case CCodeUnaryOperator.PREFIX_INCREMENT: writer.write_string ("++"); break;
71 case CCodeUnaryOperator.PREFIX_DECREMENT: writer.write_string ("--"); break;
72 case CCodeUnaryOperator.POSTFIX_INCREMENT: inner.write_inner (writer); writer.write_string ("++"); break;
73 case CCodeUnaryOperator.POSTFIX_DECREMENT: inner.write_inner (writer); writer.write_string ("--"); break;
74 default: assert_not_reached ();
78 public override void write_inner (CCodeWriter writer) {
79 writer.write_string ("(");
80 this.write (writer);
81 writer.write_string (")");
85 public enum Vala.CCodeUnaryOperator {
86 PLUS,
87 MINUS,
88 LOGICAL_NEGATION,
89 BITWISE_COMPLEMENT,
90 POINTER_INDIRECTION,
91 ADDRESS_OF,
92 PREFIX_INCREMENT,
93 PREFIX_DECREMENT,
94 POSTFIX_INCREMENT,
95 POSTFIX_DECREMENT