Types.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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::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. // FIXME: "is a union type with flattened member types including a dictionary type, or is a dictionary type,"
  64. if (includes_nullable_type() && other.includes_nullable_type())
  65. return false;
  66. // 2. If both types are either a union type or nullable union type, return true if each member type
  67. // of the one is distinguishable with each member type of the other, or false otherwise.
  68. if (is_union() && other.is_union()) {
  69. auto const& this_union = as_union();
  70. auto const& other_union = other.as_union();
  71. for (auto& this_member_type : this_union.member_types()) {
  72. for (auto& other_member_type : other_union.member_types()) {
  73. if (!this_member_type->is_distinguishable_from(other_member_type))
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. // 3. If one type is a union type or nullable union type, return true if each member type of the union
  80. // type is distinguishable with the non-union type, or false otherwise.
  81. if (is_union() || other.is_union()) {
  82. auto const& the_union = is_union() ? as_union() : other.as_union();
  83. auto const& non_union = is_union() ? other : *this;
  84. for (auto& member_type : the_union.member_types()) {
  85. if (!non_union.is_distinguishable_from(member_type))
  86. return false;
  87. }
  88. return true;
  89. }
  90. // 4. Consider the two "innermost" types derived by taking each type’s inner type if it is an annotated type,
  91. // and then taking its inner type inner type if the result is a nullable type. If these two innermost types
  92. // appear or are in categories appearing in the following table and there is a “●” mark in the corresponding
  93. // entry or there is a letter in the corresponding entry and the designated additional requirement below the
  94. // table is satisfied, then return true. Otherwise return false.
  95. auto const& this_innermost_type = innermost_type();
  96. auto const& other_innermost_type = other.innermost_type();
  97. enum class DistinguishabilityCategory {
  98. Undefined,
  99. Boolean,
  100. Numeric,
  101. BigInt,
  102. String,
  103. Object,
  104. Symbol,
  105. InterfaceLike,
  106. CallbackFunction,
  107. DictionaryLike,
  108. SequenceLike,
  109. __Count
  110. };
  111. // See https://webidl.spec.whatwg.org/#distinguishable-table
  112. // clang-format off
  113. static constexpr bool table[to_underlying(DistinguishabilityCategory::__Count)][to_underlying(DistinguishabilityCategory::__Count)] {
  114. {false, true, true, true, true, true, true, true, true, false, true},
  115. { true, false, true, true, true, true, true, true, true, true, true},
  116. { true, true, false, true, true, true, true, true, true, true, true},
  117. { true, true, true, false, true, true, true, true, true, true, true},
  118. { true, true, true, true, false, true, true, true, true, true, true},
  119. { true, true, true, true, true, false, true, false, false, false, false},
  120. { true, true, true, true, true, true, false, true, true, true, true},
  121. { true, true, true, true, true, false, true, false, true, true, true},
  122. { true, true, true, true, true, false, true, true, false, false, true},
  123. {false, true, true, true, true, false, true, true, false, false, true},
  124. { true, true, true, true, true, false, true, true, true, true, false},
  125. };
  126. // clang-format on
  127. auto determine_category = [](Type const& type) -> DistinguishabilityCategory {
  128. if (type.is_undefined())
  129. return DistinguishabilityCategory::Undefined;
  130. if (type.is_boolean())
  131. return DistinguishabilityCategory::Boolean;
  132. if (type.is_numeric())
  133. return DistinguishabilityCategory::Numeric;
  134. if (type.is_bigint())
  135. return DistinguishabilityCategory::BigInt;
  136. if (type.is_string())
  137. return DistinguishabilityCategory::String;
  138. if (type.is_object())
  139. return DistinguishabilityCategory::Object;
  140. if (type.is_symbol())
  141. return DistinguishabilityCategory::Symbol;
  142. // FIXME: InterfaceLike - see below
  143. // FIXME: CallbackFunction
  144. // FIXME: DictionaryLike
  145. // FIXME: Frozen array types are included in "sequence-like"
  146. if (type.is_sequence())
  147. return DistinguishabilityCategory::SequenceLike;
  148. // 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.
  149. dbgln("Unable to determine category for type named '{}', assuming it's an interface type.", type.name());
  150. return DistinguishabilityCategory::InterfaceLike;
  151. };
  152. auto this_distinguishability = determine_category(this_innermost_type);
  153. auto other_distinguishability = determine_category(other_innermost_type);
  154. if (this_distinguishability == DistinguishabilityCategory::InterfaceLike && other_distinguishability == DistinguishabilityCategory::InterfaceLike) {
  155. // The two identified interface-like types are not the same, and
  156. // FIXME: no single platform object implements both interface-like types.
  157. return this_innermost_type.name() != other_innermost_type.name();
  158. }
  159. return table[to_underlying(this_distinguishability)][to_underlying(other_distinguishability)];
  160. }
  161. // https://webidl.spec.whatwg.org/#dfn-json-types
  162. bool Type::is_json(Interface const& interface) const
  163. {
  164. // The JSON types are:
  165. // - numeric types,
  166. if (is_numeric())
  167. return true;
  168. // - boolean,
  169. if (is_boolean())
  170. return true;
  171. // - string types,
  172. if (is_string() || interface.enumerations.find(m_name) != interface.enumerations.end())
  173. return true;
  174. // - object,
  175. if (is_object())
  176. return true;
  177. // - nullable types whose inner type is a JSON type,
  178. // - annotated types whose inner type is a JSON type,
  179. // NOTE: We don't separate nullable and annotated into separate types.
  180. // - union types whose member types are JSON types,
  181. if (is_union()) {
  182. auto const& union_type = as_union();
  183. for (auto const& type : union_type.member_types()) {
  184. if (!type->is_json(interface))
  185. return false;
  186. }
  187. return true;
  188. }
  189. // - typedefs whose type being given a new name is a JSON type,
  190. auto typedef_iterator = interface.typedefs.find(m_name);
  191. if (typedef_iterator != interface.typedefs.end())
  192. return typedef_iterator->value.type->is_json(interface);
  193. // - sequence types whose parameterized type is a JSON type,
  194. // - frozen array types whose parameterized type is a JSON type,
  195. // - records where all of their values are JSON types,
  196. if (is_parameterized() && m_name.is_one_of("sequence", "FrozenArray", "record")) {
  197. auto const& parameterized_type = as_parameterized();
  198. for (auto const& parameter : parameterized_type.parameters()) {
  199. if (!parameter->is_json(interface))
  200. return false;
  201. }
  202. return true;
  203. }
  204. // - dictionary types where the types of all members declared on the dictionary and all its inherited dictionaries are JSON types,
  205. auto dictionary_iterator = interface.dictionaries.find(m_name);
  206. if (dictionary_iterator != interface.dictionaries.end()) {
  207. auto const& dictionary = dictionary_iterator->value;
  208. for (auto const& member : dictionary.members) {
  209. if (!member.type->is_json(interface))
  210. return false;
  211. }
  212. return true;
  213. }
  214. // - interface types that have a toJSON operation declared on themselves or one of their inherited interfaces.
  215. Optional<Interface const&> current_interface_for_to_json;
  216. if (m_name == interface.name) {
  217. current_interface_for_to_json = interface;
  218. } else {
  219. // NOTE: Interface types must have the IDL file of their interface imported.
  220. // Though the type name may not refer to an interface, so we don't assert this here.
  221. auto imported_interface_iterator = interface.imported_modules.find_if([this](IDL::Interface const& imported_interface) {
  222. return imported_interface.name == m_name;
  223. });
  224. if (imported_interface_iterator != interface.imported_modules.end())
  225. current_interface_for_to_json = *imported_interface_iterator;
  226. }
  227. while (current_interface_for_to_json.has_value()) {
  228. auto to_json_iterator = current_interface_for_to_json->functions.find_if([](IDL::Function const& function) {
  229. return function.name == "toJSON"sv;
  230. });
  231. if (to_json_iterator != current_interface_for_to_json->functions.end())
  232. return true;
  233. if (current_interface_for_to_json->parent_name.is_empty())
  234. break;
  235. auto imported_interface_iterator = current_interface_for_to_json->imported_modules.find_if([&current_interface_for_to_json](IDL::Interface const& imported_interface) {
  236. return imported_interface.name == current_interface_for_to_json->parent_name;
  237. });
  238. // Inherited interfaces must have their IDL files imported.
  239. VERIFY(imported_interface_iterator != interface.imported_modules.end());
  240. current_interface_for_to_json = *imported_interface_iterator;
  241. }
  242. return false;
  243. }
  244. void EffectiveOverloadSet::remove_all_other_entries()
  245. {
  246. Vector<Item> new_items;
  247. new_items.append(m_items[*m_last_matching_item_index]);
  248. m_items = move(new_items);
  249. }
  250. }