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: ebabc0210a855fe07bc9e7bfb2a6a316010e9b41 (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
//
// 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 Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

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

		public static object Resolve (this TypeSyntax 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 ExpressionSyntax type, EvaluationContext ctx, List<object> args)
		{
			if (type is PredefinedTypeSyntax)
				return Resolve ((PredefinedTypeSyntax) type, ctx, args);
			else if (type is PointerTypeSyntax)
				return Resolve ((PointerTypeSyntax)type, ctx, args);
			else if (type is NullableTypeSyntax)
				return Resolve ((NullableTypeSyntax)type, ctx, args);
			else if (type is RefTypeSyntax)
				return Resolve ((RefTypeSyntax)type, ctx, args);
			else if (type is QualifiedNameSyntax)
				return Resolve ((QualifiedNameSyntax) type, ctx, args);
			else if (type is IdentifierNameSyntax)
				return Resolve ((IdentifierNameSyntax)type, ctx, args);
			else if (type is GenericNameSyntax)
				return Resolve ((GenericNameSyntax)type, ctx, args);

			return null;
		}

		#endregion AstType

		#region ComposedType

		static string Resolve (this PointerTypeSyntax type, EvaluationContext ctx, List<object> args)
		{
			return type.ElementType.Resolve (ctx, args) + "*";
		}

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

		static string Resolve (this NullableTypeSyntax type, EvaluationContext ctx, List<object> args)
		{
			args.Insert (0, type.ElementType.Resolve (ctx));
			return "System.Nullable`1";
		}

		#endregion ComposedType

		#region MemberType

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

			if (!(type.Left is AliasQualifiedNameSyntax)) {
				var parent = type.Left.Resolve (ctx, args);
				name = parent + "." + type.Right.Identifier.ValueText;
			} else {
				name = type.Right.Identifier.ValueText;
			}

			
			if (type.Right is GenericNameSyntax genericName) {
				name += "`" + genericName.TypeArgumentList.Arguments.Count;
				foreach (var arg in genericName.TypeArgumentList.Arguments) {
					object resolved;

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

					args.Add (resolved);
				}
			}

			return name;
		}

		#endregion MemberType

		#region PrimitiveType

		public static string Resolve (this PredefinedTypeSyntax type)
		{
			switch (type.Keyword.Kind()) {
			case SyntaxKind.BoolKeyword:    return "System.Boolean";
			case SyntaxKind.SByteKeyword:   return "System.SByte";
			case SyntaxKind.ByteKeyword:    return "System.Byte";
			case SyntaxKind.CharKeyword:    return "System.Char";
			case SyntaxKind.ShortKeyword:   return "System.Int16";
			case SyntaxKind.UShortKeyword:  return "System.UInt16";
			case SyntaxKind.IntKeyword:     return "System.Int32";
			case SyntaxKind.UIntKeyword:    return "System.UInt32";
			case SyntaxKind.LongKeyword:    return "System.Int64";
			case SyntaxKind.ULongKeyword:   return "System.UInt64";
			case SyntaxKind.FloatKeyword:   return "System.Single";
			case SyntaxKind.DoubleKeyword:  return "System.Double";
			case SyntaxKind.DecimalKeyword: return "System.Decimal";
			case SyntaxKind.StringKeyword:  return "System.String";
			case SyntaxKind.ObjectKeyword:  return "System.Object";
			case SyntaxKind.VoidKeyword:    return "System.Void";
			default: return null;
			}
		}
        public static string Resolve (string shortTypeName)
 		{
			string longName = "";
			switch (shortTypeName) {
            case "bool":    longName = "System.Boolean"; break;
            case "byte":    longName = "System.Byte"; break;
            case "sbyte":   longName = "System.SByte"; break;
            case "char":    longName = "System.Char"; break;
            case "decimal": longName = "System.Decimal"; break;
            case "double":  longName = "System.Double"; break;
            case "float":   longName = "System.Single"; break;
            case "int":     longName = "System.Int32"; break;
            case "uint":    longName = "System.UInt32"; break;
            case "nint":    longName = "System.IntPtr"; break;
            case "nuint":   longName = "System.UIntPtr"; break;
            case "long":    longName = "System.Int64"; break;
            case "ulong":   longName = "System.UInt64"; break;
            case "short":   longName = "System.Int16"; break;
            case "ushort":  longName = "System.UInt16"; break;
            case "object":  longName = "System.Object"; break;
            case "string":  longName = "System.String"; break;
            case "dynamic": longName = "System.Object"; break;
            default: throw new ArgumentException($"Unknown type {shortTypeName}");
			}
			return longName;
 		}
		static string Resolve (this PredefinedTypeSyntax type, EvaluationContext ctx, List<object> args)
		{
			return Resolve (type);
		}

		#endregion PrimitiveType

		#region SimpleType

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

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

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

					args.Add (resolved);
				}
			}

			return type.Identifier.ValueText;
		}

		static string Resolve (this IdentifierNameSyntax type, EvaluationContext ctx, List<object> args)
		{
			return type.Identifier.ValueText;
		}

		#endregion SimpleType
	}
}