Skip to content
← Back

src/semantic/interpolated_value_rendering.ghul

1
namespace Semantic is
2
use Semantic.Types.Type
3
4
// How an interpolated value becomes text.
5
enum InterpolatedValueRendering is
6
// Formatted as .NET formats it: a string, a number, an enum, a
7
// type declaring its own to_string, or any value given a format
8
// or an alignment.
9
FORMATTED,
10
11
// `true` or `false`, as ghūl spells them.
12
BOOL,
13
14
// The value it holds, rendered by the rules for its non-optional
15
// type, or `null` when it holds none.
16
OPTIONAL,
17
18
// Rendered by the runtime's `$`: a value whose static type gives
19
// it no text of its own, which .NET would print as its type name.
20
DISPLAYED
21
si
22
23
class INTERPOLATED_VALUE_CLASSIFIER is
24
_lookup: Lookups.InnateSymbolLookup
25
26
init(lookup: Lookups.InnateSymbolLookup) is
27
_lookup = lookup
28
si
29
30
classify(type: Type, is_formatted: bool) -> InterpolatedValueRendering is
31
if is_formatted then
32
return InterpolatedValueRendering.FORMATTED
33
fi
34
35
if type.is_optional /\ type.optional_inner_type? then
36
return InterpolatedValueRendering.OPTIONAL
37
fi
38
39
if _is(type, _lookup.get_bool_type()) then
40
return InterpolatedValueRendering.BOOL
41
fi
42
43
// An enum reads as its member's name wherever its base class
44
// library gives it no to_string of its own to say so.
45
if isa Symbols.ENUM_STRUCT(type.symbol) then
46
return InterpolatedValueRendering.FORMATTED
47
fi
48
49
// A function value has no text worth reading beyond its type,
50
// and rendering its delegate's members would expose the
51
// compiler's closure machinery.
52
if type.is_function \/ type.is_action then
53
return InterpolatedValueRendering.FORMATTED
54
fi
55
56
if
57
type.is_value_tuple \/
58
isa Types.TUPLE(type) \/
59
type.is_object \/
60
type.is_type_variable \/
61
type.is_classy_generic_argument \/
62
type.is_function_generic_argument
63
then
64
return InterpolatedValueRendering.DISPLAYED
65
fi
66
67
if _is(type, _lookup.get_string_type()) \/ has_own_to_string(type) then
68
return InterpolatedValueRendering.FORMATTED
69
fi
70
71
if isa Types.ARRAY(type) \/ type.is_trait \/ type.find_ancestor(_lookup.get_unspecialized_iterable_type())? then
72
return InterpolatedValueRendering.DISPLAYED
73
fi
74
75
// An imported type that says nothing of itself keeps its .NET
76
// type name: rendering it by its members would run whatever
77
// its property getters do.
78
if type.symbol.is_reflected then
79
return InterpolatedValueRendering.FORMATTED
80
fi
81
82
return InterpolatedValueRendering.DISPLAYED
83
si
84
85
// Whether a type says what it is in its own words, through a
86
// to_string declared somewhere other than the roots every type
87
// inherits one from. An enum counts: System.Enum's to_string gives
88
// the member's name.
89
has_own_to_string(type: Type) -> bool is
90
let to_string = zero_argument_to_string(type.find_member("to_string"))
91
92
if !to_string? then
93
return false
94
fi
95
96
let owner = to_string.owner
97
98
return
99
owner? /\
100
owner != _lookup.get_object_type().symbol /\
101
owner != _lookup.get_value_type().symbol
102
si
103
104
// The overload of the runtime's `$` that renders any value, or
105
// null where the runtime referenced has no `$`.
106
display_function: Symbols.Function? is
107
if let functions = _lookup.get_display_functions() then
108
for function in functions.functions do
109
if !function.is_generic /\ function.arguments.count == 1 then
110
let parameter = function.arguments[0].type!
111
112
if (parameter.optional_inner_type ?? parameter).is_object then
113
return function
114
fi
115
fi
116
od
117
fi
118
119
return null
120
si
121
122
_is(type: Type, other: Type) -> bool =>
123
type.compare(other) == Types.MATCH.SAME
124
125
zero_argument_to_string(member: Symbols.Symbol?) -> Symbols.Function? static is
126
if let group: Symbols.FUNCTION_GROUP = member then
127
for function in group.functions do
128
if !function.are_arguments_declared \/ function.arguments.count == 0 then
129
return function
130
fi
131
od
132
fi
133
134
return null
135
si
136
si
137
si