mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-14 21:42:19 +00:00
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:
parent
4a62268d73
commit
de2cad02aa
Notes:
sideshowbarker
2024-07-16 23:34:44 +09:00
Author: https://github.com/shannonbooth
Commit: de2cad02aa
Pull-request: https://github.com/SerenityOS/serenity/pull/24125
1 changed files with 45 additions and 38 deletions
|
@ -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)
|
static void generate_prototype_or_global_mixin_declarations(IDL::Interface const& interface, StringBuilder& builder)
|
||||||
{
|
{
|
||||||
SourceGenerator generator { builder };
|
SourceGenerator generator { builder };
|
||||||
|
@ -2602,44 +2646,7 @@ static void generate_prototype_or_global_mixin_declarations(IDL::Interface const
|
||||||
|
|
||||||
)~~~");
|
)~~~");
|
||||||
|
|
||||||
for (auto& it : interface.enumerations) {
|
generate_enumerations(interface.enumerations, builder);
|
||||||
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();
|
|
||||||
}
|
|
||||||
)~~~");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://webidl.spec.whatwg.org/#create-an-inheritance-stack
|
// https://webidl.spec.whatwg.org/#create-an-inheritance-stack
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue