1
1
#include " Struct.h"
2
2
#include " ../Utils.h"
3
3
#include " types/ArrayType.h"
4
+ #include " types/PointerType.h"
4
5
#include " types/PrimitiveType.h"
5
6
#include < sstream>
6
7
7
8
Field::Field (std::string name, std::shared_ptr<Type> type)
8
9
: TypeAndName(std::move(name), std::move(type)) {}
9
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) {}
13
+
14
+ uint64_t Field::getOffset () const { return offset; }
15
+
10
16
StructOrUnion::StructOrUnion (std::string name,
11
17
std::vector<std::shared_ptr<Field>> fields,
12
18
std::shared_ptr<Location> location)
@@ -41,6 +47,8 @@ std::shared_ptr<Location> StructOrUnion::getLocation() const {
41
47
return location;
42
48
}
43
49
50
+ bool StructOrUnion::hasHelperMethods () const { return !fields.empty (); }
51
+
44
52
Struct::Struct (std::string name, std::vector<std::shared_ptr<Field>> fields,
45
53
uint64_t typeSize, std::shared_ptr<Location> location)
46
54
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
@@ -64,19 +72,15 @@ std::shared_ptr<TypeDef> Struct::generateTypeDef() {
64
72
65
73
std::string Struct::generateHelperClass () const {
66
74
assert (hasHelperMethods ());
67
- /* struct is not empty and not represented as an array */
68
75
std::stringstream s;
69
76
std::string type = getTypeAlias ();
70
77
s << " implicit class " << type << " _ops(val p: native.Ptr[" << type
71
78
<< " ])"
72
79
<< " extends AnyVal {\n " ;
73
- unsigned fieldIndex = 0 ;
74
- for (const auto &field : fields) {
75
- if (!field->getName ().empty ()) {
76
- s << generateGetter (fieldIndex) << " \n " ;
77
- s << generateSetter (fieldIndex) << " \n " ;
78
- }
79
- fieldIndex++;
80
+ if (fields.size () <= SCALA_NATIVE_MAX_STRUCT_FIELDS) {
81
+ s << generateHelperClassMethodsForStructRepresentation ();
82
+ } else {
83
+ s << generateHelperClassMethodsForArrayRepresentation ();
80
84
}
81
85
s << " }\n\n " ;
82
86
@@ -88,8 +92,26 @@ std::string Struct::generateHelperClass() const {
88
92
return s.str ();
89
93
}
90
94
91
- bool Struct::hasHelperMethods () const {
92
- return !fields.empty () && fields.size () < SCALA_NATIVE_MAX_STRUCT_FIELDS;
95
+ std::string Struct::generateHelperClassMethodsForStructRepresentation () const {
96
+ std::stringstream s;
97
+ for (unsigned fieldIndex = 0 ; fieldIndex < fields.size (); fieldIndex++) {
98
+ if (!fields[fieldIndex]->getName ().empty ()) {
99
+ s << generateGetterForStructRepresentation (fieldIndex);
100
+ s << generateSetterForStructRepresentation (fieldIndex);
101
+ }
102
+ }
103
+ return s.str ();
104
+ }
105
+
106
+ std::string Struct::generateHelperClassMethodsForArrayRepresentation () const {
107
+ std::stringstream s;
108
+ for (unsigned fieldIndex = 0 ; fieldIndex < fields.size (); fieldIndex++) {
109
+ if (!fields[fieldIndex]->getName ().empty ()) {
110
+ s << generateGetterForArrayRepresentation (fieldIndex);
111
+ s << generateSetterForArrayRepresentation (fieldIndex);
112
+ }
113
+ }
114
+ return s.str ();
93
115
}
94
116
95
117
std::string Struct::getTypeAlias () const { return " struct_" + name; }
@@ -127,7 +149,8 @@ bool Struct::operator==(const Type &other) const {
127
149
return false ;
128
150
}
129
151
130
- std::string Struct::generateSetter (unsigned fieldIndex) const {
152
+ std::string
153
+ Struct::generateSetterForStructRepresentation (unsigned fieldIndex) const {
131
154
std::shared_ptr<Field> field = fields[fieldIndex];
132
155
std::string setter = handleReservedWords (field->getName (), " _=" );
133
156
std::string parameterType = field->getType ()->str ();
@@ -139,11 +162,12 @@ std::string Struct::generateSetter(unsigned fieldIndex) const {
139
162
}
140
163
std::stringstream s;
141
164
s << " def " << setter << " (value: " + parameterType + " ): Unit = !p._"
142
- << std::to_string (fieldIndex + 1 ) << " = " << value;
165
+ << std::to_string (fieldIndex + 1 ) << " = " << value << " \n " ;
143
166
return s.str ();
144
167
}
145
168
146
- std::string Struct::generateGetter (unsigned fieldIndex) const {
169
+ std::string
170
+ Struct::generateGetterForStructRepresentation (unsigned fieldIndex) const {
147
171
std::shared_ptr<Field> field = fields[fieldIndex];
148
172
std::string getter = handleReservedWords (field->getName ());
149
173
std::string returnType = field->getType ()->str ();
@@ -156,10 +180,45 @@ std::string Struct::generateGetter(unsigned fieldIndex) const {
156
180
methodBody = " !p._" + std::to_string (fieldIndex + 1 );
157
181
}
158
182
std::stringstream s;
159
- s << " def " << getter << " : " << returnType << " = " << methodBody;
183
+ s << " def " << getter << " : " << returnType << " = " << methodBody
184
+ << " \n " ;
160
185
return s.str ();
161
186
}
162
187
188
+ std::string
189
+ Struct::generateSetterForArrayRepresentation (unsigned int fieldIndex) const {
190
+ return std::string ();
191
+ }
192
+
193
+ std::string
194
+ Struct::generateGetterForArrayRepresentation (unsigned fieldIndex) const {
195
+ std::shared_ptr<Field> field = fields[fieldIndex];
196
+ std::string getter = handleReservedWords (field->getName ());
197
+ std::string returnType;
198
+ std::string methodBody;
199
+
200
+ PointerType pointerToFieldType = PointerType (field->getType ());
201
+ if (field->getOffset () != 0 ) {
202
+ methodBody = " (p._1 + " + std::to_string (field->getOffset ()) + " )" ;
203
+ } else {
204
+ methodBody = " p._1" ;
205
+ }
206
+ methodBody = methodBody + " .cast[" + pointerToFieldType.str () + " ]" ;
207
+
208
+ if (isAliasForType<ArrayType>(field->getType ().get ()) ||
209
+ isAliasForType<Struct>(field->getType ().get ())) {
210
+ returnType = pointerToFieldType.str ();
211
+ } else {
212
+ methodBody = " !" + methodBody;
213
+ returnType = field->getType ()->str ();
214
+ }
215
+ std::stringstream s;
216
+ s << " def " << getter << " : " << returnType << " = " << methodBody
217
+ << " \n " ;
218
+ return s.str ();
219
+ return " " ;
220
+ }
221
+
163
222
Union::Union (std::string name, std::vector<std::shared_ptr<Field>> fields,
164
223
uint64_t maxSize, std::shared_ptr<Location> location)
165
224
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
@@ -171,6 +230,7 @@ std::shared_ptr<TypeDef> Union::generateTypeDef() {
171
230
}
172
231
173
232
std::string Union::generateHelperClass () const {
233
+ assert (hasHelperMethods ());
174
234
std::stringstream s;
175
235
std::string type = getTypeAlias ();
176
236
s << " implicit class " << type << " _pos"
0 commit comments