当前位置: 首页>>代码示例>>C#>>正文


C# TypeSymbol.IsOfType方法代码示例

本文整理汇总了C#中TypeSymbol.IsOfType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeSymbol.IsOfType方法的具体用法?C# TypeSymbol.IsOfType怎么用?C# TypeSymbol.IsOfType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TypeSymbol的用法示例。


在下文中一共展示了TypeSymbol.IsOfType方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EmitConvert

        /// <summary>
        /// Emits conversion from one CLR type to another using PHP conventions.
        /// </summary>
        /// <param name="from">Type of value on top of evaluation stack.</param>
        /// <param name="fromHint">Type hint in case of a multityple type choices (like PhpValue or PhpNumber or PhpAlias).</param>
        /// <param name="to">Target CLR type.</param>
        public void EmitConvert(TypeSymbol from, TypeRefMask fromHint, TypeSymbol to)
        {
            Contract.ThrowIfNull(from);
            Contract.ThrowIfNull(to);

            // conversion is not needed:
            if (from.SpecialType == to.SpecialType &&
                (from == to || (to.SpecialType != SpecialType.System_Object && from.IsOfType(to))))
            {
                return;
            }

            //
            from = EmitSpecialize(from, fromHint);

            // specialized conversions:
            switch (to.SpecialType)
            {
                case SpecialType.System_Void:
                    EmitPop(from);
                    return;
                case SpecialType.System_Boolean:
                    EmitConvertToBool(from, fromHint);
                    return;
                case SpecialType.System_Int32:
                    EmitConvertToInt(from, fromHint);
                    return;
                case SpecialType.System_Int64:
                    EmitConvertToLong(from, fromHint);
                    return;
                case SpecialType.System_Double:
                    EmitConvertToDouble(from, fromHint);
                    return;
                case SpecialType.System_String:
                    EmitConvertToString(from, fromHint);
                    return;
                case SpecialType.System_Object:
                    EmitConvertToClass(from, fromHint, to);
                    return;
                default:
                    if (to == CoreTypes.PhpValue)
                    {
                        EmitConvertToPhpValue(from, fromHint);
                    }
                    else if (to == CoreTypes.PhpAlias)
                    {
                        EmitConvertToPhpValue(from, fromHint);
                        Emit_PhpValue_MakeAlias();
                    }
                    else if (to == CoreTypes.PhpNumber)
                    {
                        EmitConvertToPhpNumber(from, fromHint);
                    }
                    else if (to == CoreTypes.PhpArray || to == CoreTypes.IPhpEnumerable || to == CoreTypes.IPhpArray)    // TODO: merge into IPhpArray
                    {
                        EmitConvertToPhpArray(from, fromHint);
                    }
                    else if (to == CoreTypes.PhpString)
                    {
                        EmitConvertToPhpString(from, fromHint);
                    }
                    else if (to.IsReferenceType)
                    {
                        EmitConvertToClass(from, fromHint, to);
                    }
                    else if (to.IsEnumType())
                    {
                        EmitConvertToEnum(from, (NamedTypeSymbol)to);
                    }
                    else if (to == CoreTypes.IntStringKey)
                    {
                        EmitConvertToIntStringKey(from, fromHint);
                    }
                    else
                    {
                        break;
                    }
                    return;
            }

            //
            throw new NotImplementedException($"{to}");
        }
开发者ID:iolevel,项目名称:peachpie,代码行数:89,代码来源:CodeGenerator.Convert.cs

示例2: EmitConvertToPhpArray

 /// <summary>
 /// Emits conversion to <c>PhpArray</c>.
 /// </summary>
 public void EmitConvertToPhpArray(TypeSymbol from, TypeRefMask fromHint)
 {
     if (from.IsOfType(CoreTypes.PhpArray))
     {
         return;
     }
     else if (from == CoreTypes.PhpValue)
     {
         EmitCall(ILOpCode.Call, CoreMethods.Operators.ToArray_PhpValue);        // TODO: ToArray(), not AsArray()
     }
     else if (   // TODO: helper method for builtin types
         from.SpecialType != SpecialType.None ||
         from.IsOfType(CoreTypes.PhpResource) || from == CoreTypes.PhpNumber || from == CoreTypes.PhpString)
     {
         EmitConvertToPhpValue(from, fromHint);
         EmitCall(ILOpCode.Call, CoreMethods.PhpArray.New_PhpValue);
     }
     else
     {
         // TODO: object to array (copy its fields to new instance)
         throw new NotImplementedException($"(array){from.Name}");
     }
 }
开发者ID:iolevel,项目名称:peachpie,代码行数:26,代码来源:CodeGenerator.Convert.cs

示例3: EmitConvertToClass

        /// <summary>
        /// Emits conversion to a class object.
        /// </summary>
        /// <param name="from">Type of value on top of the evaluation stack.</param>
        /// <param name="fromHint">Hint in case of multitype value.</param>
        /// <param name="to">Target type.</param>
        private void EmitConvertToClass(TypeSymbol from, TypeRefMask fromHint, TypeSymbol to)
        {
            Contract.ThrowIfNull(from);
            Contract.ThrowIfNull(to);
            Debug.Assert(to.IsReferenceType);   // TODO: structs other than primitive types
            Debug.Assert(to != CoreTypes.PhpAlias);

            // dereference
            if (from == CoreTypes.PhpAlias)
            {
                Emit_PhpAlias_GetValue();
                from = CoreTypes.PhpValue;
            }

            if (from == to)
                return;

            Debug.Assert(to != CoreTypes.PhpArray && to != CoreTypes.PhpString && to != CoreTypes.PhpAlias);

            if (to == CoreTypes.IPhpCallable)
            {
                // (IPhpCallable)
                if (!from.IsEqualToOrDerivedFrom(CoreTypes.IPhpCallable))
                {
                    if (from.SpecialType == SpecialType.System_String)
                    {
                        EmitCall(ILOpCode.Call, CoreMethods.Operators.AsCallable_String);
                    }
                    else if (
                        from.SpecialType == SpecialType.System_Int64 ||
                        from.SpecialType == SpecialType.System_Boolean ||
                        from.SpecialType == SpecialType.System_Double)
                    {
                        throw new ArgumentException($"{from.Name} cannot be converted to a class of type {to.Name}!");  // TODO: ErrCode
                    }
                    else
                    {
                        EmitConvertToPhpValue(from, fromHint);
                        EmitCall(ILOpCode.Call, CoreMethods.Operators.AsCallable_PhpValue);
                    }
                }
                return;
            }

            switch (from.SpecialType)
            {
                case SpecialType.System_Void:
                case SpecialType.System_Int32:
                case SpecialType.System_Int64:
                case SpecialType.System_Boolean:
                case SpecialType.System_Double:
                case SpecialType.System_String:
                    if (to == CoreTypes.Object)
                    {
                        from = EmitConvertToPhpValue(from, fromHint);
                        goto default;
                    }
                    else
                    {
                        throw new ArgumentException($"{from.Name} cannot be converted to a class of type {to.Name}!");  // TODO: ErrCode
                    }
                default:
                    if (from == CoreTypes.PhpValue)
                    {
                        // Convert.ToClass( value )
                        EmitCall(ILOpCode.Call, CoreMethods.Operators.ToClass_PhpValue)
                            .Expect(SpecialType.System_Object);

                        // (T)
                        EmitCastClass(to);
                        return;
                    }
                    if (from == CoreTypes.PhpNumber)
                    {
                        // Object
                        EmitPhpNumberAddr();
                        EmitCall(ILOpCode.Call, CoreMethods.PhpNumber.ToClass)
                            .Expect(SpecialType.System_Object);

                        // (T)
                        EmitCastClass(to);
                        return;
                    }
                    else if (from.IsOfType(CoreTypes.PhpArray))
                    {
                        // (T)PhpArray.ToClass();
                        EmitCastClass(EmitCall(ILOpCode.Call, CoreMethods.PhpArray.ToClass), to);
                        return;
                    }
                    else if (from.IsOfType(CoreTypes.IPhpArray))
                    {
                        // (T)Convert.ToClass(IPhpArray)
                        EmitCastClass(EmitCall(ILOpCode.Call, CoreMethods.Operators.ToClass_IPhpArray), to);
                        return;
//.........这里部分代码省略.........
开发者ID:iolevel,项目名称:peachpie,代码行数:101,代码来源:CodeGenerator.Convert.cs

示例4: EmitConvertToDouble

        public TypeSymbol EmitConvertToDouble(TypeSymbol from, TypeRefMask fromHint)
        {
            Contract.ThrowIfNull(from);

            // dereference
            if (from == CoreTypes.PhpAlias)
            {
                Emit_PhpAlias_GetValue();
                from = CoreTypes.PhpValue;
            }

            from = EmitSpecialize(from, fromHint);
            var dtype = CoreTypes.Double.Symbol;

            switch (from.SpecialType)
            {
                case SpecialType.System_Int32:
                    _il.EmitOpCode(ILOpCode.Conv_r8);   // Int32 -> Double
                    return dtype;

                case SpecialType.System_Int64:
                    _il.EmitOpCode(ILOpCode.Conv_r8);   // Int64 -> Double
                    return dtype;

                case SpecialType.System_Double:
                    // nop
                    return dtype;

                case SpecialType.System_String:
                    return EmitCall(ILOpCode.Call, CoreMethods.Operators.ToDouble_String)
                        .Expect(SpecialType.System_Double);

                default:
                    if (from == CoreTypes.PhpNumber)
                    {
                        EmitPhpNumberAddr();
                        EmitCall(ILOpCode.Call, CoreMethods.PhpNumber.ToDouble);
                        return dtype;
                    }
                    else if (from.IsOfType(CoreTypes.IPhpArray))
                    {
                        // (double)IPhpArray.Count
                        EmitCall(ILOpCode.Callvirt, CoreMethods.IPhpArray.get_Count);
                        _il.EmitOpCode(ILOpCode.Conv_r8);   // Int32 -> Double
                        return dtype;
                    }
                    else if (from == CoreTypes.PhpValue)
                    {
                        return EmitCall(ILOpCode.Call, CoreMethods.Operators.ToDouble_PhpValue);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
            }
        }
开发者ID:iolevel,项目名称:peachpie,代码行数:56,代码来源:CodeGenerator.Convert.cs

示例5: EmitConvertToBool

        public void EmitConvertToBool(TypeSymbol from, TypeRefMask fromHint, bool negation = false)
        {
            // TODO: use {fromHint} to emit casting in compile time

            // dereference
            if (from == CoreTypes.PhpAlias)
            {
                // <PhpAlias>.Value.ToBoolean()
                Emit_PhpAlias_GetValueRef();
                EmitCall(ILOpCode.Call, CoreMethods.PhpValue.ToBoolean);

                // !
                if (negation)
                {
                    EmitLogicNegation();
                }

                //
                return;
            }

            //
            from = EmitSpecialize(from, fromHint);

            //
            switch (from.SpecialType)
            {
                case SpecialType.System_Void:
                    _il.EmitBoolConstant(negation ? true : false);  // (bool)void == false
                    return;

                case SpecialType.System_Boolean:
                case SpecialType.System_Int32:
                    break; // nop

                case SpecialType.System_Int64:
                    _il.EmitOpCode(ILOpCode.Ldc_i4_0, 1);
                    _il.EmitOpCode(ILOpCode.Conv_i8, 0);
                    _il.EmitOpCode(negation ? ILOpCode.Ceq : ILOpCode.Cgt_un);
                    return;

                case SpecialType.System_Double:

                    // r8 == 0.0
                    _il.EmitDoubleConstant(0.0);
                    _il.EmitOpCode(ILOpCode.Ceq);

                    if (!negation)
                    {
                        // !<i4>
                        EmitLogicNegation();
                    }

                    return;

                case SpecialType.System_String:
                    // Convert.ToBoolean(string)
                    EmitCall(ILOpCode.Call, CoreMethods.Operators.ToBoolean_String);
                    break;

                case SpecialType.System_Object:
                    EmitCall(ILOpCode.Call, CoreMethods.Operators.ToBoolean_Object);
                    break;

                case SpecialType.None:
                    if (from == CoreTypes.PhpValue)
                    {
                        // (bool)value
                        EmitCall(ILOpCode.Call, CoreMethods.Operators.ToBoolean_PhpValue);
                        break;
                    }
                    else if (from == CoreTypes.PhpNumber)
                    {
                        EmitPhpNumberAddr();
                        EmitCall(ILOpCode.Call, CoreMethods.PhpNumber.ToBoolean);
                        break;
                    }
                    // TODO: IsOfType(IPhpConvertible) -> (IPhpConvertible).ToBoolean()
                    else if (from == CoreTypes.PhpString)
                    {
                        EmitCall(ILOpCode.Call, CoreMethods.PhpString.ToBoolean);
                        break;
                    }
                    else if (from.IsOfType(CoreTypes.IPhpArray))
                    {
                        // IPhpArray.Count != 0
                        EmitCall(ILOpCode.Callvirt, CoreMethods.IPhpArray.get_Count);
                        _il.EmitOpCode(ILOpCode.Ldc_i4_0, 1);
                        _il.EmitOpCode(negation ? ILOpCode.Ceq : ILOpCode.Cgt_un);
                        return; // negation handled
                    }
                    else if (from.IsReferenceType)
                    {
                        goto case SpecialType.System_Object;
                    }

                    goto default;

                default:
                    throw new NotImplementedException($"(bool){from.Name}");
//.........这里部分代码省略.........
开发者ID:iolevel,项目名称:peachpie,代码行数:101,代码来源:CodeGenerator.Convert.cs

示例6: EmitConvertToLong

        public void EmitConvertToLong(TypeSymbol from, TypeRefMask fromHint)
        {
            Contract.ThrowIfNull(from);

            // dereference
            if (from == CoreTypes.PhpAlias)
            {
                Emit_PhpAlias_GetValue();
                from = CoreTypes.PhpValue;
            }

            //
            from = EmitSpecialize(from, fromHint);

            switch (from.SpecialType)
            {
                case SpecialType.System_Boolean:
                    _il.EmitOpCode(ILOpCode.Conv_i8);   // bool -> Int64
                    return;

                case SpecialType.System_Int32:
                    _il.EmitOpCode(ILOpCode.Conv_i8);   // Int32 -> Int64
                    return;

                case SpecialType.System_Int64:
                    // nop
                    return;

                case SpecialType.System_Double:
                    _il.EmitOpCode(ILOpCode.Conv_i8);   // double -> int64
                    break;

                case SpecialType.System_String:
                    EmitCall(ILOpCode.Call, CoreMethods.Operators.ToLong_String)
                        .Expect(SpecialType.System_Int64);
                    break;

                default:
                    if (from == CoreTypes.PhpNumber)
                    {
                        EmitPhpNumberAddr();
                        EmitCall(ILOpCode.Call, CoreMethods.PhpNumber.ToLong);
                        return;
                    }
                    else if (from.IsOfType(CoreTypes.IPhpArray))
                    {
                        // (long)IPhpArray.Count
                        EmitCall(ILOpCode.Callvirt, CoreMethods.IPhpArray.get_Count);
                        _il.EmitOpCode(ILOpCode.Conv_i8);   // Int32 -> Int64
                        return;
                    }
                    else if (from == CoreTypes.PhpValue)
                    {
                        EmitCall(ILOpCode.Call, CoreMethods.Operators.ToLong_PhpValue);
                        return;
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
            }
        }
开发者ID:iolevel,项目名称:peachpie,代码行数:62,代码来源:CodeGenerator.Convert.cs

示例7: EmitConvertToInt

        public void EmitConvertToInt(TypeSymbol from, TypeRefMask fromHint)
        {
            Contract.ThrowIfNull(from);

            // dereference
            if (from == CoreTypes.PhpAlias)
            {
                Emit_PhpAlias_GetValue();
                from = CoreTypes.PhpValue;
            }

            //
            from = EmitSpecialize(from, fromHint);

            switch (from.SpecialType)
            {
                case SpecialType.System_Int32:
                    return;

                default:

                    if (from.IsOfType(CoreTypes.IPhpArray))
                    {
                        // IPhpArray.Count
                        EmitCall(ILOpCode.Callvirt, CoreMethods.IPhpArray.get_Count);
                    }
                    else
                    {
                        EmitConvertToLong(from, 0);
                        _il.EmitOpCode(ILOpCode.Conv_i4);   // Int64 -> Int32
                    }
                    return;
            }
        }
开发者ID:iolevel,项目名称:peachpie,代码行数:34,代码来源:CodeGenerator.Convert.cs

示例8: EmitConvertToPhpValue

        public static TypeSymbol EmitConvertToPhpValue(TypeSymbol from, TypeRefMask fromHint, ILBuilder il, Emit.PEModuleBuilder module, DiagnosticBag diagnostic)
        {
            Contract.ThrowIfNull(from);

            var compilation = module.Compilation;

            switch (from.SpecialType)
            {
                case SpecialType.System_Boolean:
                    il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_Boolean);
                    break;
                case SpecialType.System_Int32:
                    il.EmitOpCode(ILOpCode.Conv_i8);   // Int32 -> Int64
                    goto case SpecialType.System_Int64; // PhpValue.Create((long)<stack>)
                case SpecialType.System_Int64:
                    il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_Long);
                    break;
                case SpecialType.System_Double:
                    il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_Double);
                    break;
                case SpecialType.System_Void:
                    Emit_PhpValue_Void(il, module, diagnostic);
                    break;
                default:
                    if (from == compilation.CoreTypes.PhpAlias)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpAlias)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.PhpValue)
                    {
                        // nop
                        break;
                    }
                    else if (from == compilation.CoreTypes.String)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_String)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.PhpString)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpString)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.PhpNumber)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpNumber)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from.IsOfType(compilation.CoreTypes.PhpArray))
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpArray)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.IntStringKey)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_IntStringKey)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from.IsReferenceType)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.FromClass_Object)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else
                    {
                        throw new NotImplementedException($"{from.Name}");
                    }
            }

            //
            return compilation.CoreTypes.PhpValue;
        }
开发者ID:iolevel,项目名称:peachpie,代码行数:80,代码来源:CodeGenerator.Convert.cs


注:本文中的TypeSymbol.IsOfType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。