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

Bring bindgen up-to-date and add some fixes #200

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
64 changes: 36 additions & 28 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"] = "unsafe.CBool";
typeMap["_Bool"] = "unsafe.CBool";
typeMap["char"] = "unsafe.CChar";
typeMap["signed char"] = "unsafe.CSignedChar";
typeMap["unsigned char"] = "unsafe.CUnsignedChar";
typeMap["short"] = "unsafe.CShort";
typeMap["unsigned short"] = "unsafe.CUnsignedShort";
typeMap["int"] = "unsafe.CInt";
typeMap["long int"] = "unsafe.CLongInt";
typeMap["unsigned int"] = "unsafe.CUnsignedInt";
typeMap["unsigned long int"] = "unsafe.CUnsignedLongInt";
typeMap["long"] = "unsafe.CLong";
typeMap["unsigned long"] = "unsafe.CUnsignedLong";
typeMap["long long"] = "unsafe.CLongLong";
typeMap["unsigned long long"] = "unsafe.CUnsignedLongLong";
typeMap["size_t"] = "unsafe.CSize";
typeMap["ptrdiff_t"] = "unsafe.CPtrDiff";
typeMap["wchar_t"] = "unsafe.CWideChar";
typeMap["char16_t"] = "unsafe.CChar16";
typeMap["char32_t"] = "unsafe.CChar32";
typeMap["float"] = "unsafe.CFloat";
typeMap["double"] = "unsafe.CDouble";
typeMap["long double"] = "unsafe.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("unsafe.CChar"))
return std::make_shared<PrimitiveType>("unsafe.CString");
}
}

Expand Down Expand Up @@ -188,13 +188,21 @@ std::shared_ptr<Location> TypeTranslator::getLocation(clang::Decl *decl) {
return std::make_shared<Location>(path, lineNumber);
}

std::string getFieldName(const clang::FieldDecl *field) {
std::string name = field->getNameAsString();
if (name.empty()) {
name = "field" + std::to_string(field->getFieldIndex());
}
return name;
}

std::shared_ptr<TypeDef>
TypeTranslator::addUnionDefinition(clang::RecordDecl *record,
std::string name) {
std::vector<std::shared_ptr<Field>> fields;

for (const clang::FieldDecl *field : record->fields()) {
std::string fname = field->getNameAsString();
std::string fname = getFieldName(field);
std::shared_ptr<Type> ftype = translate(field->getType());

fields.push_back(std::make_shared<Field>(fname, ftype));
Expand Down Expand Up @@ -229,8 +237,8 @@ TypeTranslator::addStructDefinition(clang::RecordDecl *record,
std::shared_ptr<Type> ftype = translate(field->getType());
uint64_t recordOffsetInBits =
recordLayout.getFieldOffset(field->getFieldIndex());
fields.push_back(std::make_shared<Field>(field->getNameAsString(),
ftype, recordOffsetInBits));
fields.push_back(std::make_shared<Field>(getFieldName(field), ftype,
recordOffsetInBits));
}

uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl());
Expand Down
17 changes: 0 additions & 17 deletions bindgen/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@
#include "ir/types/Type.h"
#include <clang/AST/AST.h>

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

auto last_digit = v % 10;
auto rest = v / 10;

if (accumulator.empty()) {
return uint64ToScalaNat(rest,
"native.Nat._" + std::to_string(last_digit));
} else {
return uint64ToScalaNat(rest, "native.Nat.Digit[native.Nat._" +
std::to_string(last_digit) + ", " +
accumulator + "]");
}
}

static std::array<std::string, 39> reserved_words = {
{"abstract", "case", "catch", "class", "def", "do",
"else", "extends", "false", "final", "finally", "for",
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>("unsafe.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 = "unsafe.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 = "unsafe.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 == "unsafe.CLong" || type == "unsafe.CLongLong") {
scalaLiteral = scalaLiteral + "L";
}
}
} else if (parser.isFloatingLiteral()) {
if (fitsIntoDouble(parser)) {
type = "native.CDouble";
type = "unsafe.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 "unsafe.CInt";
} else if (integerFitsIntoType<long, unsigned long>(parser, positive)) {
return "native.CLong";
return "unsafe.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 == "unsafe.CLong") {
return "L";
} else if (primitiveType == "native.CUnsignedInt") {
} else if (primitiveType == "unsafe.CUnsignedInt") {
return ".toUInt";
} else if (primitiveType == "native.CUnsignedLong") {
} else if (primitiveType == "unsafe.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 << " @unsafe.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() << ": unsafe.CVararg*";
}
s << "): " << retType->str(locationManager) << " = native.extern\n";
s << "): " << retType->str(locationManager) << " = unsafe.extern\n";
return s.str();
}

Expand Down
7 changes: 4 additions & 3 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 @@ -103,14 +104,14 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
}

s << "import scala.scalanative._\n"
<< "import scala.scalanative.native._\n\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 << "@unsafe.link(\"" << ir.linkName << "\")\n";
}
s << "@native.extern\n";
s << "@unsafe.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 = unsafe.CInt
* type __darwin_pid_t = __int32_t
* type pid_t = __darwin_pid_t
* @endcode
*
* Becomes:
* @code
* type pid_t = native.CInt
* type pid_t = unsafe.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 @@ -3,6 +3,7 @@
#include "Enum.h"
#include "Struct.h"
#include <fstream>
#include <sstream>
#include <stdexcept>

LocationManager::LocationManager(std::string mainHeaderPath)
Expand Down
6 changes: 4 additions & 2 deletions bindgen/ir/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ bool Record::usesType(
if (contains(this, visitedTypes)) {
return false;
}

visitedTypes.push_back(shared_from_this());

for (const auto &field : fields) {
if (*field->getType() == *type ||
field->getType()->usesType(type, stopOnTypeDefs, visitedTypes)) {
if (field->getType() &&
(*field->getType() == *type ||
field->getType()->usesType(type, stopOnTypeDefs, visitedTypes))) {
visitedTypes.pop_back();
return true;
}
Expand Down
Loading