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 method will emit the code for the actual assignment
34 void EmitAssign (EmitContext ec, Expression source);
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
42 // Example: a [ g () ] ++
44 void CacheTemporaries (EmitContext ec);
48 /// An Expression to hold a temporary value.
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.
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.
59 public class LocalTemporary : Expression, IMemoryLocation {
62 public LocalTemporary (EmitContext ec, Type t)
65 eclass = ExprClass.Value;
67 builder = ec.GetTemporaryLocal (t);
70 public void Release (EmitContext ec)
72 ec.FreeTemporaryLocal (builder, type);
76 public LocalTemporary (LocalBuilder b, Type t)
79 eclass = ExprClass.Value;
84 public override Expression DoResolve (EmitContext ec)
89 public override void Emit (EmitContext ec)
91 ec.ig.Emit (OpCodes.Ldloc, builder);
94 public void Store (EmitContext ec)
96 ec.ig.Emit (OpCodes.Stloc, builder);
99 public void AddressOf (EmitContext ec, AddressOp mode)
101 ec.ig.Emit (OpCodes.Ldloca, builder);
106 /// The Assign node takes care of assigning the value of source into
107 /// the expression represented by target.
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;
116 public Assign (Expression target, Expression source, Location l)
118 this.target = target;
119 this.source = this.real_source = source;
123 protected Assign (Assign embedded, Location l)
124 : this (embedded.target, embedded.source, l)
126 this.is_embedded = true;
129 protected virtual Assign GetEmbeddedAssign (Location loc)
131 return new Assign (this, loc);
134 public Expression Target {
144 public Expression Source {
154 public static void error70 (EventInfo ei, Location l)
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 + "')");
162 // Will return either `this' or an instance of `New'.
164 public override Expression DoResolve (EmitContext ec)
166 // Create an embedded assignment if our source is an assignment.
167 if (source is Assign)
168 source = embedded = ((Assign) source).GetEmbeddedAssign (loc);
170 real_source = source = source.Resolve (ec);
175 // This is used in an embedded assignment.
176 // As an example, consider the statement "A = X = Y = Z".
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).
184 if (embedded == null) {
185 if (this is CompoundAssign)
186 real_temp = temp = new LocalTemporary (ec, target.Type);
188 real_temp = temp = new LocalTemporary (ec, source.Type);
190 temp = embedded.temp;
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.
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;
203 target = target.ResolveLValue (ec, source);
208 Type target_type = target.Type;
209 Type source_type = real_source.Type;
211 // If we're an embedded assignment, our parent will reuse our source as its
212 // source, it won't read from our target.
217 eclass = ExprClass.Value;
219 if (target is EventExpr) {
220 EventInfo ei = ((EventExpr) target).EventInfo;
222 Expression ml = MemberLookup (
223 ec, ec.ContainerType, ei.Name,
224 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
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
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.
238 if (!(source is BinaryDelegate)) {
245 if (source is New && target_type.IsValueType &&
246 (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
247 New n = (New) source;
249 if (n.SetValueTypeVariable (target))
255 if (!(target is IAssignMethod) && (target.eclass != ExprClass.EventAccess)) {
256 Report.Error (131, loc,
257 "Left hand of an assignment must be a variable, " +
258 "a property or an indexer");
262 if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
263 source.Error_UnexpectedKind ("variable or value");
265 } else if (!RootContext.V2 && (source is MethodGroupExpr)){
266 ((MethodGroupExpr) source).ReportUsageError ();
271 if (target_type == source_type)
275 // If this assignemnt/operator was part of a compound binary
276 // operator, then we allow an explicit conversion, as detailed
280 if (this is CompoundAssign){
281 CompoundAssign a = (CompoundAssign) this;
283 Binary b = source as Binary;
286 // 1. if the source is explicitly convertible to the
290 source = Convert.ExplicitConversion (ec, source, target_type, loc);
292 Convert.Error_CannotImplicitConversion (loc, source_type, target_type);
297 // 2. and the original right side is implicitly convertible to
298 // the type of target_type.
300 if (Convert.ImplicitStandardConversionExists (a.original_source, target_type))
303 Convert.Error_CannotImplicitConversion (loc, a.original_source.Type, target_type);
308 source = Convert.ImplicitConversionRequired (ec, source, target_type, loc);
312 // If we're an embedded assignment, we need to create a new temporary variable
313 // for the converted value. Our parent will use this new variable as its source.
314 // The same applies when we have an embedded assignment - in this case, we need
315 // to convert our embedded assignment's temporary local variable to the correct
316 // type and store it in a new temporary local.
317 if (is_embedded || embedded != null) {
319 temp = new LocalTemporary (ec, type);
320 must_free_temp = true;
326 Expression EmitEmbedded (EmitContext ec)
328 // Emit an embedded assignment.
330 if (real_temp != null) {
331 // If we're the innermost assignment, `real_source' is the right-hand
332 // expression which gets assigned to all the variables left of it.
333 // Emit this expression and store its result in real_temp.
334 real_source.Emit (ec);
335 real_temp.Store (ec);
338 if (embedded != null)
339 embedded.EmitEmbedded (ec);
341 // This happens when we've done a type conversion, in this case source will be
342 // the expression which does the type conversion from real_temp.
343 // So emit it and store the result in temp; this is the var which will be read
345 if (temp != real_temp) {
350 Expression temp_source = (temp != null) ? temp : source;
351 ((IAssignMethod) target).EmitAssign (ec, temp_source);
355 void ReleaseEmbedded (EmitContext ec)
357 if (embedded != null)
358 embedded.ReleaseEmbedded (ec);
360 if (real_temp != null)
361 real_temp.Release (ec);
367 void Emit (EmitContext ec, bool is_statement)
369 if (target is EventExpr) {
370 ((EventExpr) target).EmitAddOrRemove (ec, source);
374 bool use_temporaries = false;
377 // FIXME! We need a way to "probe" if the process can
378 // just use `dup' to propagate the result
380 IAssignMethod am = (IAssignMethod) target;
382 if (this is CompoundAssign)
383 am.CacheTemporaries (ec);
386 use_temporaries = true;
388 Expression temp_source;
389 if (embedded != null) {
390 temp_source = embedded.EmitEmbedded (ec);
398 temp_source = source;
400 if (use_temporaries){
402 // Doing this for every path is too expensive
403 // I wonder if we can work around this and have a less
406 LocalTemporary tempo;
408 tempo = new LocalTemporary (ec, source.Type);
410 temp_source.Emit (ec);
412 am.EmitAssign (ec, tempo);
418 am.EmitAssign (ec, temp_source);
421 if (embedded != null) {
424 embedded.ReleaseEmbedded (ec);
428 public override void Emit (EmitContext ec)
433 public override void EmitStatement (EmitContext ec)
441 // This class is used for compound assignments.
443 class CompoundAssign : Assign {
445 public Expression original_source;
447 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
448 : base (target, source, l)
450 original_source = source;
454 protected CompoundAssign (CompoundAssign embedded, Location l)
455 : this (embedded.op, embedded.target, embedded.source, l)
457 this.is_embedded = true;
460 protected override Assign GetEmbeddedAssign (Location loc)
462 return new CompoundAssign (this, loc);
465 public Expression ResolveSource (EmitContext ec)
467 return original_source.Resolve (ec);
470 public override Expression DoResolve (EmitContext ec)
472 original_source = original_source.Resolve (ec);
473 if (original_source == null)
476 target = target.Resolve (ec);
481 // Only now we can decouple the original source/target
482 // into a tree, to guarantee that we do not have side
485 source = new Binary (op, target, original_source, loc);
486 return base.DoResolve (ec);