Debian Codegraph @

external/api-doc-tools/mdoc/Mono.Documentation/Updater/Formatters/CppFormatters/CppWinRtFullMemberFormatter.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Mono.Cecil;
6 using Mono.Documentation.Updater.Formatters.CppFormatters;
7
8 namespace Mono.Documentation.Updater.CppFormatters
9 {
10 public class CppWinRtFullMemberFormatter : CppCxFullMemberFormatter
11 {
12 protected override bool AppendHatOnReturn => false;
13 protected override string HatModifier => $" const{RefTypeModifier}";
14 public override string Language => Consts.CppWinRt;
15 protected override string RefTypeModifier => " &";
16
17 protected override IList<string> GetAllowedTypes()
18 {
19 return new List<string>(AllowedFundamentalTypes);
20 }
21
22 protected override StringBuilder AppendNamespace(StringBuilder buf, TypeReference type)
23 {
24 var @namespace=base.AppendNamespace(buf, type);
25 if (@namespace.ToString().StartsWith($"Windows{NestedTypeSeparator}"))
26 {
27 buf.Insert(0, $"winrt{NestedTypeSeparator}");
28 }
29
30 return @namespace;
31 }
32
33 protected override string GetCppType(string t)
34 {
35 // make sure there are no modifiers in the type string (add them back before returning)
36 string typeToCompare = t;
37 string[] splitType = null;
38 if (t.Contains(' '))
39 {
40 splitType = t.Split(' ');
41 typeToCompare = splitType[0];
42
43 foreach (var str in splitType)
44 {
45 if (str == "modopt(System.Runtime.CompilerServices.IsLong)" && typeToCompare == "System.Int32")
46 return "long";
47 if (str == "modopt(System.Runtime.CompilerServices.IsSignUnspecifiedByte)" &&
48 typeToCompare == "System.SByte")
49 return "char";
50 }
51 }
52
53 switch (typeToCompare)
54 {
55 case "System.Byte": typeToCompare = "byte"; break;
56 case "System.Int16": typeToCompare = "short"; break;
57 case "System.Int32": typeToCompare = "int"; break;
58 case "System.Int64": typeToCompare = "long"; break;
59 case "System.UInt16": typeToCompare = "unsigned short"; break;
60 case "System.UInt32": typeToCompare = "unsigned int"; break;
61 case "System.UInt64": typeToCompare = "unsigned long";break;
62
63 case "System.Single": typeToCompare = "float"; break;
64 case "System.Double": typeToCompare = "double"; break;
65
66 case "System.Boolean": typeToCompare = "bool"; break;
67 case "System.Char": typeToCompare = "char"; break;
68 case "System.Void": typeToCompare = "void"; break;
69 //API specific type is "winrt::hstring"; but c++ in built type is better variant
70 case "System.String": typeToCompare = "std::wstring"; break;
71 case "System.Object": typeToCompare = "winrt::Windows::Foundation::IInspectable"; break;
72 }
73
74 if (splitType != null)
75 {
76 // re-add modreq/modopt if it was there
77 splitType[0] = typeToCompare;
78 typeToCompare = string.Join(" ", splitType);
79 }
80 return typeToCompare == t ? null : typeToCompare;
81 }
82
83 protected override StringBuilder AppendParameter(StringBuilder buf, ParameterDefinition parameter)
84 {
85 if (parameter.ParameterType is ByReferenceType && parameter.IsOut)
86 {
87 //no notion of out -> mark with attribute to distinguish in other languages
88 buf.Append("[Runtime::InteropServices::Out] ");
89 }
90
91 if (parameter.HasCustomAttributes)
92 {
93 var isParams = parameter.CustomAttributes.Any(ca => ca.AttributeType.Name == "ParamArrayAttribute");
94 if (isParams)
95 buf.AppendFormat("... ");
96 }
97
98 buf.Append(GetTypeNameWithOptions(parameter.ParameterType, !AppendHatOnReturn)).Append(" ");
99 buf.Append(parameter.Name);
100
101 if (parameter.HasDefault && parameter.IsOptional && parameter.HasConstant)
102 {
103 buf.AppendFormat(" = {0}", MDocUpdater.MakeAttributesValueString(parameter.Constant, parameter.ParameterType));
104 }
105
106 return buf;
107 }
108
109 protected override string GetTypeKind(TypeDefinition t)
110 {
111 if (t.IsEnum || t.FullName == "System.Enum")
112 return "enum";
113 if (t.IsValueType)
114 return "struct";
115 if (t.IsClass)
116 return "class";
117 if (t.IsInterface)
118 return "__interface";
119 throw new ArgumentException(t.FullName);
120 }
121
122 protected override StringBuilder AppendArrayTypeName(StringBuilder buf, TypeReference type,
123 DynamicParserContext context)
124 {
125 buf.Append("std::Array <");
126
127 var item = type is TypeSpecification spec ? spec.ElementType : type.GetElementType();
128 _AppendTypeName(buf, item, context);
129 AppendHat(buf, item);
130
131 if (type is ArrayType arrayType)
132 {
133 int rank = arrayType.Rank;
134 if (rank > 1)
135 {
136 buf.AppendFormat(", {0}", rank);
137 }
138 }
139
140 buf.Append(">");
141
142 return buf;
143 }
144
145 protected override StringBuilder AppendExplisitImplementationMethod(StringBuilder buf, MethodDefinition method)
146 {
147 //no need to add additional syntax
148 return buf;
149 }
150
151 protected override string GetTypeDeclaration(TypeDefinition type)
152 {
153 StringBuilder buf = new StringBuilder();
154
155 var genericParamList = GetTypeSpecifiGenericParameters(type);
156 AppendGenericItem(buf, genericParamList);
157 AppendGenericTypeConstraints(buf, type);
158
159 AppendWebHostHiddenAttribute(buf, type);
160
161 buf.Append(GetTypeKind(type));
162 buf.Append(" ");
163 buf.Append(GetCppType(type.FullName) == null
164 ? GetNameWithOptions(type, false, false)
165 : type.Name);
166
167 if (type.IsAbstract && !type.IsInterface)
168 buf.Append(" abstract");
169 if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
170 buf.Append(" sealed");
171
172 CppWinRtFullMemberFormatter full = new CppWinRtFullMemberFormatter();
173
174 if (!type.IsEnum)
175 {
176 TypeReference basetype = type.BaseType;
177 if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType) // FIXME
178 basetype = null;
179
180 List<string> interfaceNames;
181 try
182 {
183 //for winRt Resolve() can fail as Cecil understands winRt types as .net (eg, "System.Object" cannot be resolved)
184 interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
185 .Select(iface => full.GetNameWithOptions(iface, true, false))
186 .OrderBy(s => s)
187 .ToList();
188 }
189 catch
190 {
191 interfaceNames = null;
192 }
193
194 if (basetype != null || interfaceNames?.Count > 0)
195 buf.Append(" : ");
196
197 if (basetype != null)
198 {
199 buf.Append(full.GetNameWithOptions(basetype, true, false));
200 if (interfaceNames?.Count > 0)
201 buf.Append(", ");
202 }
203
204 for (int i = 0; i < interfaceNames?.Count; i++)
205 {
206 if (i != 0)
207 buf.Append(", ");
208 buf.Append(interfaceNames?[i]);
209 }
210
211 }
212
213 return buf.ToString();
214 }
215
216 protected override string GetTypeVisibility(TypeAttributes ta)
217 {
218 //Cannot have pubic/protected visibility since it uses native C++ which cannot be exposed
219 return string.Empty;
220 }
221
222 protected override StringBuilder AppendVisibility(StringBuilder buf, MethodDefinition method)
223 {
224 //Cannot have pubic/protected visibility since it uses native C++ which cannot be exposed
225 return buf;
226 }
227
228 protected override StringBuilder AppendFieldVisibility(StringBuilder buf, FieldDefinition field)
229 {
230 //Cannot have pubic/protected visibility since it uses native C++ which cannot be exposed
231 return buf;
232 }
233
234 protected override StringBuilder AppendGenericItem(StringBuilder buf, IList<GenericParameter> args)
235 {
236 if (args != null && args.Any())
237 {
238 buf.Append("template <typename ");
239 buf.Append(args[0].Name);
240 for (int i = 1; i < args.Count; ++i)
241 buf.Append(", typename ").Append(args[i].Name);
242 buf.Append(">");
243 buf.Append(GetLineEnding());
244 }
245 return buf;
246 }
247
248 protected override string AppendSealedModifiers(string modifiersString, MethodDefinition method)
249 {
250 if (method.IsFinal) modifiersString += " sealed";
251 if (modifiersString == " virtual sealed") modifiersString = "";
252
253 return modifiersString;
254 }
255
256 public override bool IsSupported(TypeReference tref)
257 {
258 if (HasNestedClassesDuplicateNames(tref))
259 return false;
260
261 var ns = DocUtils.GetNamespace(tref);
262 var allowedTypes = GetAllowedTypes();
263
264 if (allowedTypes.Contains(tref.FullName.Split(' ')[0])
265 //winRt specific namespaces so no need for further check
266 || ns.StartsWith("Windows.")
267 )
268 {
269 return true;
270 }
271
272 TypeDefinition typedef = null;
273 try
274 {
275 typedef = tref.Resolve();
276 }
277 catch
278 {
279 //for winRt Resolve() can fail as Cecil understands winRt types as .net (eg, "System.Object" cannot be resolved)
280 }
281
282 if (typedef != null)
283 {
284 if(allowedTypes.Contains(typedef.FullName))
285 //to check type of array
286 return true;
287
288 if (DocUtils.IsDelegate(typedef))
289 {
290 //delegates can be used only in managed context
291 return false;
292 }
293
294 if (typedef.HasGenericParameters &&
295 typedef.GenericParameters.Any(x => x.HasConstraints
296 || x.HasReferenceTypeConstraint
297 || x.HasDefaultConstructorConstraint
298 || x.HasNotNullableValueTypeConstraint)
299 )
300 {
301 //Type parameters cannot be constrained
302 return false;
303 }
304
305 if (HasUnsupportedParent(typedef))
306 {
307 return false;
308 }
309 }
310
311 return IsSupportedGenericParameter(tref)
312 && !ns.StartsWith("System.") && !ns.Equals("System");
313 }
314
315 public override bool IsSupportedProperty(PropertyDefinition pdef)
316 {
317 //properties can be used only in managed context
318 return false;
319 }
320
321 public override bool IsSupportedEvent(EventDefinition edef)
322 {
323 //events can be used only in managed context
324 return false;
325 }
326
327 public override bool IsSupportedField(FieldDefinition fdef)
328 {
329 return IsSupported(fdef.FieldType);
330 }
331
332 public override bool IsSupportedMethod(MethodDefinition mdef)
333 {
334 if (DocUtils.IsExtensionMethod(mdef)
335 //no support of 'params';
336 //can be substituted with 'Variadic functions' hovewer it's not full equivalent(not type safe + specific mechanism for reading)
337 || mdef.Parameters.Any(IsParamsParameter)
338 )
339 {
340 return false;
341 }
342
343 return
344 IsSupported(mdef.ReturnType)
345 && mdef.Parameters.All(i => IsSupported(i.ParameterType));
346 }
347 }
348 }