Skip to content

Commit 8c4f985

Browse files
authored
Merge pull request #90 from kornilova-l/size-in-bytes
Test types that use native.CArray
2 parents 11d65bd + 11a0a59 commit 8c4f985

File tree

12 files changed

+116
-16
lines changed

12 files changed

+116
-16
lines changed

bindgen/TypeTranslator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe,
132132
if (typeEquals(tpe, avoid)) {
133133
// This is a type that we want to avoid the usage.
134134
// Êxample: A struct that has a pointer to itself
135-
uint64_t size = ctx->getTypeSize(tpe);
135+
uint64_t sizeInBits = ctx->getTypeSize(tpe);
136+
assert(sizeInBits % 8 == 0);
136137
return std::make_shared<ArrayType>(
137-
std::make_shared<PrimitiveType>("Byte"), size);
138+
std::make_shared<PrimitiveType>("Byte"), sizeInBits / 8);
138139
}
139140

140141
if (tpe->isFunctionPointerType()) {

bindgen/ir/IR.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
141141
s << "object " << ir.libName << "Helpers {\n";
142142

143143
for (const auto &st : ir.structs) {
144-
s << "\n" << st->generateHelperClass();
144+
if (st->hasHelperMethods()) {
145+
s << "\n" << st->generateHelperClass();
146+
}
145147
}
146148

147149
for (const auto &u : ir.unions) {

bindgen/ir/Struct.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ std::shared_ptr<TypeDef> Struct::generateTypeDef() {
3737
}
3838

3939
std::string Struct::generateHelperClass() const {
40-
if (!hasHelperMethods()) {
41-
/* struct is empty or represented as an array */
42-
return "";
43-
}
40+
assert(hasHelperMethods());
41+
/* struct is not empty and not represented as an array */
4442
std::stringstream s;
4543
std::string type = getAliasType();
4644
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type

bindgen/visitor/TreeVisitor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
105105
std::vector<Field *> fields;
106106

107107
for (const clang::FieldDecl *field : record->fields()) {
108-
uint64_t sizeInBytes = astContext->getTypeSize(field->getType()) / 8;
109-
maxSize = std::max(maxSize, sizeInBytes);
108+
uint64_t sizeInBits = astContext->getTypeSize(field->getType());
109+
assert(sizeInBits % 8 == 0);
110+
maxSize = std::max(maxSize, sizeInBits / 8);
110111
std::string fname = field->getNameAsString();
111112
std::shared_ptr<Type> ftype =
112113
typeTranslator.translate(field->getType(), &name);
@@ -152,9 +153,10 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
152153
llvm::errs().flush();
153154
}
154155

156+
uint64_t sizeInBits = astContext->getTypeSize(record->getTypeForDecl());
157+
assert(sizeInBits % 8 == 0);
155158
std::shared_ptr<Type> alias =
156-
ir.addStruct(name, std::move(fields),
157-
astContext->getTypeSize(record->getTypeForDecl()));
159+
ir.addStruct(name, std::move(fields), sizeInBits / 8);
158160

159161
typeTranslator.addAlias("struct " + name, alias);
160162
}

tests/samples/Struct.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ point_s getPoint() {
77
point->y = 20;
88
return point;
99
}
10+
11+
int getBigStructSize() { return sizeof(struct bigStruct); }
12+
13+
char getCharFromAnonymousStruct(struct structWithAnonymousStruct *s) {
14+
return s->anonymousStruct.c;
15+
}
16+
17+
char getIntFromAnonymousStruct(struct structWithAnonymousStruct *s) {
18+
return s->anonymousStruct.i;
19+
}

tests/samples/Struct.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,43 @@ struct point {
66
typedef struct point *point_s;
77

88
point_s getPoint();
9+
10+
struct bigStruct {
11+
long one;
12+
char two;
13+
int three;
14+
float four;
15+
double five;
16+
point_s six;
17+
int seven;
18+
int eight;
19+
int nine;
20+
int ten;
21+
int eleven;
22+
int twelve;
23+
int thirteen;
24+
int fourteen;
25+
int fifteen;
26+
int sixteen;
27+
int seventeen;
28+
int eighteen;
29+
int nineteen;
30+
int twenty;
31+
int twentyOne;
32+
int twentyTwo;
33+
int twentyThree;
34+
};
35+
36+
int getBigStructSize();
37+
38+
struct structWithAnonymousStruct {
39+
int a;
40+
struct {
41+
char c;
42+
int i;
43+
} anonymousStruct;
44+
};
45+
46+
char getCharFromAnonymousStruct(struct structWithAnonymousStruct *s);
47+
48+
char getIntFromAnonymousStruct(struct structWithAnonymousStruct *s);

tests/samples/Struct.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import scala.scalanative.native._
88
object Struct {
99
type struct_point = native.CStruct2[native.CInt, native.CInt]
1010
type point_s = native.Ptr[struct_point]
11+
type struct_bigStruct = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat.Digit[native.Nat._1, native.Nat._2]]]
12+
type struct_structWithAnonymousStruct = native.CStruct2[native.CInt, native.CArray[Byte, native.Nat._8]]
1113
def getPoint(): native.Ptr[struct_point] = native.extern
14+
def getBigStructSize(): native.CInt = native.extern
15+
def getCharFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
16+
def getIntFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
1217
}
1318

1419
import Struct._
@@ -23,4 +28,13 @@ object StructHelpers {
2328
}
2429

2530
def struct_point()(implicit z: native.Zone): native.Ptr[struct_point] = native.alloc[struct_point]
31+
32+
implicit class struct_structWithAnonymousStruct_ops(val p: native.Ptr[struct_structWithAnonymousStruct]) extends AnyVal {
33+
def a: native.CInt = !p._1
34+
def a_=(value: native.CInt):Unit = !p._1 = value
35+
def anonymousStruct: native.CArray[Byte, native.Nat._8] = !p._2
36+
def anonymousStruct_=(value: native.CArray[Byte, native.Nat._8]):Unit = !p._2 = value
37+
}
38+
39+
def struct_structWithAnonymousStruct()(implicit z: native.Zone): native.Ptr[struct_structWithAnonymousStruct] = native.alloc[struct_structWithAnonymousStruct]
2640
}

tests/samples/Union.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
void setIntValue(union values *v) { v->i = 10; }
55

66
void setLongValue(union values *v) { v->l = 10000000000; }
7+
8+
int getUnionSize() { return sizeof(union values); }

tests/samples/Union.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ union values {
77
void setIntValue(union values *v);
88

99
void setLongValue(union values *v);
10+
11+
int getUnionSize();

tests/samples/Union.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ object Union {
99
type union_values = native.CArray[Byte, native.Nat._8]
1010
def setIntValue(v: native.Ptr[union_values]): Unit = native.extern
1111
def setLongValue(v: native.Ptr[union_values]): Unit = native.extern
12+
def getUnionSize(): native.CInt = native.extern
1213
}
1314

1415
import Union._

0 commit comments

Comments
 (0)