e = new IntConstant (-((ShortConstant) expr).Value);
else if (expr is UShortConstant)
e = new IntConstant (-((UShortConstant) expr).Value);
+ else if (expr is SByteConstant)
+ e = new IntConstant (-((SByteConstant) expr).Value);
+ else if (expr is ByteConstant)
+ e = new IntConstant (-((ByteConstant) expr).Value);
return e;
}
case Operator.UnaryNegation:
result = TryReduceNegative (e);
- return true;
+ return result != null;
case Operator.LogicalNot:
if (expr_type != TypeManager.bool_type) {
warning_always_matches = true;
} else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+ if (etype.IsGenericParameter)
+ expr = new BoxedCast (expr, etype);
+
//
// Second case: explicit reference convresion
//
warning_never_matches = true;
}
- if (warning_always_matches)
+ if (warning_always_matches)
Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
- else if (warning_never_matches){
- if (!(probe_type.IsInterface || expr.Type.IsInterface))
+ else if (warning_never_matches){
+ if (!(probe_type.IsInterface || expr.Type.IsInterface))
Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
}
return this;
- }
+ }
}
/// <summary>
}
if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+ if (etype.IsGenericParameter)
+ expr = new BoxedCast (expr, etype);
+
do_isinst = true;
return this;
}
bool do_varargs, ref MethodBase candidate)
{
if (!me.HasTypeArguments &&
- !InferParamsTypeArguments (ec, arguments, ref candidate))
+ !TypeManager.InferParamsTypeArguments (ec, arguments, ref candidate))
return false;
return IsParamsMethodApplicable (
ref MethodBase candidate)
{
if (!me.HasTypeArguments &&
- !InferTypeArguments (ec, arguments, ref candidate))
+ !TypeManager.InferTypeArguments (ec, arguments, ref candidate))
return false;
return IsApplicable (ec, arguments, arg_count, candidate);
if (pd.Count != arg_count)
continue;
- if (!InferTypeArguments (ec, Arguments, ref c))
+ if (!TypeManager.InferTypeArguments (ec, Arguments, ref c))
continue;
VerifyArgumentsCompat (ec, Arguments, arg_count,
if (pd.Count != arg_count)
continue;
- if (InferTypeArguments (ec, Arguments, ref c))
+ if (TypeManager.InferTypeArguments (ec, Arguments, ref c))
continue;
Report.Error (
return true;
}
- static bool InferType (Type pt, Type at, Type[] class_infered, Type[] method_infered)
- {
- if (pt.IsGenericParameter) {
- Type[] infered;
- if (pt.DeclaringMethod != null)
- infered = method_infered;
- else if (class_infered != null)
- infered = class_infered;
- else
- return false;
-
- int pos = pt.GenericParameterPosition;
-
- if (infered [pos] == null) {
- Type check = at;
- while (check.IsArray)
- check = check.GetElementType ();
-
- if (pt == check)
- return false;
-
- infered [pos] = at;
- return true;
- }
-
- if (infered [pos] != at)
- return false;
-
- return true;
- }
-
- if (!pt.ContainsGenericParameters) {
- if (at.ContainsGenericParameters)
- return InferType (at, pt, class_infered, method_infered);
- else
- return true;
- }
-
- if (at.IsArray) {
- if (!pt.IsArray ||
- (at.GetArrayRank () != pt.GetArrayRank ()))
- return false;
-
- return InferType (pt.GetElementType (), at.GetElementType (),
- class_infered, method_infered);
- }
-
- if (pt.IsArray) {
- if (!at.IsArray ||
- (pt.GetArrayRank () != at.GetArrayRank ()))
- return false;
-
- return InferType (pt.GetElementType (), at.GetElementType (),
- class_infered, method_infered);
- }
-
- if (pt.IsByRef && at.IsByRef)
- return InferType (pt.GetElementType (), at.GetElementType (),
- class_infered, method_infered);
-
- if (method_infered == null)
- return false;
-
- ArrayList list = new ArrayList ();
- if (at.IsGenericInstance)
- list.Add (at);
- else {
- for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
- list.Add (bt);
-
- list.AddRange (TypeManager.GetInterfaces (at));
- }
-
- bool found_one = false;
-
- foreach (Type type in list) {
- if (!type.IsGenericInstance)
- continue;
-
- Type[] infered_types = new Type [method_infered.Length];
-
- if (!InferGenericInstance (pt, type, infered_types))
- continue;
-
- for (int i = 0; i < infered_types.Length; i++) {
- if (method_infered [i] == null) {
- method_infered [i] = infered_types [i];
- continue;
- }
-
- if (method_infered [i] != infered_types [i])
- return false;
- }
-
- found_one = true;
- }
-
- return found_one;
- }
-
- static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
- {
- Type[] at_args = at.GetGenericArguments ();
- Type[] pt_args = pt.GetGenericArguments ();
-
- if (at_args.Length != pt_args.Length)
- return false;
-
- for (int i = 0; i < at_args.Length; i++) {
- if (!InferType (pt_args [i], at_args [i], null, infered_types))
- return false;
- }
-
- for (int i = 0; i < infered_types.Length; i++) {
- if (infered_types [i] == null)
- return false;
- }
-
- return true;
- }
-
- static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
- ref MethodBase method)
- {
- if ((arguments == null) || !TypeManager.IsGenericMethod (method))
- return true;
-
- int arg_count;
-
- if (arguments == null)
- arg_count = 0;
- else
- arg_count = arguments.Count;
-
- ParameterData pd = GetParameterData (method);
-
- int pd_count = pd.Count;
-
- if (pd_count == 0)
- return false;
-
- if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
- return false;
-
- if (pd_count - 1 > arg_count)
- return false;
-
- if (pd_count == 1 && arg_count == 0)
- return true;
-
- Type[] method_args = method.GetGenericArguments ();
- Type[] infered_types = new Type [method_args.Length];
-
- //
- // If we have come this far, the case which
- // remains is when the number of parameters is
- // less than or equal to the argument count.
- //
- for (int i = 0; i < pd_count - 1; ++i) {
- Argument a = (Argument) arguments [i];
-
- if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
- continue;
-
- Type pt = pd.ParameterType (i);
- Type at = a.Type;
-
- if (!InferType (pt, at, null, infered_types))
- return false;
- }
-
- Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
-
- for (int i = pd_count - 1; i < arg_count; i++) {
- Argument a = (Argument) arguments [i];
-
- if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
- continue;
-
- if (!InferType (element_type, a.Type, null, infered_types))
- return false;
- }
-
- for (int i = 0; i < infered_types.Length; i++)
- if (infered_types [i] == null)
- return false;
-
- method = method.BindGenericParameters (infered_types);
- return true;
- }
-
- public static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
- Type[] infered_class_types, Type[] infered_types)
- {
- if ((infered_class_types == null) && (infered_types == null))
- return false;
-
- for (int i = 0; i < arg_types.Length; i++) {
- if (arg_types [i] == null)
- continue;
-
- if (!InferType (param_types [i], arg_types [i], infered_class_types, infered_types))
- return false;
- }
-
- if (infered_class_types != null) {
- for (int i = 0; i < infered_class_types.Length; i++)
- if (infered_class_types [i] == null)
- return false;
- }
-
- if (infered_types != null) {
- for (int i = 0; i < infered_types.Length; i++)
- if (infered_types [i] == null)
- return false;
- }
-
- return true;
- }
-
- static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
- ref MethodBase method)
- {
- if (!TypeManager.IsGenericMethod (method))
- return true;
-
- int arg_count;
- if (arguments != null)
- arg_count = arguments.Count;
- else
- arg_count = 0;
-
- ParameterData pd = GetParameterData (method);
- if (arg_count != pd.Count)
- return false;
-
- Type[] method_args = method.GetGenericArguments ();
-
- bool is_open = false;
- for (int i = 0; i < method_args.Length; i++) {
- if (method_args [i].IsGenericParameter) {
- is_open = true;
- break;
- }
- }
- if (!is_open)
- return true;
-
- Type[] infered_types = new Type [method_args.Length];
-
- Type[] param_types = new Type [pd.Count];
- Type[] arg_types = new Type [pd.Count];
-
- for (int i = 0; i < arg_count; i++) {
- param_types [i] = pd.ParameterType (i);
-
- Argument a = (Argument) arguments [i];
- if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
- continue;
-
- arg_types [i] = a.Type;
- }
-
- if (!InferTypeArguments (param_types, arg_types, null, infered_types))
- return false;
-
- method = method.BindGenericParameters (infered_types);
- return true;
- }
-
- public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
- ref MethodBase method)
- {
- if (!TypeManager.IsGenericMethod (method))
- return true;
-
- ParameterData pd = GetParameterData (method);
- if (apd.Count != pd.Count)
- return false;
-
- Type[] method_args = method.GetGenericArguments ();
- Type[] infered_types = new Type [method_args.Length];
-
- Type[] param_types = new Type [pd.Count];
- Type[] arg_types = new Type [pd.Count];
-
- for (int i = 0; i < apd.Count; i++) {
- param_types [i] = pd.ParameterType (i);
- arg_types [i] = apd.ParameterType (i);
- }
-
- if (!InferTypeArguments (param_types, arg_types, null, infered_types))
- return false;
-
- method = method.BindGenericParameters (infered_types);
- return true;
- }
-
public override Expression DoResolve (EmitContext ec)
{
//