Skip to content

Commit 07a9599

Browse files
author
Joshua Warner
committed
generate new setters in type-generator
1 parent ed1f34d commit 07a9599

File tree

1 file changed

+86
-16
lines changed

1 file changed

+86
-16
lines changed

src/tools/type-generator/main.cpp

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ std::string enumName(Module& module, Field& f) {
216216
std::string& type = f.typeName;
217217
if (type == "void*") {
218218
return "word";
219-
}
220-
if(f.javaSpec.size() != 0 && (f.javaSpec[0] == 'L' || f.javaSpec[0] == '[')) {
219+
} else if(type == "maybe_object") {
220+
return "uintptr_t";
221+
} else if(f.javaSpec.size() != 0 && (f.javaSpec[0] == 'L' || f.javaSpec[0] == '[')) {
221222
return "object";
222223
}
223224
std::map<std::string, Class*>::iterator it = module.classes.find(f.typeName);
@@ -370,7 +371,7 @@ unsigned
370371
sizeOf(Module& module, const std::string& type)
371372
{
372373
if (type == "object"
373-
or type == "intptr_t" or type == "uintptr_t")
374+
or type == "intptr_t" or type == "uintptr_t" or type == "maybe_object")
374375
{
375376
return BytesPerWord;
376377
} else if (type == "unsigned" or type == "int") {
@@ -870,6 +871,8 @@ std::string cppFieldType(Module& module, Field& f) {
870871
assert(f.typeName.size() > 0);
871872
if(it != module.classes.end()) {
872873
return cppClassName(it->second);
874+
} else if(f.typeName == "maybe_object") {
875+
return "uintptr_t";
873876
} else {
874877
return f.typeName;
875878
}
@@ -976,15 +979,11 @@ writeConstructorInitializations(Output* out, Class* cl)
976979
for(std::vector<Field*>::iterator it = cl->fields.begin(); it != cl->fields.end(); ++it) {
977980
Field& f = **it;
978981
if(!f.polyfill) {
979-
out->write(" o->");
980-
out->write(obfuscate(f.name));
981-
out->write("(");
982-
if(f.threadParam) {
983-
out->write("t");
984-
}
985-
out->write(") = ");
982+
out->write(" o->set");
983+
out->write(capitalize(f.name));
984+
out->write("(t, ");
986985
out->write(obfuscate(f.name));
987-
out->write(";\n");
986+
out->write(");\n");
988987
}
989988
}
990989
}
@@ -1002,10 +1001,48 @@ void writeClassDeclarations(Output* out, Module& module)
10021001
out->write("\n");
10031002
}
10041003

1004+
bool isFieldGcVisible(Module& module, Field& f) {
1005+
return (f.typeName == "maybe_object" || enumName(module, f) == "object") && !f.nogc;
1006+
}
1007+
10051008
void writeClassAccessors(Output* out, Module& module, Class* cl)
10061009
{
10071010
for(std::vector<Field*>::iterator it = cl->fields.begin(); it != cl->fields.end(); ++it) {
10081011
Field& f = **it;
1012+
1013+
if(!f.polyfill) {
1014+
out->write(" void set");
1015+
out->write(capitalize(f.name));
1016+
out->write("(Thread* t UNUSED, ");
1017+
out->write(cppFieldType(module, f));
1018+
out->write(" value) { ");
1019+
if(isFieldGcVisible(module, f)) {
1020+
out->write("set(t, reinterpret_cast<object>(this), ");
1021+
out->write(capitalize(cl->name));
1022+
out->write(capitalize(f.name));
1023+
out->write(", reinterpret_cast<object>(value));");
1024+
} else {
1025+
out->write("field_at<");
1026+
out->write(cppFieldType(module, f));
1027+
out->write(">(");
1028+
out->write(capitalize(cl->name));
1029+
out->write(capitalize(f.name));
1030+
out->write(") = value;");
1031+
}
1032+
out->write(" }\n");
1033+
1034+
out->write(" ");
1035+
out->write(cppFieldType(module, f));
1036+
out->write("* ");
1037+
out->write(obfuscate(f.name));
1038+
out->write("Ptr() { return &field_at<");
1039+
out->write(cppFieldType(module, f));
1040+
out->write(">(");
1041+
out->write(capitalize(cl->name));
1042+
out->write(capitalize(f.name));
1043+
out->write("); }\n");
1044+
}
1045+
10091046
out->write(" ");
10101047
out->write(cppFieldType(module, f));
10111048
if(!f.polyfill) {
@@ -1032,19 +1069,52 @@ void writeClassAccessors(Output* out, Module& module, Class* cl)
10321069
if(cl->arrayField) {
10331070
Field& f = *cl->arrayField;
10341071
out->write(" avian::util::Slice<");
1035-
out->write(f.typeName);
1072+
// if(f.typeName != "maybe_object" && isFieldGcVisible(module, f)) {
1073+
// out->write("const ");
1074+
// }
1075+
out->write(cppFieldType(module, f));
10361076
out->write("> ");
10371077
out->write(obfuscate(f.name));
10381078
out->write("() { return avian::util::Slice<");
1039-
out->write(f.typeName);
1079+
// if(f.typeName != "maybe_object" && isFieldGcVisible(module, f)) {
1080+
// out->write("const ");
1081+
// }
1082+
out->write(cppFieldType(module, f));
10401083
out->write("> (&field_at<");
1041-
out->write(f.typeName);
1084+
// if(f.typeName != "maybe_object" && isFieldGcVisible(module, f)) {
1085+
// out->write("const ");
1086+
// }
1087+
out->write(cppFieldType(module, f));
10421088
out->write(">(");
10431089
out->write(capitalize(cl->name));
10441090
out->write(capitalize(f.name));
10451091
out->write("), field_at<uintptr_t>(");
10461092
out->write(capitalize(cl->name));
10471093
out->write("Length)); }\n");
1094+
1095+
out->write(" void set");
1096+
out->write(capitalize(f.name));
1097+
out->write("Element(Thread* t UNUSED, size_t index, ");
1098+
out->write(cppFieldType(module, f));
1099+
out->write(" value) { ");
1100+
if(isFieldGcVisible(module, f)) {
1101+
out->write("set(t, reinterpret_cast<object>(this), ");
1102+
out->write(capitalize(cl->name));
1103+
out->write(capitalize(f.name));
1104+
out->write(" + index * (");
1105+
out->write(sizeOf(module, f.typeName));
1106+
out->write("), reinterpret_cast<object>(value));");
1107+
} else {
1108+
out->write("field_at<");
1109+
out->write(cppFieldType(module, f));
1110+
out->write(">(");
1111+
out->write(capitalize(cl->name));
1112+
out->write(capitalize(f.name));
1113+
out->write(" + index * (");
1114+
out->write(sizeOf(module, f.typeName));
1115+
out->write(")) = value;");
1116+
}
1117+
out->write(" }\n");
10481118
}
10491119
}
10501120

@@ -1229,15 +1299,15 @@ typeObjectMask(Module& module, Class* cl)
12291299
for(std::vector<Field*>::iterator it = cl->fields.begin(); it != cl->fields.end(); it++) {
12301300
Field& f = **it;
12311301
unsigned offset = f.offset / BytesPerWord;
1232-
if(enumName(module, f) == "object" && !f.nogc) {
1302+
if(isFieldGcVisible(module, f)) {
12331303
set(&mask, offset);
12341304
}
12351305
}
12361306

12371307
if(cl->arrayField) {
12381308
Field& f = *cl->arrayField;
12391309
unsigned offset = f.offset / BytesPerWord;
1240-
if(enumName(module, f) == "object" && !f.nogc) {
1310+
if(isFieldGcVisible(module, f)) {
12411311
set(&mask, offset);
12421312
}
12431313
}

0 commit comments

Comments
 (0)