1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Function.h>
#include <AK/GenericLexer.h>
#include <AK/HashMap.h>
#include <AK/SourceGenerator.h>
#include <AK/StringBuilder.h>
#include <LibCore/File.h>
#include <ctype.h>
#include <stdio.h>
//#define GENERATE_DEBUG_CODE
struct Parameter {
Vector<String> attributes;
String type;
String name;
};
struct Message {
String name;
bool is_synchronous { false };
Vector<Parameter> inputs;
Vector<Parameter> outputs;
String response_name() const
{
StringBuilder builder;
builder.append(name);
builder.append("Response");
return builder.to_string();
}
};
struct Endpoint {
String name;
int magic;
Vector<Message> messages;
};
int main(int argc, char** argv)
{
if (argc != 2) {
outln("usage: {} <IPC endpoint definition file>", argv[0]);
return 0;
}
auto file = Core::File::construct(argv[1]);
if (!file->open(Core::IODevice::ReadOnly)) {
warnln("Error: Cannot open {}: {}", argv[1], file->error_string());
return 1;
}
auto file_contents = file->read_all();
GenericLexer lexer(file_contents);
Vector<Endpoint> endpoints;
auto assert_specific = [&](char ch) {
if (lexer.peek() != ch)
warnln("assert_specific: wanted '{}', but got '{}' at index {}", ch, lexer.peek(), lexer.tell());
bool saw_expected = lexer.consume_specific(ch);
ASSERT(saw_expected);
};
auto consume_whitespace = [&] {
lexer.ignore_while([](char ch) { return isspace(ch); });
if (lexer.peek() == '/' && lexer.peek(1) == '/')
lexer.ignore_until([](char ch) { return ch == '\n'; });
};
auto parse_parameter = [&](Vector<Parameter>& storage) {
for (;;) {
Parameter parameter;
consume_whitespace();
if (lexer.peek() == ')')
break;
if (lexer.consume_specific('[')) {
for (;;) {
if (lexer.consume_specific(']')) {
consume_whitespace();
break;
}
if (lexer.consume_specific(',')) {
consume_whitespace();
}
auto attribute = lexer.consume_until([](char ch) { return ch == ']' || ch == ','; });
parameter.attributes.append(attribute);
consume_whitespace();
}
}
parameter.type = lexer.consume_until([](char ch) { return isspace(ch); });
consume_whitespace();
parameter.name = lexer.consume_until([](char ch) { return isspace(ch) || ch == ',' || ch == ')'; });
consume_whitespace();
storage.append(move(parameter));
if (lexer.consume_specific(','))
continue;
if (lexer.peek() == ')')
break;
}
};
auto parse_parameters = [&](Vector<Parameter>& storage) {
for (;;) {
consume_whitespace();
parse_parameter(storage);
consume_whitespace();
if (lexer.consume_specific(','))
continue;
if (lexer.peek() == ')')
break;
}
};
auto parse_message = [&] {
Message message;
consume_whitespace();
message.name = lexer.consume_until([](char ch) { return isspace(ch) || ch == '('; });
consume_whitespace();
assert_specific('(');
parse_parameters(message.inputs);
assert_specific(')');
consume_whitespace();
assert_specific('=');
auto type = lexer.consume();
if (type == '>')
message.is_synchronous = true;
else if (type == '|')
message.is_synchronous = false;
else
ASSERT_NOT_REACHED();
consume_whitespace();
if (message.is_synchronous) {
assert_specific('(');
parse_parameters(message.outputs);
assert_specific(')');
}
consume_whitespace();
endpoints.last().messages.append(move(message));
};
auto parse_messages = [&] {
for (;;) {
consume_whitespace();
parse_message();
consume_whitespace();
if (lexer.peek() == '}')
break;
}
};
auto parse_endpoint = [&] {
endpoints.empend();
consume_whitespace();
lexer.consume_specific("endpoint");
consume_whitespace();
endpoints.last().name = lexer.consume_while([](char ch) { return !isspace(ch); });
consume_whitespace();
assert_specific('=');
consume_whitespace();
auto magic_string = lexer.consume_while([](char ch) { return !isspace(ch) && ch != '{'; });
endpoints.last().magic = magic_string.to_int().value();
consume_whitespace();
assert_specific('{');
parse_messages();
assert_specific('}');
consume_whitespace();
};
while (lexer.tell() < file_contents.size())
parse_endpoint();
SourceGenerator generator;
generator.append(R"~~~(
#pragma once
#include <AK/MemoryStream.h>
#include <AK/OwnPtr.h>
#include <AK/URL.h>
#include <AK/Utf8View.h>
#include <LibGfx/Color.h>
#include <LibGfx/Rect.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Dictionary.h>
#include <LibIPC/Encoder.h>
#include <LibIPC/Endpoint.h>
#include <LibIPC/Message.h>
)~~~");
for (auto& endpoint : endpoints) {
SourceGenerator endpoint_generator { generator };
endpoint_generator.set("endpoint.name", endpoint.name);
endpoint_generator.set("endpoint.magic", String::number(endpoint.magic));
endpoint_generator.append(R"~~~(
namespace Messages::@endpoint.name@ {
)~~~");
HashMap<String, int> message_ids;
endpoint_generator.append(R"~~~(
enum class MessageID : i32 {
)~~~");
for (auto& message : endpoint.messages) {
SourceGenerator message_generator { endpoint_generator };
message_ids.set(message.name, message_ids.size() + 1);
message_generator.set("message.name", message.name);
message_generator.set("message.id", String::number(message_ids.size()));
message_generator.append(R"~~~(
@message.name@ = @message.id@,
)~~~");
if (message.is_synchronous) {
message_ids.set(message.response_name(), message_ids.size() + 1);
message_generator.set("message.name", message.response_name());
message_generator.set("message.id", String::number(message_ids.size()));
message_generator.append(R"~~~(
@message.name@ = @message.id@,
)~~~");
}
}
endpoint_generator.append(R"~~~(
};
)~~~");
auto constructor_for_message = [&](const String& name, const Vector<Parameter>& parameters) {
StringBuilder builder;
builder.append(name);
if (parameters.is_empty()) {
builder.append("() {}");
return builder.to_string();
}
builder.append('(');
for (size_t i = 0; i < parameters.size(); ++i) {
auto& parameter = parameters[i];
builder.append("const ");
builder.append(parameter.type);
builder.append("& ");
builder.append(parameter.name);
if (i != parameters.size() - 1)
builder.append(", ");
}
builder.append(") : ");
for (size_t i = 0; i < parameters.size(); ++i) {
auto& parameter = parameters[i];
builder.append("m_");
builder.append(parameter.name);
builder.append("(");
builder.append(parameter.name);
builder.append(")");
if (i != parameters.size() - 1)
builder.append(", ");
}
builder.append(" {}");
return builder.to_string();
};
auto do_message = [&](const String& name, const Vector<Parameter>& parameters, const String& response_type = {}) {
SourceGenerator message_generator { endpoint_generator };
message_generator.set("message.name", name);
message_generator.set("message.response_type", response_type);
message_generator.set("message.constructor", constructor_for_message(name, parameters));
message_generator.append(R"~~~(
class @message.name@ final : public IPC::Message {
public:
)~~~");
if (!response_type.is_null())
message_generator.append(R"~~~(
typedef class @message.response_type@ ResponseType;
)~~~");
message_generator.append(R"~~~(
@message.constructor@
virtual ~@message.name@() override {}
virtual i32 endpoint_magic() const override { return @endpoint.magic@; }
virtual i32 message_id() const override { return (int)MessageID::@message.name@; }
static i32 static_message_id() { return (int)MessageID::@message.name@; }
virtual const char* message_name() const override { return "@endpoint.name@::@message.name@"; }
static OwnPtr<@message.name@> decode(InputMemoryStream& stream, size_t& size_in_bytes)
{
IPC::Decoder decoder {stream};
)~~~");
for (auto& parameter : parameters) {
SourceGenerator parameter_generator { message_generator };
parameter_generator.set("parameter.type", parameter.type);
parameter_generator.set("parameter.name", parameter.name);
if (parameter.type == "bool")
parameter_generator.set("parameter.initial_value", "false");
else
parameter_generator.set("parameter.initial_value", "{}");
parameter_generator.append(R"~~~(
@parameter.type@ @parameter.name@ = @parameter.initial_value@;
if (!decoder.decode(@parameter.name@))
return nullptr;
)~~~");
if (parameter.attributes.contains_slow("UTF8")) {
parameter_generator.append(R"~~~(
if (!Utf8View(@parameter.name@).validate())
return nullptr;
)~~~");
}
}
StringBuilder builder;
for (size_t i = 0; i < parameters.size(); ++i) {
auto& parameter = parameters[i];
builder.append(parameter.name);
if (i != parameters.size() - 1)
builder.append(", ");
}
message_generator.set("message.constructor_call_parameters", builder.build());
message_generator.append(R"~~~(
size_in_bytes = stream.offset();
return make<@message.name@>(@message.constructor_call_parameters@);
}
)~~~");
message_generator.append(R"~~~(
virtual IPC::MessageBuffer encode() const override
{
IPC::MessageBuffer buffer;
IPC::Encoder stream(buffer);
stream << endpoint_magic();
stream << (int)MessageID::@message.name@;
)~~~");
for (auto& parameter : parameters) {
SourceGenerator parameter_generator { message_generator };
parameter_generator.set("parameter.name", parameter.name);
parameter_generator.append(R"~~~(
stream << m_@parameter.name@;
)~~~");
}
message_generator.append(R"~~~(
return buffer;
}
)~~~");
for (auto& parameter : parameters) {
SourceGenerator parameter_generator { message_generator };
parameter_generator.set("parameter.type", parameter.type);
parameter_generator.set("parameter.name", parameter.name);
parameter_generator.append(R"~~~(
const @parameter.type@& @parameter.name@() const { return m_@parameter.name@; }
)~~~");
}
message_generator.append(R"~~~(
private:
)~~~");
for (auto& parameter : parameters) {
SourceGenerator parameter_generator { message_generator };
parameter_generator.set("parameter.type", parameter.type);
parameter_generator.set("parameter.name", parameter.name);
parameter_generator.append(R"~~~(
@parameter.type@ m_@parameter.name@;
)~~~");
}
message_generator.append(R"~~~(
};
)~~~");
};
for (auto& message : endpoint.messages) {
String response_name;
if (message.is_synchronous) {
response_name = message.response_name();
do_message(response_name, message.outputs);
}
do_message(message.name, message.inputs, response_name);
}
endpoint_generator.append(R"~~~(
} // namespace Messages::@endpoint.name@
)~~~");
endpoint_generator.append(R"~~~(
class @endpoint.name@Endpoint : public IPC::Endpoint {
public:
@endpoint.name@Endpoint() { }
virtual ~@endpoint.name@Endpoint() override { }
static int static_magic() { return @endpoint.magic@; }
virtual int magic() const override { return @endpoint.magic@; }
static String static_name() { return "@endpoint.name@"; }
virtual String name() const override { return "@endpoint.name@"; }
static OwnPtr<IPC::Message> decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)
{
InputMemoryStream stream { buffer };
i32 message_endpoint_magic = 0;
stream >> message_endpoint_magic;
if (stream.handle_any_error()) {
)~~~");
#ifdef GENERATE_DEBUG_CODE
endpoint_generator.append(R"~~~(
dbgln("Failed to read message endpoint magic");
)~~~");
#endif
endpoint_generator.append(R"~~~(
return nullptr;
}
if (message_endpoint_magic != @endpoint.magic@) {
)~~~");
#ifdef GENERATE_DEBUG_CODE
endpoint_generator.append(R"~~~(
dbgln("Endpoint magic number message_endpoint_magic != @endpoint.magic@");
)~~~");
#endif
endpoint_generator.append(R"~~~(
return nullptr;
}
i32 message_id = 0;
stream >> message_id;
if (stream.handle_any_error()) {
)~~~");
#ifdef GENERATE_DEBUG_CODE
endpoint_generator.append(R"~~~(
dbgln("Failed to read message ID");
)~~~");
#endif
endpoint_generator.append(R"~~~(
return nullptr;
}
OwnPtr<IPC::Message> message;
switch (message_id) {
)~~~");
for (auto& message : endpoint.messages) {
auto do_decode_message = [&](const String& name) {
SourceGenerator message_generator { endpoint_generator };
message_generator.set("message.name", name);
message_generator.append(R"~~~(
case (int)Messages::@endpoint.name@::MessageID::@message.name@:
message = Messages::@endpoint.name@::@message.name@::decode(stream, size_in_bytes);
break;
)~~~");
};
do_decode_message(message.name);
if (message.is_synchronous)
do_decode_message(message.response_name());
}
endpoint_generator.append(R"~~~(
default:
)~~~");
#ifdef GENERATE_DEBUG_CODE
endpoint_generator.append(R"~~~(
dbgln("Failed to decode @endpoint.name@.({})", message_id);
)~~~");
#endif
endpoint_generator.append(R"~~~(
return nullptr;
}
if (stream.handle_any_error()) {
)~~~");
#ifdef GENERATE_DEBUG_CODE
endpoint_generator.append(R"~~~(
dbgln("Failed to read the message");
)~~~");
#endif
endpoint_generator.append(R"~~~(
return nullptr;
}
return message;
}
virtual OwnPtr<IPC::Message> handle(const IPC::Message& message) override
{
switch (message.message_id()) {
)~~~");
for (auto& message : endpoint.messages) {
auto do_decode_message = [&](const String& name, bool returns_something) {
SourceGenerator message_generator { endpoint_generator };
message_generator.set("message.name", name);
message_generator.append(R"~~~(
case (int)Messages::@endpoint.name@::MessageID::@message.name@:
)~~~");
if (returns_something) {
message_generator.append(R"~~~(
return handle(static_cast<const Messages::@endpoint.name@::@message.name@&>(message));
)~~~");
} else {
message_generator.append(R"~~~(
handle(static_cast<const Messages::@endpoint.name@::@message.name@&>(message));
return nullptr;
)~~~");
}
};
do_decode_message(message.name, message.is_synchronous);
if (message.is_synchronous)
do_decode_message(message.response_name(), false);
}
endpoint_generator.append(R"~~~(
default:
return nullptr;
}
}
)~~~");
for (auto& message : endpoint.messages) {
SourceGenerator message_generator { endpoint_generator };
message_generator.set("message.name", message.name);
String return_type = "void";
if (message.is_synchronous) {
StringBuilder builder;
builder.append("OwnPtr<Messages::");
builder.append(endpoint.name);
builder.append("::");
builder.append(message.name);
builder.append("Response");
builder.append(">");
return_type = builder.to_string();
}
message_generator.set("message.complex_return_type", return_type);
message_generator.append(R"~~~(
virtual @message.complex_return_type@ handle(const Messages::@endpoint.name@::@message.name@&) = 0;
)~~~");
}
endpoint_generator.append(R"~~~(
private:
};
)~~~");
}
outln("{}", generator.generate());
#ifdef DEBUG
for (auto& endpoint : endpoints) {
warnln("Endpoint '{}' (magic: {})", endpoint.name, endpoint.magic);
for (auto& message : endpoint.messages) {
warnln(" Message: '{}'", message.name);
warnln(" Sync: {}", message.is_synchronous);
warnln(" Inputs:");
for (auto& parameter : message.inputs)
warnln(" Parameter: {} ({})", parameter.name, parameter.type);
if (message.inputs.is_empty())
warnln(" (none)");
if (message.is_synchronous) {
warnln(" Outputs:");
for (auto& parameter : message.outputs)
warnln(" Parameter: {} ({})", parameter.name, parameter.type);
if (message.outputs.is_empty())
warnln(" (none)");
}
}
}
#endif
return 0;
}
|