[Swift] Moves VTs from enums to structs to prevent empty enum generation

Moves VTs from enums to structs to prevent empty enum generation, which would usually cause a compilation error.
This commit is contained in:
mustiikhalil
2026-02-12 19:04:26 +01:00
committed by GitHub
parent c21bda1649
commit fcf75449b8
18 changed files with 1328 additions and 1289 deletions

View File

@@ -164,7 +164,7 @@ class SwiftGenerator : public BaseGenerator {
bool generate() {
code_.Clear();
code_.SetValue("ACCESS", "_accessor");
code_.SetValue("TABLEOFFSET", "VTOFFSET");
code_.SetValue("TABLEOFFSET", "VT");
code_ += "// " + std::string(FlatBuffersGeneratedWarning());
code_ += "// swiftlint:disable all";
code_ += "// swiftformat:disable all\n";
@@ -518,7 +518,7 @@ class SwiftGenerator : public BaseGenerator {
void GenTableAccessors(const StructDef& struct_def) {
// Generate field id constants.
if (struct_def.fields.vec.size() > 0) {
code_ += "private enum {{TABLEOFFSET}}: VOffset {";
code_ += "private struct {{TABLEOFFSET}} {";
Indent();
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
@@ -528,10 +528,8 @@ class SwiftGenerator : public BaseGenerator {
}
code_.SetValue("OFFSET_NAME", namer_.Variable(field));
code_.SetValue("OFFSET_VALUE", NumToString(field.value.offset));
code_ += "case {{OFFSET_NAME}} = {{OFFSET_VALUE}}";
code_ += "static let {{OFFSET_NAME}}: VOffset = {{OFFSET_VALUE}}";
}
code_ += "var v: Int32 { Int32(self.rawValue) }";
code_ += "var p: VOffset { self.rawValue }";
Outdent();
code_ += "}";
code_ += "";
@@ -620,7 +618,8 @@ class SwiftGenerator : public BaseGenerator {
if (should_generate_create) {
code_ += "{{ACCESS_TYPE}} static func create{{SHORT_STRUCTNAME}}(";
Indent();
code_ += "_ fbb: inout FlatBufferBuilder,";
code_ += "_ fbb: inout FlatBufferBuilder\\";
if (create_func_header.empty() == false) code_ += ",";
for (auto it = create_func_header.begin(); it < create_func_header.end();
++it) {
code_ += *it + "\\";
@@ -699,7 +698,7 @@ class SwiftGenerator : public BaseGenerator {
code_ += field.IsOptional() ? (optional_enum + "\\")
: (is_enum + ", def: {{CONSTANT}}\\");
code_ += ", at: {{TABLEOFFSET}}.{{OFFSET}}.p) }";
code_ += ", at: {{TABLEOFFSET}}.{{OFFSET}}) }";
const auto default_value =
IsEnum(field.value.type)
@@ -719,7 +718,7 @@ class SwiftGenerator : public BaseGenerator {
code_ +=
"{{VALUETYPE}}" + builder_string + "fbb.add(element: {{FIELDVAR}},\\";
code_ += field.IsOptional() ? "\\" : " def: {{CONSTANT}},";
code_ += " at: {{TABLEOFFSET}}.{{OFFSET}}.p) }";
code_ += " at: {{TABLEOFFSET}}.{{OFFSET}}) }";
create_func_header.push_back(
field_var + ": " + nullable_type + " = " +
(field.IsOptional() ? "nil" : default_value));
@@ -730,7 +729,7 @@ class SwiftGenerator : public BaseGenerator {
const auto create_struct =
"guard let {{FIELDVAR}} = {{FIELDVAR}} else { return };"
" fbb.create(struct: {{FIELDVAR}}, position: "
"{{TABLEOFFSET}}.{{OFFSET}}.p) }";
"{{TABLEOFFSET}}.{{OFFSET}}) }";
code_ += type + "?" + builder_string + create_struct;
/// Optional hard coded since structs are always optional
create_func_header.push_back(field_var + ": " + type +
@@ -747,8 +746,9 @@ class SwiftGenerator : public BaseGenerator {
(field.IsRequired() ? "" : " = Offset()"));
const auto reader_type =
IsStruct(field.value.type) && field.value.type.struct_def->fixed
? "structOffset: {{TABLEOFFSET}}.{{OFFSET}}.p) }"
: "offset: {{FIELDVAR}}, at: {{TABLEOFFSET}}.{{OFFSET}}.p) }";
? "structOffset: {{TABLEOFFSET}}.{{OFFSET}}) }"
: "offset: {{FIELDVAR}}, at: {{TABLEOFFSET}}.{{OFFSET}}) "
"}";
code_ += "Offset" + builder_string + "fbb.add(" + reader_type;
const auto vectortype = field.value.type.VectorType();
@@ -860,7 +860,7 @@ class SwiftGenerator : public BaseGenerator {
code_ += "{{ACCESS_TYPE}} var {{FIELDVAR}}SegmentArray: [UInt8]" +
is_required +
" { return "
"{{ACCESS}}.getVector(at: {{TABLEOFFSET}}.{{OFFSET}}.v) }";
"{{ACCESS}}.getVector(at: {{TABLEOFFSET}}.{{OFFSET}}) }";
break;
}
case BASE_TYPE_ARRAY:
@@ -894,7 +894,7 @@ class SwiftGenerator : public BaseGenerator {
code_ +=
"{{ACCESS_TYPE}} var {{FIELDVAR}}: "
"FlatbufferVector<{{VALUETYPE}}> "
"{ return {{ACCESS}}.vector(at: {{TABLEOFFSET}}.{{OFFSET}}.v, "
"{ return {{ACCESS}}.vector(at: {{TABLEOFFSET}}.{{OFFSET}}, "
"byteSize: {{SIZE}}) }";
}
@@ -911,7 +911,7 @@ class SwiftGenerator : public BaseGenerator {
code_ +=
"{{ACCESS_TYPE}} var {{FIELDVAR}}: "
"FlatbufferVector<{{VALUETYPE}}_Mutable> "
"{ return {{ACCESS}}.vector(at: {{TABLEOFFSET}}.{{OFFSET}}.v, "
"{ return {{ACCESS}}.vector(at: {{TABLEOFFSET}}.{{OFFSET}}, "
"byteSize: {{SIZE}}) }";
GenUnsafeBufferPointer(field);
return;
@@ -920,7 +920,8 @@ class SwiftGenerator : public BaseGenerator {
if (vectortype.base_type == BASE_TYPE_UNION) {
code_ +=
"{{ACCESS_TYPE}} var {{FIELDVAR}}: UnionFlatbufferVector "
"{ return {{ACCESS}}.unionVector(at: {{TABLEOFFSET}}.{{OFFSET}}.v, "
"{ return {{ACCESS}}.unionVector(at: "
"{{TABLEOFFSET}}.{{OFFSET}}, "
"byteSize: {{SIZE}}) }";
code_ +=
"{{ACCESS_TYPE}} func {{FIELDVAR}}<T: FlatbuffersInitializable>(at "
@@ -954,24 +955,21 @@ class SwiftGenerator : public BaseGenerator {
"{{ACCESS_TYPE}} func {{functionName}}<T>(_ body: "
"(UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T? { return "
"try "
"{{ACCESS}}.withUnsafePointerToSlice(at: {{TABLEOFFSET}}.{{OFFSET}}.v, "
"{{ACCESS}}.withUnsafePointerToSlice(at: "
"{{TABLEOFFSET}}.{{OFFSET}}, "
"body: body) }";
}
void GenerateCodingKeys(const StructDef& struct_def) {
code_ += "enum CodingKeys: String, CodingKey {";
Indent();
std::vector<std::string> GenerateCodingKeys(const StructDef& struct_def) {
std::vector<std::string> coding_keys;
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
const auto& field = **it;
if (field.deprecated) continue;
code_.SetValue("RAWVALUENAME", field.name);
code_.SetValue("FIELDVAR", namer_.Variable(field));
code_ += "case {{FIELDVAR}} = \"{{RAWVALUENAME}}\"";
coding_keys.push_back("case " + namer_.Variable(field) + " = \"" +
field.name + "\"");
}
Outdent();
code_ += "}";
return coding_keys;
}
void GenerateEncoderUnionBody(const FieldDef& field) {
@@ -1093,14 +1091,31 @@ class SwiftGenerator : public BaseGenerator {
GenOSVersionChecks();
code_ += "extension {{STRUCTNAME}}: Encodable {";
Indent();
code_ += "";
if (struct_def.fields.vec.empty() == false) GenerateCodingKeys(struct_def);
auto coding_keys = GenerateCodingKeys(struct_def);
if (coding_keys.empty() == false) {
code_ += "enum CodingKeys: String, CodingKey {";
Indent();
for (auto it = coding_keys.begin(); it != coding_keys.end(); ++it) {
const auto& field = *it;
code_ += field;
}
Outdent();
code_ += "}";
code_ += "";
}
code_ += "{{ACCESS_TYPE}} func encode(to encoder: Encoder) throws {";
Indent();
if (struct_def.fields.vec.empty() == false) GenerateEncoderBody(struct_def);
Outdent();
if (coding_keys.empty() == false) {
Indent();
GenerateEncoderBody(struct_def);
Outdent();
}
code_ += "}";
Outdent();
code_ += "}";
code_ += "";
@@ -1130,7 +1145,7 @@ class SwiftGenerator : public BaseGenerator {
}
code_ +=
"try _v.visit(field: {{TABLEOFFSET}}.{{OFFSET}}.p, fieldName: "
"try _v.visit(field: {{TABLEOFFSET}}.{{OFFSET}}, fieldName: "
"\"{{FIELDVAR}}\", required: {{ISREQUIRED}}, type: "
"{{VALUETYPE}}.self)";
}
@@ -1150,8 +1165,9 @@ class SwiftGenerator : public BaseGenerator {
code_.SetValue("VALUETYPE", namer_.NamespacedType(union_def));
code_.SetValue("FUNCTION_NAME", is_vector ? "visitUnionVector" : "visit");
code_ +=
"try _v.{{FUNCTION_NAME}}(unionKey: {{TABLEOFFSET}}.{{OFFSET}}Type.p, "
"unionField: {{TABLEOFFSET}}.{{OFFSET}}.p, unionKeyName: "
"try _v.{{FUNCTION_NAME}}(unionKey: "
"{{TABLEOFFSET}}.{{OFFSET}}Type, "
"unionField: {{TABLEOFFSET}}.{{OFFSET}}, unionKeyName: "
"\"{{FIELDVAR}}Type\", fieldName: \"{{FIELDVAR}}\", required: "
"{{ISREQUIRED}}, completion: { (verifier, key: {{VALUETYPE}}, pos) in";
Indent();
@@ -1893,7 +1909,7 @@ class SwiftGenerator : public BaseGenerator {
}
std::string GenOffset() {
return "let o = {{ACCESS}}.offset({{TABLEOFFSET}}.{{OFFSET}}.v); ";
return "let o = {{ACCESS}}.offset({{TABLEOFFSET}}.{{OFFSET}}); ";
}
std::string GenReaderMainBody(const std::string& optional = "") {