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 /// If `is_address' is true, then the value that we store is the address to the
60 /// real value, and not the value itself.
62 /// This is needed for a value type, because otherwise you just end up making a
63 /// copy of the value on the stack and modifying it. You really need a pointer
64 /// to the origional value so that you can modify it in that location. This
65 /// Does not happen with a class because a class is a pointer -- so you always
66 /// get the indirection.
68 /// The `is_address' stuff is really just a hack. We need to come up with a better
71 public class LocalTemporary : Expression, IMemoryLocation {
75 public LocalTemporary (EmitContext ec, Type t) : this (ec, t, false) {}
77 public LocalTemporary (EmitContext ec, Type t, bool is_address)
80 eclass = ExprClass.Value;
82 builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (t): t);
83 this.is_address = is_address;
86 public LocalTemporary (LocalBuilder b, Type t)
89 eclass = ExprClass.Value;
94 public void Release (EmitContext ec)
96 ec.FreeTemporaryLocal (builder, type);
100 public override Expression DoResolve (EmitContext ec)
105 public override void Emit (EmitContext ec)
107 ILGenerator ig = ec.ig;
109 ig.Emit (OpCodes.Ldloc, builder);
110 // we need to copy from the pointer
112 LoadFromPtr (ig, type);
115 // NB: if you have `is_address' on the stack there must
116 // be a managed pointer. Otherwise, it is the type from
118 public void Store (EmitContext ec)
120 ILGenerator ig = ec.ig;
121 ig.Emit (OpCodes.Stloc, builder);
124 public void AddressOf (EmitContext ec, AddressOp mode)
126 // if is_address, than this is just the address anyways,
127 // so we just return this.
128 ILGenerator ig = ec.ig;
131 ig.Emit (OpCodes.Ldloc, builder);
133 ig.Emit (OpCodes.Ldloca, builder);
136 public bool PointsToAddress {
144 /// The Assign node takes care of assigning the value of source into
145 /// the expression represented by target.
147 public class Assign : ExpressionStatement {
148 protected Expression target, source, real_source;
149 protected LocalTemporary temp = null, real_temp = null;
150 protected Assign embedded = null;
151 protected bool is_embedded = false;
152 protected bool must_free_temp = false;
154 public Assign (Expression target, Expression source, Location l)
156 this.target = target;
157 this.source = this.real_source = source;
161 protected Assign (Assign embedded, Location l)
162 : this (embedded.target, embedded.source, l)
164 this.is_embedded = true;
167 protected virtual Assign GetEmbeddedAssign (Location loc)
169 return new Assign (this, loc);
172 public Expression Target {
182 public Expression Source {
192 public static void error70 (EventInfo ei, Location l)
194 Report.Error (70, l, "The event '" + ei.Name +
195 "' can only appear on the left-side of a += or -= (except when" +
196 " used from within the type '" + ei.DeclaringType + "')");
200 // Will return either `this' or an instance of `New'.
202 public override Expression DoResolve (EmitContext ec)
204 // Create an embedded assignment if our source is an assignment.
205 if (source is Assign)
206 source = embedded = ((Assign) source).GetEmbeddedAssign (loc);
208 real_source = source = source.Resolve (ec);
213 // This is used in an embedded assignment.
214 // As an example, consider the statement "A = X = Y = Z".
216 if (is_embedded && !(source is Constant)) {
217 // If this is the innermost assignment (the "Y = Z" in our example),
218 // create a new temporary local, otherwise inherit that variable
219 // from our child (the "X = (Y = Z)" inherits the local from the
220 // "Y = Z" assignment).
222 if (embedded == null) {
223 if (this is CompoundAssign)
224 real_temp = temp = new LocalTemporary (ec, target.Type);
226 real_temp = temp = new LocalTemporary (ec, source.Type);
228 temp = embedded.temp;
230 // Set the source to the new temporary variable.
231 // This means that the following target.ResolveLValue () will tell
232 // the target to read it's source value from that variable.
236 // If we have an embedded assignment, use the embedded assignment's temporary
237 // local variable as source.
238 if (embedded != null)
239 source = (embedded.temp != null) ? embedded.temp : embedded.source;
241 target = target.ResolveLValue (ec, source);
246 Type target_type = target.Type;
247 Type source_type = real_source.Type;
249 // If we're an embedded assignment, our parent will reuse our source as its
250 // source, it won't read from our target.
255 eclass = ExprClass.Value;
257 if (target is EventExpr) {
258 EventInfo ei = ((EventExpr) target).EventInfo;
260 Expression ml = MemberLookup (
261 ec, ec.ContainerType, ei.Name,
262 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
266 // If this is the case, then the Event does not belong
267 // to this Type and so, according to the spec
268 // is allowed to only appear on the left hand of
269 // the += and -= operators
271 // Note that target will not appear as an EventExpr
272 // in the case it is being referenced within the same type container;
273 // it will appear as a FieldExpr in that case.
276 if (!(source is BinaryDelegate)) {
283 if (!(target is IAssignMethod) && (target.eclass != ExprClass.EventAccess)) {
284 Report.Error (131, loc,
285 "Left hand of an assignment must be a variable, " +
286 "a property or an indexer");
290 if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
291 source.Error_UnexpectedKind ("variable or value");
293 } else if (!RootContext.V2 && (source is MethodGroupExpr)){
294 ((MethodGroupExpr) source).ReportUsageError ();
299 if (target_type == source_type){
300 if (source is New && target_type.IsValueType &&
301 (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
302 New n = (New) source;
304 if (n.SetValueTypeVariable (target))
314 // If this assignemnt/operator was part of a compound binary
315 // operator, then we allow an explicit conversion, as detailed
319 if (this is CompoundAssign){
320 CompoundAssign a = (CompoundAssign) this;
322 Binary b = source as Binary;
325 // 1. if the source is explicitly convertible to the
329 source = Convert.ExplicitConversion (ec, source, target_type, loc);
331 Convert.Error_CannotImplicitConversion (loc, source_type, target_type);
336 // 2. and the original right side is implicitly convertible to
337 // the type of target
339 if (Convert.ImplicitStandardConversionExists (a.original_source, target_type))
343 // In the spec 2.4 they added: or if type of the target is int
344 // and the operator is a shift operator...
346 if (source_type == TypeManager.int32_type &&
347 (b.Oper == Binary.Operator.LeftShift || b.Oper == Binary.Operator.RightShift))
350 Convert.Error_CannotImplicitConversion (loc, a.original_source.Type, target_type);
355 source = Convert.ImplicitConversionRequired (ec, source, target_type, loc);
359 // If we're an embedded assignment, we need to create a new temporary variable
360 // for the converted value. Our parent will use this new variable as its source.
361 // The same applies when we have an embedded assignment - in this case, we need
362 // to convert our embedded assignment's temporary local variable to the correct
363 // type and store it in a new temporary local.
364 if (is_embedded || embedded != null) {
366 temp = new LocalTemporary (ec, type);
367 must_free_temp = true;
373 Expression EmitEmbedded (EmitContext ec)
375 // Emit an embedded assignment.
377 if (real_temp != null) {
378 // If we're the innermost assignment, `real_source' is the right-hand
379 // expression which gets assigned to all the variables left of it.
380 // Emit this expression and store its result in real_temp.
381 real_source.Emit (ec);
382 real_temp.Store (ec);
385 if (embedded != null)
386 embedded.EmitEmbedded (ec);
388 // This happens when we've done a type conversion, in this case source will be
389 // the expression which does the type conversion from real_temp.
390 // So emit it and store the result in temp; this is the var which will be read
392 if (temp != real_temp) {
397 Expression temp_source = (temp != null) ? temp : source;
398 ((IAssignMethod) target).EmitAssign (ec, temp_source);
402 void ReleaseEmbedded (EmitContext ec)
404 if (embedded != null)
405 embedded.ReleaseEmbedded (ec);
407 if (real_temp != null)
408 real_temp.Release (ec);
414 void Emit (EmitContext ec, bool is_statement)
416 if (target is EventExpr) {
417 ((EventExpr) target).EmitAddOrRemove (ec, source);
421 bool use_temporaries = false;
424 // FIXME! We need a way to "probe" if the process can
425 // just use `dup' to propagate the result
427 IAssignMethod am = (IAssignMethod) target;
429 if (this is CompoundAssign)
430 am.CacheTemporaries (ec);
433 use_temporaries = true;
435 Expression temp_source;
436 if (embedded != null) {
437 temp_source = embedded.EmitEmbedded (ec);
445 temp_source = source;
447 if (use_temporaries){
449 // Doing this for every path is too expensive
450 // I wonder if we can work around this and have a less
453 LocalTemporary tempo;
455 tempo = new LocalTemporary (ec, source.Type);
457 temp_source.Emit (ec);
459 am.EmitAssign (ec, tempo);
465 am.EmitAssign (ec, temp_source);
468 if (embedded != null) {
471 embedded.ReleaseEmbedded (ec);
475 public override void Emit (EmitContext ec)
480 public override void EmitStatement (EmitContext ec)
488 // This class is used for compound assignments.
490 class CompoundAssign : Assign {
492 public Expression original_source;
494 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
495 : base (target, source, l)
497 original_source = source;
501 protected CompoundAssign (CompoundAssign embedded, Location l)
502 : this (embedded.op, embedded.target, embedded.source, l)
504 this.is_embedded = true;
507 protected override Assign GetEmbeddedAssign (Location loc)
509 return new CompoundAssign (this, loc);
512 public Expression ResolveSource (EmitContext ec)
514 return original_source.Resolve (ec);
517 public override Expression DoResolve (EmitContext ec)
519 original_source = original_source.Resolve (ec);
520 if (original_source == null)
523 target = target.Resolve (ec);
528 // Only now we can decouple the original source/target
529 // into a tree, to guarantee that we do not have side
532 source = new Binary (op, target, original_source, loc);
533 return base.DoResolve (ec);