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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents an expression with one operand in the C code.
28 public class Vala
.CCodeUnaryExpression
: CCodeExpression
{
32 public CCodeUnaryOperator operator
{ get; set; }
37 public CCodeExpression inner
{ get; set; }
39 public CCodeUnaryExpression (CCodeUnaryOperator op
, CCodeExpression expr
) {
44 public override void write (CCodeWriter writer
) {
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
);
57 writer
.write_string ("*");
58 inner
.write_inner (writer
);
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
);
67 writer
.write_string ("&");
68 inner
.write_inner (writer
);
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 ("(");
81 writer
.write_string (")");
85 public enum Vala
.CCodeUnaryOperator
{