本文整理汇总了C#中AstExpression.CopyFrom方法的典型用法代码示例。如果您正苦于以下问题:C# AstExpression.CopyFrom方法的具体用法?C# AstExpression.CopyFrom怎么用?C# AstExpression.CopyFrom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AstExpression
的用法示例。
在下文中一共展示了AstExpression.CopyFrom方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertCastclass
/// <summary>
/// Convert node with code Cast.
/// </summary>
private static void ConvertCastclass(AssemblyCompiler compiler, AstExpression node, XTypeSystem typeSystem)
{
var type = (XTypeReference) node.Operand;
if (type.IsSystemArray())
{
// Call cast method
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var castToArray = arrayHelper.Methods.First(x => x.Name == "CastToArray");
var castToArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, castToArray, node.Arguments).SetType(type);
node.CopyFrom(castToArrayExpr);
return;
}
string castMethod = null;
if (type.IsSystemCollectionsIEnumerable())
{
castMethod = "CastToEnumerable";
}
else if (type.IsSystemCollectionsICollection())
{
castMethod = "CastToCollection";
}
else if (type.IsSystemCollectionsIList())
{
castMethod = "CastToList";
}
else if (type.IsSystemIFormattable())
{
castMethod = "CastToFormattable";
}
if (castMethod != null)
{
// Call cast method
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var castToArray = arrayHelper.Methods.First(x => x.Name == castMethod);
// Call "(x instanceof T) ? (T)x : asMethod(x)"
// "instanceof x"
var instanceofExpr = new AstExpression(node.SourceLocation, AstCode.SimpleInstanceOf, type, node.Arguments[0]).SetType(typeSystem.Bool);
// CastX(x)
var castXExpr = new AstExpression(node.SourceLocation, AstCode.Call, castToArray, node.Arguments[0]).SetType(typeSystem.Object);
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, type, node.Arguments[0]).SetType(type);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, type, instanceofExpr, txExpr, castXExpr).SetType(type);
node.CopyFrom(conditional);
return;
}
// Normal castclass
node.Code = AstCode.SimpleCastclass;
}
示例2: ConvertAsNativeIFormattable
private static void ConvertAsNativeIFormattable(AstExpression node, XTypeSystem typeSystem)
{
var method = (XMethodReference)node.Operand;
var type = method.ReturnType;
if (method.Name == "AsNativeIFormattable"
&& method.DeclaringType.Name == InternalConstants.CompilerHelperName
&& type.FullName == "System.IFormattable")
{
// make sure we don't evaluate the expression twice.
var tempVar = new AstGeneratedVariable("temp$$", null) { Type = typeSystem.Object };
var storeTempVar = new AstExpression(node.SourceLocation, AstCode.Stloc, tempVar, node.Arguments[0]) { ExpectedType = typeSystem.Object };
var loadTempVar = new AstExpression(node.SourceLocation, AstCode.Ldloc, tempVar).SetType(typeSystem.Object);
// Convert to "(x instanceof T) ? (T)x : null"
// "instanceof x"
var instanceofExpr = new AstExpression(node.SourceLocation, AstCode.SimpleInstanceOf, type, storeTempVar).SetType(typeSystem.Bool);
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, type, loadTempVar).SetType(type);
// null
var nullExpr = new AstExpression(node.SourceLocation, AstCode.Ldnull, null).SetType(type);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, type,
instanceofExpr, txExpr, nullExpr).SetType(type);
node.CopyFrom(conditional);
}
}
示例3: ConvertInstanceOf
/// <summary>
/// Convert node with code InstanceOf.
/// </summary>
private static void ConvertInstanceOf(AssemblyCompiler compiler, AstExpression node, XTypeSystem typeSystem)
{
var type = (XTypeReference)node.Operand;
if (type.IsSystemArray()) // "is System.Array"
{
// Call ArrayHelper.IsArray
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var isArray = arrayHelper.Methods.First(x => x.Name == "IsArray");
var isArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, isArray, node.Arguments).SetType(typeSystem.Bool);
node.CopyFrom(isArrayExpr);
return;
}
// make sure we don't evaluate the expression twice.
var tempVar = new AstGeneratedVariable("temp$$", null) { Type = compiler.Module.TypeSystem.Object };
var storeTempVar = new AstExpression(node.SourceLocation, AstCode.Stloc, tempVar, node.Arguments[0]) { ExpectedType = compiler.Module.TypeSystem.Object };
var loadTempVar = new AstExpression(node.SourceLocation, AstCode.Ldloc, tempVar).SetType(compiler.Module.TypeSystem.Object);
if (type.IsSystemCollectionsIEnumerable() ||
type.IsSystemCollectionsICollection() ||
type.IsSystemCollectionsIList())
{
// Call "(is x) || IsArray(x)"
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var isArray = arrayHelper.Methods.First(x => x.Name == "IsArray" && x.Parameters.Count == 1);
// "is"
var isExpr = new AstExpression(node).SetArguments(storeTempVar).SetCode(AstCode.SimpleInstanceOf);
// Call IsArray
var isArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, isArray, loadTempVar).SetType(typeSystem.Bool);
// Combined
var combined = new AstExpression(node.SourceLocation, AstCode.Or, null, isExpr, isArrayExpr).SetType(typeSystem.Bool);
node.CopyFrom(combined);
return;
}
if (type.IsSystemCollectionsIEnumerableT() ||
type.IsSystemCollectionsICollectionT() ||
type.IsSystemCollectionsIListT())
{
// TODO: implement InstanceOf with type check for array types.
// (is that even possible here?)
}
if (type.IsSystemIFormattable())
{
// Call "(is x) || IsFormattable(x)"
var formattable = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var isFormattable = formattable.Methods.First(x => x.Name == "IsVirtualFormattable");
// "is"
var isExpr = new AstExpression(node).SetArguments(storeTempVar).SetCode(AstCode.SimpleInstanceOf);
// Call IsFormattable
var isFormattableExpr = new AstExpression(node.SourceLocation, AstCode.Call, isFormattable, loadTempVar).SetType(typeSystem.Bool);
// Combined
var combined = new AstExpression(node.SourceLocation, AstCode.Or, null, isExpr, isFormattableExpr).SetType(typeSystem.Bool);
node.CopyFrom(combined);
return;
}
// Normal instanceof
node.Code = AstCode.SimpleInstanceOf;
}
示例4: ConvertIsInst
/// <summary>
/// Convert node with code IsInst.
/// </summary>
private static void ConvertIsInst(AssemblyCompiler compiler, AstExpression node, XTypeSystem typeSystem)
{
var type = (XTypeReference)node.Operand;
if (type.IsSystemArray())
{
// Call ArrayHelper.AsArray
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var asArray = arrayHelper.Methods.First(x => x.Name == "AsArray");
var asArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, asArray, node.Arguments).SetType(typeSystem.Bool);
node.CopyFrom(asArrayExpr);
return;
}
string asMethod = GetCollectionConvertMethodName(type);
if (asMethod != null)
{
asMethod = "As" + asMethod;
}
else if (type.IsSystemIFormattable())
{
asMethod = "AsFormattable";
}
// make sure we don't evaluate the expression twice.
var tempVar = new AstGeneratedVariable("temp$$", null) { Type = compiler.Module.TypeSystem.Object };
var storeTempVar = new AstExpression(node.SourceLocation, AstCode.Stloc, tempVar, node.Arguments[0]) { ExpectedType = compiler.Module.TypeSystem.Object };
var loadTempVar = new AstExpression(node.SourceLocation, AstCode.Ldloc, tempVar).SetType(compiler.Module.TypeSystem.Object);
if (asMethod != null)
{
// Call "(x instanceof T) ? (T)x : asMethod(x)"
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var asArray = arrayHelper.Methods.First(x => x.Name == asMethod);
// "instanceof x"
var instanceofExpr = new AstExpression(node.SourceLocation, AstCode.SimpleInstanceOf, type, storeTempVar).SetType(typeSystem.Bool);
// AsX(x)
var asXExpr = new AstExpression(node.SourceLocation, AstCode.Call, asArray, loadTempVar).SetType(typeSystem.Object);
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, type, loadTempVar).SetType(type);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, type, instanceofExpr, txExpr, asXExpr).SetType(type);
node.CopyFrom(conditional);
return;
}
// Normal "as": Convert to (x instanceof T) ? (T)x : null
if(!type.IsPrimitive)
{
// "instanceof x"
var instanceofExpr = new AstExpression(node.SourceLocation, AstCode.SimpleInstanceOf, type, storeTempVar).SetType(typeSystem.Bool);
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, type, loadTempVar).SetType(type);
// null
var nullExpr = new AstExpression(node.SourceLocation, AstCode.Ldnull, null).SetType(typeSystem.Object);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, type, instanceofExpr, txExpr, nullExpr).SetType(type);
node.CopyFrom(conditional);
return;
}
else
{
// treat as "x is T"
if(!node.ExpectedType.IsBoolean())
throw new NotImplementedException(); // can this happen?
node.Code = AstCode.SimpleInstanceOf;
}
}
示例5: ConvertUnboxStruct
private static void ConvertUnboxStruct(AstExpression node, XTypeReference resultType, XTypeSystem typeSystem)
{
// Structs must never be null. We have to handle structs here, since a newobj
// might need generic arguments. These would be difficult to provide at "UnboxFromGeneric",
// but will be automatically filled in by the GenericInstanceConverter
// convert to (temp$ = (T)x) != null ? temp$ : default(T)
// replace any unbox, but keep if otherwise.
var clone = node.Code == AstCode.Unbox ? new AstExpression(node.Arguments[0]) : new AstExpression(node);
// make sure we don't evaluate the expression twice.
var tempVar = new AstGeneratedVariable("temp$", "") { Type = typeSystem.Object };
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, resultType, clone)
.SetType(resultType);
// temporary storage
var storeTempVar = new AstExpression(node.SourceLocation, AstCode.Stloc, tempVar, txExpr) { ExpectedType = resultType };
var loadTempVar = new AstExpression(node.SourceLocation, AstCode.Ldloc, tempVar)
.SetType(resultType);
// default (T)
var defaultT = new AstExpression(node.SourceLocation, AstCode.DefaultValue, resultType).SetType(resultType);
var constructor = StructCallConverter.GetDefaultValueCtor(resultType.Resolve());
StructCallConverter.ConvertDefaultValue(defaultT, constructor);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, resultType,
storeTempVar, loadTempVar, defaultT)
.SetType(resultType);
node.CopyFrom(conditional);
}
示例6: ConvertOtherNewObj
/// <summary>
/// Convert a nullable(T) ctor into a convert function.
/// </summary>
private static void ConvertOtherNewObj(AstExpression node, XMethodReference ctor, XTypeReference type, AstExpression value)
{
// Clear node
node.CopyFrom(value);
/*node.Arguments.Clear();
node.InferredType = ctor.DeclaringType;
node.ExpectedType = ctor.DeclaringType;
node.Code = AstCode.Box;
node.Operand = type;
node.Arguments.Add(value);*/
}
示例7: ConvertInstanceOf
/// <summary>
/// Convert node with code InstanceOf.
/// </summary>
private static void ConvertInstanceOf(AssemblyCompiler compiler, AstExpression node, XTypeSystem typeSystem)
{
var type = (XTypeReference)node.Operand;
if (type.IsSystemArray()) // "is System.Array"
{
// Call ArrayHelper.IsArray
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var isArray = arrayHelper.Methods.First(x => x.Name == "IsArray");
var isArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, isArray, node.Arguments).SetType(typeSystem.Bool);
node.CopyFrom(isArrayExpr);
return;
}
if (type.IsSystemCollectionsIEnumerable() || type.IsSystemCollectionsICollection() ||
type.IsSystemCollectionsIList())
{
// Call "(is x) || IsArray(x)"
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var isArray = arrayHelper.Methods.First(x => x.Name == "IsArray");
// "is"
var isExpr = new AstExpression(node).SetCode(AstCode.SimpleInstanceOf);
// Call IsArray
var isArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, isArray, node.Arguments).SetType(typeSystem.Bool);
// Combined
var combined = new AstExpression(node.SourceLocation, AstCode.Or, null, isExpr, isArrayExpr).SetType(typeSystem.Bool);
node.CopyFrom(combined);
return;
}
if (type.IsSystemIFormattable())
{
// Call "(is x) || IsFormattable(x)"
var formattable = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var isFormattable = formattable.Methods.First(x => x.Name == "IsVirtualFormattable");
// "is"
var isExpr = new AstExpression(node).SetCode(AstCode.SimpleInstanceOf);
// Call IsFormattable
var isFormattableExpr = new AstExpression(node.SourceLocation, AstCode.Call, isFormattable, node.Arguments).SetType(typeSystem.Bool);
// Combined
var combined = new AstExpression(node.SourceLocation, AstCode.Or, null, isExpr, isFormattableExpr).SetType(typeSystem.Bool);
node.CopyFrom(combined);
return;
}
// Normal instanceof
node.Code = AstCode.SimpleInstanceOf;
}
示例8: ConvertIsInst
/// <summary>
/// Convert node with code IsInst.
/// </summary>
private static void ConvertIsInst(AssemblyCompiler compiler, AstExpression node, XTypeSystem typeSystem)
{
var type = (XTypeReference)node.Operand;
if (type.IsSystemArray())
{
// Call ArrayHelper.AsArray
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var asArray = arrayHelper.Methods.First(x => x.Name == "AsArray");
var asArrayExpr = new AstExpression(node.SourceLocation, AstCode.Call, asArray, node.Arguments).SetType(typeSystem.Bool);
node.CopyFrom(asArrayExpr);
return;
}
string asMethod = null;
if (type.IsSystemCollectionsIEnumerable())
{
asMethod = "AsEnumerable";
}
else if (type.IsSystemCollectionsICollection())
{
asMethod = "AsCollection";
}
else if (type.IsSystemCollectionsIList())
{
asMethod = "AsList";
}
else if (type.IsSystemIFormattable())
{
asMethod = "AsFormattable";
}
if (asMethod != null)
{
// Call "(x instanceof T) ? (T)x : asMethod(x)"
var arrayHelper = compiler.GetDot42InternalType(InternalConstants.CompilerHelperName).Resolve();
var asArray = arrayHelper.Methods.First(x => x.Name == asMethod);
// "instanceof x"
var instanceofExpr = new AstExpression(node.SourceLocation, AstCode.SimpleInstanceOf, type, node.Arguments[0]).SetType(typeSystem.Bool);
// AsX(x)
var asXExpr = new AstExpression(node.SourceLocation, AstCode.Call, asArray, node.Arguments[0]).SetType(typeSystem.Object);
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, type, node.Arguments[0]).SetType(type);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, type, instanceofExpr, txExpr, asXExpr).SetType(type);
node.CopyFrom(conditional);
return;
}
// Normal "as": Convert to (x instanceof T) ? T(x) : null
{
// "instanceof x"
var instanceofExpr = new AstExpression(node.SourceLocation, AstCode.SimpleInstanceOf, type, node.Arguments[0]).SetType(typeSystem.Bool);
// T(x)
var txExpr = new AstExpression(node.SourceLocation, AstCode.SimpleCastclass, type, node.Arguments[0]).SetType(type);
// null
var nullExpr = new AstExpression(node.SourceLocation, AstCode.Ldnull, null).SetType(typeSystem.Object);
// Combine
var conditional = new AstExpression(node.SourceLocation, AstCode.Conditional, type, instanceofExpr, txExpr, nullExpr).SetType(type);
node.CopyFrom(conditional);
return;
}
}
示例9: TransformCompareExchangeToCompareAndSet
private static bool TransformCompareExchangeToCompareAndSet(AstExpression parentExpr, AssemblyCompiler compiler, ref AstExpression interlockedCall)
{
if (parentExpr == null)
{
// as the return value is not used, transformation is possible.
SwapArgumentsAndSetResultType(interlockedCall, compiler);
return true;
}
// Java returns a 'true' on success, while BCL always returns the old value.
// Assuming the comparants are the same, in BLC terms 'ceq' means checking for
// success (i.e. old value is expected value), while 'cne' means checking for
// failure (i.e. value found if different from expected value)
// 'brfalse' a zero comparand mean branch on success, while brtrue means the opposite.
var comparant1 = interlockedCall.Arguments[2];
if (parentExpr.Code == AstCode.Brfalse || parentExpr.Code == AstCode.Brtrue)
{
// compare to not null.
switch (comparant1.Code)
{
case AstCode.Ldnull:
break;
case AstCode.Ldc_I4:
case AstCode.Ldc_I8:
{
if (System.Convert.ToInt64(comparant1.Operand) != 0)
return false;
break;
}
default:
return false;
}
// transformation is possible.
parentExpr.Code = parentExpr.Code == AstCode.Brfalse ? AstCode.Brtrue : AstCode.Brfalse;
SwapArgumentsAndSetResultType(interlockedCall, compiler);
return true;
}
// Cne should work in theory, but I have not actually seen it so I was unable to test.
// Leave it out for safety until actually encountered.
if ( parentExpr.Code != AstCode.Ceq
// parentExpr.Code != AstCode.Cne
&& parentExpr.Code != AstCode.__Beq
&& parentExpr.Code != AstCode.__Bne_Un)
return false;
var comparand2 = parentExpr.Arguments[1];
if (comparant1.Code != comparand2.Code || !Equals(comparant1.Operand, comparand2.Operand))
return false;
switch (comparant1.Code)
{
case AstCode.Ldloc:
case AstCode.Ldnull:
case AstCode.Ldc_I4:
case AstCode.Ldc_I8:
break;
default:
return false;
}
// transformation is possible.
if (parentExpr.Code == AstCode.Ceq)
{
parentExpr.CopyFrom(interlockedCall);
interlockedCall = parentExpr;
}
//else if (parentExpr.Code == AstCode.Cne) // see above.
//{
// parentExpr.Code = AstCode.Not;
// parentExpr.Arguments.RemoveAt(1);
//}
else if (parentExpr.Code == AstCode.__Beq)
{
parentExpr.Code = AstCode.Brtrue;
parentExpr.Arguments.RemoveAt(1);
}
else if (parentExpr.Code == AstCode.__Bne_Un)
{
parentExpr.Code = AstCode.Brfalse;
parentExpr.Arguments.RemoveAt(1);
}
else
{
return false;
}
SwapArgumentsAndSetResultType(interlockedCall, compiler);
return true;
}
示例10: ConvertGetValueOrDefault
/// <summary>
/// Convert a nullable .GetValueOrDefault()
/// </summary>
private static void ConvertGetValueOrDefault(AstExpression node,XTypeReference type, XModule module)
{
var defExpr = node.Arguments.Count == 1
? new AstExpression(node.SourceLocation, AstCode.DefaultValue, type).SetType(type)
: node.Arguments[1];
defExpr.ExpectedType = type;
if (type.IsPrimitive)
{
// replace with obj != null ? unbox(obj) : defExpr
AstExpression compareExpr, valueExpr;
var loadExpr = node.Arguments[0];
ConvertLoad(loadExpr);
loadExpr.InferredType = module.TypeSystem.Object;
loadExpr.ExpectedType = module.TypeSystem.Object;
if (loadExpr.Code != AstCode.Ldloc)
{
// TODO: how can we get the backend to remove/combine these variables again?
var tmpVar = new AstGeneratedVariable("tmp$", null) { Type = module.TypeSystem.Object };
compareExpr = new AstExpression(node.SourceLocation, AstCode.Stloc, tmpVar, loadExpr).SetType(module.TypeSystem.Object);
valueExpr = new AstExpression(node.SourceLocation, AstCode.Ldloc, tmpVar).SetType(module.TypeSystem.Object);
}
else
{
compareExpr = loadExpr;
valueExpr = loadExpr;
}
valueExpr = new AstExpression(node.SourceLocation, AstCode.Unbox, type, valueExpr).SetType(type);
var newNode = new AstExpression(node.SourceLocation, AstCode.Conditional, type,
compareExpr, valueExpr, defExpr)
.SetType(type);
node.CopyFrom(newNode);
}
else
{
// replace with obj ?? defExpr
var loadExpr = node.Arguments[0];
ConvertLoad(loadExpr);
if(!type.IsSame(loadExpr.InferredType))
{
//loadExpr.ExpectedType = type;
// todo: how to get the cast inserted automatically?
loadExpr = new AstExpression(loadExpr.SourceLocation, AstCode.SimpleCastclass, type, loadExpr);
}
var nullCoalescing = new AstExpression(node.SourceLocation, AstCode.NullCoalescing, null, loadExpr, defExpr);
nullCoalescing.InferredType = type;
node.CopyFrom(nullCoalescing);
}
}