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

Fix: #194 Align scala-native-bindgen with Scala Native 0.4.x API #195

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6c7b623
Update gitignore for Metals
ekrich Sep 2, 2020
372c2af
Add instructions to add jsonnlohmann/json.hpp header for macOS
ekrich Sep 3, 2020
bc20079
Add includes for stringstream
ekrich Sep 3, 2020
6dcaaf0
Update sbt and Scala 2.12 and now everything works
ekrich Sep 3, 2020
15b7a84
Remove Scala 2.10 and sbt 0.13
ekrich Sep 3, 2020
ae7bc85
Everything compiles, a couple of errors, generation needs to match
ekrich Sep 3, 2020
afaf898
More Scala file updates
ekrich Sep 4, 2020
1cab681
Fix a couple of more imports
ekrich Sep 4, 2020
4e5f828
Missed a comma, don't ignore Metals, it is probably right
ekrich Sep 4, 2020
d41ac8e
Remove some dereferences
ekrich Sep 4, 2020
e2b1670
Make quite a bit of progress so commit
ekrich Sep 4, 2020
7e3d791
Fix for pointer to field on a struct
ekrich Sep 5, 2020
bac210e
Fix remaining pointer to field sites to avoid ClassCastException
ekrich Sep 5, 2020
2122792
Update for Scala Native 0.4.0
ekrich Jan 21, 2021
8149e60
Fix versions
ekrich Jan 21, 2021
31b3af9
Update scalatest to 3.2.4-M1
ekrich Jan 24, 2021
99fb386
Fix some compile error, update Scala and sbt
ekrich May 28, 2021
be6ef9e
Change builds to slash syntax, update comparisons to ULong aka CSize
ekrich May 28, 2021
d991c28
Update some versions, try to force build to work
ekrich Nov 16, 2021
e3a063c
Fix Utf8procSpec for compilation, revert scalafmt path and format
ekrich Nov 16, 2021
bfb81c5
Handle /usr/include on macOS 10.14+
ekrich Nov 17, 2021
be44ab8
StructSpec works except for the last test
ekrich Nov 21, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ target/
scripts/.coursier
scripts/.scalafmt-*
/scala-native-bindgen-*

.vscode/
metals.sbt
3 changes: 2 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version = 1.5.1
style = defaultWithAlign
docstrings = JavaDoc
assumeStandardLibraryStripMargin = true
Expand All @@ -8,4 +9,4 @@ project.excludeFilters = [
"tests/samples/[^/]*[.]scala"
]
project.git = true
runner.dialect = Scala211
runner.dialect = scala211
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Important to note is that all contributors must have signed the [Scala CLA].

## Developer Workflow

Install the following for macOS:
```sh
brew tap nlohmann/json
brew install nlohmann-json
```

Build the `scalaBindgen` tool:

```sh
Expand Down
3 changes: 3 additions & 0 deletions bindgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ project(scala-native-bindgen)

option(STATIC_LINKING "Statically link the executable" OFF)

find_package(nlohmann_json REQUIRED)

# Locate LLVMConfig.cmake
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
Expand Down Expand Up @@ -106,6 +108,7 @@ set_target_properties(bindgen

target_link_libraries(bindgen
PRIVATE
nlohmann_json
clangFrontend
clangTooling
clangSerialization
Expand Down
50 changes: 25 additions & 25 deletions bindgen/TypeTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir)

// Native Types
typeMap["void"] = "Unit";
typeMap["bool"] = "native.CBool";
typeMap["_Bool"] = "native.CBool";
typeMap["char"] = "native.CChar";
typeMap["signed char"] = "native.CSignedChar";
typeMap["unsigned char"] = "native.CUnsignedChar";
typeMap["short"] = "native.CShort";
typeMap["unsigned short"] = "native.CUnsignedShort";
typeMap["int"] = "native.CInt";
typeMap["long int"] = "native.CLongInt";
typeMap["unsigned int"] = "native.CUnsignedInt";
typeMap["unsigned long int"] = "native.CUnsignedLongInt";
typeMap["long"] = "native.CLong";
typeMap["unsigned long"] = "native.CUnsignedLong";
typeMap["long long"] = "native.CLongLong";
typeMap["unsigned long long"] = "native.CUnsignedLongLong";
typeMap["size_t"] = "native.CSize";
typeMap["ptrdiff_t"] = "native.CPtrDiff";
typeMap["wchar_t"] = "native.CWideChar";
typeMap["char16_t"] = "native.CChar16";
typeMap["char32_t"] = "native.CChar32";
typeMap["float"] = "native.CFloat";
typeMap["double"] = "native.CDouble";
typeMap["long double"] = "native.CDouble";
typeMap["bool"] = "CBool";
typeMap["_Bool"] = "CBool";
typeMap["char"] = "CChar";
typeMap["signed char"] = "CSignedChar";
typeMap["unsigned char"] = "CUnsignedChar";
typeMap["short"] = "CShort";
typeMap["unsigned short"] = "CUnsignedShort";
typeMap["int"] = "CInt";
typeMap["long int"] = "CLongInt";
typeMap["unsigned int"] = "CUnsignedInt";
typeMap["unsigned long int"] = "CUnsignedLongInt";
typeMap["long"] = "CLong";
typeMap["unsigned long"] = "CUnsignedLong";
typeMap["long long"] = "CLongLong";
typeMap["unsigned long long"] = "CUnsignedLongLong";
typeMap["size_t"] = "CSize";
typeMap["ptrdiff_t"] = "CPtrDiff";
typeMap["wchar_t"] = "CWideChar";
typeMap["char16_t"] = "CChar16";
typeMap["char32_t"] = "CChar32";
typeMap["float"] = "CFloat";
typeMap["double"] = "CDouble";
typeMap["long double"] = "CDouble";
}

std::shared_ptr<Type>
Expand Down Expand Up @@ -73,8 +73,8 @@ TypeTranslator::translatePointer(const clang::QualType &pte) {
// Take care of char*
if (as->getKind() == clang::BuiltinType::Char_S ||
as->getKind() == clang::BuiltinType::SChar) {
// TODO: new PointerType(new PrimitiveType("native.CChar"))
return std::make_shared<PrimitiveType>("native.CString");
// TODO: new PointerType(new PrimitiveType("CChar"))
return std::make_shared<PrimitiveType>("CString");
}
}

Expand Down
26 changes: 16 additions & 10 deletions bindgen/Utils.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
#ifndef UTILS_H
#define UTILS_H

#include <iostream>
#include "ir/TypeDef.h"
#include "ir/types/Type.h"
#include <clang/AST/AST.h>

inline std::string uint64ToScalaNat(uint64_t v, std::string accumulator = "") {
inline std::string uint64ToScalaNat(uint64_t v, std::string acc = "") {
if (v == 0)
return accumulator;
return acc;

auto last_digit = v % 10;
auto rest = v / 10;
auto v_str = std::to_string(v);
auto len = v_str.length();

if (accumulator.empty()) {
return uint64ToScalaNat(rest,
"native.Nat._" + std::to_string(last_digit));
if (len == 1) {
acc = acc + "Nat._" + v_str;
} else {
return uint64ToScalaNat(rest, "native.Nat.Digit[native.Nat._" +
std::to_string(last_digit) + ", " +
accumulator + "]");
for (char const &c: v_str) {
if (acc.empty()) {
acc = acc + "Nat.Digit" + std::to_string(len) + "[Nat._" + c;
} else {
acc = acc + ", Nat._" + c;
}
}
acc = acc + "]";
}
return acc;
}

static std::array<std::string, 39> reserved_words = {
Expand Down
14 changes: 7 additions & 7 deletions bindgen/defines/DefineFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void DefineFinder::MacroDefined(const clang::Token &macroNameTok,
stringToken.getLength());
ir.addLiteralDefine(
macroName, "c" + literal,
std::make_shared<PrimitiveType>("native.CString"));
std::make_shared<PrimitiveType>("CString"));
} else if (tokens->size() == 1 &&
(*tokens)[0].getKind() == clang::tok::identifier) {
// token might be a variable
Expand Down Expand Up @@ -126,15 +126,15 @@ void DefineFinder::addNumericConstantDefine(const std::string &macroName,
if (parser.isLongLong) {
/* literal has `LL` ending. `long long` is represented as `Long`
* in Scala Native */
type = "native.CLongLong";
type = "CLongLong";

/* must fit into Scala integer type */
if (!integerFitsIntoType<long, unsigned long>(parser, positive)) {
type.clear();
}
} else if (parser.isLong) {
/* literal has `L` ending */
type = "native.CLong";
type = "CLong";

/* must fit into Scala integer type */
if (!integerFitsIntoType<long, unsigned long>(parser, positive)) {
Expand All @@ -146,13 +146,13 @@ void DefineFinder::addNumericConstantDefine(const std::string &macroName,

if (!type.empty()) {
scalaLiteral = getDecimalLiteral(parser);
if (type == "native.CLong" || type == "native.CLongLong") {
if (type == "CLong" || type == "CLongLong") {
scalaLiteral = scalaLiteral + "L";
}
}
} else if (parser.isFloatingLiteral()) {
if (fitsIntoDouble(parser)) {
type = "native.CDouble";
type = "CDouble";
scalaLiteral = getDoubleLiteral(parser);
}
}
Expand All @@ -172,9 +172,9 @@ DefineFinder::getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser,
bool positive) {

if (integerFitsIntoType<int, uint>(parser, positive)) {
return "native.CInt";
return "CInt";
} else if (integerFitsIntoType<long, unsigned long>(parser, positive)) {
return "native.CLong";
return "CLong";
} else {
llvm::errs() << "Warning: integer value does not fit into 8 bytes: "
<< literal << "\n";
Expand Down
7 changes: 4 additions & 3 deletions bindgen/ir/Enum.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Enum.h"
#include <sstream>

Enumerator::Enumerator(std::string name, int64_t value)
: name(std::move(name)), value(value) {}
Expand Down Expand Up @@ -29,11 +30,11 @@ std::string Enum::getEnumerators() const {

std::string Enum::getTypeCastSuffix() const {
std::string primitiveType = PrimitiveType::getType();
if (primitiveType == "native.CLong") {
if (primitiveType == "CLong") {
return "L";
} else if (primitiveType == "native.CUnsignedInt") {
} else if (primitiveType == "CUnsignedInt") {
return ".toUInt";
} else if (primitiveType == "native.CUnsignedLong") {
} else if (primitiveType == "CUnsignedLong") {
return "L.toULong";
}
return "";
Expand Down
6 changes: 3 additions & 3 deletions bindgen/ir/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ std::string
Function::getDefinition(const LocationManager &locationManager) const {
std::stringstream s;
if (scalaName != name) {
s << " @native.link(\"" << name << "\")\n";
s << " @link(\"" << name << "\")\n";
}
s << " def " << handleReservedWords(scalaName) << "(";
std::string sep = "";
Expand All @@ -29,9 +29,9 @@ Function::getDefinition(const LocationManager &locationManager) const {
if (isVariadic) {
/* the C Iso require at least one argument in a variadic function, so
* the comma is fine */
s << ", " << getVarargsParameterName() << ": native.CVararg*";
s << ", " << getVarargsParameterName() << ": CVarArg*";
}
s << "): " << retType->str(locationManager) << " = native.extern\n";
s << "): " << retType->str(locationManager) << " = extern\n";
return s.str();
}

Expand Down
9 changes: 5 additions & 4 deletions bindgen/ir/IR.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "IR.h"
#include "../Utils.h"
#include <sstream>

IR::IR(std::string libName, std::string linkName, std::string objectName,
std::string packageName, const LocationManager &locationManager)
Expand Down Expand Up @@ -102,15 +103,15 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
return s;
}

s << "import scala.scalanative._\n"
<< "import scala.scalanative.native._\n\n";
s << "import scala.scalanative.unsigned._\n"
<< "import scala.scalanative.unsafe._\n\n";

if (!ir.functions.empty() || !ir.varDefines.empty() ||
!ir.variables.empty()) {
if (!ir.linkName.empty()) {
s << "@native.link(\"" << ir.linkName << "\")\n";
s << "@link(\"" << ir.linkName << "\")\n";
}
s << "@native.extern\n";
s << "@extern\n";
}
s << "object " << handleReservedWords(ir.objectName) << " {\n";

Expand Down
4 changes: 2 additions & 2 deletions bindgen/ir/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ class IR {
*
* Example:
* @code
* type __int32_t = native.CInt
* type __int32_t = CInt
* type __darwin_pid_t = __int32_t
* type pid_t = __darwin_pid_t
* @endcode
*
* Becomes:
* @code
* type pid_t = native.CInt
* type pid_t = CInt
* @endcode
*
*/
Expand Down
1 change: 1 addition & 0 deletions bindgen/ir/LocationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Struct.h"
#include <fstream>
#include <stdexcept>
#include <sstream>

LocationManager::LocationManager(std::string mainHeaderPath)
: mainHeaderPath(std::move(mainHeaderPath)) {}
Expand Down
Loading