main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/GenericLexer.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibCore/File.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. //#define GENERATE_DEBUG_CODE
  34. struct Parameter {
  35. Vector<String> attributes;
  36. String type;
  37. String name;
  38. };
  39. struct Message {
  40. String name;
  41. bool is_synchronous { false };
  42. Vector<Parameter> inputs;
  43. Vector<Parameter> outputs;
  44. String response_name() const
  45. {
  46. StringBuilder builder;
  47. builder.append(name);
  48. builder.append("Response");
  49. return builder.to_string();
  50. }
  51. };
  52. struct Endpoint {
  53. String name;
  54. int magic;
  55. Vector<Message> messages;
  56. };
  57. int main(int argc, char** argv)
  58. {
  59. if (argc != 2) {
  60. printf("usage: %s <IPC endpoint definition file>\n", argv[0]);
  61. return 0;
  62. }
  63. auto file = Core::File::construct(argv[1]);
  64. if (!file->open(Core::IODevice::ReadOnly)) {
  65. fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file->error_string());
  66. return 1;
  67. }
  68. auto file_contents = file->read_all();
  69. GenericLexer lexer(file_contents);
  70. Vector<Endpoint> endpoints;
  71. auto assert_specific = [&](char ch) {
  72. if (lexer.peek() != ch)
  73. warn() << "assert_specific: wanted '" << ch << "', but got '" << lexer.peek() << "' at index " << lexer.tell();
  74. bool saw_expected = lexer.consume_specific(ch);
  75. ASSERT(saw_expected);
  76. };
  77. auto consume_whitespace = [&] {
  78. lexer.ignore_while([](char ch) { return isspace(ch); });
  79. if (lexer.peek() == '/' && lexer.peek(1) == '/')
  80. lexer.ignore_until([](char ch) { return ch == '\n'; });
  81. };
  82. auto parse_parameter = [&](Vector<Parameter>& storage) {
  83. for (;;) {
  84. Parameter parameter;
  85. consume_whitespace();
  86. if (lexer.peek() == ')')
  87. break;
  88. if (lexer.consume_specific('[')) {
  89. for (;;) {
  90. if (lexer.consume_specific(']')) {
  91. consume_whitespace();
  92. break;
  93. }
  94. if (lexer.consume_specific(',')) {
  95. consume_whitespace();
  96. }
  97. auto attribute = lexer.consume_until([](char ch) { return ch == ']' || ch == ','; });
  98. parameter.attributes.append(attribute);
  99. consume_whitespace();
  100. }
  101. }
  102. parameter.type = lexer.consume_until([](char ch) { return isspace(ch); });
  103. consume_whitespace();
  104. parameter.name = lexer.consume_until([](char ch) { return isspace(ch) || ch == ',' || ch == ')'; });
  105. consume_whitespace();
  106. storage.append(move(parameter));
  107. if (lexer.consume_specific(','))
  108. continue;
  109. if (lexer.peek() == ')')
  110. break;
  111. }
  112. };
  113. auto parse_parameters = [&](Vector<Parameter>& storage) {
  114. for (;;) {
  115. consume_whitespace();
  116. parse_parameter(storage);
  117. consume_whitespace();
  118. if (lexer.consume_specific(','))
  119. continue;
  120. if (lexer.peek() == ')')
  121. break;
  122. }
  123. };
  124. auto parse_message = [&] {
  125. Message message;
  126. consume_whitespace();
  127. message.name = lexer.consume_until([](char ch) { return isspace(ch) || ch == '('; });
  128. consume_whitespace();
  129. assert_specific('(');
  130. parse_parameters(message.inputs);
  131. assert_specific(')');
  132. consume_whitespace();
  133. assert_specific('=');
  134. auto type = lexer.consume();
  135. if (type == '>')
  136. message.is_synchronous = true;
  137. else if (type == '|')
  138. message.is_synchronous = false;
  139. else
  140. ASSERT_NOT_REACHED();
  141. consume_whitespace();
  142. if (message.is_synchronous) {
  143. assert_specific('(');
  144. parse_parameters(message.outputs);
  145. assert_specific(')');
  146. }
  147. consume_whitespace();
  148. endpoints.last().messages.append(move(message));
  149. };
  150. auto parse_messages = [&] {
  151. for (;;) {
  152. consume_whitespace();
  153. parse_message();
  154. consume_whitespace();
  155. if (lexer.peek() == '}')
  156. break;
  157. }
  158. };
  159. auto parse_endpoint = [&] {
  160. endpoints.empend();
  161. consume_whitespace();
  162. lexer.consume_specific("endpoint");
  163. consume_whitespace();
  164. endpoints.last().name = lexer.consume_while([](char ch) { return !isspace(ch); });
  165. consume_whitespace();
  166. assert_specific('=');
  167. consume_whitespace();
  168. auto magic_string = lexer.consume_while([](char ch) { return !isspace(ch) && ch != '{'; });
  169. endpoints.last().magic = magic_string.to_int().value();
  170. consume_whitespace();
  171. assert_specific('{');
  172. parse_messages();
  173. assert_specific('}');
  174. consume_whitespace();
  175. };
  176. while (lexer.tell() < file_contents.size())
  177. parse_endpoint();
  178. out() << "#pragma once";
  179. out() << "#include <AK/MemoryStream.h>";
  180. out() << "#include <AK/OwnPtr.h>";
  181. out() << "#include <AK/URL.h>";
  182. out() << "#include <AK/Utf8View.h>";
  183. out() << "#include <LibGfx/Color.h>";
  184. out() << "#include <LibGfx/Rect.h>";
  185. out() << "#include <LibGfx/ShareableBitmap.h>";
  186. out() << "#include <LibIPC/Decoder.h>";
  187. out() << "#include <LibIPC/Dictionary.h>";
  188. out() << "#include <LibIPC/Encoder.h>";
  189. out() << "#include <LibIPC/Endpoint.h>";
  190. out() << "#include <LibIPC/Message.h>";
  191. out();
  192. for (auto& endpoint : endpoints) {
  193. out() << "namespace Messages {";
  194. out() << "namespace " << endpoint.name << " {";
  195. out();
  196. HashMap<String, int> message_ids;
  197. out() << "enum class MessageID : i32 {";
  198. for (auto& message : endpoint.messages) {
  199. message_ids.set(message.name, message_ids.size() + 1);
  200. out() << " " << message.name << " = " << message_ids.size() << ",";
  201. if (message.is_synchronous) {
  202. message_ids.set(message.response_name(), message_ids.size() + 1);
  203. out() << " " << message.response_name() << " = " << message_ids.size() << ",";
  204. }
  205. }
  206. out() << "};";
  207. out();
  208. auto constructor_for_message = [&](const String& name, const Vector<Parameter>& parameters) {
  209. StringBuilder builder;
  210. builder.append(name);
  211. if (parameters.is_empty()) {
  212. builder.append("() {}");
  213. return builder.to_string();
  214. }
  215. builder.append('(');
  216. for (size_t i = 0; i < parameters.size(); ++i) {
  217. auto& parameter = parameters[i];
  218. builder.append("const ");
  219. builder.append(parameter.type);
  220. builder.append("& ");
  221. builder.append(parameter.name);
  222. if (i != parameters.size() - 1)
  223. builder.append(", ");
  224. }
  225. builder.append(") : ");
  226. for (size_t i = 0; i < parameters.size(); ++i) {
  227. auto& parameter = parameters[i];
  228. builder.append("m_");
  229. builder.append(parameter.name);
  230. builder.append("(");
  231. builder.append(parameter.name);
  232. builder.append(")");
  233. if (i != parameters.size() - 1)
  234. builder.append(", ");
  235. }
  236. builder.append(" {}");
  237. return builder.to_string();
  238. };
  239. auto do_message = [&](const String& name, const Vector<Parameter>& parameters, const String& response_type = {}) {
  240. out() << "class " << name << " final : public IPC::Message {";
  241. out() << "public:";
  242. if (!response_type.is_null())
  243. out() << " typedef class " << response_type << " ResponseType;";
  244. out() << " " << constructor_for_message(name, parameters);
  245. out() << " virtual ~" << name << "() override {}";
  246. out() << " virtual i32 endpoint_magic() const override { return " << endpoint.magic << "; }";
  247. out() << " virtual i32 message_id() const override { return (int)MessageID::" << name << "; }";
  248. out() << " static i32 static_message_id() { return (int)MessageID::" << name << "; }";
  249. out() << " virtual const char* message_name() const override { return \"" << endpoint.name << "::" << name << "\"; }";
  250. out() << " static OwnPtr<" << name << "> decode(InputMemoryStream& stream, size_t& size_in_bytes)";
  251. out() << " {";
  252. out() << " IPC::Decoder decoder(stream);";
  253. for (auto& parameter : parameters) {
  254. String initial_value = "{}";
  255. if (parameter.type == "bool")
  256. initial_value = "false";
  257. out() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
  258. out() << " if (!decoder.decode(" << parameter.name << "))";
  259. out() << " return nullptr;";
  260. if (parameter.attributes.contains_slow("UTF8")) {
  261. out() << " if (!Utf8View(" << parameter.name << ").validate())";
  262. out() << " return nullptr;";
  263. }
  264. }
  265. StringBuilder builder;
  266. for (size_t i = 0; i < parameters.size(); ++i) {
  267. auto& parameter = parameters[i];
  268. builder.append(parameter.name);
  269. if (i != parameters.size() - 1)
  270. builder.append(", ");
  271. }
  272. out() << " size_in_bytes = stream.offset();";
  273. out() << " return make<" << name << ">(" << builder.to_string() << ");";
  274. out() << " }";
  275. out() << " virtual IPC::MessageBuffer encode() const override";
  276. out() << " {";
  277. out() << " IPC::MessageBuffer buffer;";
  278. out() << " IPC::Encoder stream(buffer);";
  279. out() << " stream << endpoint_magic();";
  280. out() << " stream << (int)MessageID::" << name << ";";
  281. for (auto& parameter : parameters) {
  282. out() << " stream << m_" << parameter.name << ";";
  283. }
  284. out() << " return buffer;";
  285. out() << " }";
  286. for (auto& parameter : parameters) {
  287. out() << " const " << parameter.type << "& " << parameter.name << "() const { return m_" << parameter.name << "; }";
  288. }
  289. out() << "private:";
  290. for (auto& parameter : parameters) {
  291. out() << " " << parameter.type << " m_" << parameter.name << ";";
  292. }
  293. out() << "};";
  294. out();
  295. };
  296. for (auto& message : endpoint.messages) {
  297. String response_name;
  298. if (message.is_synchronous) {
  299. response_name = message.response_name();
  300. do_message(response_name, message.outputs);
  301. }
  302. do_message(message.name, message.inputs, response_name);
  303. }
  304. out() << "} // namespace " << endpoint.name;
  305. out() << "} // namespace Messages";
  306. out();
  307. out() << "class " << endpoint.name << "Endpoint : public IPC::Endpoint {";
  308. out() << "public:";
  309. out() << " " << endpoint.name << "Endpoint() {}";
  310. out() << " virtual ~" << endpoint.name << "Endpoint() override {}";
  311. out() << " static int static_magic() { return " << endpoint.magic << "; }";
  312. out() << " virtual int magic() const override { return " << endpoint.magic << "; }";
  313. out() << " static String static_name() { return \"" << endpoint.name << "\"; };";
  314. out() << " virtual String name() const override { return \"" << endpoint.name << "\"; };";
  315. out() << " static OwnPtr<IPC::Message> decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)";
  316. out() << " {";
  317. out() << " InputMemoryStream stream { buffer };";
  318. out() << " i32 message_endpoint_magic = 0;";
  319. out() << " stream >> message_endpoint_magic;";
  320. out() << " if (stream.handle_any_error()) {";
  321. #ifdef GENERATE_DEBUG_CODE
  322. out() << " dbg() << \"Failed to read message endpoint magic\";";
  323. #endif
  324. out() << " return nullptr;";
  325. out() << " }";
  326. out() << " if (message_endpoint_magic != " << endpoint.magic << ") {";
  327. #ifdef GENERATE_DEBUG_CODE
  328. out() << " dbg() << \"endpoint magic \" << message_endpoint_magic << \" != " << endpoint.magic << "\";";
  329. #endif
  330. out() << " return nullptr;";
  331. out() << " }";
  332. out() << " i32 message_id = 0;";
  333. out() << " stream >> message_id;";
  334. out() << " if (stream.handle_any_error()) {";
  335. #ifdef GENERATE_DEBUG_CODE
  336. out() << " dbg() << \"Failed to read message ID\";";
  337. #endif
  338. out() << " return nullptr;";
  339. out() << " }";
  340. out() << " OwnPtr<IPC::Message> message;";
  341. out() << " switch (message_id) {";
  342. for (auto& message : endpoint.messages) {
  343. auto do_decode_message = [&](const String& name) {
  344. out() << " case (int)Messages::" << endpoint.name << "::MessageID::" << name << ":";
  345. out() << " message = Messages::" << endpoint.name << "::" << name << "::decode(stream, size_in_bytes);";
  346. out() << " break;";
  347. };
  348. do_decode_message(message.name);
  349. if (message.is_synchronous)
  350. do_decode_message(message.response_name());
  351. }
  352. out() << " default:";
  353. #ifdef GENERATE_DEBUG_CODE
  354. out() << " dbg() << \"Failed to decode " << endpoint.name << ".(\" << message_id << \")\";";
  355. #endif
  356. out() << " return nullptr;";
  357. out() << " }";
  358. out() << " if (stream.handle_any_error()) {";
  359. #ifdef GENERATE_DEBUG_CODE
  360. out() << " dbg() << \"Failed to read the message\";";
  361. #endif
  362. out() << " return nullptr;";
  363. out() << " }";
  364. out() << " return message;";
  365. out() << " }";
  366. out();
  367. out() << " virtual OwnPtr<IPC::Message> handle(const IPC::Message& message) override";
  368. out() << " {";
  369. out() << " switch (message.message_id()) {";
  370. for (auto& message : endpoint.messages) {
  371. auto do_decode_message = [&](const String& name, bool returns_something) {
  372. out() << " case (int)Messages::" << endpoint.name << "::MessageID::" << name << ":";
  373. if (returns_something) {
  374. out() << " return handle(static_cast<const Messages::" << endpoint.name << "::" << name << "&>(message));";
  375. } else {
  376. out() << " handle(static_cast<const Messages::" << endpoint.name << "::" << name << "&>(message));";
  377. out() << " return nullptr;";
  378. }
  379. };
  380. do_decode_message(message.name, message.is_synchronous);
  381. if (message.is_synchronous)
  382. do_decode_message(message.response_name(), false);
  383. }
  384. out() << " default:";
  385. out() << " return nullptr;";
  386. out() << " }";
  387. out() << " }";
  388. for (auto& message : endpoint.messages) {
  389. String return_type = "void";
  390. if (message.is_synchronous) {
  391. StringBuilder builder;
  392. builder.append("OwnPtr<Messages::");
  393. builder.append(endpoint.name);
  394. builder.append("::");
  395. builder.append(message.name);
  396. builder.append("Response");
  397. builder.append(">");
  398. return_type = builder.to_string();
  399. }
  400. out() << " virtual " << return_type << " handle(const Messages::" << endpoint.name << "::" << message.name << "&) = 0;";
  401. }
  402. out() << "private:";
  403. out() << "};";
  404. }
  405. #ifdef DEBUG
  406. for (auto& endpoint : endpoints) {
  407. warn() << "Endpoint: '" << endpoint.name << "' (magic: " << endpoint.magic << ")";
  408. for (auto& message : endpoint.messages) {
  409. warn() << " Message: '" << message.name << "'";
  410. warn() << " Sync: " << message.is_synchronous;
  411. warn() << " Inputs:";
  412. for (auto& parameter : message.inputs)
  413. warn() << " Parameter: " << parameter.name << " (" << parameter.type << ")";
  414. if (message.inputs.is_empty())
  415. warn() << " (none)";
  416. if (message.is_synchronous) {
  417. warn() << " Outputs:";
  418. for (auto& parameter : message.outputs)
  419. warn() << " Parameter: " << parameter.name << " (" << parameter.type << ")";
  420. if (message.outputs.is_empty())
  421. warn() << " (none)";
  422. }
  423. }
  424. }
  425. #endif
  426. return 0;
  427. }