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

[Nim] Nim implementation #7362

Closed
wants to merge 15 commits into from
Closed
9 changes: 7 additions & 2 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ swift:
- tests/FlatBuffers.Test.Swift/**/*
- src/idl_gen_swift.cpp

nim:
- '**/*.nim'
- nim/**/*
- src/idl_gen_nim.cpp

javascript:
- '**/*.js'
- src/idl_gen_ts.cpp
Expand Down Expand Up @@ -63,7 +68,7 @@ rust:
- '**/*.rs'
- rust/**/*
- src/idl_gen_rust.cpp

dart:
- '**/*.dart'
- src/idl_gen_dart.cpp
Expand Down Expand Up @@ -92,4 +97,4 @@ CI:

grpc:
- grpc/**/*
- src/idl_gen_grpc.cpp
- src/idl_gen_grpc.cpp
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,7 @@ flatbuffers.pc
**/html/**
**/latex/**
# https://cmake.org/cmake/help/latest/module/FetchContent.html#variable:FETCHCONTENT_BASE_DIR
_deps/
_deps/
nimcache/
testresults/
testament.db
41 changes: 21 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ option(FLATBUFFERS_ENABLE_PCH
Only work if CMake supports 'target_precompile_headers'. \"
This can speed up compilation time."
OFF)
option(FLATBUFFERS_SKIP_MONSTER_EXTRA
option(FLATBUFFERS_SKIP_MONSTER_EXTRA
"Skip generating monster_extra.fbs that contains non-supported numerical\"
types." OFF)

Expand Down Expand Up @@ -156,6 +156,7 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_grpc.cpp
src/idl_gen_json_schema.cpp
src/idl_gen_swift.cpp
src/idl_gen_nim.cpp
src/idl_namer.h
src/namer.h
src/flatc.cpp
Expand Down Expand Up @@ -320,8 +321,8 @@ include_directories(grpc)

# Creates an interface library that stores the configuration settings that each
# target links too. This is a compromise between setting configuration globally
# with add_compile_options() and the more targetted target_compile_options().
# This way each target in this file can share settings and override them if
# with add_compile_options() and the more targetted target_compile_options().
# This way each target in this file can share settings and override them if
# needed.
add_library(ProjectConfig INTERFACE)
target_compile_features(ProjectConfig
Expand All @@ -335,9 +336,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# We shouldn't rely on any compiler-extensions to make things work.
set(CMAKE_CXX_EXTENSIONS OFF)

if(MSVC)
if(MSVC)
target_compile_options(ProjectConfig
INTERFACE
INTERFACE
/W4
/WX
/wd4512 # C4512: assignment operator could not be generated
Expand All @@ -352,9 +353,9 @@ else()
INTERFACE
-Wall
-Werror
-pedantic
-Werror
-Wextra
-pedantic
-Werror
-Wextra
-Wno-unused-parameter
-Wold-style-cast
-Wimplicit-fallthrough
Expand All @@ -371,13 +372,13 @@ else()

$<$<CXX_COMPILER_ID:GNU>:
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.4>:
-Wunused-result
-Werror=unused-result
-Wunused-parameter
-Wunused-result
-Werror=unused-result
-Wunused-parameter
-Werror=unused-parameter
>
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,7.0>:
-faligned-new
-faligned-new
-Werror=implicit-fallthrough=2
>
$<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,8.0>:
Expand Down Expand Up @@ -405,7 +406,7 @@ if(FLATBUFFERS_BUILD_FLATLIB)
add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})

# Attach header directory for when build via add_subdirectory().
target_include_directories(flatbuffers
target_include_directories(flatbuffers
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
Expand All @@ -423,7 +424,7 @@ if(FLATBUFFERS_BUILD_FLATC)
endif()

target_link_libraries(flatc PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
target_compile_options(flatc
target_compile_options(flatc
PUBLIC
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>:
/MT
Expand Down Expand Up @@ -623,13 +624,13 @@ if(FLATBUFFERS_BUILD_GRPCTEST)
find_package(gRPC CONFIG REQUIRED)
add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
add_dependencies(grpctest generated_code)
target_link_libraries(grpctext
PRIVATE
target_link_libraries(grpctext
PRIVATE
$<BUILD_INTERFACE:ProjectConfig>
gRPC::grpc++_unsecure
gRPC::gpr
gRPC::grpc++_unsecure
gRPC::gpr
pthread
dl
dl
)
endif()

Expand Down Expand Up @@ -747,4 +748,4 @@ add_library(FlatBuffers::FlatBuffers ALIAS FlatBuffers)
target_include_directories(
FlatBuffers
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/include>)
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/include>)
42 changes: 24 additions & 18 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ namespace flatbuffers {
// of type tokens.
// clang-format off
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8) \
TD(UTYPE, "", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8) /* begin scalar/int */ \
TD(BOOL, "bool", uint8_t, boolean,bool, bool, bool, bool, Boolean, Bool) \
TD(CHAR, "byte", int8_t, byte, int8, sbyte, int8, i8, Byte, Int8) \
TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8) \
TD(SHORT, "short", int16_t, short, int16, short, int16, i16, Short, Int16) \
TD(USHORT, "ushort", uint16_t, short, uint16, ushort, uint16, u16, UShort, UInt16) \
TD(INT, "int", int32_t, int, int32, int, int32, i32, Int, Int32) \
TD(UINT, "uint", uint32_t, int, uint32, uint, uint32, u32, UInt, UInt32) \
TD(LONG, "long", int64_t, long, int64, long, int64, i64, Long, Int64) \
TD(ULONG, "ulong", uint64_t, long, uint64, ulong, uint64, u64, ULong, UInt64) /* end int */ \
TD(FLOAT, "float", float, float, float32, float, float32, f32, Float, Float32) /* begin float */ \
TD(DOUBLE, "double", double, double, float64, double, float64, f64, Double, Double) /* end float/scalar */
TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8, uint8) \
TD(UTYPE, "", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8, uint8) /* begin scalar/int */ \
TD(BOOL, "bool", uint8_t, boolean,bool, bool, bool, bool, Boolean, Bool, bool) \
TD(CHAR, "byte", int8_t, byte, int8, sbyte, int8, i8, Byte, Int8, int8) \
TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8, uint8) \
TD(SHORT, "short", int16_t, short, int16, short, int16, i16, Short, Int16, int16) \
TD(USHORT, "ushort", uint16_t, short, uint16, ushort, uint16, u16, UShort, UInt16, uint16) \
TD(INT, "int", int32_t, int, int32, int, int32, i32, Int, Int32, int32) \
TD(UINT, "uint", uint32_t, int, uint32, uint, uint32, u32, UInt, UInt32, uint32) \
TD(LONG, "long", int64_t, long, int64, long, int64, i64, Long, Int64, int64) \
TD(ULONG, "ulong", uint64_t, long, uint64, ulong, uint64, u64, ULong, UInt64, uint64) /* end int */ \
TD(FLOAT, "float", float, float, float32, float, float32, f32, Float, Float32, float32) /* begin float */ \
TD(DOUBLE, "double", double, double, float64, double, float64, f64, Double, Double, float64) /* end float/scalar */
#define FLATBUFFERS_GEN_TYPES_POINTER(TD) \
TD(STRING, "string", Offset<void>, int, int, StringOffset, int, unused, Int, Offset<String>) \
TD(VECTOR, "", Offset<void>, int, int, VectorOffset, int, unused, Int, Offset<UOffset>) \
TD(STRUCT, "", Offset<void>, int, int, int, int, unused, Int, Offset<UOffset>) \
TD(UNION, "", Offset<void>, int, int, int, int, unused, Int, Offset<UOffset>)
TD(STRING, "string", Offset<void>, int, int, StringOffset, int, unused, Int, Offset<String>, uoffset) \
TD(VECTOR, "", Offset<void>, int, int, VectorOffset, int, unused, Int, Offset<UOffset>, uoffset) \
TD(STRUCT, "", Offset<void>, int, int, int, int, unused, Int, Offset<UOffset>, uoffset) \
TD(UNION, "", Offset<void>, int, int, int, int, unused, Int, Offset<UOffset>, uoffset)
#define FLATBUFFERS_GEN_TYPE_ARRAY(TD) \
TD(ARRAY, "", int, int, int, int, int, unused, Int, Offset<UOffset>)
TD(ARRAY, "", int, int, int, int, int, unused, Int, Offset<UOffset>, uoffset)
// The fields are:
// - enum
// - FlatBuffers schema type.
Expand All @@ -74,6 +74,7 @@ namespace flatbuffers {
// - Python type.
// - Kotlin type.
// - Rust type.
// - Nim type.

// using these macros, we can now write code dealing with types just once, e.g.

Expand Down Expand Up @@ -623,6 +624,7 @@ struct IDLOptions {
kRust = 1 << 14,
kKotlin = 1 << 15,
kSwift = 1 << 16,
kNim = 1 << 17,
kMAX
};

Expand Down Expand Up @@ -1163,6 +1165,10 @@ extern bool GenerateKotlin(const Parser &parser, const std::string &path,
extern bool GenerateSwift(const Parser &parser, const std::string &path,
const std::string &file_name);

// Generate Nim classes.
// See idl_gen_nim.cpp
extern bool GenerateNim(const Parser &parser, const std::string &path,
const std::string &file_name);
// Generate a schema file from the internal representation, useful after
// parsing a .proto schema.
extern std::string GenerateFBS(const Parser &parser,
Expand Down
7 changes: 7 additions & 0 deletions nim/flatbuffers.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version = "1.0.0"
author = "flatbuffers"
description = "Flatbuffers"
license = "Apache 2.0"
srcDir = "flatbuffers"

requires "nim >= 1.4.0"
7 changes: 7 additions & 0 deletions nim/flatbuffers/flatbuffers.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import
src/[
builder,
struct,
table
]
export flatbuffers.builder, flatbuffers.table, flatbuffers.struct
Loading