Report.SymbolRelatedToPreviousError (partype);
Report.Error (59, Location,
"Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'",
- TypeManager.CSharpName (partype), GetSignatureForError ());
+ partype.GetSignatureForError (), GetSignatureForError ());
}
}
Report.SymbolRelatedToPreviousError (ret_type);
Report.Error (58, Location,
"Inconsistent accessibility: return type `" +
- TypeManager.CSharpName (ret_type) + "' is less " +
+ ret_type.GetSignatureForError () + "' is less " +
"accessible than delegate `" + GetSignatureForError () + "'");
return false;
}
if (!Parameters.IsEmpty) {
parameters.ResolveDefaultValues (this);
}
+
+ InvokeBuilder.PrepareEmit ();
+ if (BeginInvokeBuilder != null) {
+ BeginInvokeBuilder.PrepareEmit ();
+ EndInvokeBuilder.PrepareEmit ();
+ }
}
public override void Emit ()
protected MethodSpec constructor_method;
protected MethodGroupExpr method_group;
+ public bool AllowSpecialMethodsInvocation { get; set; }
+
public override bool ContainsEmitWithAwait ()
{
return false;
}
- public static Arguments CreateDelegateMethodArguments (AParametersCollection pd, TypeSpec[] types, Location loc)
+ public static Arguments CreateDelegateMethodArguments (ResolveContext rc, AParametersCollection pd, TypeSpec[] types, Location loc)
{
Arguments delegate_arguments = new Arguments (pd.Count);
for (int i = 0; i < pd.Count; ++i) {
break;
}
- delegate_arguments.Add (new Argument (new TypeExpression (types [i], loc), atype_modifier));
+ var ptype = types[i];
+ if (ptype.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
+ ptype = rc.BuiltinTypes.Object;
+
+ delegate_arguments.Add (new Argument (new TypeExpression (ptype, loc), atype_modifier));
}
return delegate_arguments;
var invoke_method = Delegate.GetInvokeMethod (type);
- Arguments arguments = CreateDelegateMethodArguments (invoke_method.Parameters, invoke_method.Parameters.Types, loc);
+ Arguments arguments = CreateDelegateMethodArguments (ec, invoke_method.Parameters, invoke_method.Parameters.Types, loc);
method_group = method_group.OverloadResolve (ec, ref arguments, this, OverloadResolver.Restrictions.CovariantDelegate);
if (method_group == null)
return null;
return null;
}
- Invocation.IsSpecialMethodInvocation (ec, delegate_method, loc);
+ if (!AllowSpecialMethodsInvocation)
+ Invocation.IsSpecialMethodInvocation (ec, delegate_method, loc);
ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;
if (emg != null) {
TypeSpec e_type = emg.ExtensionExpression.Type;
if (TypeSpec.IsValueType (e_type)) {
ec.Report.Error (1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
- delegate_method.GetSignatureForError (), TypeManager.CSharpName (e_type));
+ delegate_method.GetSignatureForError (), e_type.GetSignatureForError ());
}
}
TypeSpec rt = delegate_method.ReturnType;
+ if (rt.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
+ rt = ec.BuiltinTypes.Object;
+
if (!Delegate.IsTypeCovariant (ec, rt, invoke_method.ReturnType)) {
- Expression ret_expr = new TypeExpression (rt, loc);
+ Expression ret_expr = new TypeExpression (delegate_method.ReturnType, loc);
Error_ConversionFailed (ec, delegate_method, ret_expr);
}
ec.Report.SymbolRelatedToPreviousError (method);
if (ec.Module.Compiler.Settings.Version == LanguageVersion.ISO_1) {
ec.Report.Error (410, loc, "A method or delegate `{0} {1}' parameters and return type must be same as delegate `{2} {3}' parameters and return type",
- TypeManager.CSharpName (method.ReturnType), member_name,
- TypeManager.CSharpName (invoke_method.ReturnType), Delegate.FullDelegateDesc (invoke_method));
+ method.ReturnType.GetSignatureForError (), member_name,
+ invoke_method.ReturnType.GetSignatureForError (), Delegate.FullDelegateDesc (invoke_method));
return;
}
ec.Report.Error (407, loc, "A method or delegate `{0} {1}' return type does not match delegate `{2} {3}' return type",
return_type.GetSignatureForError (), member_name,
- TypeManager.CSharpName (invoke_method.ReturnType), Delegate.FullDelegateDesc (invoke_method));
+ invoke_method.ReturnType.GetSignatureForError (), Delegate.FullDelegateDesc (invoke_method));
}
public static bool ImplicitStandardConversionExists (ResolveContext ec, MethodGroupExpr mg, TypeSpec target_type)
var invoke = Delegate.GetInvokeMethod (target_type);
- Arguments arguments = CreateDelegateMethodArguments (invoke.Parameters, invoke.Parameters.Types, mg.Location);
+ Arguments arguments = CreateDelegateMethodArguments (ec, invoke.Parameters, invoke.Parameters.Types, mg.Location);
return mg.OverloadResolve (ec, ref arguments, null, OverloadResolver.Restrictions.CovariantDelegate | OverloadResolver.Restrictions.ProbingOnly) != null;
}
//
public class ImplicitDelegateCreation : DelegateCreation
{
- ImplicitDelegateCreation (TypeSpec t, MethodGroupExpr mg, Location l)
+ Field mg_cache;
+
+ public ImplicitDelegateCreation (TypeSpec delegateType, MethodGroupExpr mg, Location loc)
{
- type = t;
+ type = delegateType;
this.method_group = mg;
- loc = l;
+ this.loc = loc;
+ }
+
+ //
+ // Returns true when type is MVAR or has MVAR reference
+ //
+ static bool ContainsMethodTypeParameter (TypeSpec type)
+ {
+ var tps = type as TypeParameterSpec;
+ if (tps != null)
+ return tps.IsMethodOwned;
+
+ var ec = type as ElementTypeSpec;
+ if (ec != null)
+ return ContainsMethodTypeParameter (ec.Element);
+
+ foreach (var t in type.TypeArguments) {
+ if (ContainsMethodTypeParameter (t)) {
+ return true;
+ }
+ }
+
+ return false;
}
- static public Expression Create (ResolveContext ec, MethodGroupExpr mge,
- TypeSpec target_type, Location loc)
+ protected override Expression DoResolve (ResolveContext ec)
{
- ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, mge, loc);
- return d.DoResolve (ec);
+ var expr = base.DoResolve (ec);
+ if (expr == null)
+ return null;
+
+ if (ec.IsInProbingMode)
+ return expr;
+
+ //
+ // Cache any static delegate creation
+ //
+ if (method_group.InstanceExpression != null)
+ return expr;
+
+ //
+ // Cannot easily cache types with MVAR
+ //
+ if (ContainsMethodTypeParameter (type))
+ return expr;
+
+ if (ContainsMethodTypeParameter (method_group.BestCandidate.DeclaringType))
+ return expr;
+
+ //
+ // Create type level cache for a delegate instance
+ //
+ var parent = ec.CurrentMemberDefinition.Parent.PartialContainer;
+ int id = parent.MethodGroupsCounter++;
+
+ mg_cache = new Field (parent, new TypeExpression (type, loc),
+ Modifiers.STATIC | Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED,
+ new MemberName (CompilerGeneratedContainer.MakeName (null, "f", "mg$cache", id), loc), null);
+ mg_cache.Define ();
+ parent.AddField (mg_cache);
+
+ return expr;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ Label l_initialized = ec.DefineLabel ();
+
+ if (mg_cache != null) {
+ ec.Emit (OpCodes.Ldsfld, mg_cache.Spec);
+ ec.Emit (OpCodes.Brtrue_S, l_initialized);
+ }
+
+ base.Emit (ec);
+
+ if (mg_cache != null) {
+ ec.Emit (OpCodes.Stsfld, mg_cache.Spec);
+ ec.MarkLabel (l_initialized);
+ ec.Emit (OpCodes.Ldsfld, mg_cache.Spec);
+ }
}
}