Skip to content
← Back

src/ir/program/instances.ghul

1
namespace IR.Program is
2
use Collections.LIST
3
use Collections.MAP
4
5
use Semantic.Types.Type
6
use Semantic.Symbols.Symbol
7
use Semantic.Symbols.Classy
8
use Semantic.Symbols.Function
9
use Semantic.Symbols.GENERIC
10
11
/// A class as a whole-program target compiles it: a declared class at one
12
/// choice of its type arguments. Every instantiation a program uses is
13
/// compiled separately, and a class that is not generic has one, with no
14
/// arguments.
15
///
16
/// - definition: the declared class
17
/// - arguments: its type arguments, in declaration order
18
/// - type_map: each of the definition's type parameters, to its argument
19
class CLASS_INSTANCE(definition: Classy, arguments: Collections.List[Type], type_map: Collections.Map[Symbol, Type]) is
20
/// The class with no type arguments.
21
of(definition: Classy) -> CLASS_INSTANCE static =>
22
CLASS_INSTANCE(definition, LIST[Type](), MAP[Symbol, Type]())
23
24
/// `type`, with the class's type parameters replaced by its
25
/// arguments.
26
specialize(type: Type?) -> Type? => TYPE_MAPS.specialize(type, type_map)
27
28
=~(other: CLASS_INSTANCE) -> bool pure =>
29
definition == other.definition /\ TYPE_MAPS.same(type_map, other.type_map)
30
31
get_hash_code() -> int pure => definition.get_hash_code() * 31 + TYPE_MAPS.hash(type_map)
32
33
to_string() -> string =>
34
if arguments.count == 0 then "{definition}" else "{definition}[{arguments |> Ghul.Pipes.join(",")}]" fi
35
si
36
37
/// A function as a whole-program target compiles it: a declared function at
38
/// one choice of the type arguments its body can name, its class's and
39
/// its own.
40
///
41
/// - definition: the declared function, whose body is the one compiled
42
/// - type_map: each type parameter the body can name, to its argument
43
class FUNCTION_INSTANCE(definition: Function, type_map: Collections.Map[Symbol, Type]) is
44
/// The function with no type arguments.
45
of(definition: Function) -> FUNCTION_INSTANCE static =>
46
FUNCTION_INSTANCE(definition, MAP[Symbol, Type]())
47
48
/// `type`, with the type parameters the body can name replaced by
49
/// their arguments.
50
specialize(type: Type?) -> Type? => TYPE_MAPS.specialize(type, type_map)
51
52
=~(other: FUNCTION_INSTANCE) -> bool pure =>
53
definition == other.definition /\ TYPE_MAPS.same(type_map, other.type_map)
54
55
get_hash_code() -> int pure => definition.get_hash_code() * 31 + TYPE_MAPS.hash(type_map)
56
57
to_string() -> string =>
58
if type_map.count == 0 then "{definition.qualified_name}" else "{definition.qualified_name}[{type_map.values |> Ghul.Pipes.join(",")}]" fi
59
si
60
si