mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-01 15:18:06 +00:00
LibWeb: Improve Enum generation in IDLGenerator
Generated enums have no underlying type specifier, this adds one It uses the smallest available, which is mostly u8
This commit is contained in:
parent
c276cc43f0
commit
4c54fa10ed
Notes:
github-actions[bot]
2025-03-04 16:36:05 +00:00
Author: https://github.com/Totto16
Commit: 4c54fa10ed
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3799
Reviewed-by: https://github.com/AtkinsSJ ✅
3 changed files with 45 additions and 29 deletions
|
@ -14,6 +14,7 @@
|
|||
#include "Namespaces.h"
|
||||
#include <AK/Array.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/NumericLimits.h>
|
||||
#include <AK/Queue.h>
|
||||
#include <AK/QuickSort.h>
|
||||
#include <LibIDL/Types.h>
|
||||
|
@ -2856,6 +2857,18 @@ JS::ThrowCompletionOr<GC::Ref<JS::Object>> @constructor_class@::construct([[mayb
|
|||
}
|
||||
}
|
||||
|
||||
static ByteString get_best_value_for_underlying_enum_type(size_t size)
|
||||
{
|
||||
|
||||
if (size < NumericLimits<u8>::max()) {
|
||||
return "u8";
|
||||
} else if (size < NumericLimits<u16>::max()) {
|
||||
return "u16";
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
static void generate_enumerations(HashMap<ByteString, Enumeration> const& enumerations, StringBuilder& builder)
|
||||
{
|
||||
SourceGenerator generator { builder };
|
||||
|
@ -2865,8 +2878,9 @@ static void generate_enumerations(HashMap<ByteString, Enumeration> const& enumer
|
|||
continue;
|
||||
auto enum_generator = generator.fork();
|
||||
enum_generator.set("enum.type.name", it.key);
|
||||
enum_generator.set("enum.underlying_type", get_best_value_for_underlying_enum_type(it.value.translated_cpp_names.size()));
|
||||
enum_generator.append(R"~~~(
|
||||
enum class @enum.type.name@ {
|
||||
enum class @enum.type.name@ : @enum.underlying_type@ {
|
||||
)~~~");
|
||||
for (auto const& entry : it.value.translated_cpp_names) {
|
||||
enum_generator.set("enum.entry", entry.value);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue