Skip to content

Commit 6c4a6f6

Browse files
committed
Move literal defines to main object
1 parent 3ee2ffc commit 6c4a6f6

File tree

7 files changed

+39
-45
lines changed

7 files changed

+39
-45
lines changed

bindgen/ir/IR.cpp

+14-18
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ void IR::addVarDefine(std::string name, std::shared_ptr<Variable> variable) {
8787
bool IR::libObjEmpty() const {
8888
return functions.empty() && !shouldOutputType(typeDefs) &&
8989
!shouldOutputType(structs) && !shouldOutputType(unions) &&
90-
varDefines.empty() && variables.empty() && enums.empty();
90+
varDefines.empty() && variables.empty() &&
91+
!shouldOutputType(enums) && literalDefines.empty();
9192
}
9293

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

100-
if (!ir.libObjEmpty() || ir.shouldOutputType(ir.enums) ||
101-
!ir.literalDefines.empty()) {
102-
s << "import scala.scalanative._\n"
103-
<< "import scala.scalanative.native._\n\n";
101+
if (ir.libObjEmpty()) {
102+
return s;
104103
}
105104

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

108-
bool isLibObjectEmpty = ir.libObjEmpty();
109-
110-
if (!isLibObjectEmpty) {
108+
if (!ir.functions.empty() || !ir.varDefines.empty() ||
109+
!ir.variables.empty()) {
111110
if (!ir.linkName.empty()) {
112111
s << "@native.link(\"" << ir.linkName << "\")\n";
113112
}
114-
115-
s << "@native.extern\n"
116-
<< "object " << objectName << " {\n";
113+
s << "@native.extern\n";
117114
}
115+
s << "object " << handleReservedWords(ir.objectName) << " {\n";
118116

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

@@ -175,18 +173,16 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
175173
s << "\n object implicits {\n" << ir.getHelperMethods() << " }\n";
176174
}
177175

178-
if (!isLibObjectEmpty) {
179-
s << "}\n\n";
180-
}
181-
182176
if (!ir.literalDefines.empty()) {
183-
s << "object " << ir.libName << "Defines {\n";
177+
s << "\n object defines {\n";
184178
for (const auto &literalDefine : ir.literalDefines) {
185179
s << literalDefine->getDefinition(ir.locationManager);
186180
}
187-
s << "}\n\n";
181+
s << " }\n";
188182
}
189183

184+
s << "}\n\n";
185+
190186
return s;
191187
}
192188

bindgen/ir/LiteralDefine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LiteralDefine::LiteralDefine(std::string name, std::string literal,
66

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

tests/samples/Cycles.scala

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package org.scalanative.bindgen.samples
33
import scala.scalanative._
44
import scala.scalanative.native._
55

6-
@native.link("bindgentests")
7-
@native.extern
86
object Cycles {
97
type struct_node = native.CStruct2[native.CInt, native.Ptr[Byte]]
108
type struct_b = native.CStruct1[native.Ptr[native.Ptr[Byte]]]

tests/samples/LiteralDefine.scala

+23-20
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@ package org.scalanative.bindgen.samples
33
import scala.scalanative._
44
import scala.scalanative.native._
55

6-
object LiteralDefineDefines {
7-
val STRING: native.CString = c"Hello, World!"
8-
val LONG: native.CLong = 1000000000000L
9-
val LONG_WITHOUT_ENDING: native.CLong = 1000000000000L
10-
val LONG_LONG: native.CLongLong = 1000000000000L
11-
val MAXIMUM_SIGNED_LONG: native.CLong = 9223372036854775807L
12-
val MINIMUM_SIGNED_LONG: native.CLong = -9223372036854775808L
13-
val FLOAT: native.CDouble = 5.6
14-
val INT: native.CInt = 42
15-
val MAXIMUM_INT: native.CInt = 2147483647
16-
val NEW_INT: native.CInt = 42
17-
val NEG_INT: native.CInt = -42
18-
val SHOULD_BE_DEFINED: native.CString = c"Because INT is not equal to 0"
19-
val OCTAL: native.CInt = 139
20-
val HEXADECIMAL: native.CInt = 75
21-
val EXPONENT: native.CDouble = 1e-10
22-
val DOT_EXPONENT: native.CDouble = 0.01
23-
val HEXADECIMAL_WITHOUT_RADIX: native.CDouble = 523264
24-
val HEXADECIMAL_WITH_RADIX: native.CDouble = 7.5
25-
val HEXADECIMAL_FRACTIONAL_WITH_RADIX: native.CDouble = 0.0355225
6+
object LiteralDefine {
7+
8+
object defines {
9+
val STRING: native.CString = c"Hello, World!"
10+
val LONG: native.CLong = 1000000000000L
11+
val LONG_WITHOUT_ENDING: native.CLong = 1000000000000L
12+
val LONG_LONG: native.CLongLong = 1000000000000L
13+
val MAXIMUM_SIGNED_LONG: native.CLong = 9223372036854775807L
14+
val MINIMUM_SIGNED_LONG: native.CLong = -9223372036854775808L
15+
val FLOAT: native.CDouble = 5.6
16+
val INT: native.CInt = 42
17+
val MAXIMUM_INT: native.CInt = 2147483647
18+
val NEW_INT: native.CInt = 42
19+
val NEG_INT: native.CInt = -42
20+
val SHOULD_BE_DEFINED: native.CString = c"Because INT is not equal to 0"
21+
val OCTAL: native.CInt = 139
22+
val HEXADECIMAL: native.CInt = 75
23+
val EXPONENT: native.CDouble = 1e-10
24+
val DOT_EXPONENT: native.CDouble = 0.01
25+
val HEXADECIMAL_WITHOUT_RADIX: native.CDouble = 523264
26+
val HEXADECIMAL_WITH_RADIX: native.CDouble = 7.5
27+
val HEXADECIMAL_FRACTIONAL_WITH_RADIX: native.CDouble = 0.0355225
28+
}
2629
}

tests/samples/NativeTypes.scala

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package org.scalanative.bindgen.samples
33
import scala.scalanative._
44
import scala.scalanative.native._
55

6-
@native.link("bindgentests")
7-
@native.extern
86
object NativeTypes {
97
type size_t = native.CUnsignedInt
108
type ptrdiff_t = native.CUnsignedInt

tests/samples/Typedef.scala

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package org.scalanative.bindgen.samples
33
import scala.scalanative._
44
import scala.scalanative.native._
55

6-
@native.link("bindgentests")
7-
@native.extern
86
object Typedef {
97
type enum_days = native.CUnsignedInt
108
object enum_days {

tests/src/test/scala/org/scalanative/bindgen/BindgenReportingSpec.scala

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class BindgenReportingSpec extends FunSpec {
7979
assertBindgenError(
8080
"""union undefinedUnion;
8181
|typedef union undefinedUnion aliasForUndefinedUnion;
82+
|void foo();
8283
|""".stripMargin,
8384
Seq(
8485
"Warning: type alias aliasForUndefinedUnion is skipped because it is an unused alias for incomplete type.")

0 commit comments

Comments
 (0)