1'
2' Visual Basic.Net Compiler
3' Copyright (C) 2004 - 2010 Rolf Bjarne Kvinge, RKvinge@novell.com
4'
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.
9'
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.
14'
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
18'
19
20Public Class UnaryMinusExpression
21    Inherits UnaryExpression
22
23    Sub New(ByVal Parent As ParsedObject)
24        MyBase.New(Parent)
25    End Sub
26
27    Shadows Sub Init(ByVal Expression As Expression)
28        MyBase.Init(Expression)
29    End Sub
30
31    Protected Overrides Function GenerateCodeInternal(ByVal Info As EmitInfo) As Boolean
32        Dim result As Boolean = True
33
34        Dim expInfo As EmitInfo = Info.Clone(Me, True, False, OperandType)
35
36        Select Case OperandTypeCode
37            Case TypeCode.Decimal
38                result = Expression.GenerateCode(expInfo) AndAlso result
39                Emitter.EmitCall(Info, Compiler.TypeCache.System_Decimal__Negate_Decimal)
40            Case TypeCode.Single, TypeCode.Double
41                result = Expression.GenerateCode(expInfo) AndAlso result
42                Emitter.EmitNeg(Info)
43            Case TypeCode.SByte, TypeCode.Int16
44                result = Expression.GenerateCode(expInfo) AndAlso result
45                Emitter.EmitNeg(Info)
46            Case TypeCode.Object
47                Helper.Assert(Helper.CompareType(OperandType, Compiler.TypeCache.System_Object))
48                result = Me.Expression.GenerateCode(expInfo) AndAlso result
49                Emitter.EmitCall(Info, Compiler.TypeCache.MS_VB_CS_Operators__NegateObject_Object)
50            Case TypeCode.Int32
51                Emitter.EmitLoadI4Value(Info, 0)
52                result = Expression.GenerateCode(expInfo) AndAlso result
53                Emitter.EmitSubOvf(Info, OperandType)
54            Case TypeCode.Int64
55                Emitter.EmitLoadI8Value(Info, 0)
56                result = Expression.GenerateCode(expInfo) AndAlso result
57                Emitter.EmitSubOvf(Info, OperandType)
58            Case Else
59                Throw New InternalException(Me)
60        End Select
61
62        Return result
63    End Function
64
65    Public Overrides Function GetConstant(ByRef result As Object, ByVal ShowError As Boolean) As Boolean
66        If Not Expression.GetConstant(result, ShowError) Then Return False
67
68        Select Case Helper.GetTypeCode(Compiler, CecilHelper.GetType(Compiler, result))
69            Case TypeCode.SByte
70                result = -CSByte(result)
71            Case TypeCode.Int16
72                result = -CShort(result)
73            Case TypeCode.Int32
74                result = -CInt(result)
75            Case TypeCode.Int64
76                result = -CLng(result)
77            Case TypeCode.Byte
78            Case TypeCode.UInt16
79            Case TypeCode.UInt32
80            Case TypeCode.UInt64
81                result = -CULng(result)
82            Case TypeCode.Decimal
83                result = -CDec(result)
84            Case TypeCode.Double
85                result = -CDbl(result)
86            Case TypeCode.Single
87                result = -CSng(result)
88            Case Else
89                If ShowError Then Show30059()
90                Return False
91        End Select
92
93        Return True
94    End Function
95
96    Public Overrides ReadOnly Property Keyword() As KS
97        Get
98            Return KS.Minus
99        End Get
100    End Property
101End Class