2003-07-26 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mcs / assign.cs
1 //
2 // assign.cs: Assignments.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Martin Baulig (martin@gnome.org)
7 //
8 // (C) 2001, 2002, 2003 Ximian, Inc.
9 //
10 using System;
11 using System.Reflection;
12 using System.Reflection.Emit;
13
14 namespace Mono.CSharp {
15
16         /// <summary>
17         ///   This interface is implemented by expressions that can be assigned to.
18         /// </summary>
19         /// <remarks>
20         ///   This interface is implemented by Expressions whose values can not
21         ///   store the result on the top of the stack.
22         ///
23         ///   Expressions implementing this (Properties, Indexers and Arrays) would
24         ///   perform an assignment of the Expression "source" into its final
25         ///   location.
26         ///
27         ///   No values on the top of the stack are expected to be left by
28         ///   invoking this method.
29         /// </remarks>
30         public interface IAssignMethod {
31                 //
32                 // This method will emit the code for the actual assignment
33                 //
34                 void EmitAssign (EmitContext ec, Expression source);
35
36                 //
37                 // This method is invoked before any code generation takes
38                 // place, and it is a mechanism to inform that the expression
39                 // will be invoked more than once, and that the method should
40                 // use temporary values to avoid having side effects
41                 //
42                 // Example: a [ g () ] ++
43                 //
44                 void CacheTemporaries (EmitContext ec);
45         }
46
47         /// <summary>
48         ///   An Expression to hold a temporary value.
49         /// </summary>
50         /// <remarks>
51         ///   The LocalTemporary class is used to hold temporary values of a given
52         ///   type to "simulate" the expression semantics on property and indexer
53         ///   access whose return values are void.
54         ///
55         ///   The local temporary is used to alter the normal flow of code generation
56         ///   basically it creates a local variable, and its emit instruction generates
57         ///   code to access this value, return its address or save its value.
58         /// </remarks>
59         public class LocalTemporary : Expression, IMemoryLocation {
60                 LocalBuilder builder;
61                 
62                 public LocalTemporary (EmitContext ec, Type t)
63                 {
64                         type = t;
65                         eclass = ExprClass.Value;
66                         loc = Location.Null;
67                         builder = ec.GetTemporaryStorage (t);
68                 }
69
70                 public void Release (EmitContext ec)
71                 {
72                         ec.FreeTemporaryStorage (builder);
73                         builder = null;
74                 }
75                 
76                 public LocalTemporary (LocalBuilder b, Type t)
77                 {
78                         type = t;
79                         eclass = ExprClass.Value;
80                         loc = Location.Null;
81                         builder = b;
82                 }
83                 
84                 public override Expression DoResolve (EmitContext ec)
85                 {
86                         return this;
87                 }
88
89                 public override void Emit (EmitContext ec)
90                 {
91                         ec.ig.Emit (OpCodes.Ldloc, builder); 
92                 }
93
94                 public void Store (EmitContext ec)
95                 {
96                         ec.ig.Emit (OpCodes.Stloc, builder);
97                 }
98
99                 public void AddressOf (EmitContext ec, AddressOp mode)
100                 {
101                         ec.ig.Emit (OpCodes.Ldloca, builder);
102                 }
103         }
104
105         /// <summary>
106         ///   The Assign node takes care of assigning the value of source into
107         ///   the expression represented by target. 
108         /// </summary>
109         public class Assign : ExpressionStatement {
110                 protected Expression target, source, real_source;
111                 protected LocalTemporary temp = null, real_temp = null;
112                 protected Assign embedded = null;
113                 protected bool is_embedded = false;
114                 protected bool must_free_temp = false;
115
116                 public Assign (Expression target, Expression source, Location l)
117                 {
118                         this.target = target;
119                         this.source = this.real_source = source;
120                         this.loc = l;
121                 }
122
123                 protected Assign (Assign embedded, Location l)
124                         : this (embedded.target, embedded.source, l)
125                 {
126                         this.is_embedded = true;
127                 }
128
129                 protected virtual Assign GetEmbeddedAssign (Location loc)
130                 {
131                         return new Assign (this, loc);
132                 }
133
134                 public Expression Target {
135                         get {
136                                 return target;
137                         }
138
139                         set {
140                                 target = value;
141                         }
142                 }
143
144                 public Expression Source {
145                         get {
146                                 return source;
147                         }
148
149                         set {
150                                 source = value;
151                         }
152                 }
153
154                 public static void error70 (EventInfo ei, Location l)
155                 {
156                         Report.Error (70, l, "The event '" + ei.Name +
157                                       "' can only appear on the left-side of a += or -= (except when" +
158                                       " used from within the type '" + ei.DeclaringType + "')");
159                 }
160
161                 //
162                 // Will return either `this' or an instance of `New'.
163                 //
164                 public override Expression DoResolve (EmitContext ec)
165                 {
166                         // Create an embedded assignment if our source is an assignment.
167                         if (source is Assign)
168                                 source = embedded = ((Assign) source).GetEmbeddedAssign (loc);
169
170                         real_source = source = source.Resolve (ec);
171                         if (source == null)
172                                 return null;
173
174                         //
175                         // This is used in an embedded assignment.
176                         // As an example, consider the statement "A = X = Y = Z".
177                         //
178                         if (is_embedded && !(source is Constant)) {
179                                 // If this is the innermost assignment (the "Y = Z" in our example),
180                                 // create a new temporary local, otherwise inherit that variable
181                                 // from our child (the "X = (Y = Z)" inherits the local from the
182                                 // "Y = Z" assignment).
183
184                                 if (embedded == null) {
185                                         if (this is CompoundAssign)
186                                                 real_temp = temp = new LocalTemporary (ec, target.Type);
187                                         else
188                                                 real_temp = temp = new LocalTemporary (ec, source.Type);
189                                 } else
190                                         temp = embedded.temp;
191
192                                 // Set the source to the new temporary variable.
193                                 // This means that the following target.ResolveLValue () will tell
194                                 // the target to read it's source value from that variable.
195                                 source = temp;
196                         }
197
198                         // If we have an embedded assignment, use the embedded assignment's temporary
199                         // local variable as source.
200                         if (embedded != null)
201                                 source = (embedded.temp != null) ? embedded.temp : embedded.source;
202
203                         target = target.ResolveLValue (ec, source);
204
205                         if (target == null)
206                                 return null;
207
208                         Type target_type = target.Type;
209                         Type source_type = real_source.Type;
210
211                         // If we're an embedded assignment, our parent will reuse our source as its
212                         // source, it won't read from our target.
213                         if (is_embedded)
214                                 type = source_type;
215                         else
216                                 type = target_type;
217                         eclass = ExprClass.Value;
218
219                         if (target is EventExpr) {
220                                 EventInfo ei = ((EventExpr) target).EventInfo;
221
222                                 Expression ml = MemberLookup (
223                                         ec, ec.ContainerType, ei.Name,
224                                         MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
225
226                                 if (ml == null) {
227                                         //
228                                         // If this is the case, then the Event does not belong 
229                                         // to this Type and so, according to the spec
230                                         // is allowed to only appear on the left hand of
231                                         // the += and -= operators
232                                         //
233                                         // Note that target will not appear as an EventExpr
234                                         // in the case it is being referenced within the same type container;
235                                         // it will appear as a FieldExpr in that case.
236                                         //
237                                         
238                                         if (!(source is Binary)) {
239                                                 error70 (ei, loc);
240                                                 return null;
241                                         } else {
242                                                 Binary tmp = ((Binary) source);
243                                                 if (tmp.Oper != Binary.Operator.Addition &&
244                                                     tmp.Oper != Binary.Operator.Subtraction) {
245                                                         error70 (ei, loc);
246                                                         return null;
247                                                 }
248                                         }
249                                 }
250                         }
251                         
252                         if (source is New && target_type.IsValueType &&
253                             (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
254                                 New n = (New) source;
255
256                                 if (n.SetValueTypeVariable (target))
257                                         return n;
258                                 else
259                                         return null;
260                         }
261
262                         if (target.eclass != ExprClass.Variable && target.eclass != ExprClass.EventAccess &&
263                             target.eclass != ExprClass.IndexerAccess && target.eclass != ExprClass.PropertyAccess){
264                                 Report.Error (131, loc,
265                                               "Left hand of an assignment must be a variable, " +
266                                               "a property or an indexer");
267                                 return null;
268                         }
269
270                         if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
271                                 source.Error_UnexpectedKind ("variable or value");
272                                 return null;
273                         } else if (source is MethodGroupExpr){
274                                 ((MethodGroupExpr) source).ReportUsageError ();
275                                 return null;
276                         }
277
278                         if (target_type == source_type)
279                                 return this;
280                         
281                         //
282                         // If this assignemnt/operator was part of a compound binary
283                         // operator, then we allow an explicit conversion, as detailed
284                         // in the spec. 
285                         //
286
287                         if (this is CompoundAssign){
288                                 CompoundAssign a = (CompoundAssign) this;
289                                 
290                                 Binary b = source as Binary;
291                                 if (b != null && b.IsBuiltinOperator){
292                                         //
293                                         // 1. if the source is explicitly convertible to the
294                                         //    target_type
295                                         //
296                                         
297                                         source = Convert.ExplicitConversion (ec, source, target_type, loc);
298                                         if (source == null){
299                                                 Convert.Error_CannotImplicitConversion (loc, source_type, target_type);
300                                                 return null;
301                                         }
302                                 
303                                         //
304                                         // 2. and the original right side is implicitly convertible to
305                                         // the type of target_type.
306                                         //
307                                         if (Convert.ImplicitStandardConversionExists (a.original_source, target_type))
308                                                 return this;
309
310                                         Convert.Error_CannotImplicitConversion (loc, a.original_source.Type, target_type);
311                                         return null;
312                                 }
313                         }
314                         
315                         source = Convert.ImplicitConversionRequired (ec, source, target_type, loc);
316                         if (source == null)
317                                 return null;
318
319                         // If we're an embedded assignment, we need to create a new temporary variable
320                         // for the converted value.  Our parent will use this new variable as its source.
321                         // The same applies when we have an embedded assignment - in this case, we need
322                         // to convert our embedded assignment's temporary local variable to the correct
323                         // type and store it in a new temporary local.
324                         if (is_embedded || embedded != null) {
325                                 type = target_type;
326                                 temp = new LocalTemporary (ec, type);
327                                 must_free_temp = true;
328                         }
329                         
330                         return this;
331                 }
332
333                 Expression EmitEmbedded (EmitContext ec)
334                 {
335                         // Emit an embedded assignment.
336
337                         if (real_temp != null) {
338                                 // If we're the innermost assignment, `real_source' is the right-hand
339                                 // expression which gets assigned to all the variables left of it.
340                                 // Emit this expression and store its result in real_temp.
341                                 real_source.Emit (ec);
342                                 real_temp.Store (ec);
343                         }
344
345                         if (embedded != null)
346                                 embedded.EmitEmbedded (ec);
347
348                         // This happens when we've done a type conversion, in this case source will be
349                         // the expression which does the type conversion from real_temp.
350                         // So emit it and store the result in temp; this is the var which will be read
351                         // by our parent.
352                         if (temp != real_temp) {
353                                 source.Emit (ec);
354                                 temp.Store (ec);
355                         }
356
357                         Expression temp_source = (temp != null) ? temp : source;
358                         ((IAssignMethod) target).EmitAssign (ec, temp_source);
359                         return temp_source;
360                 }
361
362                 void ReleaseEmbedded (EmitContext ec)
363                 {
364                         if (embedded != null)
365                                 embedded.ReleaseEmbedded (ec);
366
367                         if (real_temp != null)
368                                 real_temp.Release (ec);
369
370                         if (must_free_temp)
371                                 temp.Release (ec);
372                 }
373
374                 void Emit (EmitContext ec, bool is_statement)
375                 {
376                         if (target is EventExpr) {
377                                 ((EventExpr) target).EmitAddOrRemove (ec, source);
378                                 return;
379                         }
380
381                         bool use_temporaries = false;
382                         
383                         //
384                         // FIXME! We need a way to "probe" if the process can
385                         // just use `dup' to propagate the result
386                         // 
387                         IAssignMethod am = (IAssignMethod) target;
388                         
389                         if (this is CompoundAssign){
390                                 am.CacheTemporaries (ec);
391                                 use_temporaries = true;
392                         }
393
394                         if (!is_statement)
395                                 use_temporaries = true;
396
397                         Expression temp_source;
398                         if (embedded != null) {
399                                 temp_source = embedded.EmitEmbedded (ec);
400
401                                 if (temp != null) {
402                                         source.Emit (ec);
403                                         temp.Store (ec);
404                                         temp_source = temp;
405                                 }
406                         } else
407                                 temp_source = source;
408
409                         if (use_temporaries){
410                                 //
411                                 // Doing this for every path is too expensive
412                                 // I wonder if we can work around this and have a less
413                                 // expensive path
414                                 //
415                                 LocalTemporary tempo;
416                                 
417                                 tempo = new LocalTemporary (ec, source.Type);
418                                 
419                                 temp_source.Emit (ec);
420                                 tempo.Store (ec);
421                                 am.EmitAssign (ec, tempo);
422                                 if (!is_statement)
423                                         tempo.Emit (ec);
424                                 
425                                 tempo.Release (ec);
426                         } else {
427                                 am.EmitAssign (ec, temp_source);
428                         }
429                                 
430                         if (embedded != null) {
431                                 if (temp != null)
432                                         temp.Release (ec);
433                                 embedded.ReleaseEmbedded (ec);
434                         }
435                 }
436                 
437                 public override void Emit (EmitContext ec)
438                 {
439                         Emit (ec, false);
440                 }
441
442                 public override void EmitStatement (EmitContext ec)
443                 {
444                         Emit (ec, true);
445                 }
446         }
447
448         
449         //
450         // This class is used for compound assignments.  
451         //
452         class CompoundAssign : Assign {
453                 Binary.Operator op;
454                 public Expression original_source;
455                 
456                 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
457                         : base (target, source, l)
458                 {
459                         original_source = source;
460                         this.op = op;
461                 }
462
463                 protected CompoundAssign (CompoundAssign embedded, Location l)
464                         : this (embedded.op, embedded.target, embedded.source, l)
465                 {
466                         this.is_embedded = true;
467                 }
468
469                 protected override Assign GetEmbeddedAssign (Location loc)
470                 {
471                         return new CompoundAssign (this, loc);
472                 }
473
474                 public Expression ResolveSource (EmitContext ec)
475                 {
476                         return original_source.Resolve (ec);
477                 }
478
479                 public override Expression DoResolve (EmitContext ec)
480                 {
481                         original_source = original_source.Resolve (ec);
482                         if (original_source == null)
483                                 return null;
484
485                         target = target.Resolve (ec);
486                         if (target == null)
487                                 return null;
488                         
489                         //
490                         // Only now we can decouple the original source/target
491                         // into a tree, to guarantee that we do not have side
492                         // effects.
493                         //
494                         source = new Binary (op, target, original_source, loc);
495                         return base.DoResolve (ec);
496                 }
497         }
498 }
499
500
501
502