Skip to content
← Back

src/semantic/types/tuple.ghul

1
namespace Semantic.Types is
2
use Source.LOCATION;
3
4
use Logging;
5
6
use Ghul.Pipes;
7
8
class TUPLE: GENERIC is
9
names: Collections.List[string?]?;
10
11
tuple_element_names: Collections.List[string?]? => names;
12
13
short_description: string => get_short_description(self, names);
14
15
// Build an element-name list of exactly `arity` entries from
16
// the leading entries of a flattened TupleElementNamesAttribute
17
// array, padding with null.
18
take_element_names(names: Collections.List[string?]?, arity: int) -> Collections.List[string?]? static is
19
if !names? then
20
return null;
21
fi
22
23
let result = Collections.LIST[string?]();
24
25
for i in 0..arity do
26
result.add(if i < names.count then names[i] else null fi);
27
od
28
29
return result;
30
si
31
32
33
get_short_description(tuple: Type, names: Collections.List[string?]?) -> string static is
34
let args = tuple.arguments;
35
36
assert args |> all(a => a?) else "tuple at least one null argument";
37
38
return
39
if names? then
40
"({(0..args.count) |> map(i => "{_get_element_name(names, i)}{args[i].short_description}")})";
41
else
42
"({args |> map(a => a.short_description)})";
43
fi
44
si
45
46
_get_element_name(names: Collections.List[string?]?, index: int) -> string static =>
47
if names? /\ index < names.count /\ names[index]? then
48
"{names[index]}: "
49
else
50
""
51
fi;
52
53
init(
54
location: LOCATION,
55
symbol: Symbols.Classy,
56
arguments: Collections.List[Type],
57
names: Collections.List[string?]?
58
) is
59
super.init(Symbols.TUPLE(location, symbol, arguments, names));
60
61
self.names = names;
62
si
63
64
// FIXME: requires type conversions in generated IL:
65
// get_argument_type_variance(index: int) -> Types.TYPE_VARIANCE => Types.TYPE_VARIANCE.COVARIANT;
66
67
create(
68
location: LOCATION,
69
symbol: Symbols.Classy,
70
arguments: Collections.List[Type]
71
) -> GENERIC =>
72
TUPLE(location, symbol, arguments, names);
73
74
to_string() -> string => get_short_description(self, names);
75
si
76
si