private void checkMethodCallForEventCancellation(IMethodCall call) { // NAVIGATION TODO this code is duplicated from PhoneNavigationTraverser, refactor that if (!call.MethodToCall.Name.Value.StartsWith("set_Cancel")) { return; } if (call.Arguments.Count() != 1 || call.Arguments.ToList()[0].Type != host.PlatformType.SystemBoolean) { return; } ICompileTimeConstant constant = call.Arguments.ToList()[0] as ICompileTimeConstant; if (constant != null && constant.Value != null) { CompileTimeConstant falseConstant = new CompileTimeConstant() { Type = host.PlatformType.SystemBoolean, Value = false, }; if (constant.Value == falseConstant.Value) { return; } } CancelsEvents = true; }
XAttribute GetDescriptionAttribute(IContractElement contractElement) { if (contractElement == null) { return(null); } string contractDescription = null; ICompileTimeConstant descAsCTC = contractElement.Description as ICompileTimeConstant; if (descAsCTC != null && descAsCTC.Value != null) { contractDescription = descAsCTC.Value as string; } if (contractDescription != null) { return(new XAttribute("description", contractDescription)); } else { return(null); } }
/// <summary> /// Returns true if the given constant expression contains a finite numeric value. In other words, infinities and NaN are excluded. /// </summary> /// <param name="constExpression"></param> /// <returns></returns> public static bool IsFiniteNumeric(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Int16: case System.TypeCode.Int32: case System.TypeCode.Int64: case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: case System.TypeCode.UInt64: return(true); case System.TypeCode.Double: var d = ic.ToDouble(null); return(!(Double.IsNaN(d) || Double.IsInfinity(d))); case System.TypeCode.Single: var s = ic.ToSingle(null); return(!(Single.IsNaN(s) || Single.IsInfinity(s))); } return(false); }
private bool isEventCancellationMethodCall(IMethodCall call) { if (!call.MethodToCall.Name.Value.StartsWith("set_Cancel")) { return(false); } if (call.Arguments.Count() != 1 || call.Arguments.ToList()[0].Type != host.PlatformType.SystemBoolean) { return(false); } if (call.ThisArgument == null || !call.ThisArgument.Type.isCancelEventArgsClass(host)) { return(false); } ICompileTimeConstant constant = call.Arguments.ToList()[0] as ICompileTimeConstant; if (constant != null && constant.Value != null) { CompileTimeConstant falseConstant = new CompileTimeConstant() { Type = host.PlatformType.SystemBoolean, Value = false, }; if (constant.Value == falseConstant.Value) { return(false); } } return(true); }
/// <summary> /// checks if argument is locally created URI with static URI target /// </summary> /// <param name="arg"></param> /// <returns></returns> public static bool isArgumentURILocallyCreatedStatic(IExpression arg, IMetadataHost host, out string uri) { uri = null; ICreateObjectInstance creationSite = arg as ICreateObjectInstance; if (creationSite == null) { return(false); } if (!arg.Type.isURIClass(host)) { return(false); } IExpression uriTargetArg = creationSite.Arguments.First(); if (!uriTargetArg.Type.isStringClass(host)) { return(false); } ICompileTimeConstant staticURITarget = uriTargetArg as ICompileTimeConstant; if (staticURITarget == null) { return(false); } uri = staticURITarget.Value as string; return(true); }
public static bool IsIntegralMinusOne(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == -1); case System.TypeCode.Int16: return(ic.ToInt16(null) == -1); case System.TypeCode.Int32: return(ic.ToInt32(null) == -1); case System.TypeCode.Int64: return(ic.ToInt64(null) == -1); case System.TypeCode.Byte: return(ic.ToByte(null) == byte.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == ushort.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == uint.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == ulong.MaxValue); } return(false); }
public static bool IsIntegralZero(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == 0); case System.TypeCode.Int16: return(ic.ToInt16(null) == 0); case System.TypeCode.Int32: return(ic.ToInt32(null) == 0); case System.TypeCode.Int64: return(ic.ToInt64(null) == 0); case System.TypeCode.Byte: return(ic.ToByte(null) == 0); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == 0); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == 0); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == 0); case System.TypeCode.Boolean: return(!ic.ToBoolean(null)); } return(false); }
public static bool IsNumericZero(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == 0); case System.TypeCode.Int16: return(ic.ToInt16(null) == 0); case System.TypeCode.Int32: return(ic.ToInt32(null) == 0); case System.TypeCode.Int64: return(ic.ToInt64(null) == 0); case System.TypeCode.Byte: return(ic.ToByte(null) == 0); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == 0); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == 0); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == 0); case System.TypeCode.Single: return(ic.ToSingle(null) == 0); case System.TypeCode.Double: return(ic.ToDouble(null) == 0); case System.TypeCode.Decimal: return(ic.ToDecimal(null) == 0); } return(false); }
public static CompileTimeConstant /*?*/ SetBooleanTrue(ICompileTimeConstant constExpression) { Contract.Requires(constExpression != null); IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(null); } CompileTimeConstant result = new CompileTimeConstant(constExpression); switch (ic.GetTypeCode()) { case System.TypeCode.SByte: result.Value = (SByte)1; break; case System.TypeCode.Int16: result.Value = (Int16)1; break; case System.TypeCode.Int32: result.Value = (Int32)1; break; case System.TypeCode.Int64: result.Value = (Int64)1; break; case System.TypeCode.Byte: result.Value = (Byte)1; break; case System.TypeCode.UInt16: result.Value = (UInt16)1; break; case System.TypeCode.UInt32: result.Value = (UInt32)1; break; case System.TypeCode.UInt64: result.Value = (UInt64)1; break; default: return(null); } return(result); }
public override void Visit(ICompileTimeConstant constant) { if (Process(constant)) { visitor.Visit(constant); } base.Visit(constant); }
public static bool IsIntegralZero(IExpression expression) { ICompileTimeConstant /*?*/ constExpression = expression as ICompileTimeConstant; if (constExpression == null) { return(false); } return(ExpressionHelper.IsIntegralZero(constExpression)); }
/// <summary> /// Gets the description string from a IContractElement /// </summary> protected static string GetDescription(IContractElement contract) { Contract.Requires(contract != null); ICompileTimeConstant descAsCTC = contract.Description as ICompileTimeConstant; if (descAsCTC != null && descAsCTC.Value != null) { return(descAsCTC.Value as string); } return(null); }
/// <summary> /// checks whether a static URI root (a definite page base) can be extracted from the expression /// </summary> /// <param name="expr"></param> /// <returns></returns> public static bool IsStaticURIRootExtractable(this IExpression expr, out string uri) { // Pre expr.type == string IMethodCall stringConcatExpr = expr as IMethodCall; uri = null; if (stringConcatExpr == null) { return(false); } if (stringConcatExpr.MethodToCall.Name.Value != "Concat") { return(false); } IList <string> constantStrings = new List <string>(); // TODO this misses so many "static" strings, but let's start with this for now IExpression leftOp = stringConcatExpr.Arguments.FirstOrDefault(); while (leftOp != null && leftOp is ICompileTimeConstant) { ICompileTimeConstant strConst = leftOp as ICompileTimeConstant; constantStrings.Add(strConst.Value as string); if (stringConcatExpr.Arguments.ToList()[1] is IMethodCall) { stringConcatExpr = stringConcatExpr.Arguments.ToList()[1] as IMethodCall; leftOp = stringConcatExpr.Arguments.FirstOrDefault(); } else if (stringConcatExpr.Arguments.ToList()[1] is ICompileTimeConstant) { constantStrings.Add((stringConcatExpr.Arguments.ToList()[1] as ICompileTimeConstant).Value as string); break; } else { break; } } if (constantStrings.Count > 0) { uri = constantStrings.Aggregate((aggr, elem) => aggr + elem); return(Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute)); } else { return(false); } }
/// <summary> /// Returns true if the given constant expression contains a finite numeric value. In other words, infinities and NaN are excluded. /// </summary> /// <param name="constExpression"></param> /// <returns></returns> public static bool IsFiniteNumeric(ICompileTimeConstant constExpression) { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Int16: case System.TypeCode.Int32: case System.TypeCode.Int64: case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: case System.TypeCode.UInt64: return true; case System.TypeCode.Double: var d = ic.ToDouble(null); return !(Double.IsNaN(d) || Double.IsInfinity(d)); case System.TypeCode.Single: var s = ic.ToSingle(null); return !(Single.IsNaN(s) || Single.IsInfinity(s)); } return false; }
/// <summary> /// True if the given expression is a compile time constant with a value that is equal to IntPtr.Zero or UIntPtr.Zero. /// </summary> public static bool IsZeroIntPtr(IExpression expression) { ICompileTimeConstant /*?*/ constExpression = expression as ICompileTimeConstant; if (constExpression == null) { return(false); } var value = constExpression.Value; var tc = constExpression.Type.TypeCode; if (tc == PrimitiveTypeCode.IntPtr) { return(((IntPtr)value) == IntPtr.Zero); } else if (tc == PrimitiveTypeCode.UIntPtr) { return(((UIntPtr)value) == UIntPtr.Zero); } else { return(false); } }
/// <summary> /// Returns a shallow copy of the given compile time constant. /// </summary> /// <param name="constant"></param> public CompileTimeConstant Copy(ICompileTimeConstant constant) { Contract.Requires(constant != null); Contract.Ensures(Contract.Result<CompileTimeConstant>() != null); return new CompileTimeConstant(constant); }
/// <summary> /// /// </summary> /// <param name="compileTimeConstant"></param> public CompileTimeConstant(ICompileTimeConstant compileTimeConstant) : base(compileTimeConstant) { this.value = compileTimeConstant.Value; this.Type = compileTimeConstant.Type; }
public override void Visit(ICompileTimeConstant constant) { NewLineAddIndent(); AppendElementType(constant); //output.Append(constant.Value); //AppendSpace(); base.Visit(constant); }
public override void Visit(ICompileTimeConstant constant) { allElements.Add(new InvokInfo(Traverser, "ICompileTimeConstant", constant)); }
public void Visit(ICompileTimeConstant constant) { throw new NotImplementedException(); }
/// <summary> /// /// </summary> public SwitchCase() { this.body = new List<IStatement>(); this.expression = CodeDummy.Constant; this.locations = new List<ILocation>(); }
public void Visit(ICompileTimeConstant constant) { this.result = this.copier.Copy(constant); }
public override void TraverseChildren(ICompileTimeConstant constant) { if (constant.Value == null) { var bplType = sink.CciTypeToBoogie(constant.Type); if (bplType == Bpl.Type.Int) { var lit = Bpl.Expr.Literal(0); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); } else if (bplType == Bpl.Type.Bool) { TranslatedExpressions.Push(Bpl.Expr.False); } else if (bplType == this.sink.Heap.RefType) { TranslatedExpressions.Push(Bpl.Expr.Ident(this.sink.Heap.NullRef)); } else { throw new NotImplementedException(String.Format("Don't know how to translate type: '{0}'", TypeHelper.GetTypeName(constant.Type))); } return; } if (constant.Value is string) { var c = this.sink.FindOrCreateConstant((string)(constant.Value)); TranslatedExpressions.Push(Bpl.Expr.Ident(c)); return; } if (constant.Type.IsEnum) { var lit = Bpl.Expr.Literal((int)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); return; } switch (constant.Type.TypeCode) { case PrimitiveTypeCode.Boolean: // Decompiler might not have converted the constant back to a boolean? Not sure why, // but that's what I'm seeing here. if (constant.Value is bool) { TranslatedExpressions.Push(((bool)constant.Value) ? Bpl.Expr.True : Bpl.Expr.False); } else { TranslatedExpressions.Push(((int)constant.Value) != 0 ? Bpl.Expr.True : Bpl.Expr.False); } break; case PrimitiveTypeCode.Char: { var lit = Bpl.Expr.Literal((int)(char)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.Int8: { var lit = Bpl.Expr.Literal((int)(sbyte)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.Int16: { var lit = Bpl.Expr.Literal((int)(short)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.Int32: { var lit = Bpl.Expr.Literal((int)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.Int64: { var lit = Bpl.Expr.Literal(Microsoft.Basetypes.BigNum.FromLong((long)constant.Value)); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.UInt8: { var lit = Bpl.Expr.Literal((int)(byte)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.UInt16: { var lit = Bpl.Expr.Literal((int)(ushort)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.UInt32: { var lit = Bpl.Expr.Literal((int)(uint)constant.Value); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.UInt64: { var lit = Bpl.Expr.Literal(Microsoft.Basetypes.BigNum.FromULong((ulong)constant.Value)); lit.Type = Bpl.Type.Int; TranslatedExpressions.Push(lit); break; } case PrimitiveTypeCode.Float32: { var c = this.sink.FindOrCreateConstant((float)(constant.Value)); TranslatedExpressions.Push(Bpl.Expr.Ident(c)); break; } case PrimitiveTypeCode.Float64: { var c = this.sink.FindOrCreateConstant((double)(constant.Value)); TranslatedExpressions.Push(Bpl.Expr.Ident(c)); break; } default: throw new NotImplementedException(String.Format("Can't translate compile-time constant of type '{0}'", TypeHelper.GetTypeName(constant.Type))); } return; }
/// <summary> /// Returns a deep copy of the given compile time constant. /// </summary> /// <param name="constant"></param> public CompileTimeConstant Copy(ICompileTimeConstant constant) { var mutableCopy = this.shallowCopier.Copy(constant); this.CopyChildren((Expression)mutableCopy); return mutableCopy; }
/// <summary> /// Returns a shallow copy of the given compile time constant. /// </summary> /// <param name="constant"></param> public CompileTimeConstant Copy(ICompileTimeConstant constant) { return new CompileTimeConstant(constant); }
/// <summary> /// Visits the specified constant. /// </summary> /// <param name="constant">The constant.</param> public override void Visit(ICompileTimeConstant constant) { CompileTimeConstant mutableCompileTimeConstant = new CompileTimeConstant(constant); this.resultExpression = this.myCodeCopier.DeepCopy(mutableCompileTimeConstant); }
/// <summary> /// Obtains the values of AllowMultiple, Inherited and ValidOn by looking at the arguments of the AttributeUsage custom attribute that is supposed to be /// attached to the type of this custom attribute. /// </summary> private void GetUsageInformation() { this.flags = 0x40000000; //Records that the attribute usage has already been determined. INameTable nameTable = this.ContainingBlock.Compilation.NameTable; ITypeDefinition attributeUsageAttribute = this.ContainingBlock.Compilation.PlatformType.SystemAttributeUsageAttribute.ResolvedType; foreach (ICustomAttribute attribute in this.Type.ResolvedType.Attributes) { if (attribute.Type.ResolvedType != attributeUsageAttribute) { continue; } bool firstArgument = true; foreach (IExpression expression in attribute.Arguments) { ICompileTimeConstant /*?*/ cc = expression as ICompileTimeConstant; if (firstArgument) { if (cc != null) { if (cc.Value is int) { //^ assume false; //not really, but we need to shut up a bogus message: Unboxing cast might fail this.flags |= (int)cc.Value; } //else TODO: error? } //else TODO: error? } else { INamedArgument /*?*/ namedArgument = expression as INamedArgument; if (namedArgument != null) { if (namedArgument.ArgumentName.UniqueKey == nameTable.AllowMultiple.UniqueKey) { cc = namedArgument.ArgumentValue as ICompileTimeConstant; if (cc != null && cc.Value is bool) { if (((bool)cc.Value)) { this.flags |= 0x20000000; } }//else TODO: error? } else if (namedArgument.ArgumentName.UniqueKey == nameTable.Inherited.UniqueKey) { cc = namedArgument.ArgumentValue as ICompileTimeConstant; if (cc != null && cc.Value is bool) { if (((bool)cc.Value)) { this.flags |= 0x10000000; } } //else TODO: error? } } //else TODO: error? } } break; } //TODO: what if attribute does not have usage attribute? Error? }
/// <summary> /// Performs some computation with the given compile time constant. /// </summary> /// <param name="constant"></param> public virtual void Visit(ICompileTimeConstant constant) { }
public void Visit(ICompileTimeConstant constant) { Contract.Requires(constant != null); throw new NotImplementedException(); }
public override void TraverseChildren(ICompileTimeConstant constant) { Contract.Assume(!(constant.Type is Dummy)); //The type should already be filled in }
//^ ensures this.path.Count == old(this.path.Count); /// <summary> /// Traverses the given compile time constant. /// </summary> /// <param name="constant"></param> public virtual void Visit(ICompileTimeConstant constant) { }
public override void TraverseChildren(ICompileTimeConstant constant) { Contract.Assume(!(constant.Type is Dummy)); //The type should already be filled in }
/// <summary> /// Initializes a new instance of the <see cref="SwitchCase"/> class. /// </summary> /// <param name="switchCase">The switch case.</param> public SwitchCase(ISwitchCase switchCase) { this.body = new List<IStatement>(switchCase.Body); if (!switchCase.IsDefault) this.expression = switchCase.Expression; else this.expression = CodeDummy.Constant; this.locations = new List<ILocation>(switchCase.Locations); }
/// <summary> /// Returns true if the constant is an integral value that falls in the range of the target type. /// The target type does have to be an integral type. If it is not, this method always returns false. /// </summary> public static bool IsIntegerInRangeOf(ICompileTimeConstant constExpression, ITypeReference targetType) { switch (targetType.TypeCode) { case PrimitiveTypeCode.UInt8: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.Byte: return true; case System.TypeCode.SByte: return byte.MinValue <= ic.ToSByte(null); case System.TypeCode.Int16: short s = ic.ToInt16(null); return byte.MinValue <= s && s <= byte.MaxValue; case System.TypeCode.Int32: int i = ic.ToInt32(null); return byte.MinValue <= i && i <= byte.MaxValue; case System.TypeCode.Int64: long lng = ic.ToInt64(null); return byte.MinValue <= lng && lng <= byte.MaxValue; case System.TypeCode.UInt16: return ic.ToUInt16(null) <= byte.MaxValue; case System.TypeCode.UInt32: return ic.ToUInt32(null) <= byte.MaxValue; case System.TypeCode.UInt64: return ic.ToUInt64(null) <= byte.MaxValue; case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return byte.MinValue <= d && d <= byte.MaxValue; } return false; } case PrimitiveTypeCode.UInt16: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: return true; case System.TypeCode.SByte: return ushort.MinValue <= ic.ToSByte(null); case System.TypeCode.Int16: return ushort.MinValue <= ic.ToInt16(null); case System.TypeCode.Int32: int i = ic.ToInt32(null); return ushort.MinValue <= i && i <= ushort.MaxValue; case System.TypeCode.Int64: long lng = ic.ToInt64(null); return ushort.MinValue <= lng && lng <= ushort.MaxValue; case System.TypeCode.UInt32: return ic.ToUInt32(null) <= ushort.MaxValue; case System.TypeCode.UInt64: return ic.ToUInt64(null) <= ushort.MaxValue; case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return ushort.MinValue <= d && d <= ushort.MaxValue; } return false; } case PrimitiveTypeCode.UInt32: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: return true; case System.TypeCode.SByte: return uint.MinValue <= ic.ToSByte(null); case System.TypeCode.Int16: return uint.MinValue <= ic.ToInt16(null); case System.TypeCode.Int32: return uint.MinValue <= ic.ToInt32(null); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return uint.MinValue <= lng && lng <= uint.MaxValue; case System.TypeCode.UInt64: return ic.ToUInt64(null) <= uint.MaxValue; case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return uint.MinValue <= d && d <= uint.MaxValue; } return false; } case PrimitiveTypeCode.UInt64: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: case System.TypeCode.UInt64: return true; case System.TypeCode.SByte: return 0 <= ic.ToSByte(null); case System.TypeCode.Int16: return 0 <= ic.ToInt16(null); case System.TypeCode.Int32: return 0 <= ic.ToInt32(null); case System.TypeCode.Int64: return 0 <= ic.ToInt64(null); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return 0 <= d && d <= ulong.MaxValue; } return false; } case PrimitiveTypeCode.Int8: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return true; case System.TypeCode.Int16: short s = ic.ToInt16(null); return sbyte.MinValue <= s && s <= sbyte.MaxValue; case System.TypeCode.Int32: int i = ic.ToInt32(null); return sbyte.MinValue <= i && i <= sbyte.MaxValue; case System.TypeCode.Int64: long lng = ic.ToInt64(null); return sbyte.MinValue <= lng && lng <= sbyte.MaxValue; case System.TypeCode.Byte: return ic.ToByte(null) <= sbyte.MaxValue; case System.TypeCode.UInt16: return ic.ToUInt16(null) <= sbyte.MaxValue; case System.TypeCode.UInt32: return ic.ToUInt32(null) <= sbyte.MaxValue; case System.TypeCode.UInt64: return ic.ToUInt64(null) <= (ulong)sbyte.MaxValue; case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return sbyte.MinValue <= d && d <= sbyte.MaxValue; } return false; } case PrimitiveTypeCode.Int16: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: return true; case System.TypeCode.Int32: int i = ic.ToInt32(null); return short.MinValue <= i && i <= short.MaxValue; case System.TypeCode.Int64: long lng = ic.ToInt64(null); return short.MinValue <= lng && lng <= short.MaxValue; case System.TypeCode.UInt16: return ic.ToUInt16(null) <= short.MaxValue; case System.TypeCode.UInt32: return ic.ToUInt32(null) <= short.MaxValue; case System.TypeCode.UInt64: return ic.ToUInt64(null) <= (ulong)short.MaxValue; case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return short.MinValue <= d && d <= short.MaxValue; } return false; } case PrimitiveTypeCode.Int32: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: case System.TypeCode.UInt16: case System.TypeCode.Int32: return true; case System.TypeCode.Int64: long lng = ic.ToInt64(null); return int.MinValue <= lng && lng <= int.MaxValue; case System.TypeCode.UInt32: return ic.ToUInt32(null) <= int.MaxValue; case System.TypeCode.UInt64: return ic.ToUInt64(null) <= int.MaxValue; case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return int.MinValue <= d && d <= int.MaxValue; } return false; } case PrimitiveTypeCode.Int64: { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: case System.TypeCode.UInt16: case System.TypeCode.Int32: case System.TypeCode.UInt32: case System.TypeCode.Int64: return true; case System.TypeCode.UInt64: return ic.ToUInt64(null) <= int.MaxValue; case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return long.MinValue <= d && d <= long.MaxValue; } return false; } } return false; }
/// <summary> /// Rewrites the given compile time constant. /// </summary> /// <param name="constant"></param> public virtual ICompileTimeConstant Rewrite(ICompileTimeConstant constant) { return constant; }
public virtual void onASTElement(ICompileTimeConstant constant) { }
/// <summary> /// Returns true if the constant is an integral value that falls in the range of the target type. /// The target type does have to be an integral type. If it is not, this method always returns false. /// </summary> public static bool IsIntegerInRangeOf(ICompileTimeConstant constExpression, ITypeReference targetType) { switch (targetType.TypeCode) { case PrimitiveTypeCode.UInt8: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: return(true); case System.TypeCode.SByte: return(byte.MinValue <= ic.ToSByte(null)); case System.TypeCode.Int16: short s = ic.ToInt16(null); return(byte.MinValue <= s && s <= byte.MaxValue); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(byte.MinValue <= i && i <= byte.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(byte.MinValue <= lng && lng <= byte.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) <= byte.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= byte.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= byte.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(byte.MinValue <= d && d <= byte.MaxValue); } return(false); } case PrimitiveTypeCode.UInt16: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: return(true); case System.TypeCode.SByte: return(ushort.MinValue <= ic.ToSByte(null)); case System.TypeCode.Int16: return(ushort.MinValue <= ic.ToInt16(null)); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(ushort.MinValue <= i && i <= ushort.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(ushort.MinValue <= lng && lng <= ushort.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= ushort.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= ushort.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(ushort.MinValue <= d && d <= ushort.MaxValue); } return(false); } case PrimitiveTypeCode.UInt32: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: return(true); case System.TypeCode.SByte: return(uint.MinValue <= ic.ToSByte(null)); case System.TypeCode.Int16: return(uint.MinValue <= ic.ToInt16(null)); case System.TypeCode.Int32: return(uint.MinValue <= ic.ToInt32(null)); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(uint.MinValue <= lng && lng <= uint.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= uint.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(uint.MinValue <= d && d <= uint.MaxValue); } return(false); } case PrimitiveTypeCode.UInt64: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: case System.TypeCode.UInt64: return(true); case System.TypeCode.SByte: return(0 <= ic.ToSByte(null)); case System.TypeCode.Int16: return(0 <= ic.ToInt16(null)); case System.TypeCode.Int32: return(0 <= ic.ToInt32(null)); case System.TypeCode.Int64: return(0 <= ic.ToInt64(null)); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(0 <= d && d <= ulong.MaxValue); } return(false); } case PrimitiveTypeCode.Int8: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(true); case System.TypeCode.Int16: short s = ic.ToInt16(null); return(sbyte.MinValue <= s && s <= sbyte.MaxValue); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(sbyte.MinValue <= i && i <= sbyte.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(sbyte.MinValue <= lng && lng <= sbyte.MaxValue); case System.TypeCode.Byte: return(ic.ToByte(null) <= sbyte.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) <= sbyte.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= sbyte.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= (ulong)sbyte.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(sbyte.MinValue <= d && d <= sbyte.MaxValue); } return(false); } case PrimitiveTypeCode.Int16: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: return(true); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(short.MinValue <= i && i <= short.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(short.MinValue <= lng && lng <= short.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) <= short.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= short.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= (ulong)short.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(short.MinValue <= d && d <= short.MaxValue); } return(false); } case PrimitiveTypeCode.Int32: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: case System.TypeCode.UInt16: case System.TypeCode.Int32: return(true); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(int.MinValue <= lng && lng <= int.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= int.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= int.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(int.MinValue <= d && d <= int.MaxValue); } return(false); } case PrimitiveTypeCode.Int64: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: case System.TypeCode.UInt16: case System.TypeCode.Int32: case System.TypeCode.UInt32: case System.TypeCode.Int64: return(true); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= int.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(long.MinValue <= d && d <= long.MaxValue); } return(false); } } return(false); }
private HLLocation ProcessCompileTimeConstantExpression(ICompileTimeConstant pExpression) { if (pExpression.Value == null) { return(HLNullLocation.Create(HLDomain.GetOrCreateType(pExpression.Type))); } if (pExpression.Type.ResolvedType.IsEnum) { return(HLEnumLiteralLocation.Create(HLDomain.GetOrCreateType(pExpression.Type), pExpression.Value.ToString())); } if (pExpression.Value is bool) { return(HLBooleanLiteralLocation.Create((bool)pExpression.Value)); } if (pExpression.Value is sbyte) { return(HLInt8LiteralLocation.Create((sbyte)pExpression.Value)); } if (pExpression.Value is byte) { return(HLUInt8LiteralLocation.Create((byte)pExpression.Value)); } if (pExpression.Value is char) { return(HLCharLiteralLocation.Create((char)pExpression.Value)); } if (pExpression.Value is short) { return(HLInt16LiteralLocation.Create((short)pExpression.Value)); } if (pExpression.Value is ushort) { return(HLUInt16LiteralLocation.Create((ushort)pExpression.Value)); } if (pExpression.Value is float) { return(HLFloat32LiteralLocation.Create((float)pExpression.Value)); } if (pExpression.Value is int) { return(HLInt32LiteralLocation.Create((int)pExpression.Value)); } if (pExpression.Value is uint) { return(HLUInt32LiteralLocation.Create((uint)pExpression.Value)); } if (pExpression.Value is double) { return(HLFloat64LiteralLocation.Create((double)pExpression.Value)); } if (pExpression.Value is long) { return(HLInt64LiteralLocation.Create((long)pExpression.Value)); } if (pExpression.Value is ulong) { return(HLUInt64LiteralLocation.Create((ulong)pExpression.Value)); } if (pExpression.Value is string) { return(HLStringLiteralLocation.Create((string)pExpression.Value)); } throw new NotSupportedException(); }
public static bool IsNullLiteral(IExpression expression) { ICompileTimeConstant /*?*/ constExpression = expression as ICompileTimeConstant; return(constExpression != null && constExpression.Value == null); }
public void Visit(ICompileTimeConstant constant) { this.traverser.Traverse(constant); }
public override void Visit(ICompileTimeConstant constant) { if (constant.Value == null) { // TODO: (mschaef) hack TranslatedExpressions.Push(Bpl.Expr.False); } else if (constant.Value is bool) { TranslatedExpressions.Push(((bool)constant.Value) ? Bpl.Expr.True : Bpl.Expr.False); } else if (constant.Value is string) { throw new NotImplementedException("Strings are not translated"); } else { // TODO: (mschaef) hack TranslatedExpressions.Push(Bpl.Expr.Literal((int)constant.Value)); } }
/// <summary> /// Performs some computation with the given compile time constant. /// </summary> /// <param name="constant"></param> public virtual void Visit(ICompileTimeConstant constant) { this.Visit((IExpression)constant); }
public override void Visit(ICompileTimeConstant compileTimeConstant) { _formattedValue = compileTimeConstant.Value == null ? "null" : compileTimeConstant.Value.ToString(); }
/// <summary> /// Returns a deep copy of the given compile time constant. /// </summary> /// <param name="constant"></param> public CompileTimeConstant Copy(ICompileTimeConstant constant) { Contract.Requires(constant != null); Contract.Ensures(Contract.Result<CompileTimeConstant>() != null); var mutableCopy = this.shallowCopier.Copy(constant); this.CopyChildren((Expression)mutableCopy); return mutableCopy; }
public static bool IsIntegralZero(ICompileTimeConstant constExpression) { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return ic.ToSByte(null) == 0; case System.TypeCode.Int16: return ic.ToInt16(null) == 0; case System.TypeCode.Int32: return ic.ToInt32(null) == 0; case System.TypeCode.Int64: return ic.ToInt64(null) == 0; case System.TypeCode.Byte: return ic.ToByte(null) == 0; case System.TypeCode.UInt16: return ic.ToUInt16(null) == 0; case System.TypeCode.UInt32: return ic.ToUInt32(null) == 0; case System.TypeCode.UInt64: return ic.ToUInt64(null) == 0; case System.TypeCode.Boolean: return !ic.ToBoolean(null); } return false; }
/// <summary> /// Generates IL for the specified constant. /// </summary> /// <param name="constant">The constant.</param> public override void TraverseChildren(ICompileTimeConstant constant) { var ic = constant.Value as IConvertible; if (ic != null) this.EmitConstant(ic); else { var tc = constant.Type.TypeCode; if (tc == PrimitiveTypeCode.IntPtr) { this.EmitConstant(((IntPtr)constant.Value).ToInt64()); this.generator.Emit(OperationCode.Conv_Ovf_I); } else if (tc == PrimitiveTypeCode.UIntPtr) { this.EmitConstant(((UIntPtr)constant.Value).ToUInt64()); this.generator.Emit(OperationCode.Conv_Ovf_U); } else { this.generator.Emit(OperationCode.Ldnull); this.StackSize++; } } }
public static bool IsIntegralMinusOne(ICompileTimeConstant constExpression) { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return ic.ToSByte(null) == -1; case System.TypeCode.Int16: return ic.ToInt16(null) == -1; case System.TypeCode.Int32: return ic.ToInt32(null) == -1; case System.TypeCode.Int64: return ic.ToInt64(null) == -1; case System.TypeCode.Byte: return ic.ToByte(null) == byte.MaxValue; case System.TypeCode.UInt16: return ic.ToUInt16(null) == ushort.MaxValue; case System.TypeCode.UInt32: return ic.ToUInt32(null) == uint.MaxValue; case System.TypeCode.UInt64: return ic.ToUInt64(null) == ulong.MaxValue; } return false; }
public virtual void onASTElement(ICompileTimeConstant constant) { }
public static bool IsNumericZero(ICompileTimeConstant constExpression) { IConvertible/*?*/ ic = constExpression.Value as IConvertible; if (ic == null) return false; switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return ic.ToSByte(null) == 0; case System.TypeCode.Int16: return ic.ToInt16(null) == 0; case System.TypeCode.Int32: return ic.ToInt32(null) == 0; case System.TypeCode.Int64: return ic.ToInt64(null) == 0; case System.TypeCode.Byte: return ic.ToByte(null) == 0; case System.TypeCode.UInt16: return ic.ToUInt16(null) == 0; case System.TypeCode.UInt32: return ic.ToUInt32(null) == 0; case System.TypeCode.UInt64: return ic.ToUInt64(null) == 0; case System.TypeCode.Single: return ic.ToSingle(null) == 0; case System.TypeCode.Double: return ic.ToDouble(null) == 0; case System.TypeCode.Decimal: return ic.ToDecimal(null) == 0; } return false; }
public override void TraverseChildren(ICompileTimeConstant constant) { MethodEnter(constant); base.TraverseChildren(constant); MethodExit(); }
public override void TraverseChildren(ICompileTimeConstant constant) { var val = constant.Value; if (val == null) this.PrintToken(CSharpToken.Null); else if (val is bool) this.sourceEmitterOutput.Write(((bool)val) ? "true" : "false"); else if (val is string) this.PrintString((string)val); else if (val is char) PrintCharacter((char)val); else if (val is int) PrintInt((int)val); else if (val is uint) PrintUint((uint)val); else if (val is long) PrintLong((long)val); else if (val is ulong) PrintUlong((ulong)val); else this.sourceEmitterOutput.Write(val.ToString()); }
/// <summary> /// Traverses the compile time constant. /// </summary> public void Traverse(ICompileTimeConstant constant) { Contract.Requires(constant != null); if (this.preorderVisitor != null) this.preorderVisitor.Visit(constant); if (this.StopTraversal) return; this.TraverseChildren(constant); if (this.StopTraversal) return; if (this.postorderVisitor != null) this.postorderVisitor.Visit(constant); }
public override void TraverseChildren(ICompileTimeConstant constant) { MethodEnter(constant); base.TraverseChildren(constant); MethodExit(); }
/// <summary> /// Traverses the children of the compile time constant. /// </summary> public virtual void TraverseChildren(ICompileTimeConstant constant) { Contract.Requires(constant != null); this.TraverseChildren((IExpression)constant); }