LibWeb: Factor out a function to generate IDL enumerations

The prototype header generation was getting a bit long.

This is also a step towards generating code for IDL files only
containing an enum definition without any interface. In that case we
can't put the enum definitions alongside the prototype - there is no
prototype to speak of.
This commit is contained in:
Shannon Booth 2024-04-21 13:03:55 +12:00 committed by Andreas Kling
commit de2cad02aa
Notes: sideshowbarker 2024-07-16 23:34:44 +09:00

View file

@ -2545,6 +2545,50 @@ JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> @constructor_class@::constru
}
}
static void generate_enumerations(HashMap<ByteString, Enumeration> const& enumerations, StringBuilder& builder)
{
SourceGenerator generator { builder };
for (auto const& it : enumerations) {
if (!it.value.is_original_definition)
continue;
auto enum_generator = generator.fork();
enum_generator.set("enum.type.name", it.key);
enum_generator.append(R"~~~(
enum class @enum.type.name@ {
)~~~");
for (auto const& entry : it.value.translated_cpp_names) {
enum_generator.set("enum.entry", entry.value);
enum_generator.append(R"~~~(
@enum.entry@,
)~~~");
}
enum_generator.append(R"~~~(
};
)~~~");
enum_generator.append(R"~~~(
inline String idl_enum_to_string(@enum.type.name@ value)
{
switch (value) {
)~~~");
for (auto const& entry : it.value.translated_cpp_names) {
enum_generator.set("enum.entry", entry.value);
enum_generator.set("enum.string", entry.key);
enum_generator.append(R"~~~(
case @enum.type.name@::@enum.entry@:
return "@enum.string@"_string;
)~~~");
}
enum_generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
)~~~");
}
}
static void generate_prototype_or_global_mixin_declarations(IDL::Interface const& interface, StringBuilder& builder)
{
SourceGenerator generator { builder };
@ -2602,44 +2646,7 @@ static void generate_prototype_or_global_mixin_declarations(IDL::Interface const
)~~~");
for (auto& it : interface.enumerations) {
if (!it.value.is_original_definition)
continue;
auto enum_generator = generator.fork();
enum_generator.set("enum.type.name", it.key);
enum_generator.append(R"~~~(
enum class @enum.type.name@ {
)~~~");
for (auto& entry : it.value.translated_cpp_names) {
enum_generator.set("enum.entry", entry.value);
enum_generator.append(R"~~~(
@enum.entry@,
)~~~");
}
enum_generator.append(R"~~~(
};
)~~~");
enum_generator.append(R"~~~(
inline String idl_enum_to_string(@enum.type.name@ value)
{
switch (value) {
)~~~");
for (auto& entry : it.value.translated_cpp_names) {
enum_generator.set("enum.entry", entry.value);
enum_generator.set("enum.string", entry.key);
enum_generator.append(R"~~~(
case @enum.type.name@::@enum.entry@:
return "@enum.string@"_string;
)~~~");
}
enum_generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
)~~~");
}
generate_enumerations(interface.enumerations, builder);
}
// https://webidl.spec.whatwg.org/#create-an-inheritance-stack