Modifiers.PRIVATE;
public Delegate (NamespaceEntry ns, TypeContainer parent, Expression type,
- int mod_flags, string name, Parameters param_list,
+ int mod_flags, MemberName name, Parameters param_list,
Attributes attrs, Location l)
: base (ns, parent, name, attrs, l)
if (TypeBuilder != null)
return TypeBuilder;
- TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
- TypeAttributes.Class | TypeAttributes.Sealed;
+ ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
+
+ if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
+ Namespace system = Namespace.LookupNamespace ("System", true);
+ TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
+ TypeManager.multicast_delegate_type = expr.ResolveType (ec);
+ }
+
+ if (TypeManager.multicast_delegate_type == null)
+ throw new InternalErrorException ("System.MulticastDelegate unresolved");
if (IsTopLevel) {
if (TypeManager.NamespaceClash (Name, Location))
ModuleBuilder builder = CodeGen.Module.Builder;
TypeBuilder = builder.DefineType (
- Name, attr, TypeManager.multicast_delegate_type);
+ Name, TypeAttr, TypeManager.multicast_delegate_type);
} else {
TypeBuilder builder = Parent.TypeBuilder;
string name = Name.Substring (1 + Name.LastIndexOf ('.'));
TypeBuilder = builder.DefineNestedType (
- name, attr, TypeManager.multicast_delegate_type);
+ name, TypeAttr, TypeManager.multicast_delegate_type);
}
TypeManager.AddDelegateType (Name, TypeBuilder, this);
{
MethodAttributes mattr;
int i;
- EmitContext ec = new EmitContext (this, this, Location, null,
- null, ModFlags, false);
+
+ if (ec == null)
+ throw new InternalErrorException ("Define called before DefineType?");
// FIXME: POSSIBLY make this static, as it is always constant
//
//
// HACK because System.Reflection.Emit is lame
//
- //
- // FIXME: POSSIBLY make these static, as they are always the same
Parameter [] fixed_pars = new Parameter [2];
- fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
- fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
- Parameters const_parameters = new Parameters (fixed_pars, null, Location);
+ fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
+ Parameter.Modifier.NONE, null, Location);
+ fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method",
+ Parameter.Modifier.NONE, null, Location);
+ Parameters const_parameters = new Parameters (fixed_pars, null);
TypeManager.RegisterMethod (
ConstructorBuilder,
// First, call the `out of band' special method for
// defining recursively any types we need:
- if (!Parameters.ComputeAndDefineParameterTypes (this))
- return false;
-
- param_types = Parameters.GetParameterInfo (this);
+ param_types = Parameters.GetParameterInfo (ec);
if (param_types == null)
return false;
return false;
}
- ReturnType = ResolveTypeExpr (ReturnType, false, Location);
+ ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
if (ReturnType == null)
return false;
if (ret_type.IsPointer && !UnsafeOK (Parent))
return false;
+ if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
+ Method.Error1599 (Location, ret_type);
+ return false;
+ }
+
//
// We don't have to check any others because they are all
// guaranteed to be accessible - they are standard types.
for (; i < top; i++) {
p = Parameters.FixedParameters [i];
- p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
+ p.DefineParameter (ec, InvokeBuilder, null, i + 1);
if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
out_params++;
}
if (Parameters.ArrayParameter != null){
Parameter p = Parameters.ArrayParameter;
- p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
+
+ if (TypeManager.param_array_type == null && !RootContext.StdLib) {
+ Namespace system = Namespace.LookupNamespace ("System", true);
+ TypeExpr expr = system.Lookup (this, "ParamArrayAttribute", Location) as TypeExpr;
+ TypeManager.param_array_type = expr.ResolveType (ec);
+ }
+
+ if (TypeManager.cons_param_array_attribute == null) {
+ Type [] void_arg = { };
+ TypeManager.cons_param_array_attribute = TypeManager.GetConstructor (
+ TypeManager.param_array_type, void_arg);
+ }
+
+ ParameterBuilder pb = InvokeBuilder.DefineParameter (
+ i + 1, Parameters.ArrayParameter.Attributes,Parameters.ArrayParameter.Name);
+
+ pb.SetCustomAttribute (
+ new CustomAttributeBuilder (TypeManager.cons_param_array_attribute, new object [0]));
}
InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
TypeManager.RegisterMethod (InvokeBuilder,
- new InternalParameters (Parent, Parameters),
+ new InternalParameters (param_types, Parameters),
param_types);
//
for (i = 0 ; i < top; i++) {
p = Parameters.FixedParameters [i];
- p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
+ p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
}
}
if (Parameters.ArrayParameter != null){
Parameter p = Parameters.ArrayParameter;
- p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
+ p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
i++;
}
async_params [params_num] = new Parameter (
TypeManager.system_asynccallback_expr, "callback",
- Parameter.Modifier.NONE, null);
+ Parameter.Modifier.NONE, null, Location);
async_params [params_num + 1] = new Parameter (
TypeManager.system_object_expr, "object",
- Parameter.Modifier.NONE, null);
+ Parameter.Modifier.NONE, null, Location);
+
+ Parameters async_parameters = new Parameters (async_params, null);
- Parameters async_parameters = new Parameters (async_params, null, Location);
- async_parameters.ComputeAndDefineParameterTypes (this);
-
- async_parameters.ComputeAndDefineParameterTypes (this);
TypeManager.RegisterMethod (BeginInvokeBuilder,
- new InternalParameters (Parent, async_parameters),
+ new InternalParameters (async_parameters.GetParameterInfo (ec), async_parameters),
async_param_types);
//
}
}
end_param_types [out_params] = TypeManager.iasyncresult_type;
- end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
+ end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null, Location);
//
// Create method, define parameters, register parameters with type system
EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
}
- Parameters end_parameters = new Parameters (end_params, null, Location);
- end_parameters.ComputeAndDefineParameterTypes (this);
+ Parameters end_parameters = new Parameters (end_params, null);
TypeManager.RegisterMethod (
EndInvokeBuilder,
- new InternalParameters (Parent, end_parameters),
+ new InternalParameters (end_parameters.GetParameterInfo (ec), end_parameters),
end_param_types);
return true;
public override void Emit ()
{
if (OptAttributes != null) {
- EmitContext ec = new EmitContext (
- Parent, this, Location, null, null, ModFlags, false);
- Parameters.LabelParameters (ec, InvokeBuilder, Location);
+ Parameters.LabelParameters (ec, InvokeBuilder);
OptAttributes.Emit (ec, this);
}
base.Emit ();
}
+ protected override TypeAttributes TypeAttr {
+ get {
+ return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
+ TypeAttributes.Class | TypeAttributes.Sealed |
+ base.TypeAttr;
+ }
+ }
+
public override string[] ValidAttributeTargets {
get {
return attribute_targets;
AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
- Report.Error_T (3002, Location, GetSignatureForError ());
+ Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
}
return true;
}
+ //
+ // Returns the MethodBase for "Invoke" from a delegate type, this is used
+ // to extract the signature of a delegate.
+ //
+ public static MethodInfo GetInvokeMethod (EmitContext ec, Type delegate_type, Location loc)
+ {
+ Expression ml = Expression.MemberLookup (
+ ec, delegate_type, "Invoke", loc);
+
+ if (!(ml is MethodGroupExpr)) {
+ Report.Error (-100, loc, "Internal error: could not find Invoke method!");
+ return null;
+ }
+
+ return (MethodInfo) (((MethodGroupExpr) ml).Methods [0]);
+ }
+
/// <summary>
/// Verifies whether the method in question is compatible with the delegate
/// Returns the method itself if okay and null if not.
public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
Location loc)
{
- ParameterData pd = Invocation.GetParameterData (mb);
+ ParameterData pd = TypeManager.GetParameterData (mb);
int pd_count = pd.Count;
- Expression ml = Expression.MemberLookup (
- ec, delegate_type, "Invoke", loc);
-
- if (!(ml is MethodGroupExpr)) {
- Report.Error (-100, loc, "Internal error: could not find Invoke method!");
+ MethodBase invoke_mb = GetInvokeMethod (ec, delegate_type, loc);
+ if (invoke_mb == null)
return null;
- }
-
- MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
- ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
+ ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
if (invoke_pd.Count != pd_count)
return null;
for (int i = pd_count; i > 0; ) {
i--;
- if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
- invoke_pd.ParameterModifier (i) == pd.ParameterModifier (i))
+ Type invoke_pd_type = invoke_pd.ParameterType (i);
+ Type pd_type = pd.ParameterType (i);
+ Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
+ Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
+
+ if (invoke_pd_type == pd_type &&
+ invoke_pd_type_mod == pd_type_mod)
continue;
- else {
- return null;
- }
+
+ if (invoke_pd_type.IsSubclassOf (pd_type) &&
+ invoke_pd_type_mod == pd_type_mod)
+ if (RootContext.Version == LanguageVersion.ISO_1) {
+ Report.FeatureIsNotStandardized (loc, "contravariance");
+ return null;
+ } else
+ continue;
+
+ return null;
}
- if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
+ Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
+ Type mb_retval = ((MethodInfo) mb).ReturnType;
+ if (invoke_mb_retval == mb_retval)
return mb;
- else
- return null;
+
+ if (mb_retval.IsSubclassOf (invoke_mb_retval))
+ if (RootContext.Version == LanguageVersion.ISO_1) {
+ Report.FeatureIsNotStandardized (loc, "covariance");
+ return null;
+ }
+ else
+ return mb;
+
+ return null;
}
// <summary>
// Verifies whether the invocation arguments are compatible with the
// delegate's target method
// </summary>
- public static bool VerifyApplicability (EmitContext ec,
- Type delegate_type,
- ArrayList args,
- Location loc)
+ public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
+ ArrayList args, Location loc)
{
int arg_count;
Expression ml = Expression.MemberLookup (
ec, delegate_type, "Invoke", loc);
- if (!(ml is MethodGroupExpr)) {
+ MethodGroupExpr me = ml as MethodGroupExpr;
+ if (me == null) {
Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
return false;
}
- MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
- ParameterData pd = Invocation.GetParameterData (mb);
+ MethodBase mb = me.Methods [0];
+ ParameterData pd = TypeManager.GetParameterData (mb);
int pd_count = pd.Count;
bool params_method = (pd_count != 0) &&
(pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
- if (!params_method && pd_count != arg_count) {
+ bool is_params_applicable = false;
+ bool is_applicable = Invocation.IsApplicable (ec, me, args, arg_count, ref mb);
+
+ if (!is_applicable && params_method &&
+ Invocation.IsParamsMethodApplicable (ec, me, args, arg_count, ref mb))
+ is_applicable = is_params_applicable = true;
+
+ if (!is_applicable && !params_method && arg_count != pd_count) {
Report.Error (1593, loc,
- "Delegate '" + delegate_type.ToString ()
- + "' does not take '" + arg_count + "' arguments");
+ "Delegate '{0}' does not take {1} arguments",
+ delegate_type.ToString (), arg_count);
return false;
}
- //
- // Consider the case:
- // delegate void FOO(param object[] args);
- // FOO f = new FOO(...);
- // f(new object[] {1, 2, 3});
- //
- // This should be treated like f(1,2,3). This is done by ignoring the
- // 'param' modifier for that invocation. If that fails, then the
- // 'param' modifier is considered.
- //
- // One issue is that 'VerifyArgumentsCompat' modifies the elements of
- // the 'args' array. However, the modifications appear idempotent.
- // Normal 'Invocation's also have the same behaviour, implicitly.
- //
-
- bool ans = false;
- if (arg_count == pd_count)
- ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, false, delegate_type, loc);
- if (!ans && params_method)
- ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, true, delegate_type, loc);
- return ans;
+ return Invocation.VerifyArgumentsCompat (
+ ec, args, arg_count, mb,
+ is_params_applicable || (!is_applicable && params_method),
+ delegate_type, false, loc);
}
/// <summary>
}
MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
- ParameterData pd = Invocation.GetParameterData (mb);
+ ParameterData pd = TypeManager.GetParameterData (mb);
Expression probe_ml = Expression.MemberLookup (
ec, delegate_type, "Invoke", loc);
}
MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
- ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
+ ParameterData probe_pd = TypeManager.GetParameterData (probe_mb);
if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
return false;
}
}
+ //
+ // Represents header string for documentation comment.
+ //
+ public override string DocCommentHeader {
+ get { return "T:"; }
+ }
+
protected override void VerifyObsoleteAttribute()
{
CheckUsageOfObsoleteAttribute (ret_type);
protected MethodBase constructor_method;
protected MethodBase delegate_method;
protected MethodGroupExpr method_group;
+ protected Expression delegate_instance_expression;
public DelegateCreation () {}
public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
{
string method_desc;
+ MethodInfo found_method = (MethodInfo)mg.Methods [0];
if (mg.Methods.Length > 1)
- method_desc = mg.Methods [0].Name;
+ method_desc = found_method.Name;
else
- method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
+ method_desc = Invocation.FullMethodDesc (found_method);
Expression invoke_method = Expression.MemberLookup (
ec, type, "Invoke", MemberTypes.Method,
Expression.AllBindingFlags, loc);
- MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
- ParameterData param = Invocation.GetParameterData (method);
+ MethodInfo method = ((MethodGroupExpr) invoke_method).Methods [0] as MethodInfo;
+
+ ParameterData param = TypeManager.GetParameterData (method);
string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
-
- Report.Error (123, loc, "Method '" + method_desc + "' does not " +
- "match delegate '" + delegate_desc + "'");
+
+ if (method.ReturnType != found_method.ReturnType) {
+ Report.Error (407, loc, "'{0}' has the wrong return type to match delegate '{1}'", method_desc, delegate_desc);
+ } else {
+ Report.Error (123, loc, "Method '" + method_desc + "' does not " +
+ "match delegate '" + delegate_desc + "'");
+ }
}
public override void Emit (EmitContext ec)
{
- if (method_group.InstanceExpression == null ||
- delegate_method.IsStatic)
+ if (delegate_instance_expression == null || delegate_method.IsStatic)
ec.ig.Emit (OpCodes.Ldnull);
else
- method_group.InstanceExpression.Emit (ec);
+ delegate_instance_expression.Emit (ec);
if (delegate_method.IsVirtual && !method_group.IsBase) {
ec.ig.Emit (OpCodes.Dup);
return true;
}
- protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
+ protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg,
+ bool check_only)
{
foreach (MethodInfo mi in mg.Methods){
delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
}
if (delegate_method == null) {
- Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
+ if (!check_only)
+ Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
return null;
}
// Check safe/unsafe of the delegate
//
if (!ec.InUnsafe){
- ParameterData param = Invocation.GetParameterData (delegate_method);
+ ParameterData param = TypeManager.GetParameterData (delegate_method);
int count = param.Count;
for (int i = 0; i < count; i++){
IMethodData md = TypeManager.GetMethod (delegate_method);
if (md == null) {
if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
- // Cannot create delegate with '{0}' because it has a Conditional attribute
- Report.Error_T (1618, loc, TypeManager.CSharpSignature (delegate_method));
+ Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
}
} else {
+ md.SetMemberIsUsed ();
if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
- // Cannot create delegate with '{0}' because it has a Conditional attribute
- Report.Error_T (1618, loc, TypeManager.CSharpSignature (delegate_method));
+ Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
}
}
if (mg.InstanceExpression != null)
- mg.InstanceExpression = mg.InstanceExpression.Resolve (ec);
+ delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
else if (ec.IsStatic) {
if (!delegate_method.IsStatic) {
Report.Error (120, loc,
delegate_method.Name);
return null;
}
- mg.InstanceExpression = null;
+ delegate_instance_expression = null;
} else
- mg.InstanceExpression = ec.GetThis (loc);
+ delegate_instance_expression = ec.GetThis (loc);
- if (mg.InstanceExpression != null && mg.InstanceExpression.Type.IsValueType)
- mg.InstanceExpression = new BoxedCast (mg.InstanceExpression);
+ if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
+ delegate_instance_expression = new BoxedCast (delegate_instance_expression);
method_group = mg;
eclass = ExprClass.Value;
{
return this;
}
-
- static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
+
+ static public Expression Create (EmitContext ec, MethodGroupExpr mge,
+ Type target_type, bool check_only, Location loc)
{
ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
if (d.ResolveConstructorMethod (ec))
- return d.ResolveMethodGroupExpr (ec, mge);
+ return d.ResolveMethodGroupExpr (ec, mge, check_only);
else
return null;
}
}
-
+
//
// A delegate-creation-expression, invoked from the `New' class
//
Expression e = a.Expr;
+ if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
+ return ((AnonymousMethod) e).Compatible (ec, type, false);
+
MethodGroupExpr mg = e as MethodGroupExpr;
if (mg != null)
- return ResolveMethodGroupExpr (ec, mg);
+ return ResolveMethodGroupExpr (ec, mg, false);
Type e_type = e.Type;
if (!TypeManager.IsDelegateType (e_type)) {
- e.Error_UnexpectedKind ("method");
+ Report.Error (149, loc, "Method name expected");
return null;
}
return null;
}
- method_group.InstanceExpression = e;
+ delegate_instance_expression = e;
delegate_method = method_group.Methods [0];
eclass = ExprClass.Value;