本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.BoundExpression.IsDefaultValue方法的典型用法代码示例。如果您正苦于以下问题:C# BoundExpression.IsDefaultValue方法的具体用法?C# BoundExpression.IsDefaultValue怎么用?C# BoundExpression.IsDefaultValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.BoundExpression
的用法示例。
在下文中一共展示了BoundExpression.IsDefaultValue方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RewritePointerElementAccess
private BoundExpression RewritePointerElementAccess(BoundPointerElementAccess node, BoundExpression rewrittenExpression, BoundExpression rewrittenIndex)
{
// Optimization: p[0] == *p
if (rewrittenIndex.IsDefaultValue())
{
return new BoundPointerIndirectionOperator(
node.Syntax,
rewrittenExpression,
node.Type);
}
BinaryOperatorKind additionKind = BinaryOperatorKind.Addition;
switch (rewrittenIndex.Type.SpecialType)
{
case SpecialType.System_Int32:
additionKind |= BinaryOperatorKind.PointerAndIntAddition;
break;
case SpecialType.System_UInt32:
additionKind |= BinaryOperatorKind.PointerAndUIntAddition;
break;
case SpecialType.System_Int64:
additionKind |= BinaryOperatorKind.PointerAndLongAddition;
break;
case SpecialType.System_UInt64:
additionKind |= BinaryOperatorKind.PointerAndULongAddition;
break;
default:
throw ExceptionUtilities.UnexpectedValue(rewrittenIndex.Type.SpecialType);
}
if (node.Checked)
{
additionKind |= BinaryOperatorKind.Checked;
}
return new BoundPointerIndirectionOperator(
node.Syntax,
MakeBinaryOperator(
node.Syntax,
additionKind,
rewrittenExpression,
rewrittenIndex,
rewrittenExpression.Type,
method: null,
isPointerElementAccess: true), //see RewriterPointerNumericOperator
node.Type);
}
示例2: MakeBinaryOperator
//.........这里部分代码省略.........
case BinaryOperatorKind.IntLeftShift:
case BinaryOperatorKind.UIntLeftShift:
case BinaryOperatorKind.IntRightShift:
case BinaryOperatorKind.UIntRightShift:
return RewriteBuiltInShiftOperation(oldNode, syntax, operatorKind, loweredLeft, loweredRight, type, 0x1F);
case BinaryOperatorKind.LongLeftShift:
case BinaryOperatorKind.ULongLeftShift:
case BinaryOperatorKind.LongRightShift:
case BinaryOperatorKind.ULongRightShift:
return RewriteBuiltInShiftOperation(oldNode, syntax, operatorKind, loweredLeft, loweredRight, type, 0x3F);
case BinaryOperatorKind.DecimalAddition:
case BinaryOperatorKind.DecimalSubtraction:
case BinaryOperatorKind.DecimalMultiplication:
case BinaryOperatorKind.DecimalDivision:
case BinaryOperatorKind.DecimalRemainder:
case BinaryOperatorKind.DecimalEqual:
case BinaryOperatorKind.DecimalNotEqual:
case BinaryOperatorKind.DecimalLessThan:
case BinaryOperatorKind.DecimalLessThanOrEqual:
case BinaryOperatorKind.DecimalGreaterThan:
case BinaryOperatorKind.DecimalGreaterThanOrEqual:
return RewriteDecimalBinaryOperation(syntax, loweredLeft, loweredRight, operatorKind);
case BinaryOperatorKind.PointerAndIntAddition:
case BinaryOperatorKind.PointerAndUIntAddition:
case BinaryOperatorKind.PointerAndLongAddition:
case BinaryOperatorKind.PointerAndULongAddition:
case BinaryOperatorKind.PointerAndIntSubtraction:
case BinaryOperatorKind.PointerAndUIntSubtraction:
case BinaryOperatorKind.PointerAndLongSubtraction:
case BinaryOperatorKind.PointerAndULongSubtraction:
if (loweredRight.IsDefaultValue())
{
return loweredLeft;
}
return RewritePointerNumericOperator(syntax, operatorKind, loweredLeft, loweredRight, type, isPointerElementAccess, isLeftPointer: true);
case BinaryOperatorKind.IntAndPointerAddition:
case BinaryOperatorKind.UIntAndPointerAddition:
case BinaryOperatorKind.LongAndPointerAddition:
case BinaryOperatorKind.ULongAndPointerAddition:
if (loweredLeft.IsDefaultValue())
{
return loweredRight;
}
return RewritePointerNumericOperator(syntax, operatorKind, loweredLeft, loweredRight, type, isPointerElementAccess, isLeftPointer: false);
case BinaryOperatorKind.PointerSubtraction:
return RewritePointerSubtraction(operatorKind, loweredLeft, loweredRight, type);
case BinaryOperatorKind.IntAddition:
case BinaryOperatorKind.UIntAddition:
case BinaryOperatorKind.LongAddition:
case BinaryOperatorKind.ULongAddition:
if (loweredLeft.IsDefaultValue())
{
return loweredRight;
}
if (loweredRight.IsDefaultValue())
{
return loweredLeft;
}
goto default;
示例3: MakeConversion
//.........这里部分代码省略.........
{
// We can perform some optimizations if we have a nullable value type
// as the operand and we know its nullability:
// * (object)new int?() is the same as (object)null
// * (object)new int?(123) is the same as (object)123
if (NullableNeverHasValue(rewrittenOperand))
{
return new BoundDefaultOperator(syntax, rewrittenType);
}
BoundExpression nullableValue = NullableAlwaysHasValue(rewrittenOperand);
if (nullableValue != null)
{
// Recurse, eliminating the unnecessary ctor.
return MakeConversion(oldNode, syntax, nullableValue, conversionKind,
symbolOpt, @checked, explicitCastInCode, isExtensionMethod, isArrayIndex,
constantValueOpt, rewrittenType);
}
}
break;
case ConversionKind.NullLiteral:
if (!inExpressionLambda || !explicitCastInCode)
{
return new BoundDefaultOperator(syntax, rewrittenType);
}
break;
case ConversionKind.ImplicitReference:
case ConversionKind.ExplicitReference:
if (rewrittenOperand.IsDefaultValue() && (!inExpressionLambda || !explicitCastInCode))
{
return new BoundDefaultOperator(syntax, rewrittenType);
}
break;
case ConversionKind.ImplicitNumeric:
case ConversionKind.ExplicitNumeric:
if (rewrittenOperand.IsDefaultValue() && (!inExpressionLambda || !explicitCastInCode))
{
return new BoundDefaultOperator(syntax, rewrittenType);
}
if (rewrittenType.SpecialType == SpecialType.System_Decimal || rewrittenOperand.Type.SpecialType == SpecialType.System_Decimal)
{
return RewriteDecimalConversion(oldNode, syntax, rewrittenOperand, rewrittenOperand.Type, rewrittenType);
}
break;
case ConversionKind.ImplicitEnumeration:
// A conversion from constant zero to nullable is actually classified as an
// implicit enumeration conversion, not an implicit nullable conversion.
// Lower it to (E?)(E)0.
if (rewrittenType.IsNullableType())
{
var operand = MakeConversion(
oldNode,
syntax,
rewrittenOperand,
ConversionKind.ImplicitEnumeration,
symbolOpt,
@checked,
示例4: IsNullOrEmptyStringConstant
private static bool IsNullOrEmptyStringConstant(BoundExpression operand)
{
return (operand.ConstantValue != null && string.IsNullOrEmpty(operand.ConstantValue.StringValue)) ||
operand.IsDefaultValue();
}
示例5: MakeNullCoalescingOperator
private BoundExpression MakeNullCoalescingOperator(
CSharpSyntaxNode syntax,
BoundExpression rewrittenLeft,
BoundExpression rewrittenRight,
Conversion leftConversion,
TypeSymbol rewrittenResultType)
{
Debug.Assert(rewrittenLeft != null);
Debug.Assert(rewrittenRight != null);
Debug.Assert(leftConversion.IsValid);
Debug.Assert((object)rewrittenResultType != null);
Debug.Assert(rewrittenRight.Type.Equals(rewrittenResultType, ignoreDynamic: true));
if (_inExpressionLambda)
{
TypeSymbol strippedLeftType = rewrittenLeft.Type.StrippedType();
Conversion rewrittenConversion = MakeConversion(syntax, leftConversion, strippedLeftType, rewrittenResultType);
return new BoundNullCoalescingOperator(syntax, rewrittenLeft, rewrittenRight, rewrittenConversion, rewrittenResultType);
}
// first we can make a small optimization:
// If left is a constant then we already know whether it is null or not. If it is null then we
// can simply generate "right". If it is not null then we can simply generate
// MakeConversion(left).
if (rewrittenLeft.IsDefaultValue())
{
return rewrittenRight;
}
if (rewrittenLeft.ConstantValue != null)
{
Debug.Assert(!rewrittenLeft.ConstantValue.IsNull);
return GetConvertedLeftForNullCoalescingOperator(rewrittenLeft, leftConversion, rewrittenResultType);
}
// if left conversion is intrinsic implicit (always succeeds) and results in a reference type
// we can apply conversion before doing the null check that allows for a more efficient IL emit.
if (rewrittenLeft.Type.IsReferenceType &&
leftConversion.IsImplicit &&
!leftConversion.IsUserDefined)
{
if (!leftConversion.IsIdentity)
{
rewrittenLeft = MakeConversionNode(rewrittenLeft.Syntax, rewrittenLeft, leftConversion, rewrittenResultType, @checked: false);
}
return new BoundNullCoalescingOperator(syntax, rewrittenLeft, rewrittenRight, Conversion.Identity, rewrittenResultType);
}
if (leftConversion.IsIdentity || leftConversion.Kind == ConversionKind.ExplicitNullable)
{
var conditionalAccess = rewrittenLeft as BoundLoweredConditionalAccess;
if (conditionalAccess != null &&
(conditionalAccess.WhenNullOpt == null || NullableNeverHasValue(conditionalAccess.WhenNullOpt)))
{
var notNullAccess = NullableAlwaysHasValue(conditionalAccess.WhenNotNull);
if (notNullAccess != null)
{
var whenNullOpt = rewrittenRight;
if (whenNullOpt.Type.IsNullableType())
{
notNullAccess = conditionalAccess.WhenNotNull;
}
if (whenNullOpt.IsDefaultValue() && whenNullOpt.Type.SpecialType != SpecialType.System_Decimal)
{
whenNullOpt = null;
}
return conditionalAccess.Update(
conditionalAccess.Receiver,
conditionalAccess.HasValueMethodOpt,
whenNotNull: notNullAccess,
whenNullOpt: whenNullOpt,
id: conditionalAccess.Id,
type: rewrittenResultType
);
}
}
}
// We lower left ?? right to
//
// var temp = left;
// (temp != null) ? MakeConversion(temp) : right
//
BoundAssignmentOperator tempAssignment;
BoundLocal boundTemp = _factory.StoreToTemp(rewrittenLeft, out tempAssignment);
// temp != null
BoundExpression nullCheck = MakeNullCheck(syntax, boundTemp, BinaryOperatorKind.NotEqual);
// MakeConversion(temp, rewrittenResultType)
BoundExpression convertedLeft = GetConvertedLeftForNullCoalescingOperator(boundTemp, leftConversion, rewrittenResultType);
Debug.Assert(convertedLeft.Type.Equals(rewrittenResultType, ignoreDynamic: true));
// (temp != null) ? MakeConversion(temp, LeftConversion) : RightOperand
//.........这里部分代码省略.........
示例6: ReadIsSideeffecting
// a simple check for common non-side-effecting expressions
internal static bool ReadIsSideeffecting(
BoundExpression expression)
{
if (expression.ConstantValue != null)
{
return false;
}
if (expression.IsDefaultValue())
{
return false;
}
switch (expression.Kind)
{
case BoundKind.ThisReference:
case BoundKind.BaseReference:
case BoundKind.Literal:
case BoundKind.Parameter:
case BoundKind.Local:
case BoundKind.Lambda:
return false;
case BoundKind.Conversion:
var conv = (BoundConversion)expression;
return conv.ConversionHasSideEffects() ||
ReadIsSideeffecting(conv.Operand);
case BoundKind.ObjectCreationExpression:
// common production of lowered conversions to nullable
// new S?(arg)
if (expression.Type.IsNullableType())
{
var objCreation = (BoundObjectCreationExpression)expression;
return objCreation.Arguments.Length == 1 && ReadIsSideeffecting(objCreation.Arguments[0]);
}
return true;
case BoundKind.Call:
var call = (BoundCall)expression;
var method = call.Method;
// common production of lowered lifted operators
// GetValueOrDefault is known to be not sideeffecting.
if (method.ContainingType?.IsNullableType() == true)
{
if (IsSpecialMember(method, SpecialMember.System_Nullable_T_GetValueOrDefault) ||
IsSpecialMember(method, SpecialMember.System_Nullable_T_get_HasValue))
{
return ReadIsSideeffecting(call.ReceiverOpt);
}
}
return true;
default:
return true;
}
}
示例7: CanChangeValueBetweenReads
/// <summary>
/// Variables local to current frame do not need temps when re-read multiple times
/// as long as there is no code that may write to locals in between accesses and they
/// are not captured.
///
/// Example:
/// l += foo(ref l);
///
/// even though l is a local, we must access it via a temp since "foo(ref l)" may change it
/// on between accesses.
/// </summary>
internal static bool CanChangeValueBetweenReads(
BoundExpression expression,
bool localsMayBeAssignedOrCaptured = true)
{
if (expression.IsDefaultValue())
{
return false;
}
if (expression.ConstantValue != null)
{
var type = expression.Type;
return !ConstantValueIsTrivial(type);
}
switch (expression.Kind)
{
case BoundKind.ThisReference:
case BoundKind.BaseReference:
return false;
case BoundKind.Literal:
var type = expression.Type;
return !ConstantValueIsTrivial(type);
case BoundKind.Parameter:
return localsMayBeAssignedOrCaptured || ((BoundParameter)expression).ParameterSymbol.RefKind != RefKind.None;
case BoundKind.Local:
return localsMayBeAssignedOrCaptured || ((BoundLocal)expression).LocalSymbol.RefKind != RefKind.None;
default:
return true;
}
}
示例8: ReadIsSideeffecting
// a simple check for common nonsideeffecting expressions
internal static bool ReadIsSideeffecting(
BoundExpression expression)
{
if (expression.ConstantValue != null)
{
return false;
}
if (expression.IsDefaultValue())
{
return false;
}
switch (expression.Kind)
{
case BoundKind.ThisReference:
case BoundKind.BaseReference:
case BoundKind.Literal:
case BoundKind.Parameter:
case BoundKind.Local:
case BoundKind.Lambda:
return false;
case BoundKind.Conversion:
var conv = (BoundConversion)expression;
return conv.ConversionHasSideEffects() ||
ReadIsSideeffecting(conv.Operand);
case BoundKind.ObjectCreationExpression:
// common production of lowered conversions to nullable
// new S?(arg)
if (expression.Type.IsNullableType())
{
var objCreation = (BoundObjectCreationExpression)expression;
return objCreation.Arguments.Length == 1 && ReadIsSideeffecting(objCreation.Arguments[0]);
}
return false;
default:
return true;
}
}