main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_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. "WebSockets"sv,
  87. "XHR"sv,
  88. };
  89. if (libweb_interface_namespaces.span().contains_slow(namespace_)) {
  90. StringBuilder builder;
  91. builder.append(namespace_);
  92. builder.append("::"sv);
  93. builder.append(interface.name);
  94. interface.fully_qualified_name = builder.to_string();
  95. } else {
  96. interface.fully_qualified_name = interface.name;
  97. }
  98. if constexpr (WRAPPER_GENERATOR_DEBUG) {
  99. dbgln("Attributes:");
  100. for (auto& attribute : interface.attributes) {
  101. dbgln(" {}{}{} {}",
  102. attribute.readonly ? "readonly " : "",
  103. attribute.type->name,
  104. attribute.type->nullable ? "?" : "",
  105. attribute.name);
  106. }
  107. dbgln("Functions:");
  108. for (auto& function : interface.functions) {
  109. dbgln(" {}{} {}",
  110. function.return_type->name,
  111. function.return_type->nullable ? "?" : "",
  112. function.name);
  113. for (auto& parameter : function.parameters) {
  114. dbgln(" {}{} {}",
  115. parameter.type->name,
  116. parameter.type->nullable ? "?" : "",
  117. parameter.name);
  118. }
  119. }
  120. dbgln("Static Functions:");
  121. for (auto& function : interface.static_functions) {
  122. dbgln(" static {}{} {}",
  123. function.return_type->name,
  124. function.return_type->nullable ? "?" : "",
  125. function.name);
  126. for (auto& parameter : function.parameters) {
  127. dbgln(" {}{} {}",
  128. parameter.type->name,
  129. parameter.type->nullable ? "?" : "",
  130. parameter.name);
  131. }
  132. }
  133. }
  134. if (constructor_header_mode)
  135. IDL::generate_constructor_header(interface);
  136. if (constructor_implementation_mode)
  137. IDL::generate_constructor_implementation(interface);
  138. if (prototype_header_mode)
  139. IDL::generate_prototype_header(interface);
  140. if (prototype_implementation_mode)
  141. IDL::generate_prototype_implementation(interface);
  142. if (iterator_prototype_header_mode)
  143. IDL::generate_iterator_prototype_header(interface);
  144. if (iterator_prototype_implementation_mode)
  145. IDL::generate_iterator_prototype_implementation(interface);
  146. return 0;
  147. }