/// <summary> /// Builds an array of signatures for math functions. /// </summary> /// <param name="methodName">The name of the method to call on the Math class.</param> /// <returns>The array of signatures for math functions.</returns> private static BuiltInFunctionSignature[] CreateMathFunctionSignatureArray(string methodName) { BuiltInFunctionSignature doubleSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, PrimitiveTypeUtils.DoubleResourceType, PrimitiveTypeUtils.DoubleResourceType); BuiltInFunctionSignature nullableDoubleSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, PrimitiveTypeUtils.DoubleResourceType, PrimitiveTypeUtils.NullableDoubleResourceType); BuiltInFunctionSignature decimalSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, PrimitiveTypeUtils.DecimalResourceType, PrimitiveTypeUtils.DecimalResourceType); BuiltInFunctionSignature nullableDecimalSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, PrimitiveTypeUtils.DecimalResourceType, PrimitiveTypeUtils.NullableDecimalResourceType); return(new BuiltInFunctionSignature[] { doubleSignature, decimalSignature, nullableDoubleSignature, nullableDecimalSignature }); }
/// <summary> /// Returns a list of signatures for a function name. /// </summary> /// <param name="name">The name of the function to look for.</param> /// <param name="signatures">The list of signatures available for the function name.</param> /// <returns>true if the function was found, or false otherwise.</returns> internal static bool TryGetBuiltInFunction(string name, out BuiltInFunctionSignature[] signatures) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(name != null, "name != null"); InitializeBuiltInFunctions(); return builtInFunctions.TryGetValue(name, out signatures); }
/// <summary> /// Builds an array of signatures for date time functions. /// </summary> /// <param name="propertyName">The name of the property on DateTime to access.</param> /// <returns>The array of signatures for a date time functions.</returns> private static BuiltInFunctionSignature[] CreateDateTimeFunctionSignatureArray(string propertyName) { BuiltInFunctionSignature signatureNonNullable = BuiltInFunctionSignature.CreateFromPropertyAccess( propertyName, PrimitiveTypeUtils.Int32ResourceType, PrimitiveTypeUtils.DateTimeResourceType); BuiltInFunctionSignature signatureNullable = BuiltInFunctionSignature.CreateFromPropertyAccess( propertyName, PrimitiveTypeUtils.Int32ResourceType, PrimitiveTypeUtils.NullableDateTimeResourceType); return(new BuiltInFunctionSignature[] { signatureNonNullable, signatureNullable }); }
/// <summary> /// Creates a signature for a function which is executed by accessing a property. /// </summary> /// <param name="propertyName">The name of the CLR property to access on the first argument.</param> /// <param name="returnType">The return resource type of the function.</param> /// <param name="argumentTypes">The resource types of the arguments of the function. (must be exactly one).</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromPropertyAccess(string propertyName, ResourceType returnType, params ResourceType[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(argumentTypes != null, "argumentTypes != null"); Debug.Assert(argumentTypes.Length == 1, "Property access built-in function is only allowed to have one argument."); PropertyInfo property = TypeUtils.GetNonNullableType(argumentTypes[0].InstanceType).GetProperty( propertyName, BindingFlags.Public | BindingFlags.Instance); Debug.Assert(property != null, "Could not find the specified property on the target type."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = property; signature.buildExpression = signature.BuildPropertyAccessExpression; return(signature); }
/// <summary> /// Creates a signature for a function which is executed by calling an instance method. /// </summary> /// <param name="methodName">The name of the CLR method to call.</param> /// <param name="returnType">The return resource type of the function.</param> /// <param name="argumentTypes">The resource types of the arguments of the function.</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromInstanceMethodCall(string methodName, ResourceType returnType, params ResourceType[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); MethodInfo method = argumentTypes[0].InstanceType.GetMethod( methodName, BindingFlags.Public | BindingFlags.Instance, null, argumentTypes.Skip(1).Select(argumentType => TypeUtils.GetNonNullableType(argumentType.InstanceType)).ToArray(), null); Debug.Assert(method != null, "Could not find the specified method on the type of the first argument."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = method; signature.buildExpression = signature.BuildInstanceMethodCallExpression; return(signature); }
/// <summary> /// Creates all string functions. /// </summary> private static void CreateStringFunctions() { BuiltInFunctionSignature signature; BuiltInFunctionSignature[] signatures; // bool endswith(string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "EndsWith", PrimitiveTypeUtils.BoolResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("endswith", new BuiltInFunctionSignature[] { signature }); // int indexof(string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "IndexOf", PrimitiveTypeUtils.Int32ResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("indexof", new BuiltInFunctionSignature[] { signature }); // string replace(string, string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Replace", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("replace", new BuiltInFunctionSignature[] { signature }); // bool startswith(string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "StartsWith", PrimitiveTypeUtils.BoolResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("startswith", new BuiltInFunctionSignature[] { signature }); // string tolower(string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "ToLower", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("tolower", new BuiltInFunctionSignature[] { signature }); // string toupper(string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "ToUpper", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("toupper", new BuiltInFunctionSignature[] { signature }); // string trim(string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Trim", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("trim", new BuiltInFunctionSignature[] { signature }); signatures = new BuiltInFunctionSignature[] { // string substring(string, int) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.Int32ResourceType), // string substring(string, int?) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.NullableInt32ResourceType), // string substring(string, int, int) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.Int32ResourceType, PrimitiveTypeUtils.Int32ResourceType), // string substring(string, int?, int) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.NullableInt32ResourceType, PrimitiveTypeUtils.Int32ResourceType), // string substring(string, int, int?) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.Int32ResourceType, PrimitiveTypeUtils.NullableInt32ResourceType), // string substring(string, int?, int?) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.NullableInt32ResourceType, PrimitiveTypeUtils.NullableInt32ResourceType) }; builtInFunctions.Add("substring", signatures); // bool substringof(string, string) signature = new BuiltInFunctionSignature( BuildSubstringOfExpression, PrimitiveTypeUtils.BoolResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("substringof", new BuiltInFunctionSignature[] { signature }); // string concat(string, string) signature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(string), "Concat", PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("concat", new BuiltInFunctionSignature[] { signature }); // int length(string) signature = BuiltInFunctionSignature.CreateFromPropertyAccess( "Length", PrimitiveTypeUtils.Int32ResourceType, PrimitiveTypeUtils.StringResourceType); builtInFunctions.Add("length", new BuiltInFunctionSignature[] { signature }); }
/// <summary> /// Creates a signature for a function which is executed by calling an instance method. /// </summary> /// <param name="methodName">The name of the CLR method to call.</param> /// <param name="returnType">The return resource type of the function.</param> /// <param name="argumentTypes">The resource types of the arguments of the function.</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromInstanceMethodCall(string methodName, ResourceType returnType, params ResourceType[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); MethodInfo method = argumentTypes[0].InstanceType.GetMethod( methodName, BindingFlags.Public | BindingFlags.Instance, null, argumentTypes.Skip(1).Select(argumentType => TypeUtils.GetNonNullableType(argumentType.InstanceType)).ToArray(), null); Debug.Assert(method != null, "Could not find the specified method on the type of the first argument."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = method; signature.buildExpression = signature.BuildInstanceMethodCallExpression; return signature; }
/// <summary> /// Creates a signature for a function which is executed by accessing a property. /// </summary> /// <param name="propertyName">The name of the CLR property to access on the first argument.</param> /// <param name="returnType">The return resource type of the function.</param> /// <param name="argumentTypes">The resource types of the arguments of the function. (must be exactly one).</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromPropertyAccess(string propertyName, ResourceType returnType, params ResourceType[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(argumentTypes != null, "argumentTypes != null"); Debug.Assert(argumentTypes.Length == 1, "Property access built-in function is only allowed to have one argument."); PropertyInfo property = TypeUtils.GetNonNullableType(argumentTypes[0].InstanceType).GetProperty( propertyName, BindingFlags.Public | BindingFlags.Instance); Debug.Assert(property != null, "Could not find the specified property on the target type."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = property; signature.buildExpression = signature.BuildPropertyAccessExpression; return signature; }