5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.CSharp;
30
using Mono.TextEditor;
33
namespace MonoDevelop.Refactoring
35
public static class HelperMethods
37
/*static Dictionary<string, string> TypeTable = new Dictionary<string, string> ();
38
static HelperMethods ()
40
TypeTable[DomReturnType.Void.FullName] = "void";
41
TypeTable[DomReturnType.String.FullName] = "string";
42
TypeTable[DomReturnType.Int32.FullName] = "int";
43
TypeTable[DomReturnType.UInt32.FullName] = "uint";
44
TypeTable[DomReturnType.Int64.FullName] = "long";
45
TypeTable[DomReturnType.UInt64.FullName] = "ulong";
46
TypeTable[DomReturnType.Object.FullName] = "object";
47
TypeTable[DomReturnType.Float.FullName] = "float";
48
TypeTable[DomReturnType.Double.FullName] = "double";
49
TypeTable[DomReturnType.Byte.FullName] = "byte";
50
TypeTable[DomReturnType.SByte.FullName] = "sbyte";
51
TypeTable[DomReturnType.Int16.FullName] = "short";
52
TypeTable[DomReturnType.UInt16.FullName] = "ushort";
53
TypeTable[DomReturnType.Decimal.FullName] = "decimal";
54
TypeTable[DomReturnType.Char.FullName] = "char";
55
TypeTable[DomReturnType.Bool.FullName] = "bool";
58
public static bool IsIdentifierPart (this char ch)
60
return Char.IsLetterOrDigit (ch) || ch == '_';
63
public static DocumentLocation ToDocumentLocation (this TextLocation location, Document document)
65
return new DocumentLocation (location.Line, location.Column);
68
public static AstType ConvertToTypeReference (this MonoDevelop.Projects.Dom.IReturnType returnType)
71
if (TypeTable.TryGetValue (returnType.DecoratedFullName, out primitiveType))
72
return new PrimitiveType (primitiveType);
74
AstType result = null;
75
if (!string.IsNullOrEmpty (returnType.Namespace))
76
result = new SimpleType (returnType.Namespace);
77
foreach (var part in returnType.Parts) {
79
var st = new SimpleType (part.Name);
80
foreach (var type in part.GenericArguments.Select (ga => ConvertToTypeReference (ga)))
81
st.AddChild (type, SimpleType.Roles.TypeArgument);
84
var mt = new ICSharpCode.NRefactory.CSharp.MemberType () {
86
MemberName = part.Name
88
foreach (var type in part.GenericArguments.Select (ga => ConvertToTypeReference (ga)))
89
mt.AddChild (type, SimpleType.Roles.TypeArgument);
98
public static DomReturnType ConvertToReturnType (this AstType typeRef)
102
DomReturnType result;
103
if (typeRef is SimpleType) {
104
var st = (SimpleType)typeRef;
105
result = new DomReturnType (st.Identifier);
106
foreach (var arg in st.TypeArguments){
107
result.AddTypeParameter (ConvertToReturnType (arg));
109
} else if (typeRef is ICSharpCode.NRefactory.CSharp.MemberType) {
110
var mt = (ICSharpCode.NRefactory.CSharp.MemberType)typeRef;
111
result = ConvertToReturnType (mt.Target);
112
result.Parts.Add (new ReturnTypePart (mt.MemberName));
114
foreach (var arg in mt.TypeArguments){
115
result.AddTypeParameter (ConvertToReturnType (arg));
117
} else if (typeRef is ComposedType) {
118
var ct = (ComposedType)typeRef;
119
result = ConvertToReturnType (ct.BaseType);
120
result.PointerNestingLevel = ct.PointerRank;
121
result.IsNullable = ct.HasNullableSpecifier;
123
int arraySpecifiers = ct.ArraySpecifiers.Count;
124
if (arraySpecifiers> 0) {
125
result.ArrayDimensions = arraySpecifiers;
127
foreach (var spec in ct.ArraySpecifiers) {
128
result.SetDimension (i, spec.Dimensions);
132
} else if (typeRef is PrimitiveType) {
133
var pt = (PrimitiveType)typeRef;
134
result = new DomReturnType (pt.Keyword);
135
} else if (typeRef.IsNull) {
139
throw new InvalidOperationException ("unknown AstType:" + typeRef);