main.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/BufferStream.h>
  27. #include <AK/Function.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. String type;
  36. String name;
  37. };
  38. struct Message {
  39. String name;
  40. bool is_synchronous { false };
  41. Vector<Parameter> inputs;
  42. Vector<Parameter> outputs;
  43. String response_name() const
  44. {
  45. StringBuilder builder;
  46. builder.append(name);
  47. builder.append("Response");
  48. return builder.to_string();
  49. }
  50. };
  51. struct Endpoint {
  52. String name;
  53. int magic;
  54. Vector<Message> messages;
  55. };
  56. int main(int argc, char** argv)
  57. {
  58. if (argc != 2) {
  59. printf("usage: %s <IPC endpoint definition file>\n", argv[0]);
  60. return 0;
  61. }
  62. auto file = Core::File::construct(argv[1]);
  63. if (!file->open(Core::IODevice::ReadOnly)) {
  64. fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file->error_string());
  65. return 1;
  66. }
  67. auto file_contents = file->read_all();
  68. Vector<Endpoint> endpoints;
  69. Vector<char> buffer;
  70. size_t index = 0;
  71. auto peek = [&](size_t offset = 0) -> char {
  72. if ((index + offset) < file_contents.size())
  73. return file_contents[index + offset];
  74. return 0;
  75. };
  76. auto consume_one = [&]() -> char {
  77. return file_contents[index++];
  78. };
  79. auto extract_while = [&](Function<bool(char)> condition) -> String {
  80. StringBuilder builder;
  81. while (condition(peek()))
  82. builder.append(consume_one());
  83. return builder.to_string();
  84. };
  85. auto consume_specific = [&](char ch) {
  86. if (peek() != ch) {
  87. dbg() << "consume_specific: wanted '" << ch << "', but got '" << peek() << "' at index " << index;
  88. }
  89. ASSERT(peek() == ch);
  90. ++index;
  91. return ch;
  92. };
  93. auto consume_string = [&](const char* str) {
  94. for (size_t i = 0, length = strlen(str); i < length; ++i)
  95. consume_specific(str[i]);
  96. };
  97. auto consume_whitespace = [&] {
  98. while (isspace(peek()))
  99. ++index;
  100. if (peek() == '/' && peek(1) == '/') {
  101. while (peek() != '\n')
  102. ++index;
  103. }
  104. };
  105. auto parse_parameter = [&](Vector<Parameter>& storage) {
  106. for (;;) {
  107. Parameter parameter;
  108. consume_whitespace();
  109. if (peek() == ')')
  110. break;
  111. parameter.type = extract_while([](char ch) { return !isspace(ch); });
  112. consume_whitespace();
  113. parameter.name = extract_while([](char ch) { return !isspace(ch) && ch != ',' && ch != ')'; });
  114. consume_whitespace();
  115. storage.append(move(parameter));
  116. if (peek() == ',') {
  117. consume_one();
  118. continue;
  119. }
  120. if (peek() == ')')
  121. break;
  122. }
  123. };
  124. auto parse_parameters = [&](Vector<Parameter>& storage) {
  125. for (;;) {
  126. consume_whitespace();
  127. parse_parameter(storage);
  128. consume_whitespace();
  129. if (peek() == ',') {
  130. consume_one();
  131. continue;
  132. }
  133. if (peek() == ')')
  134. break;
  135. }
  136. };
  137. auto parse_message = [&] {
  138. Message message;
  139. consume_whitespace();
  140. Vector<char> buffer;
  141. while (!isspace(peek()) && peek() != '(')
  142. buffer.append(consume_one());
  143. message.name = String::copy(buffer);
  144. consume_whitespace();
  145. consume_specific('(');
  146. parse_parameters(message.inputs);
  147. consume_specific(')');
  148. consume_whitespace();
  149. consume_specific('=');
  150. auto type = consume_one();
  151. if (type == '>')
  152. message.is_synchronous = true;
  153. else if (type == '|')
  154. message.is_synchronous = false;
  155. else
  156. ASSERT_NOT_REACHED();
  157. consume_whitespace();
  158. if (message.is_synchronous) {
  159. consume_specific('(');
  160. parse_parameters(message.outputs);
  161. consume_specific(')');
  162. }
  163. consume_whitespace();
  164. endpoints.last().messages.append(move(message));
  165. };
  166. auto parse_messages = [&] {
  167. for (;;) {
  168. consume_whitespace();
  169. parse_message();
  170. consume_whitespace();
  171. if (peek() == '}')
  172. break;
  173. }
  174. };
  175. auto parse_endpoint = [&] {
  176. endpoints.empend();
  177. consume_whitespace();
  178. consume_string("endpoint");
  179. consume_whitespace();
  180. endpoints.last().name = extract_while([](char ch) { return !isspace(ch); });
  181. consume_whitespace();
  182. consume_specific('=');
  183. consume_whitespace();
  184. auto magic_string = extract_while([](char ch) { return !isspace(ch) && ch != '{'; });
  185. bool ok;
  186. endpoints.last().magic = magic_string.to_int(ok);
  187. ASSERT(ok);
  188. consume_whitespace();
  189. consume_specific('{');
  190. parse_messages();
  191. consume_specific('}');
  192. consume_whitespace();
  193. };
  194. while (index < file_contents.size())
  195. parse_endpoint();
  196. dbg() << "#pragma once";
  197. dbg() << "#include <AK/BufferStream.h>";
  198. dbg() << "#include <AK/OwnPtr.h>";
  199. dbg() << "#include <LibGfx/Color.h>";
  200. dbg() << "#include <LibGfx/Rect.h>";
  201. dbg() << "#include <LibIPC/Decoder.h>";
  202. dbg() << "#include <LibIPC/Encoder.h>";
  203. dbg() << "#include <LibIPC/Endpoint.h>";
  204. dbg() << "#include <LibIPC/Message.h>";
  205. dbg();
  206. for (auto& endpoint : endpoints) {
  207. dbg() << "namespace Messages {";
  208. dbg() << "namespace " << endpoint.name << " {";
  209. dbg();
  210. HashMap<String, int> message_ids;
  211. dbg() << "enum class MessageID : i32 {";
  212. for (auto& message : endpoint.messages) {
  213. message_ids.set(message.name, message_ids.size() + 1);
  214. dbg() << " " << message.name << " = " << message_ids.size() << ",";
  215. if (message.is_synchronous) {
  216. message_ids.set(message.response_name(), message_ids.size() + 1);
  217. dbg() << " " << message.response_name() << " = " << message_ids.size() << ",";
  218. }
  219. }
  220. dbg() << "};";
  221. dbg();
  222. auto constructor_for_message = [&](const String& name, const Vector<Parameter>& parameters) {
  223. StringBuilder builder;
  224. builder.append(name);
  225. if (parameters.is_empty()) {
  226. builder.append("() {}");
  227. return builder.to_string();
  228. }
  229. builder.append('(');
  230. for (size_t i = 0; i < parameters.size(); ++i) {
  231. auto& parameter = parameters[i];
  232. builder.append("const ");
  233. builder.append(parameter.type);
  234. builder.append("& ");
  235. builder.append(parameter.name);
  236. if (i != parameters.size() - 1)
  237. builder.append(", ");
  238. }
  239. builder.append(") : ");
  240. for (size_t i = 0; i < parameters.size(); ++i) {
  241. auto& parameter = parameters[i];
  242. builder.append("m_");
  243. builder.append(parameter.name);
  244. builder.append("(");
  245. builder.append(parameter.name);
  246. builder.append(")");
  247. if (i != parameters.size() - 1)
  248. builder.append(", ");
  249. }
  250. builder.append(" {}");
  251. return builder.to_string();
  252. };
  253. auto do_message = [&](const String& name, const Vector<Parameter>& parameters, const String& response_type = {}) {
  254. dbg() << "class " << name << " final : public IPC::Message {";
  255. dbg() << "public:";
  256. if (!response_type.is_null())
  257. dbg() << " typedef class " << response_type << " ResponseType;";
  258. dbg() << " " << constructor_for_message(name, parameters);
  259. dbg() << " virtual ~" << name << "() override {}";
  260. dbg() << " virtual i32 endpoint_magic() const override { return " << endpoint.magic << "; }";
  261. dbg() << " virtual i32 message_id() const override { return (int)MessageID::" << name << "; }";
  262. dbg() << " static i32 static_message_id() { return (int)MessageID::" << name << "; }";
  263. dbg() << " virtual const char* message_name() const override { return \"" << endpoint.name << "::" << name << "\"; }";
  264. dbg() << " static OwnPtr<" << name << "> decode(BufferStream& stream, size_t& size_in_bytes)";
  265. dbg() << " {";
  266. dbg() << " IPC::Decoder decoder(stream);";
  267. for (auto& parameter : parameters) {
  268. String initial_value = "{}";
  269. if (parameter.type == "bool")
  270. initial_value = "false";
  271. dbg() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
  272. if (parameter.type == "Vector<Gfx::Rect>") {
  273. dbg() << " u64 " << parameter.name << "_size = 0;";
  274. dbg() << " stream >> " << parameter.name << "_size;";
  275. dbg() << " for (size_t i = 0; i < " << parameter.name << "_size; ++i) {";
  276. dbg() << " Gfx::Rect rect;";
  277. dbg() << " if (!decoder.decode(rect))";
  278. dbg() << " return nullptr;";
  279. dbg() << " " << parameter.name << ".append(move(rect));";
  280. dbg() << " }";
  281. } else {
  282. dbg() << " if (!decoder.decode(" << parameter.name << "))";
  283. dbg() << " return nullptr;";
  284. }
  285. }
  286. StringBuilder builder;
  287. for (size_t i = 0; i < parameters.size(); ++i) {
  288. auto& parameter = parameters[i];
  289. builder.append(parameter.name);
  290. if (i != parameters.size() - 1)
  291. builder.append(", ");
  292. }
  293. dbg() << " size_in_bytes = stream.offset();";
  294. dbg() << " return make<" << name << ">(" << builder.to_string() << ");";
  295. dbg() << " }";
  296. dbg() << " virtual IPC::MessageBuffer encode() const override";
  297. dbg() << " {";
  298. dbg() << " IPC::MessageBuffer buffer;";
  299. dbg() << " IPC::Encoder stream(buffer);";
  300. dbg() << " stream << endpoint_magic();";
  301. dbg() << " stream << (int)MessageID::" << name << ";";
  302. for (auto& parameter : parameters) {
  303. if (parameter.type == "Gfx::Color") {
  304. dbg() << " stream << m_" << parameter.name << ".value();";
  305. } else if (parameter.type == "Gfx::Size") {
  306. dbg() << " stream << m_" << parameter.name << ".width();";
  307. dbg() << " stream << m_" << parameter.name << ".height();";
  308. } else if (parameter.type == "Gfx::Point") {
  309. dbg() << " stream << m_" << parameter.name << ".x();";
  310. dbg() << " stream << m_" << parameter.name << ".y();";
  311. } else if (parameter.type == "Gfx::Rect") {
  312. dbg() << " stream << m_" << parameter.name << ".x();";
  313. dbg() << " stream << m_" << parameter.name << ".y();";
  314. dbg() << " stream << m_" << parameter.name << ".width();";
  315. dbg() << " stream << m_" << parameter.name << ".height();";
  316. } else if (parameter.type == "Vector<Gfx::Rect>") {
  317. dbg() << " stream << (u64)m_" << parameter.name << ".size();";
  318. dbg() << " for (auto& rect : m_" << parameter.name << ") {";
  319. dbg() << " stream << rect.x();";
  320. dbg() << " stream << rect.y();";
  321. dbg() << " stream << rect.width();";
  322. dbg() << " stream << rect.height();";
  323. dbg() << " }";
  324. } else {
  325. dbg() << " stream << m_" << parameter.name << ";";
  326. }
  327. }
  328. dbg() << " return buffer;";
  329. dbg() << " }";
  330. for (auto& parameter : parameters) {
  331. dbg() << " const " << parameter.type << "& " << parameter.name << "() const { return m_" << parameter.name << "; }";
  332. }
  333. dbg() << "private:";
  334. for (auto& parameter : parameters) {
  335. dbg() << " " << parameter.type << " m_" << parameter.name << ";";
  336. }
  337. dbg() << "};";
  338. dbg();
  339. };
  340. for (auto& message : endpoint.messages) {
  341. String response_name;
  342. if (message.is_synchronous) {
  343. response_name = message.response_name();
  344. do_message(response_name, message.outputs);
  345. }
  346. do_message(message.name, message.inputs, response_name);
  347. }
  348. dbg() << "} // namespace " << endpoint.name;
  349. dbg() << "} // namespace Messages";
  350. dbg();
  351. dbg() << "class " << endpoint.name << "Endpoint : public IPC::Endpoint {";
  352. dbg() << "public:";
  353. dbg() << " " << endpoint.name << "Endpoint() {}";
  354. dbg() << " virtual ~" << endpoint.name << "Endpoint() override {}";
  355. dbg() << " static int static_magic() { return " << endpoint.magic << "; }";
  356. dbg() << " virtual int magic() const override { return " << endpoint.magic << "; }";
  357. dbg() << " static String static_name() { return \"" << endpoint.name << "\"; };";
  358. dbg() << " virtual String name() const override { return \"" << endpoint.name << "\"; };";
  359. dbg() << " static OwnPtr<IPC::Message> decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)";
  360. dbg() << " {";
  361. dbg() << " BufferStream stream(const_cast<ByteBuffer&>(buffer));";
  362. dbg() << " i32 message_endpoint_magic = 0;";
  363. dbg() << " stream >> message_endpoint_magic;";
  364. dbg() << " if (message_endpoint_magic != " << endpoint.magic << ") {";
  365. #ifdef GENERATE_DEBUG_CODE
  366. dbg() << " dbg() << \"endpoint magic \" << message_endpoint_magic << \" != " << endpoint.magic << "\";";
  367. #endif
  368. dbg() << " return nullptr;";
  369. dbg() << " }";
  370. dbg() << " i32 message_id = 0;";
  371. dbg() << " stream >> message_id;";
  372. dbg() << " switch (message_id) {";
  373. for (auto& message : endpoint.messages) {
  374. auto do_decode_message = [&](const String& name) {
  375. dbg() << " case (int)Messages::" << endpoint.name << "::MessageID::" << name << ":";
  376. dbg() << " return Messages::" << endpoint.name << "::" << name << "::decode(stream, size_in_bytes);";
  377. };
  378. do_decode_message(message.name);
  379. if (message.is_synchronous)
  380. do_decode_message(message.response_name());
  381. }
  382. dbg() << " default:";
  383. #ifdef GENERATE_DEBUG_CODE
  384. dbg() << " dbg() << \"Failed to decode " << endpoint.name << ".(\" << message_id << \")\";";
  385. #endif
  386. dbg() << " return nullptr;";
  387. dbg() << " }";
  388. dbg() << " }";
  389. dbg();
  390. dbg() << " virtual OwnPtr<IPC::Message> handle(const IPC::Message& message) override";
  391. dbg() << " {";
  392. dbg() << " switch (message.message_id()) {";
  393. for (auto& message : endpoint.messages) {
  394. auto do_decode_message = [&](const String& name, bool returns_something) {
  395. dbg() << " case (int)Messages::" << endpoint.name << "::MessageID::" << name << ":";
  396. if (returns_something) {
  397. dbg() << " return handle(static_cast<const Messages::" << endpoint.name << "::" << name << "&>(message));";
  398. } else {
  399. dbg() << " handle(static_cast<const Messages::" << endpoint.name << "::" << name << "&>(message));";
  400. dbg() << " return nullptr;";
  401. }
  402. };
  403. do_decode_message(message.name, message.is_synchronous);
  404. if (message.is_synchronous)
  405. do_decode_message(message.response_name(), false);
  406. }
  407. dbg() << " default:";
  408. dbg() << " return nullptr;";
  409. dbg() << " }";
  410. dbg() << " }";
  411. for (auto& message : endpoint.messages) {
  412. String return_type = "void";
  413. if (message.is_synchronous) {
  414. StringBuilder builder;
  415. builder.append("OwnPtr<Messages::");
  416. builder.append(endpoint.name);
  417. builder.append("::");
  418. builder.append(message.name);
  419. builder.append("Response");
  420. builder.append(">");
  421. return_type = builder.to_string();
  422. }
  423. dbg() << " virtual " << return_type << " handle(const Messages::" << endpoint.name << "::" << message.name << "&) = 0;";
  424. }
  425. dbg() << "private:";
  426. dbg() << "};";
  427. }
  428. #ifdef DEBUG
  429. for (auto& endpoint : endpoints) {
  430. dbg() << "Endpoint: '" << endpoint.name << "' (magic: " << endpoint.magic << ")";
  431. for (auto& message : endpoint.messages) {
  432. dbg() << " Message: '" << message.name << "'";
  433. dbg() << " Sync: " << message.is_synchronous;
  434. dbg() << " Inputs:";
  435. for (auto& parameter : message.inputs)
  436. dbg() << " Parameter: " << parameter.name << " (" << parameter.type << ")";
  437. if (message.inputs.is_empty())
  438. dbg() << " (none)";
  439. if (message.is_synchronous) {
  440. dbg() << " Outputs:";
  441. for (auto& parameter : message.outputs)
  442. dbg() << " Parameter: " << parameter.name << " (" << parameter.type << ")";
  443. if (message.outputs.is_empty())
  444. dbg() << " (none)";
  445. }
  446. }
  447. }
  448. #endif
  449. return 0;
  450. }