2004-03-23 Ben Maurer <bmaurer@users.sourceforge.net>
[mono.git] / mcs / mcs / literal.cs
1 //
2 // literal.cs: Literal representation for the IL tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10 // Notice that during parsing we create objects of type Literal, but the
11 // types are not loaded (thats why the Resolve method has to assign the
12 // type at that point).
13 //
14 // Literals differ from the constants in that we know we encountered them
15 // as a literal in the source code (and some extra rules apply there) and
16 // they have to be resolved (since during parsing we have not loaded the
17 // types yet) while constants are created only after types have been loaded
18 // and are fully resolved when born.
19 //
20
21 using System;
22 using System.Reflection;
23 using System.Reflection.Emit;
24
25 namespace Mono.CSharp {
26
27         public class NullLiteral : Constant {
28                 public static readonly NullLiteral Null;
29
30                 static NullLiteral ()
31                 {
32                         Null = new NullLiteral ();
33                 }
34                         
35                 public NullLiteral ()
36                 {
37                         eclass = ExprClass.Value;
38                 }
39                 
40                 override public string AsString ()
41                 {
42                         return "null";
43                 }
44
45                 public override object GetValue ()
46                 {
47                         return null;
48                 }
49
50                 public override Expression DoResolve (EmitContext ec)
51                 {
52                         type = TypeManager.object_type;
53                         return this;
54                 }
55
56                 public override void Emit (EmitContext ec)
57                 {
58                         ec.ig.Emit (OpCodes.Ldnull);
59                 }
60                 
61                 public override bool IsZeroInteger {
62                         get { return true; }
63                 }
64         }
65
66         //
67         // A null literal in a pointer context
68         //
69         public class NullPointer : NullLiteral {
70                 public new static readonly NullLiteral Null;
71
72                 static NullPointer ()
73                 {
74                         Null = new NullPointer ();
75                 }
76                 
77                 public override void Emit (EmitContext ec)
78                 {
79                         ILGenerator ig = ec.ig;
80                                 
81                         ig.Emit (OpCodes.Ldc_I4_0);
82                         ig.Emit (OpCodes.Conv_U);
83                 }
84         }
85
86         public class BoolLiteral : BoolConstant {
87                 public BoolLiteral (bool val) : base (val)
88                 {
89                 }
90
91                 public override Expression DoResolve (EmitContext ec)
92                 {
93                         type = TypeManager.bool_type;
94                         return this;
95                 }
96         }
97
98         public class CharLiteral : CharConstant {
99                 public CharLiteral (char c) : base (c)
100                 {
101                 }
102
103                 public override Expression DoResolve (EmitContext ec)
104                 {
105                         type = TypeManager.char_type;
106                         return this;
107                 }
108         }
109
110         public class IntLiteral : IntConstant {
111                 public static IntLiteral One, Zero;
112                 
113                 static IntLiteral ()
114                 {
115                         Zero = new IntLiteral (0);
116                         One = new IntLiteral (1);
117                 }
118                 
119                 public IntLiteral (int l) : base (l)
120                 {
121                 }
122
123                 public override Expression DoResolve (EmitContext ec)
124                 {
125                         type = TypeManager.int32_type;
126                         return this;
127                 }
128         }
129
130         public class UIntLiteral : UIntConstant {
131                 public UIntLiteral (uint l) : base (l)
132                 {
133                 }
134
135                 public override Expression DoResolve (EmitContext ec)
136                 {
137                         type = TypeManager.uint32_type;
138                         return this;
139                 }
140         }
141         
142         public class LongLiteral : LongConstant {
143                 public LongLiteral (long l) : base (l)
144                 {
145                 }
146
147                 public override Expression DoResolve (EmitContext ec)
148                 {
149                         type = TypeManager.int64_type;
150
151                         return this;
152                 }
153         }
154
155         public class ULongLiteral : ULongConstant {
156                 public ULongLiteral (ulong l) : base (l)
157                 {
158                 }
159
160                 public override Expression DoResolve (EmitContext ec)
161                 {
162                         type = TypeManager.uint64_type;
163                         return this;
164                 }
165         }
166         
167         public class FloatLiteral : FloatConstant {
168                 
169                 public FloatLiteral (float f) : base (f)
170                 {
171                 }
172
173                 public override Expression DoResolve (EmitContext ec)
174                 {
175                         type = TypeManager.float_type;
176                         return this;
177                 }
178         }
179
180         public class DoubleLiteral : DoubleConstant {
181                 public DoubleLiteral (double d) : base (d)
182                 {
183                 }
184
185                 public override Expression DoResolve (EmitContext ec)
186                 {
187                         type = TypeManager.double_type;
188
189                         return this;
190                 }
191         }
192
193         public class DecimalLiteral : DecimalConstant {
194                 public DecimalLiteral (decimal d) : base (d)
195                 {
196                 }
197
198                 public override Expression DoResolve (EmitContext ec)
199                 {
200                         type = TypeManager.decimal_type;
201                         return this;
202                 }
203         }
204
205         public class StringLiteral : StringConstant {
206                 public StringLiteral (string s) : base (s)
207                 {
208                 }
209
210                 public override Expression DoResolve (EmitContext ec)
211                 {
212                         type = TypeManager.string_type;
213
214                         return this;
215                 }
216         }
217 }