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


C# BinaryOperatorKind.OperandTypes方法代码示例

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


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

示例1: LowerLiftedBuiltInComparisonOperator

        private BoundExpression LowerLiftedBuiltInComparisonOperator(
            CSharpSyntaxNode syntax,
            BinaryOperatorKind kind,
            BoundExpression loweredLeft,
            BoundExpression loweredRight)
        {
            // SPEC: For the equality operators == != :
            // SPEC: The lifted operator considers two null values equal and a null value unequal to
            // SPEC: any non-null value. If both operands are non-null the lifted operator unwraps
            // SPEC: the operands and applies the underlying operator to produce the bool result.
            // SPEC:
            // SPEC: For the relational operators < > <= >= :
            // SPEC: The lifted operator produces the value false if one or both operands
            // SPEC: are null. Otherwise the lifted operator unwraps the operands and
            // SPEC: applies the underlying operator to produce the bool result.

            // Note that this means that x == y is true but x <= y is false if both are null.
            // x <= y is not the same as (x < y) || (x == y).

            // Start with some simple optimizations for cases like one side being null.
            BoundExpression optimized = TrivialLiftedComparisonOperatorOptimizations(syntax, kind, loweredLeft, loweredRight, null);
            if (optimized != null)
            {
                return optimized;
            }

            // We rewrite x == y as 
            //
            // tempx = x; 
            // tempy = y;
            // result = tempx.GetValueOrDefault() == tempy.GetValueOrDefault() ? 
            //          tempx.HasValue == tempy.HasValue : 
            //          false;
            //
            // and x != y as
            //
            // tempx = x; 
            // tempy = y;
            // result = tempx.GetValueOrDefault() == tempy.GetValueOrDefault() ? 
            //          tempx.HasValue != tempy.HasValue : 
            //          true;
            //
            // Otherwise, we rewrite x OP y as
            //
            // tempx = x;
            // tempy = y;
            // result = tempx.GetValueOrDefault() OP tempy.GetValueOrDefault() ?
            //          tempx.HasValue & tempy.HasValue :
            //          false;
            //
            // Note that there is no reason to generate "&&" over "&"; the cost of
            // the added code for the conditional branch would be about the same as simply doing 
            // the bitwise & in the first place.
            //
            // We have not yet optimized the case where we have a known-not-null value on one side, 
            // and an unknown value on the other. In those cases we will still generate a temp, but
            // we will not generate the call to the unnecessary nullable ctor or to GetValueOrDefault.
            // Rather, we will generate the value's temp instead of a call to GetValueOrDefault, and generate
            // literal true for HasValue. The tree construction methods we call will use those constants
            // to eliminate unnecessary branches.

            BoundExpression xNonNull = NullableAlwaysHasValue(loweredLeft);
            BoundExpression yNonNull = NullableAlwaysHasValue(loweredRight);

            BoundAssignmentOperator tempAssignmentX;
            BoundLocal boundTempX = _factory.StoreToTemp(xNonNull ?? loweredLeft, out tempAssignmentX);
            BoundAssignmentOperator tempAssignmentY;
            BoundLocal boundTempY = _factory.StoreToTemp(yNonNull ?? loweredRight, out tempAssignmentY);

            BoundExpression callX_GetValueOrDefault = MakeOptimizedGetValueOrDefault(syntax, boundTempX);
            BoundExpression callY_GetValueOrDefault = MakeOptimizedGetValueOrDefault(syntax, boundTempY);
            BoundExpression callX_HasValue = MakeOptimizedHasValue(syntax, boundTempX);
            BoundExpression callY_HasValue = MakeOptimizedHasValue(syntax, boundTempY);

            // tempx.GetValueOrDefault() == tempy.GetValueOrDefault()
            BinaryOperatorKind operatorKind = kind.Operator();
            BinaryOperatorKind conditionOperator = operatorKind == BinaryOperatorKind.NotEqual ?
                BinaryOperatorKind.Equal :
                operatorKind;
            TypeSymbol boolType = _compilation.GetSpecialType(SpecialType.System_Boolean);

            BoundExpression condition = MakeBinaryOperator(
                syntax: syntax,
                operatorKind: conditionOperator.WithType(kind.OperandTypes()),
                loweredLeft: callX_GetValueOrDefault,
                loweredRight: callY_GetValueOrDefault,
                type: boolType,
                method: null);

            BinaryOperatorKind consequenceOperator;
            switch (operatorKind)
            {
                case BinaryOperatorKind.Equal:
                    consequenceOperator = BinaryOperatorKind.BoolEqual;
                    break;
                case BinaryOperatorKind.NotEqual:
                    consequenceOperator = BinaryOperatorKind.BoolNotEqual;
                    break;
                default:
                    consequenceOperator = BinaryOperatorKind.BoolAnd;
//.........这里部分代码省略.........
开发者ID:Rickinio,项目名称:roslyn,代码行数:101,代码来源:LocalRewriter_BinaryOperator.cs

示例2: VisitBinaryOperator

        private BoundExpression VisitBinaryOperator(BinaryOperatorKind opKind, MethodSymbol methodOpt, TypeSymbol type, BoundExpression left, BoundExpression right)
        {
            bool isChecked, isLifted, requiresLifted;
            string opName = GetBinaryOperatorName(opKind, out isChecked, out isLifted, out requiresLifted);

            // Fix up the null value for a nullable comparison vs null
            if ((object)left.Type == null && left.IsLiteralNull())
            {
                left = _bound.Default(right.Type);
            }
            if ((object)right.Type == null && right.IsLiteralNull())
            {
                right = _bound.Default(left.Type);
            }

            var loweredLeft = Visit(left);
            var loweredRight = Visit(right);

            // Enums are handled as per their promoted underlying type
            switch (opKind.OperandTypes())
            {
                case BinaryOperatorKind.Enum:
                case BinaryOperatorKind.EnumAndUnderlying:
                case BinaryOperatorKind.UnderlyingAndEnum:
                    {
                        var enumOperand = (opKind.OperandTypes() == BinaryOperatorKind.UnderlyingAndEnum) ? right : left;
                        var promotedType = PromotedType(enumOperand.Type.StrippedType().GetEnumUnderlyingType());
                        if (opKind.IsLifted())
                        {
                            promotedType = _nullableType.Construct(promotedType);
                        }

                        loweredLeft = PromoteEnumOperand(left, loweredLeft, promotedType, isChecked);
                        loweredRight = PromoteEnumOperand(right, loweredRight, promotedType, isChecked);

                        var result = MakeBinary(methodOpt, type, isLifted, requiresLifted, opName, loweredLeft, loweredRight);
                        return Demote(result, type, isChecked);
                    }
                default:
                    return MakeBinary(methodOpt, type, isLifted, requiresLifted, opName, loweredLeft, loweredRight);
            }
        }
开发者ID:MichalStrehovsky,项目名称:roslyn,代码行数:42,代码来源:ExpressionLambdaRewriter.cs

示例3: MakeBinaryOperator

        private BoundExpression MakeBinaryOperator(
            BoundBinaryOperator oldNode,
            CSharpSyntaxNode syntax,
            BinaryOperatorKind operatorKind,
            BoundExpression loweredLeft,
            BoundExpression loweredRight,
            TypeSymbol type,
            MethodSymbol method,
            bool isPointerElementAccess = false,
            bool isCompoundAssignment = false,
            BoundUnaryOperator applyParentUnaryOperator = null)
        {
            Debug.Assert(oldNode == null || (oldNode.Syntax == syntax));

            if (_inExpressionLambda)
            {
                switch (operatorKind.Operator() | operatorKind.OperandTypes())
                {
                    case BinaryOperatorKind.ObjectAndStringConcatenation:
                    case BinaryOperatorKind.StringAndObjectConcatenation:
                    case BinaryOperatorKind.StringConcatenation:
                        return RewriteStringConcatenation(syntax, operatorKind, loweredLeft, loweredRight, type);
                    case BinaryOperatorKind.DelegateCombination:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__Combine);
                    case BinaryOperatorKind.DelegateRemoval:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__Remove);
                    case BinaryOperatorKind.DelegateEqual:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__op_Equality);
                    case BinaryOperatorKind.DelegateNotEqual:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__op_Inequality);
                }
            }
            else
            // try to lower the expression.
            {
                if (operatorKind.IsDynamic())
                {
                    Debug.Assert(!isPointerElementAccess);

                    if (operatorKind.IsLogical())
                    {
                        return MakeDynamicLogicalBinaryOperator(syntax, operatorKind, loweredLeft, loweredRight, method, type, isCompoundAssignment, applyParentUnaryOperator);
                    }
                    else
                    {
                        Debug.Assert((object)method == null);
                        return _dynamicFactory.MakeDynamicBinaryOperator(operatorKind, loweredLeft, loweredRight, isCompoundAssignment, type).ToExpression();
                    }
                }

                if (operatorKind.IsLifted())
                {
                    return RewriteLiftedBinaryOperator(syntax, operatorKind, loweredLeft, loweredRight, type, method);
                }

                if (operatorKind.IsUserDefined())
                {
                    return LowerUserDefinedBinaryOperator(syntax, operatorKind, loweredLeft, loweredRight, type, method);
                }

                switch (operatorKind.OperatorWithLogical() | operatorKind.OperandTypes())
                {
                    case BinaryOperatorKind.NullableNullEqual:
                    case BinaryOperatorKind.NullableNullNotEqual:
                        return RewriteNullableNullEquality(syntax, operatorKind, loweredLeft, loweredRight, type);

                    case BinaryOperatorKind.ObjectAndStringConcatenation:
                    case BinaryOperatorKind.StringAndObjectConcatenation:
                    case BinaryOperatorKind.StringConcatenation:
                        return RewriteStringConcatenation(syntax, operatorKind, loweredLeft, loweredRight, type);

                    case BinaryOperatorKind.StringEqual:
                        return RewriteStringEquality(oldNode, syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_String__op_Equality);

                    case BinaryOperatorKind.StringNotEqual:
                        return RewriteStringEquality(oldNode, syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_String__op_Inequality);

                    case BinaryOperatorKind.DelegateCombination:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__Combine);

                    case BinaryOperatorKind.DelegateRemoval:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__Remove);

                    case BinaryOperatorKind.DelegateEqual:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__op_Equality);

                    case BinaryOperatorKind.DelegateNotEqual:
                        return RewriteDelegateOperation(syntax, operatorKind, loweredLeft, loweredRight, type, SpecialMember.System_Delegate__op_Inequality);

                    case BinaryOperatorKind.LogicalBoolAnd:
                        if (loweredRight.ConstantValue == ConstantValue.True) return loweredLeft;
                        if (loweredLeft.ConstantValue == ConstantValue.True) return loweredRight;
                        if (loweredLeft.ConstantValue == ConstantValue.False) return loweredLeft;

                        if (loweredRight.Kind == BoundKind.Local || loweredRight.Kind == BoundKind.Parameter)
                        {
                            operatorKind &= ~BinaryOperatorKind.Logical;
                        }

                        goto default;
//.........这里部分代码省略.........
开发者ID:Rickinio,项目名称:roslyn,代码行数:101,代码来源:LocalRewriter_BinaryOperator.cs

示例4: GetConstantOneForBinOp

 private static ConstantValue GetConstantOneForBinOp(
     BinaryOperatorKind binaryOperatorKind)
 {
     switch (binaryOperatorKind.OperandTypes())
     {
         case BinaryOperatorKind.PointerAndInt:
         case BinaryOperatorKind.Int:
             return ConstantValue.Create(1);
         case BinaryOperatorKind.UInt:
             return ConstantValue.Create(1U);
         case BinaryOperatorKind.Long:
             return ConstantValue.Create(1L);
         case BinaryOperatorKind.ULong:
             return ConstantValue.Create(1LU);
         case BinaryOperatorKind.Float:
             return ConstantValue.Create(1f);
         case BinaryOperatorKind.Double:
             return ConstantValue.Create(1.0);
         case BinaryOperatorKind.Decimal:
             return ConstantValue.Create(1m);
         default:
             throw ExceptionUtilities.UnexpectedValue(binaryOperatorKind.OperandTypes());
     }
 }
开发者ID:Rickinio,项目名称:roslyn,代码行数:24,代码来源:LocalRewriter_UnaryOperator.cs

示例5: LiftedType

        private TypeSymbol LiftedType(BinaryOperatorKind kind)
        {
            Debug.Assert(kind.IsLifted());

            var nullable = Compilation.GetSpecialType(SpecialType.System_Nullable_T);

            switch (kind.OperandTypes())
            {
                case BinaryOperatorKind.Int: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_Int32));
                case BinaryOperatorKind.UInt: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_UInt32));
                case BinaryOperatorKind.Long: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_Int64));
                case BinaryOperatorKind.ULong: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_UInt64));
                case BinaryOperatorKind.Float: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_Single));
                case BinaryOperatorKind.Double: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_Double));
                case BinaryOperatorKind.Decimal: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_Decimal));
                case BinaryOperatorKind.Bool: return nullable.Construct(Compilation.GetSpecialType(SpecialType.System_Boolean));
            }
            Debug.Assert(false, "Bad operator kind in lifted type");
            return null;
        }
开发者ID:riversky,项目名称:roslyn,代码行数:20,代码来源:BuiltInOperators.cs

示例6: ReturnType

 private TypeSymbol ReturnType(BinaryOperatorKind kind)
 {
     if (kind.IsLifted())
     {
         return LiftedType(kind);
     }
     else
     {
         switch (kind.OperandTypes())
         {
             case BinaryOperatorKind.Int: return Compilation.GetSpecialType(SpecialType.System_Int32);
             case BinaryOperatorKind.UInt: return Compilation.GetSpecialType(SpecialType.System_UInt32);
             case BinaryOperatorKind.Long: return Compilation.GetSpecialType(SpecialType.System_Int64);
             case BinaryOperatorKind.ULong: return Compilation.GetSpecialType(SpecialType.System_UInt64);
             case BinaryOperatorKind.Float: return Compilation.GetSpecialType(SpecialType.System_Single);
             case BinaryOperatorKind.Double: return Compilation.GetSpecialType(SpecialType.System_Double);
             case BinaryOperatorKind.Decimal: return Compilation.GetSpecialType(SpecialType.System_Decimal);
             case BinaryOperatorKind.Bool: return Compilation.GetSpecialType(SpecialType.System_Boolean);
             case BinaryOperatorKind.Object: return Compilation.GetSpecialType(SpecialType.System_Object);
             case BinaryOperatorKind.ObjectAndString:
             case BinaryOperatorKind.StringAndObject:
             case BinaryOperatorKind.String:
                 return Compilation.GetSpecialType(SpecialType.System_String);
         }
     }
     Debug.Assert(false, "Bad operator kind in return type");
     return null;
 }
开发者ID:riversky,项目名称:roslyn,代码行数:28,代码来源:BuiltInOperators.cs

示例7: IsFloat

 private static bool IsFloat(BinaryOperatorKind opKind)
 {
     var type = opKind.OperandTypes();
     switch (type)
     {
         case BinaryOperatorKind.Float:
         case BinaryOperatorKind.Double:
             return true;
         default:
             return false;
     }
 }
开发者ID:xier2012,项目名称:roslyn,代码行数:12,代码来源:EmitOperators.cs

示例8: IsConditional

        private static bool IsConditional(BinaryOperatorKind opKind)
        {
            switch (opKind.OperatorWithLogical())
            {
                case BinaryOperatorKind.LogicalAnd:
                case BinaryOperatorKind.LogicalOr:
                case BinaryOperatorKind.Equal:
                case BinaryOperatorKind.NotEqual:
                case BinaryOperatorKind.LessThan:
                case BinaryOperatorKind.LessThanOrEqual:
                case BinaryOperatorKind.GreaterThan:
                case BinaryOperatorKind.GreaterThanOrEqual:
                    return true;

                case BinaryOperatorKind.And:
                case BinaryOperatorKind.Or:
                case BinaryOperatorKind.Xor:
                    return opKind.OperandTypes() == BinaryOperatorKind.Bool;
            }

            return false;
        }
开发者ID:xier2012,项目名称:roslyn,代码行数:22,代码来源:EmitOperators.cs


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