main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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/HashMap.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/File.h>
  30. #include <ctype.h>
  31. #include <stdio.h>
  32. //#define GENERATE_DEBUG_CODE
  33. struct Parameter {
  34. Vector<String> attributes;
  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. warn() << "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. if (peek() == '[') {
  112. consume_one();
  113. for (;;) {
  114. if (peek() == ']') {
  115. consume_one();
  116. consume_whitespace();
  117. break;
  118. }
  119. if (peek() == ',') {
  120. consume_one();
  121. consume_whitespace();
  122. }
  123. auto attribute = extract_while([](char ch) { return ch != ']' && ch != ','; });
  124. parameter.attributes.append(attribute);
  125. consume_whitespace();
  126. }
  127. }
  128. parameter.type = extract_while([](char ch) { return !isspace(ch); });
  129. consume_whitespace();
  130. parameter.name = extract_while([](char ch) { return !isspace(ch) && ch != ',' && ch != ')'; });
  131. consume_whitespace();
  132. storage.append(move(parameter));
  133. if (peek() == ',') {
  134. consume_one();
  135. continue;
  136. }
  137. if (peek() == ')')
  138. break;
  139. }
  140. };
  141. auto parse_parameters = [&](Vector<Parameter>& storage) {
  142. for (;;) {
  143. consume_whitespace();
  144. parse_parameter(storage);
  145. consume_whitespace();
  146. if (peek() == ',') {
  147. consume_one();
  148. continue;
  149. }
  150. if (peek() == ')')
  151. break;
  152. }
  153. };
  154. auto parse_message = [&] {
  155. Message message;
  156. consume_whitespace();
  157. Vector<char> buffer;
  158. while (!isspace(peek()) && peek() != '(')
  159. buffer.append(consume_one());
  160. message.name = String::copy(buffer);
  161. consume_whitespace();
  162. consume_specific('(');
  163. parse_parameters(message.inputs);
  164. consume_specific(')');
  165. consume_whitespace();
  166. consume_specific('=');
  167. auto type = consume_one();
  168. if (type == '>')
  169. message.is_synchronous = true;
  170. else if (type == '|')
  171. message.is_synchronous = false;
  172. else
  173. ASSERT_NOT_REACHED();
  174. consume_whitespace();
  175. if (message.is_synchronous) {
  176. consume_specific('(');
  177. parse_parameters(message.outputs);
  178. consume_specific(')');
  179. }
  180. consume_whitespace();
  181. endpoints.last().messages.append(move(message));
  182. };
  183. auto parse_messages = [&] {
  184. for (;;) {
  185. consume_whitespace();
  186. parse_message();
  187. consume_whitespace();
  188. if (peek() == '}')
  189. break;
  190. }
  191. };
  192. auto parse_endpoint = [&] {
  193. endpoints.empend();
  194. consume_whitespace();
  195. consume_string("endpoint");
  196. consume_whitespace();
  197. endpoints.last().name = extract_while([](char ch) { return !isspace(ch); });
  198. consume_whitespace();
  199. consume_specific('=');
  200. consume_whitespace();
  201. auto magic_string = extract_while([](char ch) { return !isspace(ch) && ch != '{'; });
  202. bool ok;
  203. endpoints.last().magic = magic_string.to_int(ok);
  204. ASSERT(ok);
  205. consume_whitespace();
  206. consume_specific('{');
  207. parse_messages();
  208. consume_specific('}');
  209. consume_whitespace();
  210. };
  211. while (index < file_contents.size())
  212. parse_endpoint();
  213. out() << "#pragma once";
  214. out() << "#include <AK/BufferStream.h>";
  215. out() << "#include <AK/OwnPtr.h>";
  216. out() << "#include <AK/URL.h>";
  217. out() << "#include <AK/Utf8View.h>";
  218. out() << "#include <LibGfx/Color.h>";
  219. out() << "#include <LibGfx/Rect.h>";
  220. out() << "#include <LibGfx/ShareableBitmap.h>";
  221. out() << "#include <LibIPC/Decoder.h>";
  222. out() << "#include <LibIPC/Dictionary.h>";
  223. out() << "#include <LibIPC/Encoder.h>";
  224. out() << "#include <LibIPC/Endpoint.h>";
  225. out() << "#include <LibIPC/Message.h>";
  226. out();
  227. for (auto& endpoint : endpoints) {
  228. out() << "namespace Messages {";
  229. out() << "namespace " << endpoint.name << " {";
  230. out();
  231. HashMap<String, int> message_ids;
  232. out() << "enum class MessageID : i32 {";
  233. for (auto& message : endpoint.messages) {
  234. message_ids.set(message.name, message_ids.size() + 1);
  235. out() << " " << message.name << " = " << message_ids.size() << ",";
  236. if (message.is_synchronous) {
  237. message_ids.set(message.response_name(), message_ids.size() + 1);
  238. out() << " " << message.response_name() << " = " << message_ids.size() << ",";
  239. }
  240. }
  241. out() << "};";
  242. out();
  243. auto constructor_for_message = [&](const String& name, const Vector<Parameter>& parameters) {
  244. StringBuilder builder;
  245. builder.append(name);
  246. if (parameters.is_empty()) {
  247. builder.append("() {}");
  248. return builder.to_string();
  249. }
  250. builder.append('(');
  251. for (size_t i = 0; i < parameters.size(); ++i) {
  252. auto& parameter = parameters[i];
  253. builder.append("const ");
  254. builder.append(parameter.type);
  255. builder.append("& ");
  256. builder.append(parameter.name);
  257. if (i != parameters.size() - 1)
  258. builder.append(", ");
  259. }
  260. builder.append(") : ");
  261. for (size_t i = 0; i < parameters.size(); ++i) {
  262. auto& parameter = parameters[i];
  263. builder.append("m_");
  264. builder.append(parameter.name);
  265. builder.append("(");
  266. builder.append(parameter.name);
  267. builder.append(")");
  268. if (i != parameters.size() - 1)
  269. builder.append(", ");
  270. }
  271. builder.append(" {}");
  272. return builder.to_string();
  273. };
  274. auto do_message = [&](const String& name, const Vector<Parameter>& parameters, const String& response_type = {}) {
  275. out() << "class " << name << " final : public IPC::Message {";
  276. out() << "public:";
  277. if (!response_type.is_null())
  278. out() << " typedef class " << response_type << " ResponseType;";
  279. out() << " " << constructor_for_message(name, parameters);
  280. out() << " virtual ~" << name << "() override {}";
  281. out() << " virtual i32 endpoint_magic() const override { return " << endpoint.magic << "; }";
  282. out() << " virtual i32 message_id() const override { return (int)MessageID::" << name << "; }";
  283. out() << " static i32 static_message_id() { return (int)MessageID::" << name << "; }";
  284. out() << " virtual const char* message_name() const override { return \"" << endpoint.name << "::" << name << "\"; }";
  285. out() << " static OwnPtr<" << name << "> decode(BufferStream& stream, size_t& size_in_bytes)";
  286. out() << " {";
  287. out() << " IPC::Decoder decoder(stream);";
  288. for (auto& parameter : parameters) {
  289. String initial_value = "{}";
  290. if (parameter.type == "bool")
  291. initial_value = "false";
  292. out() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
  293. out() << " if (!decoder.decode(" << parameter.name << "))";
  294. out() << " return nullptr;";
  295. if (parameter.attributes.contains_slow("UTF8")) {
  296. out() << " if (!Utf8View(" << parameter.name << ").validate())";
  297. out() << " return nullptr;";
  298. }
  299. }
  300. StringBuilder builder;
  301. for (size_t i = 0; i < parameters.size(); ++i) {
  302. auto& parameter = parameters[i];
  303. builder.append(parameter.name);
  304. if (i != parameters.size() - 1)
  305. builder.append(", ");
  306. }
  307. out() << " size_in_bytes = stream.offset();";
  308. out() << " return make<" << name << ">(" << builder.to_string() << ");";
  309. out() << " }";
  310. out() << " virtual IPC::MessageBuffer encode() const override";
  311. out() << " {";
  312. out() << " IPC::MessageBuffer buffer;";
  313. out() << " IPC::Encoder stream(buffer);";
  314. out() << " stream << endpoint_magic();";
  315. out() << " stream << (int)MessageID::" << name << ";";
  316. for (auto& parameter : parameters) {
  317. out() << " stream << m_" << parameter.name << ";";
  318. }
  319. out() << " return buffer;";
  320. out() << " }";
  321. for (auto& parameter : parameters) {
  322. out() << " const " << parameter.type << "& " << parameter.name << "() const { return m_" << parameter.name << "; }";
  323. }
  324. out() << "private:";
  325. for (auto& parameter : parameters) {
  326. out() << " " << parameter.type << " m_" << parameter.name << ";";
  327. }
  328. out() << "};";
  329. out();
  330. };
  331. for (auto& message : endpoint.messages) {
  332. String response_name;
  333. if (message.is_synchronous) {
  334. response_name = message.response_name();
  335. do_message(response_name, message.outputs);
  336. }
  337. do_message(message.name, message.inputs, response_name);
  338. }
  339. out() << "} // namespace " << endpoint.name;
  340. out() << "} // namespace Messages";
  341. out();
  342. out() << "class " << endpoint.name << "Endpoint : public IPC::Endpoint {";
  343. out() << "public:";
  344. out() << " " << endpoint.name << "Endpoint() {}";
  345. out() << " virtual ~" << endpoint.name << "Endpoint() override {}";
  346. out() << " static int static_magic() { return " << endpoint.magic << "; }";
  347. out() << " virtual int magic() const override { return " << endpoint.magic << "; }";
  348. out() << " static String static_name() { return \"" << endpoint.name << "\"; };";
  349. out() << " virtual String name() const override { return \"" << endpoint.name << "\"; };";
  350. out() << " static OwnPtr<IPC::Message> decode_message(const ByteBuffer& buffer, size_t& size_in_bytes)";
  351. out() << " {";
  352. out() << " BufferStream stream(const_cast<ByteBuffer&>(buffer));";
  353. out() << " i32 message_endpoint_magic = 0;";
  354. out() << " stream >> message_endpoint_magic;";
  355. out() << " if (stream.handle_read_failure()) {";
  356. #ifdef GENERATE_DEBUG_CODE
  357. out() << " dbg() << \"Failed to read message endpoint magic\";";
  358. #endif
  359. out() << " return nullptr;";
  360. out() << " }";
  361. out() << " if (message_endpoint_magic != " << endpoint.magic << ") {";
  362. #ifdef GENERATE_DEBUG_CODE
  363. out() << " dbg() << \"endpoint magic \" << message_endpoint_magic << \" != " << endpoint.magic << "\";";
  364. #endif
  365. out() << " return nullptr;";
  366. out() << " }";
  367. out() << " i32 message_id = 0;";
  368. out() << " stream >> message_id;";
  369. out() << " if (stream.handle_read_failure()) {";
  370. #ifdef GENERATE_DEBUG_CODE
  371. out() << " dbg() << \"Failed to read message ID\";";
  372. #endif
  373. out() << " return nullptr;";
  374. out() << " }";
  375. out() << " OwnPtr<IPC::Message> message;";
  376. out() << " switch (message_id) {";
  377. for (auto& message : endpoint.messages) {
  378. auto do_decode_message = [&](const String& name) {
  379. out() << " case (int)Messages::" << endpoint.name << "::MessageID::" << name << ":";
  380. out() << " message = Messages::" << endpoint.name << "::" << name << "::decode(stream, size_in_bytes);";
  381. out() << " break;";
  382. };
  383. do_decode_message(message.name);
  384. if (message.is_synchronous)
  385. do_decode_message(message.response_name());
  386. }
  387. out() << " default:";
  388. #ifdef GENERATE_DEBUG_CODE
  389. out() << " dbg() << \"Failed to decode " << endpoint.name << ".(\" << message_id << \")\";";
  390. #endif
  391. out() << " return nullptr;";
  392. out() << " }";
  393. out() << " if (stream.handle_read_failure()) {";
  394. #ifdef GENERATE_DEBUG_CODE
  395. out() << " sout() << \"Failed to read the message\";";
  396. #endif
  397. out() << " return nullptr;";
  398. out() << " }";
  399. out() << " return message;";
  400. out() << " }";
  401. out();
  402. out() << " virtual OwnPtr<IPC::Message> handle(const IPC::Message& message) override";
  403. out() << " {";
  404. out() << " switch (message.message_id()) {";
  405. for (auto& message : endpoint.messages) {
  406. auto do_decode_message = [&](const String& name, bool returns_something) {
  407. out() << " case (int)Messages::" << endpoint.name << "::MessageID::" << name << ":";
  408. if (returns_something) {
  409. out() << " return handle(static_cast<const Messages::" << endpoint.name << "::" << name << "&>(message));";
  410. } else {
  411. out() << " handle(static_cast<const Messages::" << endpoint.name << "::" << name << "&>(message));";
  412. out() << " return nullptr;";
  413. }
  414. };
  415. do_decode_message(message.name, message.is_synchronous);
  416. if (message.is_synchronous)
  417. do_decode_message(message.response_name(), false);
  418. }
  419. out() << " default:";
  420. out() << " return nullptr;";
  421. out() << " }";
  422. out() << " }";
  423. for (auto& message : endpoint.messages) {
  424. String return_type = "void";
  425. if (message.is_synchronous) {
  426. StringBuilder builder;
  427. builder.append("OwnPtr<Messages::");
  428. builder.append(endpoint.name);
  429. builder.append("::");
  430. builder.append(message.name);
  431. builder.append("Response");
  432. builder.append(">");
  433. return_type = builder.to_string();
  434. }
  435. out() << " virtual " << return_type << " handle(const Messages::" << endpoint.name << "::" << message.name << "&) = 0;";
  436. }
  437. out() << "private:";
  438. out() << "};";
  439. }
  440. #ifdef DEBUG
  441. for (auto& endpoint : endpoints) {
  442. warn() << "Endpoint: '" << endpoint.name << "' (magic: " << endpoint.magic << ")";
  443. for (auto& message : endpoint.messages) {
  444. warn() << " Message: '" << message.name << "'";
  445. warn() << " Sync: " << message.is_synchronous;
  446. warn() << " Inputs:";
  447. for (auto& parameter : message.inputs)
  448. warn() << " Parameter: " << parameter.name << " (" << parameter.type << ")";
  449. if (message.inputs.is_empty())
  450. warn() << " (none)";
  451. if (message.is_synchronous) {
  452. warn() << " Outputs:";
  453. for (auto& parameter : message.outputs)
  454. warn() << " Parameter: " << parameter.name << " (" << parameter.type << ")";
  455. if (message.outputs.is_empty())
  456. warn() << " (none)";
  457. }
  458. }
  459. }
  460. #endif
  461. return 0;
  462. }