Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AttributeValueFormatter.cs « AttributeFormatters « Formatters « Updater « Mono.Documentation « mdoc - github.com/mono/api-doc-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 415667400a4550f22553f554f4410b69503740a7 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Mono.Documentation.Updater
{
    public class AttributeValueFormatter
    {            
        public string Format(TypeReference argumentType, object argumentValue)
        {
            // When a property type of an attribute is an object type you can assign any type to it,
            // so we need to convert the object type to a concrete object type.
            if (argumentValue is CustomAttributeArgument attributeArgument)
            {
                return Format(attributeArgument.Type, attributeArgument.Value);
            }

            return FormatValue(argumentType, argumentValue);
        }

        // The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
        // https://github.com/dotnet/csharplang/blob/main/spec/attributes.md#attribute-parameter-types
        private string FormatValue(TypeReference argumentType, object argumentValue)
        {
            if (IsNull(argumentValue))
            {
                return "null";
            }

            if (IsArrayType(argumentType, argumentValue))
            {
                return ConvertToArrayType(argumentType, argumentValue);
            }

            if (IsTypeType(argumentType, argumentValue))
            {
                return ConvertToType(argumentType, argumentValue);
            }

            if (IsStringType(argumentType, argumentValue))
            {
                return ConvertToString(argumentType, argumentValue);
            }

            if (IsCharType(argumentType, argumentValue))
            {
                return ConvertToChar(argumentType, argumentValue);
            }

            if (IsBoolType(argumentType, argumentValue))
            {
                return ConvertToBool(argumentType, argumentValue);
            }

            if (IsEnumType(argumentType, argumentValue))
            {
                return ConvertToEnum(argumentType, argumentValue);
            }

            return ConvertUnhandledTypeToString(argumentValue);
        }

        private bool IsNull(object argumentValue)
        {
            return argumentValue == null;
        }

        private bool IsEnumType(TypeReference argumentType, object argumentValue)
        {
            return argumentType.Resolve().IsEnum;
        }

        private bool IsTypeType(TypeReference argumentType, object argumentValue)
        {
            return IsType("System.Type", argumentType, argumentValue);
        }

        private bool IsStringType(TypeReference argumentType, object argumentValue)
        {
            return IsType("System.String", argumentType, argumentValue);
        }

        private bool IsCharType(TypeReference argumentType, object argumentValue)
        {
            return IsType("System.Char", argumentType, argumentValue);
        }

        private bool IsBoolType(TypeReference argumentType, object argumentValue)
        {
            return IsType("System.Boolean", argumentType, argumentValue);
        }

        private bool IsType(string typeFullName, TypeReference argumentType, object argumentValue)
        {
            return argumentType.FullName.Equals(typeFullName);
        }

        private bool IsArrayType(TypeReference argumentType, object argumentValue)
        {
            return argumentType is ArrayType && argumentValue is CustomAttributeArgument[];
        }

        private string ConvertToArrayType(TypeReference argumentType, object argumentValue)
        {
            var arrayType = argumentType as ArrayType;
            var arrayArguments = argumentValue as CustomAttributeArgument[];
            var arrayTypeFullName = $"new {arrayType.FullName}{(arrayType.FullName.EndsWith("[]") ? "" : "[]")}";
            var arrayArgumentValues = arrayArguments.Select(i => FormatValue(i.Type, i.Value));

            return $"{arrayTypeFullName} {{ {string.Join(", ", arrayArgumentValues)} }}";
        }

        private bool IsFlagsEnum(TypeReference argumentType, object argumentValue)
        {
            var argumentTypeDefinition = argumentType.Resolve();
            var isApplyFlagsAttributeEnumType = argumentTypeDefinition.CustomAttributes.Any(a => a.AttributeType.FullName == "System.FlagsAttribute");
            var isNotApplyAttributeFlagsEnumType = IsNotApplyAttributeFlagsEnumType(argumentTypeDefinition, argumentValue);

            return isApplyFlagsAttributeEnumType || isNotApplyAttributeFlagsEnumType;
        }

        /// <summary>
        /// We have a few legacy flags enum type not apply FlagsAttribute to it.
        /// For example, Microsoft.JScript.JSFunctionAttributeEnum in .NET Framework 1.1 but the issue has been fixed in the newer version.
        /// </summary>
        private bool IsNotApplyAttributeFlagsEnumType(TypeDefinition argumentType, object argumentValue)
        {
            (var typeFullName, var enumConstants, var enumValue) = ExtractEnumTypeData(argumentType, argumentValue);
            if (enumConstants.ContainsKey(enumValue))
            {
                // Not is a combinations of values.
                return false;
            }

            var flagsEnumValues = enumConstants.Keys.ToList();
            flagsEnumValues.Remove(0);  // The zero value is not a valid flags enum value.

            // The following example is an invalid and valid flags enum type.
            // None = 0, Read = 1, Write = 2, ReadWrite = 3 maybe is a flags enum type but sometimes it is not.
            // Read = 1, Write = 2, Open = 4, Close = 8 actually is a flags enum type.
            var minFlagsEnumValueCount = 4;
            if (flagsEnumValues.Count() >= minFlagsEnumValueCount)
            {
                long allEnumLogicalOrValue = 0;
                foreach (var item in flagsEnumValues)
                {
                    allEnumLogicalOrValue = allEnumLogicalOrValue | item;
                }

                var isFlagsEnumType = !flagsEnumValues.Any(i => (i & allEnumLogicalOrValue) != i);
                var isCombinationValue = flagsEnumValues.Count(i => (i & enumValue) != i) > 1;

                return isFlagsEnumType && isCombinationValue;
            }

            return false;
        }

        private bool IsApplePlatformEnum(TypeReference argumentType, object argumentValue)
        {
            return MDocUpdater.GetDocTypeFullName(argumentType).Contains("ObjCRuntime.Platform");
        }

        private string ConvertToType(TypeReference argumentType, object argumentValue)
        {
            var valueResult = GetArgumentValue("System.Type", argumentType, argumentValue);
            var typeFullName = MDocUpdater.GetDocTypeFullName((TypeReference)valueResult, isTypeofOperator: true);

            return $"typeof({typeFullName})";
        }

        private string ConvertToString(TypeReference argumentType, object argumentValue)
        {
            var valueResult = GetArgumentValue("System.String", argumentType, argumentValue);
            if (valueResult == null)
            {
                return "null";
            }

            return string.Format("\"{0}\"", FilterSpecialChars(valueResult.ToString()));
        }

        private string ConvertToBool(TypeReference argumentType, object argumentValue)
        {
            return GetArgumentValue("System.Boolean", argumentType, argumentValue).ToString().ToLower();
        }

        private string ConvertToChar(TypeReference argumentType, object argumentValue)
        {
            var valueResult = GetArgumentValue("System.Char", argumentType, argumentValue).ToString();

            return string.Format("'{0}'", FilterSpecialChars(valueResult));
        }

        private string ConvertUnhandledTypeToString(object argumentValue)
        {
            return argumentValue.ToString();
        }

        private string ConvertToEnum(TypeReference argumentType, object argumentValue)
        {
            if (IsFlagsEnum(argumentType, argumentValue))
            {
                if (IsApplePlatformEnum(argumentType, argumentValue))
                {
                    return ConvertToApplePlatformEnum(argumentType, argumentValue);
                }

                return ConvertToFlagsEnum(argumentType, argumentValue);
            }

            return ConvertToNormalEnum(argumentType, argumentValue);
        }

        private string ConvertToNormalEnum(TypeReference argumentType, object argumentValue)
        {
            (var typeFullName, var enumConstants, var enumValue) = ExtractEnumTypeData(argumentType, argumentValue);
            if (enumConstants.ContainsKey(enumValue))
            {
                return typeFullName + "." + enumConstants[enumValue];
            }

            return ConvertToUnknownEnum(argumentType, argumentValue);
        }

        private string ConvertToUnknownEnum(TypeReference argumentType, object argumentValue)
        {
            (var typeFullName, var enumConstants, var enumValue) = ExtractEnumTypeData(argumentType, argumentValue);

            return $"({typeFullName}) {enumValue}";
        }

        private string ConvertToFlagsEnum(TypeReference argumentType, object argumentValue)
        {
            (var typeFullName, var enumConstants, var enumValue) = ExtractEnumTypeData(argumentType, argumentValue);
            if (enumConstants.ContainsKey(enumValue))
            {
                // Not is a combinations of values.
                return $"{typeFullName}.{enumConstants[enumValue]}";
            }

            var flagsEnumValues = enumConstants.Keys.Where(i => (enumValue & i) == i && i != 0).ToList();
            var duplicateEnumValues = flagsEnumValues.Where(i => flagsEnumValues.Any(a => (a & i) == i && a > i));

            flagsEnumValues.RemoveAll(i => duplicateEnumValues.Contains(i));
            var flagsEnumNames = flagsEnumValues
                                 .Select(i => $"{typeFullName}.{enumConstants[i]}")
                                 .OrderBy(val => val) // to maintain a consistent list across frameworks/versions
                                 .ToArray();

            if (flagsEnumNames.Length > 0)
            {
                return string.Join(" | ", flagsEnumNames);
            }

            return ConvertToUnknownEnum(argumentType, argumentValue);
        }

        private string ConvertToApplePlatformEnum(TypeReference argumentType, object argumentValue)
        {
            (var typeFullName, var enumConstants, var enumValue) = ExtractEnumTypeData(argumentType, argumentValue);
            if (enumConstants.ContainsKey(enumValue))
            {
                return typeFullName + "." + enumConstants[enumValue];
            }

            return FormatApplePlatformEnum(enumValue);
        }

        private (string typeFullName, IDictionary<long, string> enumConstants, long enumValue) ExtractEnumTypeData(TypeReference argumentType, object argumentValue)
        {
            var argumentTypeDefinition = argumentType.Resolve();
            var typeFullName = MDocUpdater.GetDocTypeFullName(argumentTypeDefinition);
            var enumConstants = GetEnumerationValues(argumentTypeDefinition);
            var enumValue = ToInt64(argumentValue);

            return (typeFullName, enumConstants, enumValue);
        }

        private string FormatApplePlatformEnum(long enumValue)
        {
            int iosarch, iosmajor, iosminor, iossubminor;
            int macarch, macmajor, macminor, macsubminor;
            GetEncodingiOS(enumValue, out iosarch, out iosmajor, out iosminor, out iossubminor);
            GetEncodingMac((ulong)enumValue, out macarch, out macmajor, out macminor, out macsubminor);

            if (iosmajor == 0 & iosminor == 0 && iossubminor == 0)
            {
                return FormatApplePlatformEnumValue("Mac", macarch, macmajor, macminor, macsubminor);
            }

            if (macmajor == 0 & macminor == 0 && macsubminor == 0)
            {
                return FormatApplePlatformEnumValue("iOS", iosarch, iosmajor, iosminor, iossubminor);
            }

            return string.Format("(Platform){0}", enumValue);
        }

        private string FormatApplePlatformEnumValue(string plat, int arch, int major, int minor, int subminor)
        {
            var archstring = string.Empty;
            switch (arch)
            {
                case 1:
                    archstring = "32";
                    break;
                case 2:
                    archstring = "64";
                    break;
            }

            return string.Format("Platform.{4}_{0}_{1}{2} | Platform.{4}_Arch{3}",
                major,
                minor,
                subminor == 0 ? "" : "_" + subminor.ToString(),
                archstring,
                plat
            );
        }

        private void GetEncodingiOS(long entireLong, out int archindex, out int major, out int minor, out int subminor)
        {
            long lowerBits = entireLong & 0xffffffff;
            int lowerBitsAsInt = (int)lowerBits;
            GetEncodingApplePlatform(lowerBitsAsInt, out archindex, out major, out minor, out subminor);
        }

        private void GetEncodingMac(ulong entireLong, out int archindex, out int major, out int minor, out int subminor)
        {
            ulong higherBits = entireLong & 0xffffffff00000000;
            int higherBitsAsInt = (int)((higherBits) >> 32);
            GetEncodingApplePlatform(higherBitsAsInt, out archindex, out major, out minor, out subminor);
        }

        private void GetEncodingApplePlatform(Int32 encodedBits, out int archindex, out int major, out int minor, out int subminor)
        {
            // format is AAJJNNSS
            archindex = (int)((encodedBits & 0xFF000000) >> 24);
            major = (int)((encodedBits & 0x00FF0000) >> 16);
            minor = (int)((encodedBits & 0x0000FF00) >> 8);
            subminor = (int)((encodedBits & 0x000000FF) >> 0);
        }

        private object GetArgumentValue(string argumentTypeFullName, TypeReference argumentType, object argumentValue)
        {
            if (argumentType.FullName.Equals(argumentTypeFullName))
            {
                return argumentValue;
            }

            throw new ArgumentException($"The argument type does not match {argumentTypeFullName}.");
        }

        private IDictionary<long, string> GetEnumerationValues(TypeDefinition argumentType)
        {
            var enumValues = from f in argumentType.Fields
                             where !(f.IsRuntimeSpecialName || f.IsSpecialName)
                             select f;

            var values = new Dictionary<long, string>();
            foreach (var item in enumValues)
            {
                values[ToInt64(item.Constant)] = item.Name;
            }

            return values;
        }

        private long ToInt64(object value)
        {
            if (value is ulong)
                return (long)(ulong)value;

            return Convert.ToInt64(value);
        }

        private string FilterSpecialChars(string value)
        {
            return value
                .Replace("\0", "\\0")
                .Replace("\t", "\\t")
                .Replace("\n", "\\n")
                .Replace("\r", "\\r")
                .Replace("\f", "\\f")
                .Replace("\b", "\\b");
        }
    }
}