Debian Codegraph @

external/api-doc-tools/mdoc/Mono.Documentation/Util/NativeTypeManager.cs

raw
1 using System.Collections.Generic;
2
3 using Mono.Cecil;
4
5 namespace Mono.Documentation.Util
6 {
7 static class NativeTypeManager
8 {
9
10 static Dictionary<string, string> toNativeType = new Dictionary<string, string> (){
11
12 {"int", "nint"},
13 {"Int32", "nint"},
14 {"System.Int32", "System.nint"},
15 {"uint", "nuint"},
16 {"UInt32", "nuint"},
17 {"System.UInt32", "System.nuint"},
18 {"float", "nfloat"},
19 {"Single", "nfloat"},
20 {"System.Single", "System.nfloat"},
21 {"SizeF", "CoreGraphics.CGSize"},
22 {"System.Drawing.SizeF", "CoreGraphics.CGSize"},
23 {"PointF", "CoreGraphics.CGPoint"},
24 {"System.Drawing.PointF", "CoreGraphics.CGPoint"},
25 {"RectangleF", "CoreGraphics.CGRect" },
26 {"System.Drawing.RectangleF", "CoreGraphics.CGRect"}
27 };
28
29 static Dictionary<string, string> fromNativeType = new Dictionary<string, string> (){
30
31 {"nint", "int"},
32 {"System.nint", "System.Int32"},
33 {"nuint", "uint"},
34 {"System.nuint", "System.UInt32"},
35 {"nfloat", "float"},
36 {"System.nfloat", "System.Single"},
37 {"CoreGraphics.CGSize", "System.Drawing.SizeF"},
38 {"CoreGraphics.CGPoint", "System.Drawing.PointF"},
39 {"CoreGraphics.CGRect", "System.Drawing.RectangleF"},
40 {"MonoTouch.CoreGraphics.CGSize", "System.Drawing.SizeF"},
41 {"MonoTouch.CoreGraphics.CGPoint", "System.Drawing.PointF"},
42 {"MonoTouch.CoreGraphics.CGRect", "System.Drawing.RectangleF"}
43 };
44
45 public static string ConvertToNativeType (string typename)
46 {
47 string nvalue;
48
49 bool isOut = false;
50 bool isArray = false;
51 string valueToCompare = StripToComparableType (typename, ref isOut, ref isArray);
52
53 if (toNativeType.TryGetValue (valueToCompare, out nvalue))
54 {
55
56 if (isArray)
57 {
58 nvalue += "[]";
59 }
60 if (isOut)
61 {
62 nvalue += "&";
63 }
64 return nvalue;
65 }
66 return typename;
67 }
68 public static string ConvertFromNativeType (string typename)
69 {
70 string nvalue;
71
72 bool isOut = false;
73 bool isArray = false;
74 string valueToCompare = StripToComparableType (typename, ref isOut, ref isArray);
75
76 if (fromNativeType.TryGetValue (valueToCompare, out nvalue))
77 {
78 if (isArray)
79 {
80 nvalue += "[]";
81 }
82 if (isOut)
83 {
84 nvalue += "&";
85 }
86 return nvalue;
87 }
88 // it wasn't one of the native types ... just return it
89 return typename;
90 }
91
92 static string StripToComparableType (string typename, ref bool isOut, ref bool isArray)
93 {
94 string valueToCompare = typename;
95 if (typename.EndsWith ("[]"))
96 {
97 valueToCompare = typename.Substring (0, typename.Length - 2);
98 isArray = true;
99 }
100 if (typename.EndsWith ("&"))
101 {
102 valueToCompare = typename.Substring (0, typename.Length - 1);
103 isOut = true;
104 }
105 if (typename.Contains ("<"))
106 {
107 // TODO: Need to recursively process generic parameters
108 }
109 return valueToCompare;
110 }
111
112 public static string GetTranslatedName (TypeReference t)
113 {
114 string typename = t.FullName;
115
116 bool isInAssembly = MDocUpdater.IsInAssemblies (t.Module.Name);
117 if (isInAssembly && !typename.StartsWith ("System") && MDocUpdater.HasDroppedNamespace (t))
118 {
119 string nameWithDropped = string.Format ("{0}.{1}", MDocUpdater.droppedNamespace, typename);
120 return nameWithDropped;
121 }
122 return typename;
123 }
124 }
125 }