This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / gmcs / 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                 private NullPointer ()
78                 {
79                         type = TypeManager.object_type;
80                 }
81
82                 public override void Emit (EmitContext ec)
83                 {
84                         ILGenerator ig = ec.ig;
85                                 
86                         ig.Emit (OpCodes.Ldc_I4_0);
87                         ig.Emit (OpCodes.Conv_U);
88                 }
89         }
90
91         public class BoolLiteral : BoolConstant {
92                 public BoolLiteral (bool val) : base (val)
93                 {
94                 }
95
96                 public override Expression DoResolve (EmitContext ec)
97                 {
98                         type = TypeManager.bool_type;
99                         return this;
100                 }
101         }
102
103         public class CharLiteral : CharConstant {
104                 public CharLiteral (char c) : base (c)
105                 {
106                 }
107
108                 public override Expression DoResolve (EmitContext ec)
109                 {
110                         type = TypeManager.char_type;
111                         return this;
112                 }
113         }
114
115         public class IntLiteral : IntConstant {
116                 public static IntLiteral One, Zero;
117                 
118                 static IntLiteral ()
119                 {
120                         Zero = new IntLiteral (0);
121                         One = new IntLiteral (1);
122                 }
123                 
124                 public IntLiteral (int l) : base (l)
125                 {
126                 }
127
128                 public override Expression DoResolve (EmitContext ec)
129                 {
130                         type = TypeManager.int32_type;
131                         return this;
132                 }
133         }
134
135         public class UIntLiteral : UIntConstant {
136                 public UIntLiteral (uint l) : base (l)
137                 {
138                 }
139
140                 public override Expression DoResolve (EmitContext ec)
141                 {
142                         type = TypeManager.uint32_type;
143                         return this;
144                 }
145         }
146         
147         public class LongLiteral : LongConstant {
148                 public LongLiteral (long l) : base (l)
149                 {
150                 }
151
152                 public override Expression DoResolve (EmitContext ec)
153                 {
154                         type = TypeManager.int64_type;
155
156                         return this;
157                 }
158         }
159
160         public class ULongLiteral : ULongConstant {
161                 public ULongLiteral (ulong l) : base (l)
162                 {
163                 }
164
165                 public override Expression DoResolve (EmitContext ec)
166                 {
167                         type = TypeManager.uint64_type;
168                         return this;
169                 }
170         }
171         
172         public class FloatLiteral : FloatConstant {
173                 
174                 public FloatLiteral (float f) : base (f)
175                 {
176                 }
177
178                 public override Expression DoResolve (EmitContext ec)
179                 {
180                         type = TypeManager.float_type;
181                         return this;
182                 }
183         }
184
185         public class DoubleLiteral : DoubleConstant {
186                 public DoubleLiteral (double d) : base (d)
187                 {
188                 }
189
190                 public override Expression DoResolve (EmitContext ec)
191                 {
192                         type = TypeManager.double_type;
193
194                         return this;
195                 }
196         }
197
198         public class DecimalLiteral : DecimalConstant {
199                 public DecimalLiteral (decimal d) : base (d)
200                 {
201                 }
202
203                 public override Expression DoResolve (EmitContext ec)
204                 {
205                         type = TypeManager.decimal_type;
206                         return this;
207                 }
208         }
209
210         public class StringLiteral : StringConstant {
211                 public StringLiteral (string s) : base (s)
212                 {
213                 }
214
215                 public override Expression DoResolve (EmitContext ec)
216                 {
217                         type = TypeManager.string_type;
218
219                         return this;
220                 }
221         }
222 }