public static GetImplicit ( |
||
op | ||
to | ||
onlyStandard | bool | |
typeMapper | ITypeMapper | |
return |
public SwitchBlock(Operand expression) { this.expression = expression; Type exprType = expression.Type; if (Array.IndexOf(validTypes, exprType) != -1) { govType = exprType; } else if (exprType.IsEnum) { govType = Enum.GetUnderlyingType(exprType); } else { // if a single implicit coversion from expression to one of the valid types exists, it's ok foreach (Type t in validTypes) { Conversion tmp = Conversion.GetImplicit(expression, t, false); if (tmp.IsValid) { if (conv == null) { conv = tmp; govType = t; } else { throw new AmbiguousMatchException(Properties.Messages.ErrAmbiguousSwitchExpression); } } } } }
public static ApplicableFunction ValidateCandidate(IMemberInfo candidate, Operand[] args, ITypeMapper typeMapper) { Type[] cTypes = candidate.ParameterTypes; if (cTypes.Length == args.Length) { Conversion[] conversions = new Conversion[args.Length]; for (int i = 0; i < cTypes.Length; i++) { conversions[i] = Conversion.GetImplicit(args[i], cTypes[i], false, typeMapper); if (!conversions[i].IsValid) { if (cTypes[i].IsByRef) { conversions[i] = Conversion.GetImplicit(args[i], cTypes[i].GetElementType(), false, typeMapper); } if (!conversions[i].IsValid) { return(null); } } } return(new ApplicableFunction(candidate, cTypes, cTypes, Operand.GetTypes(args, typeMapper), conversions)); } if (candidate.IsParameterArray && args.Length >= cTypes.Length - 1) { Type[] expandedTypes = new Type[args.Length]; Array.Copy(cTypes, expandedTypes, cTypes.Length - 1); Type varType = cTypes[cTypes.Length - 1].GetElementType(); for (int i = cTypes.Length - 1; i < expandedTypes.Length; i++) { expandedTypes[i] = varType; } Conversion[] conversions = new Conversion[args.Length]; for (int i = 0; i < expandedTypes.Length; i++) { conversions[i] = Conversion.GetImplicit(args[i], expandedTypes[i], false, typeMapper); if (!conversions[i].IsValid) { return(null); } } return(new ApplicableFunction(candidate, cTypes, expandedTypes, Operand.GetTypes(args, typeMapper), conversions)); } return(null); }
public static ApplicableFunction ValidateCandidate(IMemberInfo candidate, Operand[] args) { Type[] cTypes = candidate.ParameterTypes; bool breaking = false; if (cTypes.Length == args.Length) { Conversion[] conversions = new Conversion[args.Length]; for (int i = 0; i < cTypes.Length; i++) { conversions[i] = Conversion.GetImplicit(args[i], cTypes[i], false); if (!conversions[i].IsValid && !cTypes[i].IsGenericParameter) { breaking = true; break; } } if (!breaking) { return(new ApplicableFunction(candidate, cTypes, cTypes, Operand.GetTypes(args), conversions)); } } if (candidate.IsParameterArray && args.Length >= cTypes.Length - 1) { Type[] expandedTypes = new Type[args.Length]; Array.Copy(cTypes, expandedTypes, cTypes.Length - 1); Type varType = cTypes[cTypes.Length - 1].GetElementType(); for (int i = cTypes.Length - 1; i < expandedTypes.Length; i++) { expandedTypes[i] = varType; } Conversion[] conversions = new Conversion[args.Length]; for (int i = 0; i < expandedTypes.Length; i++) { conversions[i] = Conversion.GetImplicit(args[i], expandedTypes[i], false); if (!conversions[i].IsValid) { return(null); } } return(new ApplicableFunction(candidate, cTypes, expandedTypes, Operand.GetTypes(args), conversions)); } return(null); }
public SwitchBlock(Operand expression, ITypeMapper typeMapper) { _typeMapper = typeMapper; _strCmp = typeMapper.MapType(typeof(string)).GetMethod( "Equals", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeMapper.MapType(typeof(string)), typeMapper.MapType(typeof(string)) }, null); _expression = expression; Type exprType = expression.GetReturnType(typeMapper); foreach (var t in _validTypes) { Type mapped = typeMapper.MapType(t); if (mapped == exprType) { _govType = mapped; break; } } if (_govType == null) { if (exprType.IsEnum) { _govType = Helpers.GetEnumEnderlyingType(exprType); } else { // if a single implicit coversion from expression to one of the valid types exists, it's ok foreach (System.Type t in _validTypes) { Conversion tmp = Conversion.GetImplicit(expression, typeMapper.MapType(t), false, typeMapper); if (tmp.IsValid) { if (_conv == null) { _conv = tmp; _govType = typeMapper.MapType(t); //if (_govType==expression.) } else { throw new AmbiguousMatchException(Messages.ErrAmbiguousSwitchExpression); } } } } } }
static Better GetBetterConversion(Type from, Type left, Type right) { if (left == right) { return(Better.Neither); } if (from == left) { return(Better.Left); } if (from == right) { return(Better.Right); } Conversion lrConv = Conversion.GetImplicit(left, right, false); Conversion rlConv = Conversion.GetImplicit(right, left, false); if (lrConv.IsValid && !rlConv.IsValid) { return(Better.Left); } if (rlConv.IsValid && !lrConv.IsValid) { return(Better.Right); } if (BetterSign(left, right)) { return(Better.Left); } if (BetterSign(right, left)) { return(Better.Right); } return(Better.Neither); }
internal void Convert(Operand op, Type to, bool allowExplicit) { Conversion conv = allowExplicit ? Conversion.GetExplicit(op, to, false) : Conversion.GetImplicit(op, to, false); conv.Emit(this, (object)op == null ? null : op.Type, to); }
protected internal void EmitGetHelper(Operand op, Type desiredType, bool allowExplicitConversion) { if (desiredType.IsByRef) { EmitGetHelper_Ref(op, desiredType); return; } if ((object)op == null) { if (desiredType.IsValueType) { throw new ArgumentNullException(nameof(op)); } IL.Emit(OpCodes.Ldnull); return; } EmitGetHelper_Conversion(op, desiredType, allowExplicitConversion ? Conversion.GetExplicit(op, desiredType, false, TypeMapper) : Conversion.GetImplicit(op, desiredType, false, TypeMapper)); }