From f3602c092bc1c66fe0597defda37662a81412a6f Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Fri, 13 Jul 2018 17:30:48 +0200 Subject: [PATCH] WIP #117 Add proto file and cmake integration --- bindgen/CMakeLists.txt | 17 ++++- proto/ir.proto | 162 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 proto/ir.proto diff --git a/bindgen/CMakeLists.txt b/bindgen/CMakeLists.txt index 4f617da..066e1a1 100644 --- a/bindgen/CMakeLists.txt +++ b/bindgen/CMakeLists.txt @@ -3,10 +3,19 @@ project(scala-native-bindgen) option(STATIC_LINKING "Statically link the executable" OFF) +if (STATIC_LINKING) + set(Protobuf_USE_STATIC_LIBS ON) +endif() + +find_package(Protobuf REQUIRED) +message(STATUS "Found protobuf ${Protobuf_VERSION}") + +message(STATUS "Using protobuf include dirs: ${Protobuf_INCLUDE_DIRS}") +include_directories(SYSTEM ${Protobuf_INCLUDE_DIRS}) + # Locate LLVMConfig.cmake find_package(LLVM REQUIRED CONFIG) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") message(STATUS "Using LLVM include dirs: ${LLVM_INCLUDE_DIRS}") include_directories(SYSTEM ${LLVM_INCLUDE_DIRS}) @@ -19,6 +28,9 @@ link_directories(${LLVM_LIBRARY_DIRS}) add_compile_options(-fexceptions -std=c++11 -Wall -Wconversion -Werror) +protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS "${CMAKE_CURRENT_SOURCE_DIR}/../proto/ir.proto") +include_directories(SYSTEM ${CMAKE_CURRENT_BINARY_DIR}) + add_executable(bindgen Main.cpp visitor/ScalaFrontendAction.h @@ -74,6 +86,8 @@ add_executable(bindgen ir/Location.cpp ir/LocationManager.h ir/LocationManager.cpp + ${PROTO_SRCS} + ${PROTO_HDRS} ) if (STATIC_LINKING) @@ -113,4 +127,5 @@ target_link_libraries(bindgen clangLex clangBasic ${LLVM_LIBS} + ${Protobuf_LIBRARIES} ) diff --git a/proto/ir.proto b/proto/ir.proto new file mode 100644 index 0000000..450e0b6 --- /dev/null +++ b/proto/ir.proto @@ -0,0 +1,162 @@ +syntax = "proto3"; + +package org.scalanative.bindgen; + +option optimize_for = LITE_RUNTIME; + +message IR { + repeated EnumType enums = 1; + repeated StructType structs = 2; + repeated UnionType unions = 3; + repeated TypedefType typedefs = 4; + repeated Decl.FunctionDecl functions = 5; + repeated Decl.VariableDecl variables = 6; + //std::vector> literalDefines; + //std::vector> possibleVarDefines; + //std::vector> varDefines; +} + +message Range { + int32 start_line = 1; + int32 start_character = 2; + int32 end_line = 3; + int32 end_character = 4; +} + +message Location { + string uri = 1; + Range range = 2; +} + +message Type { + /* + enum Kind { + UNKNOWN = 0; + PRIMITIVE = 1; + ENUM = 2; + POINTER = 3; + ARRAY = 4; + FUNCTION_POINTER = 5; + UNION = 6; + STRUCT = 7; + TYPEDEF = 8; + } + */ + + //Kind kind = 1; // FIXME: remove? + Location location = 2; + + oneof kind { + PrimitiveType primitiveType = 3; + EnumType enumType = 4; + PointerType pointerType = 5; + ArrayType arrayType = 6; + FunctionPointerType functionPointerType = 7; + UnionType unionType = 8; + StructType structType = 9; + TypedefType typedefType = 10; + } +} + +message PrimitiveType { + enum Modifier { + NONE = 0; + CONST = 0x1; + } + + string type = 1; + uint64 modifiers = 2; +} + +message EnumType { + message Enumerator { + string name = 1; + // Have both int64 and uint64? + int64 value = 2; + } + + string name = 1; + repeated Enumerator enumerators = 2; +} + +message PointerType { + Type type = 1; +} + +message ArrayType { + Type type = 1; + uint64 size = 2; +} + +message FunctionPointerType { + // FIXME: Include parameter name in doc string? + Type returnType = 1; + repeated Type parameterTypes = 2; + bool isVariadic = 3; +} + +message Field { + string name = 1; + Type type = 2; +} + +message UnionType { + string name = 1; + repeated Field fields = 2; + uint64 size = 3; +} + +message StructType { + // FIXME: packed attr + string name = 1; + repeated Field fields = 2; + uint64 size = 3; +} + +message TypedefType { + string name = 1; + Type type = 2; +} + +message Decl { + /* + enum Kind { + UNKNOWN = 0; + FUNCTION = 1; + VARIABLE = 2; + VARIABLE_DEFINE = 3; + LITERAL_DEFINE = 4; + } + */ + + //Kind kind = 1; + Location location = 2; + + oneof kind { + FunctionDecl functionDecl = 11; + VariableDecl variableDecl = 12; + } + + message FunctionDecl { + message Parameter { + string name = 1; + Type type = 2; + } + + string name = 1; + Type returnType = 2; + repeated Parameter parameters = 3; + bool isVariadic = 4; + } + + message VariableDecl { + enum Modifier { + NONE = 0; + VOLATILE = 1; + } + + string name = 1; + Type type = 2; + repeated Modifier modifiers = 3; + } +} \ No newline at end of file