Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LambdaBodyOutputVisitor.cs « Mono.Debugging.Evaluation « Mono.Debugging - github.com/mono/debugger-libs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c27de389ede0c4dc32488584a9118efb3b531f9 (plain)
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Mono.Debugging.Client;

namespace Mono.Debugging.Evaluation
{
	// Outputs lambda expression inputted on Immediate Pad. Also
	// this class works for followings.
	// - Check if body of lambda has not-supported expression
	// - Output reserved words like `this` or `base` with generated
	//   identifer.
	// - Collect variable references for outside the lambda
	//   (local variables/properties...)
	public class LambdaBodyOutputVisitor : CSharpSyntaxWalker
	{
		readonly Dictionary<string, ValueReference> userVariables;
		readonly EvaluationContext ctx;

		Dictionary<string, Tuple<string, object>> localValues;
		List<string> definedIdentifier;
		int gensymCount;

		public LambdaBodyOutputVisitor (EvaluationContext ctx, Dictionary<string, ValueReference> userVariables, TextWriter writer)
		{
			this.ctx = ctx;
			this.userVariables = userVariables;
			this.localValues = new Dictionary<string, Tuple<string, object>> ();
			this.definedIdentifier = new List<string> ();
		}

		void WriteKeyword (string keyword)
		{

		}
		void WriteIdentifier (string keyword)
		{

		}

		public Tuple<string, object>[] GetLocalValues ()
		{
			var locals = new Tuple<string, object>[localValues.Count];
			int n = 0;
			foreach (var localv in localValues.Values) {
				locals[n] = localv;
				n++;
			}
			return locals;
		}

		static Exception NotSupportedToConsistency ()
		{
			return new NotSupportedExpressionException ();
		}

		static Exception NotSupported ()
		{
			return new NotSupportedExpressionException ();
		}

		static Exception EvaluationError (string message, params object[] args)
		{
			return new EvaluatorException (message, args);
		}

		bool IsPublicValueFlag (ObjectValueFlags flags)
		{
			var isField = (flags & ObjectValueFlags.Field) != 0;
			var isProperty = (flags & ObjectValueFlags.Property) != 0;
			var isPublic = (flags & ObjectValueFlags.Public) != 0;

			return !(isField || isProperty) || isPublic;
		}

		void AssertPublicType (object type)
		{
			if (!ctx.Adapter.IsPublic (ctx, type)) {
				var typeName = ctx.Adapter.GetDisplayTypeName (ctx, type);
				throw EvaluationError ("Not Support to reference non-public type: `{0}'", typeName);
			}
		}

		void AssertPublicValueReference (ValueReference vr)
		{
			if (!(vr is NamespaceValueReference)) {
				var typ = vr.Type;
				AssertPublicType (typ);
			}
			if (!IsPublicValueFlag (vr.Flags)) {
				throw EvaluationError ("Not Support to reference non-public thing: `{0}'", vr.Name);
			}
		}

		ValueReference Evaluate (IdentifierNameSyntax t)
		{
			var visitor = new NRefactoryExpressionEvaluatorVisitor (ctx, t.Identifier.ValueText, null, userVariables);
			return visitor.Visit (t);
		}

		ValueReference Evaluate (BaseExpressionSyntax t)
		{
			var visitor = new NRefactoryExpressionEvaluatorVisitor (ctx, "base", null, userVariables);
			return visitor.Visit (t);
		}

		ValueReference Evaluate (ThisExpressionSyntax t)
		{
			var visitor = new NRefactoryExpressionEvaluatorVisitor (ctx, "this", null, userVariables);
			return visitor.Visit (t);
		}

		string GenerateSymbol (string s)
		{
			var prefix = "__" + s;
			var sym = prefix;
			while (ExistsLocalName (sym)) {
				sym = prefix + gensymCount++;
			}

			return sym;
		}

		string AddToLocals (string name, ValueReference vr, bool shouldRename = false)
		{
			if (localValues.ContainsKey (name))
				return GetLocalName (name);

			string localName;
			if (shouldRename) {
				localName = GenerateSymbol (name);
			} else if (!ExistsLocalName (name)) {
				localName = name;
			} else {
				throw EvaluationError ("Cannot use a variable named {0} inside lambda", name);
			}

			AssertPublicValueReference (vr);

			var valu = vr != null ? vr.Value : null;
			var pair = Tuple.Create (localName, valu);
			localValues.Add (name, pair);
			return localName;
		}

		string GetLocalName (string name)
		{
			Tuple<string, object> pair;
			if (localValues.TryGetValue (name, out pair))
				return pair.Item1;
			return null;
		}

		bool ExistsLocalName (string localName)
		{
			foreach (var pair in localValues.Values) {
				if (pair.Item1 == localName)
					return true;
			}
			return definedIdentifier.Contains (localName);
		}

		#region IAstVisitor implementation

		public override void VisitAssignmentExpression (AssignmentExpressionSyntax node)
		{
			throw EvaluationError ("Not support assignment expression inside lambda");
		}

		public override void VisitBaseExpression (BaseExpressionSyntax node)
		{
			var basee = "base";
			var localbase = GetLocalName (basee);
			if (localbase == null) {
				var vr = Evaluate (node);
				localbase = AddToLocals (basee, vr, true);
			}
			WriteKeyword (localbase);
		}

		public override void VisitIdentifierName (IdentifierNameSyntax node)
		{
			var identifier = node.Identifier.ValueText;
			var localIdentifier = "";

			if (definedIdentifier.Contains (identifier)) {
				localIdentifier = identifier;
			} else {
				localIdentifier = GetLocalName (identifier);
				if (localIdentifier == null) {
					var vr = Evaluate (node);
					localIdentifier = AddToLocals (identifier, vr);
				}
			}
			WriteIdentifier (node.Identifier.ValueText);
		}

		public override void VisitGenericName (GenericNameSyntax node)
		{
			WriteIdentifier (node.Identifier.ValueText);
			foreach (var arg in node.TypeArgumentList.Arguments) {
				Visit (arg);
			}
		}

		public override void VisitInvocationExpression (InvocationExpressionSyntax node)
		{
			var invocationTarget = node.Expression;
			if (!(invocationTarget is IdentifierNameSyntax method)) {
				Visit(invocationTarget);
				return;
			}

			var argc = node.ArgumentList.Arguments.Count;
			var methodName = method.Identifier.ValueText;
			var vref = ctx.Adapter.GetThisReference (ctx);
			var vtype = ctx.Adapter.GetEnclosingType (ctx);
			string accessor = null;

			var hasInstanceMethod = ctx.Adapter.HasMethodWithParamLength (ctx, vtype, methodName, BindingFlags.Instance, argc);
			var hasStaticMethod = ctx.Adapter.HasMethodWithParamLength (ctx, vtype, methodName, BindingFlags.Static, argc);
			var publicFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
			var hasPublicMethod = ctx.Adapter.HasMethodWithParamLength (ctx, vtype, methodName, publicFlags, argc);

			if ((hasInstanceMethod || hasStaticMethod) && !hasPublicMethod)
				throw EvaluationError ("Only support public method invocation inside lambda");

			if (vref == null && hasStaticMethod) {
				AssertPublicType (vtype);
				var typeName = ctx.Adapter.GetTypeName (ctx, vtype);
				accessor = ctx.Adapter.GetDisplayTypeName (typeName);
			} else if (vref != null) {
				AssertPublicValueReference (vref);
				if (hasInstanceMethod) {
					if (hasStaticMethod) {
						// It's hard to determine which one is expected because
						// we don't have any information of parameter types now.
						throw EvaluationError ("Not supported invocation of static/instance overloaded method");
					}
					accessor = GetLocalName ("this");
					if (accessor == null)
						accessor = AddToLocals ("this", vref, true);
				} else if (hasStaticMethod) {
					var typeName = ctx.Adapter.GetTypeName (ctx, vtype);
					accessor = ctx.Adapter.GetDisplayTypeName (typeName);
				}
			}

			if (accessor == null)
				WriteIdentifier (methodName);
			else
				WriteKeyword (accessor + "." + methodName);
			WriteIdentifier ("(");
			for (int i = 0; i < node.ArgumentList.Arguments.Count; i++) {
				if (i > 0)
					WriteIdentifier (", ");
				Visit (node.ArgumentList.Arguments[i]);
			}
			WriteIdentifier (")");
		}

		public override void VisitSimpleLambdaExpression (SimpleLambdaExpressionSyntax node)
		{
			if (node.Parameter.Modifiers.Count > 0)
				throw NotSupported ();

			definedIdentifier.Add (node.Parameter.Identifier.ValueText);
			Visit (node.Body);
		}

		public override void VisitThisExpression (ThisExpressionSyntax node)
		{
			var thiss = "this";
			var localthis = GetLocalName (thiss);
			if (localthis == null) {
				var vr = Evaluate (node);
				localthis = AddToLocals (thiss, vr, true);
			}
			WriteKeyword (localthis);
		}

		public override void VisitParameter (ParameterSyntax node)
		{
			if (node.Parent is SimpleNameSyntax)
				base.VisitParameter (node);
			else
				throw NotSupportedToConsistency ();
		}

		public override void DefaultVisit (SyntaxNode node)
		{
			throw NotSupportedToConsistency ();
		}
		#endregion
	}
}