Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua Generator using IR. #6940

Merged
merged 38 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e3bde26
initial hack to get new Lua generator into flatc
dbaileychess Nov 18, 2021
6cd6104
Starting to output enum defs for Lua
dbaileychess Nov 18, 2021
922f5e9
Continue to work on table generation for Lua
dbaileychess Nov 18, 2021
1f41b89
Finished basic getter access for Lua
dbaileychess Nov 18, 2021
479fea9
Added ability to get object by index
dbaileychess Nov 18, 2021
f259d70
Finished struct builder
dbaileychess Nov 18, 2021
7525b49
aliased reflection to r
dbaileychess Nov 18, 2021
9e0f6d8
finish table builder generation
dbaileychess Nov 19, 2021
6b98c65
register requiring files
dbaileychess Nov 19, 2021
342e655
better generated header info
dbaileychess Nov 19, 2021
018d894
Tying up loose ends
dbaileychess Nov 19, 2021
ca7de71
Updated reflection to handle struct padding
dbaileychess Nov 19, 2021
c61d44d
Addd type sizes to reflection
dbaileychess Nov 19, 2021
02e516b
Fixed some vector indirect issues
dbaileychess Nov 19, 2021
9ae4fe4
Lua tests passed
dbaileychess Nov 19, 2021
2441bc7
Misc cleanup
dbaileychess Nov 19, 2021
96972b0
ci fixes 1
dbaileychess Nov 19, 2021
101fdd3
ci fixes 2
dbaileychess Nov 19, 2021
c099ae0
renaming
dbaileychess Nov 20, 2021
49ab4ec
up size of type sizes
dbaileychess Nov 20, 2021
3edeeba
manually ran clang-format-11 -i src/idl_parser.cpp
dbaileychess Nov 20, 2021
4936959
fixed some windows casting
dbaileychess Nov 20, 2021
9878f75
remove stupid auto import
dbaileychess Nov 20, 2021
056ddd1
more static_casting
dbaileychess Nov 20, 2021
a351733
remove std
dbaileychess Nov 20, 2021
037b8cd
update other build environments
dbaileychess Nov 20, 2021
73b7102
remove scoped enums
dbaileychess Nov 20, 2021
6696a18
replaced std::to_string with NumToString
dbaileychess Nov 20, 2021
bc9b38c
more win fixes
dbaileychess Nov 20, 2021
152586f
more win fixes
dbaileychess Nov 20, 2021
b6a2a8c
replaced old lua with new
dbaileychess Nov 20, 2021
6960790
removed auto import
dbaileychess Nov 20, 2021
0b78081
review responses
dbaileychess Nov 22, 2021
20a4704
more style fixes
dbaileychess Nov 22, 2021
19ad1b6
refactor bfbs_gen_len to use code +=
dbaileychess Nov 23, 2021
694be0c
added consts
dbaileychess Nov 23, 2021
7bf4029
fix lambda capture for windows
dbaileychess Nov 23, 2021
dee8216
remove unused return type
dbaileychess Nov 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ lua:
- '**/*.lua'
- lua/**/*
- src/idl_gen_lua.cpp
- src/bfbs_gen_lua.cpp

lobster:
- '**/*.lobster'
Expand Down
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ filegroup(
"include/flatbuffers/allocator.h",
"include/flatbuffers/array.h",
"include/flatbuffers/base.h",
"include/flatbuffers/bfbs_generator.h",
"include/flatbuffers/buffer.h",
"include/flatbuffers/buffer_ref.h",
"include/flatbuffers/code_generators.h",
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ set(FlatBuffers_Library_SRCS
include/flatbuffers/allocator.h
include/flatbuffers/array.h
include/flatbuffers/base.h
include/flatbuffers/bfbs_generator.h
include/flatbuffers/buffer.h
include/flatbuffers/buffer_ref.h
include/flatbuffers/default_allocator.h
Expand Down Expand Up @@ -139,7 +140,10 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_swift.cpp
src/flatc.cpp
src/flatc_main.cpp
src/bfbs_gen.h
src/bfbs_gen_lua.h
include/flatbuffers/code_generators.h
src/bfbs_gen_lua.cpp
src/code_generators.cpp
grpc/src/compiler/schema_interface.h
grpc/src/compiler/cpp_generator.h
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/cpp/flatbuffers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(FlatBuffers_Library_SRCS
${FLATBUFFERS_SRC}/include/flatbuffers/allocator.h
${FLATBUFFERS_SRC}/include/flatbuffers/array.h
${FLATBUFFERS_SRC}/include/flatbuffers/base.h
${FLATBUFFERS_SRC}/include/flatbuffers/bfbs_generator.h
${FLATBUFFERS_SRC}/include/flatbuffers/buffer.h
${FLATBUFFERS_SRC}/include/flatbuffers/buffer_ref.h
${FLATBUFFERS_SRC}/include/flatbuffers/default_allocator.h
Expand Down
43 changes: 43 additions & 0 deletions include/flatbuffers/bfbs_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FLATBUFFERS_BFBS_GENERATOR_H_
#define FLATBUFFERS_BFBS_GENERATOR_H_

#include <cstdint>

namespace flatbuffers {

enum GeneratorStatus {
OK,
FAILED,
FAILED_VERIFICATION,
};

// A Flatbuffer Code Generator that receives a binary serialized reflection.fbs
// and generates code from it.
class BfbsGenerator {
public:
virtual ~BfbsGenerator() {}

// Generate code from the provided `buffer` of given `length`. The buffer is
// a serialized reflection.fbs.
virtual GeneratorStatus Generate(const uint8_t *buffer, int64_t length) = 0;
};

} // namespace flatbuffers

#endif // FLATBUFFERS_BFBS_GENERATOR_H_
2 changes: 2 additions & 0 deletions include/flatbuffers/flatc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <limits>
#include <string>

#include "flatbuffers/bfbs_generator.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
Expand Down Expand Up @@ -51,6 +52,7 @@ class FlatCompiler {
flatbuffers::IDLOptions::Language lang;
const char *generator_help;
MakeRuleFn make_rule;
BfbsGenerator *bfbs_generator;
};

typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn,
Expand Down
47 changes: 41 additions & 6 deletions include/flatbuffers/reflection_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ struct Type FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
VT_BASE_TYPE = 4,
VT_ELEMENT = 6,
VT_INDEX = 8,
VT_FIXED_LENGTH = 10
VT_FIXED_LENGTH = 10,
VT_BASE_SIZE = 12,
VT_ELEMENT_SIZE = 14
};
reflection::BaseType base_type() const {
return static_cast<reflection::BaseType>(GetField<int8_t>(VT_BASE_TYPE, 0));
Expand All @@ -176,12 +178,22 @@ struct Type FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
uint16_t fixed_length() const {
return GetField<uint16_t>(VT_FIXED_LENGTH, 0);
}
/// The size (octets) of the `base_type` field.
uint32_t base_size() const {
return GetField<uint32_t>(VT_BASE_SIZE, 4);
}
/// The size (octets) of the `element` field, if present.
uint32_t element_size() const {
return GetField<uint32_t>(VT_ELEMENT_SIZE, 0);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<int8_t>(verifier, VT_BASE_TYPE) &&
VerifyField<int8_t>(verifier, VT_ELEMENT) &&
VerifyField<int32_t>(verifier, VT_INDEX) &&
VerifyField<uint16_t>(verifier, VT_FIXED_LENGTH) &&
VerifyField<uint32_t>(verifier, VT_BASE_SIZE) &&
VerifyField<uint32_t>(verifier, VT_ELEMENT_SIZE) &&
verifier.EndTable();
}
};
Expand All @@ -202,6 +214,12 @@ struct TypeBuilder {
void add_fixed_length(uint16_t fixed_length) {
fbb_.AddElement<uint16_t>(Type::VT_FIXED_LENGTH, fixed_length, 0);
}
void add_base_size(uint32_t base_size) {
fbb_.AddElement<uint32_t>(Type::VT_BASE_SIZE, base_size, 4);
}
void add_element_size(uint32_t element_size) {
fbb_.AddElement<uint32_t>(Type::VT_ELEMENT_SIZE, element_size, 0);
}
explicit TypeBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
Expand All @@ -218,8 +236,12 @@ inline flatbuffers::Offset<Type> CreateType(
reflection::BaseType base_type = reflection::None,
reflection::BaseType element = reflection::None,
int32_t index = -1,
uint16_t fixed_length = 0) {
uint16_t fixed_length = 0,
uint32_t base_size = 4,
uint32_t element_size = 0) {
TypeBuilder builder_(_fbb);
builder_.add_element_size(element_size);
builder_.add_base_size(base_size);
builder_.add_index(index);
builder_.add_fixed_length(fixed_length);
builder_.add_element(element);
Expand Down Expand Up @@ -556,7 +578,8 @@ struct Field FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
VT_KEY = 20,
VT_ATTRIBUTES = 22,
VT_DOCUMENTATION = 24,
VT_OPTIONAL = 26
VT_OPTIONAL = 26,
VT_PADDING = 28
};
const flatbuffers::String *name() const {
return GetPointer<const flatbuffers::String *>(VT_NAME);
Expand Down Expand Up @@ -600,6 +623,10 @@ struct Field FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
bool optional() const {
return GetField<uint8_t>(VT_OPTIONAL, 0) != 0;
}
/// Number of padding octets to always add after this field. Structs only.
uint16_t padding() const {
return GetField<uint16_t>(VT_PADDING, 0);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffsetRequired(verifier, VT_NAME) &&
Expand All @@ -620,6 +647,7 @@ struct Field FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
verifier.VerifyVector(documentation()) &&
verifier.VerifyVectorOfStrings(documentation()) &&
VerifyField<uint8_t>(verifier, VT_OPTIONAL) &&
VerifyField<uint16_t>(verifier, VT_PADDING) &&
verifier.EndTable();
}
};
Expand Down Expand Up @@ -664,6 +692,9 @@ struct FieldBuilder {
void add_optional(bool optional) {
fbb_.AddElement<uint8_t>(Field::VT_OPTIONAL, static_cast<uint8_t>(optional), 0);
}
void add_padding(uint16_t padding) {
fbb_.AddElement<uint16_t>(Field::VT_PADDING, padding, 0);
}
explicit FieldBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
Expand All @@ -690,14 +721,16 @@ inline flatbuffers::Offset<Field> CreateField(
bool key = false,
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>> attributes = 0,
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> documentation = 0,
bool optional = false) {
bool optional = false,
uint16_t padding = 0) {
FieldBuilder builder_(_fbb);
builder_.add_default_real(default_real);
builder_.add_default_integer(default_integer);
builder_.add_documentation(documentation);
builder_.add_attributes(attributes);
builder_.add_type(type);
builder_.add_name(name);
builder_.add_padding(padding);
builder_.add_offset(offset);
builder_.add_id(id);
builder_.add_optional(optional);
Expand All @@ -720,7 +753,8 @@ inline flatbuffers::Offset<Field> CreateFieldDirect(
bool key = false,
std::vector<flatbuffers::Offset<reflection::KeyValue>> *attributes = nullptr,
const std::vector<flatbuffers::Offset<flatbuffers::String>> *documentation = nullptr,
bool optional = false) {
bool optional = false,
uint16_t padding = 0) {
auto name__ = name ? _fbb.CreateString(name) : 0;
auto attributes__ = attributes ? _fbb.CreateVectorOfSortedTables<reflection::KeyValue>(attributes) : 0;
auto documentation__ = documentation ? _fbb.CreateVector<flatbuffers::Offset<flatbuffers::String>>(*documentation) : 0;
Expand All @@ -737,7 +771,8 @@ inline flatbuffers::Offset<Field> CreateFieldDirect(
key,
attributes__,
documentation__,
optional);
optional,
padding);
}

struct Object FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
Expand Down
6 changes: 6 additions & 0 deletions reflection/reflection.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ table Type {
// If base_type == Union, UnionType, or integral derived
// from an enum, index into "enums" below.
fixed_length:uint16 = 0; // Only if base_type == Array.
/// The size (octets) of the `base_type` field.
base_size:uint = 4; // 4 Is a common size due to offsets being that size.
/// The size (octets) of the `element` field, if present.
element_size:uint = 0;
}

table KeyValue {
Expand Down Expand Up @@ -77,6 +81,8 @@ table Field {
attributes:[KeyValue];
documentation:[string];
optional:bool = false;
/// Number of padding octets to always add after this field. Structs only.
padding:uint16 = 0;
}

table Object { // Used for both tables and structs.
Expand Down
Binary file modified samples/monster.bfbs
Binary file not shown.
7 changes: 6 additions & 1 deletion scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,19 @@ def glob(path, pattern):
"--dart",
"--go",
"--lobster",
"--lua",
"--php",
],
schema="monster_test.fbs",
include="include_test",
data="monsterdata_test.json",
)

flatc(
["--lua", "--bfbs-filenames", str(tests_path)],
schema="monster_test.fbs",
include="include_test"
)

flatc(
NO_INCL_OPTS + CPP_OPTS + ["--grpc"],
schema="monster_test.fbs",
Expand Down
6 changes: 6 additions & 0 deletions src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ cc_library(
cc_library(
name = "flatc_library",
srcs = [
"bfbs_gen.h",
"bfbs_gen_lua.cpp",
"bfbs_gen_lua.h",
"flatc.cpp",
],
hdrs = [
Expand All @@ -50,6 +53,9 @@ cc_library(
cc_library(
name = "flatc",
srcs = [
"bfbs_gen.h",
"bfbs_gen_lua.cpp",
"bfbs_gen_lua.h",
"flatc_main.cpp",
"idl_gen_cpp.cpp",
"idl_gen_csharp.cpp",
Expand Down
Loading