2 // assign.cs: Assignments.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001, 2002 Ximian, Inc.
10 using System.Reflection;
11 using System.Reflection.Emit;
13 namespace Mono.CSharp {
16 /// This interface is implemented by expressions that can be assigned to.
19 /// This interface is implemented by Expressions whose values can not
20 /// store the result on the top of the stack.
22 /// Expressions implementing this (Properties, Indexers and Arrays) would
23 /// perform an assignment of the Expression "source" into its final
26 /// No values on the top of the stack are expected to be left by
27 /// invoking this method.
29 public interface IAssignMethod {
31 // This method will emit the code for the actual assignment
33 void EmitAssign (EmitContext ec, Expression source);
36 // This method is invoked before any code generation takes
37 // place, and it is a mechanism to inform that the expression
38 // will be invoked more than once, and that the method should
39 // use temporary values to avoid having side effects
41 // Example: a [ g () ] ++
43 void CacheTemporaries (EmitContext ec);
47 /// An Expression to hold a temporary value.
50 /// The LocalTemporary class is used to hold temporary values of a given
51 /// type to "simulate" the expression semantics on property and indexer
52 /// access whose return values are void.
54 /// The local temporary is used to alter the normal flow of code generation
55 /// basically it creates a local variable, and its emit instruction generates
56 /// code to access this value, return its address or save its value.
58 public class LocalTemporary : Expression, IMemoryLocation {
61 public LocalTemporary (EmitContext ec, Type t)
64 eclass = ExprClass.Value;
65 builder = ec.GetTemporaryStorage (t);
68 public void Release (EmitContext ec)
70 ec.FreeTemporaryStorage (builder);
74 public LocalTemporary (LocalBuilder b, Type t)
77 eclass = ExprClass.Value;
81 public override Expression DoResolve (EmitContext ec)
86 public override void Emit (EmitContext ec)
88 ec.ig.Emit (OpCodes.Ldloc, builder);
91 public void Store (EmitContext ec)
93 ec.ig.Emit (OpCodes.Stloc, builder);
96 public void AddressOf (EmitContext ec, AddressOp mode)
98 ec.ig.Emit (OpCodes.Ldloca, builder);
103 /// The Assign node takes care of assigning the value of source into
104 /// the expression represented by target.
106 public class Assign : ExpressionStatement {
107 protected Expression target, source;
110 public Assign (Expression target, Expression source, Location l)
112 this.target = target;
113 this.source = source;
117 public Expression Target {
127 public Expression Source {
137 public static void error70 (EventInfo ei, Location l)
139 Report.Error (70, l, "The event '" + ei.Name +
140 "' can only appear on the left-side of a += or -= (except when" +
141 " used from within the type '" + ei.DeclaringType + "')");
145 // Will return either `this' or an instance of `New'.
147 public override Expression DoResolve (EmitContext ec)
149 source = source.Resolve (ec);
153 target = target.ResolveLValue (ec, source);
158 Type target_type = target.Type;
159 Type source_type = source.Type;
162 eclass = ExprClass.Value;
165 // If we are doing a property assignment, then
166 // set the `value' field on the property, and Resolve
169 if (target is PropertyExpr){
170 PropertyExpr property_assign = (PropertyExpr) target;
172 if (source_type != target_type){
173 source = ConvertImplicitRequired (ec, source, target_type, l);
179 // FIXME: Maybe handle this in the LValueResolve
181 if (!property_assign.VerifyAssignable ())
187 if (target is IndexerAccess){
188 if (source_type != target_type){
189 source = ConvertImplicitRequired (ec, source, target_type, l);
197 if (target is EventExpr) {
200 EventInfo ei = ((EventExpr) target).EventInfo;
203 Expression ml = MemberLookup (
204 ec, ec.ContainerType, ei.Name,
205 MemberTypes.Event, AllBindingFlags, l);
209 // If this is the case, then the Event does not belong
210 // to this TypeContainer and so, according to the spec
211 // is allowed to only appear on the left hand of
212 // the += and -= operators
214 // Note that if target will not appear as an EventExpr
215 // in the case it is being referenced within the same type container;
216 // it will appear as a FieldExpr in that case.
219 if (!(source is Binary)) {
223 tmp = ((Binary) source);
224 if (tmp.Oper != Binary.Operator.Addition &&
225 tmp.Oper != Binary.Operator.Subtraction) {
233 if (source is New && target_type.IsSubclassOf (TypeManager.value_type)){
234 New n = (New) source;
236 n.ValueTypeVariable = target;
240 if (target.eclass != ExprClass.Variable && target.eclass != ExprClass.EventAccess){
241 Report.Error (131, l,
242 "Left hand of an assignment must be a variable, " +
243 "a property or an indexer");
247 if (target_type == source_type)
251 // If this assignemnt/operator was part of a compound binary
252 // operator, then we allow an explicit conversion, as detailed
256 if (this is CompoundAssign){
257 CompoundAssign a = (CompoundAssign) this;
259 Binary b = source as Binary;
260 if (b != null && b.IsBuiltinOperator){
262 // 1. if the source is explicitly convertible to the
266 source = ConvertExplicit (ec, source, target_type, l);
268 Error_CannotConvertImplicit (l, source_type, target_type);
273 // 2. and the original right side is implicitly convertible to
274 // the type of target_type.
276 if (StandardConversionExists (a.original_source, target_type))
279 Error_CannotConvertImplicit (l, a.original_source.Type, target_type);
284 source = ConvertImplicitRequired (ec, source, target_type, l);
291 void Emit (EmitContext ec, bool is_statement)
293 if (target is EventExpr) {
294 ((EventExpr) target).EmitAddOrRemove (ec, source);
299 // FIXME! We need a way to "probe" if the process can
300 // just use `dup' to propagate the result
302 IAssignMethod am = (IAssignMethod) target;
304 if (this is CompoundAssign){
305 am.CacheTemporaries (ec);
309 am.EmitAssign (ec, source);
311 LocalTemporary tempo;
313 tempo = new LocalTemporary (ec, source.Type);
317 am.EmitAssign (ec, tempo);
323 public override void Emit (EmitContext ec)
328 public override void EmitStatement (EmitContext ec)
336 // This class is used for compound assignments.
338 class CompoundAssign : Assign {
340 public Expression original_source;
342 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
343 : base (target, source, l)
345 original_source = source;
349 public Expression ResolveSource (EmitContext ec)
351 return original_source.Resolve (ec);
354 public override Expression DoResolve (EmitContext ec)
356 target = target.ResolveLValue (ec, source);
360 original_source = original_source.Resolve (ec);
361 if (original_source == null)
365 // Only now we can decouple the original source/target
366 // into a tree, to guarantee that we do not have side
369 source = new Binary (op, target, original_source, l);
370 return base.DoResolve (ec);