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

NRefactoryExtensions.cs « Mono.Debugging.Evaluation « Mono.Debugging - github.com/mono/debugger-libs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c26421736027a9d51c82238bbc60c8999682632d (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
//
// NRefactoryExtensions.cs
//
// Author: Jeffrey Stedfast <jeff@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;

using ICSharpCode.NRefactory.CSharp;

namespace Mono.Debugging.Evaluation
{
	public static class NRefactoryExtensions
	{
		#region AstType

		public static object Resolve (this AstType type, EvaluationContext ctx)
		{
			var args = new List<object> ();
			var name = type.Resolve (ctx, args);

			//if (name.StartsWith ("global::", StringComparison.Ordinal))
			//	name = name.Substring ("global::".Length);

			if (string.IsNullOrEmpty (name))
				return null;

			if (args.Count > 0)
				return ctx.Adapter.GetType (ctx, name, args.ToArray ());

			return ctx.Adapter.GetType (ctx, name);
		}

		static string Resolve (this AstType type, EvaluationContext ctx, List<object> args)
		{
			if (type is PrimitiveType)
				return Resolve ((PrimitiveType) type, ctx, args);
			else if (type is ComposedType)
				return Resolve ((ComposedType) type, ctx, args);
			else if (type is MemberType)
				return Resolve ((MemberType) type, ctx, args);
			else if (type is SimpleType)
				return Resolve ((SimpleType) type, ctx, args);

			return null;
		}

		#endregion AstType

		#region ComposedType

		static string Resolve (this ComposedType type, EvaluationContext ctx, List<object> args)
		{
			string name;

			if (type.HasNullableSpecifier) {
				args.Insert (0, type.BaseType.Resolve (ctx));
				name = "System.Nullable`1";
			} else {
				name = type.BaseType.Resolve (ctx, args);
			}

			if (type.PointerRank > 0)
				name += new string ('*', type.PointerRank);

			if (type.ArraySpecifiers.Count > 0) {
				foreach (var spec in type.ArraySpecifiers) {
					if (spec.Dimensions > 1)
						name += "[" + new string (',', spec.Dimensions - 1) + "]";
					else
						name += "[]";
				}
			}

			return name;
		}
		
		#endregion ComposedType

		#region MemberType

		static string Resolve (this MemberType type, EvaluationContext ctx, List<object> args)
		{
			string name;

			if (!type.IsDoubleColon) {
				var parent = type.Target.Resolve (ctx, args);
				name = parent + "." + type.MemberName;
			} else {
				name = type.MemberName;
			}

			if (type.TypeArguments.Count > 0) {
				name += "`" + type.TypeArguments.Count;
				foreach (var arg in type.TypeArguments) {
					object resolved;

					if ((resolved = arg.Resolve (ctx)) == null)
						return null;

					args.Add (resolved);
				}
			}

			return name;
		}

		#endregion MemberType

		#region PrimitiveType

		public static string Resolve (this PrimitiveType type)
		{
			switch (type.Keyword) {
			case "bool":    return "System.Boolean";
			case "sbyte":   return "System.SByte";
			case "byte":    return "System.Byte";
			case "char":    return "System.Char";
			case "short":   return "System.Int16";
			case "ushort":  return "System.UInt16";
			case "int":     return "System.Int32";
			case "uint":    return "System.UInt32";
			case "long":    return "System.Int64";
			case "ulong":   return "System.UInt64";
			case "float":   return "System.Single";
			case "double":  return "System.Double";
			case "decimal": return "System.Decimal";
			case "string":  return "System.String";
			case "object":  return "System.Object";
			case "void":    return "System.Void";
			default: return null;
			}
		}

		static string Resolve (this PrimitiveType type, EvaluationContext ctx, List<object> args)
		{
			return Resolve (type);
		}

		#endregion PrimitiveType

		#region SimpleType

		static string Resolve (this SimpleType type, EvaluationContext ctx, List<object> args)
		{
			string name = type.Identifier;

			if (type.TypeArguments.Count > 0) {
				name += "`" + type.TypeArguments.Count;
				foreach (var arg in type.TypeArguments) {
					object resolved;

					if ((resolved = arg.Resolve (ctx)) == null)
						return null;

					args.Add (resolved);
				}
			}

			return name;
		}

		#endregion SimpleType
	}
}