Bitcoin Core 29.99.0
P2P Digital Currency
gen.cpp
Go to the documentation of this file.
1// Copyright (c) 2019 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <mp/config.h>
6#include <mp/util.h>
7
8#include <algorithm>
9#include <capnp/schema-parser.h>
10#include <cstdint>
11#include <cstdio>
12#include <cstdlib>
13#include <errno.h>
14#include <fstream>
15#include <functional>
16#include <iostream>
17#include <kj/array.h>
18#include <kj/common.h>
19#include <kj/filesystem.h>
20#include <kj/memory.h>
21#include <kj/string.h>
22#include <map>
23#include <set>
24#include <sstream>
25#include <stdexcept>
26#include <string>
27#include <system_error>
28#include <unistd.h>
29#include <vector>
30
31#define PROXY_BIN "mpgen"
32#define PROXY_DECL "mp/proxy.h"
33#define PROXY_TYPES "mp/proxy-types.h"
34
35constexpr uint64_t NAMESPACE_ANNOTATION_ID = 0xb9c6f99ebf805f2cull; // From c++.capnp
36constexpr uint64_t INCLUDE_ANNOTATION_ID = 0xb899f3c154fdb458ull; // From proxy.capnp
37constexpr uint64_t INCLUDE_TYPES_ANNOTATION_ID = 0xbcec15648e8a0cf1ull; // From proxy.capnp
38constexpr uint64_t WRAP_ANNOTATION_ID = 0xe6f46079b7b1405eull; // From proxy.capnp
39constexpr uint64_t COUNT_ANNOTATION_ID = 0xd02682b319f69b38ull; // From proxy.capnp
40constexpr uint64_t EXCEPTION_ANNOTATION_ID = 0x996a183200992f88ull; // From proxy.capnp
41constexpr uint64_t NAME_ANNOTATION_ID = 0xb594888f63f4dbb9ull; // From proxy.capnp
42constexpr uint64_t SKIP_ANNOTATION_ID = 0x824c08b82695d8ddull; // From proxy.capnp
43
44template <typename Reader>
45static bool AnnotationExists(const Reader& reader, uint64_t id)
46{
47 for (const auto annotation : reader.getAnnotations()) {
48 if (annotation.getId() == id) {
49 return true;
50 }
51 }
52 return false;
53}
54
55template <typename Reader>
56static bool GetAnnotationText(const Reader& reader, uint64_t id, kj::StringPtr* result)
57{
58 for (const auto annotation : reader.getAnnotations()) {
59 if (annotation.getId() == id) {
60 *result = annotation.getValue().getText();
61 return true;
62 }
63 }
64 return false;
65}
66
67template <typename Reader>
68static bool GetAnnotationInt32(const Reader& reader, uint64_t id, int32_t* result)
69{
70 for (const auto annotation : reader.getAnnotations()) {
71 if (annotation.getId() == id) {
72 *result = annotation.getValue().getInt32();
73 return true;
74 }
75 }
76 return false;
77}
78
79static void ForEachMethod(const capnp::InterfaceSchema& interface, const std::function<void(const capnp::InterfaceSchema& interface, const capnp::InterfaceSchema::Method)>& callback)
80{
81 for (const auto super : interface.getSuperclasses()) {
82 ForEachMethod(super, callback);
83 }
84 for (const auto method : interface.getMethods()) {
85 callback(interface, method);
86 }
87}
88
89using CharSlice = kj::ArrayPtr<const char>;
90
91// Overload for any type with a string .begin(), like kj::StringPtr and kj::ArrayPtr<char>.
92template <class OutputStream, class Array, const char* Enable = decltype(std::declval<Array>().begin())()>
93static OutputStream& operator<<(OutputStream& os, const Array& array)
94{
95 os.write(array.begin(), array.size());
96 return os;
97}
98
99struct Format
100{
101 template <typename Value>
102 Format& operator<<(Value&& value)
103 {
104 m_os << value;
105 return *this;
106 }
107 operator std::string() const { return m_os.str(); }
108 std::ostringstream m_os;
109};
110
111static std::string Cap(kj::StringPtr str)
112{
113 std::string result = str;
114 if (!result.empty() && 'a' <= result[0] && result[0] <= 'z') result[0] -= 'a' - 'A';
115 return result;
116}
117
118static bool BoxedType(const ::capnp::Type& type)
119{
120 return !(type.isVoid() || type.isBool() || type.isInt8() || type.isInt16() || type.isInt32() || type.isInt64() ||
121 type.isUInt8() || type.isUInt16() || type.isUInt32() || type.isUInt64() || type.isFloat32() ||
122 type.isFloat64() || type.isEnum());
123}
124
125// src_file is path to .capnp file to generate stub code from.
126//
127// src_prefix can be used to generate outputs in a different directory than the
128// source directory. For example if src_file is "/a/b/c/d/file.canp", and
129// src_prefix is "/a/b", then output files will be "c/d/file.capnp.h"
130// "c/d/file.capnp.cxx" "c/d/file.capnp.proxy.h", etc. This is equivalent to
131// the capnp "--src-prefix" option (see "capnp help compile").
132//
133// include_prefix can be used to control relative include paths used in
134// generated files. For example if src_file is "/a/b/c/d/file.canp" and
135// include_prefix is "/a/b/c" include lines like
136// "#include <d/file.capnp.proxy.h>" "#include <d/file.capnp.proxy-types.h>"i
137// will be generated.
138static void Generate(kj::StringPtr src_prefix,
139 kj::StringPtr include_prefix,
140 kj::StringPtr src_file,
141 const std::vector<kj::StringPtr>& import_paths,
142 const kj::ReadableDirectory& src_dir,
143 const std::vector<kj::Own<const kj::ReadableDirectory>>& import_dirs)
144{
145 std::string output_path;
146 if (src_prefix == ".") {
147 output_path = src_file;
148 } else if (!src_file.startsWith(src_prefix) || src_file.size() <= src_prefix.size() ||
149 src_file[src_prefix.size()] != '/') {
150 throw std::runtime_error("src_prefix is not src_file prefix");
151 } else {
152 output_path = src_file.slice(src_prefix.size() + 1);
153 }
154
155 std::string include_path;
156 if (include_prefix == ".") {
157 include_path = src_file;
158 } else if (!src_file.startsWith(include_prefix) || src_file.size() <= include_prefix.size() ||
159 src_file[include_prefix.size()] != '/') {
160 throw std::runtime_error("include_prefix is not src_file prefix");
161 } else {
162 include_path = src_file.slice(include_prefix.size() + 1);
163 }
164
165 std::string include_base = include_path;
166 const std::string::size_type p = include_base.rfind('.');
167 if (p != std::string::npos) include_base.erase(p);
168
169 std::vector<std::string> args;
170 args.emplace_back(capnp_PREFIX "/bin/capnp");
171 args.emplace_back("compile");
172 args.emplace_back("--src-prefix=");
173 args.back().append(src_prefix.cStr(), src_prefix.size());
174 for (const auto& import_path : import_paths) {
175 args.emplace_back("--import-path=");
176 args.back().append(import_path.cStr(), import_path.size());
177 }
178 args.emplace_back("--output=" capnp_PREFIX "/bin/capnpc-c++");
179 args.emplace_back(src_file);
180 const int pid = fork();
181 if (pid == -1) {
182 throw std::system_error(errno, std::system_category(), "fork");
183 }
184 if (!pid) {
186 }
187 const int status = mp::WaitProcess(pid);
188 if (status) {
189 throw std::runtime_error("Invoking " capnp_PREFIX "/bin/capnp failed");
190 }
191
192 const capnp::SchemaParser parser;
193 auto directory_pointers = kj::heapArray<const kj::ReadableDirectory*>(import_dirs.size());
194 for (size_t i = 0; i < import_dirs.size(); ++i) {
195 directory_pointers[i] = import_dirs[i].get();
196 }
197 auto file_schema = parser.parseFromDirectory(src_dir, kj::Path::parse(output_path), directory_pointers);
198
199 std::ofstream cpp_server(output_path + ".proxy-server.c++");
200 cpp_server << "// Generated by " PROXY_BIN " from " << src_file << "\n\n";
201 cpp_server << "#include <" << include_path << ".proxy-types.h>\n";
202 cpp_server << "#include <" << PROXY_TYPES << ">\n\n";
203 cpp_server << "namespace mp {\n";
204
205 std::ofstream cpp_client(output_path + ".proxy-client.c++");
206 cpp_client << "// Generated by " PROXY_BIN " from " << src_file << "\n\n";
207 cpp_client << "#include <" << include_path << ".proxy-types.h>\n";
208 cpp_client << "#include <" << PROXY_TYPES << ">\n\n";
209 cpp_client << "namespace mp {\n";
210
211 std::ofstream cpp_types(output_path + ".proxy-types.c++");
212 cpp_types << "// Generated by " PROXY_BIN " from " << src_file << "\n\n";
213 cpp_types << "#include <" << include_path << ".proxy-types.h>\n";
214 cpp_types << "#include <" << PROXY_TYPES << ">\n\n";
215 cpp_types << "namespace mp {\n";
216
217 std::string guard = output_path;
218 std::transform(guard.begin(), guard.end(), guard.begin(), [](unsigned char c) -> unsigned char {
219 if ('0' <= c && c <= '9') return c;
220 if ('A' <= c && c <= 'Z') return c;
221 if ('a' <= c && c <= 'z') return c - 'a' + 'A';
222 return '_';
223 });
224
225 std::ofstream inl(output_path + ".proxy-types.h");
226 inl << "// Generated by " PROXY_BIN " from " << src_file << "\n\n";
227 inl << "#ifndef " << guard << "_PROXY_TYPES_H\n";
228 inl << "#define " << guard << "_PROXY_TYPES_H\n\n";
229 inl << "#include <" << include_path << ".proxy.h>\n";
230 for (const auto annotation : file_schema.getProto().getAnnotations()) {
231 if (annotation.getId() == INCLUDE_TYPES_ANNOTATION_ID) {
232 inl << "#include <" << annotation.getValue().getText() << ">\n";
233 }
234 }
235 inl << "namespace mp {\n";
236
237 std::ofstream h(output_path + ".proxy.h");
238 h << "// Generated by " PROXY_BIN " from " << src_file << "\n\n";
239 h << "#ifndef " << guard << "_PROXY_H\n";
240 h << "#define " << guard << "_PROXY_H\n\n";
241 h << "#include <" << include_path << ".h>\n";
242 for (const auto annotation : file_schema.getProto().getAnnotations()) {
243 if (annotation.getId() == INCLUDE_ANNOTATION_ID) {
244 h << "#include <" << annotation.getValue().getText() << ">\n";
245 }
246 }
247 h << "#include <" << PROXY_DECL << ">\n\n";
248 h << "#if defined(__GNUC__)\n";
249 h << "#pragma GCC diagnostic push\n";
250 h << "#if !defined(__has_warning)\n";
251 h << "#pragma GCC diagnostic ignored \"-Wsuggest-override\"\n";
252 h << "#elif __has_warning(\"-Wsuggest-override\")\n";
253 h << "#pragma GCC diagnostic ignored \"-Wsuggest-override\"\n";
254 h << "#endif\n";
255 h << "#endif\n";
256 h << "namespace mp {\n";
257
258 kj::StringPtr message_namespace;
259 GetAnnotationText(file_schema.getProto(), NAMESPACE_ANNOTATION_ID, &message_namespace);
260
261 std::string base_name = include_base;
262 const size_t output_slash = base_name.rfind('/');
263 if (output_slash != std::string::npos) {
264 base_name.erase(0, output_slash + 1);
265 }
266
267 std::ostringstream methods;
268 std::set<kj::StringPtr> accessors_done;
269 std::ostringstream accessors;
270 std::ostringstream dec;
271 std::ostringstream def_server;
272 std::ostringstream def_client;
273 std::ostringstream int_client;
274 std::ostringstream def_types;
275
276 auto add_accessor = [&](kj::StringPtr name) {
277 if (!accessors_done.insert(name).second) return;
278 const std::string cap = Cap(name);
279 accessors << "struct " << cap << "\n";
280 accessors << "{\n";
281 accessors << " template<typename S> static auto get(S&& s) -> decltype(s.get" << cap << "()) { return s.get" << cap << "(); }\n";
282 accessors << " template<typename S> static bool has(S&& s) { return s.has" << cap << "(); }\n";
283 accessors << " template<typename S, typename A> static void set(S&& s, A&& a) { s.set" << cap
284 << "(std::forward<A>(a)); }\n";
285 accessors << " template<typename S, typename... A> static decltype(auto) init(S&& s, A&&... a) { return s.init"
286 << cap << "(std::forward<A>(a)...); }\n";
287 accessors << " template<typename S> static bool getWant(S&& s) { return s.getWant" << cap << "(); }\n";
288 accessors << " template<typename S> static void setWant(S&& s) { s.setWant" << cap << "(true); }\n";
289 accessors << " template<typename S> static bool getHas(S&& s) { return s.getHas" << cap << "(); }\n";
290 accessors << " template<typename S> static void setHas(S&& s) { s.setHas" << cap << "(true); }\n";
291 accessors << "};\n";
292 };
293
294 for (const auto node_nested : file_schema.getProto().getNestedNodes()) {
295 kj::StringPtr node_name = node_nested.getName();
296 const auto& node = file_schema.getNested(node_name);
297 kj::StringPtr proxied_class_type;
298 GetAnnotationText(node.getProto(), WRAP_ANNOTATION_ID, &proxied_class_type);
299
300 if (node.getProto().isStruct()) {
301 const auto& struc = node.asStruct();
302 std::ostringstream generic_name;
303 generic_name << node_name;
304 dec << "template<";
305 bool first_param = true;
306 for (const auto param : node.getProto().getParameters()) {
307 if (first_param) {
308 first_param = false;
309 generic_name << "<";
310 } else {
311 dec << ", ";
312 generic_name << ", ";
313 }
314 dec << "typename " << param.getName();
315 generic_name << "" << param.getName();
316 }
317 if (!first_param) generic_name << ">";
318 dec << ">\n";
319 dec << "struct ProxyStruct<" << message_namespace << "::" << generic_name.str() << ">\n";
320 dec << "{\n";
321 dec << " using Struct = " << message_namespace << "::" << generic_name.str() << ";\n";
322 for (const auto field : struc.getFields()) {
323 auto field_name = field.getProto().getName();
324 add_accessor(field_name);
325 dec << " using " << Cap(field_name) << "Accessor = Accessor<" << base_name
326 << "_fields::" << Cap(field_name) << ", FIELD_IN | FIELD_OUT";
327 if (BoxedType(field.getType())) dec << " | FIELD_BOXED";
328 dec << ">;\n";
329 }
330 dec << " using Accessors = std::tuple<";
331 size_t i = 0;
332 for (const auto field : struc.getFields()) {
333 if (AnnotationExists(field.getProto(), SKIP_ANNOTATION_ID)) {
334 continue;
335 }
336 if (i) dec << ", ";
337 dec << Cap(field.getProto().getName()) << "Accessor";
338 ++i;
339 }
340 dec << ">;\n";
341 dec << " static constexpr size_t fields = " << i << ";\n";
342 dec << "};\n";
343
344 if (proxied_class_type.size()) {
345 inl << "template<>\n";
346 inl << "struct ProxyType<" << proxied_class_type << ">\n";
347 inl << "{\n";
348 inl << "public:\n";
349 inl << " using Struct = " << message_namespace << "::" << node_name << ";\n";
350 size_t i = 0;
351 for (const auto field : struc.getFields()) {
352 if (AnnotationExists(field.getProto(), SKIP_ANNOTATION_ID)) {
353 continue;
354 }
355 auto field_name = field.getProto().getName();
356 auto member_name = field_name;
357 GetAnnotationText(field.getProto(), NAME_ANNOTATION_ID, &member_name);
358 inl << " static decltype(auto) get(std::integral_constant<size_t, " << i << ">) { return "
359 << "&" << proxied_class_type << "::" << member_name << "; }\n";
360 ++i;
361 }
362 inl << " static constexpr size_t fields = " << i << ";\n";
363 inl << "};\n";
364 }
365 }
366
367 if (proxied_class_type.size() && node.getProto().isInterface()) {
368 const auto& interface = node.asInterface();
369
370 std::ostringstream client;
371 client << "template<>\nstruct ProxyClient<" << message_namespace << "::" << node_name << "> final : ";
372 client << "public ProxyClientCustom<" << message_namespace << "::" << node_name << ", "
373 << proxied_class_type << ">\n{\n";
374 client << "public:\n";
375 client << " using ProxyClientCustom::ProxyClientCustom;\n";
376 client << " ~ProxyClient();\n";
377
378 std::ostringstream server;
379 server << "template<>\nstruct ProxyServer<" << message_namespace << "::" << node_name << "> : public "
380 << "ProxyServerCustom<" << message_namespace << "::" << node_name << ", " << proxied_class_type
381 << ">\n{\n";
382 server << "public:\n";
383 server << " using ProxyServerCustom::ProxyServerCustom;\n";
384 server << " ~ProxyServer();\n";
385
386 const std::ostringstream client_construct;
387 const std::ostringstream client_destroy;
388
389 int method_ordinal = 0;
390 ForEachMethod(interface, [&] (const capnp::InterfaceSchema& method_interface, const capnp::InterfaceSchema::Method& method) {
391 const kj::StringPtr method_name = method.getProto().getName();
392 kj::StringPtr proxied_method_name = method_name;
393 GetAnnotationText(method.getProto(), NAME_ANNOTATION_ID, &proxied_method_name);
394
395 const std::string method_prefix = Format() << message_namespace << "::" << method_interface.getShortDisplayName()
396 << "::" << Cap(method_name);
397 const bool is_construct = method_name == "construct";
398 const bool is_destroy = method_name == "destroy";
399
400 struct Field
401 {
402 ::capnp::StructSchema::Field param;
403 bool param_is_set = false;
404 ::capnp::StructSchema::Field result;
405 bool result_is_set = false;
406 int args = 0;
407 bool retval = false;
408 bool optional = false;
409 bool requested = false;
410 bool skip = false;
411 kj::StringPtr exception;
412 };
413
414 std::vector<Field> fields;
415 std::map<kj::StringPtr, int> field_idx; // name -> args index
416 bool has_result = false;
417
418 auto add_field = [&](const ::capnp::StructSchema::Field& schema_field, bool param) {
419 if (AnnotationExists(schema_field.getProto(), SKIP_ANNOTATION_ID)) {
420 return;
421 }
422
423 auto field_name = schema_field.getProto().getName();
424 auto inserted = field_idx.emplace(field_name, fields.size());
425 if (inserted.second) {
426 fields.emplace_back();
427 }
428 auto& field = fields[inserted.first->second];
429 if (param) {
430 field.param = schema_field;
431 field.param_is_set = true;
432 } else {
433 field.result = schema_field;
434 field.result_is_set = true;
435 }
436
437 if (!param && field_name == "result") {
438 field.retval = true;
439 has_result = true;
440 }
441
442 GetAnnotationText(schema_field.getProto(), EXCEPTION_ANNOTATION_ID, &field.exception);
443
444 int32_t count = 1;
445 if (!GetAnnotationInt32(schema_field.getProto(), COUNT_ANNOTATION_ID, &count)) {
446 if (schema_field.getType().isStruct()) {
447 GetAnnotationInt32(schema_field.getType().asStruct().getProto(),
449 } else if (schema_field.getType().isInterface()) {
450 GetAnnotationInt32(schema_field.getType().asInterface().getProto(),
452 }
453 }
454
455
456 if (inserted.second && !field.retval && !field.exception.size()) {
457 field.args = count;
458 }
459 };
460
461 for (const auto schema_field : method.getParamType().getFields()) {
462 add_field(schema_field, true);
463 }
464 for (const auto schema_field : method.getResultType().getFields()) {
465 add_field(schema_field, false);
466 }
467 for (auto& field : field_idx) {
468 auto has_field = field_idx.find("has" + Cap(field.first));
469 if (has_field != field_idx.end()) {
470 fields[has_field->second].skip = true;
471 fields[field.second].optional = true;
472 }
473 auto want_field = field_idx.find("want" + Cap(field.first));
474 if (want_field != field_idx.end() && fields[want_field->second].param_is_set) {
475 fields[want_field->second].skip = true;
476 fields[field.second].requested = true;
477 }
478 }
479
480 if (!is_construct && !is_destroy && (&method_interface == &interface)) {
481 methods << "template<>\n";
482 methods << "struct ProxyMethod<" << method_prefix << "Params>\n";
483 methods << "{\n";
484 methods << " static constexpr auto impl = &" << proxied_class_type
485 << "::" << proxied_method_name << ";\n";
486 methods << "};\n\n";
487 }
488
489 std::ostringstream client_args;
490 std::ostringstream client_invoke;
491 std::ostringstream server_invoke_start;
492 std::ostringstream server_invoke_end;
493 int argc = 0;
494 for (const auto& field : fields) {
495 if (field.skip) continue;
496
497 const auto& f = field.param_is_set ? field.param : field.result;
498 auto field_name = f.getProto().getName();
499 auto field_type = f.getType();
500
501 std::ostringstream field_flags;
502 if (!field.param_is_set) {
503 field_flags << "FIELD_OUT";
504 } else if (field.result_is_set) {
505 field_flags << "FIELD_IN | FIELD_OUT";
506 } else {
507 field_flags << "FIELD_IN";
508 }
509 if (field.optional) field_flags << " | FIELD_OPTIONAL";
510 if (field.requested) field_flags << " | FIELD_REQUESTED";
511 if (BoxedType(field_type)) field_flags << " | FIELD_BOXED";
512
513 add_accessor(field_name);
514
515 for (int i = 0; i < field.args; ++i) {
516 if (argc > 0) client_args << ",";
517 client_args << "M" << method_ordinal << "::Param<" << argc << "> " << field_name;
518 if (field.args > 1) client_args << i;
519 ++argc;
520 }
521 client_invoke << ", ";
522
523 if (field.exception.size()) {
524 client_invoke << "ClientException<" << field.exception << ", ";
525 } else {
526 client_invoke << "MakeClientParam<";
527 }
528
529 client_invoke << "Accessor<" << base_name << "_fields::" << Cap(field_name) << ", "
530 << field_flags.str() << ">>(";
531
532 if (field.retval || field.args == 1) {
533 client_invoke << field_name;
534 } else {
535 for (int i = 0; i < field.args; ++i) {
536 if (i > 0) client_invoke << ", ";
537 client_invoke << field_name << i;
538 }
539 }
540 client_invoke << ")";
541
542 if (field.exception.size()) {
543 server_invoke_start << "Make<ServerExcept, " << field.exception;
544 } else if (field.retval) {
545 server_invoke_start << "Make<ServerRet";
546 } else {
547 server_invoke_start << "MakeServerField<" << field.args;
548 }
549 server_invoke_start << ", Accessor<" << base_name << "_fields::" << Cap(field_name) << ", "
550 << field_flags.str() << ">>(";
551 server_invoke_end << ")";
552 }
553
554 const std::string static_str{is_construct || is_destroy ? "static " : ""};
555 const std::string super_str{is_construct || is_destroy ? "Super& super" : ""};
556 const std::string self_str{is_construct || is_destroy ? "super" : "*this"};
557
558 client << " using M" << method_ordinal << " = ProxyClientMethodTraits<" << method_prefix
559 << "Params>;\n";
560 client << " " << static_str << "typename M" << method_ordinal << "::Result " << method_name << "("
561 << super_str << client_args.str() << ")";
562 client << ";\n";
563 def_client << "ProxyClient<" << message_namespace << "::" << node_name << ">::M" << method_ordinal
564 << "::Result ProxyClient<" << message_namespace << "::" << node_name << ">::" << method_name
565 << "(" << super_str << client_args.str() << ") {\n";
566 if (has_result) {
567 def_client << " typename M" << method_ordinal << "::Result result;\n";
568 }
569 def_client << " clientInvoke(" << self_str << ", &" << message_namespace << "::" << node_name
570 << "::Client::" << method_name << "Request" << client_invoke.str() << ");\n";
571 if (has_result) def_client << " return result;\n";
572 def_client << "}\n";
573
574 server << " kj::Promise<void> " << method_name << "(" << Cap(method_name)
575 << "Context call_context) override;\n";
576
577 def_server << "kj::Promise<void> ProxyServer<" << message_namespace << "::" << node_name
578 << ">::" << method_name << "(" << Cap(method_name)
579 << "Context call_context) {\n"
580 " return serverInvoke(*this, call_context, "
581 << server_invoke_start.str();
582 if (is_destroy) {
583 def_server << "ServerDestroy()";
584 } else {
585 def_server << "ServerCall()";
586 }
587 def_server << server_invoke_end.str() << ");\n}\n";
588 ++method_ordinal;
589 });
590
591 client << "};\n";
592 server << "};\n";
593 dec << "\n" << client.str() << "\n" << server.str() << "\n";
594 KJ_IF_MAYBE(bracket, proxied_class_type.findFirst('<')) {
595 // Skip ProxyType definition for complex type expressions which
596 // could lead to duplicate definitions. They can be defined
597 // manually if actually needed.
598 } else {
599 dec << "template<>\nstruct ProxyType<" << proxied_class_type << ">\n{\n";
600 dec << " using Type = " << proxied_class_type << ";\n";
601 dec << " using Message = " << message_namespace << "::" << node_name << ";\n";
602 dec << " using Client = ProxyClient<Message>;\n";
603 dec << " using Server = ProxyServer<Message>;\n";
604 dec << "};\n";
605 int_client << "ProxyTypeRegister t" << node_nested.getId() << "{TypeList<" << proxied_class_type << ">{}};\n";
606 }
607 def_types << "ProxyClient<" << message_namespace << "::" << node_name
608 << ">::~ProxyClient() { clientDestroy(*this); " << client_destroy.str() << " }\n";
609 def_types << "ProxyServer<" << message_namespace << "::" << node_name
610 << ">::~ProxyServer() { serverDestroy(*this); }\n";
611 }
612 }
613
614 h << methods.str() << "namespace " << base_name << "_fields {\n"
615 << accessors.str() << "} // namespace " << base_name << "_fields\n"
616 << dec.str();
617
618 cpp_server << def_server.str();
619 cpp_server << "} // namespace mp\n";
620
621 cpp_client << def_client.str();
622 cpp_client << "namespace {\n" << int_client.str() << "} // namespace\n";
623 cpp_client << "} // namespace mp\n";
624
625 cpp_types << def_types.str();
626 cpp_types << "} // namespace mp\n";
627
628 inl << "} // namespace mp\n";
629 inl << "#endif\n";
630
631 h << "} // namespace mp\n";
632 h << "#if defined(__GNUC__)\n";
633 h << "#pragma GCC diagnostic pop\n";
634 h << "#endif\n";
635 h << "#endif\n";
636}
637
638int main(int argc, char** argv)
639{
640 if (argc < 3) {
641 std::cerr << "Usage: " << PROXY_BIN << " SRC_PREFIX INCLUDE_PREFIX SRC_FILE [IMPORT_PATH...]\n";
642 exit(1);
643 }
644 std::vector<kj::StringPtr> import_paths;
645 std::vector<kj::Own<const kj::ReadableDirectory>> import_dirs;
646 auto fs = kj::newDiskFilesystem();
647 auto cwd = fs->getCurrentPath();
648 kj::Own<const kj::ReadableDirectory> src_dir;
649 KJ_IF_MAYBE(dir, fs->getRoot().tryOpenSubdir(cwd.evalNative(argv[1]))) {
650 src_dir = kj::mv(*dir);
651 } else {
652 throw std::runtime_error(std::string("Failed to open src_prefix prefix directory: ") + argv[1]);
653 }
654 for (int i = 4; i < argc; ++i) {
655 KJ_IF_MAYBE(dir, fs->getRoot().tryOpenSubdir(cwd.evalNative(argv[i]))) {
656 import_paths.emplace_back(argv[i]);
657 import_dirs.emplace_back(kj::mv(*dir));
658 } else {
659 throw std::runtime_error(std::string("Failed to open import directory: ") + argv[i]);
660 }
661 }
662 for (const char* path : {CMAKE_INSTALL_PREFIX "/include", capnp_PREFIX "/include"}) {
663 KJ_IF_MAYBE(dir, fs->getRoot().tryOpenSubdir(cwd.evalNative(path))) {
664 import_paths.emplace_back(path);
665 import_dirs.emplace_back(kj::mv(*dir));
666 }
667 // No exception thrown if _PREFIX directories do not exist
668 }
669 Generate(argv[1], argv[2], argv[3], import_paths, *src_dir, import_dirs);
670 return 0;
671}
ArgsManager & args
Definition: bitcoind.cpp:277
kj::ArrayPtr< const char > CharSlice
Definition: gen.cpp:89
constexpr uint64_t EXCEPTION_ANNOTATION_ID
Definition: gen.cpp:40
constexpr uint64_t NAME_ANNOTATION_ID
Definition: gen.cpp:41
static OutputStream & operator<<(OutputStream &os, const Array &array)
Definition: gen.cpp:93
static void ForEachMethod(const capnp::InterfaceSchema &interface, const std::function< void(const capnp::InterfaceSchema &interface, const capnp::InterfaceSchema::Method)> &callback)
Definition: gen.cpp:79
constexpr uint64_t SKIP_ANNOTATION_ID
Definition: gen.cpp:42
int main(int argc, char **argv)
Definition: gen.cpp:638
constexpr uint64_t WRAP_ANNOTATION_ID
Definition: gen.cpp:38
constexpr uint64_t INCLUDE_ANNOTATION_ID
Definition: gen.cpp:36
static void Generate(kj::StringPtr src_prefix, kj::StringPtr include_prefix, kj::StringPtr src_file, const std::vector< kj::StringPtr > &import_paths, const kj::ReadableDirectory &src_dir, const std::vector< kj::Own< const kj::ReadableDirectory > > &import_dirs)
Definition: gen.cpp:138
static bool GetAnnotationInt32(const Reader &reader, uint64_t id, int32_t *result)
Definition: gen.cpp:68
static bool BoxedType(const ::capnp::Type &type)
Definition: gen.cpp:118
#define PROXY_TYPES
Definition: gen.cpp:33
#define PROXY_BIN
Definition: gen.cpp:31
constexpr uint64_t INCLUDE_TYPES_ANNOTATION_ID
Definition: gen.cpp:37
constexpr uint64_t NAMESPACE_ANNOTATION_ID
Definition: gen.cpp:35
static bool AnnotationExists(const Reader &reader, uint64_t id)
Definition: gen.cpp:45
static std::string Cap(kj::StringPtr str)
Definition: gen.cpp:111
static bool GetAnnotationText(const Reader &reader, uint64_t id, kj::StringPtr *result)
Definition: gen.cpp:56
#define PROXY_DECL
Definition: gen.cpp:32
constexpr uint64_t COUNT_ANNOTATION_ID
Definition: gen.cpp:39
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
Definition: util.cpp:145
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
Definition: util.cpp:131
Definition: messages.h:20
const char * name
Definition: rest.cpp:49
Definition: gen.cpp:100
Format & operator<<(Value &&value)
Definition: gen.cpp:102
std::ostringstream m_os
Definition: gen.cpp:108
static int count