cf6008979fd5c562ebb66e05ee8f2702d1725635
[mono.git] / mcs / gmcs / 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 is an extra version of Emit. If leave_copy is `true'
33                 // A copy of the expression will be left on the stack at the
34                 // end of the code generated for EmitAssign
35                 //
36                 void Emit (EmitContext ec, bool leave_copy);
37                 
38                 //
39                 // This method does the assignment
40                 // `source' will be stored into the location specified by `this'
41                 // if `leave_copy' is true, a copy of `source' will be left on the stack
42                 // if `prepare_for_load' is true, when `source' is emitted, there will
43                 // be data on the stack that it can use to compuatate its value. This is
44                 // for expressions like a [f ()] ++, where you can't call `f ()' twice.
45                 //
46                 void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load);
47                 
48                 /*
49                 For simple assignments, this interface is very simple, EmitAssign is called with source
50                 as the source expression and leave_copy and prepare_for_load false.
51                 
52                 For compound assignments it gets complicated.
53                 
54                 EmitAssign will be called as before, however, prepare_for_load will be
55                 true. The @source expression will contain an expression
56                 which calls Emit. So, the calls look like:
57                 
58                 this.EmitAssign (ec, source, false, true) ->
59                         source.Emit (ec); ->
60                                 [...] ->
61                                         this.Emit (ec, false); ->
62                                         end this.Emit (ec, false); ->
63                                 end [...]
64                         end source.Emit (ec);
65                 end this.EmitAssign (ec, source, false, true)
66                 
67                 
68                 When prepare_for_load is true, EmitAssign emits a `token' on the stack that
69                 Emit will use for its state.
70                 
71                 Let's take FieldExpr as an example. assume we are emitting f ().y += 1;
72                 
73                 Here is the call tree again. This time, each call is annotated with the IL
74                 it produces:
75                 
76                 this.EmitAssign (ec, source, false, true)
77                         call f
78                         dup
79                         
80                         Binary.Emit ()
81                                 this.Emit (ec, false);
82                                 ldfld y
83                                 end this.Emit (ec, false);
84                                 
85                                 IntConstant.Emit ()
86                                 ldc.i4.1
87                                 end IntConstant.Emit
88                                 
89                                 add
90                         end Binary.Emit ()
91                         
92                         stfld
93                 end this.EmitAssign (ec, source, false, true)
94                 
95                 Observe two things:
96                         1) EmitAssign left a token on the stack. It was the result of f ().
97                         2) This token was used by Emit
98                 
99                 leave_copy (in both EmitAssign and Emit) tells the compiler to leave a copy
100                 of the expression at that point in evaluation. This is used for pre/post inc/dec
101                 and for a = x += y. Let's do the above example with leave_copy true in EmitAssign
102                 
103                 this.EmitAssign (ec, source, true, true)
104                         call f
105                         dup
106                         
107                         Binary.Emit ()
108                                 this.Emit (ec, false);
109                                 ldfld y
110                                 end this.Emit (ec, false);
111                                 
112                                 IntConstant.Emit ()
113                                 ldc.i4.1
114                                 end IntConstant.Emit
115                                 
116                                 add
117                         end Binary.Emit ()
118                         
119                         dup
120                         stloc temp
121                         stfld
122                         ldloc temp
123                 end this.EmitAssign (ec, source, true, true)
124                 
125                 And with it true in Emit
126                 
127                 this.EmitAssign (ec, source, false, true)
128                         call f
129                         dup
130                         
131                         Binary.Emit ()
132                                 this.Emit (ec, true);
133                                 ldfld y
134                                 dup
135                                 stloc temp
136                                 end this.Emit (ec, true);
137                                 
138                                 IntConstant.Emit ()
139                                 ldc.i4.1
140                                 end IntConstant.Emit
141                                 
142                                 add
143                         end Binary.Emit ()
144                         
145                         stfld
146                         ldloc temp
147                 end this.EmitAssign (ec, source, false, true)
148                 
149                 Note that these two examples are what happens for ++x and x++, respectively.
150                 */
151         }
152
153         /// <summary>
154         ///   An Expression to hold a temporary value.
155         /// </summary>
156         /// <remarks>
157         ///   The LocalTemporary class is used to hold temporary values of a given
158         ///   type to "simulate" the expression semantics on property and indexer
159         ///   access whose return values are void.
160         ///
161         ///   The local temporary is used to alter the normal flow of code generation
162         ///   basically it creates a local variable, and its emit instruction generates
163         ///   code to access this value, return its address or save its value.
164         ///
165         ///   If `is_address' is true, then the value that we store is the address to the
166         ///   real value, and not the value itself. 
167         ///
168         ///   This is needed for a value type, because otherwise you just end up making a
169         ///   copy of the value on the stack and modifying it. You really need a pointer
170         ///   to the origional value so that you can modify it in that location. This
171         ///   Does not happen with a class because a class is a pointer -- so you always
172         ///   get the indirection.
173         ///
174         ///   The `is_address' stuff is really just a hack. We need to come up with a better
175         ///   way to handle it.
176         /// </remarks>
177         public class LocalTemporary : Expression, IMemoryLocation {
178                 LocalBuilder builder;
179                 bool is_address;
180                 
181                 public LocalTemporary (EmitContext ec, Type t) : this (ec, t, false) {}
182                         
183                 public LocalTemporary (EmitContext ec, Type t, bool is_address) 
184                 {
185                         type = t;
186                         eclass = ExprClass.Value;
187                         loc = Location.Null;
188                         builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (t): t);
189                         this.is_address = is_address;
190                 }
191
192                 public LocalTemporary (LocalBuilder b, Type t)
193                 {
194                         type = t;
195                         eclass = ExprClass.Value;
196                         loc = Location.Null;
197                         builder = b;
198                 }
199
200                 public void Release (EmitContext ec)
201                 {
202                         ec.FreeTemporaryLocal (builder, type);
203                         builder = null;
204                 }
205                 
206                 public override Expression DoResolve (EmitContext ec)
207                 {
208                         return this;
209                 }
210
211                 public override void Emit (EmitContext ec)
212                 {
213                         ILGenerator ig = ec.ig;
214                         
215                         ig.Emit (OpCodes.Ldloc, builder);
216                         // we need to copy from the pointer
217                         if (is_address)
218                                 LoadFromPtr (ig, type);
219                 }
220
221                 // NB: if you have `is_address' on the stack there must
222                 // be a managed pointer. Otherwise, it is the type from
223                 // the ctor.
224                 public void Store (EmitContext ec)
225                 {
226                         ILGenerator ig = ec.ig;
227                         ig.Emit (OpCodes.Stloc, builder);
228                 }
229
230                 public void AddressOf (EmitContext ec, AddressOp mode)
231                 {
232                         // if is_address, than this is just the address anyways,
233                         // so we just return this.
234                         ILGenerator ig = ec.ig;
235                                 
236                         if (is_address)
237                                 ig.Emit (OpCodes.Ldloc, builder);
238                         else
239                                 ig.Emit (OpCodes.Ldloca, builder);
240                 }
241
242                 public bool PointsToAddress {
243                         get {
244                                 return is_address;
245                         }
246                 }
247         }
248
249         /// <summary>
250         ///   The Assign node takes care of assigning the value of source into
251         ///   the expression represented by target. 
252         /// </summary>
253         public class Assign : ExpressionStatement {
254                 protected Expression target, source, real_source;
255                 protected LocalTemporary temp = null, real_temp = null;
256                 protected Assign embedded = null;
257                 protected bool is_embedded = false;
258                 protected bool must_free_temp = false;
259
260                 public Assign (Expression target, Expression source, Location l)
261                 {
262                         this.target = target;
263                         this.source = this.real_source = source;
264                         this.loc = l;
265                 }
266
267                 protected Assign (Assign embedded, Location l)
268                         : this (embedded.target, embedded.source, l)
269                 {
270                         this.is_embedded = true;
271                 }
272
273                 protected virtual Assign GetEmbeddedAssign (Location loc)
274                 {
275                         return new Assign (this, loc);
276                 }
277
278                 public Expression Target {
279                         get {
280                                 return target;
281                         }
282
283                         set {
284                                 target = value;
285                         }
286                 }
287
288                 public Expression Source {
289                         get {
290                                 return source;
291                         }
292
293                         set {
294                                 source = value;
295                         }
296                 }
297
298                 public static void error70 (EventInfo ei, Location l)
299                 {
300                         Report.Error (70, l, "The event '" + ei.Name +
301                                       "' can only appear on the left-side of a += or -= (except when" +
302                                       " used from within the type '" + ei.DeclaringType + "')");
303                 }
304
305                 //
306                 // Will return either `this' or an instance of `New'.
307                 //
308                 public override Expression DoResolve (EmitContext ec)
309                 {
310                         // Create an embedded assignment if our source is an assignment.
311                         if (source is Assign)
312                                 source = embedded = ((Assign) source).GetEmbeddedAssign (loc);
313
314                         real_source = source = source.Resolve (ec);
315                         if (source == null)
316                                 return null;
317
318                         //
319                         // This is used in an embedded assignment.
320                         // As an example, consider the statement "A = X = Y = Z".
321                         //
322                         if (is_embedded && !(source is Constant)) {
323                                 // If this is the innermost assignment (the "Y = Z" in our example),
324                                 // create a new temporary local, otherwise inherit that variable
325                                 // from our child (the "X = (Y = Z)" inherits the local from the
326                                 // "Y = Z" assignment).
327
328                                 if (embedded == null) {
329                                         if (this is CompoundAssign)
330                                                 real_temp = temp = new LocalTemporary (ec, target.Type);
331                                         else
332                                                 real_temp = temp = new LocalTemporary (ec, source.Type);
333                                 } else
334                                         temp = embedded.temp;
335
336                                 // Set the source to the new temporary variable.
337                                 // This means that the following target.ResolveLValue () will tell
338                                 // the target to read it's source value from that variable.
339                                 source = temp;
340                         }
341
342                         // If we have an embedded assignment, use the embedded assignment's temporary
343                         // local variable as source.
344                         if (embedded != null)
345                                 source = (embedded.temp != null) ? embedded.temp : embedded.source;
346
347                         target = target.ResolveLValue (ec, source);
348
349                         if (target == null)
350                                 return null;
351
352                         Type target_type = target.Type;
353                         Type source_type = real_source.Type;
354
355                         // If we're an embedded assignment, our parent will reuse our source as its
356                         // source, it won't read from our target.
357                         if (is_embedded)
358                                 type = source_type;
359                         else
360                                 type = target_type;
361                         eclass = ExprClass.Value;
362
363                         if (target is EventExpr) {
364                                 EventInfo ei = ((EventExpr) target).EventInfo;
365
366                                 Expression ml = MemberLookup (
367                                         ec, ec.ContainerType, ei.Name,
368                                         MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
369
370                                 if (ml == null) {
371                                         //
372                                         // If this is the case, then the Event does not belong 
373                                         // to this Type and so, according to the spec
374                                         // is allowed to only appear on the left hand of
375                                         // the += and -= operators
376                                         //
377                                         // Note that target will not appear as an EventExpr
378                                         // in the case it is being referenced within the same type container;
379                                         // it will appear as a FieldExpr in that case.
380                                         //
381                                         
382                                         if (!(source is BinaryDelegate)) {
383                                                 error70 (ei, loc);
384                                                 return null;
385                                         } 
386                                 }
387                         }
388
389                         if (!(target is IAssignMethod) && (target.eclass != ExprClass.EventAccess)) {
390                                 Report.Error (131, loc,
391                                               "Left hand of an assignment must be a variable, " +
392                                               "a property or an indexer");
393                                 return null;
394                         }
395
396                         if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
397                                 source.Error_UnexpectedKind ("variable or value", loc);
398                                 return null;
399                         } else if ((RootContext.Version == LanguageVersion.ISO_1) &&
400                                    (source is MethodGroupExpr)){
401                                 ((MethodGroupExpr) source).ReportUsageError ();
402                                 return null;
403
404                         }
405
406                         if (target_type == source_type){
407                         if (source is New && target_type.IsValueType &&
408                             (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
409                                 New n = (New) source;
410
411                                 if (n.SetValueTypeVariable (target))
412                                         return n;
413                                 else
414                                         return null;
415                         }
416
417                                 return this;
418                         }
419
420                         //
421                         // If this assignemnt/operator was part of a compound binary
422                         // operator, then we allow an explicit conversion, as detailed
423                         // in the spec. 
424                         //
425
426                         if (this is CompoundAssign){
427                                 CompoundAssign a = (CompoundAssign) this;
428                                 
429                                 Binary b = source as Binary;
430                                 if (b != null){
431                                         //
432                                         // 1. if the source is explicitly convertible to the
433                                         //    target_type
434                                         //
435                                         
436                                         source = Convert.ExplicitConversion (ec, source, target_type, loc);
437                                         if (source == null){
438                                                 Convert.Error_CannotImplicitConversion (loc, source_type, target_type);
439                                                 return null;
440                                         }
441                                 
442                                         //
443                                         // 2. and the original right side is implicitly convertible to
444                                         // the type of target
445                                         //
446                                         if (Convert.ImplicitStandardConversionExists (ec, a.original_source, target_type))
447                                                 return this;
448
449                                         //
450                                         // In the spec 2.4 they added: or if type of the target is int
451                                         // and the operator is a shift operator...
452                                         //
453                                         if (source_type == TypeManager.int32_type &&
454                                             (b.Oper == Binary.Operator.LeftShift || b.Oper == Binary.Operator.RightShift))
455                                                 return this;
456
457                                         Convert.Error_CannotImplicitConversion (loc, a.original_source.Type, target_type);
458                                         return null;
459                                 }
460                         }
461
462                         source = Convert.ImplicitConversionRequired (ec, source, target_type, loc);
463                         if (source == null)
464                                 return null;
465
466                         // If we're an embedded assignment, we need to create a new temporary variable
467                         // for the converted value.  Our parent will use this new variable as its source.
468                         // The same applies when we have an embedded assignment - in this case, we need
469                         // to convert our embedded assignment's temporary local variable to the correct
470                         // type and store it in a new temporary local.
471                         if (is_embedded || embedded != null) {
472                                 type = target_type;
473                                 temp = new LocalTemporary (ec, type);
474                                 must_free_temp = true;
475                         }
476                         
477                         return this;
478                 }
479
480                 Expression EmitEmbedded (EmitContext ec)
481                 {
482                         // Emit an embedded assignment.
483
484                         if (real_temp != null) {
485                                 // If we're the innermost assignment, `real_source' is the right-hand
486                                 // expression which gets assigned to all the variables left of it.
487                                 // Emit this expression and store its result in real_temp.
488                                 real_source.Emit (ec);
489                                 real_temp.Store (ec);
490                         }
491
492                         if (embedded != null)
493                                 embedded.EmitEmbedded (ec);
494
495                         // This happens when we've done a type conversion, in this case source will be
496                         // the expression which does the type conversion from real_temp.
497                         // So emit it and store the result in temp; this is the var which will be read
498                         // by our parent.
499                         if (temp != real_temp) {
500                                 source.Emit (ec);
501                                 temp.Store (ec);
502                         }
503
504                         Expression temp_source = (temp != null) ? temp : source;
505                         ((IAssignMethod) target).EmitAssign (ec, temp_source, false, false);
506                         return temp_source;
507                 }
508
509                 void ReleaseEmbedded (EmitContext ec)
510                 {
511                         if (embedded != null)
512                                 embedded.ReleaseEmbedded (ec);
513
514                         if (real_temp != null)
515                                 real_temp.Release (ec);
516
517                         if (must_free_temp)
518                                 temp.Release (ec);
519                 }
520
521                 void Emit (EmitContext ec, bool is_statement)
522                 {
523                         if (target is EventExpr) {
524                                 ((EventExpr) target).EmitAddOrRemove (ec, source);
525                                 return;
526                         }
527                         
528                         IAssignMethod am = (IAssignMethod) target;
529
530                         Expression temp_source;
531                         if (embedded != null) {
532                                 temp_source = embedded.EmitEmbedded (ec);
533
534                                 if (temp != null) {
535                                         source.Emit (ec);
536                                         temp.Store (ec);
537                                         temp_source = temp;
538                                 }
539                         } else
540                                 temp_source = source;
541                 
542                         am.EmitAssign (ec, temp_source, !is_statement, this is CompoundAssign);
543                                 
544                         if (embedded != null) {
545                                 if (temp != null)
546                                         temp.Release (ec);
547                                 embedded.ReleaseEmbedded (ec);
548                         }
549                 }
550                 
551                 public override void Emit (EmitContext ec)
552                 {
553                         Emit (ec, false);
554                 }
555
556                 public override void EmitStatement (EmitContext ec)
557                 {
558                         Emit (ec, true);
559                 }
560         }
561
562         
563         //
564         // This class is used for compound assignments.  
565         //
566         class CompoundAssign : Assign {
567                 Binary.Operator op;
568                 public Expression original_source;
569                 
570                 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
571                         : base (target, source, l)
572                 {
573                         original_source = source;
574                         this.op = op;
575                 }
576
577                 protected CompoundAssign (CompoundAssign embedded, Location l)
578                         : this (embedded.op, embedded.target, embedded.source, l)
579                 {
580                         this.is_embedded = true;
581                 }
582
583                 protected override Assign GetEmbeddedAssign (Location loc)
584                 {
585                         return new CompoundAssign (this, loc);
586                 }
587
588                 public Expression ResolveSource (EmitContext ec)
589                 {
590                         return original_source.Resolve (ec);
591                 }
592
593                 public override Expression DoResolve (EmitContext ec)
594                 {
595                         original_source = original_source.Resolve (ec);
596                         if (original_source == null)
597                                 return null;
598
599                         target = target.Resolve (ec);
600                         if (target == null)
601                                 return null;
602                         
603                         //
604                         // Only now we can decouple the original source/target
605                         // into a tree, to guarantee that we do not have side
606                         // effects.
607                         //
608                         source = new Binary (op, target, original_source, loc);
609                         return base.DoResolve (ec);
610                 }
611         }
612 }
613
614
615
616