main.cpp 6.2 KB

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