Skip to content
← Back

src/driver/compile_server.ghul

1
@@IF.dotnet()
2
3
namespace Driver is
4
use System.Exception
5
6
use Collections.LIST
7
8
use IO.File
9
use IO.Path
10
11
use Analysis.Protocol.COMPILE_REPLY
12
use Analysis.Protocol.COMPILE_REQUEST
13
use Analysis.Protocol.DIAGNOSTIC
14
15
// Compiles submissions one after another in one process, so each pays
16
// for its own few lines rather than for starting a runtime and importing
17
// the reference set. Requests and replies are one line of JSON each; the
18
// first line out says the server is ready.
19
//
20
// Every setting a compile depends on is set again for each request, and
21
// everything the previous compile left - its diagnostics, its symbols,
22
// its queued files, its emitter - is cleared first, so that a request is
23
// answered exactly as a batch compile of the same file would be. What
24
// does carry over is the reference set, which only grows: each request's
25
// references are imported into it, and that is the point of keeping the
26
// process.
27
class COMPILE_SERVER(
28
_compiler: Compiler.COMPILER,
29
_container: IoC.CONTAINER,
30
_module_version: string,
31
_input: IO.TextReader,
32
_output: IO.TextWriter
33
) is
34
serve() is
35
_write(COMPILE_REPLY.ready_reply())
36
37
do
38
let line = _input.read_line()
39
40
if !line? then
41
return
42
fi
43
44
_write(answer(line))
45
od
46
si
47
48
// The reply to one request line. Nothing that goes wrong inside a
49
// compile escapes: it becomes a reply with status 2, and the server
50
// carries on with the next line.
51
answer(line: string) -> COMPILE_REPLY is
52
let request: COMPILE_REQUEST? mut = null
53
54
try
55
request = cast COMPILE_REQUEST?(Analysis.Protocol.REFLECTION_JSON.read(line, typeof COMPILE_REQUEST))
56
catch e: Exception
57
return COMPILE_REPLY.failure("malformed request: {e.message}")
58
yrt
59
60
if !request? then
61
return COMPILE_REPLY.failure("malformed request: expected an object")
62
fi
63
64
if request.check then
65
return check(request)
66
fi
67
68
let problem = COMPILE_REQUEST_CHECK.problem_with(request)
69
70
if problem? then
71
return COMPILE_REPLY.failure(problem)
72
fi
73
74
try
75
return compile(request)
76
catch e: Exception
77
return COMPILE_REPLY.failure("{e.get_type().name}: {e.message}")
78
yrt
79
si
80
81
// Whether a request's source is complete, parsed with a logger and a
82
// parser state of its own: nothing a compile reads is touched, so a
83
// check between two compiles changes neither.
84
check(request: COMPILE_REQUEST) -> COMPILE_REPLY is
85
if !request.text? /\ !request.source? then
86
return COMPILE_REPLY.failure("check request has neither source nor text")
87
fi
88
89
let path = COMPILE_REQUEST_CHECK.source_path(request)
90
91
try
92
let reader: IO.TextReader =
93
if let text = request.text then
94
IO.StringReader(text)
95
else
96
File.open_text(path)
97
fi
98
99
let use disposing_reader = reader
100
101
let checked = _compiler.check(path, reader)
102
103
return COMPILE_REPLY.checked(
104
case checked.completeness
105
when Syntax.Parsers.COMPLETENESS.COMPLETE then "complete"
106
when Syntax.Parsers.COMPLETENESS.INCOMPLETE then "incomplete"
107
else "invalid"
108
esac,
109
checked.last_statement)
110
catch e: Exception
111
return COMPILE_REPLY.failure("{e.get_type().name}: {e.message}")
112
yrt
113
si
114
115
compile(request: COMPILE_REQUEST) -> COMPILE_REPLY is
116
_reset(request)
117
118
let path = COMPILE_REQUEST_CHECK.source_path(request)
119
120
let reader: IO.TextReader =
121
if let text = request.text then
122
IO.StringReader(text)
123
else
124
File.open_text(path)
125
fi
126
127
let use disposing_reader = reader
128
129
_compiler.parse_and_queue(
130
path,
131
reader,
132
_container.build_flags.want_compile_up_to_expressions,
133
_container.build_flags.want_compile_expressions,
134
false)
135
136
_compiler.post_parse()
137
_compiler.build()
138
139
let diagnostics = LIST[DIAGNOSTIC]()
140
141
Analysis.DIAGNOSTICS_COLLECTOR.collect_into(_container.logger, diagnostics, LIST[string]())
142
143
let logger = _container.logger
144
145
if logger.any_errors then
146
return COMPILE_REPLY(false, 1, null, diagnostics)
147
elif logger.is_poisoned then
148
return COMPILE_REPLY(false, 2, "internal error", diagnostics)
149
fi
150
151
_container.backend.write(request.output)
152
153
return COMPILE_REPLY(false, 0, null, diagnostics)
154
si
155
156
_reset(request: COMPILE_REQUEST) is
157
let flags = _container.build_flags
158
159
flags.submission_name = request.name
160
flags.want_library = true
161
flags.want_executable = false
162
163
_container.value_boxer.want_boxing = true
164
165
_clear_diagnostics()
166
167
_container.state_store_registry.clear_all()
168
169
_compiler.clear_queue()
170
_compiler.clear_symbols()
171
172
Semantic.DotNet.ADDITIONAL_REFERENCES.add(_container, request.references ?? Collections.LIST[string]())
173
174
_container.backend.install(
175
Path.get_file_name_without_extension(request.output) ?? "module",
176
_module_version,
177
true)
178
si
179
180
_clear_diagnostics() is
181
let logger = _container.logger
182
183
for path in LIST[string](logger.diagnostic_paths) do
184
logger.clear(path, false)
185
od
186
187
logger.clear_suppression_regions()
188
logger.clear_consumed_error()
189
logger.clear_consumed_any()
190
si
191
192
_write(reply: COMPILE_REPLY) is
193
_output.write(Analysis.Protocol.REFLECTION_JSON.write(reply, typeof COMPILE_REPLY))
194
_output.write("\n")
195
_output.flush()
196
si
197
si
198
199
// What a compile request needs before the server can act on it, and
200
// the file name it compiles under.
201
class COMPILE_REQUEST_CHECK is
202
problem_with(request: COMPILE_REQUEST) -> string? static is
203
if string.is_null_or_empty(request.name) then
204
return "request has no name"
205
fi
206
207
if string.is_null_or_empty(request.output) then
208
return "request has no output"
209
fi
210
211
if !request.source? /\ !request.text? then
212
return "request has neither source nor text"
213
fi
214
215
if let source = request.source /\ !request.text? /\ !File.exists(source) then
216
return "source file not found: {source}"
217
fi
218
219
if let references = request.references then
220
for reference in references do
221
if !File.exists(reference) then
222
return "reference not found: {reference}"
223
fi
224
od
225
fi
226
227
return null
228
si
229
230
// Source carried inline is named after the submission, so its
231
// diagnostics name a file a reader can tell apart from any other.
232
source_path(request: COMPILE_REQUEST) -> string static =>
233
if request.text? then "{request.name}.ghul" else request.source ?? "{request.name}.ghul" fi
234
si
235
si