Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3478bae

Browse files
JonHannaVSadov
authored andcommitted
Simplify some casting and conversion related code in Microsoft.CSharp (#25357)
* Remove pDestinationTypeForLambdaErrorReporting from conversions. Stored in a field and passed around, but never used for anything beside * Don't use ExprClass in casts and typeof Acts as a wrapper around the type, but provides nothing beyond that wrapper, so it is just unwrapped again. (Visiting an ExprClass is always a nop, so that's not a reason to wrap it). Use the CType directly instead. * Make visiting ExprClass explicitly just return it directly. This is what happens anyway, but since the previous change depends on this, making this clear makes that being correct clearer, as well as a minor efficiency gain. * Make ExprTypeOf.SourceType init-only. As never changed now.
1 parent 4fc3974 commit 3478bae

File tree

11 files changed

+113
-205
lines changed

11 files changed

+113
-205
lines changed

src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/ExpressionTreeCallRewriter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ private Expression GenerateConvert(ExprCall pExpr)
326326
ExprList list = (ExprList)pExpr.OptionalArguments;
327327
ExprList list2 = (ExprList)list.OptionalNextListNode;
328328
e = GetExpression(list.OptionalElement);
329-
t = ((ExprTypeOf)list2.OptionalElement).SourceType.Type.AssociatedSystemType;
329+
t = ((ExprTypeOf)list2.OptionalElement).SourceType.AssociatedSystemType;
330330

331331
if (e.Type.MakeByRefType() == t)
332332
{
@@ -354,7 +354,7 @@ private Expression GenerateConvert(ExprCall pExpr)
354354
ExprList list = (ExprList)pExpr.OptionalArguments;
355355

356356
e = GetExpression(list.OptionalElement);
357-
t = ((ExprTypeOf)list.OptionalNextListNode).SourceType.Type.AssociatedSystemType;
357+
t = ((ExprTypeOf)list.OptionalNextListNode).SourceType.AssociatedSystemType;
358358

359359
if (e.Type.MakeByRefType() == t)
360360
{
@@ -470,7 +470,7 @@ private Expression GenerateConstantType(ExprCall pExpr)
470470

471471
return Expression.Constant(
472472
GetObject(list.OptionalElement),
473-
((ExprTypeOf)list.OptionalNextListNode).SourceType.Type.AssociatedSystemType);
473+
((ExprTypeOf)list.OptionalNextListNode).SourceType.AssociatedSystemType);
474474
}
475475

476476
/////////////////////////////////////////////////////////////////////////////////
@@ -784,7 +784,7 @@ private Expression GetExpression(Expr pExpr)
784784
ExprList list = (ExprList)call.OptionalArguments;
785785
return
786786
Expression.NewArrayInit(
787-
((ExprTypeOf)list.OptionalElement).SourceType.Type.AssociatedSystemType,
787+
((ExprTypeOf)list.OptionalElement).SourceType.AssociatedSystemType,
788788
GetArgumentsFromArrayInit((ExprArrayInit)list.OptionalNextListNode));
789789
}
790790

@@ -885,7 +885,7 @@ private object GetObject(Expr pExpr)
885885
}
886886
else if (pExpr is ExprTypeOf typeOf)
887887
{
888-
return typeOf.SourceType.Type.AssociatedSystemType;
888+
return typeOf.SourceType.AssociatedSystemType;
889889
}
890890
else if (pExpr is ExprMethodInfo methodInfo)
891891
{

src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Conversion.cs

Lines changed: 44 additions & 90 deletions
Large diffs are not rendered by default.

src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/ExplicitConversion.cs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ private sealed class ExplicitConversion
1919
private Expr _exprSrc;
2020
private readonly CType _typeSrc;
2121
private readonly CType _typeDest;
22-
private readonly ExprClass _exprTypeDest;
2322

2423
// This is for lambda error reporting. The reason we have this is because we
2524
// store errors for lambda conversions, and then we don't bind the conversion
@@ -36,7 +35,6 @@ private sealed class ExplicitConversion
3635
// to report the error on, so that when the lambda conversion fails, it reports
3736
// errors on the correct type.
3837

39-
private readonly CType _pDestinationTypeForLambdaErrorReporting;
4038
private Expr _exprDest;
4139
private readonly bool _needsExprDest;
4240
private readonly CONVERTTYPE _flags;
@@ -45,14 +43,12 @@ private sealed class ExplicitConversion
4543
// BindExplicitConversion
4644
// ----------------------------------------------------------------------------
4745

48-
public ExplicitConversion(ExpressionBinder binder, Expr exprSrc, CType typeSrc, ExprClass typeDest, CType pDestinationTypeForLambdaErrorReporting, bool needsExprDest, CONVERTTYPE flags)
46+
public ExplicitConversion(ExpressionBinder binder, Expr exprSrc, CType typeSrc, CType typeDest, bool needsExprDest, CONVERTTYPE flags)
4947
{
5048
_binder = binder;
5149
_exprSrc = exprSrc;
5250
_typeSrc = typeSrc;
53-
_typeDest = typeDest.Type;
54-
_pDestinationTypeForLambdaErrorReporting = pDestinationTypeForLambdaErrorReporting;
55-
_exprTypeDest = typeDest;
51+
_typeDest = typeDest;
5652
_needsExprDest = needsExprDest;
5753
_flags = flags;
5854
_exprDest = null;
@@ -100,7 +96,7 @@ public bool Bind()
10096
// The set of explicit conversions includes all implicit conversions.
10197

10298
// Don't try user-defined conversions now because we'll try them again later.
103-
if (_binder.BindImplicitConversion(_exprSrc, _typeSrc, _exprTypeDest, _pDestinationTypeForLambdaErrorReporting, _needsExprDest, out _exprDest, _flags | CONVERTTYPE.ISEXPLICIT))
99+
if (_binder.BindImplicitConversion(_exprSrc, _typeSrc, _typeDest, _needsExprDest, out _exprDest, _flags | CONVERTTYPE.ISEXPLICIT))
104100
{
105101
return true;
106102
}
@@ -194,7 +190,7 @@ private bool bindExplicitConversionFromNub()
194190

195191
// If S and T are value types and there is a builtin conversion from S => T then there is an
196192
// explicit conversion from S? => T that throws on null.
197-
if (_typeDest.IsValType() && _binder.BindExplicitConversion(null, _typeSrc.StripNubs(), _exprTypeDest, _pDestinationTypeForLambdaErrorReporting, _flags | CONVERTTYPE.NOUDC))
193+
if (_typeDest.IsValType() && _binder.BindExplicitConversion(null, _typeSrc.StripNubs(), _typeDest, _flags | CONVERTTYPE.NOUDC))
198194
{
199195
if (_needsExprDest)
200196
{
@@ -205,7 +201,7 @@ private bool bindExplicitConversionFromNub()
205201
}
206202

207203
Debug.Assert(valueSrc.Type == _typeSrc.StripNubs());
208-
if (!_binder.BindExplicitConversion(valueSrc, valueSrc.Type, _exprTypeDest, _pDestinationTypeForLambdaErrorReporting, _needsExprDest, out _exprDest, _flags | CONVERTTYPE.NOUDC))
204+
if (!_binder.BindExplicitConversion(valueSrc, valueSrc.Type, _typeDest, _needsExprDest, out _exprDest, _flags | CONVERTTYPE.NOUDC))
209205
{
210206
Debug.Fail("BindExplicitConversion failed unexpectedly");
211207
return false;
@@ -263,7 +259,7 @@ private bool bindExplicitConversionFromArrayToIList()
263259
}
264260

265261
if (_needsExprDest)
266-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
262+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
267263
return true;
268264
}
269265

@@ -304,7 +300,7 @@ private bool bindExplicitConversionFromIListToArray(ArrayType arrayDest)
304300
return false;
305301
}
306302
if (_needsExprDest)
307-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
303+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
308304
return true;
309305
}
310306

@@ -330,7 +326,7 @@ private bool bindExplicitConversionFromArrayToArray(ArrayType arraySrc, ArrayTyp
330326
if (CConversions.FExpRefConv(GetSymbolLoader(), arraySrc.GetElementType(), arrayDest.GetElementType()))
331327
{
332328
if (_needsExprDest)
333-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
329+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
334330
return true;
335331
}
336332

@@ -361,7 +357,7 @@ private bool bindExplicitConversionToArray(ArrayType arrayDest)
361357
if (_binder.canConvert(_binder.GetPredefindType(PredefinedType.PT_ARRAY), _typeSrc, CONVERTTYPE.NOUDC))
362358
{
363359
if (_needsExprDest)
364-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
360+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK);
365361
return true;
366362
}
367363
return false;
@@ -380,7 +376,7 @@ private bool bindExplicitConversionToPointer()
380376
if (_typeSrc is PointerType || _typeSrc.fundType() <= FUNDTYPE.FT_LASTINTEGRAL && _typeSrc.isNumericType())
381377
{
382378
if (_needsExprDest)
383-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest);
379+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest);
384380
return true;
385381
}
386382
return false;
@@ -428,7 +424,7 @@ private AggCastResult bindExplicitConversionFromEnumToAggregate(AggregateType ag
428424

429425
if (_exprSrc.GetConst() != null)
430426
{
431-
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _exprTypeDest, _needsExprDest, out _exprDest, true);
427+
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _typeDest, _needsExprDest, out _exprDest, true);
432428
if (result == ConstCastResult.Success)
433429
{
434430
return AggCastResult.Success;
@@ -440,7 +436,7 @@ private AggCastResult bindExplicitConversionFromEnumToAggregate(AggregateType ag
440436
}
441437

442438
if (_needsExprDest)
443-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest);
439+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest);
444440
return AggCastResult.Success;
445441
}
446442

@@ -453,7 +449,7 @@ private AggCastResult bindExplicitConversionFromDecimalToEnum(AggregateType aggT
453449
if (_exprSrc.GetConst() != null)
454450
{
455451
// Fold the constant cast if possible.
456-
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _exprTypeDest, _needsExprDest, out _exprDest, true);
452+
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _typeDest, _needsExprDest, out _exprDest, true);
457453
if (result == ConstCastResult.Success)
458454
{
459455
return AggCastResult.Success; // else, don't fold and use a regular cast, below.
@@ -478,7 +474,7 @@ private AggCastResult bindExplicitConversionFromDecimalToEnum(AggregateType aggT
478474
if (bIsConversionOK)
479475
{
480476
// upcast to the Enum type
481-
_binder.bindSimpleCast(_exprDest, _exprTypeDest, out _exprDest);
477+
_binder.bindSimpleCast(_exprDest, _typeDest, out _exprDest);
482478
}
483479
}
484480
return bIsConversionOK ? AggCastResult.Success : AggCastResult.Failure;
@@ -502,16 +498,15 @@ private AggCastResult bindExplicitConversionFromEnumToDecimal(AggregateType aggT
502498
}
503499
else
504500
{
505-
ExprClass underlyingExpr = GetExprFactory().CreateClass(underlyingType);
506-
_binder.bindSimpleCast(_exprSrc, underlyingExpr, out exprCast);
501+
_binder.bindSimpleCast(_exprSrc, underlyingType, out exprCast);
507502
}
508503

509504
// There is always an implicit conversion from any integral type to decimal.
510505

511506
if (exprCast.GetConst() != null)
512507
{
513508
// Fold the constant cast if possible.
514-
ConstCastResult result = _binder.bindConstantCast(exprCast, _exprTypeDest, _needsExprDest, out _exprDest, true);
509+
ConstCastResult result = _binder.bindConstantCast(exprCast, _typeDest, _needsExprDest, out _exprDest, true);
515510
if (result == ConstCastResult.Success)
516511
{
517512
return AggCastResult.Success; // else, don't fold and use a regular cast, below.
@@ -558,7 +553,7 @@ private AggCastResult bindExplicitConversionToEnum(AggregateType aggTypeDest)
558553
// Transform constant to constant.
559554
if (_exprSrc.GetConst() != null)
560555
{
561-
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _exprTypeDest, _needsExprDest, out _exprDest, true);
556+
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _typeDest, _needsExprDest, out _exprDest, true);
562557
if (result == ConstCastResult.Success)
563558
{
564559
return AggCastResult.Success;
@@ -569,14 +564,14 @@ private AggCastResult bindExplicitConversionToEnum(AggregateType aggTypeDest)
569564
}
570565
}
571566
if (_needsExprDest)
572-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest);
567+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest);
573568
return AggCastResult.Success;
574569
}
575570
else if (_typeSrc.isPredefined() &&
576571
(_typeSrc.isPredefType(PredefinedType.PT_OBJECT) || _typeSrc.isPredefType(PredefinedType.PT_VALUE) || _typeSrc.isPredefType(PredefinedType.PT_ENUM)))
577572
{
578573
if (_needsExprDest)
579-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_UNBOX);
574+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_UNBOX);
580575
return AggCastResult.Success;
581576
}
582577
return AggCastResult.Failure;
@@ -620,7 +615,7 @@ private AggCastResult bindExplicitConversionBetweenSimpleTypes(AggregateType agg
620615
if (_exprSrc.GetConst() != null)
621616
{
622617
// Fold the constant cast if possible.
623-
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _exprTypeDest, _needsExprDest, out _exprDest, true);
618+
ConstCastResult result = _binder.bindConstantCast(_exprSrc, _typeDest, _needsExprDest, out _exprDest, true);
624619
if (result == ConstCastResult.Success)
625620
{
626621
return AggCastResult.Success; // else, don't fold and use a regular cast, below.
@@ -644,7 +639,7 @@ private AggCastResult bindExplicitConversionBetweenSimpleTypes(AggregateType agg
644639
}
645640
else
646641
{
647-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, (_flags & CONVERTTYPE.CHECKOVERFLOW) != 0 ? EXPRFLAG.EXF_CHECKOVERFLOW : 0);
642+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, (_flags & CONVERTTYPE.CHECKOVERFLOW) != 0 ? EXPRFLAG.EXF_CHECKOVERFLOW : 0);
648643
}
649644
}
650645
return bConversionOk ? AggCastResult.Success : AggCastResult.Failure;
@@ -681,11 +676,11 @@ private AggCastResult bindExplicitConversionBetweenAggregates(AggregateType aggT
681676
{
682677
if (aggDest.IsValueType() && aggSrc.getThisType().fundType() == FUNDTYPE.FT_REF)
683678
{
684-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_UNBOX);
679+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_UNBOX);
685680
}
686681
else
687682
{
688-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK | (_exprSrc?.Flags & EXPRFLAG.EXF_CANTBENULL ?? 0));
683+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK | (_exprSrc?.Flags & EXPRFLAG.EXF_CANTBENULL ?? 0));
689684
}
690685
}
691686
return AggCastResult.Success;
@@ -697,7 +692,7 @@ private AggCastResult bindExplicitConversionBetweenAggregates(AggregateType aggT
697692
CConversions.HasGenericDelegateExplicitReferenceConversion(GetSymbolLoader(), _typeSrc, aggTypeDest))
698693
{
699694
if (_needsExprDest)
700-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK | (_exprSrc?.Flags & EXPRFLAG.EXF_CANTBENULL ?? 0));
695+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest, EXPRFLAG.EXF_REFCHECK | (_exprSrc?.Flags & EXPRFLAG.EXF_CANTBENULL ?? 0));
701696
return AggCastResult.Success;
702697
}
703698
return AggCastResult.Failure;
@@ -716,7 +711,7 @@ private AggCastResult bindExplicitConversionFromPointerToInt(AggregateType aggTy
716711
return AggCastResult.Failure;
717712
}
718713
if (_needsExprDest)
719-
_binder.bindSimpleCast(_exprSrc, _exprTypeDest, out _exprDest);
714+
_binder.bindSimpleCast(_exprSrc, _typeDest, out _exprDest);
720715
return AggCastResult.Success;
721716
}
722717

src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/ExprFactory.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public ExprMemberGroup CreateMemGroup(Expr obj, MethPropWithInst method)
5151
public ExprUserDefinedConversion CreateUserDefinedConversion(Expr arg, Expr call, MethWithInst method) =>
5252
new ExprUserDefinedConversion(arg, call, method);
5353

54-
public ExprCast CreateCast(CType type, Expr argument) => CreateCast(0, CreateClass(type), argument);
54+
public ExprCast CreateCast(CType type, Expr argument) => CreateCast(0, type, argument);
5555

56-
public ExprCast CreateCast(EXPRFLAG flags, ExprClass type, Expr argument) => new ExprCast(flags, type, argument);
56+
public ExprCast CreateCast(EXPRFLAG flags, CType type, Expr argument) => new ExprCast(flags, type, argument);
5757

5858
public ExprReturn CreateReturn(Expr optionalObject) => new ExprReturn(optionalObject);
5959

@@ -81,10 +81,9 @@ public ExprPropertyInfo CreatePropertyInfo(PropertySymbol prop, AggregateType pr
8181
public ExprFieldInfo CreateFieldInfo(FieldSymbol field, AggregateType fieldType) =>
8282
new ExprFieldInfo(field, fieldType, Types.GetPredefAgg(PredefinedType.PT_FIELDINFO).getThisType());
8383

84-
private ExprTypeOf CreateTypeOf(ExprClass sourceType) =>
84+
public ExprTypeOf CreateTypeOf(CType sourceType) =>
8585
new ExprTypeOf(Types.GetPredefAgg(PredefinedType.PT_TYPE).getThisType(), sourceType);
8686

87-
public ExprTypeOf CreateTypeOf(CType sourceType) => CreateTypeOf(CreateClass(sourceType));
8887

8988
public ExprUserLogicalOp CreateUserLogOp(CType type, Expr trueFalseCall, ExprCall operatorCall) =>
9089
new ExprUserLogicalOp(type, trueFalseCall, operatorCall);
@@ -126,7 +125,7 @@ public Expr CreateZeroInit(CType type)
126125
case FUNDTYPE.FT_PTR:
127126
{
128127
// Just allocate a new node and fill it in.
129-
return CreateCast(0, CreateClass(type), CreateNull());
128+
return CreateCast(0, type, CreateNull());
130129
}
131130

132131
case FUNDTYPE.FT_STRUCT:

0 commit comments

Comments
 (0)