void FabricateBodyStatement()
{
var cas = TypeManager.gen_interlocked_compare_exchange;
if (cas == null) {
var t = Module.PredefinedTypes.Interlocked.Resolve (Location);
if (t == null)
return;
var p = new ParametersImported (
new[] {
new ParameterData (null, Parameter.Modifier.REF),
new ParameterData (null, Parameter.Modifier.NONE),
new ParameterData (null, Parameter.Modifier.NONE)
},
new[] {
new TypeParameterSpec (0, null, SpecialConstraint.None, Variance.None, null),
new TypeParameterSpec (0, null, SpecialConstraint.None, Variance.None, null),
new TypeParameterSpec (0, null, SpecialConstraint.None, Variance.None, null),
}, false);
var filter = new MemberFilter ("CompareExchange", 1, MemberKind.Method, p, null);
cas = TypeManager.gen_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (t, filter, Location);
}
//
// Delegate obj1 = backing_field
// do {
// Delegate obj2 = obj1;
// obj1 = Interlocked.CompareExchange (ref backing_field, Delegate.Combine|Remove(obj2, value), obj1);
// } while (obj1 != obj2)
//
var field_info = ((EventField) method).backing_field;
FieldExpr f_expr = new FieldExpr (field_info, Location);
if (!IsStatic)
f_expr.InstanceExpression = new CompilerGeneratedThis (Parent.CurrentType, Location);
var obj1 = LocalVariable.CreateCompilerGenerated (field_info.MemberType, block, Location);
var obj2 = LocalVariable.CreateCompilerGenerated (field_info.MemberType, block, Location);
block.AddStatement (new StatementExpression (new SimpleAssign (new LocalVariableReference (obj1, Location), f_expr)));
var cond = new BooleanExpression (new Binary (Binary.Operator.Inequality,
new LocalVariableReference (obj1, Location), new LocalVariableReference (obj2, Location), Location));
var body = new ExplicitBlock (block, Location, Location);
block.AddStatement (new Do (body, cond, Location));
body.AddStatement (new StatementExpression (
new SimpleAssign (new LocalVariableReference (obj2, Location), new LocalVariableReference (obj1, Location))));
var args_oper = new Arguments (2);
args_oper.Add (new Argument (new LocalVariableReference (obj2, Location)));
args_oper.Add (new Argument (block.GetParameterReference (0, Location)));
var args = new Arguments (3);
args.Add (new Argument (f_expr, Argument.AType.Ref));
args.Add (new Argument (new Cast (
new TypeExpression (field_info.MemberType, Location),
new Invocation (MethodGroupExpr.CreatePredefined (Operation, Operation.DeclaringType, Location), args_oper),
Location)));
args.Add (new Argument (new LocalVariableReference (obj1, Location)));
body.AddStatement (new StatementExpression (new SimpleAssign (
new LocalVariableReference (obj1, Location),
new Invocation (MethodGroupExpr.CreatePredefined (cas, cas.DeclaringType, Location), args))));
}