瀏覽代碼

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.
Shannon Booth 1 年之前
父節點
當前提交
de2cad02aa
共有 1 個文件被更改,包括 45 次插入38 次删除
  1. 45 38
      Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

+ 45 - 38
Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

@@ -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