2 // assign.cs: Assignments.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Martin Baulig (martin@gnome.org)
8 // (C) 2001, 2002 Ximian, Inc.
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 using System.Collections;
26 using System.Reflection;
27 using System.Reflection.Emit;
29 namespace Mono.CSharp {
32 /// This interface is implemented by expressions that can be assigned to.
35 /// This interface is implemented by Expressions whose values can not
36 /// store the result on the top of the stack.
38 /// Expressions implementing this (Properties, Indexers and Arrays) would
39 /// perform an assignment of the Expression "source" into its final
42 /// No values on the top of the stack are expected to be left by
43 /// invoking this method.
45 public interface IAssignMethod {
47 // This method will emit the code for the actual assignment
49 void EmitAssign (EmitContext ec, Expression source);
52 // This method is invoked before any code generation takes
53 // place, and it is a mechanism to inform that the expression
54 // will be invoked more than once, and that the method should
55 // use temporary values to avoid having side effects
57 // Example: a [ g () ] ++
59 void CacheTemporaries (EmitContext ec);
63 /// An Expression to hold a temporary value.
66 /// The LocalTemporary class is used to hold temporary values of a given
67 /// type to "simulate" the expression semantics on property and indexer
68 /// access whose return values are void.
70 /// The local temporary is used to alter the normal flow of code generation
71 /// basically it creates a local variable, and its emit instruction generates
72 /// code to access this value, return its address or save its value.
74 public class LocalTemporary : Expression, IMemoryLocation {
77 public LocalTemporary (EmitContext ec, Type t)
80 eclass = ExprClass.Value;
82 builder = ec.GetTemporaryStorage (t);
85 public void Release (EmitContext ec)
87 ec.FreeTemporaryStorage (builder);
91 public LocalTemporary (LocalBuilder b, Type t)
94 eclass = ExprClass.Value;
99 public override Expression DoResolve (EmitContext ec)
104 public override void Emit (EmitContext ec)
106 ec.ig.Emit (OpCodes.Ldloc, builder);
109 public void Store (EmitContext ec)
111 ec.ig.Emit (OpCodes.Stloc, builder);
114 public void AddressOf (EmitContext ec, AddressOp mode)
116 ec.ig.Emit (OpCodes.Ldloca, builder);
121 /// The Assign node takes care of assigning the value of source into
122 /// the expression represented by target.
124 public class Assign : ExpressionStatement {
125 protected Expression target, source, real_source;
126 protected LocalTemporary temp = null, real_temp = null;
127 protected Assign embedded = null;
128 protected bool is_embedded = false;
129 protected bool must_free_temp = false;
131 public Assign (Expression target, Expression source, Location l)
133 this.target = target;
134 this.source = this.real_source = source;
138 protected Assign (Assign embedded, Location l)
139 : this (embedded.target, embedded.source, l)
141 this.is_embedded = true;
144 public Expression Target {
154 public Expression Source {
164 public static void error70 (EventInfo ei, Location l)
166 Report.Error (70, l, "The event '" + ei.Name +
167 "' can only appear on the left-side of a += or -= (except when" +
168 " used from within the type '" + ei.DeclaringType + "')");
171 bool IsLateIndexSet(Expression target, EmitContext ec)
173 if (target is Invocation)
175 Invocation i = (Invocation) target;
176 if (i.Expr is MethodGroupExpr)
178 MethodGroupExpr mg = (MethodGroupExpr) i.Expr;
179 if (mg.Name == "LateIndexSet")
187 // Will return either `this' or an instance of `New'.
189 public override Expression DoResolve (EmitContext ec)
191 // Create an embedded assignment if our source is an assignment.
192 if (source is Assign)
193 source = embedded = new Assign ((Assign) source, loc);
195 real_source = source = source.Resolve (ec);
200 // This is used in an embedded assignment.
201 // As an example, consider the statement "A = X = Y = Z".
203 if (is_embedded && !(source is Constant)) {
204 // If this is the innermost assignment (the "Y = Z" in our example),
205 // create a new temporary local, otherwise inherit that variable
206 // from our child (the "X = (Y = Z)" inherits the local from the
207 // "Y = Z" assignment).
208 if (embedded == null)
209 real_temp = temp = new LocalTemporary (ec, source.Type);
211 temp = embedded.temp;
213 // Set the source to the new temporary variable.
214 // This means that the following target.ResolveLValue () will tell
215 // the target to read it's source value from that variable.
219 // If we have an embedded assignment, use the embedded assignment's temporary
220 // local variable as source.
221 if (embedded != null)
222 source = (embedded.temp != null) ? embedded.temp : embedded.source;
224 target = target.ResolveLValue (ec, source);
229 Type target_type = target.Type;
230 Type source_type = real_source.Type;
232 // If we're an embedded assignment, our parent will reuse our source as its
233 // source, it won't read from our target.
238 eclass = ExprClass.Value;
241 // If we are doing a property assignment, then
242 // set the `value' field on the property, and Resolve
245 if (target is PropertyExpr){
246 PropertyExpr property_assign = (PropertyExpr) target;
248 if (source_type != target_type){
249 source = ConvertImplicitRequired (ec, source, target_type, loc);
255 // FIXME: Maybe handle this in the LValueResolve
257 if (!property_assign.VerifyAssignable ())
263 if (target is IndexerAccess) {
267 if (target is EventExpr) {
268 EventInfo ei = ((EventExpr) target).EventInfo;
270 Expression ml = MemberLookup (
271 ec, ec.ContainerType, ei.Name,
272 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
276 // If this is the case, then the Event does not belong
277 // to this Type and so, according to the spec
278 // is allowed to only appear on the left hand of
279 // the += and -= operators
281 // Note that target will not appear as an EventExpr
282 // in the case it is being referenced within the same type container;
283 // it will appear as a FieldExpr in that case.
286 if (!(source is Binary)) {
290 Binary tmp = ((Binary) source);
291 if (tmp.Oper != Binary.Operator.Addition &&
292 tmp.Oper != Binary.Operator.Subtraction) {
300 if (source is New && target_type.IsValueType){
301 New n = (New) source;
303 n.ValueTypeVariable = target;
307 if (target.eclass != ExprClass.Variable && target.eclass != ExprClass.EventAccess){
308 Report.Error (131, loc,
309 "Left hand of an assignment must be a variable, " +
310 "a property or an indexer");
314 if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
315 source.Error118 ("variable or value");
317 } else if (source is MethodGroupExpr){
318 ((MethodGroupExpr) source).ReportUsageError ();
322 if (target_type == source_type)
326 // If this assignemnt/operator was part of a compound binary
327 // operator, then we allow an explicit conversion, as detailed
331 if (this is CompoundAssign){
332 CompoundAssign a = (CompoundAssign) this;
334 Binary b = source as Binary;
335 if (b != null && b.IsBuiltinOperator){
337 // 1. if the source is explicitly convertible to the
341 source = ConvertExplicit (ec, source, target_type, loc);
343 Error_CannotConvertImplicit (loc, source_type, target_type);
348 // 2. and the original right side is implicitly convertible to
349 // the type of target_type.
351 if (StandardConversionExists (a.original_source, target_type))
354 Error_CannotConvertImplicit (loc, a.original_source.Type, target_type);
358 if (IsLateIndexSet(target, ec))
360 // then we must rewrite the whole expression, since
361 // THIS assign is no longer valid/needed
362 Invocation i = (Invocation) target;
365 source = ConvertImplicitRequired (ec, source, target_type, loc);
369 // If we're an embedded assignment, we need to create a new temporary variable
370 // for the converted value. Our parent will use this new variable as its source.
371 // The same applies when we have an embedded assignment - in this case, we need
372 // to convert our embedded assignment's temporary local variable to the correct
373 // type and store it in a new temporary local.
374 if (is_embedded || embedded != null) {
376 temp = new LocalTemporary (ec, type);
377 must_free_temp = true;
383 Expression EmitEmbedded (EmitContext ec)
385 // Emit an embedded assignment.
387 if (real_temp != null) {
388 // If we're the innermost assignment, `real_source' is the right-hand
389 // expression which gets assigned to all the variables left of it.
390 // Emit this expression and store its result in real_temp.
391 real_source.Emit (ec);
392 real_temp.Store (ec);
395 if (embedded != null)
396 embedded.EmitEmbedded (ec);
398 // This happens when we've done a type conversion, in this case source will be
399 // the expression which does the type conversion from real_temp.
400 // So emit it and store the result in temp; this is the var which will be read
402 if (temp != real_temp) {
407 Expression temp_source = (temp != null) ? temp : source;
408 ((IAssignMethod) target).EmitAssign (ec, temp_source);
412 void ReleaseEmbedded (EmitContext ec)
414 if (embedded != null)
415 embedded.ReleaseEmbedded (ec);
417 if (real_temp != null)
418 real_temp.Release (ec);
424 void Emit (EmitContext ec, bool is_statement)
426 if (target is EventExpr) {
427 ((EventExpr) target).EmitAddOrRemove (ec, source);
432 // FIXME! We need a way to "probe" if the process can
433 // just use `dup' to propagate the result
435 IAssignMethod am = (IAssignMethod) target;
437 if (this is CompoundAssign){
438 am.CacheTemporaries (ec);
441 Expression temp_source;
442 if (embedded != null) {
443 temp_source = embedded.EmitEmbedded (ec);
451 temp_source = source;
454 am.EmitAssign (ec, temp_source);
456 LocalTemporary tempo;
458 tempo = new LocalTemporary (ec, source.Type);
460 temp_source.Emit (ec);
462 am.EmitAssign (ec, tempo);
467 if (embedded != null) {
470 embedded.ReleaseEmbedded (ec);
474 public override void Emit (EmitContext ec)
479 public override void EmitStatement (EmitContext ec)
487 // This class is used for compound assignments.
489 class CompoundAssign : Assign {
491 public Expression original_source;
493 public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
494 : base (target, source, l)
496 original_source = source;
500 public Expression ResolveSource (EmitContext ec)
502 return original_source.Resolve (ec);
505 public override Expression DoResolve (EmitContext ec)
507 target = target.ResolveLValue (ec, source);
511 original_source = original_source.Resolve (ec);
512 if (original_source == null)
516 // Only now we can decouple the original source/target
517 // into a tree, to guarantee that we do not have side
520 source = new Binary (op, target, original_source, loc);
521 return base.DoResolve (ec);