8
8
Field::Field (std::string name, std::shared_ptr<Type> type)
9
9
: TypeAndName(std::move(name), std::move(type)) {}
10
10
11
- Field::Field (std::string name, std::shared_ptr<Type> type, uint64_t offset)
12
- : TypeAndName(std::move(name), std::move(type)), offset(offset) {}
11
+ Field::Field (std::string name, std::shared_ptr<Type> type,
12
+ uint64_t offsetInBits)
13
+ : TypeAndName(std::move(name), std::move(type)),
14
+ offsetInBits(offsetInBits) {}
13
15
14
- uint64_t Field::getOffset () const { return offset ; }
16
+ uint64_t Field::getOffsetInBits () const { return offsetInBits ; }
15
17
16
18
StructOrUnion::StructOrUnion (std::string name,
17
19
std::vector<std::shared_ptr<Field>> fields,
@@ -34,7 +36,7 @@ Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
34
36
typeSize(typeSize), isPacked(isPacked) {}
35
37
36
38
std::shared_ptr<TypeDef> Struct::generateTypeDef () {
37
- if (fields. size () < SCALA_NATIVE_MAX_STRUCT_FIELDS ) {
39
+ if (isRepresentedAsStruct () ) {
38
40
return std::make_shared<TypeDef>(getTypeAlias (), shared_from_this (),
39
41
nullptr );
40
42
} else {
@@ -56,7 +58,7 @@ std::string Struct::generateHelperClass() const {
56
58
s << " implicit class " << type << " _ops(val p: native.Ptr[" << type
57
59
<< " ])"
58
60
<< " extends AnyVal {\n " ;
59
- if (fields. size () <= SCALA_NATIVE_MAX_STRUCT_FIELDS ) {
61
+ if (isRepresentedAsStruct () ) {
60
62
s << generateHelperClassMethodsForStructRepresentation ();
61
63
} else {
62
64
s << generateHelperClassMethodsForArrayRepresentation ();
@@ -72,6 +74,9 @@ std::string Struct::generateHelperClass() const {
72
74
}
73
75
74
76
bool Struct::hasHelperMethods () const {
77
+ if (isBitField ()) {
78
+ return false ;
79
+ }
75
80
if (!isRepresentedAsStruct ()) {
76
81
return !fields.empty ();
77
82
}
@@ -176,7 +181,7 @@ Struct::generateGetterForStructRepresentation(unsigned fieldIndex) const {
176
181
}
177
182
178
183
bool Struct::isRepresentedAsStruct () const {
179
- return fields.size () <= SCALA_NATIVE_MAX_STRUCT_FIELDS;
184
+ return fields.size () <= SCALA_NATIVE_MAX_STRUCT_FIELDS && ! isBitField () ;
180
185
}
181
186
182
187
std::string
@@ -188,9 +193,10 @@ Struct::generateSetterForArrayRepresentation(unsigned int fieldIndex) const {
188
193
std::string castedField = " p._1" ;
189
194
190
195
PointerType pointerToFieldType = PointerType (field->getType ());
191
- if (field->getOffset () > 0 ) {
192
- castedField = " (" + castedField + " + " +
193
- std::to_string (field->getOffset ()) + " )" ;
196
+ uint64_t offsetInBytes = field->getOffsetInBits () / 8 ;
197
+ if (offsetInBytes > 0 ) {
198
+ castedField =
199
+ " (" + castedField + " + " + std::to_string (offsetInBytes) + " )" ;
194
200
}
195
201
castedField = " !" + castedField + " .cast[" + pointerToFieldType.str () + " ]" ;
196
202
if (isAliasForType<ArrayType>(field->getType ().get ()) ||
@@ -215,8 +221,9 @@ Struct::generateGetterForArrayRepresentation(unsigned fieldIndex) const {
215
221
std::string methodBody;
216
222
217
223
PointerType pointerToFieldType = PointerType (field->getType ());
218
- if (field->getOffset () > 0 ) {
219
- methodBody = " (p._1 + " + std::to_string (field->getOffset ()) + " )" ;
224
+ uint64_t offsetInBytes = field->getOffsetInBits () / 8 ;
225
+ if (offsetInBytes > 0 ) {
226
+ methodBody = " (p._1 + " + std::to_string (offsetInBytes) + " )" ;
220
227
} else {
221
228
methodBody = " p._1" ;
222
229
}
@@ -233,7 +240,16 @@ Struct::generateGetterForArrayRepresentation(unsigned fieldIndex) const {
233
240
s << " def " << getter << " : " << returnType << " = " << methodBody
234
241
<< " \n " ;
235
242
return s.str ();
236
- return " " ;
243
+ }
244
+
245
+ bool Struct::isBitField () const {
246
+ // TODO: find proper way to check it
247
+ for (const auto &field : fields) {
248
+ if (field->getOffsetInBits () % 8 != 0 ) {
249
+ return true ;
250
+ }
251
+ }
252
+ return false ;
237
253
}
238
254
239
255
Union::Union (std::string name, std::vector<std::shared_ptr<Field>> fields,
0 commit comments