Skip to content
← Back

src/semantic/dotnet/assembly_info.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.DotNet is
4
use Collections
5
6
use TYPE = System.Type
7
8
// Attributes applied to the assembly itself, each requested as the
9
// attribute's full name and a string value. A request is matched to a
10
// public constructor of the attribute taking only strings before
11
// anything is emitted: one taking a single string is given the whole
12
// value, and failing that one taking two is given the value split at
13
// its first comma, which is how an attribute such as
14
// AssemblyMetadataAttribute is given a key and a value.
15
class ASSEMBLY_INFO is
16
_requested: LIST[(name: string, value: string)]
17
_resolved: LIST[(name: string, assembly_name: string, arguments: List[string])]
18
19
UNMATCHED: string static => "assembly-attribute-unmatched"
20
21
init() is
22
_requested = LIST[(name: string, value: string)]()
23
_resolved = LIST[(name: string, assembly_name: string, arguments: List[string])]()
24
si
25
26
// The requested attributes that matched a constructor, each with
27
// the assembly its type is in and the strings it is given.
28
resolved: Collections.Iterable[(name: string, assembly_name: string, arguments: List[string])] => _resolved
29
30
add_attribute(name: string, value: string) is
31
_requested.add((name = name, value = value))
32
si
33
34
// Matches every requested attribute to a constructor, returning a
35
// message for each that cannot be. Only the matched ones are
36
// emitted, so nothing refers to a constructor that does not exist,
37
// and an unmatched one is left out of the assembly.
38
resolve(find_type: (string) -> TYPE?) -> List[string] is
39
let problems = LIST[string]()
40
41
_resolved.clear()
42
43
// Found the same way as the attributes, so that it is the string
44
// type their constructors are declared with: a type loaded for
45
// inspection is not the running process's own.
46
let string_type = find_type("System.String")
47
48
for request in _requested do
49
let type = find_type(request.name)
50
51
if !type? then
52
problems.add("assembly attribute {request.name} not found")
53
54
continue
55
fi
56
57
let arguments =
58
if let text = string_type then
59
arguments_for(string_constructor_arities(type, text), request.value)
60
else
61
null
62
fi
63
64
if let matched = arguments then
65
_resolved.add((
66
name = request.name,
67
assembly_name = type.assembly.get_name().name ?? "System.Runtime",
68
arguments = matched
69
))
70
else
71
problems.add(
72
"assembly attribute {request.name} has no constructor taking "
73
"{if request.value.contains(',') then "one or two strings" else "one string" fi}"
74
)
75
fi
76
od
77
78
return problems
79
si
80
81
// How many strings each public constructor taking nothing but
82
// strings takes.
83
string_constructor_arities(type: TYPE, string_type: TYPE) -> LIST[int] static is
84
let result = LIST[int]()
85
86
for constructor in type.get_constructors() do
87
let parameters = constructor.get_parameters()
88
let all_strings mut = true
89
90
for parameter in parameters do
91
if parameter.parameter_type != string_type then
92
all_strings = false
93
fi
94
od
95
96
if all_strings then
97
result.add(parameters.count)
98
fi
99
od
100
101
return result
102
si
103
104
// The constructor arguments a value supplies, given the arities of
105
// the string-only constructors on offer, or null if none fits.
106
arguments_for(arities: LIST[int], value: string) -> List[string]? static is
107
if arities.contains(1) then
108
let whole = LIST[string]()
109
110
whole.add(value)
111
112
return whole
113
fi
114
115
let comma = value.index_of(',')
116
117
if arities.contains(2) /\ comma >= 0 then
118
let pair = LIST[string]()
119
120
pair.add(value.substring(0, comma).trim())
121
pair.add(value.substring(comma + 1).trim())
122
123
return pair
124
fi
125
126
return null
127
si
128
si
129
si