Skip to content

Commit c1c1852

Browse files
authored
Merge pull request #80 from kornilova-l/extern-variables
Generate extern variable declarations
2 parents 920d23e + 4344a39 commit c1c1852

File tree

9 files changed

+97
-1
lines changed

9 files changed

+97
-1
lines changed

bindgen/ir/IR.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
9595
s << *typeDef;
9696
}
9797

98+
for (const auto &variable : ir.variables) {
99+
s << *variable;
100+
}
101+
98102
for (const auto &varDefine : ir.varDefines) {
99103
s << *varDefine;
100104
}

bindgen/ir/VarDefine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "Define.h"
55
#include "Variable.h"
6-
#include <llvm/Support/raw_ostream.h>
76

87
/**
98
* Stores a pointer to Variable instance from IR

bindgen/ir/Variable.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22

33
Variable::Variable(const std::string &name, std::shared_ptr<Type> type)
44
: TypeAndName(name, type) {}
5+
6+
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Variable &variable) {
7+
s << " val " << variable.getName() << ": " << variable.getType()->str()
8+
<< " = native.extern\n";
9+
return s;
10+
}

bindgen/ir/Variable.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
#define SCALA_NATIVE_BINDGEN_VARIABLE_H
33

44
#include "TypeAndName.h"
5+
#include <llvm/Support/raw_ostream.h>
56

67
class Variable : public TypeAndName {
78
public:
89
Variable(const std::string &name, std::shared_ptr<Type> type);
10+
11+
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
12+
const Variable &variable);
913
};
1014

1115
#endif // SCALA_NATIVE_BINDGEN_VARIABLE_H

tests/samples/Extern.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "Extern.h"
2+
3+
int forty_two = 42;
4+
const char version[] = "0.1.0";
5+
6+
enum mode mode = USER;
7+
8+
struct version semver_instance = {.major = 1, .minor = 2, .patch = 3};
9+
struct version *semver = &semver_instance;

tests/samples/Extern.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extern int forty_two;
2+
extern const char version[];
3+
4+
enum mode { SYSTEM, USER };
5+
extern enum mode mode;
6+
7+
struct version {
8+
int major;
9+
int minor;
10+
int patch;
11+
};
12+
extern struct version *semver;

tests/samples/Extern.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.scalanative.bindgen.samples
2+
3+
import scala.scalanative._
4+
import scala.scalanative.native._
5+
6+
@native.link("bindgentests")
7+
@native.extern
8+
object Extern {
9+
type enum_mode = native.CUnsignedInt
10+
type struct_version = native.CStruct3[native.CInt, native.CInt, native.CInt]
11+
val forty_two: native.CInt = native.extern
12+
val version: native.CString = native.extern
13+
val mode: enum_mode = native.extern
14+
val semver: native.Ptr[struct_version] = native.extern
15+
}
16+
17+
import Extern._
18+
19+
object ExternEnums {
20+
final val enum_mode_SYSTEM: enum_mode = 0.toUInt
21+
final val enum_mode_USER: enum_mode = 1.toUInt
22+
}
23+
24+
object ExternHelpers {
25+
26+
implicit class struct_version_ops(val p: native.Ptr[struct_version]) extends AnyVal {
27+
def major: native.CInt = !p._1
28+
def major_=(value: native.CInt):Unit = !p._1 = value
29+
def minor: native.CInt = !p._2
30+
def minor_=(value: native.CInt):Unit = !p._2 = value
31+
def patch: native.CInt = !p._3
32+
def patch_=(value: native.CInt):Unit = !p._3 = value
33+
}
34+
35+
def struct_version()(implicit z: native.Zone): native.Ptr[struct_version] = native.alloc[struct_version]
36+
}

tests/samples/VarDefine.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import scala.scalanative.native._
66
@native.link("bindgentests")
77
@native.extern
88
object VarDefine {
9+
val a: native.CInt = native.extern
10+
val constInt: native.CInt = native.extern
11+
val constIntPointer: native.Ptr[native.CInt] = native.extern
12+
val c: native.CInt = native.extern
13+
val f: native.CFloat = native.extern
914
@name("a")
1015
val A: native.CInt = native.extern
1116
@name("constInt")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.scalanative.bindgen.samples
2+
3+
import utest._
4+
import scalanative.native._
5+
6+
object ExternTests extends TestSuite {
7+
val tests = Tests {
8+
'forty_two - {
9+
assert(Extern.forty_two == 42)
10+
}
11+
12+
'mode - {
13+
assert(Extern.mode == ExternEnums.enum_mode_USER)
14+
}
15+
16+
'semver - {
17+
import ExternHelpers._
18+
assert(Extern.semver.major == 1 && Extern.semver.minor == 2 && Extern.semver.patch == 3)
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)