[jit] Rewrite some logging to work on embedded systems.
[mono.git] / mcs / class / Microsoft.CSharp / Microsoft.CSharp.RuntimeBinder / CSharpBinder.cs
1 //
2 // CSharpBinder.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Dynamic;
31 using System.Linq.Expressions;
32 using Compiler = Mono.CSharp;
33 using System.Reflection;
34 using System.Collections.Generic;
35
36 namespace Microsoft.CSharp.RuntimeBinder
37 {
38         class CSharpBinder
39         {
40                 static ConstructorInfo binder_exception_ctor;
41                 static object resolver = new object ();
42
43                 DynamicMetaObjectBinder binder;
44                 Compiler.Expression expr;
45                 BindingRestrictions restrictions;
46                 DynamicMetaObject errorSuggestion;
47
48                 public CSharpBinder (DynamicMetaObjectBinder binder, Compiler.Expression expr, DynamicMetaObject errorSuggestion)
49                 {
50                         this.binder = binder;
51                         this.expr = expr;
52                         this.restrictions = BindingRestrictions.Empty;
53                         this.errorSuggestion = errorSuggestion;
54                 }
55
56                 public Compiler.ResolveContext.Options ResolveOptions { get; set; }
57
58                 public void AddRestrictions (DynamicMetaObject arg)
59                 {
60                         restrictions = restrictions.Merge (CreateRestrictionsOnTarget (arg));
61                 }
62
63                 public void AddRestrictions (DynamicMetaObject[] args)
64                 {
65                         restrictions = restrictions.Merge (CreateRestrictionsOnTarget (args));
66                 }
67
68                 public DynamicMetaObject Bind (DynamicContext ctx, Type callingType)
69                 {
70                         Expression res;
71                         try {
72                                 var rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx, callingType), ResolveOptions);
73
74                                 // Static typemanager and internal caches are not thread-safe
75                                 lock (resolver) {
76                                         expr = expr.Resolve (rc, Compiler.ResolveFlags.VariableOrValue);
77                                 }
78
79                                 if (expr == null)
80                                         throw new RuntimeBinderInternalCompilerException ("Expression resolved to null");
81
82                                 res = expr.MakeExpression (new Compiler.BuilderContext ());
83                         } catch (RuntimeBinderException e) {
84                                 if (errorSuggestion != null)
85                                         return errorSuggestion;
86
87                                 res = CreateBinderException (e.Message);
88                         } catch (Exception) {
89                                 if (errorSuggestion != null)
90                                         return errorSuggestion;
91
92                                 throw;
93                         }
94
95                         return new DynamicMetaObject (res, restrictions);
96                 }
97
98                 Expression CreateBinderException (string message)
99                 {
100                         if (binder_exception_ctor == null)
101                                 binder_exception_ctor = typeof (RuntimeBinderException).GetConstructor (new[] { typeof (string) });
102
103                         //
104                         // Uses target type to keep expressions composition working
105                         //
106                         return Expression.Throw (Expression.New (binder_exception_ctor, Expression.Constant (message)), binder.ReturnType);
107                 }
108
109                 static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject arg)
110                 {
111                         return arg.HasValue && arg.Value == null ?
112                                 BindingRestrictions.GetInstanceRestriction (arg.Expression, null) :
113                                 BindingRestrictions.GetTypeRestriction (arg.Expression, arg.LimitType);
114                 }
115
116                 public static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject[] args)
117                 {
118                         if (args.Length == 0)
119                                 return BindingRestrictions.Empty;
120
121                         var res = CreateRestrictionsOnTarget (args[0]);
122                         for (int i = 1; i < args.Length; ++i)
123                                 res = res.Merge (CreateRestrictionsOnTarget (args[i]));
124
125                         return res;
126                 }
127         }
128 }