2001-08-06 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / expression.cs
1 //
2 // expression.cs: Expression representation for the IL tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 namespace CIR {
10         using System.Collections;
11         using System.Diagnostics;
12         using System;
13         
14         public abstract class Expression {
15                 string type;
16                 bool is_lvalue;
17
18                 public string Type {
19                         get {
20                                 return type;
21                         }
22                 }
23
24                 public bool IsLValue {
25                         get {
26                                 return is_lvalue;
27                         }
28
29                         set {
30                                 is_lvalue = value;
31                         }
32                 }
33
34                 public virtual bool IsLiteral {
35                         get {
36                                 return false;
37                         }
38                 }
39                 
40                 // <summary>
41                 //   Protected constructor.  Only derivate types should
42                 //   be able to be created
43                 // </summary>
44                 // protected Type (
45
46                 protected Expression ()
47                 {
48                         type = null;
49                 }
50                 
51                 static bool EvaluateType (Expression expr, out string setType)
52                 {
53                         setType = null;
54                         return true;
55                 }
56         }
57
58         public class Unary : Expression {
59                 public enum Operator {
60                         Plus, Minus, Negate, BitComplement,
61                         Indirection, AddressOf, PreIncrement,
62                         PreDecrement, PostIncrement, PostDecrement
63                 }
64
65                 Operator   oper;
66                 Expression expr;
67                 
68                 public Unary (Operator op, Expression expr)
69                 {
70                         this.oper = op;
71                         this.expr = expr;
72                 }
73
74                 public Expression Expr {
75                         get {
76                                 return expr;
77                         }
78
79                         set {
80                                 expr = value;
81                         }
82                 }
83
84                 public Operator Oper {
85                         get {
86                                 return oper;
87                         }
88
89                         set {
90                                 oper = value;
91                         }
92                 }
93         }
94
95         public class Probe : Expression {
96                 string type;
97                 Expression expr;
98                 Operator oper;
99
100                 public enum Operator {
101                         Is, As
102                 }
103                 
104                 public Probe (Operator oper, Expression expr, string type)
105                 {
106                         this.oper = oper;
107                         this.type = type;
108                         this.expr = expr;
109                 }
110
111                 public Operator Oper {
112                         get {
113                                 return oper;
114                         }
115                 }
116
117                 public Expression Expr {
118                         get {
119                                 return expr;
120                         }
121                 }
122
123                 public string ProbeType {
124                         get {
125                                 return type;
126                         }
127                 }
128                                
129         }
130         
131         public class Cast : Expression {
132                 string type;
133                 Expression expr;
134                 
135                 public Cast (string type, Expression expr)
136                 {
137                         this.type = type;
138                         this.expr = expr;
139                 }
140
141                 public string TargetType {
142                         get {
143                                 return type;
144                         }
145                 }
146
147                 public Expression Expr {
148                         get {
149                                 return expr;
150                         }
151                         set {
152                                 expr = value;
153                         }
154                 }
155         }
156
157         public class Binary : Expression {
158                 public enum Operator {
159                         Multiply, Divide, Modulo,
160                         Add, Substract,
161                         ShiftLeft, ShiftRight,
162                         LessThan, GreatherThan, LessOrEqual, GreatherOrEqual, 
163                         Equal, NotEqual,
164                         BitwiseAnd,
165                         ExclusiveOr,
166                         BitwiseOr,
167                         LogicalAnd,
168                         LogicalOr
169                 }
170
171                 Operator oper;
172                 Expression left, right;
173                 
174                 public Binary (Operator oper, Expression left, Expression right)
175                 {
176                         this.oper = oper;
177                         this.left = left;
178                         this.right = right;
179                 }
180
181                 public Operator Oper {
182                         get {
183                                 return oper;
184                         }
185                         set {
186                                 oper = value;
187                         }
188                 }
189                 
190                 public Expression Left {
191                         get {
192                                 return left;
193                         }
194                         set {
195                                 left = value;
196                         }
197                 }
198
199                 public Expression Right {
200                         get {
201                                 return right;
202                         }
203                         set {
204                                 right = value;
205                         }
206                 }
207         }
208
209         public class Conditional : Expression {
210                 Expression expr, trueExpr, falseExpr;
211                 
212                 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
213                 {
214                         this.expr = expr;
215                         this.trueExpr = trueExpr;
216                         this.falseExpr = falseExpr;
217                 }
218
219                 public Expression Expr {
220                         get {
221                                 return expr;
222                         }
223                 }
224
225                 public Expression TrueExpr {
226                         get {
227                                 return trueExpr;
228                         }
229                 }
230
231                 public Expression FalseExpr {
232                         get {
233                                 return falseExpr;
234                         }
235                 }
236         }
237
238         public class SimpleName : Expression {
239                 string name;
240                 
241                 public SimpleName (string name)
242                 {
243                         this.name = name;
244                 }
245
246                 public string Name {
247                         get {
248                                 return name;
249                         }
250                 }
251         }
252         
253         public class LocalVariableReference : Expression {
254                 string name;
255                 Block block;
256                 
257                 public LocalVariableReference (Block block, string name)
258                 {
259                         this.block = block;
260                         this.name = name;
261                 }
262
263                 public Block Block {
264                         get {
265                                 return block;
266                         }
267                 }
268
269                 public string Name {
270                         get {
271                                 return name;
272                         }
273                 }
274         }
275
276         public class ParameterReference : Expression {
277                 Parameters pars;
278                 string name;
279                 
280                 public ParameterReference (Parameters pars, string name)
281                 {
282                         this.pars = pars;
283                         this.name = name;
284                 }
285
286                 public string Name {
287                         get {
288                                 return name;
289                         }
290                 }
291         }
292         
293         // <summary>
294         //   Used for arguments to New(), Invocation()
295         // </summary>
296         public class Argument {
297                 public enum AType {
298                         Expression,
299                         Ref,
300                         Out
301                 };
302
303                 AType type;
304                 Expression expr;
305
306                 public Argument (Expression expr, AType type)
307                 {
308                         this.expr = expr;
309                         this.type = type;
310                 }
311
312                 public Expression Expr {
313                         get {
314                                 return expr;
315                         }
316                 }
317
318                 public AType Type {
319                         get {
320                                 return type;
321                         }
322                 }
323         }
324
325         // <summary>
326         //   Invocation of methods or delegates.
327         // </summary>
328         public class Invocation : Expression {
329                 ArrayList arguments;
330                 Expression expr;
331
332                 //
333                 // arguments is an ArrayList, but we do not want to typecast,
334                 // as it might be null.
335                 //
336                 // FIXME: only allow expr to be a method invocation or a
337                 // delegate invocation (7.5.5)
338                 //
339                 public Invocation (Expression expr, ArrayList arguments)
340                 {
341                         this.expr = expr;
342                         this.arguments = arguments;
343                 }
344
345                 public Expression Expr {
346                         get {
347                                 return expr;
348                         }
349                 }
350
351                 public ArrayList Arguments {
352                         get {
353                                 return arguments;
354                         }
355                 }
356         }
357
358         public class New : Expression {
359                 ArrayList arguments;
360                 string requested_type;
361
362                 public New (string requested_type, ArrayList arguments)
363                 {
364                         this.requested_type = requested_type;
365                         this.arguments = arguments;
366                 }
367                 
368                 public ArrayList Arguments{
369                         get {
370                                 return arguments;
371                         }
372                 }
373                 
374                 public string RequestedType {
375                         get {
376                                 return requested_type;
377                         }
378                 }
379         }
380
381         public class This : Expression {
382         }
383
384         public class TypeOf : Expression {
385                 string queried_type;
386                 
387                 public TypeOf (string queried_type)
388                 {
389                         this.queried_type = queried_type;
390                 }
391
392                 public string QueriedType {
393                         get {
394                                 return queried_type;
395                         }
396                 }
397         }
398
399         public class SizeOf : Expression {
400                 string queried_type;
401                 
402                 public SizeOf (string queried_type)
403                 {
404                         this.queried_type = queried_type;
405                 }
406
407                 public string QueriedType {
408                         get {
409                                 return queried_type;
410                         }
411                 }
412         }
413
414         public class MemberAccess : Expression {
415                 Expression expr;
416                 string id;
417                 
418                 public MemberAccess (Expression expr, string id)
419                 {
420                         this.expr = expr;
421                         this.id = id;
422                 }
423
424                 public Expression Expr {
425                         get {
426                                 return expr;
427                         }
428                 }
429
430                 public string Identifier {
431                         get {
432                                 return id;
433                         }
434                 }
435         }
436
437         public class BuiltinTypeAccess : Expression {
438                 string access_base;
439                 string method;
440                 
441                 public BuiltinTypeAccess (string type, string method)
442                 {
443                         System.Console.WriteLine ("DUDE! This type should be fully resolved!");
444                         this.access_base = access_base;
445                         this.method = method;
446                 }
447
448                 public string AccessBase {
449                         get {
450                                 return access_base;
451                         }
452                 }
453
454                 public string Method {
455                         get {
456                                 return method;
457                         }
458                 }
459         }
460
461         public class MethodGroup : Expression {
462                 Hashtable signatures;
463                 string name;
464                 
465                 public MethodGroup (string name)
466                 {
467                         signatures = new Hashtable ();
468                         this.name = name;
469                 }
470
471                 public bool Add (Method method)
472                 {
473                         string sig = method.ArgumentSignature;
474
475                         if (signatures.Contains (sig))
476                                 return false;
477
478                         signatures.Add (sig, method);
479                         return true;
480                 }
481
482                 public string Name {
483                         get {
484                                 return name;
485                         }
486                 }
487
488                 public Hashtable Methods {
489                         get {
490                                 return signatures;
491                         }
492                 }
493         }
494 }
495
496
497
498
499