blob: 70228e5786cc0d19c554dfc5dd970a72f7e7f7b7 [file] [log] [blame]
James Kuszmaulf5eb4682023-09-22 17:16:59 -07001#include "aos/flatbuffers/static_flatbuffers.h"
2
3#include "absl/strings/str_cat.h"
4#include "absl/strings/str_format.h"
5#include "absl/strings/str_join.h"
6#include "absl/strings/str_replace.h"
7#include "glog/logging.h"
8
9#include "aos/flatbuffers/static_table.h"
10#include "aos/json_to_flatbuffer.h"
11namespace aos::fbs {
12namespace {
13// Represents a given field within a type with all of the data that we actually
14// care about.
15struct FieldData {
16 // Field name.
17 std::string name;
18 // Whether it is an inline data type (scalar/struct vs vector/table/string).
19 bool is_inline = true;
20 // Whether this is a struct or not.
21 bool is_struct = false;
James Kuszmaul6be41022023-12-20 11:55:28 -080022 // Whether this is a repeated type (vector or string).
23 bool is_repeated = false;
James Kuszmaulf5eb4682023-09-22 17:16:59 -070024 // Full C++ type of this field.
25 std::string full_type = "";
26 // Full flatbuffer type for this field.
27 // Only specified for Tables.
28 std::optional<std::string> fbs_type = std::nullopt;
29 // Size of this field in the inline field data (i.e., size of the field for
30 // is_inline fields; 4 bytes for the offset for vectors/tables/strings).
31 size_t inline_size = 0u;
32 // Alignment of the inline data.
33 size_t inline_alignment = 0u;
34 // vtable offset of the field.
35 size_t vtable_offset = 0u;
36};
37
38const reflection::Object *GetObject(const reflection::Schema *schema,
39 const int index) {
40 return (index == -1) ? schema->root_table() : schema->objects()->Get(index);
41}
42
43// Returns the flatbuffer field attribute with the specified name, if available.
44std::optional<std::string_view> GetAttribute(const reflection::Field *field,
45 std::string_view attribute) {
46 if (!field->has_attributes()) {
47 return std::nullopt;
48 }
49 const reflection::KeyValue *kv =
50 field->attributes()->LookupByKey(attribute.data());
51 if (kv == nullptr) {
52 return std::nullopt;
53 }
54 return kv->value()->string_view();
55}
56
57// Returns the implied value of an attribute that specifies a length (i.e., 0 if
58// the attribute is not specified; the integer value otherwise).
59int64_t GetLengthAttributeOrZero(const reflection::Field *field,
60 std::string_view attribute) {
61 std::optional<std::string_view> str = GetAttribute(field, attribute);
62 if (!str.has_value()) {
63 return 0;
64 }
65 int64_t value;
66 CHECK(absl::SimpleAtoi(str.value(), &value))
67 << ": Field " << field->name()->string_view()
68 << " must specify a positive integer for the " << attribute
69 << " attribute. Got \"" << str.value() << "\".";
70 CHECK_LE(0, value) << ": Field " << field->name()->string_view()
71 << " must have a non-negative " << attribute << ".";
72 return value;
73}
74
75const std::string ScalarCppType(const reflection::BaseType type) {
76 switch (type) {
77 case reflection::BaseType::Bool:
78 return "bool";
79 case reflection::BaseType::Byte:
80 return "int8_t";
81 case reflection::BaseType::UByte:
82 return "uint8_t";
83 case reflection::BaseType::Short:
84 return "int16_t";
85 case reflection::BaseType::UShort:
86 return "uint16_t";
87 case reflection::BaseType::Int:
88 return "int32_t";
89 case reflection::BaseType::UInt:
90 return "uint32_t";
91 case reflection::BaseType::Long:
92 return "int64_t";
93 case reflection::BaseType::ULong:
94 return "uint64_t";
95 case reflection::BaseType::Float:
96 return "float";
97 case reflection::BaseType::Double:
98 return "double";
99 case reflection::BaseType::UType:
100 case reflection::BaseType::String:
101 case reflection::BaseType::Vector:
102 case reflection::BaseType::Obj:
103 case reflection::BaseType::None:
104 case reflection::BaseType::Union:
105 case reflection::BaseType::Array:
106 case reflection::BaseType::MaxBaseType:
107 LOG(FATAL) << ": Type " << reflection::EnumNameBaseType(type)
108 << " not a scalar.";
109 }
110 LOG(FATAL) << "Unreachable";
111}
112
113const std::string FlatbufferNameToCppName(const std::string_view input) {
114 return absl::StrReplaceAll(input, {{".", "::"}});
115}
116
117const std::string AosNameForRawFlatbuffer(const std::string_view base_name) {
118 return absl::StrCat(base_name, "Static");
119}
120
121const std::string IncludePathForFbs(
122 std::string_view fbs_file, std::string_view include_suffix = "static") {
123 fbs_file.remove_suffix(4);
124 return absl::StrCat(fbs_file, "_", include_suffix, ".h");
125}
126
127std::string ScalarOrEnumType(const reflection::Schema *schema,
128 const reflection::BaseType type, int index) {
129 return (index < 0) ? ScalarCppType(type)
130 : FlatbufferNameToCppName(
131 schema->enums()->Get(index)->name()->string_view());
132}
133
134void PopulateTypeData(const reflection::Schema *schema,
135 const reflection::Field *field_fbs, FieldData *field) {
136 VLOG(1) << aos::FlatbufferToJson(field_fbs);
137 const reflection::Type *type = field_fbs->type();
138 field->inline_size = type->base_size();
139 field->inline_alignment = type->base_size();
140 switch (type->base_type()) {
141 case reflection::BaseType::Bool:
142 case reflection::BaseType::Byte:
143 case reflection::BaseType::UByte:
144 case reflection::BaseType::Short:
145 case reflection::BaseType::UShort:
146 case reflection::BaseType::Int:
147 case reflection::BaseType::UInt:
148 case reflection::BaseType::Long:
149 case reflection::BaseType::ULong:
150 case reflection::BaseType::Float:
151 case reflection::BaseType::Double:
152 // We have a scalar field, so things are relatively
153 // straightforwards.
154 field->is_inline = true;
155 field->is_struct = false;
James Kuszmaul6be41022023-12-20 11:55:28 -0800156 field->is_repeated = false;
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700157 field->full_type =
158 ScalarOrEnumType(schema, type->base_type(), type->index());
159 return;
160 case reflection::BaseType::String: {
161 field->is_inline = false;
162 field->is_struct = false;
James Kuszmaul6be41022023-12-20 11:55:28 -0800163 field->is_repeated = true;
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700164 field->full_type =
165 absl::StrFormat("::aos::fbs::String<%d>",
166 GetLengthAttributeOrZero(field_fbs, "static_length"));
167 return;
168 }
169 case reflection::BaseType::Vector: {
170 // We need to extract the name of the elements of the vector.
171 std::string element_type;
172 bool elements_are_inline = true;
James Kuszmaul6be41022023-12-20 11:55:28 -0800173 field->is_repeated = true;
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700174 if (type->base_type() == reflection::BaseType::Vector) {
175 switch (type->element()) {
176 case reflection::BaseType::Obj: {
177 const reflection::Object *element_object =
178 GetObject(schema, type->index());
179 element_type =
180 FlatbufferNameToCppName(element_object->name()->string_view());
181 elements_are_inline = element_object->is_struct();
182 if (!element_object->is_struct()) {
183 element_type = AosNameForRawFlatbuffer(element_type);
184 field->fbs_type = element_object->name()->string_view();
185 }
186 break;
187 }
188 case reflection::BaseType::String:
189 element_type =
190 absl::StrFormat("::aos::fbs::String<%d>",
191 GetLengthAttributeOrZero(
192 field_fbs, "static_vector_string_length"));
193 elements_are_inline = false;
194 break;
195 case reflection::BaseType::Vector:
196 LOG(FATAL) << "Vectors of vectors do not exist in flatbuffers.";
197 default:
198 element_type =
199 ScalarOrEnumType(schema, type->element(), type->index());
200 };
201 }
202 field->is_inline = false;
203 field->is_struct = false;
204 field->full_type =
205 absl::StrFormat("::aos::fbs::Vector<%s, %d, %s, %s>", element_type,
206 GetLengthAttributeOrZero(field_fbs, "static_length"),
207 elements_are_inline ? "true" : "false",
208 GetAttribute(field_fbs, "force_align").value_or("0"));
209 return;
210 }
211 case reflection::BaseType::Obj: {
212 const reflection::Object *object = GetObject(schema, type->index());
213 field->is_inline = object->is_struct();
214 field->is_struct = object->is_struct();
James Kuszmaul6be41022023-12-20 11:55:28 -0800215 field->is_repeated = false;
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700216 const std::string flatbuffer_name =
217 FlatbufferNameToCppName(object->name()->string_view());
218 if (field->is_inline) {
219 field->full_type = flatbuffer_name;
220 field->inline_size = object->bytesize();
221 field->inline_alignment = object->minalign();
222 } else {
223 field->fbs_type = object->name()->string_view();
224 field->full_type = AosNameForRawFlatbuffer(flatbuffer_name);
225 }
226 return;
227 }
228 case reflection::BaseType::None:
229 case reflection::BaseType::UType:
230 case reflection::BaseType::Union:
231 case reflection::BaseType::Array:
232 case reflection::BaseType::MaxBaseType:
233 LOG(FATAL) << ": Type " << reflection::EnumNameBaseType(type->base_type())
234 << " not supported currently.";
235 };
236}
237
238std::string MakeMoveConstructor(std::string_view type_name) {
239 return absl::StrFormat(R"code(
240 // We need to provide a MoveConstructor to allow this table to be
241 // used inside of vectors, but we do not want it readily available to
242 // users. See TableMover for more details.
243 %s(%s &&) = default;
244 friend struct ::aos::fbs::internal::TableMover<%s>;
245 )code",
246 type_name, type_name, type_name);
247}
248
249std::string MakeConstructor(std::string_view type_name) {
250 const std::string constructor_body =
251 R"code(
252 CHECK_EQ(buffer.size(), kSize);
253 CHECK_EQ(0u, reinterpret_cast<size_t>(buffer.data()) % kAlign);
254 PopulateVtable();
255)code";
256 return absl::StrFormat(R"code(
257 // Constructors for creating a flatbuffer object.
258 // Users should typically use the Builder class to create these objects,
259 // in order to allow it to populate the root table offset.
260
261 // The buffer provided to these constructors should be aligned to kAlign
262 // and kSize in length.
263 // The parent/allocator may not be nullptr.
264 %s(std::span<uint8_t> buffer, ::aos::fbs::ResizeableObject *parent) : Table(buffer, parent) {
265 %s
266 }
267 %s(std::span<uint8_t> buffer, ::aos::fbs::Allocator *allocator) : Table(buffer, allocator) {
268 %s
269 }
270 %s(std::span<uint8_t> buffer, ::std::unique_ptr<::aos::fbs::Allocator> allocator) : Table(buffer, ::std::move(allocator)) {
271 %s
272 }
273)code",
274 type_name, constructor_body, type_name,
275 constructor_body, type_name, constructor_body);
276}
277
278std::string MemberName(const FieldData &data) {
279 return absl::StrCat(data.name, "_");
280}
281
282std::string ObjectAbsoluteOffsetName(const FieldData &data) {
283 return absl::StrCat("object_absolute_offset_", data.name);
284}
285
286std::string InlineAbsoluteOffsetName(const FieldData &data) {
287 return absl::StrCat("kInlineAbsoluteOffset_", data.name);
288}
289
290// Generate the clear_* method for the requested field.
291std::string MakeClearer(const FieldData &field) {
292 std::string logical_clearer;
293 if (!field.is_inline) {
294 logical_clearer = MemberName(field) + ".reset();";
295 }
296 return absl::StrFormat(R"code(
297 // Clears the %s field. This will cause has_%s() to return false.
298 void clear_%s() {
299 %s
300 ClearField(%s, %d, %d);
301 }
302 )code",
303 field.name, field.name, field.name, logical_clearer,
304 InlineAbsoluteOffsetName(field), field.inline_size,
305 field.vtable_offset);
306}
307
308// Generate the has_* method for the requested field.
309std::string MakeHaser(const FieldData &field) {
310 return absl::StrFormat(R"code(
311 // Returns true if the %s field is set and can be accessed.
312 bool has_%s() const {
313 return AsFlatbuffer().has_%s();
314 }
315 )code",
316 field.name, field.name, field.name);
317}
318
319// Generates the accessors for fields which are stored inline in the flatbuffer
320// table (scalars, structs, and enums) .
321std::string MakeInlineAccessors(const FieldData &field,
322 const size_t inline_absolute_offset) {
323 CHECK_EQ(inline_absolute_offset % field.inline_alignment, 0u)
324 << ": Unaligned field " << field.name << " on " << field.full_type
325 << " with inline offset of " << inline_absolute_offset
326 << " and alignment of " << field.inline_alignment;
327 const std::string setter =
328 absl::StrFormat(R"code(
329 // Sets the %s field, causing it to be populated if it is not already.
330 // This will populate the field even if the specified value is the default.
331 void set_%s(const %s &value) {
332 SetField<%s>(%s, %d, value);
333 }
334 )code",
335 field.name, field.name, field.full_type, field.full_type,
336 InlineAbsoluteOffsetName(field), field.vtable_offset);
337 const std::string getters = absl::StrFormat(
338 R"code(
339 // Returns the value of %s if set; nullopt otherwise.
340 std::optional<%s> %s() const {
341 return has_%s() ? std::make_optional(Get<%s>(%s)) : std::nullopt;;
342 }
343 // Returns a pointer to modify the %s field.
344 // The pointer may be invalidated by mutations/movements of the underlying buffer.
345 // Returns nullptr if the field is not set.
346 %s* mutable_%s() {
347 return has_%s() ? MutableGet<%s>(%s) : nullptr;
348 }
349 )code",
350 field.name, field.full_type, field.name, field.name, field.full_type,
351 InlineAbsoluteOffsetName(field), field.name, field.full_type, field.name,
352 field.name, field.full_type, InlineAbsoluteOffsetName(field));
353 const std::string clearer = MakeClearer(field);
354 return setter + getters + clearer + MakeHaser(field);
355}
356
357// Generates the accessors for fields which are not inline fields and have an
358// offset to the actual field content stored inline in the flatbuffer table.
359std::string MakeOffsetDataAccessors(const FieldData &field) {
360 const std::string setter = absl::StrFormat(
361 R"code(
362 // Creates an empty object for the %s field, which you can
363 // then populate/modify as desired.
364 // The field must not be populated yet.
365 %s* add_%s() {
366 CHECK(!%s.has_value());
367 constexpr size_t kVtableIndex = %d;
368 // Construct the *Static object that we will use for managing this subtable.
369 %s.emplace(BufferForObject(%s, %s::kSize, kAlign), this);
370 // Actually set the appropriate fields in the flatbuffer memory itself.
371 SetField<::flatbuffers::uoffset_t>(%s, kVtableIndex, %s + %s::kOffset - %s);
372 return &%s.value().t;
373 }
374 )code",
375 field.name, field.full_type, field.name, MemberName(field),
376 field.vtable_offset, MemberName(field), ObjectAbsoluteOffsetName(field),
377 field.full_type, InlineAbsoluteOffsetName(field),
378 ObjectAbsoluteOffsetName(field), field.full_type,
379 InlineAbsoluteOffsetName(field), MemberName(field));
380 const std::string getters = absl::StrFormat(
381 R"code(
382 // Returns a pointer to the %s field, if set. nullptr otherwise.
383 const %s* %s() const {
384 return %s.has_value() ? &%s.value().t : nullptr;
385 }
386 %s* mutable_%s() {
387 return %s.has_value() ? &%s.value().t : nullptr;
388 }
389 )code",
390 field.name, field.full_type, field.name, MemberName(field),
391 MemberName(field), field.full_type, field.name, MemberName(field),
392 MemberName(field));
393 return setter + getters + MakeClearer(field) + MakeHaser(field);
394}
395
396std::string MakeAccessors(const FieldData &field,
397 size_t inline_absolute_offset) {
398 return field.is_inline ? MakeInlineAccessors(field, inline_absolute_offset)
399 : MakeOffsetDataAccessors(field);
400}
401
402std::string MakeMembers(const FieldData &field,
403 std::string_view offset_data_absolute_offset,
404 size_t inline_absolute_offset) {
405 if (field.is_inline) {
406 return absl::StrFormat(
407 R"code(
408 // Offset from the start of the buffer to the inline data for the %s field.
409 static constexpr size_t %s = %d;
410 )code",
411 field.name, InlineAbsoluteOffsetName(field), inline_absolute_offset);
412 } else {
413 return absl::StrFormat(
414 R"code(
415 // Members relating to the %s field.
416 //
417 // *Static object used for managing this subtable. Will be nullopt
418 // when the field is not populated.
419 // We use the TableMover to be able to make this object moveable.
420 std::optional<::aos::fbs::internal::TableMover<%s>> %s;
421 // Offset from the start of the buffer to the start of the actual
422 // data for this field. Will be updated even when the table is not
423 // populated, so that we know where to construct it when requested.
424 size_t %s = %s;
425 // Offset from the start of the buffer to the offset in the inline data for
426 // this field.
427 static constexpr size_t %s = %d;
428 )code",
429 field.name, field.full_type, MemberName(field),
430 ObjectAbsoluteOffsetName(field), offset_data_absolute_offset,
431 InlineAbsoluteOffsetName(field), inline_absolute_offset);
432 }
433}
434
435std::string MakeFullClearer(const std::vector<FieldData> &fields) {
436 std::vector<std::string> clearers;
437 for (const FieldData &field : fields) {
438 clearers.emplace_back(absl::StrFormat("clear_%s();", field.name));
439 }
440 return absl::StrFormat(R"code(
441 // Clears every field of the table, removing any existing state.
442 void Clear() { %s })code",
443 absl::StrJoin(clearers, "\n"));
444}
445
James Kuszmaul6be41022023-12-20 11:55:28 -0800446// Creates the FromFlatbuffer() method that copies from a flatbuffer object API
447// object (i.e., the FlatbufferT types).
448std::string MakeObjectCopier(const std::vector<FieldData> &fields) {
449 std::vector<std::string> copiers;
450 for (const FieldData &field : fields) {
451 if (field.is_struct) {
452 // Structs are stored as unique_ptr<FooStruct>
453 copiers.emplace_back(absl::StrFormat(R"code(
454 if (other.%s) {
455 set_%s(*other.%s);
456 }
457 )code",
458 field.name, field.name, field.name));
459 } else if (field.is_inline) {
460 // Inline non-struct elements are stored as FooType.
461 copiers.emplace_back(absl::StrFormat(R"code(
462 set_%s(other.%s);
463 )code",
464 field.name, field.name));
465 } else if (field.is_repeated) {
466 // strings are stored as std::string's.
467 // vectors are stored as std::vector's.
468 copiers.emplace_back(absl::StrFormat(R"code(
469 // Unconditionally copy strings/vectors, even if it will just end up
470 // being 0-length (this maintains consistency with the flatbuffer Pack()
471 // behavior).
472 if (!CHECK_NOTNULL(add_%s())->FromFlatbuffer(other.%s)) {
473 // Fail if we were unable to copy (e.g., if we tried to copy in a long
474 // vector and do not have the space for it).
475 return false;
476 }
477 )code",
478 field.name, field.name));
479 } else {
480 // Tables are stored as unique_ptr<FooTable>
481 copiers.emplace_back(absl::StrFormat(R"code(
482 if (other.%s) {
483 if (!CHECK_NOTNULL(add_%s())->FromFlatbuffer(*other.%s)) {
484 // Fail if we were unable to copy (e.g., if we tried to copy in a long
485 // vector and do not have the space for it).
486 return false;
487 }
488 }
489 )code",
490 field.name, field.name, field.name));
491 }
492 }
493 return absl::StrFormat(
494 R"code(
495 // Copies the contents of the provided flatbuffer into this flatbuffer,
496 // returning true on success.
497 // Because the Flatbuffer Object API does not provide any concept of an
498 // optionally populated scalar field, all scalar fields will be populated
499 // after a call to FromFlatbufferObject().
500 // This is a deep copy, and will call FromFlatbufferObject on
501 // any constituent objects.
502 [[nodiscard]] bool FromFlatbuffer(const Flatbuffer::NativeTableType &other) {
503 Clear();
504 %s
505 return true;
506 }
507 [[nodiscard]] bool FromFlatbuffer(const flatbuffers::unique_ptr<Flatbuffer::NativeTableType>& other) {
508 return FromFlatbuffer(*other);
509 }
510)code",
511 absl::StrJoin(copiers, "\n"));
512}
513
514// Creates the FromFlatbuffer() method that copies from an actual flatbuffer
515// object.
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700516std::string MakeCopier(const std::vector<FieldData> &fields) {
517 std::vector<std::string> copiers;
518 for (const FieldData &field : fields) {
519 if (field.is_struct) {
520 copiers.emplace_back(absl::StrFormat(R"code(
521 if (other->has_%s()) {
522 set_%s(*other->%s());
523 }
524 )code",
525 field.name, field.name, field.name));
526 } else if (field.is_inline) {
527 copiers.emplace_back(absl::StrFormat(R"code(
528 if (other->has_%s()) {
529 set_%s(other->%s());
530 }
531 )code",
532 field.name, field.name, field.name));
533 } else {
534 copiers.emplace_back(absl::StrFormat(R"code(
535 if (other->has_%s()) {
536 if (!CHECK_NOTNULL(add_%s())->FromFlatbuffer(other->%s())) {
537 // Fail if we were unable to copy (e.g., if we tried to copy in a long
538 // vector and do not have the space for it).
539 return false;
540 }
541 }
542 )code",
543 field.name, field.name, field.name));
544 }
545 }
546 return absl::StrFormat(
547 R"code(
548 // Copies the contents of the provided flatbuffer into this flatbuffer,
549 // returning true on success.
James Kuszmaul710883b2023-12-14 14:34:48 -0800550 // This is a deep copy, and will call FromFlatbuffer on any constituent
551 // objects.
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700552 [[nodiscard]] bool FromFlatbuffer(const Flatbuffer *other) {
553 Clear();
554 %s
555 return true;
556 }
557)code",
558 absl::StrJoin(copiers, "\n"));
559}
560
561std::string MakeSubObjectList(const std::vector<FieldData> &fields) {
562 size_t num_object_fields = 0;
563 std::vector<std::string> object_offsets;
564 std::vector<std::string> objects;
565 std::vector<std::string> inline_offsets;
566 for (const FieldData &field : fields) {
567 if (!field.is_inline) {
568 ++num_object_fields;
569 object_offsets.push_back(
570 absl::StrFormat("&%s", ObjectAbsoluteOffsetName(field)));
571 objects.push_back(absl::StrFormat("&%s->t", MemberName(field)));
572 inline_offsets.push_back(InlineAbsoluteOffsetName(field));
573 }
574 }
575 if (num_object_fields == 0) {
576 return R"code(
577 // This object has no non-inline subobjects, so we don't have to do anything special.
578 size_t NumberOfSubObjects() const final { return 0; }
579 using ::aos::fbs::ResizeableObject::SubObject;
580 SubObject GetSubObject(size_t) final { LOG(FATAL) << "No subobjects."; }
581 )code";
582 }
583 return absl::StrFormat(R"code(
584 size_t NumberOfSubObjects() const final { return %d; }
585 using ::aos::fbs::ResizeableObject::SubObject;
586 SubObject GetSubObject(size_t index) final {
587 SubObject object;
588 // Note: The below arrays are local variables rather than class members to
589 // avoid having to deal with what happens to them if the object is moved.
590
591 // Array of the members that we use for tracking where the buffers for
592 // each subobject belong.
593 // Pointers because these may need to be modified when memory is
594 // inserted into the buffer.
595 const std::array<size_t*, %d> subobject_object_offsets{%s};
596 // Actual subobjects; note that the pointers will be invalid when the
597 // field is not populated.
598 const std::array<::aos::fbs::ResizeableObject*, %d> subobject_objects{%s};
599 // Absolute offsets from the start of the buffer to where the inline
600 // entry is for each table. These offsets do not need to change at
601 // runtime (because memory is never inserted into the start of
602 // a given table), but the offsets pointed to by these offsets
603 // may need to be updated.
604 const std::array<size_t, %d> subobject_inline_offsets{%s};
605 object.inline_entry = MutableGet<::flatbuffers::uoffset_t>(subobject_inline_offsets[index]);
606 object.object = (*object.inline_entry == 0) ? nullptr : subobject_objects[index];
607 object.absolute_offset = subobject_object_offsets[index];
608 return object;
609 }
610 )code",
611 num_object_fields, num_object_fields,
612 absl::StrJoin(object_offsets, ", "), num_object_fields,
613 absl::StrJoin(objects, ", "), num_object_fields,
614 absl::StrJoin(inline_offsets, ", "));
615}
616
617std::string AlignCppString(const std::string_view expression,
618 const std::string_view alignment) {
619 return absl::StrFormat("::aos::fbs::PaddedSize(%s, %s)", expression,
620 alignment);
621}
622
623std::string MakeInclude(std::string_view path, bool system = false) {
624 return absl::StrFormat("#include %s%s%s\n", system ? "<" : "\"", path,
625 system ? ">" : "\"");
626}
627
628} // namespace
629GeneratedObject GenerateCodeForObject(const reflection::Schema *schema,
630 int object_index) {
631 return GenerateCodeForObject(schema, GetObject(schema, object_index));
632}
633GeneratedObject GenerateCodeForObject(const reflection::Schema *schema,
634 const reflection::Object *object) {
635 std::vector<FieldData> fields;
636 for (const reflection::Field *field_fbs : *object->fields()) {
637 if (field_fbs->deprecated()) {
638 // Don't codegen anything for deprecated fields.
639 continue;
640 }
641 FieldData field{.name = field_fbs->name()->str(),
642 .vtable_offset = field_fbs->offset()};
643 PopulateTypeData(schema, field_fbs, &field);
644 fields.push_back(field);
645 }
646 const size_t nominal_min_align = object->minalign();
647 std::string out_of_line_member_size = "";
648 // inline_absolute_offset tracks the current position of the inline table
649 // contents so that we can assign static offsets to each field.
650 size_t inline_absolute_offset = sizeof(soffset_t);
651 // offset_data_relative_offset tracks the current size of the various
652 // sub-tables/vectors/strings that get stored at the end of the buffer.
653 // For simplicity, the offset data will start at a fully aligned offset
654 // (which may be larger than the soffset_t at the start of the table).
655 // Note that this is a string because it's irritating to actually pipe the
656 // numbers for size/alignment up here, so we just accumulate them here and
657 // then write the expression directly into the C++.
658 std::string offset_data_relative_offset = "0";
659 const std::string offset_data_start_expression =
660 "::aos::fbs::PaddedSize(kVtableStart + kVtableSize, kAlign)";
661 std::string accessors;
662 std::string members;
663 std::set<std::string> includes = {
664 MakeInclude("optional", true),
665 MakeInclude("aos/flatbuffers/static_table.h"),
666 MakeInclude("aos/flatbuffers/static_vector.h")};
667 for (const reflection::SchemaFile *file : *schema->fbs_files()) {
668 includes.insert(
669 MakeInclude(IncludePathForFbs(file->filename()->string_view())));
670 includes.insert(MakeInclude(
671 IncludePathForFbs(file->filename()->string_view(), "generated")));
672 for (const flatbuffers::String *included : *file->included_filenames()) {
673 includes.insert(MakeInclude(IncludePathForFbs(included->string_view())));
674 }
675 }
676 std::vector<std::string> alignments;
677 std::set<std::string> subobject_names;
678 for (const FieldData &field : fields) {
679 inline_absolute_offset =
680 PaddedSize(inline_absolute_offset, field.inline_alignment);
681 if (!field.is_inline) {
682 // All sub-fields will get aligned to the parent alignment. This makes
683 // some book-keeping a bit easier, at the expense of some gratuitous
684 // padding.
685 offset_data_relative_offset =
686 AlignCppString(offset_data_relative_offset, "kAlign");
687 alignments.push_back(field.full_type + "::kAlign");
688 } else {
689 alignments.push_back(std::to_string(field.inline_alignment));
690 }
691 const std::string offset_data_absolute_offset =
692 offset_data_start_expression + " + " + offset_data_relative_offset;
693 accessors += MakeAccessors(field, inline_absolute_offset);
694 members +=
695 MakeMembers(field, offset_data_absolute_offset, inline_absolute_offset);
696
697 inline_absolute_offset += field.inline_size;
698 if (!field.is_inline) {
699 offset_data_relative_offset +=
700 absl::StrFormat(" + %s::kSize", field.full_type);
701 }
702 if (field.fbs_type.has_value()) {
703 // Is this not getting populate for the root schema?
704 subobject_names.insert(field.fbs_type.value());
705 }
706 }
707
James Kuszmaula75cd7c2023-12-07 15:52:51 -0800708 const std::string alignment = absl::StrCat(
709 "static constexpr size_t kAlign = std::max<size_t>({kMinAlign, ",
710 absl::StrJoin(alignments, ", "), "});\n");
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700711 const std::string size =
712 absl::StrCat("static constexpr size_t kSize = ",
713 AlignCppString(offset_data_start_expression + " + " +
714 offset_data_relative_offset,
715 "kAlign"),
716 ";");
717 const size_t inline_data_size = inline_absolute_offset;
718 const std::string constants = absl::StrFormat(
719 R"code(
720 // Space taken up by the inline portion of the flatbuffer table data, in bytes.
721 static constexpr size_t kInlineDataSize = %d;
722 // Space taken up by the vtable for this object, in bytes.
723 static constexpr size_t kVtableSize = sizeof(::flatbuffers::voffset_t) * (2 + %d);
724 // Offset from the start of the internal memory buffer to the start of the vtable.
725 static constexpr size_t kVtableStart = ::aos::fbs::PaddedSize(kInlineDataSize, alignof(::flatbuffers::voffset_t));
726 // Required alignment of this object. The buffer that this object gets constructed
727 // into must be aligned to this value.
728 %s
729 // Nominal size of this object, in bytes. The object may grow beyond this size,
730 // but will always start at this size and so the initial buffer must match
731 // this size.
732 %s
733 static_assert(%d <= kAlign, "Flatbuffer schema minalign should not exceed our required alignment.");
734 // Offset from the start of the memory buffer to the start of any out-of-line data (subtables,
735 // vectors, strings).
736 static constexpr size_t kOffsetDataStart = %s;
737 // Size required for a buffer that includes a root table offset at the start.
738 static constexpr size_t kRootSize = ::aos::fbs::PaddedSize(kSize + sizeof(::flatbuffers::uoffset_t), kAlign);
739 // Minimum size required to build this flatbuffer in an entirely unaligned buffer
740 // (including the root table offset). Made to be a multiple of kAlign for convenience.
741 static constexpr size_t kUnalignedBufferSize = kRootSize + kAlign;
742 // Offset at which the table vtable offset occurs. This is only needed for vectors.
743 static constexpr size_t kOffset = 0;
744 // Various overrides to support the Table parent class.
745 size_t FixedVtableOffset() const final { return kVtableStart; }
746 size_t VtableSize() const final { return kVtableSize; }
747 size_t InlineTableSize() const final { return kInlineDataSize; }
748 size_t OffsetDataStart() const final { return kOffsetDataStart; }
749 size_t Alignment() const final { return kAlign; }
750 // Exposes the name of the flatbuffer type to allow interchangeable use
751 // of the Flatbuffer and FlatbufferStatic types in various AOS methods.
752 static const char *GetFullyQualifiedName() { return Flatbuffer::GetFullyQualifiedName(); }
753)code",
754 inline_data_size, object->fields()->size(), alignment, size,
755 nominal_min_align, offset_data_start_expression);
756 const std::string_view fbs_type_name = object->name()->string_view();
757 const std::string type_namespace = FlatbufferNameToCppName(
758 fbs_type_name.substr(0, fbs_type_name.find_last_of(".")));
759 const std::string type_name = AosNameForRawFlatbuffer(
760 fbs_type_name.substr(fbs_type_name.find_last_of(".") + 1));
761 const std::string object_code = absl::StrFormat(
762 R"code(
763namespace %s {
764class %s : public ::aos::fbs::Table {
765 public:
766 // The underlying "raw" flatbuffer type for this type.
767 typedef %s Flatbuffer;
James Kuszmaul6be41022023-12-20 11:55:28 -0800768 typedef flatbuffers::unique_ptr<Flatbuffer::NativeTableType> FlatbufferObjectType;
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700769 // Returns this object as a flatbuffer type. This reference may not be valid
770 // following mutations to the underlying flatbuffer, due to how memory may get
771 // may get moved around.
772 const Flatbuffer &AsFlatbuffer() const { return *GetFlatbuffer<Flatbuffer>(); }
773%s
774%s
775 virtual ~%s() {}
776%s
777%s
778%s
James Kuszmaul6be41022023-12-20 11:55:28 -0800779%s
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700780 private:
781%s
782%s
783%s
784};
785}
786 )code",
787 type_namespace, type_name, FlatbufferNameToCppName(fbs_type_name),
788 constants, MakeConstructor(type_name), type_name, accessors,
James Kuszmaul6be41022023-12-20 11:55:28 -0800789 MakeFullClearer(fields), MakeCopier(fields), MakeObjectCopier(fields),
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700790 MakeMoveConstructor(type_name), members, MakeSubObjectList(fields));
791
792 GeneratedObject result;
793 result.name = fbs_type_name;
794 result.include_declarations = includes;
795 result.code = object_code;
796 result.subobjects = subobject_names;
797 return result;
798}
799
800namespace {
801
802// Generated C++ code for an entire fbs file.
803// This includes all of the actual C++ code that will be written to a file (call
804// GenerateCode() to actually get the desired contents of the file).
805struct GeneratedCode {
806 // Prefix (for include guards).
807 std::string contents_prefix;
808 // Full set of required #include declarations.
809 std::set<std::string> include_declarations;
810 // Ordered list of objects (order is necessary to ensure that any dependencies
811 // between objects are managed correctly).
812 std::vector<GeneratedObject> objects;
813 // Suffix (for include guards).
814 std::string contents_suffix;
815
816 // Combine the above things into the string that actually needs to be written
817 // to a file.
818 std::string GenerateCode() const;
819 // Combines the code for multiple objects into one.
820 static GeneratedCode MergeCode(const std::vector<GeneratedObject> &objects);
821};
822
823std::string GeneratedCode::GenerateCode() const {
824 std::string result =
825 contents_prefix + absl::StrJoin(include_declarations, "");
826 for (const auto &object : objects) {
827 result += object.code;
828 }
829 result += contents_suffix;
830 return result;
831}
832
833GeneratedCode GeneratedCode::MergeCode(
834 const std::vector<GeneratedObject> &objects) {
835 GeneratedCode result;
836 // TODO(james): Should we use #ifdef include guards instead?
837 result.contents_prefix =
838 "#pragma once\n// This is a generated file. Do not modify.\n";
839 // We need to get the ordering of objects correct in order to ensure that
840 // depended-on objects appear before their dependees.
841 // In order to do this, we:
842 // 1) Assume that any objects not in the provided vector must exist in
843 // #includes and so can be ignored.
844 // 2) Create a list of all the objects we have been provided but which we have
845 // not yet added to the results vector.
846 // 3) Until said list is empty, we iterate over it and find any object(s)
847 // which have no dependencies in the list itself, and add them to the
848 // result.
849 // We aren't going to worry about efficient graph traversal here or anything.
850 // We also don't currently attempt to support circular dependencies.
851 std::map<std::string_view, const GeneratedObject *> remaining_objects;
852 for (const auto &object : objects) {
853 remaining_objects[object.name] = &object;
854 }
855 while (!remaining_objects.empty()) {
856 std::string_view to_remove;
857 for (const auto &pair : remaining_objects) {
858 bool has_dependencies = false;
859 for (const std::string_view subobject : pair.second->subobjects) {
860 if (remaining_objects.contains(subobject)) {
861 has_dependencies = true;
862 }
863 }
864 if (has_dependencies) {
865 continue;
866 }
867 to_remove = pair.first;
868 result.objects.push_back(*pair.second);
869 result.include_declarations.insert(
870 pair.second->include_declarations.begin(),
871 pair.second->include_declarations.end());
872 break;
873 }
874 // In order to support circular dependencies, two main things have to
875 // change:
876 // 1. We have to dynamically allow depopulating table members (rather than
877 // just supporting dynamically lengthed vectors).
878 // 2. Some of the codegen needs to be tweaked so that we can have the
879 // generated
880 // C++ classes depend on one another.
881 CHECK(!to_remove.empty())
882 << ": Circular dependencies in flatbuffers schemas are not supported.";
883 CHECK_EQ(1u, remaining_objects.erase(to_remove))
884 << ": Failed to remove " << to_remove;
885 }
886 return result;
887}
888} // namespace
889
James Kuszmaul9a2d5f02023-12-14 11:38:35 -0800890std::string GenerateCodeForRootTableFile(const reflection::Schema *schema,
891 std::string_view file_hint) {
892 const reflection::Object *root_object = GetObject(schema, -1);
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700893 const std::string_view root_file =
James Kuszmaul9a2d5f02023-12-14 11:38:35 -0800894 (root_object == nullptr) ? file_hint
895 : root_object->declaration_file()->string_view();
896 std::vector<GeneratedObject> objects;
897 if (root_object != nullptr) {
898 objects.push_back(GenerateCodeForObject(schema, root_object));
899 }
James Kuszmaulf5eb4682023-09-22 17:16:59 -0700900 for (const reflection::Object *object : *schema->objects()) {
901 if (object->is_struct()) {
902 continue;
903 }
904 if (object->declaration_file()->string_view() == root_file) {
905 objects.push_back(GenerateCodeForObject(schema, object));
906 }
907 }
908 return GeneratedCode::MergeCode(objects).GenerateCode();
909}
910} // namespace aos::fbs