1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using System.Linq.Expressions;
19 using Microsoft.Scripting.Ast;
23 using System.Collections.Generic;
25 using Microsoft.Scripting.Interpreter;
26 using Microsoft.Scripting.Generation;
28 namespace Microsoft.Scripting.Ast {
29 public class LightLambdaExpression : Expression {
30 private readonly Expression _body;
31 private readonly Type _retType;
32 private readonly string _name;
33 private readonly IList<ParameterExpression> _args;
35 internal LightLambdaExpression(Type retType, Expression body, string name, IList<ParameterExpression> args) {
42 public Expression Body {
54 public IList<ParameterExpression> Parameters {
60 internal virtual LambdaExpression ReduceToLambdaWorker() {
61 throw new InvalidOperationException();
64 public Delegate Compile() {
68 public Delegate Compile(int compilationThreshold) {
69 return new LightCompiler(compilationThreshold).CompileTop(this).CreateDelegate();
72 public override ExpressionType NodeType {
73 get { return ExpressionType.Extension; }
76 public override bool CanReduce {
80 public override Expression Reduce() {
81 return ReduceToLambdaWorker();
84 public Type ReturnType {
91 internal class TypedLightLambdaExpression : LightLambdaExpression {
92 private readonly Type _delegateType;
94 internal TypedLightLambdaExpression(Type retType, Type delegateType, Expression body, string name, IList<ParameterExpression> args)
95 : base(retType, body, name, args) {
96 _delegateType = delegateType;
99 internal override LambdaExpression ReduceToLambdaWorker() {
100 return Expression.Lambda(
108 public override Type Type {
109 get { return _delegateType; }
113 public class LightExpression<T> : LightLambdaExpression {
114 internal LightExpression(Type retType, Expression body, string name, IList<ParameterExpression> args)
115 : base(retType, body, name, args) {
118 public Expression<T> ReduceToLambda() {
119 return Expression.Lambda<T>(Body, Name, Parameters);
122 public override Type Type {
123 get { return typeof(T); }
126 public new T Compile() {
130 public new T Compile(int compilationThreshold) {
131 return (T)(object)new LightCompiler(compilationThreshold).CompileTop(this).CreateDelegate();
134 internal override LambdaExpression ReduceToLambdaWorker() {
135 return ReduceToLambda();
139 public static partial class Utils {
140 public static LightExpression<T> LightLambda<T>(Type retType, Expression body, string name, IList<ParameterExpression> args) {
141 return new LightExpression<T>(retType, body, name, args);
144 public static LightLambdaExpression LightLambda(Type retType, Type delegateType, Expression body, string name, IList<ParameterExpression> args) {
145 return new TypedLightLambdaExpression(retType, delegateType, body, name, args);