Merge branch 'BigIntegerParse'
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Ast / LightLambdaExpression.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
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.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 #if FEATURE_CORE_DLR
17 using System.Linq.Expressions;
18 #else
19 using Microsoft.Scripting.Ast;
20 #endif
21
22 using System;
23 using System.Collections.Generic;
24 using System.Text;
25 using Microsoft.Scripting.Interpreter;
26 using Microsoft.Scripting.Generation;
27
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;
34
35         internal LightLambdaExpression(Type retType, Expression body, string name, IList<ParameterExpression> args) {
36             _body = body;
37             _name = name;
38             _args = args;
39             _retType = retType;
40         }
41
42         public Expression Body {
43             get {
44                 return _body;
45             }
46         }
47
48         public string Name {
49             get {
50                 return _name;
51             }
52         }
53
54         public IList<ParameterExpression> Parameters {
55             get {
56                 return _args;
57             }
58         }
59
60         internal virtual LambdaExpression ReduceToLambdaWorker() {
61             throw new InvalidOperationException();
62         }
63
64         public Delegate Compile() {
65             return Compile(-1);
66         }
67
68         public Delegate Compile(int compilationThreshold) {
69             return new LightCompiler(compilationThreshold).CompileTop(this).CreateDelegate();
70         }
71
72         public override ExpressionType NodeType {
73             get { return ExpressionType.Extension; }
74         }
75
76         public override bool CanReduce {
77             get { return true; }
78         }
79
80         public override Expression Reduce() {
81             return ReduceToLambdaWorker();
82         }
83
84         public Type ReturnType {
85             get {
86                 return _retType;
87             }
88         }
89     }
90
91     internal class TypedLightLambdaExpression : LightLambdaExpression {
92         private readonly Type _delegateType;
93
94         internal TypedLightLambdaExpression(Type retType, Type delegateType, Expression body, string name, IList<ParameterExpression> args)
95             : base(retType, body, name, args) {
96             _delegateType = delegateType;
97         }
98
99         internal override LambdaExpression ReduceToLambdaWorker() {
100             return Expression.Lambda(
101                 _delegateType,
102                 Body,
103                 Name,
104                 Parameters
105             );
106         }
107
108         public override Type Type {
109             get { return _delegateType; }
110         }
111     }
112
113     public class LightExpression<T> : LightLambdaExpression {
114         internal LightExpression(Type retType, Expression body, string name, IList<ParameterExpression> args)
115             : base(retType, body, name, args) {
116         }
117
118         public Expression<T> ReduceToLambda() {
119             return Expression.Lambda<T>(Body, Name, Parameters);
120         }
121
122         public override Type Type {
123             get { return typeof(T); }
124         }
125
126         public new T Compile() {
127             return Compile(-1);
128         }
129
130         public new T Compile(int compilationThreshold) {
131             return (T)(object)new LightCompiler(compilationThreshold).CompileTop(this).CreateDelegate();
132         }
133
134         internal override LambdaExpression ReduceToLambdaWorker() {
135             return ReduceToLambda();
136         }
137     }
138
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);
142         }
143
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);
146         }
147     }
148
149 }