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)
190 if (target is EventExpr) {
193 EventInfo ei = ((EventExpr) target).EventInfo;
196 Expression ml = MemberLookup (
197 ec, ec.ContainerType, ei.Name,
198 MemberTypes.Event, AllBindingFlags, l);
202 // If this is the case, then the Event does not belong
203 // to this TypeContainer and so, according to the spec
204 // is allowed to only appear on the left hand of
205 // the += and -= operators
207 // Note that if target will not appear as an EventExpr
208 // in the case it is being referenced within the same type container;
209 // it will appear as a FieldExpr in that case.
212 if (!(source is Binary)) {
216 tmp = ((Binary) source);
217 if (tmp.Oper != Binary.Operator.Addition &&
218 tmp.Oper != Binary.Operator.Subtraction) {
226 if (source is New && target_type.IsSubclassOf (TypeManager.value_type)){
227 New n = (New) source;
229 n.ValueTypeVariable = target;
233 if (target.eclass != ExprClass.Variable && target.eclass != ExprClass.EventAccess){
234 Report.Error (131, l,
235 "Left hand of an assignment must be a variable, " +
236 "a property or an indexer");
240 if (target_type == source_type)
244 // If this assignemnt/operator was part of a compound binary
245 // operator, then we allow an explicit conversion, as detailed
249 if (this is CompoundAssign){
250 CompoundAssign a = (CompoundAssign) this;
252 Binary b = source as Binary;
253 if (b != null && b.IsBuiltinOperator){
255 // 1. if the source is explicitly convertible to the
259 source = ConvertExplicit (ec, source, target_type, l);
261 Error_CannotConvertImplicit (l, source_type, target_type);
266 // 2. and the original right side is implicitly convertible to
267 // the type of target_type.
269 if (StandardConversionExists (a.original_source, target_type))
272 Error_CannotConvertImplicit (l, a.original_source.Type, target_type);
277 source = ConvertImplicitRequired (ec, source, target_type, l);
284 void Emit (EmitContext ec, bool is_statement)
286 if (target is EventExpr) {
287 ((EventExpr) target).EmitAddOrRemove (ec, source);
292 // FIXME! We need a way to "probe" if the process can
293 // just use `dup' to propagate the result
295 IAssignMethod am = (IAssignMethod) target;
297 if (this is CompoundAssign){
298 am.CacheTemporaries (ec);
302 am.EmitAssign (ec, source);
304 LocalTemporary tempo;
306 tempo = new LocalTemporary (ec, source.Type);
310 am.EmitAssign (ec, tempo);
316 public override void Emit (EmitContext ec)
321 public override void EmitStatement (EmitContext ec)
329 // This class is used for compound assignments.
331 class CompoundAssign : Assign {
333 public Expression original_source;
335 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
336 : base (target, source, l)
338 original_source = source;
342 public Expression ResolveSource (EmitContext ec)
344 return original_source.Resolve (ec);
347 public override Expression DoResolve (EmitContext ec)
349 target = target.ResolveLValue (ec, source);
353 original_source = original_source.Resolve (ec);
354 if (original_source == null)
358 // Only now we can decouple the original source/target
359 // into a tree, to guarantee that we do not have side
362 source = new Binary (op, target, original_source, l);
363 return base.DoResolve (ec);