Skip to content
← Back

src/semantic/name_display.ghul

1
namespace Semantic is
2
use Symbols.Symbol
3
use Ghul.Disposable
4
5
// Decides the scope that symbol names render relative to. By default
6
// that is the scope currently being compiled (read on demand from the
7
// symbol table, so it stays correct however the scope stack is
8
// unwound), which is why a diagnostic reads relative to where it is
9
// reported. An override replaces that default for the duration of a
10
// language-service render (targeting the cursor's scope) or a region
11
// that needs scope-independent output (`with_scope(null)` forces fully
12
// qualified names, e.g. for a canonical ordering). Separately, a set of
13
// symbols can be marked to render fully qualified whatever the scope,
14
// so that a diagnostic never names two distinct types identically.
15
class NAME_DISPLAY is
16
_override_scope: Scope?
17
_has_override: bool
18
_qualified: Collections.List[Symbol]
19
20
init() is
21
_qualified = Collections.LIST[Symbol](0)
22
si
23
24
render_scope: Scope? =>
25
if _has_override then
26
_override_scope
27
else
28
_current_build_scope
29
fi
30
31
_current_build_scope: Scope? is
32
let symbol_table = IoC.CONTAINER.instance.symbol_table
33
34
return symbol_table.current_scope
35
si
36
37
name_for(symbol: Symbol) -> string =>
38
symbol.render_name(_scope_for(symbol))
39
40
// The scope-relative name without its type-argument suffix — for
41
// rendering a constructed generic's head, which then appends its own
42
// actual arguments.
43
bare_name_for(symbol: Symbol) -> string =>
44
symbol._render_scope_relative_name(_scope_for(symbol))
45
46
// The bare declared name, except for a symbol marked to render
47
// fully qualified.
48
short_name_for(symbol: Symbol) -> string =>
49
if _qualified.count > 0 /\ _is_qualified(symbol.unspecialized_symbol) then
50
symbol.unspecialized_symbol._render_scope_relative_name(null)
51
else
52
symbol.name
53
fi
54
55
_is_qualified(symbol: Symbol) -> bool is
56
for q in _qualified do
57
if q == symbol then
58
return true
59
fi
60
od
61
62
return false
63
si
64
65
_scope_for(symbol: Symbol) -> Scope? =>
66
if _qualified.count > 0 /\ _is_qualified(symbol) then
67
null
68
else
69
render_scope
70
fi
71
72
// Across `left` and `right`, render every distinct declaration that
73
// shares its name with another fully qualified until the returned
74
// holder is disposed. Consume with `let use`.
75
qualifying_alike(left: Types.Type?, right: Types.Type?) -> QUALIFIED_SYMBOLS_HOLDER is
76
let alike = ALIKE_NAMED_SYMBOLS()
77
78
alike.add(left)
79
alike.add(right)
80
81
return qualifying(alike)
82
si
83
84
qualifying(alike: ALIKE_NAMED_SYMBOLS) -> QUALIFIED_SYMBOLS_HOLDER is
85
let holder = QUALIFIED_SYMBOLS_HOLDER(self, _qualified)
86
87
if alike.symbols.count > 0 then
88
let combined = Collections.LIST[Symbol](_qualified)
89
90
combined.add_range(alike.symbols)
91
92
_qualified = combined
93
fi
94
95
return holder
96
si
97
98
restore_qualified(qualified: Collections.List[Symbol]) is
99
_qualified = qualified
100
si
101
102
// Render relative to `scope` (null forces fully qualified) until the
103
// returned holder is disposed. Consume with `let use` so the
104
// previous render scope is restored on every exit path, including
105
// when rendering throws.
106
with_scope(scope: Scope?) -> RENDER_SCOPE_HOLDER is
107
let holder = RENDER_SCOPE_HOLDER(self, _override_scope, _has_override)
108
109
_override_scope = scope
110
_has_override = true
111
112
return holder
113
si
114
115
restore_override(scope: Scope?, has_override: bool) is
116
_override_scope = scope
117
_has_override = has_override
118
si
119
si
120
121
struct RENDER_SCOPE_HOLDER: Disposable is
122
_display: NAME_DISPLAY?
123
_previous_scope: Scope?
124
_previous_has_override: bool
125
126
init(display: NAME_DISPLAY, previous_scope: Scope?, previous_has_override: bool) is
127
_display = display
128
_previous_scope = previous_scope
129
_previous_has_override = previous_has_override
130
si
131
132
dispose() is
133
// A value-type disposable must be safe on its default value:
134
// `let use` disposal null-checks reference types before calling
135
// dispose but not value types, so an early return past the
136
// `let use` hands dispose a zero-initialized holder. A null
137
// display means the holder was never constructed and so set no
138
// override to restore.
139
let display = _display
140
141
if display? then
142
display.restore_override(_previous_scope, _previous_has_override)
143
fi
144
si
145
si
146
147
struct QUALIFIED_SYMBOLS_HOLDER: Disposable is
148
_display: NAME_DISPLAY?
149
_previous: Collections.List[Symbol]?
150
151
init(display: NAME_DISPLAY, previous: Collections.List[Symbol]) is
152
_display = display
153
_previous = previous
154
si
155
156
dispose() is
157
let display = _display
158
let previous = _previous
159
160
if display? /\ previous? then
161
display.restore_qualified(previous)
162
fi
163
si
164
si
165
si