main.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  5. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "IDLParser.h"
  10. #include "IDLTypes.h"
  11. #include <AK/Debug.h>
  12. #include <AK/LexicalPath.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/File.h>
  15. extern Vector<StringView> s_header_search_paths;
  16. namespace IDL {
  17. void generate_constructor_header(IDL::Interface const&);
  18. void generate_constructor_implementation(IDL::Interface const&);
  19. void generate_prototype_header(IDL::Interface const&);
  20. void generate_prototype_implementation(IDL::Interface const&);
  21. void generate_header(IDL::Interface const&);
  22. void generate_implementation(IDL::Interface const&);
  23. void generate_iterator_prototype_header(IDL::Interface const&);
  24. void generate_iterator_prototype_implementation(IDL::Interface const&);
  25. void generate_iterator_header(IDL::Interface const&);
  26. void generate_iterator_implementation(IDL::Interface const&);
  27. }
  28. int main(int argc, char** argv)
  29. {
  30. Core::ArgsParser args_parser;
  31. StringView path;
  32. StringView import_base_path;
  33. bool header_mode = false;
  34. bool implementation_mode = false;
  35. bool constructor_header_mode = false;
  36. bool constructor_implementation_mode = false;
  37. bool prototype_header_mode = false;
  38. bool prototype_implementation_mode = false;
  39. bool iterator_header_mode = false;
  40. bool iterator_implementation_mode = false;
  41. bool iterator_prototype_header_mode = false;
  42. bool iterator_prototype_implementation_mode = false;
  43. args_parser.add_option(header_mode, "Generate the wrapper .h file", "header", 'H');
  44. args_parser.add_option(implementation_mode, "Generate the wrapper .cpp file", "implementation", 'I');
  45. args_parser.add_option(constructor_header_mode, "Generate the constructor .h file", "constructor-header", 'C');
  46. args_parser.add_option(constructor_implementation_mode, "Generate the constructor .cpp file", "constructor-implementation", 'O');
  47. args_parser.add_option(prototype_header_mode, "Generate the prototype .h file", "prototype-header", 'P');
  48. args_parser.add_option(prototype_implementation_mode, "Generate the prototype .cpp file", "prototype-implementation", 'R');
  49. args_parser.add_option(iterator_header_mode, "Generate the iterator wrapper .h file", "iterator-header", 0);
  50. args_parser.add_option(iterator_implementation_mode, "Generate the iterator wrapper .cpp file", "iterator-implementation", 0);
  51. args_parser.add_option(iterator_prototype_header_mode, "Generate the iterator prototype .h file", "iterator-prototype-header", 0);
  52. args_parser.add_option(iterator_prototype_implementation_mode, "Generate the iterator prototype .cpp file", "iterator-prototype-implementation", 0);
  53. args_parser.add_option(Core::ArgsParser::Option {
  54. .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
  55. .help_string = "Add a header search path passed to the compiler",
  56. .long_name = "header-include-path",
  57. .short_name = 'i',
  58. .value_name = "path",
  59. .accept_value = [&](char const* s) {
  60. s_header_search_paths.append({ s, strlen(s) });
  61. return true;
  62. },
  63. });
  64. args_parser.add_positional_argument(path, "IDL file", "idl-file");
  65. args_parser.add_positional_argument(import_base_path, "Import base path", "import-base-path", Core::ArgsParser::Required::No);
  66. args_parser.parse(argc, argv);
  67. auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
  68. if (file_or_error.is_error()) {
  69. warnln("Failed to open {}: {}", path, file_or_error.error());
  70. return 1;
  71. }
  72. LexicalPath lexical_path(path);
  73. auto& namespace_ = lexical_path.parts_view().at(lexical_path.parts_view().size() - 2);
  74. auto data = file_or_error.value()->read_all();
  75. if (import_base_path.is_null())
  76. import_base_path = lexical_path.dirname();
  77. auto& interface = IDL::Parser(path, data, import_base_path).parse();
  78. static constexpr Array libweb_interface_namespaces = {
  79. "CSS"sv,
  80. "Crypto"sv,
  81. "DOM"sv,
  82. "DOMParsing"sv,
  83. "Encoding"sv,
  84. "Fetch"sv,
  85. "FileAPI"sv,
  86. "Geometry"sv,
  87. "HTML"sv,
  88. "HighResolutionTime"sv,
  89. "IntersectionObserver"sv,
  90. "NavigationTiming"sv,
  91. "RequestIdleCallback"sv,
  92. "ResizeObserver"sv,
  93. "SVG"sv,
  94. "Selection"sv,
  95. "UIEvents"sv,
  96. "URL"sv,
  97. "WebGL"sv,
  98. "WebSockets"sv,
  99. "XHR"sv,
  100. };
  101. if (libweb_interface_namespaces.span().contains_slow(namespace_)) {
  102. StringBuilder builder;
  103. builder.append(namespace_);
  104. builder.append("::"sv);
  105. builder.append(interface.name);
  106. interface.fully_qualified_name = builder.to_string();
  107. } else {
  108. interface.fully_qualified_name = interface.name;
  109. }
  110. if constexpr (WRAPPER_GENERATOR_DEBUG) {
  111. dbgln("Attributes:");
  112. for (auto& attribute : interface.attributes) {
  113. dbgln(" {}{}{} {}",
  114. attribute.readonly ? "readonly " : "",
  115. attribute.type->name,
  116. attribute.type->nullable ? "?" : "",
  117. attribute.name);
  118. }
  119. dbgln("Functions:");
  120. for (auto& function : interface.functions) {
  121. dbgln(" {}{} {}",
  122. function.return_type->name,
  123. function.return_type->nullable ? "?" : "",
  124. function.name);
  125. for (auto& parameter : function.parameters) {
  126. dbgln(" {}{} {}",
  127. parameter.type->name,
  128. parameter.type->nullable ? "?" : "",
  129. parameter.name);
  130. }
  131. }
  132. dbgln("Static Functions:");
  133. for (auto& function : interface.static_functions) {
  134. dbgln(" static {}{} {}",
  135. function.return_type->name,
  136. function.return_type->nullable ? "?" : "",
  137. function.name);
  138. for (auto& parameter : function.parameters) {
  139. dbgln(" {}{} {}",
  140. parameter.type->name,
  141. parameter.type->nullable ? "?" : "",
  142. parameter.name);
  143. }
  144. }
  145. }
  146. if (header_mode)
  147. IDL::generate_header(interface);
  148. if (implementation_mode)
  149. IDL::generate_implementation(interface);
  150. if (constructor_header_mode)
  151. IDL::generate_constructor_header(interface);
  152. if (constructor_implementation_mode)
  153. IDL::generate_constructor_implementation(interface);
  154. if (prototype_header_mode)
  155. IDL::generate_prototype_header(interface);
  156. if (prototype_implementation_mode)
  157. IDL::generate_prototype_implementation(interface);
  158. if (iterator_header_mode)
  159. IDL::generate_iterator_header(interface);
  160. if (iterator_implementation_mode)
  161. IDL::generate_iterator_implementation(interface);
  162. if (iterator_prototype_header_mode)
  163. IDL::generate_iterator_prototype_header(interface);
  164. if (iterator_prototype_implementation_mode)
  165. IDL::generate_iterator_prototype_implementation(interface);
  166. return 0;
  167. }