2 // assign.cs: Assignments.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Martin Baulig (martin@gnome.org)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
11 using System.Reflection;
12 using System.Reflection.Emit;
14 namespace Mono.CSharp {
17 /// This interface is implemented by expressions that can be assigned to.
20 /// This interface is implemented by Expressions whose values can not
21 /// store the result on the top of the stack.
23 /// Expressions implementing this (Properties, Indexers and Arrays) would
24 /// perform an assignment of the Expression "source" into its final
27 /// No values on the top of the stack are expected to be left by
28 /// invoking this method.
30 public interface IAssignMethod {
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
36 void Emit (EmitContext ec, bool leave_copy);
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.
46 void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load);
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.
52 For compound assignments it gets complicated.
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:
58 this.EmitAssign (ec, source, false, true) ->
61 this.Emit (ec, false); ->
62 end this.Emit (ec, false); ->
65 end this.EmitAssign (ec, source, false, true)
68 When prepare_for_load is true, EmitAssign emits a `token' on the stack that
69 Emit will use for its state.
71 Let's take FieldExpr as an example. assume we are emitting f ().y += 1;
73 Here is the call tree again. This time, each call is annotated with the IL
76 this.EmitAssign (ec, source, false, true)
81 this.Emit (ec, false);
83 end this.Emit (ec, false);
93 end this.EmitAssign (ec, source, false, true)
96 1) EmitAssign left a token on the stack. It was the result of f ().
97 2) This token was used by Emit
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
103 this.EmitAssign (ec, source, true, true)
108 this.Emit (ec, false);
110 end this.Emit (ec, false);
123 end this.EmitAssign (ec, source, true, true)
125 And with it true in Emit
127 this.EmitAssign (ec, source, false, true)
132 this.Emit (ec, true);
136 end this.Emit (ec, true);
147 end this.EmitAssign (ec, source, false, true)
149 Note that these two examples are what happens for ++x and x++, respectively.
154 /// An Expression to hold a temporary value.
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.
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.
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.
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.
174 /// The `is_address' stuff is really just a hack. We need to come up with a better
175 /// way to handle it.
177 public class LocalTemporary : Expression, IMemoryLocation {
178 LocalBuilder builder;
181 public LocalTemporary (EmitContext ec, Type t) : this (ec, t, false) {}
183 public LocalTemporary (EmitContext ec, Type t, bool is_address)
186 eclass = ExprClass.Value;
188 builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (t): t);
189 this.is_address = is_address;
192 public LocalTemporary (LocalBuilder b, Type t)
195 eclass = ExprClass.Value;
200 public void Release (EmitContext ec)
202 ec.FreeTemporaryLocal (builder, type);
206 public override Expression DoResolve (EmitContext ec)
211 public override void Emit (EmitContext ec)
213 ILGenerator ig = ec.ig;
215 ig.Emit (OpCodes.Ldloc, builder);
216 // we need to copy from the pointer
218 LoadFromPtr (ig, type);
221 // NB: if you have `is_address' on the stack there must
222 // be a managed pointer. Otherwise, it is the type from
224 public void Store (EmitContext ec)
226 ILGenerator ig = ec.ig;
227 ig.Emit (OpCodes.Stloc, builder);
230 public void AddressOf (EmitContext ec, AddressOp mode)
232 // if is_address, than this is just the address anyways,
233 // so we just return this.
234 ILGenerator ig = ec.ig;
237 ig.Emit (OpCodes.Ldloc, builder);
239 ig.Emit (OpCodes.Ldloca, builder);
242 public bool PointsToAddress {
250 /// The Assign node takes care of assigning the value of source into
251 /// the expression represented by target.
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;
260 public Assign (Expression target, Expression source, Location l)
262 this.target = target;
263 this.source = this.real_source = source;
267 protected Assign (Assign embedded, Location l)
268 : this (embedded.target, embedded.source, l)
270 this.is_embedded = true;
273 protected virtual Assign GetEmbeddedAssign (Location loc)
275 return new Assign (this, loc);
278 public Expression Target {
288 public Expression Source {
298 public static void error70 (EventInfo ei, Location l)
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 + "')");
306 // Will return either `this' or an instance of `New'.
308 public override Expression DoResolve (EmitContext ec)
310 // Create an embedded assignment if our source is an assignment.
311 if (source is Assign)
312 source = embedded = ((Assign) source).GetEmbeddedAssign (loc);
314 real_source = source = source.Resolve (ec);
319 // This is used in an embedded assignment.
320 // As an example, consider the statement "A = X = Y = Z".
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).
328 if (embedded == null) {
329 if (this is CompoundAssign)
330 real_temp = temp = new LocalTemporary (ec, target.Type);
332 real_temp = temp = new LocalTemporary (ec, source.Type);
334 temp = embedded.temp;
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.
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;
347 target = target.ResolveLValue (ec, source);
352 Type target_type = target.Type;
353 Type source_type = real_source.Type;
355 // If we're an embedded assignment, our parent will reuse our source as its
356 // source, it won't read from our target.
361 eclass = ExprClass.Value;
363 if (target is EventExpr) {
364 EventInfo ei = ((EventExpr) target).EventInfo;
366 Expression ml = MemberLookup (
367 ec, ec.ContainerType, ei.Name,
368 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
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
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.
382 if (!(source is BinaryDelegate)) {
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");
396 if (source is New && target_type.IsValueType &&
397 (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
398 New n = (New) source;
400 if (n.SetValueTypeVariable (target))
406 if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
407 source.Error_UnexpectedKind ("variable or value");
409 } else if (!RootContext.V2 && (source is MethodGroupExpr)){
410 ((MethodGroupExpr) source).ReportUsageError ();
415 if (target_type == source_type)
419 // If this assignemnt/operator was part of a compound binary
420 // operator, then we allow an explicit conversion, as detailed
424 if (this is CompoundAssign){
425 CompoundAssign a = (CompoundAssign) this;
427 Binary b = source as Binary;
430 // 1. if the source is explicitly convertible to the
434 source = Convert.ExplicitConversion (ec, source, target_type, loc);
436 Convert.Error_CannotImplicitConversion (loc, source_type, target_type);
441 // 2. and the original right side is implicitly convertible to
442 // the type of target
444 if (Convert.ImplicitStandardConversionExists (a.original_source, target_type))
448 // In the spec 2.4 they added: or if type of the target is int
449 // and the operator is a shift operator...
451 if (source_type == TypeManager.int32_type &&
452 (b.Oper == Binary.Operator.LeftShift || b.Oper == Binary.Operator.RightShift))
455 Convert.Error_CannotImplicitConversion (loc, a.original_source.Type, target_type);
460 source = Convert.ImplicitConversionRequired (ec, source, target_type, loc);
464 // If we're an embedded assignment, we need to create a new temporary variable
465 // for the converted value. Our parent will use this new variable as its source.
466 // The same applies when we have an embedded assignment - in this case, we need
467 // to convert our embedded assignment's temporary local variable to the correct
468 // type and store it in a new temporary local.
469 if (is_embedded || embedded != null) {
471 temp = new LocalTemporary (ec, type);
472 must_free_temp = true;
478 Expression EmitEmbedded (EmitContext ec)
480 // Emit an embedded assignment.
482 if (real_temp != null) {
483 // If we're the innermost assignment, `real_source' is the right-hand
484 // expression which gets assigned to all the variables left of it.
485 // Emit this expression and store its result in real_temp.
486 real_source.Emit (ec);
487 real_temp.Store (ec);
490 if (embedded != null)
491 embedded.EmitEmbedded (ec);
493 // This happens when we've done a type conversion, in this case source will be
494 // the expression which does the type conversion from real_temp.
495 // So emit it and store the result in temp; this is the var which will be read
497 if (temp != real_temp) {
502 Expression temp_source = (temp != null) ? temp : source;
503 ((IAssignMethod) target).EmitAssign (ec, temp_source, false, false);
507 void ReleaseEmbedded (EmitContext ec)
509 if (embedded != null)
510 embedded.ReleaseEmbedded (ec);
512 if (real_temp != null)
513 real_temp.Release (ec);
519 void Emit (EmitContext ec, bool is_statement)
521 if (target is EventExpr) {
522 ((EventExpr) target).EmitAddOrRemove (ec, source);
526 IAssignMethod am = (IAssignMethod) target;
528 Expression temp_source;
529 if (embedded != null) {
530 temp_source = embedded.EmitEmbedded (ec);
538 temp_source = source;
540 am.EmitAssign (ec, temp_source, !is_statement, this is CompoundAssign);
542 if (embedded != null) {
545 embedded.ReleaseEmbedded (ec);
549 public override void Emit (EmitContext ec)
554 public override void EmitStatement (EmitContext ec)
562 // This class is used for compound assignments.
564 class CompoundAssign : Assign {
566 public Expression original_source;
568 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
569 : base (target, source, l)
571 original_source = source;
575 protected CompoundAssign (CompoundAssign embedded, Location l)
576 : this (embedded.op, embedded.target, embedded.source, l)
578 this.is_embedded = true;
581 protected override Assign GetEmbeddedAssign (Location loc)
583 return new CompoundAssign (this, loc);
586 public Expression ResolveSource (EmitContext ec)
588 return original_source.Resolve (ec);
591 public override Expression DoResolve (EmitContext ec)
593 original_source = original_source.Resolve (ec);
594 if (original_source == null)
597 target = target.Resolve (ec);
602 // Only now we can decouple the original source/target
603 // into a tree, to guarantee that we do not have side
606 source = new Binary (op, target, original_source, loc);
607 return base.DoResolve (ec);