~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/Mono.Debugging/Mono.Debugging.Evaluation/NRefactoryExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// NRefactoryExtensions.cs
 
3
//
 
4
// Author: Jeffrey Stedfast <jeff@xamarin.com>
 
5
//
 
6
// Copyright (c) 2013 Xamarin Inc.
 
7
//
 
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
9
// of this software and associated documentation files (the "Software"), to deal
 
10
// in the Software without restriction, including without limitation the rights
 
11
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
12
// copies of the Software, and to permit persons to whom the Software is
 
13
// furnished to do so, subject to the following conditions:
 
14
//
 
15
// The above copyright notice and this permission notice shall be included in
 
16
// all copies or substantial portions of the Software.
 
17
//
 
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
19
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
20
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
21
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
22
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
23
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
24
// THE SOFTWARE.
 
25
 
 
26
using System;
 
27
using System.Collections.Generic;
 
28
 
 
29
using ICSharpCode.NRefactory.CSharp;
 
30
 
 
31
namespace Mono.Debugging.Evaluation
 
32
{
 
33
        public static class NRefactoryExtensions
 
34
        {
 
35
                #region AstType
 
36
 
 
37
                public static object Resolve (this AstType type, EvaluationContext ctx)
 
38
                {
 
39
                        var args = new List<object> ();
 
40
                        var name = type.Resolve (ctx, args);
 
41
 
 
42
                        //if (name.StartsWith ("global::", StringComparison.Ordinal))
 
43
                        //      name = name.Substring ("global::".Length);
 
44
 
 
45
                        if (args.Count > 0)
 
46
                                return ctx.Adapter.GetType (ctx, name, args.ToArray ());
 
47
 
 
48
                        return ctx.Adapter.GetType (ctx, name);
 
49
                }
 
50
 
 
51
                static string Resolve (this AstType type, EvaluationContext ctx, List<object> args)
 
52
                {
 
53
                        if (type is PrimitiveType)
 
54
                                return Resolve ((PrimitiveType) type, ctx, args);
 
55
                        else if (type is ComposedType)
 
56
                                return Resolve ((ComposedType) type, ctx, args);
 
57
                        else if (type is MemberType)
 
58
                                return Resolve ((MemberType) type, ctx, args);
 
59
                        else if (type is SimpleType)
 
60
                                return Resolve ((SimpleType) type, ctx, args);
 
61
 
 
62
                        return null;
 
63
                }
 
64
 
 
65
                #endregion AstType
 
66
 
 
67
                #region ComposedType
 
68
 
 
69
                static string Resolve (this ComposedType type, EvaluationContext ctx, List<object> args)
 
70
                {
 
71
                        string name;
 
72
 
 
73
                        if (type.HasNullableSpecifier) {
 
74
                                args.Insert (0, type.BaseType.Resolve (ctx));
 
75
                                name = "System.Nullable`1";
 
76
                        } else {
 
77
                                name = type.BaseType.Resolve (ctx, args);
 
78
                        }
 
79
 
 
80
                        if (type.PointerRank > 0)
 
81
                                name += new string ('*', type.PointerRank);
 
82
 
 
83
                        if (type.ArraySpecifiers.Count > 0) {
 
84
                                foreach (var spec in type.ArraySpecifiers) {
 
85
                                        if (spec.Dimensions > 1)
 
86
                                                name += "[" + new string (',', spec.Dimensions - 1) + "]";
 
87
                                        else
 
88
                                                name += "[]";
 
89
                                }
 
90
                        }
 
91
 
 
92
                        return name;
 
93
                }
 
94
                
 
95
                #endregion ComposedType
 
96
 
 
97
                #region MemberType
 
98
 
 
99
                static string Resolve (this MemberType type, EvaluationContext ctx, List<object> args)
 
100
                {
 
101
                        string name;
 
102
 
 
103
                        if (!type.IsDoubleColon) {
 
104
                                var parent = type.Target.Resolve (ctx, args);
 
105
                                name = parent + "." + type.MemberName;
 
106
                        } else {
 
107
                                name = type.MemberName;
 
108
                        }
 
109
 
 
110
                        if (type.TypeArguments.Count > 0) {
 
111
                                name += "`" + type.TypeArguments.Count;
 
112
                                foreach (var arg in type.TypeArguments) {
 
113
                                        object resolved;
 
114
 
 
115
                                        if ((resolved = arg.Resolve (ctx)) == null)
 
116
                                                return null;
 
117
 
 
118
                                        args.Add (resolved);
 
119
                                }
 
120
                        }
 
121
 
 
122
                        return name;
 
123
                }
 
124
 
 
125
                #endregion MemberType
 
126
 
 
127
                #region PrimitiveType
 
128
 
 
129
                public static string Resolve (this PrimitiveType type)
 
130
                {
 
131
                        switch (type.Keyword) {
 
132
                        case "bool":    return "System.Boolean";
 
133
                        case "sbyte":   return "System.SByte";
 
134
                        case "byte":    return "System.Byte";
 
135
                        case "char":    return "System.Char";
 
136
                        case "short":   return "System.Int16";
 
137
                        case "ushort":  return "System.UInt16";
 
138
                        case "int":     return "System.Int32";
 
139
                        case "uint":    return "System.UInt32";
 
140
                        case "long":    return "System.Int64";
 
141
                        case "ulong":   return "System.UInt64";
 
142
                        case "float":   return "System.Single";
 
143
                        case "double":  return "System.Double";
 
144
                        case "decimal": return "System.Decimal";
 
145
                        case "string":  return "System.String";
 
146
                        case "object":  return "System.Object";
 
147
                        case "void":    return "System.Void";
 
148
                        default: return null;
 
149
                        }
 
150
                }
 
151
 
 
152
                static string Resolve (this PrimitiveType type, EvaluationContext ctx, List<object> args)
 
153
                {
 
154
                        return Resolve (type);
 
155
                }
 
156
 
 
157
                #endregion PrimitiveType
 
158
 
 
159
                #region SimpleType
 
160
 
 
161
                static string Resolve (this SimpleType type, EvaluationContext ctx, List<object> args)
 
162
                {
 
163
                        string name = type.Identifier;
 
164
 
 
165
                        if (type.TypeArguments.Count > 0) {
 
166
                                name += "`" + type.TypeArguments.Count;
 
167
                                foreach (var arg in type.TypeArguments) {
 
168
                                        object resolved;
 
169
 
 
170
                                        if ((resolved = arg.Resolve (ctx)) == null)
 
171
                                                return null;
 
172
 
 
173
                                        args.Add (resolved);
 
174
                                }
 
175
                        }
 
176
 
 
177
                        return name;
 
178
                }
 
179
 
 
180
                #endregion SimpleType
 
181
        }
 
182
}