public static CodegenExpression Codegen( ExprCastNodeForge forge, CodegenClassScope codegenClassScope) { if (forge.EvaluationType == null) { return ConstantNull(); } var evaluationType = forge.EvaluationType.GetBoxedType(); var initMethod = codegenClassScope.NamespaceScope.InitMethod.MakeChildWithScope( evaluationType, typeof(ExprCastNodeForgeConstEval), CodegenSymbolProviderEmpty.INSTANCE, codegenClassScope); var exprSymbol = new ExprForgeCodegenSymbol(true, null); var compute = initMethod.MakeChildWithScope( evaluationType, typeof(ExprCastNodeForgeConstEval), exprSymbol, codegenClassScope) .AddParam(ExprForgeCodegenNames.PARAMS); compute.Block.MethodReturn( ExprCastNodeForgeNonConstEval.Codegen(forge, compute, exprSymbol, codegenClassScope)); initMethod.Block.MethodReturn(LocalMethod(compute, Constant(null), ConstantTrue(), ConstantNull())); return codegenClassScope.AddDefaultFieldUnshared(true, evaluationType, LocalMethod(initMethod)); }
public ExprCastNodeForgeConstEval( ExprCastNodeForge forge, object theConstant) { this.forge = forge; this.theConstant = theConstant; }
public ExprCastNodeForgeNonConstEval( ExprCastNodeForge forge, ExprEvaluator evaluator, ExprCastNode.CasterParserComputer casterParserComputer) { _forge = forge; _evaluator = evaluator; _casterParserComputer = casterParserComputer; }
public static CodegenExpression Codegen( ExprCastNodeForge forge, CodegenMethodScope codegenMethodScope, ExprForgeCodegenSymbol exprSymbol, CodegenClassScope codegenClassScope) { if (forge.EvaluationType == null) { return ConstantNull(); } var child = forge.ForgeRenderableCast.ChildNodes[0]; var childType = child.Forge.EvaluationType; if (childType == null) { return ConstantNull(); } var forgeEvaluationType = forge.EvaluationType; // TBD: Should all generated classes be boxed to nullable to allow // for nullable values in case we do primitive conversion? if (childType.CanBeNull()) { forgeEvaluationType = forgeEvaluationType.GetBoxedType(); } var methodNode = codegenMethodScope.MakeChild( forgeEvaluationType, typeof(ExprCastNodeForgeNonConstEval), codegenClassScope); var block = methodNode.Block .DeclareVar( childType, "result", child.Forge.EvaluateCodegen(childType, methodNode, exprSymbol, codegenClassScope)); if (childType.CanBeNull()) { block.IfRefNullReturnNull("result"); } var cast = forge.CasterParserComputerForge.CodegenPremade( forgeEvaluationType, Ref("result"), childType, methodNode, exprSymbol, codegenClassScope); block.MethodReturn(cast); return LocalMethod(methodNode); }
public override ExprNode Validate(ExprValidationContext validationContext) { if (ChildNodes.Length == 0 || ChildNodes.Length > 2) { throw new ExprValidationException("Cast function node must have one or two child expressions"); } var fromType = ChildNodes[0].Forge.EvaluationType; var classIdentifier = ClassIdentifierWArray.ClassIdentifier; var classIdentifierInvariant = classIdentifier.Trim(); var arrayDimensions = ClassIdentifierWArray.ArrayDimensions; // Local function to match a class identifier bool MatchesClassIdentifier(string identifier) { return string.Equals( classIdentifierInvariant, identifier, StringComparison.InvariantCultureIgnoreCase); } // determine date format parameter var namedParams = ExprNodeUtilityValidate.GetNamedExpressionsHandleDups(ChildNodes); ExprNodeUtilityValidate.ValidateNamed(namedParams, new[] {"dateformat"}); var dateFormatParameter = namedParams.Get("dateformat"); if (dateFormatParameter != null) { ExprNodeUtilityValidate.ValidateNamedExpectType( dateFormatParameter, new[] { typeof(string), typeof(DateFormat), typeof(DateTimeFormat) }); } // identify target type // try the primitive names including "string" SimpleTypeCaster caster; var targetType = TypeHelper.GetPrimitiveTypeForName(classIdentifier.Trim()); if (!ClassIdentifierWArray.IsArrayOfPrimitive) { targetType = targetType.GetBoxedType(); } targetType = ApplyDimensions(targetType); bool numeric; CasterParserComputerForge casterParserComputerForge = null; if (dateFormatParameter != null) { if (fromType != typeof(string)) { throw new ExprValidationException( "Use of the '" + dateFormatParameter.ParameterName + "' named parameter requires a string-type input"); } if (targetType == null) { try { targetType = TypeHelper.GetClassForName( classIdentifier.Trim(), validationContext.ImportService.ClassForNameProvider); targetType = ApplyDimensions(targetType); } catch (TypeLoadException) { // expected } } // dynamic or static date format numeric = false; caster = null; if (targetType == typeof(DateTimeEx) || MatchesClassIdentifier("calendar") || MatchesClassIdentifier("dateTimeEx")) { targetType = typeof(DateTimeEx); var desc = ValidateDateFormat(dateFormatParameter, validationContext); if (desc.IsIso8601Format) { casterParserComputerForge = new StringToDateTimeExIsoFormatComputer(); } else if (desc.StaticDateFormat != null) { casterParserComputerForge = new StringToDateTimExWStaticFormatComputer( desc.StaticDateFormat, TimeZoneInfo.Utc); // Note how code-generation does not use the default time zone } else { casterParserComputerForge = new StringToDateTimeExWExprFormatComputer( desc.DynamicDateFormat, TimeZoneInfo.Utc); } } else if (targetType == typeof(DateTimeOffset) || targetType == typeof(DateTimeOffset?) || MatchesClassIdentifier("dto") || MatchesClassIdentifier("datetimeoffset")) { targetType = typeof(DateTimeOffset); var desc = ValidateDateFormat(dateFormatParameter, validationContext); if (desc.IsIso8601Format) { casterParserComputerForge = new StringToDateTimeOffsetIsoFormatComputer(); } else if (desc.StaticDateFormat != null) { casterParserComputerForge = new StringToDateTimeOffsetWStaticFormatComputer(desc.StaticDateFormat); } else { casterParserComputerForge = new StringToDateTimeOffsetWExprFormatComputerForge(desc.DynamicDateFormat); } } else if (targetType == typeof(DateTime) || targetType == typeof(DateTime?) || MatchesClassIdentifier("datetime")) { targetType = typeof(DateTime); var desc = ValidateDateFormat(dateFormatParameter, validationContext); if (desc.IsIso8601Format) { casterParserComputerForge = new StringToDateTimeIsoFormatComputer(); } else if (desc.StaticDateFormat != null) { casterParserComputerForge = new StringToDateTimeWStaticFormatComputer(desc.StaticDateFormat); } else { casterParserComputerForge = new StringToDateTimeWExprFormatComputerForge(desc.DynamicDateFormat); } } else if (targetType == typeof(long) || targetType == typeof(long?)) { targetType = typeof(long); var desc = ValidateDateFormat(dateFormatParameter, validationContext); if (desc.IsIso8601Format) { casterParserComputerForge = new StringToLongWStaticISOFormatComputer(); } else if (desc.StaticDateFormat != null) { casterParserComputerForge = new StringToLongWStaticFormatComputer(desc.StaticDateFormat); } else { casterParserComputerForge = new StringToLongWExprFormatComputerForge(desc.DynamicDateFormat); } } else { throw new ExprValidationException( "Use of the '" + dateFormatParameter.ParameterName + "' named parameter requires a target type of long, DateTime, DateTimeOffset or DateEx"); } } else if (targetType != null) { targetType = targetType.GetBoxedType(); caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType); numeric = caster.IsNumericCast; } else if (MatchesClassIdentifier("bigint") || MatchesClassIdentifier("biginteger")) { targetType = typeof(BigInteger); targetType = ApplyDimensions(targetType); caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType); numeric = true; } else if (MatchesClassIdentifier("decimal")) { targetType = typeof(decimal); targetType = ApplyDimensions(targetType); caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType); numeric = true; } else { try { targetType = TypeHelper.GetClassForName( classIdentifier.Trim(), validationContext.ImportService.ClassForNameProvider); } catch (TypeLoadException e) { throw new ExprValidationException( "Class as listed in cast function by name '" + classIdentifier + "' cannot be loaded", e); } targetType = ApplyDimensions(targetType); numeric = targetType.IsNumeric(); if (numeric) { caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType); } else { caster = new SimpleTypeCasterAnyType(targetType); } } // assign a computer unless already assigned if (casterParserComputerForge == null) { // to-string if (targetType == typeof(string)) { casterParserComputerForge = new StringXFormComputer(); } else if (fromType == typeof(string) && targetType != typeof(char)) { // parse SimpleTypeParserSPI parser = SimpleTypeParserFactory.GetParser(targetType.GetBoxedType()); casterParserComputerForge = new StringParserComputer(parser); } else if (numeric) { // numeric cast with check casterParserComputerForge = new NumberCasterComputer(caster); } else { // non-numeric cast casterParserComputerForge = new NonnumericCasterComputer(caster); } } // determine constant or not object theConstant = null; var isConstant = false; if (ChildNodes[0].Forge.ForgeConstantType.IsCompileTimeConstant) { isConstant = casterParserComputerForge.IsConstantForConstInput; if (isConstant) { var @in = ChildNodes[0].Forge.ExprEvaluator.Evaluate(null, true, null); theConstant = @in == null ? null : casterParserComputerForge.EvaluatorComputer.Compute(@in, null, true, null); } } forge = new ExprCastNodeForge(this, casterParserComputerForge, targetType, isConstant, theConstant); return null; }