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

Move literal defines to main object #158

Merged
merged 1 commit into from
Aug 13, 2018
Merged
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
40 changes: 18 additions & 22 deletions bindgen/ir/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ void IR::addVarDefine(std::string name, std::shared_ptr<Variable> variable) {
bool IR::libObjEmpty() const {
return functions.empty() && !shouldOutputType(typeDefs) &&
!shouldOutputType(structs) && !shouldOutputType(unions) &&
varDefines.empty() && variables.empty() && enums.empty();
varDefines.empty() && variables.empty() &&
!shouldOutputType(enums) && literalDefines.empty();
}

llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
Expand All @@ -97,24 +98,21 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
s << "package " << ir.packageName << "\n\n";
}

if (!ir.libObjEmpty() || ir.shouldOutputType(ir.enums) ||
!ir.literalDefines.empty()) {
s << "import scala.scalanative._\n"
<< "import scala.scalanative.native._\n\n";
if (ir.libObjEmpty()) {
return s;
}

std::string objectName = handleReservedWords(ir.objectName);
s << "import scala.scalanative._\n"
<< "import scala.scalanative.native._\n\n";

bool isLibObjectEmpty = ir.libObjEmpty();

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

s << "@native.extern\n"
<< "object " << objectName << " {\n";
s << "@native.extern\n";
}
s << "object " << handleReservedWords(ir.objectName) << " {\n";

std::vector<std::shared_ptr<const Type>> visitedTypes;

Expand Down Expand Up @@ -171,22 +169,20 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
}
}

if (ir.hasHelperMethods()) {
s << "\n object implicits {\n" << ir.getHelperMethods() << " }\n";
}

if (!isLibObjectEmpty) {
s << "}\n\n";
}

if (!ir.literalDefines.empty()) {
s << "object " << ir.libName << "Defines {\n";
s << "\n object defines {\n";
for (const auto &literalDefine : ir.literalDefines) {
s << literalDefine->getDefinition(ir.locationManager);
}
s << "}\n\n";
s << " }\n";
}

if (ir.hasHelperMethods()) {
s << "\n object implicits {\n" << ir.getHelperMethods() << " }\n";
}

s << "}\n\n";

return s;
}

Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/LiteralDefine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LiteralDefine::LiteralDefine(std::string name, std::string literal,

std::string
LiteralDefine::getDefinition(const LocationManager &locationManager) const {
return " val " + name + ": " + type->str(locationManager) + " = " +
return " val " + name + ": " + type->str(locationManager) + " = " +
literal + "\n";
}

Expand Down
2 changes: 0 additions & 2 deletions tests/samples/Cycles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.scalanative.bindgen.samples
import scala.scalanative._
import scala.scalanative.native._

@native.link("bindgentests")
@native.extern
object Cycles {
type struct_node = native.CStruct2[native.CInt, native.Ptr[Byte]]
type struct_b = native.CStruct1[native.Ptr[native.Ptr[Byte]]]
Expand Down
43 changes: 23 additions & 20 deletions tests/samples/LiteralDefine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ package org.scalanative.bindgen.samples
import scala.scalanative._
import scala.scalanative.native._

object LiteralDefineDefines {
val STRING: native.CString = c"Hello, World!"
val LONG: native.CLong = 1000000000000L
val LONG_WITHOUT_ENDING: native.CLong = 1000000000000L
val LONG_LONG: native.CLongLong = 1000000000000L
val MAXIMUM_SIGNED_LONG: native.CLong = 9223372036854775807L
val MINIMUM_SIGNED_LONG: native.CLong = -9223372036854775808L
val FLOAT: native.CDouble = 5.6
val INT: native.CInt = 42
val MAXIMUM_INT: native.CInt = 2147483647
val NEW_INT: native.CInt = 42
val NEG_INT: native.CInt = -42
val SHOULD_BE_DEFINED: native.CString = c"Because INT is not equal to 0"
val OCTAL: native.CInt = 139
val HEXADECIMAL: native.CInt = 75
val EXPONENT: native.CDouble = 1e-10
val DOT_EXPONENT: native.CDouble = 0.01
val HEXADECIMAL_WITHOUT_RADIX: native.CDouble = 523264
val HEXADECIMAL_WITH_RADIX: native.CDouble = 7.5
val HEXADECIMAL_FRACTIONAL_WITH_RADIX: native.CDouble = 0.0355225
object LiteralDefine {

object defines {
val STRING: native.CString = c"Hello, World!"
val LONG: native.CLong = 1000000000000L
val LONG_WITHOUT_ENDING: native.CLong = 1000000000000L
val LONG_LONG: native.CLongLong = 1000000000000L
val MAXIMUM_SIGNED_LONG: native.CLong = 9223372036854775807L
val MINIMUM_SIGNED_LONG: native.CLong = -9223372036854775808L
val FLOAT: native.CDouble = 5.6
val INT: native.CInt = 42
val MAXIMUM_INT: native.CInt = 2147483647
val NEW_INT: native.CInt = 42
val NEG_INT: native.CInt = -42
val SHOULD_BE_DEFINED: native.CString = c"Because INT is not equal to 0"
val OCTAL: native.CInt = 139
val HEXADECIMAL: native.CInt = 75
val EXPONENT: native.CDouble = 1e-10
val DOT_EXPONENT: native.CDouble = 0.01
val HEXADECIMAL_WITHOUT_RADIX: native.CDouble = 523264
val HEXADECIMAL_WITH_RADIX: native.CDouble = 7.5
val HEXADECIMAL_FRACTIONAL_WITH_RADIX: native.CDouble = 0.0355225
}
}
2 changes: 0 additions & 2 deletions tests/samples/NativeTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.scalanative.bindgen.samples
import scala.scalanative._
import scala.scalanative.native._

@native.link("bindgentests")
@native.extern
object NativeTypes {
type size_t = native.CUnsignedInt
type ptrdiff_t = native.CUnsignedInt
Expand Down
2 changes: 0 additions & 2 deletions tests/samples/Typedef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.scalanative.bindgen.samples
import scala.scalanative._
import scala.scalanative.native._

@native.link("bindgentests")
@native.extern
object Typedef {
type enum_days = native.CUnsignedInt
object enum_days {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class BindgenReportingSpec extends FunSpec {
assertBindgenError(
"""union undefinedUnion;
|typedef union undefinedUnion aliasForUndefinedUnion;
|void foo();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this function all declarations are skipped and no warning printed

|""".stripMargin,
Seq(
"Warning: type alias aliasForUndefinedUnion is skipped because it is an unused alias for incomplete type.")
Expand Down