Committing Miguel's type lookup patch.
[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                         if (Null != null)
38                                 throw new Exception ("More than one null has been created!");
39                         eclass = ExprClass.Value;
40                 }
41                 
42                 override public string AsString ()
43                 {
44                         return "null";
45                 }
46
47                 public override object GetValue ()
48                 {
49                         return null;
50                 }
51
52                 public override Expression DoResolve (EmitContext ec)
53                 {
54                         type = TypeManager.object_type;
55                         return this;
56                 }
57
58                 public override void Emit (EmitContext ec)
59                 {
60                         ec.ig.Emit (OpCodes.Ldnull);
61                 }
62         }
63
64         public class BoolLiteral : BoolConstant {
65                 public BoolLiteral (bool val) : base (val)
66                 {
67                 }
68
69                 public override Expression DoResolve (EmitContext ec)
70                 {
71                         type = TypeManager.bool_type;
72                         return this;
73                 }
74         }
75
76         public class CharLiteral : CharConstant {
77                 public CharLiteral (char c) : base (c)
78                 {
79                 }
80
81                 public override Expression DoResolve (EmitContext ec)
82                 {
83                         type = TypeManager.char_type;
84                         return this;
85                 }
86         }
87
88         public class IntLiteral : IntConstant {
89                 public IntLiteral (int l) : base (l)
90                 {
91                 }
92
93                 public override Expression DoResolve (EmitContext ec)
94                 {
95                         type = TypeManager.int32_type;
96                         return this;
97                 }
98         }
99
100         public class UIntLiteral : UIntConstant {
101                 public UIntLiteral (uint l) : base (l)
102                 {
103                 }
104
105                 public override Expression DoResolve (EmitContext ec)
106                 {
107                         type = TypeManager.uint32_type;
108                         return this;
109                 }
110         }
111         
112         public class LongLiteral : LongConstant {
113                 public LongLiteral (long l) : base (l)
114                 {
115                 }
116
117                 public override Expression DoResolve (EmitContext ec)
118                 {
119                         type = TypeManager.int64_type;
120
121                         return this;
122                 }
123         }
124
125         public class ULongLiteral : ULongConstant {
126                 public ULongLiteral (ulong l) : base (l)
127                 {
128                 }
129
130                 public override Expression DoResolve (EmitContext ec)
131                 {
132                         type = TypeManager.uint64_type;
133                         return this;
134                 }
135         }
136         
137         public class FloatLiteral : FloatConstant {
138                 
139                 public FloatLiteral (float f) : base (f)
140                 {
141                 }
142
143                 public override Expression DoResolve (EmitContext ec)
144                 {
145                         type = TypeManager.float_type;
146                         return this;
147                 }
148         }
149
150         public class DoubleLiteral : DoubleConstant {
151                 public DoubleLiteral (double d) : base (d)
152                 {
153                 }
154
155                 public override Expression DoResolve (EmitContext ec)
156                 {
157                         type = TypeManager.double_type;
158
159                         return this;
160                 }
161         }
162
163         public class DecimalLiteral : DecimalConstant {
164                 public DecimalLiteral (decimal d) : base (d)
165                 {
166                 }
167
168                 public override Expression DoResolve (EmitContext ec)
169                 {
170                         type = TypeManager.decimal_type;
171                         return this;
172                 }
173         }
174
175         public class StringLiteral : StringConstant {
176                 public StringLiteral (string s) : base (s)
177                 {
178                 }
179
180                 public override Expression DoResolve (EmitContext ec)
181                 {
182                         type = TypeManager.string_type;
183
184                         return this;
185                 }
186         }
187 }