Skip to content
← Back

src/semantic/dotnet/tuple_element_names.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.DotNet is
4
use Collections
5
6
use ATTRIBUTE_DATA = System.Reflection.CustomAttributeData
7
use TYPED_ARGUMENT = System.Reflection.CustomAttributeTypedArgument
8
9
use Ghul.Pipes
10
11
// Reads `System.Runtime.CompilerServices.TupleElementNamesAttribute`
12
// — the standard .NET carrier for tuple element names — off the
13
// custom attributes of a reflected member, parameter, field or
14
// property. The `ValueTuple` type itself holds no names, so a
15
// reflected tuple needs this to recover the names a C# (or ghūl)
16
// producer recorded at the declaration site.
17
class TUPLE_ELEMENT_NAMES is
18
// The flattened element-name array declared by a
19
// TupleElementNamesAttribute among `attributes`, or null when
20
// there is none. A null entry marks an unnamed element.
21
read(attributes: Iterable[ATTRIBUTE_DATA]?) -> List[string?]? static is
22
if !attributes? then
23
return null
24
fi
25
26
try
27
for attribute in attributes do
28
let attribute_type = attribute.attribute_type
29
30
if
31
attribute_type.full_name =~ "System.Runtime.CompilerServices.TupleElementNamesAttribute"
32
then
33
for constructor_argument in attribute.constructor_arguments do
34
return _read_names(constructor_argument)
35
od
36
fi
37
od
38
catch ex: System.Exception
39
yrt
40
41
return null
42
si
43
44
// The single `string[]` constructor argument surfaces as a
45
// collection of CustomAttributeTypedArgument, one per element.
46
_read_names(argument: TYPED_ARGUMENT) -> List[string?]? static is
47
let elements = cast Iterable[TYPED_ARGUMENT]?(argument.value)
48
49
if !elements? then
50
return null
51
fi
52
53
let result = LIST[string?]()
54
55
for element in elements do
56
result.add(cast string?(element.value))
57
od
58
59
return result
60
si
61
62
// The element names a `TupleElementNamesAttribute` on a slot of
63
// `type` would carry, or null when no tuple anywhere in it is
64
// named. The array covers every tuple in the type, however deep:
65
// a pre-order walk that lists each tuple's own element names and
66
// then descends into its type's arguments, left to right, which
67
// is the layout the attribute is defined to have and C# writes.
68
// An unnamed element, or an unnamed tuple, takes nulls.
69
attribute_names_for_type(type: Types.Type) -> List[string?]? static is
70
let names = LIST[string?]()
71
72
_flatten(type, names)
73
74
if !(names |> any(name => name?)) then
75
return null
76
fi
77
78
return names
79
si
80
81
_flatten(type: Types.Type?, names: LIST[string?]) static is
82
if !type? then
83
return
84
fi
85
86
let arguments = type.arguments
87
88
if _is_value_tuple(type) then
89
let own = type.tuple_element_names
90
91
for index in 0..arguments.count do
92
names.add(if own? /\ index < own.count then own[index] else null fi)
93
od
94
fi
95
96
for argument in arguments do
97
_flatten(argument, names)
98
od
99
si
100
101
// `type` with the names from a flattened attribute array put back
102
// on every tuple in it, by the same walk that wrote them.
103
apply(type: Types.Type, names: List[string?]) -> Types.Type static is
104
let (result, _, _) = _apply_walk(type, names, 0)
105
106
return result
107
si
108
109
// Returns the rebuilt type, the next index into `names`, and
110
// whether anything in the type took a name, so that an enclosing
111
// generic is rebuilt only when one of its arguments changed.
112
_apply_walk(type: Types.Type, names: List[string?], start: int) -> (Types.Type, int, bool) static is
113
let arguments = type.arguments
114
let index mut = start
115
let own: LIST[string?]? mut = null
116
117
if _is_value_tuple(type) then
118
let taken = LIST[string?]()
119
120
for _ in 0..arguments.count do
121
taken.add(if index < names.count then names[index] else null fi)
122
123
index = index + 1
124
od
125
126
own = taken
127
fi
128
129
let rebuilt mut = type
130
let changed mut = false
131
132
if arguments.count > 0 then
133
let new_arguments = LIST[Types.Type]()
134
135
for argument in arguments do
136
let (new_argument, next, argument_changed) = _apply_walk(argument, names, index)
137
138
index = next
139
140
if argument_changed then
141
changed = true
142
fi
143
144
new_arguments.add(new_argument)
145
od
146
147
if changed then
148
if let generic: Types.GENERIC = type then
149
if let rebuilt_generic = NULLABILITY.rebuild_generic(generic, new_arguments) then
150
rebuilt = rebuilt_generic
151
fi
152
fi
153
fi
154
fi
155
156
if let named = own /\ named |> any(name => name?) then
157
rebuilt = rebuilt.apply_tuple_element_names(named)
158
changed = true
159
fi
160
161
return (rebuilt, index, changed)
162
si
163
164
_is_value_tuple(type: Types.Type) -> bool static =>
165
type.is_value_tuple \/ isa Types.TUPLE(type)
166
si
167
si