Types.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibIDL/Types.h>
  8. namespace IDL {
  9. ParameterizedType const& Type::as_parameterized() const
  10. {
  11. return verify_cast<ParameterizedType const>(*this);
  12. }
  13. ParameterizedType& Type::as_parameterized()
  14. {
  15. return verify_cast<ParameterizedType>(*this);
  16. }
  17. UnionType const& Type::as_union() const
  18. {
  19. return verify_cast<UnionType const>(*this);
  20. }
  21. UnionType& Type::as_union()
  22. {
  23. return verify_cast<UnionType>(*this);
  24. }
  25. // https://webidl.spec.whatwg.org/#dfn-includes-a-nullable-type
  26. bool Type::includes_nullable_type() const
  27. {
  28. // A type includes a nullable type if:
  29. // - the type is a nullable type, or
  30. if (is_nullable())
  31. return true;
  32. // FIXME: - the type is an annotated type and its inner type is a nullable type, or
  33. // - the type is a union type and its number of nullable member types is 1.
  34. if (is_union() && as_union().number_of_nullable_member_types() == 1)
  35. return true;
  36. return false;
  37. }
  38. // https://webidl.spec.whatwg.org/#dfn-includes-undefined
  39. bool Type::includes_undefined() const
  40. {
  41. // A type includes undefined if:
  42. // - the type is undefined, or
  43. if (is_undefined())
  44. return true;
  45. // - the type is a nullable type and its inner type includes undefined, or
  46. // NOTE: We don't treat nullable as its own type, so this is handled by the other cases.
  47. // FIXME: - the type is an annotated type and its inner type includes undefined, or
  48. // - the type is a union type and one of its member types includes undefined.
  49. if (is_union()) {
  50. for (auto& type : as_union().member_types()) {
  51. if (type->includes_undefined())
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. // https://webidl.spec.whatwg.org/#dfn-distinguishable
  58. bool Type::is_distinguishable_from(IDL::Interface const& interface, IDL::Type const& other) const
  59. {
  60. // 1. If one type includes a nullable type and the other type either includes a nullable type,
  61. // is a union type with flattened member types including a dictionary type, or is a dictionary type,
  62. // return false.
  63. if (includes_nullable_type() && (other.includes_nullable_type() || (other.is_union() && any_of(other.as_union().flattened_member_types(), [&interface](auto const& type) { return interface.dictionaries.contains(type->name()); })) || interface.dictionaries.contains(other.name())))
  64. return false;
  65. // 2. If both types are either a union type or nullable union type, return true if each member type
  66. // of the one is distinguishable with each member type of the other, or false otherwise.
  67. if (is_union() && other.is_union()) {
  68. auto const& this_union = as_union();
  69. auto const& other_union = other.as_union();
  70. for (auto& this_member_type : this_union.member_types()) {
  71. for (auto& other_member_type : other_union.member_types()) {
  72. if (!this_member_type->is_distinguishable_from(interface, other_member_type))
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. // 3. If one type is a union type or nullable union type, return true if each member type of the union
  79. // type is distinguishable with the non-union type, or false otherwise.
  80. if (is_union() || other.is_union()) {
  81. auto const& the_union = is_union() ? as_union() : other.as_union();
  82. auto const& non_union = is_union() ? other : *this;
  83. for (auto& member_type : the_union.member_types()) {
  84. if (!non_union.is_distinguishable_from(interface, member_type))
  85. return false;
  86. }
  87. return true;
  88. }
  89. // 4. Consider the two "innermost" types derived by taking each type’s inner type if it is an annotated type,
  90. // and then taking its inner type inner type if the result is a nullable type. If these two innermost types
  91. // appear or are in categories appearing in the following table and there is a “●” mark in the corresponding
  92. // entry or there is a letter in the corresponding entry and the designated additional requirement below the
  93. // table is satisfied, then return true. Otherwise return false.
  94. auto const& this_innermost_type = innermost_type();
  95. auto const& other_innermost_type = other.innermost_type();
  96. enum class DistinguishabilityCategory {
  97. Undefined,
  98. Boolean,
  99. Numeric,
  100. BigInt,
  101. String,
  102. Object,
  103. Symbol,
  104. InterfaceLike,
  105. CallbackFunction,
  106. DictionaryLike,
  107. SequenceLike,
  108. __Count
  109. };
  110. // See https://webidl.spec.whatwg.org/#distinguishable-table
  111. // clang-format off
  112. static constexpr bool table[to_underlying(DistinguishabilityCategory::__Count)][to_underlying(DistinguishabilityCategory::__Count)] {
  113. {false, true, true, true, true, true, true, true, true, false, true},
  114. { true, false, true, true, true, true, true, true, true, true, true},
  115. { true, true, false, true, true, true, true, true, true, true, true},
  116. { true, true, true, false, true, true, true, true, true, true, true},
  117. { true, true, true, true, false, true, true, true, true, true, true},
  118. { true, true, true, true, true, false, true, false, false, false, false},
  119. { true, true, true, true, true, true, false, true, true, true, true},
  120. { true, true, true, true, true, false, true, false, true, true, true},
  121. { true, true, true, true, true, false, true, true, false, false, true},
  122. {false, true, true, true, true, false, true, true, false, false, true},
  123. { true, true, true, true, true, false, true, true, true, true, false},
  124. };
  125. // clang-format on
  126. auto determine_category = [&interface](Type const& type) -> DistinguishabilityCategory {
  127. if (type.is_undefined())
  128. return DistinguishabilityCategory::Undefined;
  129. if (type.is_boolean())
  130. return DistinguishabilityCategory::Boolean;
  131. if (type.is_numeric())
  132. return DistinguishabilityCategory::Numeric;
  133. if (type.is_bigint())
  134. return DistinguishabilityCategory::BigInt;
  135. if (type.is_string())
  136. return DistinguishabilityCategory::String;
  137. if (type.is_object())
  138. return DistinguishabilityCategory::Object;
  139. if (type.is_symbol())
  140. return DistinguishabilityCategory::Symbol;
  141. // FIXME: InterfaceLike - see below
  142. // FIXME: CallbackFunction
  143. // DictionaryLike
  144. // * Dictionary Types
  145. // * Record Types
  146. // FIXME: * Callback Interface Types
  147. if (interface.dictionaries.contains(type.name()) || (type.is_parameterized() && type.name() == "record"sv))
  148. return DistinguishabilityCategory::DictionaryLike;
  149. // FIXME: Frozen array types are included in "sequence-like"
  150. if (type.is_sequence())
  151. return DistinguishabilityCategory::SequenceLike;
  152. // FIXME: For lack of a better way of determining if something is an interface type, this just assumes anything we don't recognise is one.
  153. dbgln("Unable to determine category for type named '{}', assuming it's an interface type.", type.name());
  154. return DistinguishabilityCategory::InterfaceLike;
  155. };
  156. auto this_distinguishability = determine_category(this_innermost_type);
  157. auto other_distinguishability = determine_category(other_innermost_type);
  158. if (this_distinguishability == DistinguishabilityCategory::InterfaceLike && other_distinguishability == DistinguishabilityCategory::InterfaceLike) {
  159. // The two identified interface-like types are not the same, and
  160. // FIXME: no single platform object implements both interface-like types.
  161. return this_innermost_type.name() != other_innermost_type.name();
  162. }
  163. return table[to_underlying(this_distinguishability)][to_underlying(other_distinguishability)];
  164. }
  165. // https://webidl.spec.whatwg.org/#dfn-json-types
  166. bool Type::is_json(Interface const& interface) const
  167. {
  168. // The JSON types are:
  169. // - numeric types,
  170. if (is_numeric())
  171. return true;
  172. // - boolean,
  173. if (is_boolean())
  174. return true;
  175. // - string types,
  176. if (is_string() || interface.enumerations.find(m_name) != interface.enumerations.end())
  177. return true;
  178. // - object,
  179. if (is_object())
  180. return true;
  181. // - nullable types whose inner type is a JSON type,
  182. // - annotated types whose inner type is a JSON type,
  183. // NOTE: We don't separate nullable and annotated into separate types.
  184. // - union types whose member types are JSON types,
  185. if (is_union()) {
  186. auto const& union_type = as_union();
  187. for (auto const& type : union_type.member_types()) {
  188. if (!type->is_json(interface))
  189. return false;
  190. }
  191. return true;
  192. }
  193. // - typedefs whose type being given a new name is a JSON type,
  194. auto typedef_iterator = interface.typedefs.find(m_name);
  195. if (typedef_iterator != interface.typedefs.end())
  196. return typedef_iterator->value.type->is_json(interface);
  197. // - sequence types whose parameterized type is a JSON type,
  198. // - frozen array types whose parameterized type is a JSON type,
  199. // - records where all of their values are JSON types,
  200. if (is_parameterized() && m_name.is_one_of("sequence", "FrozenArray", "record")) {
  201. auto const& parameterized_type = as_parameterized();
  202. for (auto const& parameter : parameterized_type.parameters()) {
  203. if (!parameter->is_json(interface))
  204. return false;
  205. }
  206. return true;
  207. }
  208. // - dictionary types where the types of all members declared on the dictionary and all its inherited dictionaries are JSON types,
  209. auto dictionary_iterator = interface.dictionaries.find(m_name);
  210. if (dictionary_iterator != interface.dictionaries.end()) {
  211. auto const& dictionary = dictionary_iterator->value;
  212. for (auto const& member : dictionary.members) {
  213. if (!member.type->is_json(interface))
  214. return false;
  215. }
  216. return true;
  217. }
  218. // - interface types that have a toJSON operation declared on themselves or one of their inherited interfaces.
  219. Optional<Interface const&> current_interface_for_to_json;
  220. if (m_name == interface.name) {
  221. current_interface_for_to_json = interface;
  222. } else {
  223. // NOTE: Interface types must have the IDL file of their interface imported.
  224. // Though the type name may not refer to an interface, so we don't assert this here.
  225. auto imported_interface_iterator = interface.imported_modules.find_if([this](IDL::Interface const& imported_interface) {
  226. return imported_interface.name == m_name;
  227. });
  228. if (imported_interface_iterator != interface.imported_modules.end())
  229. current_interface_for_to_json = *imported_interface_iterator;
  230. }
  231. while (current_interface_for_to_json.has_value()) {
  232. auto to_json_iterator = current_interface_for_to_json->functions.find_if([](IDL::Function const& function) {
  233. return function.name == "toJSON"sv;
  234. });
  235. if (to_json_iterator != current_interface_for_to_json->functions.end())
  236. return true;
  237. if (current_interface_for_to_json->parent_name.is_empty())
  238. break;
  239. auto imported_interface_iterator = current_interface_for_to_json->imported_modules.find_if([&current_interface_for_to_json](IDL::Interface const& imported_interface) {
  240. return imported_interface.name == current_interface_for_to_json->parent_name;
  241. });
  242. // Inherited interfaces must have their IDL files imported.
  243. VERIFY(imported_interface_iterator != interface.imported_modules.end());
  244. current_interface_for_to_json = *imported_interface_iterator;
  245. }
  246. return false;
  247. }
  248. void EffectiveOverloadSet::remove_all_other_entries()
  249. {
  250. Vector<Item> new_items;
  251. new_items.append(m_items[*m_last_matching_item_index]);
  252. m_items = move(new_items);
  253. }
  254. }