forked from dotnet/macios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNomenclator.cs
More file actions
100 lines (77 loc) · 2.97 KB
/
Copy pathNomenclator.cs
File metadata and controls
100 lines (77 loc) · 2.97 KB
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
using System;
using System.Collections.Generic;
using System.Reflection;
#nullable enable
// Noun. nomenclator (plural nomenclators) An assistant who specializes in providing timely and spatially relevant
// reminders of the names of persons and other socially important information
public class Nomenclator {
readonly Dictionary<string, bool> skipGeneration = new ();
readonly HashSet<string> repeatedDelegateApiNames = new ();
readonly Dictionary<Type, int> trampolinesGenericVersions = new ();
readonly IAttributeManager attributeManager;
public Nomenclator (IAttributeManager attributeManager)
{
this.attributeManager = attributeManager;
}
public string GetDelegateName (MethodInfo mi)
{
Attribute a = attributeManager.GetCustomAttribute<DelegateNameAttribute> (mi);
if (a is not null)
return ((DelegateNameAttribute) a).Name;
a = attributeManager.GetCustomAttribute<EventArgsAttribute> (mi);
if (a is null)
throw new BindingException (1006, true, mi.DeclaringType!.FullName, mi.Name);
ErrorHelper.Warning (1102, mi.DeclaringType!.FullName, mi.Name);
return ((EventArgsAttribute) a).ArgName;
}
public string GetEventName (MethodInfo mi)
{
var a = attributeManager.GetCustomAttribute<EventNameAttribute> (mi);
return a is null ? mi.Name : a.EvtName;
}
public string GetDelegateApiName (MethodInfo mi)
{
var apiName = attributeManager.GetCustomAttribute<DelegateApiNameAttribute> (mi);
if (repeatedDelegateApiNames.Contains (mi.Name) && apiName is null)
throw new BindingException (1043, true, mi.Name);
if (apiName is null) {
repeatedDelegateApiNames.Add (mi.Name);
return mi.Name;
}
if (repeatedDelegateApiNames.Contains (apiName.Name))
throw new BindingException (1044, true, apiName.Name);
return apiName.Name;
}
public string GetEventArgName (MethodInfo mi)
{
if (mi.GetParameters ().Length == 1)
return "EventArgs";
var a = attributeManager.GetCustomAttribute<EventArgsAttribute> (mi);
if (a is null)
throw new BindingException (1004, true, mi.DeclaringType!.FullName, mi.Name, mi.GetParameters ().Length);
var ea = (EventArgsAttribute) a;
if (ea.ArgName.EndsWith ("EventArgs", StringComparison.Ordinal))
throw new BindingException (1005, true, mi.DeclaringType!.FullName, mi.Name);
if (ea.SkipGeneration) {
skipGeneration [ea.FullName ? ea.ArgName : ea.ArgName + "EventArgs"] = true;
}
if (ea.FullName)
return ea.ArgName;
return ea.ArgName + "EventArgs";
}
public bool WasEventArgGenerated (string eaclass)
=> skipGeneration.ContainsKey (eaclass);
public string GetTrampolineName (Type t)
{
var trampolineName = t.Name.Replace ("`", "Arity");
if (t.IsGenericType) {
var gdef = t.GetGenericTypeDefinition ();
if (!trampolinesGenericVersions.ContainsKey (gdef))
trampolinesGenericVersions.Add (gdef, 0);
trampolineName = trampolineName + "V" + trampolinesGenericVersions [gdef]++;
}
return trampolineName;
}
public void ForgetDelegateApiNames ()
=> repeatedDelegateApiNames.Clear ();
}