File: CompiledDataContext.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (327 lines) | stat: -rw-r--r-- 12,020 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities.XamlIntegration
{
    using System;
    using System.Activities;
    using System.Activities.Expressions;
    using System.Activities.Runtime;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq.Expressions;
    using System.Runtime;

    public abstract class CompiledDataContext
    {
        IList<Location> locations;
        IList<LocationReference> locationReferences;
        ExpressionTreeRewriter visitor;        

        [SuppressMessage(FxCop.Category.Usage, FxCop.Rule.DoNotCallOverridableMethodsInConstructors, Justification = "Derived classes are always generated code")]
        protected CompiledDataContext(IList<LocationReference> locationReferences, ActivityContext activityContext)
        {
            this.locationReferences = locationReferences;

            if (this.locationReferences == null)
            {
                this.locationReferences = new List<LocationReference>();
            }

            this.locations = ConvertReferences(this.locationReferences, activityContext);
        }

        protected CompiledDataContext(IList<Location> locations)
        {
            this.locations = locations;
        }

        protected CompiledDataContext(IList<LocationReference> locationReferences)
        {
            this.visitor = new ExpressionTreeRewriter(locationReferences);
        }

        protected object GetVariableValue(int index)
        {
            return this.locations[index].Value;
        }

        protected void SetVariableValue(int index, object value)
        {
            this.locations[index].Value = value;
        }

        protected virtual void GetValueTypeValues()
        {
        }
        
        protected virtual void SetValueTypeValues()
        {
        }

        protected Expression RewriteExpressionTree(Expression originalExpression)
        {
            LambdaExpression lambdaExpression = originalExpression as LambdaExpression;

            if (lambdaExpression == null)
            {
                throw FxTrace.Exception.Argument("originalExpression", SR.LambdaExpressionTypeRequired);
            }

            if (lambdaExpression.ReturnType == null || lambdaExpression.ReturnType == typeof(void))
            {
                throw FxTrace.Exception.Argument("originalExpression", SR.LambdaExpressionReturnTypeInvalid);
            }
            
            return this.visitor.Visit(Expression.Lambda(
                typeof(Func<,>).MakeGenericType(typeof(ActivityContext), lambdaExpression.ReturnType),
                lambdaExpression.Body, 
                new ParameterExpression[] { ExpressionUtilities.RuntimeContextParameter }));
        }
                
        public Location<T> GetLocation<T>(Func<T> getMethod, Action<T> setMethod, int expressionId, Activity compiledRootActivity, ActivityContext activityContext)
        {
            return new CompiledLocation<T>(getMethod, setMethod, this.locationReferences, this.locations, expressionId, compiledRootActivity, activityContext);
        }

        public Location<T> GetLocation<T>(Func<T> getMethod, Action<T> setMethod)
        {
            return new CompiledLocation<T>(getMethod, setMethod);
        }

        protected static object GetDataContextActivities(Activity compiledRoot, bool forImplementation)
        {
            CompiledDataContextActivityVistor vistor = new CompiledDataContextActivityVistor();
            vistor.Visit(compiledRoot, forImplementation);
            CompiledDataContextActivitiesCache cache = new CompiledDataContextActivitiesCache(vistor.DataContextActivities);
            return cache;
        }

        protected static CompiledDataContext[] GetCompiledDataContextCache(object dataContextActivities, ActivityContext activityContext, Activity compiledRoot, bool forImplementation, int compiledDataContextCount)
        {
            ActivityInstance cacheInstance = GetDataContextInstance((CompiledDataContextActivitiesCache)dataContextActivities, activityContext, compiledRoot);

            HybridDictionary<Activity, CompiledDataContext[]> cache = null;
            if (forImplementation)
            {
                cache = (HybridDictionary<Activity, CompiledDataContext[]>)cacheInstance.CompiledDataContextsForImplementation;
            }
            else
            {
                cache = (HybridDictionary<Activity, CompiledDataContext[]>)cacheInstance.CompiledDataContexts;
            }

            if (cache == null)
            {
                cache = new HybridDictionary<Activity, CompiledDataContext[]>();

                if (forImplementation)
                {
                    cacheInstance.CompiledDataContextsForImplementation = cache;
                }
                else
                {
                    cacheInstance.CompiledDataContexts = cache;
                }
            }

            CompiledDataContext[] result = null;
            if (!cache.TryGetValue(compiledRoot, out result))
            {
                result = new CompiledDataContext[compiledDataContextCount];
                cache.Add(compiledRoot, result);
            }

            return result;
        }

        static ActivityInstance GetDataContextInstance(CompiledDataContextActivitiesCache dataContextActivities, ActivityContext activityContext, Activity compiledRoot)
        {
            ActivityInstance dataContextInstance = null;

            ActivityInstance currentInstance = activityContext.CurrentInstance;

            while (currentInstance != null)
            {
                if (dataContextActivities.Contains(currentInstance.Activity))
                {
                    dataContextInstance = currentInstance;
                    break;
                }
                //
                // Make sure we don't walk out of our IdSpace
                if (currentInstance.Activity == compiledRoot)
                {
                    break;
                }
                //
                // For SecondaryRoot scenarios the ActivityInstance tree may not
                // contain any of the data context activity instances because
                // the instance tree does not have to match the activity definition tree.
                // In this case just use the root instance.
                if (currentInstance.Parent == null)
                {
                    dataContextInstance = currentInstance;
                }

                currentInstance = currentInstance.Parent;
            }
            
            if (dataContextInstance == null)
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.CompiledExpressionsNoCompiledRoot(activityContext.Activity.Id)));
            }

            return dataContextInstance;
        }

        IList<Location> ConvertReferences(IList<LocationReference> locationReferences, ActivityContext activityContext)
        {
            IList<Location> temp = new List<Location>(locationReferences.Count);

            foreach (LocationReference reference in locationReferences)
            {
                temp.Add(reference.GetLocation(activityContext));
            }

            return temp;
        }

        class CompiledDataContextActivitiesCache
        {
            bool optimized;
            HashSet<Activity> activities;

            Activity activity0;
            Activity activity1;
            Activity activity2;
            Activity activity3;
            Activity activity4;

            public CompiledDataContextActivitiesCache(HashSet<Activity> dataContextActivities)
            {
                this.activities = dataContextActivities;

                if (this.activities != null && this.activities.Count <= 5)
                {
                    Activity[] activitiesArray = new Activity[5];
                    this.activities.CopyTo(activitiesArray);

                    activity0 = activitiesArray[0];
                    activity1 = activitiesArray[1];
                    activity2 = activitiesArray[2];
                    activity3 = activitiesArray[3];
                    activity4 = activitiesArray[4];

                    this.optimized = true;
                }
            }

            public bool Contains(Activity target)
            {
                if (this.optimized)
                {
                    if (this.activity0 == target)
                    {
                        return true;
                    }
                    else if (this.activity1 == target)
                    {
                        return true;
                    }
                    else if (this.activity2 == target)
                    {
                        return true;
                    }
                    else if (this.activity3 == target)
                    {
                        return true;
                    }
                    else if (this.activity4 == target)
                    {
                        return true;
                    }

                    return false;
                }
                else
                {
                    return this.activities.Contains(target);
                }
            }
        }

        class CompiledDataContextActivityVistor : CompiledExpressionActivityVisitor
        {
            HashSet<Activity> dataContextActivities;
            bool inVariableScopeArgument;

            public CompiledDataContextActivityVistor()
            {
                this.dataContextActivities = new HashSet<Activity>(new ReferenceComparer<Activity>());
            }

            public HashSet<Activity> DataContextActivities
            {
                get
                {
                    return this.dataContextActivities;
                }
            }

            protected override void VisitRoot(Activity activity, out bool exit)
            {
                this.dataContextActivities.Add(activity);
                base.VisitRoot(activity, out exit);
            }
            
            protected override void VisitVariableScope(Activity activity, out bool exit)
            {
                if (!this.dataContextActivities.Contains(activity))
                {
                    this.dataContextActivities.Add(activity);
                }
                base.VisitVariableScope(activity, out exit);
            }

            protected override void VisitDelegate(ActivityDelegate activityDelegate, out bool exit)
            {
                if (activityDelegate.Handler != null)
                {
                    this.dataContextActivities.Add(activityDelegate.Handler);
                }
                base.VisitDelegate(activityDelegate, out exit);
            }

            protected override void VisitVariableScopeArgument(RuntimeArgument runtimeArgument, out bool exit)
            {
                this.inVariableScopeArgument = true;
                base.VisitVariableScopeArgument(runtimeArgument, out exit);
                this.inVariableScopeArgument = false;
            }

            protected override void VisitITextExpression(Activity activity, out bool exit)
            {
                if (this.inVariableScopeArgument)
                {
                    this.dataContextActivities.Add(activity);
                }
                base.VisitITextExpression(activity, out exit);
            }
        }

        class ReferenceComparer<T> : IEqualityComparer<T>
        {
            bool IEqualityComparer<T>.Equals(T x, T y)
            {
                return object.ReferenceEquals(x, y);
            }

            int IEqualityComparer<T>.GetHashCode(T target)
            {
                return target.GetHashCode();
            }
        }
    }    
}